Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

mod_form.php 8.55 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
5
6
7
8
<?php

/**
 * The main mod_labnbook configuration form.
 *
 * @copyright   2019 Université Grenoble Alpes
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
9
10
use mod_labnbook\fetch;
use mod_labnbook\form\select_dynamic;
11
use mod_labnbook\local\helper;
12

Francois Gannaz's avatar
Francois Gannaz committed
13
\defined('MOODLE_INTERNAL') || die();
Francois Gannaz's avatar
Francois Gannaz committed
14

Francois Gannaz's avatar
Francois Gannaz committed
15
require_once($CFG->dirroot . '/course/moodleform_mod.php');
Francois Gannaz's avatar
Francois Gannaz committed
16

Francois Gannaz's avatar
Francois Gannaz committed
17
// short-cut before loading the form, see `course/modedit.php`
18
19
helper::requireLabnbookAuthentication();
helper::grantTeacherAccess($COURSE->id);
Francois Gannaz's avatar
Francois Gannaz committed
20

Francois Gannaz's avatar
Francois Gannaz committed
21
\define('TEAMCONFIG_METHOD_STUDENTS', 1);
David Beniamine's avatar
David Beniamine committed
22
\define('TEAMCONFIG_METHOD_TEACHER', 2);
Francois Gannaz's avatar
Francois Gannaz committed
23
\define('TEAMCONFIG_METHOD_RANDOM', 3);
24

Francois Gannaz's avatar
Francois Gannaz committed
25
26
27
28
29
30
/**
 * Module instance settings form.
 *
 * @copyright  2019 Université Grenoble Alpes
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Francois Gannaz's avatar
Francois Gannaz committed
31
32
class mod_labnbook_mod_form extends \moodleform_mod
{
Francois Gannaz's avatar
Francois Gannaz committed
33
    /**
Francois Gannaz's avatar
Francois Gannaz committed
34
     * Defines forms elements.
Francois Gannaz's avatar
Francois Gannaz committed
35
     */
