Skip to content

Commit

Permalink
Improved notification placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace committed Sep 30, 2023
1 parent f81965f commit b16ed57
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 64 deletions.
6 changes: 3 additions & 3 deletions actions/notification/classes/local/entities/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ protected function get_all_filters(): array {
$filters[] = (new filter(
text::class,
'automationinstance',
new lang_string('messagetype', 'pulseaction_notification'),
new lang_string('automationinstance', 'pulseaction_notification'),
$this->get_entity_name(),
"IF ({$templatesinsalias}.title <> '', {$templatesinsalias}.title, {$templatesalias}.title)"
"{$templatesinsalias}.title"
));

// Automation template.
$filters[] = (new filter(
text::class,
'automationtemplate',
new lang_string('messagetype', 'pulseaction_notification'),
new lang_string('automationtemplate', 'pulseaction_notification'),
$this->get_entity_name(),
"{$templatesalias}.title"
));
Expand Down
1 change: 1 addition & 0 deletions actions/notification/classes/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ public function build_notification_content(?stdClass $cm=null, $context=null, $o
}

$finalcontent = $headercontent . $staticcontent . $footercontent;

return format_text($finalcontent, FORMAT_HTML, ['noclean' => true, 'overflowdiv' => true]);
}

Expand Down
3 changes: 3 additions & 0 deletions actions/notification/lang/en/pulseaction_notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@
$string['noactiveusers'] = 'Course doesn\'t contain any active enrolments. <span> Please enroll users in the course.</span>';
$string['coursenotstarted'] = 'Course has not started. <span> Please set the course start date to a date in the past.</span>';
$string['courseenddatereached'] = 'Course has ended. <span> Please set the course end date to a date in the future or remove the end date.</span>';
// ...Reports filter string.
$string['automationinstance'] = 'Automation instance';
$string['automationtemplate'] = 'Automation template';
20 changes: 16 additions & 4 deletions classes/automation/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace mod_pulse\automation;

use core_calendar\local\event\entities\event;
use html_writer;
use mod_pulse\plugininfo\pulseaction;
use moodle_url;
use single_button;
Expand Down Expand Up @@ -446,6 +448,20 @@ public function timemanagement_details(string $var, \stdClass $course, int $user

require_once($CFG->dirroot.'/enrol/locallib.php');

// Upcoming event dates.
if ($var == 'eventdates') {
$calendar = \calendar_information::create(time(), $course->id, null);
list($data, $template) = calendar_get_view($calendar, 'upcoming_mini');
$final = isset($data->events) ? array_map(function($event) {
$link = \html_writer::link($event->viewurl, $event->popupname);
$link .= '<br>'.$event->formattedtime;
return \html_writer::tag('li', $link);
} , $data->events) : [];

return $final ? \html_writer::tag('ul', implode('', $final)) : '';
}

// Other than eventdates all are need learning tools time management installed.
if (!$this->timemanagement_installed()) {
return '';
}
Expand Down Expand Up @@ -482,10 +498,6 @@ public function timemanagement_details(string $var, \stdClass $course, int $user
array_values($mods)));
}

if ($var == 'eventdates') {
return '';
}

return '';
}

Expand Down
32 changes: 0 additions & 32 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ public static function update_emailvars($templatetext, $subject, $course, $user,
// Get formatted name of the category.
$course->category = is_number($course->category)
? core_course_category::get($course->category)->get_formatted_name() : $course->category;
// Convert the placeholders timeformat to user readable.
self::convert_varstime_format($course);
self::convert_varstime_format($user);
self::convert_varstime_format($mod);

$vars = new pulse_email_vars($user, $course, $sender, $mod);

Expand All @@ -102,34 +98,6 @@ public static function update_emailvars($templatetext, $subject, $course, $user,
return [$subject, $templatetext];
}

/**
* Converts specific timestamp values in an array to user-readable time format.
*
* @param array $var The array containing timestamp values to be converted.
*/
public static function convert_varstime_format(&$var) {
if (empty($var)) {
return;
}
// Update the timestamp to user readable time.
array_walk($var, function(&$value, $key) {
if (in_array(strtolower($key), ['timecreated', 'timemodified', 'startdate', 'enddate', 'firstaccess',
'lastaccess', 'lastlogin', 'currentlogin', 'timecreated', 'starttime', 'endtime'])) {
$value = $value ? userdate($value) : '';
}
// Update the status to user readable strings.
if (in_array(strtolower($key), ['visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid', 'enablecompletion'])) {
$value = $value == 1 ? get_string('enabled', 'pulse') : get_string('disabled', 'pulse');
}

if (strtolower($key) == 'lang') {
// Get the list of translations.
$translations = get_string_manager()->get_list_of_translations();
$value = $translations[$value] ?? '';
}
});
}

/**
* Find the course module is visible to current user.
*
Expand Down
3 changes: 2 additions & 1 deletion conditions/course/classes/conditionform.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ public static function course_completed($eventdata) {
LEFT JOIN {pulse_condition_overrides} co ON co.instanceid = ai.id AND co.triggercondition = 'course'
WHERE ai.courseid=:courseid AND (co.status > 0 OR $like)";

// Parameters.
$params = ['courseid' => $courseid, 'value' => '%"course"%'];

// Fetch the list of notifications.
$instances = $DB->get_records_sql($sql, $params);

foreach ($instances as $key => $instance) {
Expand Down
22 changes: 15 additions & 7 deletions conditions/session/classes/conditionform.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function signup_success($eventdata) {
$completion = new \completion_info($course);

// Get all the notification instance configures the suppress with this session.
$notifications = self::get_acitivty_notifications($cm->instance);
$notifications = self::get_session_notifications($cm->instance);

foreach ($notifications as $notification) {
// Get the notification suppres module ids.
Expand All @@ -145,8 +145,9 @@ public static function signup_success($eventdata) {
// Trigger all the instance for notifications.
$condition->trigger_instance($notification->instanceid, $userid, $session->timestart);
}
return true;
}

return true;
}

/**
Expand All @@ -157,14 +158,21 @@ public static function signup_success($eventdata) {
* @param int $id ID of the triggered method, Role or cohort id.
* @return array
*/
public static function get_acitivty_notifications($id) {
public static function get_session_notifications($id) {
global $DB;

$like = $DB->sql_like('additional', ':value');
$sessionlike = $DB->sql_like('triggercondition', ':session');
$sql = "SELECT * FROM {pulse_condition_overrides} WHERE status >= 1 AND $sessionlike AND $like";
$params = ['session' => 'session', 'value' => '%"'.$id.'"%'];
$like = $DB->sql_like('co.additional', ':value'); // Like query to fetch the instances assigned this module.
$sessionlike = $DB->sql_like('pat.triggerconditions', ':session');

$sql = "SELECT *, ai.id as id, ai.id as instanceid FROM {pulse_autoinstances} ai
JOIN {pulse_autotemplates} pat ON pat.id = ai.templateid
LEFT JOIN {pulse_condition_overrides} co ON co.instanceid = ai.id AND co.triggercondition = 'session'
WHERE $like AND (co.status > 0 OR $sessionlike)";
// Params.
$params = ['session' => '%"session"%', 'value' => '%"'.$id.'"%'];

$records = $DB->get_records_sql($sql, $params);

return $records;
}

Expand Down
75 changes: 59 additions & 16 deletions lib/vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class pulse_email_vars {
*/
protected $coursecontext = null;

/**
* Course element without updateing its vars.
*
* @var stdclass
*/
protected $orgcourse = null;

/**
* Sets up and retrieves the API objects.
*
Expand All @@ -114,14 +121,19 @@ class pulse_email_vars {
public function __construct($user, $course, $sender, $pulse) {
global $CFG;

self::convert_varstime_format($user);
$this->user =& $user;

$this->sender =& $sender;
$wwwroot = $CFG->wwwroot;
$this->pulse = $pulse;

self::convert_varstime_format($pulse);
$this->mod = $pulse; // Auomation templates pulse is used as module.

$this->course =& $course;
$this->orgcourse = clone $course; // Store the course record without before update its format.
self::convert_varstime_format($course);
$this->course = $course;
// Course context.
$this->coursecontext = \context_course::instance($this->course->id);

Expand All @@ -142,7 +154,38 @@ public function __construct($user, $course, $sender, $pulse) {
* @return string
**/
private static function ok2call($methodname) {
return ($methodname != "vars" && $methodname != "__construct" && $methodname != "__get" && $methodname != "ok2call");
return ($methodname != "vars" && $methodname != "__construct" && $methodname != "__get"
&& $methodname != "ok2call" && $methodname != "convert_varstime_format");
}

/**
* Converts specific timestamp values in an array to user-readable time format.
*
* @param stdclass $var The array containing timestamp values to be converted.
*/
private static function convert_varstime_format(&$var) {
if (empty($var)) {
return;
}
// Update the timestamp to user readable time.
array_walk($var, function(&$value, $key) {
if (in_array(strtolower($key), ['timecreated', 'timemodified', 'startdate', 'enddate', 'firstaccess',
'lastaccess', 'lastlogin', 'currentlogin', 'timecreated', 'starttime', 'endtime'])) {
$value = $value ? userdate($value) : '';
}
// Update the status to user readable strings.
if (in_array(strtolower($key), ['visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid'])) {
$value = $value == 1 ? get_string('enabled', 'pulse') : get_string('disabled', 'pulse');
}

if (strtolower($key) == 'lang') {
// Get the list of translations.
$translations = get_string_manager()->get_list_of_translations();
$value = $translations[$value] ?? '';
}
});

$var = (object) $var;
}

/**
Expand Down Expand Up @@ -292,16 +335,6 @@ public function reaction() {
return \mod_pulse\extendpro::pulse_extend_reaction($this);
}

/**
* Course progress.
*
* @return string
*/
public function courseprogress() {
$progress = \core_completion\progress::get_course_progress_percentage($this->course, $this->user->id);
return $progress .'%';
}

/**
* Completion status.
*
Expand Down Expand Up @@ -339,7 +372,7 @@ public function get_user_enrolment() {
}
require_once($CFG->dirroot.'/enrol/locallib.php');

$enrolmanager = new course_enrolment_manager($PAGE, $this->course);
$enrolmanager = new course_enrolment_manager($PAGE, $this->orgcourse);
$enrolments = $enrolmanager->get_user_enrolments($this->user->id);

if (!empty($enrolments)) {
Expand All @@ -361,8 +394,14 @@ public function get_user_enrolment() {
* @return string
*/
public function training($key) {
// Course user progress in percentage.
if ($key == 'courseprogress') {
$progress = \core_completion\progress::get_course_progress_percentage($this->orgcourse, $this->user->id);
return round($progress) .'%';
}

$data = helper::create()->timemanagement_details($key, $this->course, $this->user->id, $this->coursecontext, $this->pulse);
$data = helper::create()->timemanagement_details(
$key, $this->orgcourse, $this->user->id, $this->coursecontext, $this->pulse);

return $data ?? '';
}
Expand Down Expand Up @@ -504,12 +543,15 @@ public static function session_fields() {
}

/**
* Traning fields. Timemanagement ltools support.
* Training fields. Timemanagement ltools support.
*
* @return string
*/
public static function training_data_fields() {
return ['Traning_Eventdates', 'Training_Coursedue', 'Training_Activityduedate', 'Training_Upcomingmods'];
return [
'Training_Eventdates', 'Training_Coursedue', 'Training_Activityduedate',
'Training_Upcomingmods', 'Training_Courseprogress'
];
}

}
Expand All @@ -535,3 +577,4 @@ public static function vars() {
}
}
}

2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'mod_pulse';
$plugin->version = 2023051837;
$plugin->version = 2023051838;
$plugin->requires = 2020061500; // Requires Moodle 3.90.
$plugin->release = 'v2.0';
$plugin->maturity = MATURITY_STABLE;
Expand Down

0 comments on commit b16ed57

Please sign in to comment.