Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Bulk user upload forms
19
 *
20
 * @package    tool
21
 * @subpackage uploaduser
22
 * @copyright  2007 Dan Poltawski
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once $CFG->libdir.'/formslib.php';
29
require_once($CFG->dirroot . '/user/editlib.php');
30
 
31
/**
32
 * Upload a file CVS file with user information.
33
 *
34
 * @copyright  2007 Petr Skoda  {@link http://skodak.org}
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class admin_uploaduser_form1 extends moodleform {
38
    function definition() {
39
        $mform = $this->_form;
40
 
41
        $mform->addElement('header', 'settingsheader', get_string('upload'));
42
 
43
        $url = new moodle_url('example.csv');
44
        $link = html_writer::link($url, 'example.csv');
45
        $mform->addElement('static', 'examplecsv', get_string('examplecsv', 'tool_uploaduser'), $link);
46
        $mform->addHelpButton('examplecsv', 'examplecsv', 'tool_uploaduser');
47
 
48
        $mform->addElement('filepicker', 'userfile', get_string('file'));
49
        $mform->addRule('userfile', null, 'required');
50
 
51
        $choices = csv_import_reader::get_delimiter_list();
52
        $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices);
53
        if (array_key_exists('cfg', $choices)) {
54
            $mform->setDefault('delimiter_name', 'cfg');
55
        } else if (get_string('listsep', 'langconfig') == ';') {
56
            $mform->setDefault('delimiter_name', 'semicolon');
57
        } else {
58
            $mform->setDefault('delimiter_name', 'comma');
59
        }
60
 
61
        $choices = core_text::get_encodings();
62
        $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices);
63
        $mform->setDefault('encoding', 'UTF-8');
64
 
65
        $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
66
        $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices);
67
        $mform->setType('previewrows', PARAM_INT);
68
 
69
        $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser'));
70
    }
71
 
72
    /**
73
     * Returns list of elements and their default values, to be used in CLI
74
     *
75
     * @return array
76
     */
77
    public function get_form_for_cli() {
78
        $elements = array_filter($this->_form->_elements, function($element) {
79
            return !in_array($element->getName(), ['buttonar', 'userfile', 'previewrows']);
80
        });
81
        return [$elements, $this->_form->_defaultValues];
82
    }
83
}
84
 
85
 
86
/**
87
 * Specify user upload details
88
 *
89
 * @copyright  2007 Petr Skoda  {@link http://skodak.org}
90
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
91
 */
92
class admin_uploaduser_form2 extends moodleform {
93
    function definition() {
94
        global $CFG, $USER;
95
 
96
        $mform   = $this->_form;
97
        $columns = $this->_customdata['columns'];
98
        $data    = $this->_customdata['data'];
99
 
100
        // upload settings and file
101
        $mform->addElement('header', 'settingsheader', get_string('settings'));
102
 
103
        $choices = array(UU_USER_ADDNEW     => get_string('uuoptype_addnew', 'tool_uploaduser'),
104
                         UU_USER_ADDINC     => get_string('uuoptype_addinc', 'tool_uploaduser'),
105
                         UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'),
106
                         UU_USER_UPDATE     => get_string('uuoptype_update', 'tool_uploaduser'));
107
        $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices);
108
 
109
        $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
110
        $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
111
        $mform->setDefault('uupasswordnew', 1);
112
        $mform->hideIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
113
 
114
        $choices = array(UU_UPDATE_NOCHANGES    => get_string('nochanges', 'tool_uploaduser'),
115
                         UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'),
116
                         UU_UPDATE_ALLOVERRIDE  => get_string('uuupdateall', 'tool_uploaduser'),
117
                         UU_UPDATE_MISSING      => get_string('uuupdatemissing', 'tool_uploaduser'));
118
        $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices);
119
        $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
120
        $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
121
        $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
122
 
123
        $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
124
        $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
125
        $mform->setDefault('uupasswordold', 0);
126
        $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
127
        $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
128
        $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 0);
129
        $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 3);
130
 
131
        $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
132
                         UU_PWRESET_NONE => get_string('none'),
133
                         UU_PWRESET_ALL  => get_string('all'));
134
        if (empty($CFG->passwordpolicy)) {
135
            unset($choices[UU_PWRESET_WEAK]);
136
        }
137
        $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
138
 
139
        $mform->addElement('selectyesno', 'uumatchemail', get_string('matchemail', 'tool_uploaduser'));
140
        $mform->setDefault('uumatchemail', 0);
141
        $mform->hideIf('uumatchemail', 'uutype', 'eq', UU_USER_ADDNEW);
142
        $mform->hideIf('uumatchemail', 'uutype', 'eq', UU_USER_ADDINC);
143
 
144
        $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
145
        $mform->setDefault('uuallowrenames', 0);
146
        $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
147
        $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
148
 
149
        $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
150
        $mform->setDefault('uuallowdeletes', 0);
