Skip to content

Commit

Permalink
CTP-4066 SORA extenson for new student enrolment in course
Browse files Browse the repository at this point in the history
  • Loading branch information
aydevworks committed Nov 26, 2024
1 parent 1b2dd7f commit 5a7ceea
Show file tree
Hide file tree
Showing 23 changed files with 618 additions and 90 deletions.
20 changes: 14 additions & 6 deletions classes/cachemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class cachemanager {
/** @var string Cache area for storing marking schemes.*/
const CACHE_AREA_MARKINGSCHEMES = 'markingschemes';

/** @var string Cache area for storing mapping and mab information.*/
const CACHE_AREA_MAPPING_MAB_INFO = 'mappingmabinfo';

/**
* Get cache.
*
Expand All @@ -49,13 +52,18 @@ class cachemanager {
*/
public static function get_cache(string $area, string $key) {
// Check if cache exists or expired.
$cache = cache::make('local_sitsgradepush', $area)->get($key);
// Expire key.
$expires = 'expires_' . $key;
if (empty($cache) || empty($expires) || time() >= $expires) {
$cache = cache::make('local_sitsgradepush', $area);
$cachevalue = $cache->get($key);
$expires = $cache->get('expires_' . $key);

if (empty($cachevalue) || empty($expires) || time() >= $expires) {
if ($expires && time() >= $expires) {
// Cache expired, delete it.
self::purge_cache($area, $key);
}
return null;
} else {
return $cache;
return $cachevalue;
}
}

Expand All @@ -71,7 +79,7 @@ public static function get_cache(string $area, string $key) {
public static function set_cache(string $area, string $key, mixed $value, int $expiresafter): void {
$cache = cache::make('local_sitsgradepush', $area);
$cache->set($key, $value);
$cache->set('expires_' . $key, $expiresafter);
$cache->set('expires_' . $key, time() + $expiresafter);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions classes/extension/ec.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public function get_new_deadline(): string {

/**
* Process the extension.
*
* @param array $mappings
* @throws \dml_exception
*/
public function process_extension(): void {
// Get all mappings for the SITS assessment.
// We only allow one mapping per SITS assessment for now.
$mappings = $this->get_mappings_by_mab($this->get_mab_identifier());

public function process_extension(array $mappings): void {
// Exit if empty mappings.
if (empty($mappings)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions classes/extension/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function is_module_supported(?string $module): bool {
* @throws \dml_exception
* @throws \moodle_exception
*/
protected function get_mappings_by_mab(string $mabidentifier): array {
public function get_mappings_by_mab(string $mabidentifier): array {
global $DB;

// Extract the map code and MAB sequence number from the MAB identifier.
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function get_mappings_by_mab(string $mabidentifier): array {
* @return array
* @throws \dml_exception|\coding_exception
*/
protected function get_mappings_by_userid(int $userid): array {
public function get_mappings_by_userid(int $userid): array {
global $DB;

// Find all enrolled courses for the student.
Expand Down
4 changes: 3 additions & 1 deletion classes/extension/iextension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
interface iextension {
/**
* Process the extension.
*
* @param array $mappings SITS component mappings.
*/
public function process_extension(): void;
public function process_extension(array $mappings): void;
}
18 changes: 11 additions & 7 deletions classes/extension/sora.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,24 @@ public function set_properties_from_get_students_api(array $student): void {
/**
* Process the extension.
*
* @param array $mappings
*
* @return void
* @throws \coding_exception
* @throws \dml_exception
* @throws \dml_exception|\moodle_exception
*/
public function process_extension(): void {
public function process_extension(array $mappings): void {
// Empty mappings, exit early.
if (empty($mappings)) {
return;
}

if (!$this->dataisset) {
throw new \coding_exception('error:extensiondataisnotset', 'local_sitsgradepush');
}

// Get all mappings for the student.
$mappings = $this->get_mappings_by_userid($this->get_userid());

// No mappings found.
if (empty($mappings)) {
// Exit if SORA extra assessment duration and rest duration are both 0.
if ($this->extraduration == 0 && $this->restduration == 0) {
return;
}

Expand Down
6 changes: 5 additions & 1 deletion classes/extension/sora_queue_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace local_sitsgradepush\extension;

use local_sitsgradepush\manager;

/**
* SORA queue processor.
*
Expand Down Expand Up @@ -47,6 +49,8 @@ protected function get_queue_url(): string {
protected function process_message(array $messagebody): void {
$sora = new sora();
$sora->set_properties_from_aws_message($messagebody['Message']);
$sora->process_extension();
// Get all mappings for the student.
$mappings = $sora->get_mappings_by_userid($sora->get_userid());
$sora->process_extension($mappings);
}
}
73 changes: 57 additions & 16 deletions classes/extensionmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace local_sitsgradepush;

use local_sitsgradepush\extension\sora;
use local_sitsgradepush\task\process_extensions_new_enrolment;

/**
* Manager class for extension related operations.
Expand All @@ -31,37 +32,77 @@ class extensionmanager {
/**
* Update SORA extension for students in a mapping.
*
* @param int $mapid
* @param \stdClass $mapping Assessment component mapping ID.
* @param array $students Students data from the SITS get students API.
* @return void
* @throws \dml_exception
*/
public static function update_sora_for_mapping(int $mapid): void {
public static function update_sora_for_mapping(\stdClass $mapping, array $students): void {
try {
// Find the SITS assessment component.
$manager = manager::get_manager();
$mab = $manager->get_mab_by_mapping_id($mapid);

// Throw exception if the SITS assessment component is not found.
if (!$mab) {
throw new \moodle_exception('error:mab_not_found', 'local_sitsgradepush', '', $mapid);
if ($mapping->enableextension !== '1') {
throw new \moodle_exception('error:extension_not_enabled_for_mapping', 'local_sitsgradepush', '', $mapping->id);
}

// Get students information for that assessment component.
$students = $manager->get_students_from_sits($mab);

// If no students found, nothing to do.
// If no students returned from SITS, nothing to do.
if (empty($students)) {
return;
}

// Process SORA extension for each student.
// Process SORA extension for each student or the specified student if user id is provided.
foreach ($students as $student) {
$sora = new sora();
$sora->set_properties_from_get_students_api($student);
$sora->process_extension();
$sora->process_extension([$mapping]);
}
} catch (\Exception $e) {
logger::log($e->getMessage(), null, "Mapping ID: $mapid");
logger::log($e->getMessage(), null, "Mapping ID: $mapping->id");
}
}

/**
* Check if the extension is enabled.
*
* @return bool
* @throws \dml_exception
*/
public static function is_extension_enabled(): bool {
return get_config('local_sitsgradepush', 'extension_enabled') == '1';
}

/**
* Check if the user is enrolling a gradable role.
*
* @param int $roleid Role ID.
* @return bool
*/
public static function user_is_enrolling_a_gradable_role(int $roleid): bool {
global $CFG;

$gradebookroles = !empty($CFG->gradebookroles) ? explode(',', $CFG->gradebookroles) : [];

return in_array($roleid, $gradebookroles);
}

/**
* Get the user enrolment events stored for a course.
*
* @param int $courseid Course ID.
* @return array
* @throws \dml_exception
*/
public static function get_user_enrolment_events(int $courseid): array {
global $DB;
$sql = "SELECT ue.*
FROM {local_sitsgradepush_enrol} ue
WHERE ue.courseid = :courseid AND ue.attempts < :maxattempts";

return $DB->get_records_sql(
$sql,
[
'courseid' => $courseid,
'maxattempts' => process_extensions_new_enrolment::MAX_ATTEMPTS,
],
limitnum: process_extensions_new_enrolment::BATCH_LIMIT
);
}
}
Loading

0 comments on commit 5a7ceea

Please sign in to comment.