Skip to content

Commit

Permalink
CTP-2779 Allow multiple assessemnt component mappings to a single moo…
Browse files Browse the repository at this point in the history
…dle activity
  • Loading branch information
aydevworks committed Nov 3, 2023
1 parent a42dd91 commit 82dd384
Show file tree
Hide file tree
Showing 16 changed files with 895 additions and 280 deletions.
111 changes: 111 additions & 0 deletions apiclients/easikit/classes/requests/getstudents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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 sitsapiclient_easikit\requests;

use local_sitsgradepush\cachemanager;

/**
* Class for getstudents request.
*
* @package sitsapiclient_easikit
* @copyright 2023 onwards University College London {@link https://www.ucl.ac.uk/}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Alex Yeung <[email protected]>
*/
class getstudents extends request {

/** @var string[] Fields mapping - Local data fields to SITS' fields */
const FIELDS_MAPPING = [
'mapcode' => 'MAP_CODE',
'mabseq' => 'MAB_SEQ',
];

/** @var string request method */
const METHOD = 'GET';

/**
* Constructor.
*
* @param \stdClass $data
* @throws \dml_exception
* @throws \moodle_exception
*/
public function __construct(\stdClass $data) {
// Set request name.
$this->name = 'Get students';

// Get request endpoint.
$endpointurl = get_config('sitsapiclient_easikit', 'endpoint_get_student');

// Check if endpoint is set.
if (empty($endpointurl)) {
throw new \moodle_exception('Endpoint URL for ' . $this->name . ' is not set');
}

// Set the fields mapping, params fields and data.
parent::__construct(self::FIELDS_MAPPING, $endpointurl, $data);
}

/**
* Process returned response.
*
* @param mixed $response
* @return array
*/
public function process_response($response): array {
$result = [];
if (!empty($response)) {
// Convert response to suitable format.
$response = json_decode($response, true);

if (!empty($response['response']['student_collection']['student'])) {
// Build cache.
$key = implode('_', [
cachemanager::CACHE_AREA_STUDENTSPR,
$this->paramsdata['MAP_CODE'],
$this->paramsdata['MAB_SEQ'],
]);

cachemanager::set_cache(
cachemanager::CACHE_AREA_STUDENTSPR,
$key,
$response['response']['student_collection']['student'],
strtotime('+30 days'),
);

$result = $response['response']['student_collection']['student'];
}
}

return $result;
}

/**
* Get endpoint url with params.
*
* @return string
*/
public function get_endpoint_url_with_params(): string {
// Return endpoint url with params.
return sprintf(
'%s/%s-%s/student',
$this->endpointurl,
$this->paramsdata['MAP_CODE'],
$this->paramsdata['MAB_SEQ']
);
}
}
4 changes: 4 additions & 0 deletions apiclients/easikit/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use sitsapiclient_easikit\requests\getcomponentgrade;
use sitsapiclient_easikit\requests\getmarkingschemes;
use sitsapiclient_easikit\requests\getstudent;
use sitsapiclient_easikit\requests\getstudents;
use sitsapiclient_easikit\requests\pushgrade;
use sitsapiclient_easikit\requests\pushsubmissionlog;
use sitsapiclient_easikit\requests\request;
Expand Down Expand Up @@ -66,6 +67,9 @@ public function build_request(string $action, \stdClass $data = null, submission
case manager::GET_STUDENT:
$request = new getstudent($data);
break;
case manager::GET_STUDENTS:
$request = new getstudents($data);
break;
case manager::PUSH_SUBMISSION_LOG:
$request = new pushsubmissionlog($data, $submission);
break;
Expand Down
70 changes: 70 additions & 0 deletions classes/cachemanager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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 cache;
use cache_application;
use cache_session;
use cache_store;

/**
* Cache manager class for handling caches.
*
* @package local_sitsgradepush
* @copyright 2023 onwards University College London {@link https://www.ucl.ac.uk/}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Alex Yeung <[email protected]>
*/
class cachemanager {
/** @var string Cache area for storing students in an assessment component.*/
const CACHE_AREA_STUDENTSPR = 'studentspr';

/**
* Get cache.
*
* @param string $area
* @param string $key
* @return cache_application|cache_session|cache_store|null
* @throws \coding_exception
*/
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) {
return null;
} else {
return $cache;
}
}

/**
* Set cache.
*
* @param string $area
* @param string $key
* @param mixed $value
* @param int $expiresafter
* @return void
*/
public static function set_cache(string $area, string $key, mixed $value, int $expiresafter) {
$cache = cache::make('local_sitsgradepush', $area);
$cache->set($key, $value);
$cache->set('expires_' . $key, $expiresafter);
}
}
Loading

0 comments on commit 82dd384

Please sign in to comment.