Skip to content

Commit

Permalink
Using sync threshold to control sync transfer on status page
Browse files Browse the repository at this point in the history
  • Loading branch information
aydevworks committed Feb 14, 2024
1 parent f9198b3 commit 5cd2121
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 82 deletions.
2 changes: 1 addition & 1 deletion amd/build/sitsgradepush.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/sitsgradepush.min.js.map

Large diffs are not rendered by default.

124 changes: 66 additions & 58 deletions amd/src/sitsgradepush.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,65 +66,73 @@ function initConfirmationModal(courseid, coursemoduleid) {

// Add event listener to the confirmation modal.
confirmTransferButton.addEventListener("click", async function() {
let promises = [];

// Find all valid assessment mapping IDs.
let mappingtables = document.querySelectorAll('.sitsgradepush-history-table');

// Number of assessment mappings.
let total = mappingtables.length - 1; // Exclude the invalid students table.
let count = 0;

// Schedule a task to push grades to SITS for each assessment mapping.
mappingtables.forEach(function(table) {
let mappingid = table.getAttribute('data-assessmentmappingid');
let markscount = table.getAttribute('data-markscount');
if (mappingid !== null && markscount > 0) {
let promise = schedulePushTask(mappingid)
.then(function(result) {
if (result.success) {
count = count + 1;
} else {
// Create an error message row.
let errormessageid = "error-message-" + mappingid;
let errormessagerow = document.createElement("div");
errormessagerow.setAttribute("id", errormessageid);
errormessagerow.setAttribute("class", "error-message-row");
errormessagerow.innerHTML =
'<div class="alert alert-danger" role="alert">' + result.message + '</div>';

// Find the closest row to the assessment mapping.
let currentrow = document.getElementById(errormessageid);

// Remove the error message row if it exists.
if (currentrow !== null) {
currentrow.remove();
// Check if it is an async push button.
let sync = confirmTransferButton.getAttribute('data-sync');
if (sync === "1") {
// Do sync push.
window.location.href = '/local/sitsgradepush/index.php?id=' + coursemoduleid + '&pushgrade=1';
} else {
// Do async push.
let promises = [];

// Find all valid assessment mapping IDs.
let mappingtables = document.querySelectorAll('.sitsgradepush-history-table');

// Number of assessment mappings.
let total = mappingtables.length - 1; // Exclude the invalid students table.
let count = 0;

// Schedule a task to push grades to SITS for each assessment mapping.
mappingtables.forEach(function(table) {
let mappingid = table.getAttribute('data-assessmentmappingid');
let markscount = table.getAttribute('data-markscount');
if (mappingid !== null && markscount > 0) {
let promise = schedulePushTask(mappingid)
.then(function(result) {
if (result.success) {
count = count + 1;
} else {
// Create an error message row.
let errormessageid = "error-message-" + mappingid;
let errormessagerow = document.createElement("div");
errormessagerow.setAttribute("id", errormessageid);
errormessagerow.setAttribute("class", "error-message-row");
errormessagerow.innerHTML =
'<div class="alert alert-danger" role="alert">' + result.message + '</div>';

// Find the closest row to the assessment mapping.
let currentrow = document.getElementById(errormessageid);

// Remove the error message row if it exists.
if (currentrow !== null) {
currentrow.remove();
}

// Insert the error message above the table.
table.parentNode.insertBefore(errormessagerow, table);
}

// Insert the error message above the table.
table.parentNode.insertBefore(errormessagerow, table);
}
return result.success;
})
.catch(function(error) {
window.console.error(error);
});

promises.push(promise);
}
});

// Wait for all the push tasks to be scheduled.
await Promise.all(promises);

// Update the page.
await updateTasksInfo(courseid, coursemoduleid);

// Display a notification.
await notification.addNotification({
message: count + ' of ' + total + ' push tasks have been scheduled.',
type: (count === total) ? 'success' : 'warning'
});
return result.success;
})
.catch(function(error) {
window.console.error(error);
});

promises.push(promise);
}
});

// Wait for all the push tasks to be scheduled.
await Promise.all(promises);

// Update the page.
await updateTasksInfo(courseid, coursemoduleid);

// Display a notification.
await notification.addNotification({
message: count + ' of ' + total + ' push tasks have been scheduled.',
type: (count === total) ? 'success' : 'warning'
});
}
});
}

Expand Down
22 changes: 15 additions & 7 deletions classes/output/pushrecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,23 @@ class pushrecord {
/** @var int Last grade push error type */
public int $lastgradepusherrortype = 0;

/** @var string Last grade push time */
public string $lastgradepushtime = '-';
/** @var string Last grade push time string */
public string $lastgradepushtimestring = '-';

/** @var int Last grade push time */
public int $lastgradepushtime = 0;

/** @var string|null Last submission log push result */
public ?string $lastsublogpushresult = null;

/** @var int Last submission log push error type */
public int $lastsublogpusherrortype = 0;

/** @var string Last submission log push time */
public string $lastsublogpushtime = '-';
/** @var string Last submission log push time string */
public string $lastsublogpushtimestring = '-';

/** @var int Last submission log push time */
public int $lastsublogpushtime = 0;

/** @var bool Is grade pushed */
public bool $isgradepushed = false;
Expand Down Expand Up @@ -188,7 +194,7 @@ protected function set_transfer_records(int $assessmentmappingid, int $studentid
} else {
$errortype = $log->errortype ?: errormanager::ERROR_UNKNOWN;
}
$timecreated = date('Y-m-d H:i:s', $log->timecreated);

// Get <MAP CODE>-<MAB SEQ> from request url.
if (preg_match('#moodle/(.*?)/student#', $log->request, $matches)) {
$this->componentgrade = $matches[1];
Expand All @@ -202,11 +208,13 @@ protected function set_transfer_records(int $assessmentmappingid, int $studentid
}
$this->lastgradepushresult = $result;
$this->lastgradepusherrortype = $errortype;
$this->lastgradepushtime = $timecreated;
$this->lastgradepushtimestring = date('Y-m-d H:i:s', $log->timecreated);;
$this->lastgradepushtime = $log->timecreated;
} else if ($log->type == manager::PUSH_SUBMISSION_LOG) {
$this->lastsublogpushresult = $result;
$this->lastsublogpusherrortype = $errortype;
$this->lastsublogpushtime = $timecreated;
$this->lastsublogpushtimestring = date('Y-m-d H:i:s', $log->timecreated);;
$this->lastsublogpushtime = $log->timecreated;
}
}
}
Expand Down
32 changes: 23 additions & 9 deletions classes/output/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function render_marks_transfer_history_page(array $assessmentdata, int $c
$showsublogcolumn = has_capability('local/sitsgradepush:showsubmissionlogcolumn', \context_course::instance($courseid));

$mappingtables = [];
$totalmarkscount = 0;
foreach ($assessmentdata['mappings'] as $mapping) {
$students = null;
// Modify the timestamp format and add the label for the last push result.
Expand All @@ -112,6 +113,10 @@ public function render_marks_transfer_history_page(array $assessmentdata, int $c
$students = $mapping->students;
}

// Get the total marks count.
$totalmarkscount += $mapping->markscount;

// Add the mapping table.
$mappingtable = new \stdClass();
$mappingtable->mappingid = $mapping->id;
$mappingtable->markscount = $mapping->markscount ?? 0;
Expand All @@ -127,12 +132,16 @@ public function render_marks_transfer_history_page(array $assessmentdata, int $c
$assessmentdata['invalidstudents']->showsublogcolumn = $showsublogcolumn;
}

// Sync threshold.
$syncthreshold = get_config('local_sitsgradepush', 'sync_threshold');

// Render the table.
return $this->output->render_from_template('local_sitsgradepush/marks_transfer_history_page', [
'module-delivery-tables' => $mappingtables,
'transfer-all-button-label' => get_string('label:pushgrade', 'local_sitsgradepush'),
'latest-transferred-text' => $this->get_latest_tranferred_text($assessmentdata['mappings']),
'invalid-students' => !empty($assessmentdata['invalidstudents']->students) ? $assessmentdata['invalidstudents'] : null,
'sync' => $totalmarkscount <= $syncthreshold ? 1 : 0,
]);
}

Expand Down Expand Up @@ -322,23 +331,28 @@ private function get_label_html(int $errortype = null) : string {
* @param array $mappings
* @return string
* @throws \coding_exception
* @throws \dml_exception
*/
public function get_latest_tranferred_text(array $mappings): string {
$lasttasktext = '';
$lasttasktime = 0;

// Get the latest transferred time among all transfer records.
foreach ($mappings as $mapping) {
$lasttask = taskmanager::get_last_finished_push_task($mapping->id);
if ($lasttask && $lasttask->timeupdated && $lasttask->timeupdated > $lasttasktime) {
$lasttasktime = $lasttask->timeupdated;
$lasttasktext = get_string(
'label:lastpushtext',
'local_sitsgradepush', [
'date' => date('d/m/Y', $lasttasktime),
'time' => date('g:i:s a', $lasttasktime), ]);
foreach ($mapping->students as $student) {
if ($student->lastgradepushtime && $student->lastgradepushtime > $lasttasktime) {
$lasttasktime = $student->lastgradepushtime;
}
}
}

if ($lasttasktime > 0) {
$lasttasktext = get_string(
'label:lastpushtext',
'local_sitsgradepush', [
'date' => date('d/m/Y', $lasttasktime),
'time' => date('g:i:s a', $lasttasktime), ]);
}

return $lasttasktext;
}
}
Loading

0 comments on commit 5cd2121

Please sign in to comment.