Francois Gannaz's avatar
Francois Gannaz committed
36
37
    public function definition()
    {
Francois Gannaz's avatar
Francois Gannaz committed
38
39
40
41
        global $CFG;

        $mform = $this->_form;

Francois Gannaz's avatar
Francois Gannaz committed
42
        $mform->addElement('header', 'external', "LabNbook");
43
        $dynamicSelect = new select_dynamic(
Francois Gannaz's avatar
Francois Gannaz committed
44
45
            'labnbook_missionid',
            get_string('mission', 'mod_labnbook'),
46
            ['0' => "-"],
Francois Gannaz's avatar
Francois Gannaz committed
47
48
            [
                'onchange' => 'javascript: '
49
                    . 'document.querySelector("#id_name").value = getMission(this.value).name;'
Francois Gannaz's avatar
Francois Gannaz committed
50
                    // update the Atto text, but Atto is undocumented stuff (wiki pages are all dead)
51
                    . 'document.querySelector("#id_introeditoreditable").innerHTML = getMission(this.value).description;'
Francois Gannaz's avatar
Francois Gannaz committed
52
                    . 'document.querySelector("#id_introeditor").innerHTML = getMission(this.value).description;',
53
                    // TODO update the TinyMCE text. Is there a way to detect the editor plugin? A common API for pluggable editors?
54
            ]
Francois Gannaz's avatar
Francois Gannaz committed
55
        );
56
57
        $mform->addElement($dynamicSelect);
        $mform->setType('labnbook_missionid', PARAM_INT);
Francois Gannaz's avatar
Francois Gannaz committed
58
59
        $mform->addRule('labnbook_missionid', null, 'required', null, 'client');

Francois Gannaz's avatar
Francois Gannaz committed
60
61
62
63
        $fetchMissions = (new fetch\user())->getJsMissions();
        $mform->addElement(
            'html',
            <<<"EOJS"
64
<script>
Francois Gannaz's avatar
Francois Gannaz committed
65
var labnbookMissions = new Map();
66
67
68
69
function getMission(id) {
    return labnbookMissions.get(parseInt(id));
}

Francois Gannaz's avatar
Francois Gannaz committed
70
var refreshLock = 0;
71
function refreshMissions() {
Francois Gannaz's avatar
Francois Gannaz committed
72
73
74
75
    if (refreshLock) {
        return;
    }
    refreshLock = 1;
76
    var container = document.querySelector('select[name="labnbook_missionid"]');
Francois Gannaz's avatar
Francois Gannaz committed
77
78

    var loading = document.createElement('span');
79
    loading.setAttribute('class', 'fa fa-spinner fa-pulse fa-fw');
Francois Gannaz's avatar
Francois Gannaz committed
80
    loading.setAttribute('id', 'labnbook-missions-loading');
81
    document.querySelector('#refresh-missions').parentNode.appendChild(loading);
Francois Gannaz's avatar
Francois Gannaz committed
82
83
84
85
86

    $fetchMissions.then(missions => {
        refreshLock = 0;
        document.querySelector('#labnbook-missions-loading').remove();

87
        container.innerHTML = '<option value="0">-</option>';
Francois Gannaz's avatar
Francois Gannaz committed
88
89
        for (m of missions) {
            labnbookMissions.set(m.id_mission, m);
90
91
92
            var e = document.createElement('option');
            e.setAttribute('value', m.id_mission);
            e.innerText = '[' + m.code + '] ' + m.name;
Francois Gannaz's avatar
Francois Gannaz committed
93
            container.appendChild(e);
94
        }
95
96
97
        if (typeof(window.missionid) != 'undefined') {
            document.querySelector('select[name="labnbook_missionid"]').value = window.missionid;
        }
Francois Gannaz's avatar
Francois Gannaz committed
98
99
    }).catch(() => {
        refreshLock = 0;
100
        document.querySelector('#labnbook-missions-loading').remove();
Francois Gannaz's avatar
Francois Gannaz committed
101
        // TODO: error, what shall we do?
102
        alert("Erreur...");
103
104
    });
}
105
106
107
108

document.addEventListener("DOMContentLoaded", function(event) { 
    refreshMissions();
})
109
110
111
112
</script>
EOJS
        );
        $mform->addElement(
Francois Gannaz's avatar
Francois Gannaz committed
113
114
115
            'static',
            'refresh',
            '',
116
            '<button type="button" onclick="refreshMissions()" id="refresh-missions">'
117
118
119
                . get_string('refresh', 'mod_labnbook')
                . '</a>'
        );
120
121
122
        $mform->addHelpButton('refresh', 'refresh', 'mod_labnbook');

        $fetcher = new fetch\User();
Francois Gannaz's avatar
Francois Gannaz committed
123
        $createMissionUrl = $fetcher->getRedirectUrl() . "?token=" . $fetcher->getMissionTokenForRedirect();
124
        $mform->addElement(
Francois Gannaz's avatar
Francois Gannaz committed
125
126
127
128
            'static',
            'addMission',
            '',
            '<a href="' . $createMissionUrl . '" target="_blank">'
129
130
131
132
                . get_string('addMission', 'mod_labnbook')
                . '</a>'
        );
        $mform->addHelpButton('addMission', 'addMission', 'mod_labnbook');
133

Francois Gannaz's avatar
Francois Gannaz committed
134
135
136
137
        // Adding the "general" fieldset, where all the common settings are showed.
        $mform->addElement('header', 'general', get_string('general', 'form'));

        // Adding the standard "name" field.
138
        $mform->addElement('text', 'name', get_string('instancename', 'mod_labnbook'), ['size' => '64']);
Francois Gannaz's avatar
Francois Gannaz committed
139
140
141
142
143
144
145
        if (!empty($CFG->formatstringstriptags)) {
            $mform->setType('name', PARAM_TEXT);
        } else {
            $mform->setType('name', PARAM_CLEANHTML);
        }
        $mform->addRule('name', null, 'required', null, 'client');
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
146
        $mform->addHelpButton('name', 'instancename', 'mod_labnbook');
Francois Gannaz's avatar
Francois Gannaz committed
147
148
149
150
151
152
153
154

        // Adding the standard "intro" and "introformat" fields.
        if ($CFG->branch >= 29) {
            $this->standard_intro_elements();
        } else {
            $this->add_intro_editor();
        }

155
156
157
158
159
160
        $mform->addElement('header', 'teaming', get_string('teaming', 'mod_labnbook'));

        $options = [
            "" => "-",
            TEAMCONFIG_METHOD_STUDENTS => get_string('teamconfigmethod_students', 'mod_labnbook'),
            TEAMCONFIG_METHOD_RANDOM => get_string('teamconfigmethod_random', 'mod_labnbook'),
David Beniamine's avatar
David Beniamine committed
161
            TEAMCONFIG_METHOD_TEACHER => get_string('teamconfigmethod_teacher', 'mod_labnbook'),
162
163
164
        ];
        $mform->addElement('select', 'method', get_string('method', 'mod_labnbook'), $options);
        $mform->addHelpButton('method', 'method', 'mod_labnbook');
165
        $mform->setType('method', PARAM_INT);
166
167
168
169
170
171
        $mform->addRule("method", null, 'required', null, 'client');

        $this->addInputInt('teams_max', true);
        $this->addInputInt('size_opt', true);
        $this->addInputInt('size_max', false);
        $this->addInputInt('size_min', false);
172
173
        $mform->addElement('hidden', 'labnbook_teamconfigid', false);
        $mform->setType('labnbook_teamconfigid', PARAM_INT);
174

Francois Gannaz's avatar
Francois Gannaz committed
175
176
177
178
179
180
181
182
183
        // Add standard grading elements.
        $this->standard_grading_coursemodule_elements();

        // Add standard elements.
        $this->standard_coursemodule_elements();

        // Add standard buttons.
        $this->add_action_buttons();
    }
