Skip to content

Commit dcedfa0

Browse files
committed
add core moodleoverflow events
1 parent 3c3536d commit dcedfa0

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

classes/moodleoverflow.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,35 @@ class moodleoverflow implements townsquaresupportinterface {
4646
public static function get_events(): array {
4747
global $DB;
4848

49-
$courses = townsquare_get_courses();
50-
$timestart = townsquare_get_timestart();
51-
5249
// If moodleoverflow is not installed or not activated, return empty array.
5350
if (!$DB->get_record('modules', ['name' => 'forum', 'visible' => 1])) {
5451
return [];
5552
}
5653

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();
5958

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]);
6474
}
6575
}
66-
return $moodleoverflowposts;
76+
77+
return $moodleoverflowevents;
6778
}
6879

6980
private static function get_moodleoverflowposts_from_db($courses, $timestart): array {
@@ -106,4 +117,30 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a
106117
return $DB->get_records_sql($sql, $params);
107118
}
108119

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+
109146
}

0 commit comments

Comments
 (0)