From 745d1a57b08f4b176dbc3ad2fd92cae4c4757a8f Mon Sep 17 00:00:00 2001 From: "raja.lmsace@gmail.com" Date: Thu, 25 Apr 2024 17:20:45 +0530 Subject: [PATCH] Custom fields placeholders improved. --- classes/helper.php | 24 ++++++++++++++++++++++-- lib/vars.php | 13 +++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/classes/helper.php b/classes/helper.php index 04b55d7..aa8dc3d 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -51,14 +51,14 @@ class helper { * @return array Updated subject and message body content. */ public static function update_emailvars($templatetext, $subject, $course, $user, $mod, $sender) { - global $DB, $CFG; + global $DB, $CFG, $USER; // Include placholders handler and user profile library. require_once($CFG->dirroot.'/mod/pulse/lib/vars.php'); require_once($CFG->dirroot.'/user/profile/lib.php'); // Load user profile field data. - $newuser = (object) ['id' => $user->id]; + $newuser = (object) ['id' => !empty($user->id) ? $user->id : $USER->id]; profile_load_data($newuser); // Make the profile custom field data to separate element of the user object. $newuserkeys = array_map(function($value) { @@ -87,6 +87,26 @@ public static function update_emailvars($templatetext, $subject, $course, $user, $course->category = is_number($course->category) ? core_course_category::get($course->category)->get_formatted_name() : $course->category; + if (!empty($mod->id)) { + if ($DB->record_exists('course_modules', ['id' => $mod->id])) { + $module = $DB->get_record('course_modules', ['id' => $mod->id]); + $name = $DB->get_field('modules', 'name', ['id' => $module->module]); + $mod->url = new moodle_url("/mod/$name/view.php", ['id' => $mod->id]); + + if (\mod_pulse\automation\helper::create()->timemanagement_installed()) { + $userenrolments = ltool_timemanagement_get_course_user_enrollment($course->id, $user->id); + if (!empty($userenrolments)) { + $record = $DB->get_record('ltool_timemanagement_modules', ['cmid' => $mod->id ?? 0]); + if ($record) { + $dates = ltool_timemanagement_cal_coursemodule_managedates($record, $userenrolments[0]['timestart']); + $duedate = isset($dates['duedate']) ? userdate($dates['duedate']) : ''; + } + } + } + $mod->duedate = $duedate ?? ''; + } + } + $vars = new pulse_email_vars($user, $course, $sender, $mod); foreach ($amethods as $varscat => $placeholders) { diff --git a/lib/vars.php b/lib/vars.php index cb52238..2a6a4f2 100644 --- a/lib/vars.php +++ b/lib/vars.php @@ -124,10 +124,11 @@ class pulse_email_vars { * @return void */ public function __construct($user, $course, $sender, $pulse) { - global $CFG; + global $CFG, $USER; - self::convert_varstime_format($user); - $this->user =& $user; + $newuser = !empty($user->id) ? $user : $USER; + self::convert_varstime_format($newuser); + $this->user =& $newuser; $this->sender =& $sender; $wwwroot = $CFG->wwwroot; @@ -435,7 +436,11 @@ public static function course_fields() { 'groupmode', 'groupmodeforce', 'defaultgroupingid', 'lang', 'calendartype', 'theme', 'timecreated', 'timemodified', 'enablecompletion', ]; - $records = $DB->get_records('customfield_field', [], '', 'shortname'); + + $sql = "SELECT cf.shortname FROM {customfield_field} cf + JOIN {customfield_category} cc ON cc.id = cf.categoryid + WHERE cc.component = :component"; + $records = $DB->get_records_sql($sql, ['component' => 'core_course']); $customfields = array_map(function($value) { return 'customfield_'.$value;