From 2d8f12896e83952d5b8b7797053e74a8ebbc6042 Mon Sep 17 00:00:00 2001 From: Davor Budimir Date: Wed, 4 Sep 2024 11:52:41 +0200 Subject: [PATCH] php 8.2 and moodle 4.4 --- classes/task/notify_users.php | 55 ++++++++++++----------- classes/task/sendinvitation.php | 72 +++++++++++++++++------------- classes/task/update_completion.php | 29 ++++++------ db/tasks.php | 2 +- 4 files changed, 86 insertions(+), 72 deletions(-) diff --git a/classes/task/notify_users.php b/classes/task/notify_users.php index 90dfc34..978b711 100644 --- a/classes/task/notify_users.php +++ b/classes/task/notify_users.php @@ -56,16 +56,16 @@ public function execute() { /** * Pulse cron task to send notification for course users. * - * Here users are filtered by their activity avaialbility status. - * if the pulse instance are available to user then it will send the notificaion to the user. + * Here users are filtered by their activity availability status. + * If the pulse instance is available to user then it will send the notification to the user. * - * @param mixed $extend Extend the pro invitation method. - * @return void + * @param bool $extend Extend the pro invitation method. + * @return bool */ - public static function pulse_cron_task($extend=true) { + public static function pulse_cron_task(bool $extend = true): bool { global $DB; - pulse_mtrace( 'Fetching notificaion instance list - MOD-Pulse INIT '); + pulse_mtrace('Fetching notification instance list - MOD-Pulse INIT'); if ($extend && \mod_pulse\extendpro::pulse_extend_invitation()) { return true; @@ -89,15 +89,16 @@ public static function pulse_cron_task($extend=true) { JOIN {course} cu ON cu.id = nt.course RIGHT JOIN {context} ctx ON ctx.instanceid = cm.id and contextlevel = 70 WHERE md.name = 'pulse' AND cm.visible = 1 AND cu.visible = 1 AND nt.pulse = 1 - AND cu.startdate <= :startdate AND (cu.enddate = 0 OR cu.enddate >= :enddate)"; + AND cu.startdate <= :startdate AND (cu.enddate = 0 OR cu.enddate >= :enddate)"; $records = $DB->get_records_sql($sql, ['startdate' => time(), 'enddate' => time()]); if (empty($records)) { pulse_mtrace('No pulse instance are added yet'."\n"); return true; } - $modinfo = []; - foreach ($records as $key => $record) { + + + foreach ($records as $record) { $params = []; $record = (array) $record; $keys = array_keys($record); @@ -108,24 +109,30 @@ public static function pulse_cron_task($extend=true) { // Context. $ctxpos = array_search('contextid', $keys); $ctxendpos = array_search('locked', $keys); - $context = array_slice($record, $ctxpos, ($ctxendpos - $ctxpos) + 1 ); - $context['id'] = $context['contextid']; unset($context['contextid']); + $context = array_slice($record, $ctxpos, ($ctxendpos - $ctxpos) + 1); + $context['id'] = $context['contextid']; + unset($context['contextid']); // Course module. $cmpos = array_search('cmid', $keys); $cmendpos = array_search('deletioninprogress', $keys); - $cm = array_slice($record, $cmpos, ($cmendpos - $cmpos) + 1 ); - $cm['id'] = $cm['cmid']; unset($cm['cmid']); + $cm = array_slice($record, $cmpos, ($cmendpos - $cmpos) + 1); + $cm['id'] = $cm['cmid']; + unset($cm['cmid']); // Course records. $coursepos = array_search('courseid', $keys); $course = array_slice($record, $coursepos); $course['id'] = $course['courseid']; - $course['groupmode'] = isset($course['coursegroupmode']) ? $course['coursegroupmode'] : ''; - $course['idnumber'] = isset($course['courseidnumber']) ? $course['courseidnumber'] : ''; - pulse_mtrace( 'Initiate pulse module - '.$pulse['name'].' course - '. $course['id'] ); + $course['groupmode'] = $course['coursegroupmode'] ?? ''; + $course['idnumber'] = $course['courseidnumber'] ?? ''; + pulse_mtrace('Initiate pulse module - '.$pulse['name'].' course - '. $course['id']); + + // Restore the missing line + $courseid = $pulse['course']; + // Get enrolled users with capability. $contextlevel = explode('/', $context['path']); list($insql, $inparams) = $DB->get_in_or_equal(array_filter($contextlevel)); - // Enrolled users list. + // Enrolled users list. $usersql = "SELECT u.* FROM {user} u JOIN (SELECT DISTINCT eu1_u.id @@ -145,7 +152,7 @@ public static function pulse_cron_task($extend=true) { SELECT userid FROM {pulse_users} WHERE pulseid = ? AND status = 1 ) ORDER BY u.lastname, u.firstname, u.id"; - $params[] = $course['id']; + $params[] = $courseid; // Use $courseid here instead of $course['id'] $params = array_merge($params, array_filter($inparams)); $params = array_merge($params, array_filter($roleinparams)); $params[] = time(); @@ -155,9 +162,7 @@ public static function pulse_cron_task($extend=true) { $limit = get_config('mod_pulse', 'schedulecount') ?: 100; $students = $DB->get_records_sql($usersql, $params, 0, $limit); - $courseid = $pulse['course']; - - $instance = new \stdclass(); + $instance = new \stdClass(); $instance->pulse = (object) $pulse; $instance->course = (object) $course; $instance->context = (object) $context; @@ -175,13 +180,13 @@ public static function pulse_cron_task($extend=true) { /** * Set adhoc task to send reminder notification for each instance * - * @param mixed $instance + * @param \stdClass $instance * @return void */ - public static function pulse_set_notification_adhoc($instance) { + public static function pulse_set_notification_adhoc(\stdClass $instance): void { $task = new \mod_pulse\task\sendinvitation(); $task->set_custom_data($instance); - $task->set_component('pulse'); + $task->set_component('mod_pulse'); \core\task\manager::queue_adhoc_task($task, true); } -} +} \ No newline at end of file diff --git a/classes/task/sendinvitation.php b/classes/task/sendinvitation.php index 660f979..b27f55f 100644 --- a/classes/task/sendinvitation.php +++ b/classes/task/sendinvitation.php @@ -24,10 +24,18 @@ namespace mod_pulse\task; +use core\task\adhoc_task; +use mod_pulse\helper; +use core_user; +use moodle_exception; +use ReflectionProperty; +use context_module; +use stdClass; + /** * Defined the invitation send method and filter methods. */ -class sendinvitation extends \core\task\adhoc_task { +class sendinvitation extends adhoc_task { /** * Adhoc task execution. @@ -42,28 +50,28 @@ public function execute() { // Check pulse enabled. if (!$DB->record_exists('course_modules', ['id' => $instance->cm->id])) { - return true; + return; } // Filter users from course pariticipants by completion. - $listofusers = \mod_pulse\helper::get_course_students((array) $instance->students, $instance); + $listofusers = helper::get_course_students((array) $instance->students, $instance); // Extend the pulse pro version to send notifications on selected recipients. if (!empty($listofusers)) { $this->send_pulse($listofusers, $instance->pulse, $instance->course, $instance->context); } else { - pulse_mtrace('There is not users to send pulse'); + pulse_mtrace('There are no users to send pulse'); } } /** * Send pulse data. * - * @param mixed $users Users data record. - * @param mixed $pulse Pulse instance record. - * @param mixed $course Course data record. - * @param mixed $context Module Context data record. + * @param array $users Users data record. + * @param stdClass $pulse Pulse instance record. + * @param stdClass $course Course data record. + * @param stdClass $context Module Context data record. * @return void */ - public function send_pulse($users, $pulse, $course, $context) { + public function send_pulse(array $users, stdClass $pulse, stdClass $course, stdClass $context): void { global $DB, $USER, $PAGE; // Store current user for update the user after filter. @@ -76,21 +84,21 @@ public function send_pulse($users, $pulse, $course, $context) { // Filtercodes plugin used $PAGE->course proprety for coursestartdate, course enddata and other course related shortcodes. // Tried to use $PAGE->set_course(), But the theme already completed the setup, so we can't use that moodle method. // For this reason, here updated the protected _course property using reflection. - if (\mod_pulse\helper::change_pagevalue()) { + if (helper::change_pagevalue()) { - $coursereflection = new \ReflectionProperty(get_class($PAGE), '_course'); + $coursereflection = new ReflectionProperty(get_class($PAGE), '_course'); $coursereflection->setAccessible(true); $coursereflection->setValue($PAGE, $course); // Setup the course module data to support filtercodes. $pulsecm = get_coursemodule_from_instance('pulse', $pulse->id); - $cmreflection = new \ReflectionProperty(get_class($PAGE), '_cm'); + $cmreflection = new ReflectionProperty(get_class($PAGE), '_cm'); $cmreflection->setAccessible(true); $cmreflection->setValue($PAGE, $pulsecm); - $contextreflection = new \ReflectionProperty(get_class($PAGE), '_context'); + $contextreflection = new ReflectionProperty(get_class($PAGE), '_context'); $contextreflection->setAccessible(true); - $context = \context_module::instance($pulsecm->id); + $context = context_module::instance($pulsecm->id); $contextreflection->setValue($PAGE, $context); } @@ -114,7 +122,7 @@ public function send_pulse($users, $pulse, $course, $context) { $filearea = 'pulse_content'; } // Replace the email text placeholders with data. - list($subject, $messagehtml) = \mod_pulse\helper::update_emailvars($template, $subject, $course, + list($subject, $messagehtml) = helper::update_emailvars($template, $subject, $course, $student, $pulse, $sender); // Rewrite the plugin file placeholders in the email text. $messagehtml = file_rewrite_pluginfile_urls($messagehtml, 'pluginfile.php', @@ -139,20 +147,20 @@ public function send_pulse($users, $pulse, $course, $context) { try { $transaction = $DB->start_delegated_transaction(); - if (\mod_pulse\helper::update_notified_user($userto->id, $pulse)) { - $messagesend = \mod_pulse\helper::messagetouser( + if (helper::update_notified_user($userto->id, $pulse)) { + $messagesend = helper::messagetouser( $userto, $subject, $messageplain, $messagehtml, $pulse, $sender ); if ($messagesend) { $notifiedusers[] = $userto->id; } else { - throw new \moodle_exception('mailnotsend', 'pulse'); + throw new moodle_exception('mailnotsend', 'pulse'); } } else { - throw new \moodle_exception('invitationDB', 'pulse'); + throw new moodle_exception('invitationDB', 'pulse'); } $transaction->allow_commit(); - } catch (\Exception $e) { + } catch (moodle_exception $e) { // Return to current USER. \core\session\manager::set_user($currentuser); $transaction->rollback($e); @@ -161,7 +169,7 @@ public function send_pulse($users, $pulse, $course, $context) { } } - if (\mod_pulse\helper::change_pagevalue()) { + if (helper::change_pagevalue()) { // Return to current USER. \core\session\manager::set_user($currentuser); @@ -179,11 +187,11 @@ public function send_pulse($users, $pulse, $course, $context) { /** * Find the correct sender user from the course and group contacts. * - * @param mixed $senderdata Listof course and group contact users - * @param mixed $userid // Studnet user id - * @return object Sender user obejct + * @param stdClass $senderdata Listof course and group contact users + * @param int $userid // Studnet user id + * @return stdClass Sender user obejct */ - public static function find_user_sender($senderdata, $userid) { + public static function find_user_sender(stdClass $senderdata, int $userid): stdClass { if (!empty($senderdata->groupcontact)) { $groups = $senderdata->groupcontact; @@ -198,17 +206,17 @@ public static function find_user_sender($senderdata, $userid) { } } - return (!empty($senderdata->coursecontact) ? $senderdata->coursecontact : \core_user::get_support_user()); + return (!empty($senderdata->coursecontact) ? $senderdata->coursecontact : core_user::get_support_user()); } /** * Get list of available senders users from group and course seperately. * - * @param mixed $courseid - * @return object + * @param int $courseid + * @return stdClass */ - public static function get_sender($courseid) { + public static function get_sender(int $courseid): stdClass { global $DB; $rolesql = "SELECT rc.id, rc.roleid FROM {role_capabilities} rc JOIN {capabilities} cap ON rc.capability = cap.name @@ -242,7 +250,7 @@ public static function get_sender($courseid) { $teacherids = array_keys($records); // If no teachers enroled in course then use the support user. if (empty($teacherids)) { - return []; + return new stdClass(); } $coursecontact = current($records); // Get first course contact user. @@ -264,7 +272,7 @@ public static function get_sender($courseid) { foreach ($teacherids as $id) { if (!in_array($id, $users)) { $coursecontact = $records[$id]; - continue; + break; } } $groups = []; @@ -281,4 +289,4 @@ public static function get_sender($courseid) { return (object) ['coursecontact' => $coursecontact, 'groupcontact' => $groups]; } -} +} \ No newline at end of file diff --git a/classes/task/update_completion.php b/classes/task/update_completion.php index 9f8107f..d5d0a87 100644 --- a/classes/task/update_completion.php +++ b/classes/task/update_completion.php @@ -28,17 +28,21 @@ require_once($CFG->dirroot.'/mod/pulse/lib.php'); +use core\task\scheduled_task; +use mod_pulse\pulse_course_modinfo; +use context_module; + /** * Update user completion status for pulse. triggered from scheduled task. */ -class update_completion extends \core\task\scheduled_task { +class update_completion extends scheduled_task { /** * Return the task's name as shown in admin screens. * * @return string */ - public function get_name() { + public function get_name(): string { return get_string('updatecompletion', 'mod_pulse'); } @@ -47,9 +51,7 @@ public function get_name() { * * @return void */ - public function execute() { - global $CFG; - + public function execute(): void { $this->mod_pulse_completion_crontask(); } @@ -58,7 +60,7 @@ public function execute() { * * @return void */ - public function mod_pulse_completion_crontask() { + public function mod_pulse_completion_crontask(): void { global $DB, $USER, $CFG; pulse_mtrace('Pulse activity completion - Pulse Starting'); @@ -90,7 +92,7 @@ public function mod_pulse_completion_crontask() { if (empty($records)) { pulse_mtrace('No pulse instance are added yet'."\n"); - return true; + return; } $modinfo = []; foreach ($records as $key => $record) { @@ -148,7 +150,7 @@ public function mod_pulse_completion_crontask() { $pulse = (object) $pulse; if (!in_array($courseid, $modinfo)) { - $modinfo[$courseid] = new \mod_pulse\pulse_course_modinfo($course, 0); + $modinfo[$courseid] = new pulse_course_modinfo($course, 0); } if (empty($modinfo[$courseid]->cms[$cm['id']])) { @@ -164,12 +166,12 @@ public function mod_pulse_completion_crontask() { if (!empty($students)) { $completion = new \completion_info($course); - $context = \context_module::instance($cm->id); + $context = context_module::instance($cm->id); foreach ($students as $key => $user) { $modinfo[$course->id]->set_userid($user->id); $md = $modinfo[$course->id]; // Get pulse module completion state for user. - $currentstate = ($user->completionstate) ?? COMPLETION_INCOMPLETE; + $currentstate = $user->completionstate ?? COMPLETION_INCOMPLETE; $result = pulse_get_completion_state($course, $cm, $user->id, $currentstate, $pulse, $user, $md); if (isset($user->completionstate) && $result == $currentstate) { continue; @@ -184,11 +186,10 @@ public function mod_pulse_completion_crontask() { } } } else { - pulse_mtrace('There is not users to update pulse module completion'); + pulse_mtrace('There are no users to update pulse module completion'); } } - pulse_mtrace('Course module completions are updated for all pulse module....'); - return true; + pulse_mtrace('Course module completions are updated for all pulse modules....'); } -} +} \ No newline at end of file diff --git a/db/tasks.php b/db/tasks.php index ce773ab..992c9ce 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -44,4 +44,4 @@ 'month' => '*', 'dayofweek' => '*', ], -]; +]; \ No newline at end of file