enrol/authorize: MDL-22777 upgrade enrol/authorize plugin to new enrol_plugin base...
[moodle.git] / lib / formslib.php
CommitLineData
ba21c9d4 1<?php
117bd748
PS
2// This file is part of Moodle - http://moodle.org/
3//
ba21c9d4 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.
117bd748 13//
ba21c9d4 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
da6f8763 17/**
18 * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms.
da1320da 19 *
20 * To use formslib then you will want to create a new file purpose_form.php eg. edit_form.php
21 * and you want to name your class something like {modulename}_{purpose}_form. Your class will
22 * extend moodleform overriding abstract classes definition and optionally defintion_after_data
23 * and validation.
24 *
25 * See examples of use of this library in course/edit.php and course/edit_form.php
26 *
27 * A few notes :
78bfb562 28 * form definition is used for both printing of form and processing and should be the same
da1320da 29 * for both or you may lose some submitted data which won't be let through.
30 * you should be using setType for every form element except select, radio or checkbox
31 * elements, these elements clean themselves.
32 *
33 *
78bfb562
PS
34 * @copyright Jamie Pratt <me@jamiep.org>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @package core
37 * @subpackage form
da6f8763 38 */
39
78bfb562
PS
40defined('MOODLE_INTERNAL') || die();
41
42/** setup.php includes our hacked pear libs first */
da6f8763 43require_once 'HTML/QuickForm.php';
44require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
45require_once 'HTML/QuickForm/Renderer/Tableless.php';
46
a83ad946 47require_once $CFG->libdir.'/filelib.php';
49292f8c 48
832e13f1 49define('EDITOR_UNLIMITED_FILES', -1);
50
a23f0aaf 51/**
52 * Callback called when PEAR throws an error
53 *
54 * @param PEAR_Error $error
55 */
56function pear_handle_error($error){
57 echo '<strong>'.$error->GetMessage().'</strong> '.$error->getUserInfo();
58 echo '<br /> <strong>Backtrace </strong>:';
59 print_object($error->backtrace);
60}
61
66491cf1 62if (!empty($CFG->debug) and $CFG->debug >= DEBUG_ALL){
a23f0aaf 63 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_handle_error');
864cc1de 64}
65
ba21c9d4 66/**
117bd748 67 *
ba21c9d4 68 * @staticvar bool $done
e5473854 69 * @global moodle_page $PAGE
ba21c9d4 70 */
8e7cebb0 71function form_init_date_js() {
cf615522 72 global $PAGE;
8e7cebb0 73 static $done = false;
74 if (!$done) {
e5473854
SH
75 $module = 'moodle-form-dateselector';
76 $function = 'M.form.dateselector.init_date_selectors';
77 $config = array(array('firstdayofweek'=>get_string('firstdayofweek', 'langconfig')));
78 $PAGE->requires->yui_module($module, $function, $config);
8e7cebb0 79 $done = true;
80 }
81}
f07b9627 82
05f5c40c 83/**
da1320da 84 * Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly
6073a598 85 * use this class you should write a class definition which extends this class or a more specific
da1320da 86 * subclass such a moodleform_mod for each form you want to display and/or process with formslib.
87 *
88 * You will write your own definition() method which performs the form set up.
ba21c9d4 89 *
90 * @package moodlecore
91 * @copyright Jamie Pratt <me@jamiep.org>
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
05f5c40c 93 */
afd63fe5 94abstract class moodleform {
ba21c9d4 95 /** @var string */
172dd12c 96 protected $_formname; // form name
3c7656b4 97 /**
98 * quickform object definition
99 *
572dd8ec 100 * @var MoodleQuickForm MoodleQuickForm
3c7656b4 101 */
172dd12c 102 protected $_form;
3c7656b4 103 /**
104 * globals workaround
105 *
106 * @var array
107 */
172dd12c 108 protected $_customdata;
4f51f48f 109 /**
110 * definition_after_data executed flag
ba21c9d4 111 * @var object definition_finalized
4f51f48f 112 */
172dd12c 113 protected $_definition_finalized = false;
ebd3c7ac 114
da1320da 115 /**
116 * The constructor function calls the abstract function definition() and it will then
117 * process and clean and attempt to validate incoming data.
118 *
119 * It will call your custom validate method to validate data and will also check any rules
120 * you have specified in definition using addRule
121 *
122 * The name of the form (id attribute of the form) is automatically generated depending on
123 * the name you gave the class extending moodleform. You should call your class something
124 * like
125 *
4f51f48f 126 * @param mixed $action the action attribute for the form. If empty defaults to auto detect the
127 * current url. If a moodle_url object then outputs params as hidden variables.
da1320da 128 * @param array $customdata if your form defintion method needs access to data such as $course
129 * $cm, etc. to construct the form definition then pass it in this array. You can
130 * use globals for somethings.
131 * @param string $method if you set this to anything other than 'post' then _GET and _POST will
132 * be merged and used as incoming data to the form.
133 * @param string $target target frame for form submission. You will rarely use this. Don't use
134 * it if you don't need to as the target attribute is deprecated in xhtml
135 * strict.
136 * @param mixed $attributes you can pass a string of html attributes here or an array.
ba21c9d4 137 * @param bool $editable
138 * @return object moodleform
da1320da 139 */
4f51f48f 140 function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
a23f0aaf 141 if (empty($action)){
142 $action = strip_querystring(qualified_me());
143 }
f07b9627 144
72f46d11 145 $this->_formname = get_class($this); // '_form' suffix kept in order to prevent collisions of form id and other element
7f40a229 146 $this->_customdata = $customdata;
66491cf1 147 $this->_form = new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes);
4f51f48f 148 if (!$editable){
149 $this->_form->hardFreeze();
150 }
7f40a229 151
152 $this->definition();
153
154 $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection
d18e0fe6 155 $this->_form->setType('sesskey', PARAM_RAW);
7f40a229 156 $this->_form->setDefault('sesskey', sesskey());
5bc97c98 157 $this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker
d18e0fe6 158 $this->_form->setType('_qf__'.$this->_formname, PARAM_RAW);
5bc97c98 159 $this->_form->setDefault('_qf__'.$this->_formname, 1);
160 $this->_form->_setDefaultRuleMessages();
7f40a229 161
162 // we have to know all input types before processing submission ;-)
163 $this->_process_submission($method);
7f40a229 164 }
05f5c40c 165
2c412890 166 /**
da1320da 167 * To autofocus on first form element or first element with error.
2c412890 168 *
8dec2253 169 * @param string $name if this is set then the focus is forced to a field with this name
170 *
2c412890 171 * @return string javascript to select form element with first error or
da1320da 172 * first element if no errors. Use this as a parameter
173 * when calling print_header
2c412890 174 */
46f3921e 175 function focus($name=NULL) {
9403060a 176 $form =& $this->_form;
46f3921e 177 $elkeys = array_keys($form->_elementIndex);
178 $error = false;
9403060a 179 if (isset($form->_errors) && 0 != count($form->_errors)){
180 $errorkeys = array_keys($form->_errors);
181 $elkeys = array_intersect($elkeys, $errorkeys);
46f3921e 182 $error = true;
2c412890 183 }
46f3921e 184
185 if ($error or empty($name)) {
186 $names = array();
187 while (empty($names) and !empty($elkeys)) {
188 $el = array_shift($elkeys);
189 $names = $form->_getElNamesRecursive($el);
190 }
191 if (!empty($names)) {
192 $name = array_shift($names);
193 }
8dec2253 194 }
46f3921e 195
196 $focus = '';
197 if (!empty($name)) {
198 $focus = 'forms[\''.$form->getAttribute('id').'\'].elements[\''.$name.'\']';
9403060a 199 }
46f3921e 200
9403060a 201 return $focus;
202 }
7f40a229 203
05f5c40c 204 /**
205 * Internal method. Alters submitted data to be suitable for quickforms processing.
206 * Must be called when the form is fully set up.
ba21c9d4 207 *
208 * @param string $method
05f5c40c 209 */
7f40a229 210 function _process_submission($method) {
211 $submission = array();
212 if ($method == 'post') {
213 if (!empty($_POST)) {
214 $submission = $_POST;
215 }
216 } else {
217 $submission = array_merge_recursive($_GET, $_POST); // emulate handling of parameters in xxxx_param()
218 }
219
220 // following trick is needed to enable proper sesskey checks when using GET forms
5bc97c98 221 // the _qf__.$this->_formname serves as a marker that form was actually submitted
222 if (array_key_exists('_qf__'.$this->_formname, $submission) and $submission['_qf__'.$this->_formname] == 1) {
7f40a229 223 if (!confirm_sesskey()) {
c3f9ee10 224 print_error('invalidsesskey');
7f40a229 225 }
05f5c40c 226 $files = $_FILES;
7f40a229 227 } else {
228 $submission = array();
05f5c40c 229 $files = array();
7f40a229 230 }
231
05f5c40c 232 $this->_form->updateSubmission($submission, $files);
7f40a229 233 }
234
05f5c40c 235 /**
f728d49b
PS
236 * Internal method. Validates all old-style deprecated uploaded files.
237 * The new way is to upload files via repository api.
117bd748 238 *
ba21c9d4 239 * @global object
240 * @global object
241 * @param array $files
242 * @return bool|array Success or an array of errors
05f5c40c 243 */
89489cfe 244 function _validate_files(&$files) {
172dd12c 245 global $CFG, $COURSE;
246
89489cfe 247 $files = array();
248
49292f8c 249 if (empty($_FILES)) {
250 // we do not need to do any checks because no files were submitted
89489cfe 251 // note: server side rules do not work for files - use custom verification in validate() instead
49292f8c 252 return true;
253 }
49292f8c 254
172dd12c 255 $errors = array();
256 $filenames = array();
49292f8c 257
258 // now check that we really want each file
259 foreach ($_FILES as $elname=>$file) {
172dd12c 260 $required = $this->_form->isElementRequired($elname);
89489cfe 261
172dd12c 262 if ($file['error'] == 4 and $file['size'] == 0) {
263 if ($required) {
264 $errors[$elname] = get_string('required');
49292f8c 265 }
172dd12c 266 unset($_FILES[$elname]);
267 continue;
268 }
269
a83ad946 270 if (!empty($file['error'])) {
271 $errors[$elname] = file_get_upload_error($file['error']);
172dd12c 272 unset($_FILES[$elname]);
273 continue;
274 }
275
276 if (!is_uploaded_file($file['tmp_name'])) {
277 // TODO: improve error message
278 $errors[$elname] = get_string('error');
279 unset($_FILES[$elname]);
280 continue;
281 }
282
283 if (!$this->_form->elementExists($elname) or !$this->_form->getElementType($elname)=='file') {
284 // hmm, this file was not requested
285 unset($_FILES[$elname]);
286 continue;
287 }
288
289/*
f728d49b 290 // TODO: rethink the file scanning MDL-19380
172dd12c 291 if ($CFG->runclamonupload) {
292 if (!clam_scan_moodle_file($_FILES[$elname], $COURSE)) {
293 $errors[$elname] = $_FILES[$elname]['uploadlog'];
294 unset($_FILES[$elname]);
295 continue;
296 }
297 }
298*/
299 $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
300 if ($filename === '') {
301 // TODO: improve error message - wrong chars
302 $errors[$elname] = get_string('error');
303 unset($_FILES[$elname]);
304 continue;
49292f8c 305 }
172dd12c 306 if (in_array($filename, $filenames)) {
307 // TODO: improve error message - duplicate name
308 $errors[$elname] = get_string('error');
309 unset($_FILES[$elname]);
310 continue;
311 }
312 $filenames[] = $filename;
313 $_FILES[$elname]['name'] = $filename;
314
315 $files[$elname] = $_FILES[$elname]['tmp_name'];
49292f8c 316 }
317
318 // return errors if found
172dd12c 319 if (count($errors) == 0){
49292f8c 320 return true;
89489cfe 321
49292f8c 322 } else {
89489cfe 323 $files = array();
49292f8c 324 return $errors;
325 }
326 }
327
05f5c40c 328 /**
da1320da 329 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
330 * form definition (new entry form); this function is used to load in data where values
331 * already exist and data is being edited (edit entry form).
05f5c40c 332 *
294ce987 333 * note: $slashed param removed
334 *
05f5c40c 335 * @param mixed $default_values object or array of default values
05f5c40c 336 */
294ce987 337 function set_data($default_values) {
7f40a229 338 if (is_object($default_values)) {
339 $default_values = (array)$default_values;
340 }
294ce987 341 $this->_form->setDefaults($default_values);
7f40a229 342 }
343
ba21c9d4 344 /**
f728d49b 345 * @deprecated
ba21c9d4 346 */
feaf5d06 347 function set_upload_manager($um=false) {
f728d49b 348 debugging('Old file uploads can not be used any more, please use new filepicker element');
c80a13c7 349 }
350
05f5c40c 351 /**
352 * Check that form was submitted. Does not check validity of submitted data.
353 *
354 * @return bool true if form properly submitted
355 */
7f40a229 356 function is_submitted() {
357 return $this->_form->isSubmitted();
358 }
359
ba21c9d4 360 /**
361 * @staticvar bool $nosubmit
362 */
a23f0aaf 363 function no_submit_button_pressed(){
364 static $nosubmit = null; // one check is enough
365 if (!is_null($nosubmit)){
366 return $nosubmit;
367 }
368 $mform =& $this->_form;
369 $nosubmit = false;
f07b9627 370 if (!$this->is_submitted()){
371 return false;
372 }
a23f0aaf 373 foreach ($mform->_noSubmitButtons as $nosubmitbutton){
374 if (optional_param($nosubmitbutton, 0, PARAM_RAW)){
375 $nosubmit = true;
376 break;
377 }
378 }
379 return $nosubmit;
380 }
381
382
05f5c40c 383 /**
384 * Check that form data is valid.
6bba6dbb 385 * You should almost always use this, rather than {@see validate_defined_fields}
05f5c40c 386 *
ba21c9d4 387 * @staticvar bool $validated
05f5c40c 388 * @return bool true if form data valid
389 */
7f40a229 390 function is_validated() {
4f51f48f 391 //finalize the form definition before any processing
392 if (!$this->_definition_finalized) {
393 $this->_definition_finalized = true;
394 $this->definition_after_data();
395 }
396
6bba6dbb 397 return $this->validate_defined_fields();
398 }
399
400 /**
401 * Validate the form.
402 *
403 * You almost always want to call {@see is_validated} instead of this
404 * because it calls {@see definition_after_data} first, before validating the form,
405 * which is what you want in 99% of cases.
406 *
407 * This is provided as a separate function for those special cases where
408 * you want the form validated before definition_after_data is called
409 * for example, to selectively add new elements depending on a no_submit_button press,
410 * but only when the form is valid when the no_submit_button is pressed,
411 *
412 * @param boolean $validateonnosubmit optional, defaults to false. The default behaviour
413 * is NOT to validate the form when a no submit button has been pressed.
414 * pass true here to override this behaviour
415 *
416 * @return bool true if form data valid
417 */
418 function validate_defined_fields($validateonnosubmit=false) {
419 static $validated = null; // one validation is enough
420 $mform =& $this->_form;
421 if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
9aa022fe 422 return false;
423 } elseif ($validated === null) {
3ba2c187 424 $internal_val = $mform->validate();
89489cfe 425
426 $files = array();
427 $file_val = $this->_validate_files($files);
428 if ($file_val !== true) {
429 if (!empty($file_val)) {
430 foreach ($file_val as $element=>$msg) {
431 $mform->setElementError($element, $msg);
432 }
433 }
434 $file_val = false;
435 }
436
4287fc0d 437 $data = $mform->exportValues();
89489cfe 438 $moodle_val = $this->validation($data, $files);
a78890d5 439 if ((is_array($moodle_val) && count($moodle_val)!==0)) {
440 // non-empty array means errors
441 foreach ($moodle_val as $element=>$msg) {
442 $mform->setElementError($element, $msg);
7f40a229 443 }
a78890d5 444 $moodle_val = false;
445
446 } else {
447 // anything else means validation ok
448 $moodle_val = true;
7f40a229 449 }
89489cfe 450
49292f8c 451 $validated = ($internal_val and $moodle_val and $file_val);
7f40a229 452 }
9aa022fe 453 return $validated;
7f40a229 454 }
455
19110c57 456 /**
457 * Return true if a cancel button has been pressed resulting in the form being submitted.
458 *
459 * @return boolean true if a cancel button has been pressed
460 */
461 function is_cancelled(){
462 $mform =& $this->_form;
a23f0aaf 463 if ($mform->isSubmitted()){
464 foreach ($mform->_cancelButtons as $cancelbutton){
465 if (optional_param($cancelbutton, 0, PARAM_RAW)){
466 return true;
467 }
19110c57 468 }
469 }
470 return false;
471 }
472
05f5c40c 473 /**
da1320da 474 * Return submitted data if properly submitted or returns NULL if validation fails or
475 * if there is no submitted data.
172dd12c 476 *
294ce987 477 * note: $slashed param removed
05f5c40c 478 *
05f5c40c 479 * @return object submitted data; NULL if not valid or not submitted
480 */
294ce987 481 function get_data() {
19110c57 482 $mform =& $this->_form;
3ba2c187 483
7f40a229 484 if ($this->is_submitted() and $this->is_validated()) {
294ce987 485 $data = $mform->exportValues();
5bc97c98 486 unset($data['sesskey']); // we do not need to return sesskey
487 unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too
7f40a229 488 if (empty($data)) {
489 return NULL;
490 } else {
491 return (object)$data;
492 }
493 } else {
494 return NULL;
495 }
496 }
497
4f51f48f 498 /**
499 * Return submitted data without validation or NULL if there is no submitted data.
294ce987 500 * note: $slashed param removed
4f51f48f 501 *
4f51f48f 502 * @return object submitted data; NULL if not submitted
503 */
294ce987 504 function get_submitted_data() {
4f51f48f 505 $mform =& $this->_form;
506
507 if ($this->is_submitted()) {
294ce987 508 $data = $mform->exportValues();
4f51f48f 509 unset($data['sesskey']); // we do not need to return sesskey
510 unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too
511 if (empty($data)) {
512 return NULL;
513 } else {
514 return (object)$data;
515 }
516 } else {
517 return NULL;
518 }
519 }
520
05f5c40c 521 /**
522 * Save verified uploaded files into directory. Upload process can be customised from definition()
172dd12c 523 * NOTE: please use save_stored_file() or save_file()
ba21c9d4 524 *
525 * @return bool Always false
05f5c40c 526 */
49292f8c 527 function save_files($destination) {
172dd12c 528 debugging('Not used anymore, please fix code! Use save_stored_file() or save_file() instead');
49292f8c 529 return false;
530 }
2b63df96 531
feaf5d06 532 /**
172dd12c 533 * Returns name of uploaded file.
ba21c9d4 534 *
535 * @global object
172dd12c 536 * @param string $elname, first element if null
feaf5d06 537 * @return mixed false in case of failure, string if ok
538 */
172dd12c 539 function get_new_filename($elname=null) {
4287fc0d 540 global $USER;
541
172dd12c 542 if (!$this->is_submitted() or !$this->is_validated()) {
543 return false;
544 }
545
546 if (is_null($elname)) {
547 if (empty($_FILES)) {
548 return false;
549 }
550 reset($_FILES);
551 $elname = key($_FILES);
552 }
4287fc0d 553
554 if (empty($elname)) {
555 return false;
556 }
557
558 $element = $this->_form->getElement($elname);
559
0022e2d7 560 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
4287fc0d 561 $values = $this->_form->exportValues($elname);
562 if (empty($values[$elname])) {
563 return false;
564 }
565 $draftid = $values[$elname];
566 $fs = get_file_storage();
567 $context = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 568 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
4287fc0d 569 return false;
570 }
571 $file = reset($files);
572 return $file->get_filename();
573 }
574
172dd12c 575 if (!isset($_FILES[$elname])) {
576 return false;
577 }
578
579 return $_FILES[$elname]['name'];
feaf5d06 580 }
581
b6b1d1ca 582 /**
172dd12c 583 * Save file to standard filesystem
ba21c9d4 584 *
585 * @global object
172dd12c 586 * @param string $elname name of element
587 * @param string $pathname full path name of file
588 * @param bool $override override file if exists
589 * @return bool success
b6b1d1ca 590 */
172dd12c 591 function save_file($elname, $pathname, $override=false) {
4287fc0d 592 global $USER;
b6b1d1ca 593
4287fc0d 594 if (!$this->is_submitted() or !$this->is_validated()) {
b6b1d1ca 595 return false;
596 }
172dd12c 597 if (file_exists($pathname)) {
598 if ($override) {
599 if (!@unlink($pathname)) {
600 return false;
601 }
602 } else {
603 return false;
604 }
605 }
4287fc0d 606
607 $element = $this->_form->getElement($elname);
608
0022e2d7 609 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
4287fc0d 610 $values = $this->_form->exportValues($elname);
611 if (empty($values[$elname])) {
612 return false;
613 }
614 $draftid = $values[$elname];
615 $fs = get_file_storage();
616 $context = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 617 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
4287fc0d 618 return false;
619 }
620 $file = reset($files);
621
622 return $file->copy_content_to($pathname);
623
624 } else if (isset($_FILES[$elname])) {
625 return copy($_FILES[$elname]['tmp_name'], $pathname);
172dd12c 626 }
627
4287fc0d 628 return false;
172dd12c 629 }
630
edfd6a5e
PS
631 /**
632 * Returns a temporary file, do not forget to delete after not needed any more.
633 *
634 * @param string $elname
635 * @return string or false
636 */
637 function save_temp_file($elname) {
638 if (!$this->get_new_filename($elname)) {
639 return false;
640 }
641 if (!$dir = make_upload_directory('temp/forms')) {
642 return false;
643 }
644 if (!$tempfile = tempnam($dir, 'tempup_')) {
645 return false;
646 }
647 if (!$this->save_file($elname, $tempfile, true)) {
648 // something went wrong
649 @unlink($tempfile);
650 return false;
651 }
652
653 return $tempfile;
654 }
655
0022e2d7 656 /**
d3d98a3a
DC
657 * Get draft files of a form element
658 * This is a protected method which will be used only inside moodleforms
659 *
660 * @global object $USER
661 * @param string $elname name of element
662 * @return array
663 */
664 protected function get_draft_files($elname) {
665 global $USER;
666
667 if (!$this->is_submitted()) {
668 return false;
669 }
670
671 $element = $this->_form->getElement($elname);
672
673 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
674 $values = $this->_form->exportValues($elname);
675 if (empty($values[$elname])) {
676 return false;
677 }
678 $draftid = $values[$elname];
679 $fs = get_file_storage();
680 $context = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 681 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
d3d98a3a
DC
682 return null;
683 }
684 return $files;
685 }
686 return null;
687 }
688
172dd12c 689 /**
690 * Save file to local filesystem pool
ba21c9d4 691 *
692 * @global object
172dd12c 693 * @param string $elname name of element
924ddb15 694 * @param int $newcontextid
695 * @param string $newfilearea
696 * @param string $newfilepath
697 * @param string $newfilename - use specified filename, if not specified name of uploaded file used
49583e9e 698 * @param bool $overwrite - overwrite file if exists
924ddb15 699 * @param int $newuserid - new userid if required
172dd12c 700 * @return mixed stored_file object or false if error; may throw exception if duplicate found
701 */
64f93798 702 function save_stored_file($elname, $newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath='/',
49583e9e 703 $newfilename=null, $overwrite=false, $newuserid=null) {
924ddb15 704 global $USER;
705
172dd12c 706 if (!$this->is_submitted() or !$this->is_validated()) {
89489cfe 707 return false;
172dd12c 708 }
89489cfe 709
924ddb15 710 if (empty($newuserid)) {
711 $newuserid = $USER->id;
89489cfe 712 }
b6b1d1ca 713
4287fc0d 714 $element = $this->_form->getElement($elname);
715 $fs = get_file_storage();
172dd12c 716
4287fc0d 717 if ($element instanceof MoodleQuickForm_filepicker) {
718 $values = $this->_form->exportValues($elname);
719 if (empty($values[$elname])) {
720 return false;
721 }
722 $draftid = $values[$elname];
723 $context = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 724 if (!$files = $fs->get_area_files($context->id, 'user' ,'draft', $draftid, 'id DESC', false)) {
4287fc0d 725 return false;
726 }
727 $file = reset($files);
728 if (is_null($newfilename)) {
729 $newfilename = $file->get_filename();
730 }
172dd12c 731
4287fc0d 732 if ($overwrite) {
64f93798 733 if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
4287fc0d 734 if (!$oldfile->delete()) {
735 return false;
736 }
924ddb15 737 }
738 }
739
64f93798 740 $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
4287fc0d 741 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
742 return $fs->create_file_from_storedfile($file_record, $file);
924ddb15 743
4287fc0d 744 } else if (isset($_FILES[$elname])) {
745 $filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;
172dd12c 746
4287fc0d 747 if ($overwrite) {
64f93798 748 if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
4287fc0d 749 if (!$oldfile->delete()) {
750 return false;
751 }
752 }
924ddb15 753 }
4287fc0d 754
64f93798 755 $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
4287fc0d 756 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
757 return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
924ddb15 758 }
759
760 return false;
172dd12c 761 }
762
763 /**
764 * Get content of uploaded file.
ba21c9d4 765 *
766 * @global object
172dd12c 767 * @param $element name of file upload element
768 * @return mixed false in case of failure, string if ok
769 */
770 function get_file_content($elname) {
4287fc0d 771 global $USER;
772
172dd12c 773 if (!$this->is_submitted() or !$this->is_validated()) {
774 return false;
775 }
776
4287fc0d 777 $element = $this->_form->getElement($elname);
778
0022e2d7 779 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
4287fc0d 780 $values = $this->_form->exportValues($elname);
781 if (empty($values[$elname])) {
782 return false;
783 }
784 $draftid = $values[$elname];
785 $fs = get_file_storage();
786 $context = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 787 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
4287fc0d 788 return false;
789 }
790 $file = reset($files);
791
792 return $file->get_content();
793
794 } else if (isset($_FILES[$elname])) {
795 return file_get_contents($_FILES[$elname]['tmp_name']);
b6b1d1ca 796 }
172dd12c 797
4287fc0d 798 return false;
b6b1d1ca 799 }
800
05f5c40c 801 /**
802 * Print html form.
803 */
7f40a229 804 function display() {
4f51f48f 805 //finalize the form definition if not yet done
806 if (!$this->_definition_finalized) {
807 $this->_definition_finalized = true;
808 $this->definition_after_data();
809 }
7f40a229 810 $this->_form->display();
811 }
812
49292f8c 813 /**
05f5c40c 814 * Abstract method - always override!
49292f8c 815 */
afd63fe5 816 protected abstract function definition();
2c412890 817
c08ac016 818 /**
05f5c40c 819 * Dummy stub method - override if you need to setup the form depending on current
beac4717 820 * values. This method is called after definition(), data submission and set_data().
05f5c40c 821 * All form setup that is dependent on form values should go in here.
c08ac016 822 */
823 function definition_after_data(){
c08ac016 824 }
7f40a229 825
05f5c40c 826 /**
827 * Dummy stub method - override if you needed to perform some extra validation.
828 * If there are errors return array of errors ("fieldname"=>"error message"),
829 * otherwise true if ok.
38f394b2 830 *
89489cfe 831 * Server side rules do not work for uploaded files, implement serverside rules here if needed.
832 *
05f5c40c 833 * @param array $data array of ("fieldname"=>value) of submitted data
89489cfe 834 * @param array $files array of uploaded files "element_name"=>tmp_file_path
a78890d5 835 * @return array of "element_name"=>"error_description" if there are errors,
836 * or an empty array if everything is OK (true allowed for backwards compatibility too).
05f5c40c 837 */
89489cfe 838 function validation($data, $files) {
13ccb7bd 839 return array();
7f40a229 840 }
ebd3c7ac 841
616b549a 842 /**
843 * Method to add a repeating group of elements to a form.
844 *
845 * @param array $elementobjs Array of elements or groups of elements that are to be repeated
846 * @param integer $repeats no of times to repeat elements initially
847 * @param array $options Array of options to apply to elements. Array keys are element names.
848 * This is an array of arrays. The second sets of keys are the option types
849 * for the elements :
850 * 'default' - default value is value
851 * 'type' - PARAM_* constant is value
852 * 'helpbutton' - helpbutton params array is value
853 * 'disabledif' - last three moodleform::disabledIf()
854 * params are value as an array
855 * @param string $repeathiddenname name for hidden element storing no of repeats in this form
856 * @param string $addfieldsname name for button to add more fields
857 * @param int $addfieldsno how many fields to add at a time
271ffe3f 858 * @param string $addstring name of button, {no} is replaced by no of blanks that will be added.
6f3b54c8 859 * @param boolean $addbuttoninside if true, don't call closeHeaderBefore($addfieldsname). Default false.
a23f0aaf 860 * @return int no of repeats of element in this page
616b549a 861 */
6f3b54c8 862 function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
863 $addfieldsname, $addfieldsno=5, $addstring=null, $addbuttoninside=false){
271ffe3f 864 if ($addstring===null){
865 $addstring = get_string('addfields', 'form', $addfieldsno);
866 } else {
867 $addstring = str_ireplace('{no}', $addfieldsno, $addstring);
868 }
ebd3c7ac 869 $repeats = optional_param($repeathiddenname, $repeats, PARAM_INT);
870 $addfields = optional_param($addfieldsname, '', PARAM_TEXT);
871 if (!empty($addfields)){
872 $repeats += $addfieldsno;
873 }
ebd3c7ac 874 $mform =& $this->_form;
a23f0aaf 875 $mform->registerNoSubmitButton($addfieldsname);
ebd3c7ac 876 $mform->addElement('hidden', $repeathiddenname, $repeats);
d18e0fe6 877 $mform->setType($repeathiddenname, PARAM_INT);
ebd3c7ac 878 //value not to be overridden by submitted value
879 $mform->setConstants(array($repeathiddenname=>$repeats));
414f7bee 880 $namecloned = array();
881 for ($i = 0; $i < $repeats; $i++) {
ebd3c7ac 882 foreach ($elementobjs as $elementobj){
985f0ddd 883 $elementclone = fullclone($elementobj);
7b41a4a9 884 $name = $elementclone->getName();
414f7bee 885 $namecloned[] = $name;
886 if (!empty($name)) {
86aab05c 887 $elementclone->setName($name."[$i]");
888 }
414f7bee 889 if (is_a($elementclone, 'HTML_QuickForm_header')) {
890 $value = $elementclone->_text;
271ffe3f 891 $elementclone->setValue(str_replace('{no}', ($i+1), $value));
892
893 } else {
894 $value=$elementclone->getLabel();
895 $elementclone->setLabel(str_replace('{no}', ($i+1), $value));
ebd3c7ac 896
897 }
7b41a4a9 898
ebd3c7ac 899 $mform->addElement($elementclone);
900 }
901 }
902 for ($i=0; $i<$repeats; $i++) {
903 foreach ($options as $elementname => $elementoptions){
904 $pos=strpos($elementname, '[');
905 if ($pos!==FALSE){
906 $realelementname = substr($elementname, 0, $pos+1)."[$i]";
907 $realelementname .= substr($elementname, $pos+1);
908 }else {
909 $realelementname = $elementname."[$i]";
910 }
911 foreach ($elementoptions as $option => $params){
912
913 switch ($option){
914 case 'default' :
915 $mform->setDefault($realelementname, $params);
916 break;
ebd3c7ac 917 case 'helpbutton' :
cd188086
DM
918 $params = array_merge(array($realelementname), $params);
919 call_user_func_array(array(&$mform, 'addHelpButton'), $params);
ebd3c7ac 920 break;
921 case 'disabledif' :
414f7bee 922 foreach ($namecloned as $num => $name){
923 if ($params[0] == $name){
924 $params[0] = $params[0]."[$i]";
925 break;
926 }
927 }
9aa022fe 928 $params = array_merge(array($realelementname), $params);
929 call_user_func_array(array(&$mform, 'disabledIf'), $params);
930 break;
931 case 'rule' :
932 if (is_string($params)){
933 $params = array(null, $params, null, 'client');
934 }
935 $params = array_merge(array($realelementname), $params);
936 call_user_func_array(array(&$mform, 'addRule'), $params);
ebd3c7ac 937 break;
938
939 }
940 }
941 }
942 }
271ffe3f 943 $mform->addElement('submit', $addfieldsname, $addstring);
a23f0aaf 944
6f3b54c8 945 if (!$addbuttoninside) {
946 $mform->closeHeaderBefore($addfieldsname);
947 }
ebd3c7ac 948
19194f82 949 return $repeats;
ebd3c7ac 950 }
6073a598 951
952 /**
953 * Adds a link/button that controls the checked state of a group of checkboxes.
ba21c9d4 954 *
955 * @global object
6073a598 956 * @param int $groupid The id of the group of advcheckboxes this element controls
ba21c9d4 957 * @param string $buttontext The text of the link. Defaults to "select all/none"
6073a598 958 * @param array $attributes associative array of HTML attributes
959 * @param int $originalValue The original general state of the checkboxes before the user first clicks this element
960 */
172dd12c 961 function add_checkbox_controller($groupid, $buttontext, $attributes, $originalValue = 0) {
6073a598 962 global $CFG;
963 if (empty($text)) {
964 $text = get_string('selectallornone', 'form');
965 }
966
967 $mform = $this->_form;
968 $select_value = optional_param('checkbox_controller'. $groupid, null, PARAM_INT);
969
970 if ($select_value == 0 || is_null($select_value)) {
971 $new_select_value = 1;
972 } else {
973 $new_select_value = 0;
974 }
975
976 $mform->addElement('hidden', "checkbox_controller$groupid");
d18e0fe6 977 $mform->setType("checkbox_controller$groupid", PARAM_INT);
6073a598 978 $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
172dd12c 979
6073a598 980 // Locate all checkboxes for this group and set their value, IF the optional param was given
981 if (!is_null($select_value)) {
982 foreach ($this->_form->_elements as $element) {
983 if ($element->getAttribute('class') == "checkboxgroup$groupid") {
984 $mform->setConstants(array($element->getAttribute('name') => $select_value));
985 }
986 }
987 }
988
989 $checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid;
990 $mform->registerNoSubmitButton($checkbox_controller_name);
172dd12c 991
6073a598 992 // Prepare Javascript for submit element
993 $js = "\n//<![CDATA[\n";
994 if (!defined('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS')) {
995 $js .= <<<EOS
996function html_quickform_toggle_checkboxes(group) {
997 var checkboxes = getElementsByClassName(document, 'input', 'checkboxgroup' + group);
998 var newvalue = false;
999 var global = eval('html_quickform_checkboxgroup' + group + ';');
1000 if (global == 1) {
172dd12c 1001 eval('html_quickform_checkboxgroup' + group + ' = 0;');
6073a598 1002 newvalue = '';
1003 } else {
172dd12c 1004 eval('html_quickform_checkboxgroup' + group + ' = 1;');
6073a598 1005 newvalue = 'checked';
1006 }
1007
1008 for (i = 0; i < checkboxes.length; i++) {
172dd12c 1009 checkboxes[i].checked = newvalue;
6073a598 1010 }
1011}
1012EOS;
1013 define('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS', true);
1014 }
1015 $js .= "\nvar html_quickform_checkboxgroup$groupid=$originalValue;\n";
172dd12c 1016
6073a598 1017 $js .= "//]]>\n";
172dd12c 1018
6073a598 1019 require_once("$CFG->libdir/form/submitlink.php");
1020 $submitlink = new MoodleQuickForm_submitlink($checkbox_controller_name, $attributes);
1021 $submitlink->_js = $js;
1022 $submitlink->_onclick = "html_quickform_toggle_checkboxes($groupid); return false;";
172dd12c 1023 $mform->addElement($submitlink);
6073a598 1024 $mform->setDefault($checkbox_controller_name, $text);
1025 }
1026
a23f0aaf 1027 /**
1d284fbd 1028 * Use this method to a cancel and submit button to the end of your form. Pass a param of false
a23f0aaf 1029 * if you don't want a cancel button in your form. If you have a cancel button make sure you
1030 * check for it being pressed using is_cancelled() and redirecting if it is true before trying to
beac4717 1031 * get data with get_data().
a23f0aaf 1032 *
1033 * @param boolean $cancel whether to show cancel button, default true
a23f0aaf 1034 * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
1035 */
1d284fbd 1036 function add_action_buttons($cancel = true, $submitlabel=null){
a23f0aaf 1037 if (is_null($submitlabel)){
1038 $submitlabel = get_string('savechanges');
1039 }
1040 $mform =& $this->_form;
1d284fbd 1041 if ($cancel){
1042 //when two elements we need a group
a23f0aaf 1043 $buttonarray=array();
1044 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
1d284fbd 1045 $buttonarray[] = &$mform->createElement('cancel');
a23f0aaf 1046 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
4f51f48f 1047 $mform->closeHeaderBefore('buttonar');
a23f0aaf 1048 } else {
1049 //no group needed
1050 $mform->addElement('submit', 'submitbutton', $submitlabel);
4f51f48f 1051 $mform->closeHeaderBefore('submitbutton');
a23f0aaf 1052 }
1053 }
572dd8ec
SH
1054
1055 /**
2836e77d 1056 * Adds an initialisation call for a standard JavaScript enhancement.
572dd8ec
SH
1057 *
1058 * This function is designed to add an initialisation call for a JavaScript
2836e77d 1059 * enhancement that should exist within javascript-static M.form.init_{enhancementname}.
572dd8ec
SH
1060 *
1061 * Current options:
1062 * - Selectboxes
1063 * - smartselect: Turns a nbsp indented select box into a custom drop down
1064 * control that supports multilevel and category selection.
1065 * $enhancement = 'smartselect';
1066 * $options = array('selectablecategories' => true|false)
1067 *
1068 * @since 2.0
1069 * @param string|element $element
1070 * @param string $enhancement
1071 * @param array $options
1072 * @param array $strings
1073 */
1074 function init_javascript_enhancement($element, $enhancement, array $options=array(), array $strings=null) {
1075 global $PAGE;
1076 if (is_string($element)) {
1077 $element = $this->_form->getElement($element);
1078}
1079 if (is_object($element)) {
1080 $element->_generateId();
1081 $elementid = $element->getAttribute('id');
1082 $PAGE->requires->js_init_call('M.form.init_'.$enhancement, array($elementid, $options));
1083 if (is_array($strings)) {
1084 foreach ($strings as $string) {
1085 if (is_array($string)) {
1086 call_user_method_array('string_for_js', $PAGE->requires, $string);
1087 } else {
1088 $PAGE->requires->string_for_js($string, 'moodle');
1089 }
1090 }
1091 }
1092 }
1093 }
7f40a229 1094}
1095
da1320da 1096/**
1097 * You never extend this class directly. The class methods of this class are available from
6073a598 1098 * the private $this->_form property on moodleform and its children. You generally only
da1320da 1099 * call methods on this class from within abstract methods that you override on moodleform such
1100 * as definition and definition_after_data
1101 *
ba21c9d4 1102 * @package moodlecore
1103 * @copyright Jamie Pratt <me@jamiep.org>
1104 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
da1320da 1105 */
7f40a229 1106class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
ba21c9d4 1107 /** @var array */
7f40a229 1108 var $_types = array();
50ef8eb9 1109 var $_dependencies = array();
19110c57 1110 /**
1111 * Array of buttons that if pressed do not result in the processing of the form.
1112 *
1113 * @var array
1114 */
1115 var $_noSubmitButtons=array();
1116 /**
1117 * Array of buttons that if pressed do not result in the processing of the form.
1118 *
1119 * @var array
1120 */
1121 var $_cancelButtons=array();
7f40a229 1122
19194f82 1123 /**
1124 * Array whose keys are element names. If the key exists this is a advanced element
1125 *
1126 * @var array
1127 */
1128 var $_advancedElements = array();
1129
1130 /**
1131 * Whether to display advanced elements (on page load)
1132 *
1133 * @var boolean
1134 */
1135 var $_showAdvanced = null;
1136
f07b9627 1137 /**
2836e77d 1138 * The form name is derived from the class name of the wrapper minus the trailing form
f07b9627 1139 * It is a name with words joined by underscores whereas the id attribute is words joined by
1140 * underscores.
1141 *
1142 * @var unknown_type
1143 */
1144 var $_formName = '';
43914931 1145
4f51f48f 1146 /**
1147 * String with the html for hidden params passed in as part of a moodle_url object for the action. Output in the form.
1148 *
1149 * @var string
1150 */
1151 var $_pageparams = '';
1152
da6f8763 1153 /**
1154 * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
ba21c9d4 1155 *
1156 * @global object
1157 * @staticvar int $formcounter
da6f8763 1158 * @param string $formName Form's name.
1159 * @param string $method (optional)Form's method defaults to 'POST'
4f51f48f 1160 * @param mixed $action (optional)Form's action - string or moodle_url
da6f8763 1161 * @param string $target (optional)Form's target defaults to none
1162 * @param mixed $attributes (optional)Extra attributes for <form> tag
da6f8763 1163 * @access public
1164 */
7f40a229 1165 function MoodleQuickForm($formName, $method, $action, $target='', $attributes=null){
96705c33 1166 global $CFG, $OUTPUT;
dcf6d93c 1167
271ffe3f 1168 static $formcounter = 1;
7f40a229 1169
da6f8763 1170 HTML_Common::HTML_Common($attributes);
da6f8763 1171 $target = empty($target) ? array() : array('target' => $target);
f07b9627 1172 $this->_formName = $formName;
4f51f48f 1173 if (is_a($action, 'moodle_url')){
6ea66ff3 1174 $this->_pageparams = html_writer::input_hidden_params($action);
eb788065 1175 $action = $action->out_omit_querystring();
4f51f48f 1176 } else {
1177 $this->_pageparams = '';
1178 }
da6f8763 1179 //no 'name' atttribute for form in xhtml strict :
1327f08e 1180 $attributes = array('action'=>$action, 'method'=>$method,
1181 'accept-charset'=>'utf-8', 'id'=>'mform'.$formcounter) + $target;
271ffe3f 1182 $formcounter++;
da6f8763 1183 $this->updateAttributes($attributes);
da6f8763 1184
7f40a229 1185 //this is custom stuff for Moodle :
da6f8763 1186 $oldclass= $this->getAttribute('class');
1187 if (!empty($oldclass)){
1188 $this->updateAttributes(array('class'=>$oldclass.' mform'));
1189 }else {
80f962df 1190 $this->updateAttributes(array('class'=>'mform'));
da6f8763 1191 }
b5d0cafc
PS
1192 $this->_reqHTML = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
1193 $this->_advancedHTML = '<img class="adv" title="'.get_string('advancedelement', 'form').'" alt="'.get_string('advancedelement', 'form').'" src="'.$OUTPUT->pix_url('adv') .'" />';
1194 $this->setRequiredNote(get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />'));
19194f82 1195 }
1196
a23f0aaf 1197 /**
1198 * Use this method to indicate an element in a form is an advanced field. If items in a form
1199 * are marked as advanced then 'Hide/Show Advanced' buttons will automatically be displayed in the
1200 * form so the user can decide whether to display advanced form controls.
1201 *
1202 * If you set a header element to advanced then all elements it contains will also be set as advanced.
1203 *
1204 * @param string $elementName group or element name (not the element name of something inside a group).
1205 * @param boolean $advanced default true sets the element to advanced. False removes advanced mark.
1206 */
1207 function setAdvanced($elementName, $advanced=true){
1208 if ($advanced){
1209 $this->_advancedElements[$elementName]='';
1210 } elseif (isset($this->_advancedElements[$elementName])) {
1211 unset($this->_advancedElements[$elementName]);
1212 }
1213 if ($advanced && $this->getElementType('mform_showadvanced_last')===false){
1214 $this->setShowAdvanced();
1215 $this->registerNoSubmitButton('mform_showadvanced');
1216
1217 $this->addElement('hidden', 'mform_showadvanced_last');
d18e0fe6 1218 $this->setType('mform_showadvanced_last', PARAM_INT);
a23f0aaf 1219 }
1220 }
1221 /**
1222 * Set whether to show advanced elements in the form on first displaying form. Default is not to
1223 * display advanced elements in the form until 'Show Advanced' is pressed.
1224 *
1225 * You can get the last state of the form and possibly save it for this user by using
1226 * value 'mform_showadvanced_last' in submitted data.
1227 *
1228 * @param boolean $showadvancedNow
1229 */
1230 function setShowAdvanced($showadvancedNow = null){
1231 if ($showadvancedNow === null){
1232 if ($this->_showAdvanced !== null){
1233 return;
1234 } else { //if setShowAdvanced is called without any preference
1235 //make the default to not show advanced elements.
f07b9627 1236 $showadvancedNow = get_user_preferences(
1237 moodle_strtolower($this->_formName.'_showadvanced', 0));
a23f0aaf 1238 }
a23f0aaf 1239 }
1240 //value of hidden element
1241 $hiddenLast = optional_param('mform_showadvanced_last', -1, PARAM_INT);
1242 //value of button
1243 $buttonPressed = optional_param('mform_showadvanced', 0, PARAM_RAW);
1244 //toggle if button pressed or else stay the same
1245 if ($hiddenLast == -1) {
1246 $next = $showadvancedNow;
1247 } elseif ($buttonPressed) { //toggle on button press
1248 $next = !$hiddenLast;
1249 } else {
1250 $next = $hiddenLast;
1251 }
1252 $this->_showAdvanced = $next;
f07b9627 1253 if ($showadvancedNow != $next){
1254 set_user_preference($this->_formName.'_showadvanced', $next);
1255 }
a23f0aaf 1256 $this->setConstants(array('mform_showadvanced_last'=>$next));
19194f82 1257 }
1258 function getShowAdvanced(){
1259 return $this->_showAdvanced;
1260 }
1261
19194f82 1262
1263 /**
1264 * Accepts a renderer
1265 *
ba21c9d4 1266 * @param object $renderer HTML_QuickForm_Renderer An HTML_QuickForm_Renderer object
19194f82 1267 * @access public
1268 * @return void
1269 */
46f3921e 1270 function accept(&$renderer) {
19194f82 1271 if (method_exists($renderer, 'setAdvancedElements')){
1272 //check for visible fieldsets where all elements are advanced
1273 //and mark these headers as advanced as well.
1274 //And mark all elements in a advanced header as advanced
1275 $stopFields = $renderer->getStopFieldSetElements();
1276 $lastHeader = null;
1277 $lastHeaderAdvanced = false;
1278 $anyAdvanced = false;
1279 foreach (array_keys($this->_elements) as $elementIndex){
1280 $element =& $this->_elements[$elementIndex];
46f3921e 1281
1282 // if closing header and any contained element was advanced then mark it as advanced
19194f82 1283 if ($element->getType()=='header' || in_array($element->getName(), $stopFields)){
46f3921e 1284 if ($anyAdvanced && !is_null($lastHeader)){
19194f82 1285 $this->setAdvanced($lastHeader->getName());
1286 }
1287 $lastHeaderAdvanced = false;
46f3921e 1288 unset($lastHeader);
1289 $lastHeader = null;
19194f82 1290 } elseif ($lastHeaderAdvanced) {
1291 $this->setAdvanced($element->getName());
1292 }
46f3921e 1293
19194f82 1294 if ($element->getType()=='header'){
1295 $lastHeader =& $element;
1296 $anyAdvanced = false;
1297 $lastHeaderAdvanced = isset($this->_advancedElements[$element->getName()]);
1298 } elseif (isset($this->_advancedElements[$element->getName()])){
1299 $anyAdvanced = true;
1300 }
1301 }
46f3921e 1302 // the last header may not be closed yet...
1303 if ($anyAdvanced && !is_null($lastHeader)){
1304 $this->setAdvanced($lastHeader->getName());
1305 }
19194f82 1306 $renderer->setAdvancedElements($this->_advancedElements);
19194f82 1307
19194f82 1308 }
1309 parent::accept($renderer);
1310 }
1311
ba21c9d4 1312 /**
1313 * @param string $elementName
1314 */
19194f82 1315 function closeHeaderBefore($elementName){
1316 $renderer =& $this->defaultRenderer();
1317 $renderer->addStopFieldsetElements($elementName);
da6f8763 1318 }
bb40325e 1319
da1320da 1320 /**
1321 * Should be used for all elements of a form except for select, radio and checkboxes which
1322 * clean their own data.
1323 *
1324 * @param string $elementname
1325 * @param integer $paramtype use the constants PARAM_*.
1326 * * PARAM_CLEAN is deprecated and you should try to use a more specific type.
1327 * * PARAM_TEXT should be used for cleaning data that is expected to be plain text.
1328 * It will strip all html tags. But will still let tags for multilang support
1329 * through.
1330 * * PARAM_RAW means no cleaning whatsoever, it is used mostly for data from the
1331 * html editor. Data from the editor is later cleaned before display using
1332 * format_text() function. PARAM_RAW can also be used for data that is validated
1333 * by some other way or printed by p() or s().
1334 * * PARAM_INT should be used for integers.
1335 * * PARAM_ACTION is an alias of PARAM_ALPHA and is used for hidden fields specifying
1336 * form actions.
1337 */
7f40a229 1338 function setType($elementname, $paramtype) {
1339 $this->_types[$elementname] = $paramtype;
1340 }
49292f8c 1341
da1320da 1342 /**
1343 * See description of setType above. This can be used to set several types at once.
1344 *
1345 * @param array $paramtypes
1346 */
c56f1826 1347 function setTypes($paramtypes) {
1348 $this->_types = $paramtypes + $this->_types;
1349 }
49292f8c 1350
ba21c9d4 1351 /**
1352 * @param array $submission
1353 * @param array $files
1354 */
49292f8c 1355 function updateSubmission($submission, $files) {
1356 $this->_flagSubmitted = false;
1357
7f40a229 1358 if (empty($submission)) {
1359 $this->_submitValues = array();
7f40a229 1360 } else {
1361 foreach ($submission as $key=>$s) {
1362 if (array_key_exists($key, $this->_types)) {
1363 $submission[$key] = clean_param($s, $this->_types[$key]);
1364 }
1365 }
294ce987 1366 $this->_submitValues = $submission;
7f40a229 1367 $this->_flagSubmitted = true;
1368 }
1369
49292f8c 1370 if (empty($files)) {
1371 $this->_submitFiles = array();
1372 } else {
49292f8c 1373 $this->_submitFiles = $files;
1374 $this->_flagSubmitted = true;
1375 }
1376
2c412890 1377 // need to tell all elements that they need to update their value attribute.
1378 foreach (array_keys($this->_elements) as $key) {
1379 $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
1380 }
7f40a229 1381 }
1382
ba21c9d4 1383 /**
1384 * @return string
1385 */
da6f8763 1386 function getReqHTML(){
1387 return $this->_reqHTML;
1388 }
117bd748 1389
ba21c9d4 1390 /**
1391 * @return string
1392 */
19194f82 1393 function getAdvancedHTML(){
1394 return $this->_advancedHTML;
1395 }
1396
7f40a229 1397 /**
da1320da 1398 * Initializes a default form value. Used to specify the default for a new entry where
beac4717 1399 * no data is loaded in using moodleform::set_data()
7f40a229 1400 *
294ce987 1401 * note: $slashed param removed
1402 *
7f40a229 1403 * @param string $elementname element name
1404 * @param mixed $values values for that element name
7f40a229 1405 * @access public
1406 * @return void
1407 */
294ce987 1408 function setDefault($elementName, $defaultValue){
1409 $this->setDefaults(array($elementName=>$defaultValue));
7f40a229 1410 } // end func setDefault
da6f8763 1411 /**
c56f1826 1412 * Add an array of buttons to the form
7f40a229 1413 * @param array $buttons An associative array representing help button to attach to
da6f8763 1414 * to the form. keys of array correspond to names of elements in form.
c9a2cdf8 1415 * @deprecated since Moodle 2.0 - use addHelpButton() call on each element manually
ba21c9d4 1416 * @param bool $suppresscheck
1417 * @param string $function
da6f8763 1418 * @access public
1419 */
d4fe14d3 1420 function setHelpButtons($buttons, $suppresscheck=false, $function='helpbutton'){
7f40a229 1421
c9a2cdf8
DM
1422 debugging('function moodle_form::setHelpButtons() is deprecated');
1423 //foreach ($buttons as $elementname => $button){
1424 // $this->setHelpButton($elementname, $button, $suppresscheck, $function);
1425 //}
da6f8763 1426 }
c56f1826 1427 /**
da1320da 1428 * Add a single button.
c56f1826 1429 *
ff95caa8 1430 * @deprecated use addHelpButton() instead
c56f1826 1431 * @param string $elementname name of the element to add the item to
ba21c9d4 1432 * @param array $button arguments to pass to function $function
1433 * @param boolean $suppresscheck whether to throw an error if the element
c56f1826 1434 * doesn't exist.
d4fe14d3 1435 * @param string $function - function to generate html from the arguments in $button
ba21c9d4 1436 * @param string $function
c56f1826 1437 */
4bcc5118 1438 function setHelpButton($elementname, $buttonargs, $suppresscheck=false, $function='helpbutton'){
642816a6 1439 global $OUTPUT;
cde9bee8
DM
1440
1441 debugging('function moodle_form::setHelpButton() is deprecated');
4bcc5118 1442 if ($function !== 'helpbutton') {
b65d2720 1443 //debugging('parameter $function in moodle_form::setHelpButton() is not supported any more');
4bcc5118
PS
1444 }
1445
1446 $buttonargs = (array)$buttonargs;
1447
1448 if (array_key_exists($elementname, $this->_elementIndex)) {
c56f1826 1449 //_elements has a numeric index, this code accesses the elements by name
4bcc5118 1450 $element = $this->_elements[$this->_elementIndex[$elementname]];
642816a6 1451
4bcc5118
PS
1452 $page = isset($buttonargs[0]) ? $buttonargs[0] : null;
1453 $text = isset($buttonargs[1]) ? $buttonargs[1] : null;
1454 $module = isset($buttonargs[2]) ? $buttonargs[2] : 'moodle';
1455 $linktext = isset($buttonargs[3]) ? $buttonargs[3] : false;
1456
596509e4 1457 $element->_helpbutton = $OUTPUT->old_help_icon($page, $text, $module, $linktext);
642816a6 1458
4bcc5118 1459 } else if (!$suppresscheck) {
c56f1826 1460 print_error('nonexistentformelements', 'form', '', $elementname);
2c412890 1461 }
c56f1826 1462 }
7f40a229 1463
53a78cef 1464 /**
259c165d
PS
1465 * Add a help button to element, only one button per element is allowed.
1466 *
ff95caa8
DM
1467 * This is new, simplified and preferable method of setting a help icon on form elements.
1468 * It uses the new $OUTPUT->help_icon().
1469 *
1470 * Typically, you will provide the same identifier and the component as you have used for the
1471 * label of the element. The string identifier with the _help suffix added is then used
1472 * as the help string.
1473 *
259c165d
PS
1474 * There has to be two strings defined:
1475 * 1/ get_string($identifier, $component) - the title of the help page
5435c9dc 1476 * 2/ get_string($identifier.'_help', $component) - the actual help page text
53a78cef 1477 *
ff95caa8 1478 * @since 2.0
53a78cef 1479 * @param string $elementname name of the element to add the item to
2836e77d 1480 * @param string $identifier help string identifier without _help suffix
ff95caa8
DM
1481 * @param string $component component name to look the help string in
1482 * @param string $linktext optional text to display next to the icon
1483 * @param boolean $suppresscheck set to true if the element may not exist
53a78cef
PS
1484 * @return void
1485 */
259c165d
PS
1486 function addHelpButton($elementname, $identifier, $component = 'moodle', $linktext = '', $suppresscheck = false) {
1487 global $OUTPUT;
53a78cef 1488 if (array_key_exists($elementname, $this->_elementIndex)) {
af140288
DC
1489 $element = $this->_elements[$this->_elementIndex[$elementname]];
1490 $element->_helpbutton = $OUTPUT->help_icon($identifier, $component, $linktext);
53a78cef
PS
1491 } else if (!$suppresscheck) {
1492 debugging(get_string('nonexistentformelements', 'form', $elementname));
1493 }
1494 }
1495
cc444336 1496 /**
2836e77d 1497 * Set constant value not overridden by _POST or _GET
cc444336 1498 * note: this does not work for complex names with [] :-(
ba21c9d4 1499 *
cc444336 1500 * @param string $elname name of element
1501 * @param mixed $value
1502 * @return void
1503 */
1504 function setConstant($elname, $value) {
1505 $this->_constantValues = HTML_QuickForm::arrayMerge($this->_constantValues, array($elname=>$value));
1506 $element =& $this->getElement($elname);
1507 $element->onQuickFormEvent('updateValue', null, $this);
1508 }
1509
ba21c9d4 1510 /**
1511 * @param string $elementList
1512 */
294ce987 1513 function exportValues($elementList = null){
0ffb4cc7 1514 $unfiltered = array();
1515 if (null === $elementList) {
1516 // iterate over all elements, calling their exportValue() methods
98af2d1d 1517 $emptyarray = array();
0ffb4cc7 1518 foreach (array_keys($this->_elements) as $key) {
1519 if ($this->_elements[$key]->isFrozen() && !$this->_elements[$key]->_persistantFreeze){
98af2d1d 1520 $value = $this->_elements[$key]->exportValue($emptyarray, true);
0ffb4cc7 1521 } else {
1522 $value = $this->_elements[$key]->exportValue($this->_submitValues, true);
1523 }
1524
1525 if (is_array($value)) {
1526 // This shit throws a bogus warning in PHP 4.3.x
1527 $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
1528 }
1529 }
1530 } else {
1531 if (!is_array($elementList)) {
1532 $elementList = array_map('trim', explode(',', $elementList));
1533 }
1534 foreach ($elementList as $elementName) {
1535 $value = $this->exportValue($elementName);
1536 if (PEAR::isError($value)) {
1537 return $value;
1538 }
4287fc0d 1539 //oh, stock QuickFOrm was returning array of arrays!
1540 $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
0ffb4cc7 1541 }
1542 }
7f40a229 1543
294ce987 1544 return $unfiltered;
da6f8763 1545 }
f07b9627 1546 /**
1547 * Adds a validation rule for the given field
1548 *
1549 * If the element is in fact a group, it will be considered as a whole.
1550 * To validate grouped elements as separated entities,
1551 * use addGroupRule instead of addRule.
1552 *
1553 * @param string $element Form element name
1554 * @param string $message Message to display for invalid data
1555 * @param string $type Rule type, use getRegisteredRules() to get types
1556 * @param string $format (optional)Required for extra rule data
1557 * @param string $validation (optional)Where to perform validation: "server", "client"
1558 * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
1559 * @param boolean $force Force the rule to be applied, even if the target form element does not exist
f07b9627 1560 * @access public
f07b9627 1561 */
1562 function addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false)
1563 {
1564 parent::addRule($element, $message, $type, $format, $validation, $reset, $force);
1565 if ($validation == 'client') {
1566 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
1567 }
1568
1569 } // end func addRule
1570 /**
1571 * Adds a validation rule for the given group of elements
1572 *
1573 * Only groups with a name can be assigned a validation rule
1574 * Use addGroupRule when you need to validate elements inside the group.
1575 * Use addRule if you need to validate the group as a whole. In this case,
1576 * the same rule will be applied to all elements in the group.
1577 * Use addRule if you need to validate the group against a function.
1578 *
1579 * @param string $group Form group name
1580 * @param mixed $arg1 Array for multiple elements or error message string for one element
1581 * @param string $type (optional)Rule type use getRegisteredRules() to get types
1582 * @param string $format (optional)Required for extra rule data
1583 * @param int $howmany (optional)How many valid elements should be in the group
1584 * @param string $validation (optional)Where to perform validation: "server", "client"
1585 * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed.
f07b9627 1586 * @access public
f07b9627 1587 */
1588 function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)
1589 {
1590 parent::addGroupRule($group, $arg1, $type, $format, $howmany, $validation, $reset);
1591 if (is_array($arg1)) {
3a298174 1592 foreach ($arg1 as $rules) {
f07b9627 1593 foreach ($rules as $rule) {
1594 $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server';
1595
1596 if ('client' == $validation) {
1597 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
1598 }
1599 }
1600 }
1601 } elseif (is_string($arg1)) {
1602
1603 if ($validation == 'client') {
3a298174 1604 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
f07b9627 1605 }
1606 }
1607 } // end func addGroupRule
1608
1609 // }}}
5bc97c98 1610 /**
1611 * Returns the client side validation script
1612 *
1613 * The code here was copied from HTML_QuickForm_DHTMLRulesTableless who copied it from HTML_QuickForm
1614 * and slightly modified to run rules per-element
1615 * Needed to override this because of an error with client side validation of grouped elements.
1616 *
1617 * @access public
1618 * @return string Javascript to perform validation, empty string if no 'client' rules were added
1619 */
1620 function getValidationScript()
1621 {
1622 if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
1623 return '';
1624 }
1625
1626 include_once('HTML/QuickForm/RuleRegistry.php');
1627 $registry =& HTML_QuickForm_RuleRegistry::singleton();
1628 $test = array();
1629 $js_escape = array(
1630 "\r" => '\r',
1631 "\n" => '\n',
1632 "\t" => '\t',
1633 "'" => "\\'",
1634 '"' => '\"',
1635 '\\' => '\\\\'
1636 );
1637
1638 foreach ($this->_rules as $elementName => $rules) {
1639 foreach ($rules as $rule) {
1640 if ('client' == $rule['validation']) {
da1320da 1641 unset($element); //TODO: find out how to properly initialize it
5bc97c98 1642
1643 $dependent = isset($rule['dependent']) && is_array($rule['dependent']);
1644 $rule['message'] = strtr($rule['message'], $js_escape);
1645
1646 if (isset($rule['group'])) {
1647 $group =& $this->getElement($rule['group']);
1648 // No JavaScript validation for frozen elements
1649 if ($group->isFrozen()) {
1650 continue 2;
1651 }
1652 $elements =& $group->getElements();
1653 foreach (array_keys($elements) as $key) {
1654 if ($elementName == $group->getElementName($key)) {
1655 $element =& $elements[$key];
1656 break;
1657 }
1658 }
1659 } elseif ($dependent) {
1660 $element = array();
1661 $element[] =& $this->getElement($elementName);
3a298174 1662 foreach ($rule['dependent'] as $elName) {
5bc97c98 1663 $element[] =& $this->getElement($elName);
1664 }
1665 } else {
1666 $element =& $this->getElement($elementName);
1667 }
1668 // No JavaScript validation for frozen elements
1669 if (is_object($element) && $element->isFrozen()) {
1670 continue 2;
1671 } elseif (is_array($element)) {
1672 foreach (array_keys($element) as $key) {
1673 if ($element[$key]->isFrozen()) {
1674 continue 3;
1675 }
1676 }
1677 }
1678 // Fix for bug displaying errors for elements in a group
1679 //$test[$elementName][] = $registry->getValidationScript($element, $elementName, $rule);
1680 $test[$elementName][0][] = $registry->getValidationScript($element, $elementName, $rule);
1681 $test[$elementName][1]=$element;
1682 //end of fix
1683 }
1684 }
1685 }
7c77033f 1686
1687 // Fix for MDL-9524. If you don't do this, then $element may be left as a reference to one of the fields in
1688 // the form, and then that form field gets corrupted by the code that follows.
1689 unset($element);
1690
5bc97c98 1691 $js = '
1692<script type="text/javascript">
1693//<![CDATA[
1cbb09f1 1694
1695var skipClientValidation = false;
1696
5bc97c98 1697function qf_errorHandler(element, _qfMsg) {
1698 div = element.parentNode;
1699 if (_qfMsg != \'\') {
e35c9eeb 1700 var errorSpan = document.getElementById(\'id_error_\'+element.name);
e7004d05 1701 if (!errorSpan) {
1702 errorSpan = document.createElement("span");
e35c9eeb 1703 errorSpan.id = \'id_error_\'+element.name;
1704 errorSpan.className = "error";
fed13a5e 1705 element.parentNode.insertBefore(errorSpan, element.parentNode.firstChild);
5bc97c98 1706 }
fed13a5e 1707
e7004d05 1708 while (errorSpan.firstChild) {
1709 errorSpan.removeChild(errorSpan.firstChild);
5bc97c98 1710 }
2c412890 1711
e7004d05 1712 errorSpan.appendChild(document.createTextNode(_qfMsg.substring(3)));
e35c9eeb 1713 errorSpan.appendChild(document.createElement("br"));
5bc97c98 1714
1715 if (div.className.substr(div.className.length - 6, 6) != " error"
1716 && div.className != "error") {
1717 div.className += " error";
1718 }
1719
1720 return false;
1721 } else {
e35c9eeb 1722 var errorSpan = document.getElementById(\'id_error_\'+element.name);
e7004d05 1723 if (errorSpan) {
1724 errorSpan.parentNode.removeChild(errorSpan);
5bc97c98 1725 }
1726
1727 if (div.className.substr(div.className.length - 6, 6) == " error") {
1728 div.className = div.className.substr(0, div.className.length - 6);
1729 } else if (div.className == "error") {
1730 div.className = "";
1731 }
1732
1733 return true;
1734 }
1735}';
1736 $validateJS = '';
1737 foreach ($test as $elementName => $jsandelement) {
1738 // Fix for bug displaying errors for elements in a group
1739 //unset($element);
1740 list($jsArr,$element)=$jsandelement;
1741 //end of fix
1742 $js .= '
f07b9627 1743function validate_' . $this->_formName . '_' . $elementName . '(element) {
5bc97c98 1744 var value = \'\';
1745 var errFlag = new Array();
1746 var _qfGroups = {};
1747 var _qfMsg = \'\';
1748 var frm = element.parentNode;
cd350b53 1749 while (frm && frm.nodeName.toUpperCase() != "FORM") {
5bc97c98 1750 frm = frm.parentNode;
1751 }
1752' . join("\n", $jsArr) . '
1753 return qf_errorHandler(element, _qfMsg);
1754}
1755';
1756 $validateJS .= '
573e7e33 1757 ret = validate_' . $this->_formName . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret;
2ef7c374 1758 if (!ret && !first_focus) {
1759 first_focus = true;
1760 frm.elements[\''.$elementName.'\'].focus();
1761 }
1762';
4f51f48f 1763
5bc97c98 1764 // Fix for bug displaying errors for elements in a group
1765 //unset($element);
1766 //$element =& $this->getElement($elementName);
1767 //end of fix
f07b9627 1768 $valFunc = 'validate_' . $this->_formName . '_' . $elementName . '(this)';
5bc97c98 1769 $onBlur = $element->getAttribute('onBlur');
1770 $onChange = $element->getAttribute('onChange');
1771 $element->updateAttributes(array('onBlur' => $onBlur . $valFunc,
1772 'onChange' => $onChange . $valFunc));
1773 }
e7004d05 1774// do not rely on frm function parameter, because htmlarea breaks it when overloading the onsubmit method
5bc97c98 1775 $js .= '
f07b9627 1776function validate_' . $this->_formName . '(frm) {
1cbb09f1 1777 if (skipClientValidation) {
1778 return true;
1779 }
5bc97c98 1780 var ret = true;
4f51f48f 1781
0befbdfd 1782 var frm = document.getElementById(\''. $this->_attributes['id'] .'\')
2ef7c374 1783 var first_focus = false;
5bc97c98 1784' . $validateJS . ';
1785 return ret;
1786}
1787//]]>
1788</script>';
1789 return $js;
1790 } // end func getValidationScript
1791 function _setDefaultRuleMessages(){
1792 foreach ($this->_rules as $field => $rulesarr){
1793 foreach ($rulesarr as $key => $rule){
1794 if ($rule['message']===null){
1795 $a=new object();
1796 $a->format=$rule['format'];
1797 $str=get_string('err_'.$rule['type'], 'form', $a);
1798 if (strpos($str, '[[')!==0){
1799 $this->_rules[$field][$key]['message']=$str;
2c412890 1800 }
5bc97c98 1801 }
1802 }
1803 }
1804 }
bb40325e 1805
ba21c9d4 1806 /**
1807 * @return string
1808 */
d01a38cb 1809 function getLockOptionEndScript(){
dd07bbac 1810
1811 $iname = $this->getAttribute('id').'items';
f4ba7e1a 1812 $js = '<script type="text/javascript">'."\n";
5e87b920 1813 $js .= '//<![CDATA['."\n";
dd07bbac 1814 $js .= "var $iname = Array();\n";
1815
1816 foreach ($this->_dependencies as $dependentOn => $conditions){
1817 $js .= "{$iname}['$dependentOn'] = Array();\n";
1818 foreach ($conditions as $condition=>$values) {
1819 $js .= "{$iname}['$dependentOn']['$condition'] = Array();\n";
1820 foreach ($values as $value=>$dependents) {
1821 $js .= "{$iname}['$dependentOn']['$condition']['$value'] = Array();\n";
1822 $i = 0;
1823 foreach ($dependents as $dependent) {
1824 $elements = $this->_getElNamesRecursive($dependent);
46f3921e 1825 if (empty($elements)) {
1826 // probably element inside of some group
1827 $elements = array($dependent);
1828 }
dd07bbac 1829 foreach($elements as $element) {
1830 if ($element == $dependentOn) {
1831 continue;
1832 }
1833 $js .= "{$iname}['$dependentOn']['$condition']['$value'][$i]='$element';\n";
1834 $i++;
1835 }
11f260f4 1836 }
1837 }
50ef8eb9 1838 }
dd07bbac 1839 }
d01a38cb 1840 $js .="lockoptionsallsetup('".$this->getAttribute('id')."');\n";
5e87b920 1841 $js .='//]]>'."\n";
50ef8eb9 1842 $js .='</script>'."\n";
1843 return $js;
bb40325e 1844 }
d01a38cb 1845
ba21c9d4 1846 /**
1847 * @param mixed $element
1848 * @return array
1849 */
46f3921e 1850 function _getElNamesRecursive($element) {
1851 if (is_string($element)) {
4f51f48f 1852 if (!$this->elementExists($element)) {
1853 return array();
1854 }
46f3921e 1855 $element = $this->getElement($element);
d01a38cb 1856 }
46f3921e 1857
1858 if (is_a($element, 'HTML_QuickForm_group')) {
1859 $elsInGroup = $element->getElements();
9403060a 1860 $elNames = array();
d01a38cb 1861 foreach ($elsInGroup as $elInGroup){
e850ec48 1862 if (is_a($elInGroup, 'HTML_QuickForm_group')) {
1863 // not sure if this would work - groups nested in groups
1864 $elNames = array_merge($elNames, $this->_getElNamesRecursive($elInGroup));
1865 } else {
1866 $elNames[] = $element->getElementName($elInGroup->getName());
1867 }
d01a38cb 1868 }
46f3921e 1869
1870 } else if (is_a($element, 'HTML_QuickForm_header')) {
1871 return array();
1872
1873 } else if (is_a($element, 'HTML_QuickForm_hidden')) {
1874 return array();
1875
1876 } else if (method_exists($element, 'getPrivateName')) {
1877 return array($element->getPrivateName());
1878
1879 } else {
1880 $elNames = array($element->getName());
d01a38cb 1881 }
d01a38cb 1882
46f3921e 1883 return $elNames;
50ef8eb9 1884 }
46f3921e 1885
6e372b25 1886 /**
1887 * Adds a dependency for $elementName which will be disabled if $condition is met.
9403060a 1888 * If $condition = 'notchecked' (default) then the condition is that the $dependentOn element
1889 * is not checked. If $condition = 'checked' then the condition is that the $dependentOn element
31a6c06c 1890 * is checked. If $condition is something else (like "eq" for equals) then it is checked to see if the value
1891 * of the $dependentOn element is $condition (such as equal) to $value.
6e372b25 1892 *
1893 * @param string $elementName the name of the element which will be disabled
1894 * @param string $dependentOn the name of the element whose state will be checked for
1895 * condition
1896 * @param string $condition the condition to check
19110c57 1897 * @param mixed $value used in conjunction with condition.
6e372b25 1898 */
dd07bbac 1899 function disabledIf($elementName, $dependentOn, $condition = 'notchecked', $value='1'){
1900 if (!array_key_exists($dependentOn, $this->_dependencies)) {
1901 $this->_dependencies[$dependentOn] = array();
1902 }
1903 if (!array_key_exists($condition, $this->_dependencies[$dependentOn])) {
1904 $this->_dependencies[$dependentOn][$condition] = array();
1905 }
1906 if (!array_key_exists($value, $this->_dependencies[$dependentOn][$condition])) {
1907 $this->_dependencies[$dependentOn][$condition][$value] = array();
1908 }
1909 $this->_dependencies[$dependentOn][$condition][$value][] = $elementName;
bb40325e 1910 }
dd07bbac 1911
a23f0aaf 1912 function registerNoSubmitButton($buttonname){
1913 $this->_noSubmitButtons[]=$buttonname;
1914 }
dd07bbac 1915
ba21c9d4 1916 /**
1917 * @param string $buttonname
1918 * @return mixed
1919 */
a23f0aaf 1920 function isNoSubmitButton($buttonname){
1921 return (array_search($buttonname, $this->_noSubmitButtons)!==FALSE);
19110c57 1922 }
dd07bbac 1923
ba21c9d4 1924 /**
1925 * @param string $buttonname
1926 */
19110c57 1927 function _registerCancelButton($addfieldsname){
1928 $this->_cancelButtons[]=$addfieldsname;
1929 }
acc9c3e0 1930 /**
1931 * Displays elements without HTML input tags.
1932 * This method is different to freeze() in that it makes sure no hidden
cc444336 1933 * elements are included in the form.
1934 * Note: If you want to make sure the submitted value is ignored, please use setDefaults().
acc9c3e0 1935 *
4af06dda 1936 * This function also removes all previously defined rules.
1937 *
acc9c3e0 1938 * @param mixed $elementList array or string of element(s) to be frozen
acc9c3e0 1939 * @access public
acc9c3e0 1940 */
1941 function hardFreeze($elementList=null)
1942 {
1943 if (!isset($elementList)) {
1944 $this->_freezeAll = true;
1945 $elementList = array();
1946 } else {
1947 if (!is_array($elementList)) {
1948 $elementList = preg_split('/[ ]*,[ ]*/', $elementList);
1949 }
1950 $elementList = array_flip($elementList);
1951 }
1952
1953 foreach (array_keys($this->_elements) as $key) {
1954 $name = $this->_elements[$key]->getName();
1955 if ($this->_freezeAll || isset($elementList[$name])) {
1956 $this->_elements[$key]->freeze();
1957 $this->_elements[$key]->setPersistantFreeze(false);
1958 unset($elementList[$name]);
4af06dda 1959
1960 // remove all rules
1961 $this->_rules[$name] = array();
1962 // if field is required, remove the rule
1963 $unset = array_search($name, $this->_required);
1964 if ($unset !== false) {
1965 unset($this->_required[$unset]);
1966 }
acc9c3e0 1967 }
1968 }
1969
1970 if (!empty($elementList)) {
1971 return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Nonexistant element(s): '" . implode("', '", array_keys($elementList)) . "' in HTML_QuickForm::freeze()", 'HTML_QuickForm_Error', true);
1972 }
1973 return true;
4f51f48f 1974 }
1975 /**
1976 * Hard freeze all elements in a form except those whose names are in $elementList or hidden elements in a form.
1977 *
1978 * This function also removes all previously defined rules of elements it freezes.
1979 *
ba21c9d4 1980 * throws HTML_QuickForm_Error
1981 *
4f51f48f 1982 * @param array $elementList array or string of element(s) not to be frozen
4f51f48f 1983 * @access public
4f51f48f 1984 */
1985 function hardFreezeAllVisibleExcept($elementList)
1986 {
1987 $elementList = array_flip($elementList);
1988 foreach (array_keys($this->_elements) as $key) {
1989 $name = $this->_elements[$key]->getName();
1990 $type = $this->_elements[$key]->getType();
56015454 1991
4f51f48f 1992 if ($type == 'hidden'){
1993 // leave hidden types as they are
1994 } elseif (!isset($elementList[$name])) {
1995 $this->_elements[$key]->freeze();
1996 $this->_elements[$key]->setPersistantFreeze(false);
1997
1998 // remove all rules
1999 $this->_rules[$name] = array();
2000 // if field is required, remove the rule
2001 $unset = array_search($name, $this->_required);
2002 if ($unset !== false) {
2003 unset($this->_required[$unset]);
2004 }
2005 }
2006 }
2007 return true;
2008 }
2009 /**
2010 * Tells whether the form was already submitted
2011 *
2012 * This is useful since the _submitFiles and _submitValues arrays
2013 * may be completely empty after the trackSubmit value is removed.
2014 *
2015 * @access public
2016 * @return bool
2017 */
2018 function isSubmitted()
2019 {
2020 return parent::isSubmitted() && (!$this->isFrozen());
2021 }
da6f8763 2022}
2023
e24b7f85 2024
da6f8763 2025/**
7f40a229 2026 * A renderer for MoodleQuickForm that only uses XHTML and CSS and no
da6f8763 2027 * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless
7f40a229 2028 *
da6f8763 2029 * Stylesheet is part of standard theme and should be automatically included.
2030 *
ba21c9d4 2031 * @package moodlecore
2032 * @copyright Jamie Pratt <me@jamiep.org>
2033 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
da6f8763 2034 */
7f40a229 2035class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{
da6f8763 2036
2037 /**
2038 * Element template array
2039 * @var array
2040 * @access private
2041 */
2042 var $_elementTemplates;
49c53687 2043 /**
2044 * Template used when opening a hidden fieldset
2045 * (i.e. a fieldset that is opened when there is no header element)
2046 * @var string
2047 * @access private
2048 */
c02345e3 2049 var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\"><div>";
19194f82 2050 /**
2051 * Header Template string
2052 * @var string
2053 * @access private
2054 */
2055 var $_headerTemplate =
c02345e3 2056 "\n\t\t<legend class=\"ftoggler\">{header}</legend>\n\t\t<div class=\"advancedbutton\">{advancedimg}{button}</div><div class=\"fcontainer clearfix\">\n\t\t";
7f40a229 2057
49c53687 2058 /**
bc9ec4a6 2059 * Template used when opening a fieldset
2060 * @var string
2061 * @access private
2062 */
2063 var $_openFieldsetTemplate = "\n\t<fieldset class=\"clearfix\" {id}>";
2064
2065 /**
49c53687 2066 * Template used when closing a fieldset
2067 * @var string
2068 * @access private
2069 */
c02345e3 2070 var $_closeFieldsetTemplate = "\n\t\t</div></fieldset>";
42f248e6 2071
49c53687 2072 /**
2073 * Required Note template string
2074 * @var string
2075 * @access private
2076 */
7f40a229 2077 var $_requiredNoteTemplate =
6ba2c73d 2078 "\n\t\t<div class=\"fdescription required\">{requiredNote}</div>";
7f40a229 2079
19194f82 2080 var $_advancedElements = array();
2081
2082 /**
2083 * Whether to display advanced elements (on page load)
2084 *
2085 * @var integer 1 means show 0 means hide
2086 */
2087 var $_showAdvanced;
2088
7f40a229 2089 function MoodleQuickForm_Renderer(){
42f248e6 2090 // switch next two lines for ol li containers for form items.
49c7f3a8 2091 // $this->_elementTemplates=array('default'=>"\n\t\t".'<li class="fitem"><label>{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class="qfelement<!-- BEGIN error --> error<!-- END error --> {type}"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div></li>');
9aa022fe 2092 $this->_elementTemplates = array(
f8b9ac74 2093 'default'=>"\n\t\t".'<div class="fitem {advanced}<!-- BEGIN required --> required<!-- END required -->"><div class="fitemtitle"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} {help}</label></div><div class="felement {type}<!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div></div>',
49c7f3a8 2094
f9f9be73 2095 'fieldset'=>"\n\t\t".'<div class="fitem {advanced}<!-- BEGIN required --> required<!-- END required -->"><div class="fitemtitle"><div class="fgrouplabel"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} {help}</label></div></div><fieldset class="felement {type}<!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</fieldset></div>',
4f51f48f 2096
f9f9be73 2097 'static'=>"\n\t\t".'<div class="fitem {advanced}"><div class="fitemtitle"><div class="fstaticlabel"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} {help}</label></div></div><div class="felement fstatic <!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}&nbsp;</div></div>',
43914931 2098
1ae1941e 2099'warning'=>"\n\t\t".'<div class="fitem {advanced}">{element}</div>',
2100
4f51f48f 2101 'nodisplay'=>'');
da6f8763 2102
2103 parent::HTML_QuickForm_Renderer_Tableless();
2104 }
7f40a229 2105
ba21c9d4 2106 /**
2107 * @param array $elements
2108 */
19194f82 2109 function setAdvancedElements($elements){
2110 $this->_advancedElements = $elements;
2111 }
2112
2113 /**
2114 * What to do when starting the form
2115 *
ba21c9d4 2116 * @param object $form MoodleQuickForm
19194f82 2117 */
da6f8763 2118 function startForm(&$form){
9403060a 2119 $this->_reqHTML = $form->getReqHTML();
2120 $this->_elementTemplates = str_replace('{req}', $this->_reqHTML, $this->_elementTemplates);
19194f82 2121 $this->_advancedHTML = $form->getAdvancedHTML();
2122 $this->_showAdvanced = $form->getShowAdvanced();
da6f8763 2123 parent::startForm($form);
4f51f48f 2124 if ($form->isFrozen()){
2125 $this->_formTemplate = "\n<div class=\"mform frozen\">\n{content}\n</div>";
2126 } else {
0524b1d9 2127 $this->_formTemplate = "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{content}\n</form>";
4f51f48f 2128 $this->_hiddenHtml .= $form->_pageparams;
2129 }
2130
2131
da6f8763 2132 }
117bd748 2133
ba21c9d4 2134 /**
2135 * @param object $group Passed by reference
2136 * @param mixed $required
2137 * @param mixed $error
2138 */
da6f8763 2139 function startGroup(&$group, $required, $error){
2140 if (method_exists($group, 'getElementTemplateType')){
e249661f 2141 $html = $this->_elementTemplates[$group->getElementTemplateType()];
da6f8763 2142 }else{
2143 $html = $this->_elementTemplates['default'];
7f40a229 2144
da6f8763 2145 }
19194f82 2146 if ($this->_showAdvanced){
2147 $advclass = ' advanced';
2148 } else {
2149 $advclass = ' advanced hide';
2150 }
2151 if (isset($this->_advancedElements[$group->getName()])){
2152 $html =str_replace(' {advanced}', $advclass, $html);
2153 $html =str_replace('{advancedimg}', $this->_advancedHTML, $html);
2154 } else {
2155 $html =str_replace(' {advanced}', '', $html);
2156 $html =str_replace('{advancedimg}', '', $html);
2157 }
da6f8763 2158 if (method_exists($group, 'getHelpButton')){
2159 $html =str_replace('{help}', $group->getHelpButton(), $html);
2160 }else{
2161 $html =str_replace('{help}', '', $html);
da6f8763 2162 }
e7004d05 2163 $html =str_replace('{name}', $group->getName(), $html);
49c53687 2164 $html =str_replace('{type}', 'fgroup', $html);
7f40a229 2165
da6f8763 2166 $this->_templates[$group->getName()]=$html;
2167 // Fix for bug in tableless quickforms that didn't allow you to stop a
2168 // fieldset before a group of elements.
2169 // if the element name indicates the end of a fieldset, close the fieldset
2170 if ( in_array($group->getName(), $this->_stopFieldsetElements)
2171 && $this->_fieldsetsOpen > 0
2172 ) {
2173 $this->_html .= $this->_closeFieldsetTemplate;
2174 $this->_fieldsetsOpen--;
2175 }
2176 parent::startGroup($group, $required, $error);
2177 }
ba21c9d4 2178 /**
2179 * @param object $element
2180 * @param mixed $required
2181 * @param mixed $error
2182 */
da6f8763 2183 function renderElement(&$element, $required, $error){
172dd12c 2184 //manipulate id of all elements before rendering
86aab05c 2185 if (!is_null($element->getAttribute('id'))) {
2186 $id = $element->getAttribute('id');
2187 } else {
2188 $id = $element->getName();
2189 }
2190 //strip qf_ prefix and replace '[' with '_' and strip ']'
2191 $id = preg_replace(array('/^qf_|\]/', '/\[/'), array('', '_'), $id);
2192 if (strpos($id, 'id_') !== 0){
2193 $element->updateAttributes(array('id'=>'id_'.$id));
2194 }
2195
2196 //adding stuff to place holders in template
172dd12c 2197 //check if this is a group element first
906ebc4b 2198 if (($this->_inGroup) and !empty($this->_groupElementTemplate)) {
3493eb15 2199 // so it gets substitutions for *each* element
c07b5ad4 2200 $html = $this->_groupElementTemplate;
906ebc4b 2201 }
2202 elseif (method_exists($element, 'getElementTemplateType')){
da6f8763 2203 $html = $this->_elementTemplates[$element->getElementTemplateType()];
2204 }else{
2205 $html = $this->_elementTemplates['default'];
19194f82 2206 }
2207 if ($this->_showAdvanced){
2208 $advclass = ' advanced';
2209 } else {
2210 $advclass = ' advanced hide';
2211 }
2212 if (isset($this->_advancedElements[$element->getName()])){
2213 $html =str_replace(' {advanced}', $advclass, $html);
2214 } else {
2215 $html =str_replace(' {advanced}', '', $html);
2216 }
2217 if (isset($this->_advancedElements[$element->getName()])||$element->getName() == 'mform_showadvanced'){
2218 $html =str_replace('{advancedimg}', $this->_advancedHTML, $html);
2219 } else {
2220 $html =str_replace('{advancedimg}', '', $html);
da6f8763 2221 }
49c53687 2222 $html =str_replace('{type}', 'f'.$element->getType(), $html);
e7004d05 2223 $html =str_replace('{name}', $element->getName(), $html);
da6f8763 2224 if (method_exists($element, 'getHelpButton')){
9403060a 2225 $html = str_replace('{help}', $element->getHelpButton(), $html);
da6f8763 2226 }else{
9403060a 2227 $html = str_replace('{help}', '', $html);
7f40a229 2228
da6f8763 2229 }
906ebc4b 2230 if (($this->_inGroup) and !empty($this->_groupElementTemplate)) {
2231 $this->_groupElementTemplate = $html;
41b6d001 2232 }
906ebc4b 2233 elseif (!isset($this->_templates[$element->getName()])) {
2234 $this->_templates[$element->getName()] = $html;
172dd12c 2235 }
2236
da6f8763 2237 parent::renderElement($element, $required, $error);
2238 }
19194f82 2239
ba21c9d4 2240 /**
2241 * @param object $form Passed by reference
2242 */
bb40325e 2243 function finishForm(&$form){
4f51f48f 2244 if ($form->isFrozen()){
2245 $this->_hiddenHtml = '';
2246 }
bb40325e 2247 parent::finishForm($form);
4f51f48f 2248 if ((!$form->isFrozen()) && ('' != ($script = $form->getLockOptionEndScript()))) {
2249 // add a lockoptions script
bb40325e 2250 $this->_html = $this->_html . "\n" . $script;
2251 }
2252 }
19194f82 2253 /**
2254 * Called when visiting a header element
2255 *
ba21c9d4 2256 * @param object $header An HTML_QuickForm_header element being visited
19194f82 2257 * @access public
2258 * @return void
2259 */
c28bf5c9 2260 function renderHeader(&$header) {
2261 global $PAGE;
9262d2d3 2262 static $advformcount;
2263
2264 // This ensures that if 2(+) advanced buttons are used
2265 // that all show/hide buttons appear in the correct place
2266 // Because of now using $PAGE->requires->js_function_call
2267 if ($advformcount==null) {
2268 $advformcount = 1;
2269 }
2270
19194f82 2271 $name = $header->getName();
2272
2273 $id = empty($name) ? '' : ' id="' . $name . '"';
78354cec 2274 $id = preg_replace(array('/\]/', '/\[/'), array('', '_'), $id);
19194f82 2275 if (is_null($header->_text)) {
2276 $header_html = '';
2277 } elseif (!empty($name) && isset($this->_templates[$name])) {
2278 $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
2279 } else {
2280 $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
2281 }
2282
2283 if (isset($this->_advancedElements[$name])){
2284 $header_html =str_replace('{advancedimg}', $this->_advancedHTML, $header_html);
2285 } else {
2286 $header_html =str_replace('{advancedimg}', '', $header_html);
2287 }
2288 $elementName='mform_showadvanced';
2289 if ($this->_showAdvanced==0){
2290 $buttonlabel = get_string('showadvanced', 'form');
2291 } else {
2292 $buttonlabel = get_string('hideadvanced', 'form');
2293 }
2294
2295 if (isset($this->_advancedElements[$name])){
f44b10ed 2296 $PAGE->requires->yui2_lib('event');
7c9b1d31 2297 // this is tricky - the first submit button on form is "clicked" if user presses enter
2298 // we do not want to "submit" using advanced button if javascript active
9262d2d3 2299 $button_nojs = '<input name="'.$elementName.'" id="'.$elementName.(string)$advformcount.'" class="showadvancedbtn" value="'.$buttonlabel.'" type="submit" />';
cd350b53 2300
2301 $buttonlabel = addslashes_js($buttonlabel);
fc4f5796 2302 $PAGE->requires->string_for_js('showadvanced', 'form');
2303 $PAGE->requires->string_for_js('hideadvanced', 'form');
2304 $PAGE->requires->js_function_call('showAdvancedInit', Array($elementName.(string)$advformcount, $elementName, $buttonlabel));
117bd748 2305
9262d2d3 2306 $advformcount++;
2307 $header_html = str_replace('{button}', $button_nojs, $header_html);
19194f82 2308 } else {
7c9b1d31 2309 $header_html = str_replace('{button}', '', $header_html);
19194f82 2310 }
2311
2312 if ($this->_fieldsetsOpen > 0) {
2313 $this->_html .= $this->_closeFieldsetTemplate;
2314 $this->_fieldsetsOpen--;
2315 }
2316
2317 $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate);
2318 if ($this->_showAdvanced){
2319 $advclass = ' class="advanced"';
2320 } else {
2321 $advclass = ' class="advanced hide"';
2322 }
2323 if (isset($this->_advancedElements[$name])){
2324 $openFieldsetTemplate = str_replace('{advancedclass}', $advclass, $openFieldsetTemplate);
2325 } else {
2326 $openFieldsetTemplate = str_replace('{advancedclass}', '', $openFieldsetTemplate);
2327 }
2328 $this->_html .= $openFieldsetTemplate . $header_html;
2329 $this->_fieldsetsOpen++;
2330 } // end func renderHeader
2331
2332 function getStopFieldsetElements(){
2333 return $this->_stopFieldsetElements;
2334 }
da6f8763 2335}
2336
ba21c9d4 2337/**
2338 * @global object $GLOBALS['_HTML_QuickForm_default_renderer']
2339 * @name $_HTML_QuickForm_default_renderer
2340 */
66491cf1 2341$GLOBALS['_HTML_QuickForm_default_renderer'] = new MoodleQuickForm_Renderer();
da6f8763 2342
ba21c9d4 2343/** Please keep this list in alphabetical order. */
c583482c 2344MoodleQuickForm::registerElementType('advcheckbox', "$CFG->libdir/form/advcheckbox.php", 'MoodleQuickForm_advcheckbox');
2345MoodleQuickForm::registerElementType('button', "$CFG->libdir/form/button.php", 'MoodleQuickForm_button');
2346MoodleQuickForm::registerElementType('cancel', "$CFG->libdir/form/cancel.php", 'MoodleQuickForm_cancel');
09179b78 2347MoodleQuickForm::registerElementType('searchableselector', "$CFG->libdir/form/searchableselector.php", 'MoodleQuickForm_searchableselector');
7f40a229 2348MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox');
c583482c 2349MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector');
2350MoodleQuickForm::registerElementType('date_time_selector', "$CFG->libdir/form/datetimeselector.php", 'MoodleQuickForm_date_time_selector');
32fa2272 2351MoodleQuickForm::registerElementType('duration', "$CFG->libdir/form/duration.php", 'MoodleQuickForm_duration');
c583482c 2352MoodleQuickForm::registerElementType('editor', "$CFG->libdir/form/editor.php", 'MoodleQuickForm_editor');
7f40a229 2353MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'MoodleQuickForm_file');
241431cd 2354MoodleQuickForm::registerElementType('filemanager', "$CFG->libdir/form/filemanager.php", 'MoodleQuickForm_filemanager');
c5704ec6 2355MoodleQuickForm::registerElementType('filepicker', "$CFG->libdir/form/filepicker.php", 'MoodleQuickForm_filepicker');
c583482c 2356MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format');
7f40a229 2357MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group');
c583482c 2358MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", 'MoodleQuickForm_header');
2359MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden');
2360MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor');
2361MoodleQuickForm::registerElementType('modgrade', "$CFG->libdir/form/modgrade.php", 'MoodleQuickForm_modgrade');
2362MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible');
7f40a229 2363MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password');
4f51f48f 2364MoodleQuickForm::registerElementType('passwordunmask', "$CFG->libdir/form/passwordunmask.php", 'MoodleQuickForm_passwordunmask');
c583482c 2365MoodleQuickForm::registerElementType('questioncategory', "$CFG->libdir/form/questioncategory.php", 'MoodleQuickForm_questioncategory');
7f40a229 2366MoodleQuickForm::registerElementType('radio', "$CFG->libdir/form/radio.php", 'MoodleQuickForm_radio');
c583482c 2367MoodleQuickForm::registerElementType('recaptcha', "$CFG->libdir/form/recaptcha.php", 'MoodleQuickForm_recaptcha');
7f40a229 2368MoodleQuickForm::registerElementType('select', "$CFG->libdir/form/select.php", 'MoodleQuickForm_select');
4f51f48f 2369MoodleQuickForm::registerElementType('selectgroups', "$CFG->libdir/form/selectgroups.php", 'MoodleQuickForm_selectgroups');
c583482c 2370MoodleQuickForm::registerElementType('selectwithlink', "$CFG->libdir/form/selectwithlink.php", 'MoodleQuickForm_selectwithlink');
2371MoodleQuickForm::registerElementType('selectyesno', "$CFG->libdir/form/selectyesno.php", 'MoodleQuickForm_selectyesno');
2372MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static');
2373MoodleQuickForm::registerElementType('submit', "$CFG->libdir/form/submit.php", 'MoodleQuickForm_submit');
6073a598 2374MoodleQuickForm::registerElementType('submitlink', "$CFG->libdir/form/submitlink.php", 'MoodleQuickForm_submitlink');
9f1c9dfc 2375MoodleQuickForm::registerElementType('tags', "$CFG->libdir/form/tags.php", 'MoodleQuickForm_tags');
7f40a229 2376MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text');
2377MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea');
1a03384f 2378MoodleQuickForm::registerElementType('url', "$CFG->libdir/form/url.php", 'MoodleQuickForm_url');
1ae1941e 2379MoodleQuickForm::registerElementType('warning', "$CFG->libdir/form/warning.php", 'MoodleQuickForm_warning');