Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moodle 4.4 and php 8.2 compatibility #44

Open
wants to merge 1 commit into
base: v2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions classes/task/notify_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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);
}
}
}
72 changes: 40 additions & 32 deletions classes/task/sendinvitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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);
}

Expand All @@ -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',
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 = [];
Expand All @@ -281,4 +289,4 @@ public static function get_sender($courseid) {

return (object) ['coursecontact' => $coursecontact, 'groupcontact' => $groups];
}
}
}
Loading