@@ -46,24 +46,35 @@ class moodleoverflow implements townsquaresupportinterface {
46
46
public static function get_events (): array {
47
47
global $ DB ;
48
48
49
- $ courses = townsquare_get_courses ();
50
- $ timestart = townsquare_get_timestart ();
51
-
52
49
// If moodleoverflow is not installed or not activated, return empty array.
53
50
if (!$ DB ->get_record ('modules ' , ['name ' => 'forum ' , 'visible ' => 1 ])) {
54
51
return [];
55
52
}
56
53
57
- // Get posts from the database.
58
- $ moodleoverflowposts = self ::get_moodleoverflowposts_from_db ($ courses , $ timestart );
54
+ // Get important parameters directly from townsquare.
55
+ $ courses = townsquare_get_courses ();
56
+ $ timestart = townsquare_get_timestart ();
57
+ $ timeend = townsquare_get_timeend ();
59
58
60
- // Filter posts by availability.
61
- foreach ($ moodleoverflowposts as $ post ) {
62
- if (townsquare_filter_availability ($ post )) {
63
- unset($ moodleoverflowposts [$ post ->row_num ]);
59
+ // Get all moodleoverflow posts and events.
60
+ $ moodleoverflowevents = array_merge ( self ::get_moodleoverflowposts_from_db ($ courses , $ timestart ),
61
+ self ::get_other_events_from_db ($ courses , $ timestart , $ timeend ));
62
+
63
+ // Filter out events that are irrelevant for the user.
64
+ // Irrelevant are events/posts from unavailable moodleoverflows or activity completion notifications, that are completed.
65
+ foreach ($ moodleoverflowevents as $ key => $ event ) {
66
+ if ( (townsquare_filter_availability ($ event )) ||
67
+ ($ event ->eventtype == 'expectcompletionon ' && townsquare_filter_activitycompletions ($ event ))) {
68
+ unset($ moodleoverflowevents [$ key ]);
69
+ }
70
+
71
+ // Add the name of the instance to events.
72
+ if ($ event ->eventtype != 'post ' ) {
73
+ $ event ->instancename = $ DB ->get_field ($ event ->modulename , 'name ' , ['id ' => $ event ->instance ]);
64
74
}
65
75
}
66
- return $ moodleoverflowposts ;
76
+
77
+ return $ moodleoverflowevents ;
67
78
}
68
79
69
80
private static function get_moodleoverflowposts_from_db ($ courses , $ timestart ): array {
@@ -106,4 +117,30 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a
106
117
return $ DB ->get_records_sql ($ sql , $ params );
107
118
}
108
119
120
+ private static function get_other_events_from_db ($ courses , $ timestart , $ timeend ): array {
121
+ global $ DB ;
122
+ // Prepare params for sql statement.
123
+ list ($ insqlcourses , $ inparamscourses ) = $ DB ->get_in_or_equal ($ courses , SQL_PARAMS_NAMED );
124
+
125
+ $ params = ['timestart ' => $ timestart , 'timeduration ' => $ timestart ,
126
+ 'timeend ' => $ timeend , 'courses ' => $ courses ] + $ inparamscourses ;
127
+ // Set the sql statement.
128
+ $ sql = "SELECT e.id, e.name, e.courseid, cm.id AS coursemoduleid, cm.availability AS availability, e.groupid, e.userid,
129
+ e.modulename, e.instance, e.eventtype, e.timestart, e.timemodified, e.visible
130
+ FROM {event} e
131
+ JOIN {modules} m ON e.modulename = m.name
132
+ JOIN {course_modules} cm ON (cm.course = e.courseid AND cm.module = m.id AND cm.instance = e.instance)
133
+ WHERE (e.timestart >= :timestart OR e.timestart+e.timeduration > :timeduration)
134
+ AND e.timestart <= :timeend
135
+ AND e.courseid $ insqlcourses
136
+ AND e.modulename = 'moodleoverflow'
137
+ AND m.visible = 1
138
+ AND (e.name NOT LIKE ' " .'0 ' . "' AND e.eventtype NOT LIKE ' " .'0 ' . "' )
139
+ AND (e.instance <> 0 AND e.visible = 1)
140
+ ORDER BY e.timestart DESC " ;
141
+
142
+ // Get all events.
143
+ return $ DB ->get_records_sql ($ sql , $ params );
144
+ }
145
+
109
146
}
0 commit comments