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.41 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
22
\define('TEAMCONFIG_METHOD_STUDENTS', 1);
\define('TEAMCONFIG_METHOD_RANDOM', 3);
23

Francois Gannaz's avatar
Francois Gannaz committed
24
25
26
27
28
29
/**
 * 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
30
31
class mod_labnbook_mod_form extends \moodleform_mod
{
Francois Gannaz's avatar
Francois Gannaz committed
32
    /**
Francois Gannaz's avatar
Francois Gannaz committed
33
     * Defines forms elements.
Francois Gannaz's avatar
Francois Gannaz committed
34
     */
Francois Gannaz's avatar
Francois Gannaz committed
35
36
    public function definition()
    {
Francois Gannaz's avatar
Francois Gannaz committed
37
38
39
40
        global $CFG;

        $mform = $this->_form;

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

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

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

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

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

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

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

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

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

        // Adding the standard "name" field.
137
        $mform->addElement('text', 'name', get_string('instancename', 'mod_labnbook'), ['size' => '64']);
Francois Gannaz's avatar
Francois Gannaz committed
138
139
140
141
142
143
144
        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');
145
        $mform->addHelpButton('name', 'instancename', 'mod_labnbook');
Francois Gannaz's avatar
Francois Gannaz committed
146
147
148
149
150
151
152
153

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

154
155
156
157
158
159
160
161
162
        $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'),
        ];
        $mform->addElement('select', 'method', get_string('method', 'mod_labnbook'), $options);
        $mform->addHelpButton('method', 'method', 'mod_labnbook');
163
        $mform->setType('method', PARAM_INT);
164
165
166
167
168
169
        $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);
170
171
        $mform->addElement('hidden', 'labnbook_teamconfigid', false);
        $mform->setType('labnbook_teamconfigid', PARAM_INT);
172

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

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

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

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    /**
     * 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
205
206
207
    /**
     * Hook called once the form data has been loaded into the elements.
     */
Francois Gannaz's avatar
Francois Gannaz committed
208
209
    public function definition_after_data()
    {
210
211
        parent::definition_after_data();
        if ($this->_form->getElementValue('update')) {
212
213
214
215
            //$this->_form->freeze('labnbook_missionid');
        }
    }

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

    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
235
}