Skip to content

Commit

Permalink
CTP-3894 Set initial SORA status for students
Browse files Browse the repository at this point in the history
  • Loading branch information
aydevworks committed Oct 29, 2024
1 parent 4b50369 commit 842490b
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 151 deletions.
48 changes: 23 additions & 25 deletions classes/extension/ec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace local_sitsgradepush\extension;

use local_sitsgradepush\assessment\assessmentfactory;
use local_sitsgradepush\logger;

/**
* Class for extenuating circumstance (EC).
Expand All @@ -34,19 +35,6 @@ class ec extends extension {
/** @var string MAB identifier, e.g. CCME0158A6UF-001 */
protected string $mabidentifier;

/**
* Constructor.
*
* @param string $message
* @throws \dml_exception
*/
public function __construct(string $message) {
parent::__construct($message);

// Set the EC properties that we need.
$this->set_ec_properties();
}

/**
* Returns the new deadline.
*
Expand Down Expand Up @@ -75,30 +63,40 @@ public function process_extension(): void {
$assessment->apply_extension($this);
}
} catch (\Exception $e) {
// Consider logging the error here.
continue;
logger::log($e->getMessage(), null, "Mapping ID: $mapping->id");
}
}
}

/**
* Set the EC properties.
* Set the EC properties from the AWS EC update message.
* Note: The AWS EC update message is not yet developed, will implement this when the message is available.
*
* @param string $message
* @return void
* @throws \dml_exception
* @throws \dml_exception|\moodle_exception
*/
private function set_ec_properties(): void {
global $DB;
public function set_properties_from_aws_message(string $message): void {
// Decode the JSON message.
$messagedata = $this->parse_event_json($message);

// Find and set the user ID from the student.
$idnumber = $this->message->student_code;
$user = $DB->get_record('user', ['idnumber' => $idnumber], 'id', MUST_EXIST);
$this->userid = $user->id;
// Set the user ID of the student.
$this->set_userid($messagedata->student_code);

// Set the MAB identifier.
$this->mabidentifier = $this->message->identifier;
$this->mabidentifier = $messagedata->identifier;

// Set new deadline.
$this->newdeadline = $this->message->new_deadline;
$this->newdeadline = $messagedata->new_deadline;
}

/**
* Set the EC properties from the get students API.
*
* @param array $student
* @return void
*/
public function set_properties_from_get_students_api(array $student): void {
// Will implement this when the get students API includes EC data.
}
}
37 changes: 29 additions & 8 deletions classes/extension/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ abstract class extension implements iextension {
/** @var array Supported module types */
const SUPPORTED_MODULE_TYPES = ['assign', 'quiz'];

/** @var \stdClass Message from AWS */
protected \stdClass $message;

/** @var int User ID */
protected int $userid;

/** @var bool Used to check if the extension data is set. */
protected bool $dataisset = false;

/**
* Constructor.
* Set properties from JSON message like SORA / EC update message from AWS.
*
* @param string $message
* @throws \Exception
* @return void
*/
public function __construct(string $message) {
$this->message = $this->parse_event_json($message);
}
abstract public function set_properties_from_aws_message(string $message): void;

/**
* Set properties from get students API.
*
* @param array $student
* @return void
*/
abstract public function set_properties_from_get_students_api(array $student): void;

/**
* Get the user ID.
Expand Down Expand Up @@ -156,4 +162,19 @@ protected function parse_event_json(string $message): \stdClass {
}
return $messageobject;
}

/**
* Set the user ID of the student.
*
* @param string $studentcode
* @return void
* @throws \dml_exception
*/
protected function set_userid(string $studentcode): void {
global $DB;

// Find and set the user ID of the student.
$user = $DB->get_record('user', ['idnumber' => $studentcode], 'id', MUST_EXIST);
$this->userid = $user->id;
}
}
109 changes: 71 additions & 38 deletions classes/extension/sora.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace local_sitsgradepush\extension;

use local_sitsgradepush\assessment\assessmentfactory;
use local_sitsgradepush\logger;

/**
* Class for Summary of Reasonable Adjustments (SORA).
Expand All @@ -31,27 +32,15 @@ class sora extends extension {
/** @var string Prefix used to create SORA groups */
const SORA_GROUP_PREFIX = 'DEFAULT-SORA-';

/** @var \stdClass Event data in the AWS message */
protected \stdClass $eventdata;
/** @var int Extra duration in minutes per hour */
protected int $extraduration;

/** @var \stdClass SORA data in the AWS message */
protected \stdClass $soradata;
/** @var int Rest duration in minutes per hour */
protected int $restduration;

/** @var int Time extension in seconds, including extra and rest duration */
protected int $timeextension;

/**
* Constructor.
*
* @param string $message
*/
public function __construct(string $message) {
parent::__construct($message);

// Set the SORA properties that we need.
$this->set_sora_properties();
}

