Skip to content

Commit

Permalink
Included the Training data placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace committed Sep 28, 2023
1 parent 60d9f83 commit f81965f
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 85 deletions.
9 changes: 8 additions & 1 deletion actions/notification/classes/actionform.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use mod_pulse\automation\helper;
use pulseaction_notification\notification;
use pulseaction_notification\schedule;
use pulseaction_notification\task\notify_users;

/**
* Notification action form, contains important method and basic plugin details.
Expand Down Expand Up @@ -219,10 +220,16 @@ public function delete_template_action($templateid) {
public function trigger_action($instancedata, $userid, $expectedtime=null, $newuser=false) {

$notification = notification::instance($instancedata->pulsenotification_id);
$notificationinstance = helper::filter_record_byprefix($instancedata, $this->config_shortname());
$notificationinstance = (object) helper::filter_record_byprefix($instancedata, $this->config_shortname());

$notification->set_notification_data($notificationinstance, $instancedata);

// Find the suppress conditions are reached.
$course = $instancedata->course;
$suppressreached = notify_users::is_suppress_reached($notificationinstance, $userid, $course, null);
if ($suppressreached) { // Suppress reached not need to setup new schedules.
return '';
}
// Create a schedule for user. This method verify the user activity completion before creating schedules.
$notification->create_schedule_foruser($userid, '', null, $expectedtime ?? null, $newuser);

Expand Down
30 changes: 21 additions & 9 deletions actions/notification/classes/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@

use book;
use DateTime;
use stdClass;
use moodle_url;
use html_writer;
use pulseaction_notification\task\notify_users;
use mod_pulse\automation\helper;
use mod_pulse\automation\instances;
use mod_pulse\helper as pulsehelper;
use mod_pulse\plugininfo\pulseaction;
use moodle_url;
use html_writer;
use stdClass;
use tool_dataprivacy\form\context_instance;

/**
Expand Down Expand Up @@ -250,10 +251,16 @@ protected function create_instance_data() {
* @return void
*/
public function set_notification_data($notificationdata, $instancedata) {
$data = (object) $notificationdata;
$this->notificationdata = $this->update_data_structure($data);

$data = (object) $instancedata;
// Set the notification data.
$notificationdata = (object) $notificationdata;
$this->notificationdata = $this->update_data_structure($notificationdata);

// Set the instance data.
$instancedata = (object) $instancedata;
// Instance not contains course then include course.
if (!isset($instancedata->course)) {
$instancedata->course = get_course($instancedata->courseid);
}
$this->instancedata = $instancedata;
}