151
        // Ensure user is able to perform user deletion.
152
        if (!has_capability('moodle/user:delete', context_system::instance())) {
153
            $mform->hardFreeze('uuallowdeletes');
154
            $mform->setConstant('uuallowdeletes', 0);
155
        }
156
        $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
157
        $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
158
 
159
        $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
160
        $mform->setDefault('uuallowsuspends', 1);
161
        $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
162
        $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
163
 
164
        if (!empty($CFG->allowaccountssameemail)) {
165
            $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
166
            $mform->setDefault('uunoemailduplicates', 1);
167
        } else {
168
            $mform->addElement('hidden', 'uunoemailduplicates', 1);
169
        }
170
        $mform->setType('uunoemailduplicates', PARAM_BOOL);
171
 
172
        $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
173
        $mform->setDefault('uustandardusernames', 1);
174
 
175
        $choices = array(UU_BULK_NONE    => get_string('no'),
176
                         UU_BULK_NEW     => get_string('uubulknew', 'tool_uploaduser'),
177
                         UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
178
                         UU_BULK_ALL     => get_string('uubulkall', 'tool_uploaduser'));
179
        $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
180
        $mform->setDefault('uubulk', 0);
181
 
182
        // roles selection
183
        $showroles = false;
184
        foreach ($columns as $column) {
185
            if (preg_match('/^type\d+$/', $column)) {
186
                $showroles = true;
187
                break;
188
            }
189
        }
190
        if ($showroles) {
191
            $mform->addElement('header', 'rolesheader', get_string('roles'));
192
 
193
            $choices = uu_allowed_roles(true);
194
 
195
            $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
196
            if ($studentroles = get_archetype_roles('student')) {
197
                foreach ($studentroles as $role) {
198
                    if (isset($choices[$role->id])) {
199
                        $mform->setDefault('uulegacy1', $role->id);
200
                        break;
201
                    }
202
                }
203
                unset($studentroles);
204
            }
205
 
206
            $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
207
            if ($editteacherroles = get_archetype_roles('editingteacher')) {
208
                foreach ($editteacherroles as $role) {
209
                    if (isset($choices[$role->id])) {
210
                        $mform->setDefault('uulegacy2', $role->id);
211
                        break;
212
                    }
213
                }
214
                unset($editteacherroles);
215
            }
216
 
217
            $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
218
            if ($teacherroles = get_archetype_roles('teacher')) {
219
                foreach ($teacherroles as $role) {
220
                    if (isset($choices[$role->id])) {
221
                        $mform->setDefault('uulegacy3', $role->id);
222
                        break;
223
                    }
224
                }
225
                unset($teacherroles);
226
            }
227
        }
228
 
229
        // default values
230
        $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
231
 
232
        $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
233
        $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
234
        $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
235
        $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
236
        $mform->setForceLtr('username');
237
 
238
        $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
239
        $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
240
        $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
241
        $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
242
        $mform->setForceLtr('email');
243
 
244
        // only enabled and known to work plugins
245
        $choices = uu_supported_auths();
246
        $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
247
        $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
248
        $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
249
        $mform->setAdvanced('auth');
250
 
251
        $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
252
        $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
253
        $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
254
        $mform->addHelpButton('maildisplay', 'emaildisplay');
255
 
256
        $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
257
        $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices);
258
        $mform->setDefault('emailstop', core_user::get_property_default('emailstop'));
259
        $mform->setAdvanced('emailstop');
260
 
261
        $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
262
        $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
263
        $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
264
        $mform->setAdvanced('mailformat');
265
 
266
        $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
267
        $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
268
        $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
269
        $mform->setAdvanced('maildigest');
270
 
271
        $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
272
        $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
273
        $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
274
 
275
        $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
276
        $mform->setType('city', core_user::get_property_type('city'));
277
        $mform->setDefault('city', core_user::get_property_default('city'));
278
 
279
        $mform->addElement('select', 'country', get_string('selectacountry'), core_user::get_property_choices('country'));
280
        $mform->setDefault('country', core_user::get_property_default('country') ?: '');
281
        $mform->setAdvanced('country');
282
 
283
        $mform->addElement('select', 'timezone', get_string('timezone'), core_date::get_list_of_timezones(null, true));
284
        $mform->setDefault('timezone', core_user::get_property_default('timezone'));
285
        $mform->setAdvanced('timezone');
286
 
287
        $mform->addElement('select', 'lang', get_string('preferredlanguage'), core_user::get_property_choices('lang'));
288
        $mform->setDefault('lang', core_user::get_property_default('lang'));
289
        $mform->setAdvanced('lang');
290
 
291
        $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
292
        $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
293
        $mform->setType('description', PARAM_CLEANHTML);
294
        $mform->addHelpButton('description', 'userdescription');
295
        $mform->setAdvanced('description');
296
 
297
        $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
298
        $mform->setType('idnumber', core_user::get_property_type('idnumber'));