184

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    /**
     * Allows module to modify data returned by get_moduleinfo_data() or prepare_new_moduleinfo_data() before calling set_data()
     * This method is also called in the bulk activity completion form.
     *
     * Only available on moodleform_mod.
     *
     * @param array $default_values passed by reference
     */
    public function data_preprocessing(&$default_values){
        parent::data_preprocessing($default_values);
        // The labnbook missionid will not be correctly set as option list is retrieved by ajax
        // Thus we store the id and let the ajax use it later
        echo '<script>window.missionid='.$default_values['labnbook_missionid'].';</script>';
        // Retrieve teamconfig data
        $fetcher = new fetch\User();
        $teamconfig = $fetcher->getTeamconfig($default_values['labnbook_teamconfigid']);
        foreach (['method', 'size_min', 'size_max', 'size_opt', 'teams_max'] as $field) {
            $default_values[$field]= $teamconfig->$field;
        }
    }


Francois Gannaz's avatar
Francois Gannaz committed
207
208
209
    /**
     * Hook called once the form data has been loaded into the elements.
     */
Francois Gannaz's avatar
Francois Gannaz committed
210
211
    public function definition_after_data()
    {
212
213
        parent::definition_after_data();
        if ($this->_form->getElementValue('update')) {
214
215
216
217
            //$this->_form->freeze('labnbook_missionid');
        }
    }

Francois Gannaz's avatar
Francois Gannaz committed
218
219
    public function data_postprocessing($data)
    {
220
221
222
223
224
        if (empty($data->size_min)) {
            $data->size_min = 1;
        }
        if (empty($data->size_max)) {
            $data->size_max = $data->size_opt + 1;
225
226
        }
    }
Francois Gannaz's avatar
Francois Gannaz committed
227
228
229
230
231
232
233
234
235
236

    private function addInputInt($name, $required)
    {
        $this->_form->addElement('text', $name, get_string($name, 'mod_labnbook'), ['size' => '3']);
        $this->_form->setType($name, PARAM_INT);
        if ($required) {
            $this->_form->addRule($name, null, 'required', null, 'client');
            $this->_form->addHelpButton($name, $name, 'mod_labnbook');
        }
    }
Francois Gannaz's avatar
Francois Gannaz committed
237
}