Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1434 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Manage module notifications form.
19
 *
20
 * @package     local_moofactory_notification
21
 * @copyright   2020 Patrick ROCHET <patrick.r@lmsfactory.com>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
require_once($CFG->libdir . '/formslib.php');
28
require_once('lib.php');
29
 
30
class module_form extends moodleform
31
{
32
    public function definition()
33
    {
34
        global $CFG, $DB, $PAGE;
35
 
36
        $mform = $this->_form;
37
        $courseid = $this->_customdata['courseid'];
38
        $id = $this->_customdata['id'];
39
 
40
 
41
        $mform->addElement('hidden', 'courseid');
42
        $mform->setType('courseid', PARAM_RAW);
43
        $mform->setConstant('courseid', $courseid);
44
 
45
        $mform->addElement('hidden', 'id', $id);
46
        $mform->setType('id', PARAM_RAW);
47
 
48
        $mform->addElement('html', '<br>');
49
 
50
        $moduleeventsname = 'moduleevents_' . $courseid . '_' . $id;
51
        $mform->addElement('checkbox', $moduleeventsname, get_string('moduleevents', 'local_moofactory_notification'));
52
        $moduleeventsvalue = get_config('local_moofactory_notification', $moduleeventsname);
53
        $mform->setDefault($moduleeventsname, $moduleeventsvalue);
54
 
55
        $modulecheckavailabilityname = 'modulecheckavailability_' . $courseid . '_' . $id;
56
        if (empty($moduleeventsvalue)) {
57
            $mform->addElement('checkbox', $modulecheckavailabilityname, get_string('modulecheckavailability', 'local_moofactory_notification'), '', array('disabled' => 'disabled'));
58
        } else {
59
            $mform->addElement('checkbox', $modulecheckavailabilityname, get_string('modulecheckavailability', 'local_moofactory_notification'));
60
        }
61
 
62
        $value = get_config('local_moofactory_notification', $modulecheckavailabilityname);
63
        if ($value === false) {
64
            $value = local_moofactory_notification_getCustomfield($courseid, 'courseeventscheckavailability', 'checkbox');
65
        }
66
        $mform->setDefault($modulecheckavailabilityname, $value);
67
 
68
        $modulecheckdateavailabilityname = 'modulecheckdateavailability_' . $courseid . '_' . $id;
69
        if (empty($moduleeventsvalue)) {
70
            $mform->addElement('checkbox', $modulecheckdateavailabilityname, get_string('modulecheckdateavailability', 'local_moofactory_notification'), '', array('disabled' => 'disabled'));
71
        } else {
72
            $mform->addElement('checkbox', $modulecheckdateavailabilityname, get_string('modulecheckdateavailability', 'local_moofactory_notification'));
73
        }
74
 
75
        $value = get_config('local_moofactory_notification', $modulecheckdateavailabilityname);
76
        if ($value === false) {
77
            $value = local_moofactory_notification_getCustomfield($courseid, 'courseeventscheckdateavailability', 'checkbox');
78
        }
79
        $mform->setDefault($modulecheckdateavailabilityname, $value);
80
 
81
        $modulecheckgroupavailabilityname = 'modulecheckgroupavailability_' . $courseid . '_' . $id;
82
        if (empty($moduleeventsvalue)) {
83
            $mform->addElement('checkbox', $modulecheckgroupavailabilityname, get_string('modulecheckgroupavailability', 'local_moofactory_notification'), '', array('disabled' => 'disabled'));
84
        } else {
85
            $mform->addElement('checkbox', $modulecheckgroupavailabilityname, get_string('modulecheckgroupavailability', 'local_moofactory_notification'));
86
        }
87
 
88
        $value = get_config('local_moofactory_notification', $modulecheckgroupavailabilityname);
89
        if ($value === false) {
90
            $value = local_moofactory_notification_getCustomfield($courseid, 'courseeventscheckgroupavailability', 'checkbox');
91
        }
92
        $mform->setDefault($modulecheckgroupavailabilityname, $value);
93
 
94
        $modulenotificationname = 'modulenotification_' . $courseid . '_' . $id;
95
        $records = $DB->get_records('local_mf_notification', array('type' => 'courseevent'));
96
        foreach ($records as $record) {
97
            $options[$record->id] = $record->name;
98
        }
99
        if (empty($moduleeventsvalue)) {
100
            $mform->addElement('select', $modulenotificationname, get_string('usednotification', 'local_moofactory_notification'), $options, array('disabled' => 'disabled'));
101
        } else {
102
            $mform->addElement('select', $modulenotificationname, get_string('usednotification', 'local_moofactory_notification'), $options);
103
        }
104
 
105
        $value = get_config('local_moofactory_notification', $modulenotificationname);
106
        if (empty($value)) {
107
            $value = (int)local_moofactory_notification_getCustomfield($courseid, 'courseeventsnotification', 'select');
108
            if (!empty($value)) {
109
                $value--;
110
                $courseeventsnotifications = array_values($records);
111
                $value = $courseeventsnotifications[$value]->id;
112
            } else {
113
                $value = get_config('local_moofactory_notification', 'courseseventsnotification');
114
            }
115
        }
116
        $mform->setDefault($modulenotificationname, $value);
117
 
118
        $configvars = ['daysbeforeevents1', 'hoursbeforeevents1', 'daysbeforeevents2', 'hoursbeforeevents2', 'daysbeforeevents3', 'hoursbeforeevents3'];
119
 
120
        // Tableau pour stocker les groupes de champs pour l'affichage
121
        $eventGroups = [];
122
 
123
        foreach ($configvars as $index => $configvar) {
124
            $name = 'module' . $configvar . '_' . $courseid . '_' . $id;
125
 
126
            $attributes = ['maxlength' => 3, 'size' => 3];
127
            // Si l'événement est désactivé on bloque la saisie
128
            if (empty($moduleeventsvalue)) {
129
                $attributes['disabled'] = 'disabled';
130
            }
131
 
132
            $daysField = $mform->createElement('text', $name, '', $attributes);
133
            $mform->setType($name, PARAM_TEXT);
134
 
135
            // Récupérer la valeur par défaut pour les jours
136
            $value = get_config('local_moofactory_notification', $name);
137
            if ($value === false) {
138
                $value = local_moofactory_notification_getCustomfield($courseid, $configvar, 'text');
139
            }
140
            $mform->setDefault($name, $value);
141
 
142
            // Créer un champ statique pour afficher "jour(s)" après le champ de texte
143
            $daysDescription = $index % 2 === 0 ? 'jour(s)' : 'heure(s)';
144
            $daysDescriptionElement = $mform->createElement('static', $name . '_desc', '', $daysDescription);
145
 
146
            // Ajout des champs jours et description dans un groupe
147
            if ($index % 2 === 0) {
148
                $eventGroups[] = [$daysField, $daysDescriptionElement];
149
            } else {
150
                $eventGroups[count($eventGroups) - 1][] = $daysField;
151
                $eventGroups[count($eventGroups) - 1][] = $daysDescriptionElement;
152
            }
153
        }
154
 
155
        // Ajouter les groupes au formulaire
156
        $numrappel = 1;
157
        foreach ($eventGroups as $group) {
158
            $groupName = 'event_group_' . $courseid;
159
            $mform->addGroup($group, $groupName, get_string('daysbeforeevents' . $numrappel++, 'local_moofactory_notification'), [' '], false);
160
        }
161
 
162
        /*********** form notification après levée des restrictions d’accès ************/
163
        $mform->addElement('html', '<hr>');
164
        $mform->addElement('html', '<h3>' . get_string('moduleaccesstitle', 'local_moofactory_notification') . '</h3>');
165
 
166
        $moduleleveename = 'modulelevee_' . $courseid . '_' . $id;
167
        $mform->addElement('checkbox', $moduleleveename, get_string('moduleaccess', 'local_moofactory_notification'));
168
        $moduleleveevalue = get_config('local_moofactory_notification', $moduleleveename);
169
        $mform->setDefault($moduleleveename, $moduleleveevalue);
170
 
171
        $moduleleveenotificationname = 'moduleleveenotification_' . $courseid . '_' . $id;
172
        $records = $DB->get_records('local_mf_notification', array('type' => 'moduleaccess'));
173
 
174
        foreach ($records as $record) {
175
            $optionslevee[$record->id] = $record->name;
176
        }
177
        if (empty($moduleleveevalue)) {
178
            $mform->addElement('select', $moduleleveenotificationname, get_string('usednotification', 'local_moofactory_notification'), $optionslevee, array('disabled' => 'disabled'));
179
        } else {
180
            $mform->addElement('select', $moduleleveenotificationname, get_string('usednotification', 'local_moofactory_notification'), $optionslevee);
181
        }
182
 
183
        $value = get_config('local_moofactory_notification', $moduleleveenotificationname);
184
 
185
        $mform->setDefault($moduleleveenotificationname, $value);
186
 
187
        $copiemaillevee = 'copiemaillevee_' . $courseid . '_' . $id;
188
        $mform->addElement('text', $copiemaillevee, get_string('copienotif', 'local_moofactory_notification'),array('class' => 'copiemaillevee'));
189
 
190
        $mform->addRule($copiemaillevee, get_string('maxlength', 'form', 255), 'maxlength', 255, 'client');
191
        $mform->addRule($copiemaillevee, get_string('invalidemail', 'error'), 'regex', '/^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}[;,]?\s*)+$/', 'client');
192
        $mform->setType($copiemaillevee, PARAM_TEXT);
193
        $value = get_config('local_moofactory_notification', $copiemaillevee);
194
 
195
        if($value){
196
            $mform->setDefault($copiemaillevee, $value);
197
        }
198
 
199
        $nameleveedelay = 'moduleleveedelai_' . $courseid . '_' . $id;
200
 
201
        // Ajouter un groupe contenant l'input et le texte "minute(s)"
202
        $groupdiv = [];
203
        if (empty($moduleleveevalue)) {
204
            $groupdiv[] = $mform->createElement('text', $nameleveedelay, '', array('maxlength' => 3, 'size' => 3, 'disabled' => 'disabled'));
205
        } else {
206
            $groupdiv[] = $mform->createElement('text', $nameleveedelay, '', array('maxlength' => 3, 'size' => 3));
207
        }
208
 
209
        // Ajouter le texte "minute(s)" dans le même groupe
210
        $groupdiv[] = $mform->createElement('static', 'minutes_label', '', get_string('moduleleveetime_desc', 'local_moofactory_notification'));
211
 
212
        // Ajouter le groupe au formulaire
213
        $mform->addGroup($groupdiv, $nameleveedelay . '_group', get_string('leveetime', 'local_moofactory_notification'), [' '], false);
214
 
215
        $mform->setType($nameleveedelay, PARAM_TEXT);
216
        $leveedelayvalue = get_config('local_moofactory_notification', $nameleveedelay);
217
 
218
        $leveetimeconfig = get_config('local_moofactory_notification', 'modulesleveetime');
219
 
220
        if ($leveedelayvalue===false) { //will be false for the first time so we get the default value from config at this time
221
            $mform->setDefault($nameleveedelay, $leveetimeconfig);
222
        } else {
223
            $mform->setDefault($nameleveedelay, $leveedelayvalue);
224
        }
225
 
226
        $configvarslevee = ['daysbeforelevee1', 'hoursbeforelevee1', 'daysbeforelevee2', 'hoursbeforelevee2', 'daysbeforelevee3', 'hoursbeforelevee3'];
227
 
228
        $leveeGroups = [];
229
        foreach ($configvarslevee as $index => $configvar) {
230
            $name = 'module' . $configvar . '_' . $courseid . '_' . $id;
231
 
232
            //attributs du champ.
233
            $attributes = ['maxlength' => 3, 'size' => 3];
234
            if (empty($moduleleveevalue)) {
235
                $attributes['disabled'] = 'disabled';
236
            }
237
 
238
            $element = $mform->createElement('text', $name, '', $attributes);
239
            $mform->setType($name, PARAM_TEXT);
240
 
241
            //valeur par défaut.
242
            $value = get_config('local_moofactory_notification', $name);
243
            if ($value === false) {
244
                $value = local_moofactory_notification_getCustomfield($courseid, $configvar, 'text');
245
            }
246
 
247
            // Créer un élément statique pour la description.
248
            $description = $index % 2 === 0 ? 'jour(s)' : 'heures(s)';
249
            $descriptionElement = $mform->createElement('static', $name . '_desc', '', $description);
250
 
251
            // Grouper les champs en paires jours/heuress.
252
            if ($index % 2 === 0) {
253
                $leveeGroups[] = [$element, $descriptionElement];
254
            } else {
255
                $leveeGroups[count($leveeGroups) - 1][] = $element;
256
                $leveeGroups[count($leveeGroups) - 1][] = $descriptionElement;
257
            }
258
 
259
            // Enregistrer la valeur par défaut.
260
            $mform->setDefault($name, $value);
261
        }
262
 
263
        // Ajouter les groupes et appliquer les règles.
264
        $numrappel = 1;
265
        foreach ($leveeGroups as $group) {
266
            $groupName = 'levee_group_' . $courseid;
267
            $mform->addGroup($group, $groupName, get_string('daysbeforelevee' . $numrappel++, 'local_moofactory_notification'), [' '], false);
268
        }
269
 
270
        $this->add_action_buttons();
271
 
272
        $js = "$('#id_$moduleeventsname').change(function(){";
273
        $js .= "    if($('#id_$moduleeventsname').is(':checked')){";
274
        $js .= "        $('#id_$modulecheckavailabilityname').removeAttr('disabled');";
275
        $js .= "        if($('#id_$modulecheckavailabilityname').is(':checked')){";
276
        $js .= "            $('#id_$modulecheckdateavailabilityname').removeAttr('disabled');";
277
        $js .= "            $('#id_$modulecheckgroupavailabilityname').removeAttr('disabled');";
278
        $js .= "        }";
279
        $js .= "        $('#id_$modulenotificationname').removeAttr('disabled');";
280
        foreach ($configvars as $configvar) {
281
            $name = 'id_module' . $configvar . '_' . $courseid . '_' . $id;
282
            $js .= "        $('#$name').removeAttr('disabled');";
283
        }
284
        $js .= "    }";
285
        $js .= "    else{";
286
        $js .= "        $('#id_$modulecheckavailabilityname').attr('disabled', 'disabled');";
287
        $js .= "        $('#id_$modulecheckdateavailabilityname').attr('disabled', 'disabled');";
288
        $js .= "        $('#id_$modulecheckgroupavailabilityname').attr('disabled', 'disabled');";
289
        $js .= "        $('#id_$modulenotificationname').attr('disabled', 'disabled');";
290
        foreach ($configvars as $configvar) {
291
            $name = 'id_module' . $configvar . '_' . $courseid . '_' . $id;
292
            $js .= "        $('#$name').attr('disabled', 'disabled');";
293
        }
294
        $js .= "    }";
295
        $js .= "});";
296
 
297
        $js .= "$('#id_$modulecheckavailabilityname').change(function(){";
298
        $js .= "    if($('#id_$modulecheckavailabilityname').is(':checked')){";
299
        $js .= "        $('#id_$modulecheckdateavailabilityname').removeAttr('disabled');";
300
        $js .= "        $('#id_$modulecheckgroupavailabilityname').removeAttr('disabled');";
301
        $js .= "    }";
302
        $js .= "    else{";
303
        $js .= "        $('#id_$modulecheckdateavailabilityname').attr('disabled', 'disabled');";
304
        $js .= "        $('#id_$modulecheckgroupavailabilityname').attr('disabled', 'disabled');";
305
        $js .= "    }";
306
        $js .= "});";
307
 
308
        $js .= "initModuleCheckAvailability($('#id_$modulecheckavailabilityname').is(':checked'), '#id_$modulecheckdateavailabilityname', '#id_$modulecheckgroupavailabilityname');";
309
 
310
        //js for levee restriction
311
        $js .= "$('#id_$moduleleveename').change(function(){";
312
        $js .= "    if($('#id_$moduleleveename').is(':checked')){";
313
        $js .= "        $('#id_$nameleveedelay').removeAttr('disabled');";
314
        $js .= "        $('#id_$moduleleveenotificationname').removeAttr('disabled');";
315
        foreach ($configvarslevee as $configvar) {
316
            $name = 'id_module' . $configvar . '_' . $courseid . '_' . $id;
317
            $js .= "        $('#$name').removeAttr('disabled');";
318
        }
319
        $js .= "    }";
320
        $js .= "    else{";
321
        $js .= "        $('#id_$nameleveedelay').attr('disabled', 'disabled');";
322
        $js .= "        $('#id_$moduleleveenotificationname').attr('disabled', 'disabled');";
323
        foreach ($configvarslevee as $configvar) {
324
            $name = 'id_module' . $configvar . '_' . $courseid . '_' . $id;
325
            $js .= "        $('#$name').attr('disabled', 'disabled');";
326
        }
327
        $js .= "    }";
328
        $js .= "});";
329
 
330
        $PAGE->requires->js('/local/moofactory_notification/util.js');
331
        $PAGE->requires->js_init_code($js, true);
332
    }
333
 
334
    public function validation($data, $files)
335
    {
336
        $errors = parent::validation($data, $files);
337
 
338
        // Récupération des paramètres nécessaires.
339
        $courseid = $this->_customdata['courseid'];
340
        $id = $this->_customdata['id'];
341
 
342
        // Validation des champs spécifiques.
343
        $configvarslevee = ['daysbeforelevee1', 'hoursbeforelevee1', 'daysbeforelevee2', 'hoursbeforelevee2', 'daysbeforelevee3', 'hoursbeforelevee3'];
344
 
345
        foreach ($configvarslevee as $configvar) {
346
            $fieldname = 'module' . $configvar . '_' . $courseid . '_' . $id;
347
 
348
            if (!empty($data[$fieldname]) && !is_numeric($data[$fieldname])) {
349
                $errors[$fieldname] = get_string('notanumber', 'local_moofactory_notification');
350
            }
351
 
352
            // Exemple : vérifier une plage spécifique pour certaines valeurs.
353
            if (!empty($data[$fieldname]) && (int)$data[$fieldname] < 0) {
354
                $errors[$fieldname] = get_string('invalidrange', 'local_moofactory_notification');
355
            }
356
        }
357
 
358
        // Validation pour d'autres champs si nécessaire.
359
        $configvars = ['daysbeforeevents1', 'hoursbeforeevents1', 'daysbeforeevents2', 'hoursbeforeevents2', 'daysbeforeevents3', 'hoursbeforeevents3'];
360
 
361
        foreach ($configvars as $configvar) {
362
            $fieldname = 'module' . $configvar . '_' . $courseid . '_' . $id;
363
 
364
            if (!empty($data[$fieldname]) && !is_numeric($data[$fieldname])) {
365
                $errors[$fieldname] = get_string('notanumber', 'local_moofactory_notification');
366
            }
367
        }
368
 
369
        return $errors;
370
    }
371
}