299
        $mform->setForceLtr('idnumber');
300
 
301
        $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
302
        $mform->setType('institution', core_user::get_property_type('institution'));
303
 
304
        $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
305
        $mform->setType('department', core_user::get_property_type('department'));
306
 
307
        $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
308
        $mform->setType('phone1', core_user::get_property_type('phone1'));
309
        $mform->setAdvanced('phone1');
310
        $mform->setForceLtr('phone1');
311
 
312
        $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
313
        $mform->setType('phone2', core_user::get_property_type('phone2'));
314
        $mform->setAdvanced('phone2');
315
        $mform->setForceLtr('phone2');
316
 
317
        $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
318
        $mform->setType('address', core_user::get_property_type('address'));
319
        $mform->setAdvanced('address');
320
 
321
        // Next the profile defaults
322
        profile_definition($mform);
323
 
324
        // hidden fields
325
        $mform->addElement('hidden', 'iid');
326
        $mform->setType('iid', PARAM_INT);
327
 
328
        $mform->addElement('hidden', 'previewrows');
329
        $mform->setType('previewrows', PARAM_INT);
330
 
331
        $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
332
 
333
        $this->set_data($data);
334
    }
335
 
336
    /**
337
     * Form tweaks that depend on current data.
338
     */
339
    function definition_after_data() {
340
        $mform   = $this->_form;
341
        $columns = $this->_customdata['columns'];
342
 
343
        foreach ($columns as $column) {
344
            if ($mform->elementExists($column)) {
345
                $mform->removeElement($column);
346
            }
347
        }
348
 
349
        if (!in_array('password', $columns)) {
350
            // password resetting makes sense only if password specified in csv file
351
            if ($mform->elementExists('uuforcepasswordchange')) {
352
                $mform->removeElement('uuforcepasswordchange');
353
            }
354
        }
355
    }
356
 
357
    /**
358
     * Server side validation.
359
     */
360
    function validation($data, $files) {
361
        $errors = parent::validation($data, $files);
362
        $columns = $this->_customdata['columns'];
363
        $optype  = $data['uutype'];
364
        $updatetype = $data['uuupdatetype'];
365
 
366
        // detect if password column needed in file
367
        if (!in_array('password', $columns)) {
368
            switch ($optype) {
369
                case UU_USER_UPDATE:
370
                    if (!empty($data['uupasswordold'])) {
371
                        $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
372
                    }
373
                    break;
374
 
375
                case UU_USER_ADD_UPDATE:
376
                    if (empty($data['uupasswordnew'])) {
377
                        $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
378
                    }
379
                    if  (!empty($data['uupasswordold'])) {
380
                        $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
381
                    }
382
                    break;
383
 
384
                case UU_USER_ADDNEW:
385
                    if (empty($data['uupasswordnew'])) {
386
                        $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
387
                    }
388
                    break;
389
                case UU_USER_ADDINC:
390
                    if (empty($data['uupasswordnew'])) {
391
                        $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
392
                    }
393
                    break;
394
             }
395
        }
396
 
397
        // If the 'Existing user details' value is set we need to ensure that the
398
        // 'Upload type' is not set to something invalid.
399
        if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
400
            $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
401
        }
402
 
403
        // look for other required data
404
        if ($optype != UU_USER_UPDATE) {
405
            $requiredusernames = useredit_get_required_name_fields();
406
            $missing = array();
407
            foreach ($requiredusernames as $requiredusername) {
408
                if (!in_array($requiredusername, $columns)) {
409
                    $missing[] = get_string('missingfield', 'error', $requiredusername);;
410
                }
411
            }
412
            if ($missing) {
413
                $errors['uutype'] = implode('<br />',  $missing);
414
            }
415
            if (!in_array('email', $columns) and empty($data['email'])) {
416
                $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
417
            }
418
        }
419
        return $errors;
420
    }
421
 
422
    /**
423
     * Used to reformat the data from the editor component
424
     *
425
     * @return stdClass
426
     */
427
    function get_data() {
428
        $data = parent::get_data();
429
 
430
        if ($data !== null and isset($data->description)) {
431
            $data->descriptionformat = $data->description['format'];
432
            $data->description = $data->description['text'];
433
        }
434
 
435
        return $data;
436
    }
437
 
438
    /**
439
     * Returns list of elements and their default values, to be used in CLI
440
     *
441
     * @return array
442
     */
443
    public function get_form_for_cli() {
444
        $elements = array_filter($this->_form->_elements, function($element) {
445
            return !in_array($element->getName(), ['buttonar', 'uubulk']);
446
        });
447
        return [$elements, $this->_form->_defaultValues];
448
    }
449
 
450
    /**
451
     * Returns validation errors (used in CLI)
452
     *
453
     * @return array
454
     */
455
    public function get_validation_errors(): array {
456
        return $this->_form->_errors;
457
    }
458
}