From 6efaa8ddd073960b083221321cb95a68142f843b Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Thu, 15 Aug 2024 14:19:11 -0700 Subject: [PATCH] reduce complexity --- classes/moodleoverflow.php | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/classes/moodleoverflow.php b/classes/moodleoverflow.php index 88a1c7e..1d82f85 100644 --- a/classes/moodleoverflow.php +++ b/classes/moodleoverflow.php @@ -72,19 +72,12 @@ public static function get_events(): array { if ($event->eventtype == 'post') { // Add an anonymous attribute. - if ($event->anonymoussetting == anonymous::EVERYTHING_ANONYMOUS) { - $event->anonymous = true; - } else if ($event->anonymoussetting == anonymous::QUESTION_ANONYMOUS) { - $event->anonymous = $event->postuserid == $event->discussionuserid; - } else { - $event->anonymous = false; - } + $event->anonymous = self::is_post_anonymous($event); // If the post is anonymous, make the author anonymous. if ($event->anonymous) { $event->postuserfirstname = 'anonymous'; $event->postuserlastname = ''; - $event->postuserid = -1; } // Add links. @@ -94,7 +87,6 @@ public static function get_events(): array { new moodle_url('/user/view.php', ['id' => $event->postuserid]); } } - return $moodleoverflowevents; } @@ -129,7 +121,8 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a posts.parent AS postparentid, posts.userid AS postuserid, posts.created AS timestart, - posts.message AS postmessage + posts.message AS content, + posts.messageformat AS postmessageformat FROM {moodleoverflow_posts} posts JOIN {moodleoverflow_discussions} discuss ON discuss.id = posts.discussion JOIN {moodleoverflow} module ON module.id = discuss.moodleoverflow @@ -160,8 +153,9 @@ private static function get_other_events_from_db($courses, $timestart, $timeend) $params = ['timestart' => $timestart, 'timeduration' => $timestart, 'timeend' => $timeend, 'courses' => $courses, ] + $inparamscourses; // Set the sql statement. - $sql = "SELECT e.id, e.name, mo.name AS instancename, e.courseid, cm.id AS coursemoduleid, cm.availability AS availability, - e.groupid, e.userid, e.modulename, e.instance, e.eventtype, e.timestart, e.timemodified, e.visible + $sql = "SELECT e.id, e.name AS content, mo.name AS instancename, e.courseid, cm.id AS coursemoduleid, + cm.availability AS availability, e.groupid, e.userid, e.modulename, e.instance, e.eventtype, e.timestart, + e.timemodified, e.visible FROM {event} e JOIN {modules} m ON e.modulename = m.name 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) return $DB->get_records_sql($sql, $params); } + /** + * Helper function to check if a post is anonymous. Helps to reduce the cyclomatic complexity. + * @param $event + * @return bool + */ + private static function is_post_anonymous($event) { + if ($event->anonymoussetting == anonymous::EVERYTHING_ANONYMOUS) { + return true; + } else if ($event->anonymoussetting == anonymous::QUESTION_ANONYMOUS) { + return $event->postuserid == $event->discussionuserid; + } + return false; + } }