Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LabNbook
LabNbook-Moodle
Commits
30f2ced7
Commit
30f2ced7
authored
Mar 08, 2019
by
Francois Gannaz
Browse files
Activity form: button for refreshing the mission list
parent
c7157b6c
Changes
4
Hide whitespace changes
Inline
Side-by-side
classes/session.php
View file @
30f2ced7
...
...
@@ -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"
,
[]);
...
...
lang/en/labnbook.php
View file @
30f2ced7
...
...
@@ -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"
;
mod_form.php
View file @
30f2ced7
...
...
@@ -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'
));
...
...
refresh.php
0 → 100644
View file @
30f2ced7
<?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
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment