Commit 30f2ced7 authored by Francois Gannaz's avatar Francois Gannaz
Browse files

Activity form: button for refreshing the mission list

parent c7157b6c
......@@ -58,6 +58,8 @@ class mod_labnbook_session
/**
* Empties the cache and session, so forces to fetch data from the LabNbook server.
*
* @return mod_labnbook_session
*/
public function refresh() {
global $SESSION;
......@@ -65,6 +67,7 @@ class mod_labnbook_session
unset($SESSION->labnbook_userid);
unset($this->missions);
unset($SESSION->labnbook_missions);
return $this;
}
/**
......@@ -85,9 +88,9 @@ class mod_labnbook_session
/**
* @return array mission[]
*/
public function getMissions() {
if (!isset($this->missions)) {
$this->missions = $this->fetchMissions();
public function getMissions($refresh = false) {
if ($refresh || !isset($this->missions)) {
$this->missions = $this->fetchMissions($refresh);
}
return $this->missions;
}
......@@ -164,12 +167,12 @@ class mod_labnbook_session
/**
* @global object $SESSION
*
* @param boolean $force
* @param boolean $refresh
* @return array assoc: ID => name
*/
protected function fetchMissions($force = false) {
protected function fetchMissions($refresh = false) {
global $SESSION;
if ($force || !isset($SESSION->labnbook_missions)) {
if ($refresh || !isset($SESSION->labnbook_missions)) {
$userId = $this->getUserId();
if ($userId) {
$SESSION->labnbook_missions = $this->fetch('GET', "/teacher/{$userId}/mission/list", []);
......
......@@ -49,4 +49,5 @@ $string['mission_help'] = "Select a mission among those you own in LabNbook. If
$string['labnbook_url'] = "LabNbook URL";
$string['labnbook_url_descr'] = "The URL to the root of labnbook, with a trailing slash. E.g. https://uga.labnbook.fr/";
$string['labnbook_key'] = "LabNbook Key";
$string['labnbook_key_descr'] = "The secret key that grants Moodle acces the API of LabNbook. This key is provided by the LabNbook instance.";
$string['labnbook_key_descr'] = "The secret key that grants Moodle access to the API of LabNbook. This key is provided by the LabNbook instance.";
$string['refresh'] = "refresh";
......@@ -46,8 +46,30 @@ class mod_labnbook_mod_form extends moodleform_mod {
$mform = $this->_form;
$missions = session::load()->getMissions();
$mform->addElement('html', '<script>var missions = ' . json_encode($missions) . ';</script>');
$missionsJs = json_encode($missions);
$refreshUrl = (new moodle_url('/mod/labnbook/refresh.php'))->out(false);
$mform->addElement('html', <<<"EOJS"
<script>
var missions = $missionsJs;
function refreshMissions() {
fetch('$refreshUrl').then(r => {
return r.json();
}).then(ms => {
missions = {};
document.querySelector('#id_labnbook_missionid').innerHTML = '<option value="">-</option>';
for (m of ms) {
missions[m.id_mission] = m;
var e = document.createElement('option');
e.setAttribute('value', m.id_mission);
e.innerText = '[' + m.code + '] ' + m.name;
document.querySelector('#id_labnbook_missionid').appendChild(e);
}
});
}
</script>
EOJS
);
$missionsList = array_map(
function($x) { return "[{$x->code}] {$x->name}"; },
(array) $missions
......@@ -60,7 +82,7 @@ class mod_labnbook_mod_form extends moodleform_mod {
array_merge(['' => "-"], $missionsList),
[
'onchange' => 'javascript: '
. 'document.querySelector("#id_name").value = missions[parseInt(this.value)].name;'
. 'document.querySelector("#id_name").value = missions[this.value].name;'
// try to update the Atto text, but Atto is undocumented stuff (wiki pages are all dead)
. 'document.querySelector("#id_introeditor").innerHTML = missions[this.value].description;'
. 'document.querySelector("#id_introeditoreditable").innerHTML = missions[this.value].description;'
......@@ -68,6 +90,13 @@ class mod_labnbook_mod_form extends moodleform_mod {
);
$mform->addRule('labnbook_missionid', null, 'required', null, 'client');
$mform->addElement(
'static', '', '',
'<button type="button" onclick="refreshMissions()">'
. get_string('refresh', 'mod_labnbook')
. '</a>'
);
// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));
......
<?php
/*
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
*/
use \mod_labnbook_session as session;
require(__DIR__.'/../../config.php');
require_once(__DIR__.'/lib.php');
require_login();
$missions = session::load()->refresh()->getMissions();
header("Content-type: application/json");
echo json_encode(array_values((array) $missions));
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