Expand Down Expand Up @@ -523,6 +530,11 @@ public function create_schedule_forinstance($newenrolment=false) {
// Get the users for this receipents roles.
$users = $this->get_users_withroles($roles, $context);
foreach ($users as $userid => $user) {
$suppressreached = notify_users::is_suppress_reached(
$this->notificationdata, $userid, $this->instancedata->course, null);
if ($suppressreached) {
continue;
}
$this->create_schedule_foruser($user->id, null, 0, null, $newenrolment);
}

Expand Down Expand Up @@ -865,7 +877,7 @@ public static function generate_dynamic_content($contenttype, $contentlength, $c
* @param stdclass $user
* @param \context $context
* @param array $notificationoverrides
* @return array Basic details to send notification.
* @return stdclass Basic details to send notification.
*/
public function generate_notification_details($moddata, $user, $context, $notificationoverrides=[]) {

Expand Down Expand Up @@ -1000,7 +1012,7 @@ public static function get_schedule_subject($value, $row) {
}

/**
* Get the list of modules data for the placholders.
* Get the list of modules data for the placholders. includes the metadata fields.
*
* @param array $modules List of modules.
* @return array
Expand Down
41 changes: 24 additions & 17 deletions actions/notification/classes/schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,6 @@ public function send_scheduled_notification($userid=null) {

$sender = $this->find_sender_user();

if (is_string($sender)) {
$replyto = $sender;
$sender = (object) [
'from' => $replyto,
'firstname' => '',
'lastname' => '',
'firstnamephonetic' => '',
'lastnamephonetic' => '',
'middlename' => '',
'alternatename' => '',
'firstname' => '',
'lastname' => '',
];
}
// Add bcc and CC to sender user custom headers.
$sender->customheaders = [
"Bcc: $detail->bcc\r\n",
Expand Down Expand Up @@ -210,7 +196,7 @@ public function send_scheduled_notification($userid=null) {
// TODO: NOTE using notification API takes 16 queries. Direct email_to_user method will take totally 9 queries.
// Send the notification to user.
$messagesend = email_to_user($detail->recepient, $sender, $subject,
$messageplain, $messagehtml, '', '', true, $replyto ?? '');
$messageplain, $messagehtml, '', '', true, $sender->from ?? '');

if ($messagesend) {
// Update the current time as lastrun.
Expand Down Expand Up @@ -283,6 +269,7 @@ protected function get_scheduled_records($userid=null) {
// Number of notification to send in this que.
$limit = get_config('pulse', 'schedulecount') ?: 100;

// Trigger the schedules for sepecied users.
$userwhere = $userid ? ' AND ns.userid =:userid ' : '';
$userparam = $userid ? ['userid' => $userid] : [];

Expand All @@ -309,7 +296,7 @@ protected function get_scheduled_records($userid=null) {
AND (ej1_ue.timeend = 0 OR ej1_ue.timeend > :timeend)
GROUP BY eu1_u.id, ej1_e.courseid
) active_enrols ON active_enrols.id = ue.id AND active_enrols.courseid = c.id
WHERE ns.status = :status
WHERE ns.status = :status AND ai.status <> 0
AND active_enrols.activeenrolment <> 0
AND c.visible = 1
AND c.startdate <= :startdate AND (c.enddate = 0 OR c.enddate >= :enddate)
Expand Down Expand Up @@ -421,8 +408,26 @@ protected function build_schedule_values($schedule) {
protected function find_sender_user() {
// Find the sender for this schedule.
if ($this->notificationdata->sender == notification::SENDERCUSTOM) {
// Find the user from moodle for this custom email.
$emailuser = \core_user::get_user_by_email($this->notificationdata->senderemail);
// Use the custom sender email as the support user email.
$sender = $this->notificationdata->senderemail;

// SEnder is not found and is a custom email. then create dummy user data with custom email.
if (is_string($sender)) {
$replyto = $sender;
$expsender = explode('@', $sender);
$sender = (object) [
'from' => $replyto,
'firstname' => $expsender[0] ?? $sender, // Use the first part of the email as firstname of the user.
'lastname' => '',
'firstnamephonetic' => '',
'lastnamephonetic' => '',
'middlename' => '',
'alternatename' => '',
];
}

} else if ($this->notificationdata->sender == notification::SENDERTENANTROLE) {
$sender = $this->notification->get_tenantrole_sender($this->schedulerecord);
} else {
Expand Down Expand Up @@ -521,12 +526,14 @@ public function include_session_data(&$mod, $sessiondata, $userid) {
$finalsessiondata->capacity = $session->capacity;
$finalsessiondata->normalcost = format_cost($session->normalcost);
$finalsessiondata->discountcost = format_cost($session->discountcost);
$finalsessiondata->link = $CFG->wwwroot . "/mod/facetoface/signup.php?s=$session->id";

$formatedtime = facetoface_format_session_times($session->timestart, $session->timefinish, null);
$finalsessiondata = (object) array_merge((array) $finalsessiondata, (array) $formatedtime);

// Fetch the sessions custom fields.
$customfields = facetoface_get_session_customfields();
$finalsessiondata->customfield = new \stdclass();
// Include the session custom fields.
foreach ($customfields as $field) {
$finalsessiondata->customfield->{$field->shortname} = facetoface_get_customfield_value(
$field, $session->sessionid, 'session');
Expand Down
95 changes: 50 additions & 45 deletions actions/notification/classes/task/notify_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,81 +80,86 @@ public static function module_completed($eventdata) {
// Get all the notification instance configures the suppress with this activity.
$notifications = self::get_suppress_notifications($cmid);

self::is_suppress_reached($notifications, $userid, $course, $completion);
foreach ($notifications as $notification) {
// Update suppress reached for all queued shedules.
self::is_suppress_reached($notification, $userid, $course, $completion);
}
}

/**
* Find the scheduled notification instance supress conditions are reached for the user.
*
* @param array $notifications List of notification to verify the suppress.
* @param object $notification List of notification to verify the suppress.
* @param int $userid User ID to verify for.
* @param stdclass $course Instance Course record.
* @param \completion_info $completion Instance course completion info.
* @return bool True if the user is reached the suppress conditions for the instance. Otherwise False.
*/
public static function is_suppress_reached($notifications, $userid, $course, $completion=null) {
public static function is_suppress_reached($notification, $userid, $course, $completion=null) {
global $DB;

$completion = $completion ?: new \completion_info($course);

foreach ($notifications as $notification) {
// Get the notification suppres module ids.
$suppress = $notification->suppress ? json_decode($notification->suppress) : '';

if (!empty($suppress)) {
$result = [];
// Find the completion status for all this suppress modules.
foreach ($suppress as $cmid) {
if (method_exists($completion, 'get_completion_data')) {
$modulecompletion = $completion->get_completion_data($cmid, $userid, []);
} else {
$cminfo = get_coursemodule_from_id('', $cmid);
$modulecompletion = (array) $completion->get_data($cminfo, false, $userid);
}
if ($modulecompletion['completionstate'] == COMPLETION_COMPLETE) {
$result[] = true;
}
// Get the notification suppres module ids.
$suppress = $notification->suppress && is_string($notification->suppress)
? json_decode($notification->suppress) : $notification->suppress;

if (!empty($suppress)) {
$result = [];
// Find the completion status for all this suppress modules.
foreach ($suppress as $cmid) {
if (method_exists($completion, 'get_completion_data')) {
$modulecompletion = $completion->get_completion_data($cmid, $userid, []);
} else {
$cminfo = get_coursemodule_from_id('', $cmid);
$modulecompletion = (array) $completion->get_data($cminfo, false, $userid);
}
if ($modulecompletion['completionstate'] == COMPLETION_COMPLETE) {
$result[] = true;
}
}

// If suppress operator set as all, check all the configures modules are completed.
if ($notification->suppressoperator == \mod_pulse\automation\action_base::OPERATOR_ALL) {
// Remove the schedule only if all the activites are completed.
if (count($result) == count($suppress)) {
$remove = true;
}
// If suppress operator set as all, check all the configures modules are completed.
if ($notification->suppressoperator == \mod_pulse\automation\action_base::OPERATOR_ALL) {
// Remove the schedule only if all the activites are completed.
if (count($result) == count($suppress)) {
$remove = true;
}

} else {
// If any one of the activity is completed then remove the schedule from the user.
if (count($result) >= 1) {
$remove = true;
}
} else {
// If any one of the activity is completed then remove the schedule from the user.
if (count($result) >= 1) {
$remove = true;
}
}

// Update the flag to user schedules as suppress reached, it prevents the update of the schedule on notification.
if (isset($remove) && $remove) {
$remove = false; // Reset for the next notification test.
// Update the flag to user schedules as suppress reached, it prevents the update of the schedule on notification.
if (isset($remove) && $remove) {
$remove = false; // Reset for the next notification test.

$sql = "SELECT * FROM {pulseaction_notification_sch}
WHERE instanceid = :instanceid AND userid = :userid
AND (status = :disabledstatus OR status = :queued)";
$sql = "SELECT * FROM {pulseaction_notification_sch}
WHERE instanceid = :instanceid AND userid = :userid
AND (status = :disabledstatus OR status = :queued)";

$params = [
'instanceid' => $notification->instanceid, 'userid' => $userid,
'disabledstatus' => notification::STATUS_DISABLED, 'queued' => notification::STATUS_QUEUED
];
$params = [
'instanceid' => $notification->instanceid, 'userid' => $userid,
'disabledstatus' => notification::STATUS_DISABLED, 'queued' => notification::STATUS_QUEUED
];

if ($record = $DB->get_record_sql($sql, $params)) {
if ($records = $DB->get_records_sql($sql, $params)) {
foreach ($records as $record) {
$DB->set_field('pulseaction_notification_sch', 'suppressreached', notification::SUPPRESSREACHED,
['id' => $record->id]
);
}

$suppressreached[$notification->id] = notification::SUPPRESSREACHED;
}

return notification::SUPPRESSREACHED;
}

return $suppressreached ?? false;
}
return false;

}

/**
Expand Down
2 changes: 1 addition & 1 deletion automation/instances/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
echo $OUTPUT->heading(get_string('automation', 'pulse'));

// Show smart menus description.
echo get_string('autotemplates_desc', 'pulse');
echo get_string('autoinstance_desc', 'pulse');

// Prepare 'Create smart menu' button. // TODO Review.
$createbutton = $OUTPUT->box_start();
Expand Down
Loading

0 comments on commit f81965f

Please sign in to comment.