/**
* Return the whole time extension in seconds, including extra and rest duration.
*
Expand All @@ -67,7 +56,7 @@ public function get_time_extension(): int {
* @return int
*/
public function get_extra_duration(): int {
return (int) $this->soradata->extra_duration;
return $this->extraduration;
}

/**
Expand All @@ -76,7 +65,7 @@ public function get_extra_duration(): int {
* @return int
*/
public function get_rest_duration(): int {
return (int) $this->soradata->rest_duration;
return $this->restduration;
}

/**
Expand Down Expand Up @@ -106,7 +95,7 @@ public function get_sora_group_id(int $courseid, int $userid): int {
$newgroup->description = '';
$newgroup->enrolmentkey = '';
$newgroup->picture = 0;
$newgroup->visibility = GROUPS_VISIBILITY_MEMBERS;
$newgroup->visibility = GROUPS_VISIBILITY_OWN;
$newgroup->hidepicture = 0;
$newgroup->timecreated = time();
$newgroup->timemodified = time();
Expand All @@ -133,6 +122,57 @@ public function get_extension_group_name(): string {
return sprintf(self::SORA_GROUP_PREFIX . '%d', $this->get_extra_duration() + $this->get_rest_duration());
}

/**
* Set properties from AWS SORA update message.
*
* @param string $message
* @return void
* @throws \dml_exception
* @throws \moodle_exception
*/
public function set_properties_from_aws_message(string $message): void {

// Decode the JSON message.
$messagedata = $this->parse_event_json($message);

// Check the message is valid.
if (empty($messagedata->entity->person_sora->sora[0])) {
throw new \moodle_exception('error:invalid_message', 'local_sitsgradepush', '', null, $message);
}

$soradata = $messagedata->entity->person_sora->sora[0];

// Set the user ID of the student.
$this->set_userid($soradata->person->student_code);

// Set properties.
$this->extraduration = (int) $soradata->extra_duration;
$this->restduration = (int) $soradata->rest_duration;

// Calculate and set the time extension in seconds.
$this->timeextension = $this->calculate_time_extension($this->get_extra_duration(), $this->get_rest_duration());
$this->dataisset = true;
}

/**
* Set properties from get students API.
*
* @param array $student
* @return void
*/
public function set_properties_from_get_students_api(array $student): void {
// Set the user ID.
$this->set_userid($student['code']);

// Set properties.
$this->extraduration = (int) $student['assessment']['sora_assessment_duration'];
$this->restduration = (int) $student['assessment']['sora_rest_duration'];

// Calculate and set the time extension in seconds.
$this->timeextension = $this->calculate_time_extension($this->get_extra_duration(), $this->get_rest_duration());
$this->dataisset = true;
}

/**
* Process the extension.
*
Expand All @@ -141,6 +181,10 @@ public function get_extension_group_name(): string {
* @throws \dml_exception
*/
public function process_extension(): void {
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());

Expand All @@ -157,8 +201,7 @@ public function process_extension(): void {
$assessment->apply_extension($this);
}
} catch (\Exception $e) {
// Consider logging the exception here.
continue;
logger::log($e->getMessage());
}
}
}
Expand Down Expand Up @@ -194,23 +237,13 @@ protected function remove_user_from_previous_sora_groups(int $newgroupid, int $c
}

/**
* Set the SORA properties.
* Calculate the time extension in seconds.
*
* @param int $extraduration Extra duration in minutes.
* @param int $restduration Rest duration in minutes.
* @return int
*/
private function set_sora_properties(): void {
global $DB;

// Set the event data.
$this->eventdata = $this->message->event;

// Set the SORA data.
$this->soradata = $this->message->entity->metadata->sora;

// Find and set the user ID of the student.
$idnumber = $this->soradata->person->student_code;
$user = $DB->get_record('user', ['idnumber' => $idnumber], 'id', MUST_EXIST);
$this->userid = $user->id;

// Set the time extension in seconds.
$this->timeextension = ($this->get_extra_duration() + $this->get_rest_duration()) * MINSECS;
private function calculate_time_extension(int $extraduration, int $restduration): int {
return ($extraduration + $restduration) * MINSECS;
}
}
67 changes: 67 additions & 0 deletions classes/extensionmanager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace local_sitsgradepush;

use local_sitsgradepush\extension\sora;

/**
* Manager class for extension related operations.
*
* @package local_sitsgradepush
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Alex Yeung <k.yeung@ucl.ac.uk>
*/
class extensionmanager {

/**
* Update SORA extension for students in a mapping.
*
* @param int $mapid
* @return void
* @throws \dml_exception
*/
public static function update_sora_for_mapping(int $mapid): 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);
}

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

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

// Process SORA extension for each student.
foreach ($students as $student) {
$sora = new sora();
$sora->set_properties_from_get_students_api($student);
$sora->process_extension();
}
} catch (\Exception $e) {
logger::log($e->getMessage(), null, "Mapping ID: $mapid");
}
}
}
Loading

0 comments on commit 842490b

Please sign in to comment.