Skip to content

Commit

Permalink
Custom fields placeholders improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
raja-lmsace committed Apr 25, 2024
1 parent d972be1 commit 745d1a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
24 changes: 22 additions & 2 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions lib/vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 745d1a5

Please sign in to comment.