From 9db7f1ca8556edb0e932dff16331ad6ded5b472e Mon Sep 17 00:00:00 2001 From: Lai Wei Date: Wed, 17 May 2023 15:48:19 +0100 Subject: [PATCH] reduce debugging messages from observer functions on notification sent event --- classes/observers.php | 41 +++++++++++++++++++++++++++++++---------- classes/task/bot.php | 24 +++++++++++++++++++----- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/classes/observers.php b/classes/observers.php index 2e8083cb8..8d69b5e21 100644 --- a/classes/observers.php +++ b/classes/observers.php @@ -728,6 +728,9 @@ public static function handle_user_deleted(user_deleted $event) : bool { public static function handle_notification_sent(notification_sent $event) : bool { global $CFG, $DB, $PAGE; + $debugmode = get_config('local_o365', 'debugmode'); + $debuggingon = !PHPUNIT_TEST && !defined('BEHAT_SITE_RUNNING') && $debugmode; + // Check if we have the configuration to send proactive notifications. $aadtenant = get_config('local_o365', 'aadtenant'); $botappid = get_config('local_o365', 'bot_app_id'); @@ -735,7 +738,7 @@ public static function handle_notification_sent(notification_sent $event) : bool $notificationendpoint = get_config('local_o365', 'bot_webhook_endpoint'); if (empty($aadtenant) || empty($botappid) || empty($botappsecret) || empty($notificationendpoint)) { // Incomplete settings, exit. - if (!PHPUNIT_TEST && !defined('BEHAT_SITE_RUNNING')) { + if ($debuggingon) { debugging('SKIPPED: handle_notification_sent - incomplete settings', DEBUG_DEVELOPER); } return true; @@ -745,20 +748,26 @@ public static function handle_notification_sent(notification_sent $event) : bool $notification = $DB->get_record('notifications', ['id' => $notificationid]); if (!$notification) { // Notification cannot be found, exit. - debugging('SKIPPED: handle_notification_sent - notification cannot be found', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - notification cannot be found', DEBUG_DEVELOPER); + } return true; } $user = $DB->get_record('user', ['id' => $notification->useridto]); if (!$user) { // Recipient user invalid, exit. - debugging('SKIPPED: handle_notification_sent - recipient user invalid', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - recipient user invalid', DEBUG_DEVELOPER); + } return true; } if ($user->auth != 'oidc') { // Recipient user is not Microsoft 365 user, exit. - debugging('SKIPPED: handle_notification_sent - recipient user is not Microsoft 365 user', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - recipient user is not Microsoft 365 user', DEBUG_DEVELOPER); + } return true; } @@ -766,26 +775,34 @@ public static function handle_notification_sent(notification_sent $event) : bool $userrecord = $DB->get_record('local_o365_objects', ['type' => 'user', 'moodleid' => $user->id]); if (!$userrecord) { // Recipient user doesn't have an object ID, exit. - debugging('SKIPPED: handle_notification_sent - recipient user doesn\'t have an object ID', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - recipient user doesn\'t have an object ID', DEBUG_DEVELOPER); + } return true; } // Get course object record. if (!array_key_exists('courseid', $event->other)) { // Course doesn't exist, exit. - debugging('SKIPPED: handle_notification_sent - course doesn\'t exist', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - course doesn\'t exist', DEBUG_DEVELOPER); + } return true; } $courseid = $event->other['courseid']; if (!$courseid || $courseid == SITEID) { // Invalid course id, exit. - debugging('SKIPPED: handle_notification_sent - invalid course id', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - invalid course id', DEBUG_DEVELOPER); + } return true; } $course = $DB->get_record('course', ['id' => $courseid]); if (!$course) { // Invalid course, exit. - debugging('SKIPPED: handle_notification_sent - invalid course id', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - invalid course id', DEBUG_DEVELOPER); + } return true; } @@ -794,7 +811,9 @@ public static function handle_notification_sent(notification_sent $event) : bool ['type' => 'group', 'subtype' => 'course', 'moodleid' => $courseid]); if (!$courserecord) { // Course record doesn't have an object ID, exit. - debugging('SKIPPED: handle_notification_sent - course record doesn\'t have an object ID', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - course record doesn\'t have an object ID', DEBUG_DEVELOPER); + } return true; } else { $courseobjectid = $courserecord->objectid; @@ -804,7 +823,9 @@ public static function handle_notification_sent(notification_sent $event) : bool $botframework = new botframework(); if (!$botframework->has_token()) { // Cannot get token, exit. - debugging('SKIPPED: handle_notification_sent - cannot get token from bot framework', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - cannot get token from bot framework', DEBUG_DEVELOPER); + } return true; } diff --git a/classes/task/bot.php b/classes/task/bot.php index b81d0890f..3353de189 100644 --- a/classes/task/bot.php +++ b/classes/task/bot.php @@ -62,6 +62,9 @@ public function execute() { return false; } + $debugmode = get_config('local_o365', 'debugmode'); + $debuggingon = !PHPUNIT_TEST && !defined('BEHAT_SITE_RUNNING') && $debugmode; + // Get all courses to be included. $sql = 'SELECT crs.id, crs.id AS id2 FROM {course} crs @@ -78,14 +81,18 @@ public function execute() { $botframework = new \local_o365\rest\botframework(); if (!$botframework->has_token()) { // Cannot get token, exit. - debugging('SKIPPED: handle_notification_sent - cannot get token from bot framework', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - cannot get token from bot framework', DEBUG_DEVELOPER); + } return true; } $this->botframework = $botframework; $notificationendpoint = get_config('local_o365', 'bot_webhook_endpoint'); if (empty($notificationendpoint)) { - debugging('SKIPPED: handle_notification_sent - incomplete settings, bot_webhook_endpoint empty', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - incomplete settings, bot_webhook_endpoint empty', DEBUG_DEVELOPER); + } return true; } @@ -121,6 +128,9 @@ private function assignment_past_due_date() { $assignments = $DB->get_records_sql($sql, $params); mtrace(count($assignments). ' not processed assignment(s) after due date found.'); + $debugmode = get_config('local_o365', 'debugmode'); + $debuggingon = !PHPUNIT_TEST && !defined('BEHAT_SITE_RUNNING') && $debugmode; + require_once($CFG->dirroot . '/mod/assign/locallib.php'); foreach ($assignments as $assignment) { [$course, $cm] = get_course_and_cm_from_instance($assignment->id, 'assign'); @@ -130,7 +140,9 @@ private function assignment_past_due_date() { ['type' => 'group', 'subtype' => 'course', 'moodleid' => $course->id]); if (!$o365course) { // Course record doesn't have an object ID can't send for this assignment. - debugging('SKIPPED: handle_notification_sent - course record doesn\'t have an object ID', DEBUG_DEVELOPER); + if ($debuggingon) { + debugging('SKIPPED: handle_notification_sent - course record doesn\'t have an object ID', DEBUG_DEVELOPER); + } continue; } @@ -152,8 +164,10 @@ private function assignment_past_due_date() { foreach ($tonotify as $iduser) { $o365user = $DB->get_record('local_o365_objects', ['type' => 'user', 'moodleid' => $iduser]); if (!$o365user) { - debugging("SKIPPED: handle_notification_sent - recipient user ID:$iduser doesn\'t have an object ID", - DEBUG_DEVELOPER); + if ($debuggingon) { + debugging("SKIPPED: handle_notification_sent - recipient user ID:$iduser doesn\'t have an object ID", + DEBUG_DEVELOPER); + } continue; }