Commit c8b13236 authored by Francois Gannaz's avatar Francois Gannaz
Browse files

class mod_labnbook_session to store and query LnN info

parent 2d3747c2
<?php
/**
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
*/
/* @var $DB moodle_database */
/**
* Queries on LabNbook info for the current user.
*/
class mod_labnbook_session
{
private static $singleton;
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $secret;
/**
* @var int
*/
private $userId;
/**
* @var array
*/
private $missions = [];
private $identifyByCasAuth = true;
private $identifyByStudentNumber = true;
private $autoCreateUser = true;
public static function load() {
if (empty(self::$singleton)) {
self::$singleton = new self;
}
return self::$singleton;
}
public function __construct() {
global $CFG, $USER;
$this->url = rtrim($CFG->labnbook_url, '/');
$this->secret = $CFG->labnbook_key;
$this->userId = $this->fetchUserId($USER->id);
}
/**
* Search the LabNbook user ID in the cache (object attribute),
* then in the user session,
* then the DB,
* then through the LabNbook REST API (first GET, then PUT).
*
* @return int
*/
public function getUserId() {
if (!isset($this->userId)) {
$this->userId = $this->fetchUserId();
}
return $this->userId;
}
public function getMissions() {
if (!isset($this->missions)) {
$this->missions = $this->fetchMissions();
}
return $this->missions;
}
/**
* @global moodle_database $DB
* @global object $SESSION
* @global object $USER
* @param boolean $force
* @return int
*/
protected function fetchUserId($force = false) {
global $DB, $SESSION, $USER;
if ($force || !isset($SESSION->labnbook_userid)) {
$userId = $DB->get_field("mod_labnbook_user", "labnbook_userid", ['userid' => $USER->id]);
if (!$userId && $this->identifyByStudentNumber && $USER->idnumber) {
$userId = $this->fetch('GET', "/user/identify", null, ['idnumber' => $USER->idnumber]);
}
if (!$userId && $this->identifyByCasAuth && $USER->auth === 'cas') {
$userId = $this->fetch('GET', "/user/identify", null, ['username' => $USER->username]);
}
if (!$userId && $this->autoCreateUser) {
$user = array_filter(
(array) $USER,
function($k) {
$want = ['username', 'firstname', 'lastname', 'email'];
return in_array($k, $want);
},
ARRAY_FILTER_USE_KEY
);
/**
* @todo Add the boolean attribute 'teacher', true if user has editing rights in the current course.
*/
$userId = $this->fetch('PUT', "/user/create", null, $user);
}
$SESSION->labnbook_userid = (int) $userId;
}
return $SESSION->labnbook_userid;
}
/**
* @global object $SESSION
* @param boolean $force
* @return array assoc: ID => name
*/
protected function fetchMissions($force = false) {
global $SESSION;
if ($force || !isset($SESSION->labnbook_missions)) {
$userId = $this->getUserId();
if ($userId) {
$SESSION->labnbook_missions = $this->fetch('GET', "/teacher/{$userId}/mission/list", []);
} else {
$SESSION->labnbook_missions = [];
}
}
return $SESSION->labnbook_missions;
}
/**
* Query the LabNbook API.
*
* @param string $urlPath
* @param mixed $defaultValue
* @param array $payload
* @return mixed
*/
protected function fetch($verb, $urlPath, $defaultValue = null, $payload = []) {
$payload['key'] = $this->secret;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST, $verb,
CURLOPT_URL => $this->url . "/api/index.php" . $urlPath,
CURLOPT_HTTPHEADER, ['Content-Type:application/json', 'Accept:application/json'],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if ($response === false || $responseCode != 200) {
// log the error?
// See curl_error($curl);
curl_close($curl);
return $defaultValue;
}
curl_close($curl);
return json_encode($response);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment