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

CTP-3030 Improve notification email #33

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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
22 changes: 14 additions & 8 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,12 @@ public function get_students_from_sits(\stdClass $componentgrade): mixed {
*
* @param \stdClass $assessmentmapping
* @param int $userid
* @param int|null $taskid
* @return bool
* @throws \dml_exception
* @throws \moodle_exception
*/
public function push_grade_to_sits(\stdClass $assessmentmapping, int $userid): bool {
public function push_grade_to_sits(\stdClass $assessmentmapping, int $userid, int $taskid = null): bool {
try {
// Check if last push was succeeded, exit if succeeded.
if ($this->last_push_succeeded($assessmentmapping->id, $userid, self::PUSH_GRADE)) {
Expand All @@ -730,7 +731,7 @@ public function push_grade_to_sits(\stdClass $assessmentmapping, int $userid): b
$this->check_response($response, $request);

// Save transfer log.
$this->save_transfer_log(self::PUSH_GRADE, $assessmentmapping->id, $userid, $request, $response);
$this->save_transfer_log(self::PUSH_GRADE, $assessmentmapping->id, $userid, $request, $response, $taskid);

return true;
}
Expand All @@ -747,10 +748,11 @@ public function push_grade_to_sits(\stdClass $assessmentmapping, int $userid): b
*
* @param \stdClass $assessmentmapping
* @param int $userid
* @param int|null $taskid
* @return bool
* @throws \dml_exception
*/
public function push_submission_log_to_sits(\stdClass $assessmentmapping, int $userid): bool {
public function push_submission_log_to_sits(\stdClass $assessmentmapping, int $userid, ?int $taskid = null): bool {
try {
// Check if submission log push is enabled.
if (!get_config('local_sitsgradepush', 'sublogpush')) {
Expand Down Expand Up @@ -778,12 +780,12 @@ public function push_submission_log_to_sits(\stdClass $assessmentmapping, int $u

// Save push log.
$this->save_transfer_log(
self::PUSH_SUBMISSION_LOG, $assessmentmapping->id, $userid, $request, $response);
self::PUSH_SUBMISSION_LOG, $assessmentmapping->id, $userid, $request, $response, $taskid);

return true;
}
} catch (\moodle_exception $e) {
$this->mark_push_as_failed(self::PUSH_SUBMISSION_LOG, $assessmentmapping->id, $userid, $e);
$this->mark_push_as_failed(self::PUSH_SUBMISSION_LOG, $assessmentmapping->id, $userid, $taskid, $e);
return false;
}

Expand Down Expand Up @@ -1364,12 +1366,14 @@ public function get_data_for_page_update($courseid, $couresmoduleid = 0): array
* @param int $userid
* @param mixed $request
* @param array $response
* @param int|null $taskid
* @param int|null $errorlogid
* @return void
* @throws \dml_exception
*/
private function save_transfer_log(
string $type, int $assessmentmappingid, int $userid, mixed $request, array $response, int $errorlogid = null): void {
string $type, int $assessmentmappingid, int $userid, mixed $request, array $response, ?int $taskid, int $errorlogid = null)
: void {
global $USER, $DB;
$insert = new \stdClass();
$insert->type = $type;
Expand All @@ -1379,6 +1383,7 @@ private function save_transfer_log(
$insert->requestbody = ($request instanceof irequest) ? $request->get_request_body() : null;
$insert->response = json_encode($response);
$insert->errlogid = $errorlogid;
$insert->taskid = $taskid;
$insert->usermodified = $USER->id;
$insert->timecreated = time();

Expand Down Expand Up @@ -1453,12 +1458,13 @@ private function check_response(mixed $response, irequest $request): void {
* @param string $requestidentifier
* @param int $assessmentmappingid
* @param int $userid
* @param int|null $taskid
* @param \moodle_exception $exception
* @return void
* @throws \dml_exception
*/
private function mark_push_as_failed(
string $requestidentifier, int $assessmentmappingid, int $userid, \moodle_exception $exception): void {
string $requestidentifier, int $assessmentmappingid, int $userid, ?int $taskid, \moodle_exception $exception): void {
// Failed response.
$response = [
"code" => "-1",
Expand All @@ -1469,6 +1475,6 @@ private function mark_push_as_failed(
$errorlogid = $exception->debuginfo ?: null;

// Add failed transfer log.
$this->save_transfer_log($requestidentifier, $assessmentmappingid, $userid, null, $response, intval($errorlogid));
$this->save_transfer_log($requestidentifier, $assessmentmappingid, $userid, null, $response, $taskid, intval($errorlogid));
}
}
4 changes: 2 additions & 2 deletions classes/task/adhoctask.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function execute() {

// Run task.
taskmanager::run_task($data->taskid);
taskmanager::send_email_notification($data->taskid, true);
taskmanager::send_email_notification($data->taskid);
} catch (\Exception $e) {
// Log error.
$errlogid = logger::log('Push task failed: ' . $e->getMessage());
Expand All @@ -60,7 +60,7 @@ public function execute() {
taskmanager::update_task_status($data->taskid, taskmanager::PUSH_TASK_STATUS_FAILED, $errlogid);

// Email the user.
taskmanager::send_email_notification($data->taskid, false);
taskmanager::send_email_notification($data->taskid);
}
}
}
95 changes: 52 additions & 43 deletions classes/taskmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public static function run_task(int $taskid) {
$i = 0;
// Push mark and submission log for each student in the mapping.
foreach ($mapping->students as $student) {
$manager->push_grade_to_sits($mapping, $student->userid);
$manager->push_submission_log_to_sits($mapping, $student->userid);
$manager->push_grade_to_sits($mapping, $student->userid, $task->id);
$manager->push_submission_log_to_sits($mapping, $student->userid, $task->id);
$i++;

// Calculate progress.
Expand Down Expand Up @@ -328,64 +328,73 @@ public static function get_pending_task_in_queue(int $assessmentmappingid): bool
* Email user the result of the task.
*
* @param int $taskid
* @param bool $success
* @return void
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public static function send_email_notification($taskid, bool $success) : void {
global $DB, $PAGE;

// Define email object for the subject and content.
$email = new \stdClass();
$email->status = $success ? get_string('task:status:completed', 'local_sitsgradepush') :
get_string('task:status:failed', 'local_sitsgradepush');
$email->activityname = get_string('email:unknown', 'local_sitsgradepush');
$email->mab = get_string('email:unknown', 'local_sitsgradepush');

// Get the task first.
if (!$task = $DB->get_record(manager::TABLE_TASKS, ['id' => $taskid])) {
throw new \moodle_exception('error:tasknotfound', 'local_sitsgradepush');
}
public static function send_email_notification(int $taskid) : void {
global $DB, $PAGE, $OUTPUT;

// Get the content of the task.
$sql = 'SELECT am.id, am.coursemoduleid, CONCAT(cg.mapcode, "-", cg.mabseq) AS mab
FROM {' . manager::TABLE_ASSESSMENT_MAPPING . '} am
$sql = 'SELECT
t.id as taskid,
t.userid,
am.id as assessmentmappingid,
am.coursemoduleid,
CONCAT(cg.mapcode, "-", cg.mabseq) AS mab,
cg.mabname
FROM {' . manager::TABLE_TASKS . '} t
JOIN {' . manager::TABLE_ASSESSMENT_MAPPING . '} am ON t.assessmentmappingid = am.id
JOIN {' . manager::TABLE_COMPONENT_GRADE . '} cg ON am.componentgradeid = cg.id
WHERE am.id = :assessmentmappingid';
WHERE t.id = :taskid';

$params = [
'assessmentmappingid' => $task->assessmentmappingid,
'taskid' => $taskid,
];

// Task content found.
if ($result = $DB->get_record_sql($sql, $params)) {
// Get the course module information.
$coursemodule = get_coursemodule_from_id(null, $result->coursemoduleid);
$email->mab = $result->mab;
$email->activityname = $coursemodule->name;
$url = new \moodle_url(
'/local/sitsgradepush/index.php',
['id' => $coursemodule->id]
);
$email->link = $url->out(false);
}

// Email user the result of the task.
if ($success) {
$content = get_string('email:content:success', 'local_sitsgradepush', $email);
// Transfer history page link.
$url = new \moodle_url('/local/sitsgradepush/index.php', ['id' => $coursemodule->id]);

// Get the user who scheduled the task.
$user = $DB->get_record('user', ['id' => $result->userid]);
$PAGE->set_context(context_user::instance($user->id));

// Get transfer records for the task.
$transferrecords = $DB->get_records(manager::TABLE_TRANSFER_LOG, ['taskid' => $taskid, 'type' => manager::PUSH_GRADE]);

// Separate succeeded and failed transfer records.
$succeededcount = 0;
$failedcount = 0;
foreach ($transferrecords as $transferrecord) {
$transfer = new \stdClass();
$response = json_decode($transferrecord->response);
if ($response->code == "0") {
$succeededcount++;
} else {
$failedcount++;
}
}

// Render the email content.
$content = $OUTPUT->render_from_template('local_sitsgradepush/notification_email', [
'user_name' => fullname($user),
'activity_name' => $coursemodule->name,
'map_code' => $result->mab,
'sits_assessment' => $result->mabname,
'activity_url' => $url->out(false),
'support_url' => get_config('local_sitsgradepush', 'suppuort_page_url') ?? '',
'succeeded_count' => $succeededcount,
'failed_count' => $failedcount,
]);
email_to_user($user, $user, get_string('email:subject', 'local_sitsgradepush'), $content);
} else {
$content = get_string('email:content:fail', 'local_sitsgradepush', $email);
throw new \moodle_exception('error:tasknotfound', 'local_sitsgradepush');
}

// Make status' first letter uppercase for subject.
$email->status = ucfirst($email->status);
$subject = get_string('email:subject', 'local_sitsgradepush', $email);

// Get the user who scheduled the task.
$user = $DB->get_record('user', ['id' => $task->userid]);

$PAGE->set_context(context_user::instance($user->id));
email_to_user($user, $user, $subject, $content);
}
}
3 changes: 2 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/sitsgradepush/db" VERSION="20231219" COMMENT="XMLDB file for Moodle local/sits_grade_push"
<XMLDB PATH="local/sitsgradepush/db" VERSION="20240201" COMMENT="XMLDB file for Moodle local/sits_grade_push"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -54,6 +54,7 @@
<FIELD NAME="requestbody" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="response" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="taskid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="errlogid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
Expand Down
15 changes: 15 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,20 @@ function xmldb_local_sitsgradepush_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2023121900, 'local', 'sitsgradepush');
}

if ($oldversion < 2024020100) {

// Define field taskid to be added to local_sitsgradepush_tfr_log.
$table = new xmldb_table('local_sitsgradepush_tfr_log');
$field = new xmldb_field('taskid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'usermodified');

// Conditionally launch add field taskid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Sitsgradepush savepoint reached.
upgrade_plugin_savepoint(true, 2024020100, 'local', 'sitsgradepush');
}

return true;
}
20 changes: 14 additions & 6 deletions lang/en/local_sitsgradepush.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
$string['settings:userprofilefield:desc'] = 'User profile field for export staff';
$string['settings:sync_threshold'] = 'Sync Threshold';
$string['settings:sync_threshold:desc'] = 'The threshold to allow for running synchronous mark transfer task';
$string['settings:support_page_url'] = 'Support Page URL';
$string['settings:support_page_url:desc'] = 'Used in the notification email to provide a link to the support page';
$string['label:gradepushassessmentselect'] = 'Select SITS assessment to link to';
$string['label:jumpto'] = 'Jump to';
$string['label:pushall'] = 'Transfer All';
Expand Down Expand Up @@ -99,7 +101,6 @@
$string['dashboard:transfermarks'] = 'Transfer marks';
$string['dashboard:changesource'] = 'Change Source';
$string['dashboard:marks_transfer_in_progress'] = 'Marks transfer in progress';
$string['dashboard:activity_button_label'] = '{$a} activity';

// Select source page.
$string['selectsource:header'] = 'Select Source';
Expand All @@ -115,7 +116,6 @@
$string['existingactivity:no_match_row'] = 'No matching rows found.';
$string['existingactivity:navbar'] = 'Existing Activity';


// Error strings.
$string['error:assessmentmapping'] = 'Assessment mapping is not found. ID: {$a}';
$string['error:assessmentisnotmapped'] = 'This activity is not mapped to any assessment component.';
Expand Down Expand Up @@ -185,10 +185,18 @@
$string['privacy:metadata:local_sitsgradepush_tasks:info'] = 'Additional information about the transfer task.';

// Email strings.
$string['email:subject'] = 'Marks Transfer Task {$a->status}: {$a->activityname} - {$a->mab}';
$string['email:content:success'] = 'Marks Transfer Task from {$a->activityname} to {$a->mab} has been {$a->status}.<br><br>You can check the marks transfer history for this task here: <a href="{$a->link}">{$a->activityname} - {$a->mab}</a>';
$string['email:content:fail'] = 'Marks Transfer Task from {$a->activityname} to {$a->mab} has been {$a->status}.<br><br>Please try again later.';
$string['email:unknown'] = 'unknown';
$string['email:subject'] = 'Marks Transfer Completed';
$string['email:username'] = 'Dear {$a},';
$string['email:part_one'] = 'The marks transfer task has been completed. Below are the details of the task:';
$string['email:activity_name'] = 'Activity name:';
$string['email:map_code'] = 'Map code:';
$string['email:sits_assessment'] = 'SITS assessment:';
$string['email:summary'] = 'Summary:';
$string['email:no_of_succeeded_transfers'] = '<strong>Number of succeeded transfers:</strong> {$a}';
$string['email:no_of_failed_transfers'] = '<strong>Number of failed transfers:</strong> {$a}';
$string['email:transfer_history_text'] = 'Click <a href="{$a}">here</a> to see the marks transfer records.';
$string['email:support_text'] = 'To get more information or assistance related to marks transfer, please visit our <a href="{$a}">Support Page</a>.';
$string['email:best_regards'] = 'Best regards, <br>DLE Team';

// Confirmation Modal strings.
$string['confirmmodal:header'] = 'Confirm mark transfer?';
Expand Down
9 changes: 9 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@
'',
$options)
);

// Set the support page URL.
$settings->add(new admin_setting_configtext('local_sitsgradepush/suppuort_page_url',
get_string('settings:support_page_url', 'local_sitsgradepush'),
get_string('settings:support_page_url:desc', 'local_sitsgradepush'),
'https://wiki.ucl.ac.uk/pages/viewpage.action?pageId=306185189',
PARAM_URL,
50
));
}

$subplugins = core_plugin_manager::instance()->get_plugins_of_type('sitsapiclient');
Expand Down
Loading
Loading