Skip to content

Commit

Permalink
Merge branch 'wip-91296-m402' into MOODLE_402_STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed May 17, 2023
2 parents 1d7c964 + 9db7f1c commit 3135ecf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
41 changes: 31 additions & 10 deletions classes/observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,17 @@ 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');
$botappsecret = get_config('local_o365', 'bot_app_password');
$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;
Expand All @@ -745,47 +748,61 @@ 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;
}

// Get user object record.
$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;
}

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
24 changes: 19 additions & 5 deletions classes/task/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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');
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 3135ecf

Please sign in to comment.