Skip to content

Commit 6efaa8d

Browse files
committed
reduce complexity
1 parent ee511aa commit 6efaa8d

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

classes/moodleoverflow.php

+19-12
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,12 @@ public static function get_events(): array {
7272

7373
if ($event->eventtype == 'post') {
7474
// Add an anonymous attribute.
75-
if ($event->anonymoussetting == anonymous::EVERYTHING_ANONYMOUS) {
76-
$event->anonymous = true;
77-
} else if ($event->anonymoussetting == anonymous::QUESTION_ANONYMOUS) {
78-
$event->anonymous = $event->postuserid == $event->discussionuserid;
79-
} else {
80-
$event->anonymous = false;
81-
}
75+
$event->anonymous = self::is_post_anonymous($event);
8276

8377
// If the post is anonymous, make the author anonymous.
8478
if ($event->anonymous) {
8579
$event->postuserfirstname = 'anonymous';
8680
$event->postuserlastname = '';
87-
$event->postuserid = -1;
8881
}
8982

9083
// Add links.
@@ -94,7 +87,6 @@ public static function get_events(): array {
9487
new moodle_url('/user/view.php', ['id' => $event->postuserid]);
9588
}
9689
}
97-
9890
return $moodleoverflowevents;
9991
}
10092

@@ -129,7 +121,8 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a
129121
posts.parent AS postparentid,
130122
posts.userid AS postuserid,
131123
posts.created AS timestart,
132-
posts.message AS postmessage
124+
posts.message AS content,
125+
posts.messageformat AS postmessageformat
133126
FROM {moodleoverflow_posts} posts
134127
JOIN {moodleoverflow_discussions} discuss ON discuss.id = posts.discussion
135128
JOIN {moodleoverflow} module ON module.id = discuss.moodleoverflow
@@ -160,8 +153,9 @@ private static function get_other_events_from_db($courses, $timestart, $timeend)
160153
$params = ['timestart' => $timestart, 'timeduration' => $timestart,
161154
'timeend' => $timeend, 'courses' => $courses, ] + $inparamscourses;
162155
// Set the sql statement.
163-
$sql = "SELECT e.id, e.name, mo.name AS instancename, e.courseid, cm.id AS coursemoduleid, cm.availability AS availability,
164-
e.groupid, e.userid, e.modulename, e.instance, e.eventtype, e.timestart, e.timemodified, e.visible
156+
$sql = "SELECT e.id, e.name AS content, mo.name AS instancename, e.courseid, cm.id AS coursemoduleid,
157+
cm.availability AS availability, e.groupid, e.userid, e.modulename, e.instance, e.eventtype, e.timestart,
158+
e.timemodified, e.visible
165159
FROM {event} e
166160
JOIN {modules} m ON e.modulename = m.name
167161
JOIN {course_modules} cm ON (cm.course = e.courseid AND cm.module = m.id AND cm.instance = e.instance)
@@ -179,4 +173,17 @@ private static function get_other_events_from_db($courses, $timestart, $timeend)
179173
return $DB->get_records_sql($sql, $params);
180174
}
181175

176+
/**
177+
* Helper function to check if a post is anonymous. Helps to reduce the cyclomatic complexity.
178+
* @param $event
179+
* @return bool
180+
*/
181+
private static function is_post_anonymous($event) {
182+
if ($event->anonymoussetting == anonymous::EVERYTHING_ANONYMOUS) {
183+
return true;
184+
} else if ($event->anonymoussetting == anonymous::QUESTION_ANONYMOUS) {
185+
return $event->postuserid == $event->discussionuserid;
186+
}
187+
return false;
188+
}
182189
}

0 commit comments

Comments
 (0)