MDL-31532 restore: avoid null course->hiddensections problems. Credit goes to Petri...
[moodle.git] / lib / formslib.php
blob0f06fcff0b86200256672163153555ba5fddd0a9
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
17 /**
18 * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms.
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.
25 * See examples of use of this library in course/edit.php and course/edit_form.php
27 * A few notes :
28 * form definition is used for both printing of form and processing and should be the same
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.
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
40 defined('MOODLE_INTERNAL') || die();
42 /** setup.php includes our hacked pear libs first */
43 require_once 'HTML/QuickForm.php';
44 require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
45 require_once 'HTML/QuickForm/Renderer/Tableless.php';
46 require_once 'HTML/QuickForm/Rule.php';
48 require_once $CFG->libdir.'/filelib.php';
50 define('EDITOR_UNLIMITED_FILES', -1);
52 /**
53 * Callback called when PEAR throws an error
55 * @param PEAR_Error $error
57 function pear_handle_error($error){
58 echo '<strong>'.$error->GetMessage().'</strong> '.$error->getUserInfo();
59 echo '<br /> <strong>Backtrace </strong>:';
60 print_object($error->backtrace);
63 if (!empty($CFG->debug) and $CFG->debug >= DEBUG_ALL){
64 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_handle_error');
67 /**
69 * @staticvar bool $done
70 * @global moodle_page $PAGE
72 function form_init_date_js() {
73 global $PAGE;
74 static $done = false;
75 if (!$done) {
76 $module = 'moodle-form-dateselector';
77 $function = 'M.form.dateselector.init_date_selectors';
78 $config = array(array('firstdayofweek'=>get_string('firstdayofweek', 'langconfig')));
79 $PAGE->requires->yui_module($module, $function, $config);
80 $done = true;
84 /**
85 * Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly
86 * use this class you should write a class definition which extends this class or a more specific
87 * subclass such a moodleform_mod for each form you want to display and/or process with formslib.
89 * You will write your own definition() method which performs the form set up.
91 * @package moodlecore
92 * @copyright Jamie Pratt <me@jamiep.org>
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
95 abstract class moodleform {
96 /** @var string */
97 protected $_formname; // form name
98 /**
99 * quickform object definition
101 * @var MoodleQuickForm MoodleQuickForm
103 protected $_form;
105 * globals workaround
107 * @var array
109 protected $_customdata;
111 * definition_after_data executed flag
112 * @var object definition_finalized
114 protected $_definition_finalized = false;
117 * The constructor function calls the abstract function definition() and it will then
118 * process and clean and attempt to validate incoming data.
120 * It will call your custom validate method to validate data and will also check any rules
121 * you have specified in definition using addRule
123 * The name of the form (id attribute of the form) is automatically generated depending on
124 * the name you gave the class extending moodleform. You should call your class something
125 * like
127 * @param mixed $action the action attribute for the form. If empty defaults to auto detect the
128 * current url. If a moodle_url object then outputs params as hidden variables.
129 * @param array $customdata if your form defintion method needs access to data such as $course
130 * $cm, etc. to construct the form definition then pass it in this array. You can
131 * use globals for somethings.
132 * @param string $method if you set this to anything other than 'post' then _GET and _POST will
133 * be merged and used as incoming data to the form.
134 * @param string $target target frame for form submission. You will rarely use this. Don't use
135 * it if you don't need to as the target attribute is deprecated in xhtml
136 * strict.
137 * @param mixed $attributes you can pass a string of html attributes here or an array.
138 * @param bool $editable
139 * @return object moodleform
141 function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
142 global $CFG;
143 if (empty($CFG->xmlstrictheaders)) {
144 // no standard mform in moodle should allow autocomplete with the exception of user signup
145 // this is valid attribute in html5, sorry, we have to ignore validation errors in legacy xhtml 1.0
146 if (empty($attributes)) {
147 $attributes = array('autocomplete'=>'off');
148 } else if (is_array($attributes)) {
149 $attributes['autocomplete'] = 'off';
150 } else {
151 if (strpos($attributes, 'autocomplete') === false) {
152 $attributes .= ' autocomplete="off" ';
157 if (empty($action)){
158 $action = strip_querystring(qualified_me());
160 // Assign custom data first, so that get_form_identifier can use it.
161 $this->_customdata = $customdata;
162 $this->_formname = $this->get_form_identifier();
164 $this->_form = new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes);
165 if (!$editable){
166 $this->_form->hardFreeze();
169 $this->definition();
171 $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection
172 $this->_form->setType('sesskey', PARAM_RAW);
173 $this->_form->setDefault('sesskey', sesskey());
174 $this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker
175 $this->_form->setType('_qf__'.$this->_formname, PARAM_RAW);
176 $this->_form->setDefault('_qf__'.$this->_formname, 1);
177 $this->_form->_setDefaultRuleMessages();
179 // we have to know all input types before processing submission ;-)
180 $this->_process_submission($method);
184 * It should returns unique identifier for the form.
185 * Currently it will return class name, but in case two same forms have to be
186 * rendered on same page then override function to get unique form identifier.
187 * e.g This is used on multiple self enrollments page.
189 * @return string form identifier.
191 protected function get_form_identifier() {
192 return get_class($this);
196 * To autofocus on first form element or first element with error.
198 * @param string $name if this is set then the focus is forced to a field with this name
200 * @return string javascript to select form element with first error or
201 * first element if no errors. Use this as a parameter
202 * when calling print_header
204 function focus($name=NULL) {
205 $form =& $this->_form;
206 $elkeys = array_keys($form->_elementIndex);
207 $error = false;
208 if (isset($form->_errors) && 0 != count($form->_errors)){
209 $errorkeys = array_keys($form->_errors);
210 $elkeys = array_intersect($elkeys, $errorkeys);
211 $error = true;
214 if ($error or empty($name)) {
215 $names = array();
216 while (empty($names) and !empty($elkeys)) {
217 $el = array_shift($elkeys);
218 $names = $form->_getElNamesRecursive($el);
220 if (!empty($names)) {
221 $name = array_shift($names);
225 $focus = '';
226 if (!empty($name)) {
227 $focus = 'forms[\''.$form->getAttribute('id').'\'].elements[\''.$name.'\']';
230 return $focus;
234 * Internal method. Alters submitted data to be suitable for quickforms processing.
235 * Must be called when the form is fully set up.
237 * @param string $method
239 function _process_submission($method) {
240 $submission = array();
241 if ($method == 'post') {
242 if (!empty($_POST)) {
243 $submission = $_POST;
245 } else {
246 $submission = array_merge_recursive($_GET, $_POST); // emulate handling of parameters in xxxx_param()
249 // following trick is needed to enable proper sesskey checks when using GET forms
250 // the _qf__.$this->_formname serves as a marker that form was actually submitted
251 if (array_key_exists('_qf__'.$this->_formname, $submission) and $submission['_qf__'.$this->_formname] == 1) {
252 if (!confirm_sesskey()) {
253 print_error('invalidsesskey');
255 $files = $_FILES;
256 } else {
257 $submission = array();
258 $files = array();
261 $this->_form->updateSubmission($submission, $files);
265 * Internal method. Validates all old-style deprecated uploaded files.
266 * The new way is to upload files via repository api.
268 * @global object
269 * @global object
270 * @param array $files
271 * @return bool|array Success or an array of errors
273 function _validate_files(&$files) {
274 global $CFG, $COURSE;
276 $files = array();
278 if (empty($_FILES)) {
279 // we do not need to do any checks because no files were submitted
280 // note: server side rules do not work for files - use custom verification in validate() instead
281 return true;
284 $errors = array();
285 $filenames = array();
287 // now check that we really want each file
288 foreach ($_FILES as $elname=>$file) {
289 $required = $this->_form->isElementRequired($elname);
291 if ($file['error'] == 4 and $file['size'] == 0) {
292 if ($required) {
293 $errors[$elname] = get_string('required');
295 unset($_FILES[$elname]);
296 continue;
299 if (!empty($file['error'])) {
300 $errors[$elname] = file_get_upload_error($file['error']);
301 unset($_FILES[$elname]);
302 continue;
305 if (!is_uploaded_file($file['tmp_name'])) {
306 // TODO: improve error message
307 $errors[$elname] = get_string('error');
308 unset($_FILES[$elname]);
309 continue;
312 if (!$this->_form->elementExists($elname) or !$this->_form->getElementType($elname)=='file') {
313 // hmm, this file was not requested
314 unset($_FILES[$elname]);
315 continue;
319 // TODO: rethink the file scanning MDL-19380
320 if ($CFG->runclamonupload) {
321 if (!clam_scan_moodle_file($_FILES[$elname], $COURSE)) {
322 $errors[$elname] = $_FILES[$elname]['uploadlog'];
323 unset($_FILES[$elname]);
324 continue;
328 $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
329 if ($filename === '') {
330 // TODO: improve error message - wrong chars
331 $errors[$elname] = get_string('error');
332 unset($_FILES[$elname]);
333 continue;
335 if (in_array($filename, $filenames)) {
336 // TODO: improve error message - duplicate name
337 $errors[$elname] = get_string('error');
338 unset($_FILES[$elname]);
339 continue;
341 $filenames[] = $filename;
342 $_FILES[$elname]['name'] = $filename;
344 $files[$elname] = $_FILES[$elname]['tmp_name'];
347 // return errors if found
348 if (count($errors) == 0){
349 return true;
351 } else {
352 $files = array();
353 return $errors;
358 * Internal method. Validates filepicker and filemanager files if they are
359 * set as required fields. Also, sets the error message if encountered one.
361 * @return bool/array with errors
363 protected function validate_draft_files() {
364 global $USER;
365 $mform =& $this->_form;
367 $errors = array();
368 //Go through all the required elements and make sure you hit filepicker or
369 //filemanager element.
370 foreach ($mform->_rules as $elementname => $rules) {
371 $elementtype = $mform->getElementType($elementname);
372 //If element is of type filepicker then do validation
373 if (($elementtype == 'filepicker') || ($elementtype == 'filemanager')){
374 //Check if rule defined is required rule
375 foreach ($rules as $rule) {
376 if ($rule['type'] == 'required') {
377 $draftid = (int)$mform->getSubmitValue($elementname);
378 $fs = get_file_storage();
379 $context = get_context_instance(CONTEXT_USER, $USER->id);
380 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
381 $errors[$elementname] = $rule['message'];
387 if (empty($errors)) {
388 return true;
389 } else {
390 return $errors;
395 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
396 * form definition (new entry form); this function is used to load in data where values
397 * already exist and data is being edited (edit entry form).
399 * note: $slashed param removed
401 * @param mixed $default_values object or array of default values
403 function set_data($default_values) {
404 if (is_object($default_values)) {
405 $default_values = (array)$default_values;
407 $this->_form->setDefaults($default_values);
411 * @deprecated
413 function set_upload_manager($um=false) {
414 debugging('Old file uploads can not be used any more, please use new filepicker element');
418 * Check that form was submitted. Does not check validity of submitted data.
420 * @return bool true if form properly submitted
422 function is_submitted() {
423 return $this->_form->isSubmitted();
427 * @staticvar bool $nosubmit
429 function no_submit_button_pressed(){
430 static $nosubmit = null; // one check is enough
431 if (!is_null($nosubmit)){
432 return $nosubmit;
434 $mform =& $this->_form;
435 $nosubmit = false;
436 if (!$this->is_submitted()){
437 return false;
439 foreach ($mform->_noSubmitButtons as $nosubmitbutton){
440 if (optional_param($nosubmitbutton, 0, PARAM_RAW)){
441 $nosubmit = true;
442 break;
445 return $nosubmit;
450 * Check that form data is valid.
451 * You should almost always use this, rather than {@see validate_defined_fields}
453 * @staticvar bool $validated
454 * @return bool true if form data valid
456 function is_validated() {
457 //finalize the form definition before any processing
458 if (!$this->_definition_finalized) {
459 $this->_definition_finalized = true;
460 $this->definition_after_data();
463 return $this->validate_defined_fields();
467 * Validate the form.
469 * You almost always want to call {@see is_validated} instead of this
470 * because it calls {@see definition_after_data} first, before validating the form,
471 * which is what you want in 99% of cases.
473 * This is provided as a separate function for those special cases where
474 * you want the form validated before definition_after_data is called
475 * for example, to selectively add new elements depending on a no_submit_button press,
476 * but only when the form is valid when the no_submit_button is pressed,
478 * @param boolean $validateonnosubmit optional, defaults to false. The default behaviour
479 * is NOT to validate the form when a no submit button has been pressed.
480 * pass true here to override this behaviour
482 * @return bool true if form data valid
484 function validate_defined_fields($validateonnosubmit=false) {
485 static $validated = null; // one validation is enough
486 $mform =& $this->_form;
487 if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
488 return false;
489 } elseif ($validated === null) {
490 $internal_val = $mform->validate();
492 $files = array();
493 $file_val = $this->_validate_files($files);
494 //check draft files for validation and flag them if required files
495 //are not in draft area.
496 $draftfilevalue = $this->validate_draft_files();
498 if ($file_val !== true && $draftfilevalue !== true) {
499 $file_val = array_merge($file_val, $draftfilevalue);
500 } else if ($draftfilevalue !== true) {
501 $file_val = $draftfilevalue;
502 } //default is file_val, so no need to assign.
504 if ($file_val !== true) {
505 if (!empty($file_val)) {
506 foreach ($file_val as $element=>$msg) {
507 $mform->setElementError($element, $msg);
510 $file_val = false;
513 $data = $mform->exportValues();
514 $moodle_val = $this->validation($data, $files);
515 if ((is_array($moodle_val) && count($moodle_val)!==0)) {
516 // non-empty array means errors
517 foreach ($moodle_val as $element=>$msg) {
518 $mform->setElementError($element, $msg);
520 $moodle_val = false;
522 } else {
523 // anything else means validation ok
524 $moodle_val = true;
527 $validated = ($internal_val and $moodle_val and $file_val);
529 return $validated;
533 * Return true if a cancel button has been pressed resulting in the form being submitted.
535 * @return boolean true if a cancel button has been pressed
537 function is_cancelled(){
538 $mform =& $this->_form;
539 if ($mform->isSubmitted()){
540 foreach ($mform->_cancelButtons as $cancelbutton){
541 if (optional_param($cancelbutton, 0, PARAM_RAW)){
542 return true;
546 return false;
550 * Return submitted data if properly submitted or returns NULL if validation fails or
551 * if there is no submitted data.
553 * note: $slashed param removed
555 * @return object submitted data; NULL if not valid or not submitted or cancelled
557 function get_data() {
558 $mform =& $this->_form;
560 if (!$this->is_cancelled() and $this->is_submitted() and $this->is_validated()) {
561 $data = $mform->exportValues();
562 unset($data['sesskey']); // we do not need to return sesskey
563 unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too
564 if (empty($data)) {
565 return NULL;
566 } else {
567 return (object)$data;
569 } else {
570 return NULL;
575 * Return submitted data without validation or NULL if there is no submitted data.
576 * note: $slashed param removed
578 * @return object submitted data; NULL if not submitted
580 function get_submitted_data() {
581 $mform =& $this->_form;
583 if ($this->is_submitted()) {
584 $data = $mform->exportValues();
585 unset($data['sesskey']); // we do not need to return sesskey
586 unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too
587 if (empty($data)) {
588 return NULL;
589 } else {
590 return (object)$data;
592 } else {
593 return NULL;
598 * Save verified uploaded files into directory. Upload process can be customised from definition()
599 * NOTE: please use save_stored_file() or save_file()
601 * @return bool Always false
603 function save_files($destination) {
604 debugging('Not used anymore, please fix code! Use save_stored_file() or save_file() instead');
605 return false;
609 * Returns name of uploaded file.
611 * @global object
612 * @param string $elname, first element if null
613 * @return mixed false in case of failure, string if ok
615 function get_new_filename($elname=null) {
616 global $USER;
618 if (!$this->is_submitted() or !$this->is_validated()) {
619 return false;
622 if (is_null($elname)) {
623 if (empty($_FILES)) {
624 return false;
626 reset($_FILES);
627 $elname = key($_FILES);
630 if (empty($elname)) {
631 return false;
634 $element = $this->_form->getElement($elname);
636 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
637 $values = $this->_form->exportValues($elname);
638 if (empty($values[$elname])) {
639 return false;
641 $draftid = $values[$elname];
642 $fs = get_file_storage();
643 $context = get_context_instance(CONTEXT_USER, $USER->id);
644 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
645 return false;
647 $file = reset($files);
648 return $file->get_filename();
651 if (!isset($_FILES[$elname])) {
652 return false;
655 return $_FILES[$elname]['name'];
659 * Save file to standard filesystem
661 * @global object
662 * @param string $elname name of element
663 * @param string $pathname full path name of file
664 * @param bool $override override file if exists
665 * @return bool success
667 function save_file($elname, $pathname, $override=false) {
668 global $USER;
670 if (!$this->is_submitted() or !$this->is_validated()) {
671 return false;
673 if (file_exists($pathname)) {
674 if ($override) {
675 if (!@unlink($pathname)) {
676 return false;
678 } else {
679 return false;
683 $element = $this->_form->getElement($elname);
685 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
686 $values = $this->_form->exportValues($elname);
687 if (empty($values[$elname])) {
688 return false;
690 $draftid = $values[$elname];
691 $fs = get_file_storage();
692 $context = get_context_instance(CONTEXT_USER, $USER->id);
693 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
694 return false;
696 $file = reset($files);
698 return $file->copy_content_to($pathname);
700 } else if (isset($_FILES[$elname])) {
701 return copy($_FILES[$elname]['tmp_name'], $pathname);
704 return false;
708 * Returns a temporary file, do not forget to delete after not needed any more.
710 * @param string $elname
711 * @return string or false
713 function save_temp_file($elname) {
714 if (!$this->get_new_filename($elname)) {
715 return false;
717 if (!$dir = make_temp_directory('forms')) {
718 return false;
720 if (!$tempfile = tempnam($dir, 'tempup_')) {
721 return false;
723 if (!$this->save_file($elname, $tempfile, true)) {
724 // something went wrong
725 @unlink($tempfile);
726 return false;
729 return $tempfile;
733 * Get draft files of a form element
734 * This is a protected method which will be used only inside moodleforms
736 * @global object $USER
737 * @param string $elname name of element
738 * @return array
740 protected function get_draft_files($elname) {
741 global $USER;
743 if (!$this->is_submitted()) {
744 return false;
747 $element = $this->_form->getElement($elname);
749 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
750 $values = $this->_form->exportValues($elname);
751 if (empty($values[$elname])) {
752 return false;
754 $draftid = $values[$elname];
755 $fs = get_file_storage();
756 $context = get_context_instance(CONTEXT_USER, $USER->id);
757 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
758 return null;
760 return $files;
762 return null;
766 * Save file to local filesystem pool
768 * @global object
769 * @param string $elname name of element
770 * @param int $newcontextid
771 * @param string $newfilearea
772 * @param string $newfilepath
773 * @param string $newfilename - use specified filename, if not specified name of uploaded file used
774 * @param bool $overwrite - overwrite file if exists
775 * @param int $newuserid - new userid if required
776 * @return mixed stored_file object or false if error; may throw exception if duplicate found
778 function save_stored_file($elname, $newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath='/',
779 $newfilename=null, $overwrite=false, $newuserid=null) {
780 global $USER;
782 if (!$this->is_submitted() or !$this->is_validated()) {
783 return false;
786 if (empty($newuserid)) {
787 $newuserid = $USER->id;
790 $element = $this->_form->getElement($elname);
791 $fs = get_file_storage();
793 if ($element instanceof MoodleQuickForm_filepicker) {
794 $values = $this->_form->exportValues($elname);
795 if (empty($values[$elname])) {
796 return false;
798 $draftid = $values[$elname];
799 $context = get_context_instance(CONTEXT_USER, $USER->id);
800 if (!$files = $fs->get_area_files($context->id, 'user' ,'draft', $draftid, 'id DESC', false)) {
801 return false;
803 $file = reset($files);
804 if (is_null($newfilename)) {
805 $newfilename = $file->get_filename();
808 if ($overwrite) {
809 if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
810 if (!$oldfile->delete()) {
811 return false;
816 $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
817 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
818 return $fs->create_file_from_storedfile($file_record, $file);
820 } else if (isset($_FILES[$elname])) {
821 $filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;
823 if ($overwrite) {
824 if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
825 if (!$oldfile->delete()) {
826 return false;
831 $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
832 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
833 return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
836 return false;
840 * Get content of uploaded file.
842 * @global object
843 * @param $element name of file upload element
844 * @return mixed false in case of failure, string if ok
846 function get_file_content($elname) {
847 global $USER;
849 if (!$this->is_submitted() or !$this->is_validated()) {
850 return false;
853 $element = $this->_form->getElement($elname);
855 if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
856 $values = $this->_form->exportValues($elname);
857 if (empty($values[$elname])) {
858 return false;
860 $draftid = $values[$elname];
861 $fs = get_file_storage();
862 $context = get_context_instance(CONTEXT_USER, $USER->id);
863 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
864 return false;
866 $file = reset($files);
868 return $file->get_content();
870 } else if (isset($_FILES[$elname])) {
871 return file_get_contents($_FILES[$elname]['tmp_name']);
874 return false;
878 * Print html form.
880 function display() {
881 //finalize the form definition if not yet done
882 if (!$this->_definition_finalized) {
883 $this->_definition_finalized = true;
884 $this->definition_after_data();
886 $this->_form->display();
890 * Abstract method - always override!
892 protected abstract function definition();
895 * Dummy stub method - override if you need to setup the form depending on current
896 * values. This method is called after definition(), data submission and set_data().
897 * All form setup that is dependent on form values should go in here.
899 function definition_after_data(){
903 * Dummy stub method - override if you needed to perform some extra validation.
904 * If there are errors return array of errors ("fieldname"=>"error message"),
905 * otherwise true if ok.
907 * Server side rules do not work for uploaded files, implement serverside rules here if needed.
909 * @param array $data array of ("fieldname"=>value) of submitted data
910 * @param array $files array of uploaded files "element_name"=>tmp_file_path
911 * @return array of "element_name"=>"error_description" if there are errors,
912 * or an empty array if everything is OK (true allowed for backwards compatibility too).
914 function validation($data, $files) {
915 return array();
919 * Helper used by {@link repeat_elements()}.
920 * @param int $i the index of this element.
921 * @param HTML_QuickForm_element $elementclone
922 * @param array $namecloned array of names
924 function repeat_elements_fix_clone($i, $elementclone, &$namecloned) {
925 $name = $elementclone->getName();
926 $namecloned[] = $name;
928 if (!empty($name)) {
929 $elementclone->setName($name."[$i]");
932 if (is_a($elementclone, 'HTML_QuickForm_header')) {
933 $value = $elementclone->_text;
934 $elementclone->setValue(str_replace('{no}', ($i+1), $value));
936 } else {
937 $value=$elementclone->getLabel();
938 $elementclone->setLabel(str_replace('{no}', ($i+1), $value));
943 * Method to add a repeating group of elements to a form.
945 * @param array $elementobjs Array of elements or groups of elements that are to be repeated
946 * @param integer $repeats no of times to repeat elements initially
947 * @param array $options Array of options to apply to elements. Array keys are element names.
948 * This is an array of arrays. The second sets of keys are the option types
949 * for the elements :
950 * 'default' - default value is value
951 * 'type' - PARAM_* constant is value
952 * 'helpbutton' - helpbutton params array is value
953 * 'disabledif' - last three moodleform::disabledIf()
954 * params are value as an array
955 * @param string $repeathiddenname name for hidden element storing no of repeats in this form
956 * @param string $addfieldsname name for button to add more fields
957 * @param int $addfieldsno how many fields to add at a time
958 * @param string $addstring name of button, {no} is replaced by no of blanks that will be added.
959 * @param boolean $addbuttoninside if true, don't call closeHeaderBefore($addfieldsname). Default false.
960 * @return int no of repeats of element in this page
962 function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
963 $addfieldsname, $addfieldsno=5, $addstring=null, $addbuttoninside=false){
964 if ($addstring===null){
965 $addstring = get_string('addfields', 'form', $addfieldsno);
966 } else {
967 $addstring = str_ireplace('{no}', $addfieldsno, $addstring);
969 $repeats = optional_param($repeathiddenname, $repeats, PARAM_INT);
970 $addfields = optional_param($addfieldsname, '', PARAM_TEXT);
971 if (!empty($addfields)){
972 $repeats += $addfieldsno;
974 $mform =& $this->_form;
975 $mform->registerNoSubmitButton($addfieldsname);
976 $mform->addElement('hidden', $repeathiddenname, $repeats);
977 $mform->setType($repeathiddenname, PARAM_INT);
978 //value not to be overridden by submitted value
979 $mform->setConstants(array($repeathiddenname=>$repeats));
980 $namecloned = array();
981 for ($i = 0; $i < $repeats; $i++) {
982 foreach ($elementobjs as $elementobj){
983 $elementclone = fullclone($elementobj);
984 $this->repeat_elements_fix_clone($i, $elementclone, $namecloned);
986 if ($elementclone instanceof HTML_QuickForm_group && !$elementclone->_appendName) {
987 foreach ($elementclone->getElements() as $el) {
988 $this->repeat_elements_fix_clone($i, $el, $namecloned);
990 $elementclone->setLabel(str_replace('{no}', $i + 1, $elementclone->getLabel()));
993 $mform->addElement($elementclone);
996 for ($i=0; $i<$repeats; $i++) {
997 foreach ($options as $elementname => $elementoptions){
998 $pos=strpos($elementname, '[');
999 if ($pos!==FALSE){
1000 $realelementname = substr($elementname, 0, $pos+1)."[$i]";
1001 $realelementname .= substr($elementname, $pos+1);
1002 }else {
1003 $realelementname = $elementname."[$i]";
1005 foreach ($elementoptions as $option => $params){
1007 switch ($option){
1008 case 'default' :
1009 $mform->setDefault($realelementname, $params);
1010 break;
1011 case 'helpbutton' :
1012 $params = array_merge(array($realelementname), $params);
1013 call_user_func_array(array(&$mform, 'addHelpButton'), $params);
1014 break;
1015 case 'disabledif' :
1016 foreach ($namecloned as $num => $name){
1017 if ($params[0] == $name){
1018 $params[0] = $params[0]."[$i]";
1019 break;
1022 $params = array_merge(array($realelementname), $params);
1023 call_user_func_array(array(&$mform, 'disabledIf'), $params);
1024 break;
1025 case 'rule' :
1026 if (is_string($params)){
1027 $params = array(null, $params, null, 'client');
1029 $params = array_merge(array($realelementname), $params);
1030 call_user_func_array(array(&$mform, 'addRule'), $params);
1031 break;
1032 case 'type' :
1033 //Type should be set only once
1034 if (!isset($mform->_types[$elementname])) {
1035 $mform->setType($elementname, $params);
1037 break;
1042 $mform->addElement('submit', $addfieldsname, $addstring);
1044 if (!$addbuttoninside) {
1045 $mform->closeHeaderBefore($addfieldsname);
1048 return $repeats;
1052 * Adds a link/button that controls the checked state of a group of checkboxes.
1054 * @global object
1055 * @param int $groupid The id of the group of advcheckboxes this element controls
1056 * @param string $text The text of the link. Defaults to selectallornone ("select all/none")
1057 * @param array $attributes associative array of HTML attributes
1058 * @param int $originalValue The original general state of the checkboxes before the user first clicks this element
1060 function add_checkbox_controller($groupid, $text = null, $attributes = null, $originalValue = 0) {
1061 global $CFG;
1063 // Set the default text if none was specified
1064 if (empty($text)) {
1065 $text = get_string('selectallornone', 'form');
1068 $mform = $this->_form;
1069 $select_value = optional_param('checkbox_controller'. $groupid, null, PARAM_INT);
1071 if ($select_value == 0 || is_null($select_value)) {
1072 $new_select_value = 1;
1073 } else {
1074 $new_select_value = 0;
1077 $mform->addElement('hidden', "checkbox_controller$groupid");
1078 $mform->setType("checkbox_controller$groupid", PARAM_INT);
1079 $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
1081 $checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid;
1082 $mform->registerNoSubmitButton($checkbox_controller_name);
1084 // Prepare Javascript for submit element
1085 $js = "\n//<![CDATA[\n";
1086 if (!defined('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS')) {
1087 $js .= <<<EOS
1088 function html_quickform_toggle_checkboxes(group) {
1089 var checkboxes = document.getElementsByClassName('checkboxgroup' + group);
1090 var newvalue = false;
1091 var global = eval('html_quickform_checkboxgroup' + group + ';');
1092 if (global == 1) {
1093 eval('html_quickform_checkboxgroup' + group + ' = 0;');
1094 newvalue = '';
1095 } else {
1096 eval('html_quickform_checkboxgroup' + group + ' = 1;');
1097 newvalue = 'checked';
1100 for (i = 0; i < checkboxes.length; i++) {
1101 checkboxes[i].checked = newvalue;
1104 EOS;
1105 define('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS', true);
1107 $js .= "\nvar html_quickform_checkboxgroup$groupid=$originalValue;\n";
1109 $js .= "//]]>\n";
1111 require_once("$CFG->libdir/form/submitlink.php");
1112 $submitlink = new MoodleQuickForm_submitlink($checkbox_controller_name, $attributes);
1113 $submitlink->_js = $js;
1114 $submitlink->_onclick = "html_quickform_toggle_checkboxes($groupid); return false;";
1115 $mform->addElement($submitlink);
1116 $mform->setDefault($checkbox_controller_name, $text);
1120 * Use this method to a cancel and submit button to the end of your form. Pass a param of false
1121 * if you don't want a cancel button in your form. If you have a cancel button make sure you
1122 * check for it being pressed using is_cancelled() and redirecting if it is true before trying to
1123 * get data with get_data().
1125 * @param boolean $cancel whether to show cancel button, default true
1126 * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
1128 function add_action_buttons($cancel = true, $submitlabel=null){
1129 if (is_null($submitlabel)){
1130 $submitlabel = get_string('savechanges');
1132 $mform =& $this->_form;
1133 if ($cancel){
1134 //when two elements we need a group
1135 $buttonarray=array();
1136 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
1137 $buttonarray[] = &$mform->createElement('cancel');
1138 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
1139 $mform->closeHeaderBefore('buttonar');
1140 } else {
1141 //no group needed
1142 $mform->addElement('submit', 'submitbutton', $submitlabel);
1143 $mform->closeHeaderBefore('submitbutton');
1148 * Adds an initialisation call for a standard JavaScript enhancement.
1150 * This function is designed to add an initialisation call for a JavaScript
1151 * enhancement that should exist within javascript-static M.form.init_{enhancementname}.
1153 * Current options:
1154 * - Selectboxes
1155 * - smartselect: Turns a nbsp indented select box into a custom drop down
1156 * control that supports multilevel and category selection.
1157 * $enhancement = 'smartselect';
1158 * $options = array('selectablecategories' => true|false)
1160 * @since 2.0
1161 * @param string|element $element
1162 * @param string $enhancement
1163 * @param array $options
1164 * @param array $strings
1166 function init_javascript_enhancement($element, $enhancement, array $options=array(), array $strings=null) {
1167 global $PAGE;
1168 if (is_string($element)) {
1169 $element = $this->_form->getElement($element);
1171 if (is_object($element)) {
1172 $element->_generateId();
1173 $elementid = $element->getAttribute('id');
1174 $PAGE->requires->js_init_call('M.form.init_'.$enhancement, array($elementid, $options));
1175 if (is_array($strings)) {
1176 foreach ($strings as $string) {
1177 if (is_array($string)) {
1178 call_user_method_array('string_for_js', $PAGE->requires, $string);
1179 } else {
1180 $PAGE->requires->string_for_js($string, 'moodle');
1188 * Returns a JS module definition for the mforms JS
1189 * @return array
1191 public static function get_js_module() {
1192 global $CFG;
1193 return array(
1194 'name' => 'mform',
1195 'fullpath' => '/lib/form/form.js',
1196 'requires' => array('base', 'node'),
1197 'strings' => array(
1198 array('showadvanced', 'form'),
1199 array('hideadvanced', 'form')
1206 * You never extend this class directly. The class methods of this class are available from
1207 * the private $this->_form property on moodleform and its children. You generally only
1208 * call methods on this class from within abstract methods that you override on moodleform such
1209 * as definition and definition_after_data
1211 * @package moodlecore
1212 * @copyright Jamie Pratt <me@jamiep.org>
1213 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1215 class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
1216 /** @var array */
1217 var $_types = array();
1218 var $_dependencies = array();
1220 * Array of buttons that if pressed do not result in the processing of the form.
1222 * @var array
1224 var $_noSubmitButtons=array();
1226 * Array of buttons that if pressed do not result in the processing of the form.
1228 * @var array
1230 var $_cancelButtons=array();
1233 * Array whose keys are element names. If the key exists this is a advanced element
1235 * @var array
1237 var $_advancedElements = array();
1240 * Whether to display advanced elements (on page load)
1242 * @var boolean
1244 var $_showAdvanced = null;
1247 * The form name is derived from the class name of the wrapper minus the trailing form
1248 * It is a name with words joined by underscores whereas the id attribute is words joined by
1249 * underscores.
1251 * @var unknown_type
1253 var $_formName = '';
1256 * String with the html for hidden params passed in as part of a moodle_url object for the action. Output in the form.
1258 * @var string
1260 var $_pageparams = '';
1263 * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
1265 * @global object
1266 * @staticvar int $formcounter
1267 * @param string $formName Form's name.
1268 * @param string $method (optional)Form's method defaults to 'POST'
1269 * @param mixed $action (optional)Form's action - string or moodle_url
1270 * @param string $target (optional)Form's target defaults to none
1271 * @param mixed $attributes (optional)Extra attributes for <form> tag
1272 * @access public
1274 function MoodleQuickForm($formName, $method, $action, $target='', $attributes=null){
1275 global $CFG, $OUTPUT;
1277 static $formcounter = 1;
1279 HTML_Common::HTML_Common($attributes);
1280 $target = empty($target) ? array() : array('target' => $target);
1281 $this->_formName = $formName;
1282 if (is_a($action, 'moodle_url')){
1283 $this->_pageparams = html_writer::input_hidden_params($action);
1284 $action = $action->out_omit_querystring();
1285 } else {
1286 $this->_pageparams = '';
1288 //no 'name' atttribute for form in xhtml strict :
1289 $attributes = array('action'=>$action, 'method'=>$method,
1290 'accept-charset'=>'utf-8', 'id'=>'mform'.$formcounter) + $target;
1291 $formcounter++;
1292 $this->updateAttributes($attributes);
1294 //this is custom stuff for Moodle :
1295 $oldclass= $this->getAttribute('class');
1296 if (!empty($oldclass)){
1297 $this->updateAttributes(array('class'=>$oldclass.' mform'));
1298 }else {
1299 $this->updateAttributes(array('class'=>'mform'));
1301 $this->_reqHTML = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
1302 $this->_advancedHTML = '<img class="adv" title="'.get_string('advancedelement', 'form').'" alt="'.get_string('advancedelement', 'form').'" src="'.$OUTPUT->pix_url('adv') .'" />';
1303 $this->setRequiredNote(get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />'));
1307 * Use this method to indicate an element in a form is an advanced field. If items in a form
1308 * are marked as advanced then 'Hide/Show Advanced' buttons will automatically be displayed in the
1309 * form so the user can decide whether to display advanced form controls.
1311 * If you set a header element to advanced then all elements it contains will also be set as advanced.
1313 * @param string $elementName group or element name (not the element name of something inside a group).
1314 * @param boolean $advanced default true sets the element to advanced. False removes advanced mark.
1316 function setAdvanced($elementName, $advanced=true){
1317 if ($advanced){
1318 $this->_advancedElements[$elementName]='';
1319 } elseif (isset($this->_advancedElements[$elementName])) {
1320 unset($this->_advancedElements[$elementName]);
1322 if ($advanced && $this->getElementType('mform_showadvanced_last')===false){
1323 $this->setShowAdvanced();
1324 $this->registerNoSubmitButton('mform_showadvanced');
1326 $this->addElement('hidden', 'mform_showadvanced_last');
1327 $this->setType('mform_showadvanced_last', PARAM_INT);
1331 * Set whether to show advanced elements in the form on first displaying form. Default is not to
1332 * display advanced elements in the form until 'Show Advanced' is pressed.
1334 * You can get the last state of the form and possibly save it for this user by using
1335 * value 'mform_showadvanced_last' in submitted data.
1337 * @param boolean $showadvancedNow
1339 function setShowAdvanced($showadvancedNow = null){
1340 if ($showadvancedNow === null){
1341 if ($this->_showAdvanced !== null){
1342 return;
1343 } else { //if setShowAdvanced is called without any preference
1344 //make the default to not show advanced elements.
1345 $showadvancedNow = get_user_preferences(
1346 moodle_strtolower($this->_formName.'_showadvanced', 0));
1349 //value of hidden element
1350 $hiddenLast = optional_param('mform_showadvanced_last', -1, PARAM_INT);
1351 //value of button
1352 $buttonPressed = optional_param('mform_showadvanced', 0, PARAM_RAW);
1353 //toggle if button pressed or else stay the same
1354 if ($hiddenLast == -1) {
1355 $next = $showadvancedNow;
1356 } elseif ($buttonPressed) { //toggle on button press
1357 $next = !$hiddenLast;
1358 } else {
1359 $next = $hiddenLast;
1361 $this->_showAdvanced = $next;
1362 if ($showadvancedNow != $next){
1363 set_user_preference($this->_formName.'_showadvanced', $next);
1365 $this->setConstants(array('mform_showadvanced_last'=>$next));
1367 function getShowAdvanced(){
1368 return $this->_showAdvanced;
1373 * Accepts a renderer
1375 * @param object $renderer HTML_QuickForm_Renderer An HTML_QuickForm_Renderer object
1376 * @access public
1377 * @return void
1379 function accept(&$renderer) {
1380 if (method_exists($renderer, 'setAdvancedElements')){
1381 //check for visible fieldsets where all elements are advanced
1382 //and mark these headers as advanced as well.
1383 //And mark all elements in a advanced header as advanced
1384 $stopFields = $renderer->getStopFieldSetElements();
1385 $lastHeader = null;
1386 $lastHeaderAdvanced = false;
1387 $anyAdvanced = false;
1388 foreach (array_keys($this->_elements) as $elementIndex){
1389 $element =& $this->_elements[$elementIndex];
1391 // if closing header and any contained element was advanced then mark it as advanced
1392 if ($element->getType()=='header' || in_array($element->getName(), $stopFields)){
1393 if ($anyAdvanced && !is_null($lastHeader)){
1394 $this->setAdvanced($lastHeader->getName());
1396 $lastHeaderAdvanced = false;
1397 unset($lastHeader);
1398 $lastHeader = null;
1399 } elseif ($lastHeaderAdvanced) {
1400 $this->setAdvanced($element->getName());
1403 if ($element->getType()=='header'){
1404 $lastHeader =& $element;
1405 $anyAdvanced = false;
1406 $lastHeaderAdvanced = isset($this->_advancedElements[$element->getName()]);
1407 } elseif (isset($this->_advancedElements[$element->getName()])){
1408 $anyAdvanced = true;
1411 // the last header may not be closed yet...
1412 if ($anyAdvanced && !is_null($lastHeader)){
1413 $this->setAdvanced($lastHeader->getName());
1415 $renderer->setAdvancedElements($this->_advancedElements);
1418 parent::accept($renderer);
1422 * @param string $elementName
1424 function closeHeaderBefore($elementName){
1425 $renderer =& $this->defaultRenderer();
1426 $renderer->addStopFieldsetElements($elementName);
1430 * Should be used for all elements of a form except for select, radio and checkboxes which
1431 * clean their own data.
1433 * @param string $elementname
1434 * @param integer $paramtype use the constants PARAM_*.
1435 * * PARAM_CLEAN is deprecated and you should try to use a more specific type.
1436 * * PARAM_TEXT should be used for cleaning data that is expected to be plain text.
1437 * It will strip all html tags. But will still let tags for multilang support
1438 * through.
1439 * * PARAM_RAW means no cleaning whatsoever, it is used mostly for data from the
1440 * html editor. Data from the editor is later cleaned before display using
1441 * format_text() function. PARAM_RAW can also be used for data that is validated
1442 * by some other way or printed by p() or s().
1443 * * PARAM_INT should be used for integers.
1444 * * PARAM_ACTION is an alias of PARAM_ALPHA and is used for hidden fields specifying
1445 * form actions.
1447 function setType($elementname, $paramtype) {
1448 $this->_types[$elementname] = $paramtype;
1452 * See description of setType above. This can be used to set several types at once.
1454 * @param array $paramtypes
1456 function setTypes($paramtypes) {
1457 $this->_types = $paramtypes + $this->_types;
1461 * @param array $submission
1462 * @param array $files
1464 function updateSubmission($submission, $files) {
1465 $this->_flagSubmitted = false;
1467 if (empty($submission)) {
1468 $this->_submitValues = array();
1469 } else {
1470 foreach ($submission as $key=>$s) {
1471 if (array_key_exists($key, $this->_types)) {
1472 $type = $this->_types[$key];
1473 } else {
1474 $type = PARAM_RAW;
1476 if (is_array($s)) {
1477 $submission[$key] = clean_param_array($s, $type, true);
1478 } else {
1479 $submission[$key] = clean_param($s, $type);
1482 $this->_submitValues = $submission;
1483 $this->_flagSubmitted = true;
1486 if (empty($files)) {
1487 $this->_submitFiles = array();
1488 } else {
1489 $this->_submitFiles = $files;
1490 $this->_flagSubmitted = true;
1493 // need to tell all elements that they need to update their value attribute.
1494 foreach (array_keys($this->_elements) as $key) {
1495 $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
1500 * @return string
1502 function getReqHTML(){
1503 return $this->_reqHTML;
1507 * @return string
1509 function getAdvancedHTML(){
1510 return $this->_advancedHTML;
1514 * Initializes a default form value. Used to specify the default for a new entry where
1515 * no data is loaded in using moodleform::set_data()
1517 * note: $slashed param removed
1519 * @param string $elementname element name
1520 * @param mixed $values values for that element name
1521 * @access public
1522 * @return void
1524 function setDefault($elementName, $defaultValue){
1525 $this->setDefaults(array($elementName=>$defaultValue));
1526 } // end func setDefault
1528 * Add an array of buttons to the form
1529 * @param array $buttons An associative array representing help button to attach to
1530 * to the form. keys of array correspond to names of elements in form.
1531 * @deprecated since Moodle 2.0 - use addHelpButton() call on each element manually
1532 * @param bool $suppresscheck
1533 * @param string $function
1534 * @access public
1536 function setHelpButtons($buttons, $suppresscheck=false, $function='helpbutton'){
1538 debugging('function moodle_form::setHelpButtons() is deprecated');
1539 //foreach ($buttons as $elementname => $button){
1540 // $this->setHelpButton($elementname, $button, $suppresscheck, $function);
1544 * Add a single button.
1546 * @deprecated use addHelpButton() instead
1547 * @param string $elementname name of the element to add the item to
1548 * @param array $button arguments to pass to function $function
1549 * @param boolean $suppresscheck whether to throw an error if the element
1550 * doesn't exist.
1551 * @param string $function - function to generate html from the arguments in $button
1552 * @param string $function
1554 function setHelpButton($elementname, $buttonargs, $suppresscheck=false, $function='helpbutton'){
1555 global $OUTPUT;
1557 debugging('function moodle_form::setHelpButton() is deprecated');
1558 if ($function !== 'helpbutton') {
1559 //debugging('parameter $function in moodle_form::setHelpButton() is not supported any more');
1562 $buttonargs = (array)$buttonargs;
1564 if (array_key_exists($elementname, $this->_elementIndex)) {
1565 //_elements has a numeric index, this code accesses the elements by name
1566 $element = $this->_elements[$this->_elementIndex[$elementname]];
1568 $page = isset($buttonargs[0]) ? $buttonargs[0] : null;
1569 $text = isset($buttonargs[1]) ? $buttonargs[1] : null;
1570 $module = isset($buttonargs[2]) ? $buttonargs[2] : 'moodle';
1571 $linktext = isset($buttonargs[3]) ? $buttonargs[3] : false;
1573 $element->_helpbutton = $OUTPUT->old_help_icon($page, $text, $module, $linktext);
1575 } else if (!$suppresscheck) {
1576 print_error('nonexistentformelements', 'form', '', $elementname);
1581 * Add a help button to element, only one button per element is allowed.
1583 * This is new, simplified and preferable method of setting a help icon on form elements.
1584 * It uses the new $OUTPUT->help_icon().
1586 * Typically, you will provide the same identifier and the component as you have used for the
1587 * label of the element. The string identifier with the _help suffix added is then used
1588 * as the help string.
1590 * There has to be two strings defined:
1591 * 1/ get_string($identifier, $component) - the title of the help page
1592 * 2/ get_string($identifier.'_help', $component) - the actual help page text
1594 * @since 2.0
1595 * @param string $elementname name of the element to add the item to
1596 * @param string $identifier help string identifier without _help suffix
1597 * @param string $component component name to look the help string in
1598 * @param string $linktext optional text to display next to the icon
1599 * @param boolean $suppresscheck set to true if the element may not exist
1600 * @return void
1602 function addHelpButton($elementname, $identifier, $component = 'moodle', $linktext = '', $suppresscheck = false) {
1603 global $OUTPUT;
1604 if (array_key_exists($elementname, $this->_elementIndex)) {
1605 $element = $this->_elements[$this->_elementIndex[$elementname]];
1606 $element->_helpbutton = $OUTPUT->help_icon($identifier, $component, $linktext);
1607 } else if (!$suppresscheck) {
1608 debugging(get_string('nonexistentformelements', 'form', $elementname));
1613 * Set constant value not overridden by _POST or _GET
1614 * note: this does not work for complex names with [] :-(
1616 * @param string $elname name of element
1617 * @param mixed $value
1618 * @return void
1620 function setConstant($elname, $value) {
1621 $this->_constantValues = HTML_QuickForm::arrayMerge($this->_constantValues, array($elname=>$value));
1622 $element =& $this->getElement($elname);
1623 $element->onQuickFormEvent('updateValue', null, $this);
1627 * @param string $elementList
1629 function exportValues($elementList = null){
1630 $unfiltered = array();
1631 if (null === $elementList) {
1632 // iterate over all elements, calling their exportValue() methods
1633 $emptyarray = array();
1634 foreach (array_keys($this->_elements) as $key) {
1635 if ($this->_elements[$key]->isFrozen() && !$this->_elements[$key]->_persistantFreeze){
1636 $value = $this->_elements[$key]->exportValue($emptyarray, true);
1637 } else {
1638 $value = $this->_elements[$key]->exportValue($this->_submitValues, true);
1641 if (is_array($value)) {
1642 // This shit throws a bogus warning in PHP 4.3.x
1643 $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
1646 } else {
1647 if (!is_array($elementList)) {
1648 $elementList = array_map('trim', explode(',', $elementList));
1650 foreach ($elementList as $elementName) {
1651 $value = $this->exportValue($elementName);
1652 if (PEAR::isError($value)) {
1653 return $value;
1655 //oh, stock QuickFOrm was returning array of arrays!
1656 $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
1660 if (is_array($this->_constantValues)) {
1661 $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $this->_constantValues);
1664 return $unfiltered;
1667 * Adds a validation rule for the given field
1669 * If the element is in fact a group, it will be considered as a whole.
1670 * To validate grouped elements as separated entities,
1671 * use addGroupRule instead of addRule.
1673 * @param string $element Form element name
1674 * @param string $message Message to display for invalid data
1675 * @param string $type Rule type, use getRegisteredRules() to get types
1676 * @param string $format (optional)Required for extra rule data
1677 * @param string $validation (optional)Where to perform validation: "server", "client"
1678 * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
1679 * @param boolean $force Force the rule to be applied, even if the target form element does not exist
1680 * @access public
1682 function addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false)
1684 parent::addRule($element, $message, $type, $format, $validation, $reset, $force);
1685 if ($validation == 'client') {
1686 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
1689 } // end func addRule
1691 * Adds a validation rule for the given group of elements
1693 * Only groups with a name can be assigned a validation rule
1694 * Use addGroupRule when you need to validate elements inside the group.
1695 * Use addRule if you need to validate the group as a whole. In this case,
1696 * the same rule will be applied to all elements in the group.
1697 * Use addRule if you need to validate the group against a function.
1699 * @param string $group Form group name
1700 * @param mixed $arg1 Array for multiple elements or error message string for one element
1701 * @param string $type (optional)Rule type use getRegisteredRules() to get types
1702 * @param string $format (optional)Required for extra rule data
1703 * @param int $howmany (optional)How many valid elements should be in the group
1704 * @param string $validation (optional)Where to perform validation: "server", "client"
1705 * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed.
1706 * @access public
1708 function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)
1710 parent::addGroupRule($group, $arg1, $type, $format, $howmany, $validation, $reset);
1711 if (is_array($arg1)) {
1712 foreach ($arg1 as $rules) {
1713 foreach ($rules as $rule) {
1714 $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server';
1716 if ('client' == $validation) {
1717 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
1721 } elseif (is_string($arg1)) {
1723 if ($validation == 'client') {
1724 $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);'));
1727 } // end func addGroupRule
1729 // }}}
1731 * Returns the client side validation script
1733 * The code here was copied from HTML_QuickForm_DHTMLRulesTableless who copied it from HTML_QuickForm
1734 * and slightly modified to run rules per-element
1735 * Needed to override this because of an error with client side validation of grouped elements.
1737 * @access public
1738 * @return string Javascript to perform validation, empty string if no 'client' rules were added
1740 function getValidationScript()
1742 if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
1743 return '';
1746 include_once('HTML/QuickForm/RuleRegistry.php');
1747 $registry =& HTML_QuickForm_RuleRegistry::singleton();
1748 $test = array();
1749 $js_escape = array(
1750 "\r" => '\r',
1751 "\n" => '\n',
1752 "\t" => '\t',
1753 "'" => "\\'",
1754 '"' => '\"',
1755 '\\' => '\\\\'
1758 foreach ($this->_rules as $elementName => $rules) {
1759 foreach ($rules as $rule) {
1760 if ('client' == $rule['validation']) {
1761 unset($element); //TODO: find out how to properly initialize it
1763 $dependent = isset($rule['dependent']) && is_array($rule['dependent']);
1764 $rule['message'] = strtr($rule['message'], $js_escape);
1766 if (isset($rule['group'])) {
1767 $group =& $this->getElement($rule['group']);
1768 // No JavaScript validation for frozen elements
1769 if ($group->isFrozen()) {
1770 continue 2;
1772 $elements =& $group->getElements();
1773 foreach (array_keys($elements) as $key) {
1774 if ($elementName == $group->getElementName($key)) {
1775 $element =& $elements[$key];
1776 break;
1779 } elseif ($dependent) {
1780 $element = array();
1781 $element[] =& $this->getElement($elementName);
1782 foreach ($rule['dependent'] as $elName) {
1783 $element[] =& $this->getElement($elName);
1785 } else {
1786 $element =& $this->getElement($elementName);
1788 // No JavaScript validation for frozen elements
1789 if (is_object($element) && $element->isFrozen()) {
1790 continue 2;
1791 } elseif (is_array($element)) {
1792 foreach (array_keys($element) as $key) {
1793 if ($element[$key]->isFrozen()) {
1794 continue 3;
1798 //for editor element, [text] is appended to the name.
1799 if ($element->getType() == 'editor') {
1800 $elementName .= '[text]';
1801 //Add format to rule as moodleform check which format is supported by browser
1802 //it is not set anywhere... So small hack to make sure we pass it down to quickform
1803 if (is_null($rule['format'])) {
1804 $rule['format'] = $element->getFormat();
1807 // Fix for bug displaying errors for elements in a group
1808 $test[$elementName][0][] = $registry->getValidationScript($element, $elementName, $rule);
1809 $test[$elementName][1]=$element;
1810 //end of fix
1815 // Fix for MDL-9524. If you don't do this, then $element may be left as a reference to one of the fields in
1816 // the form, and then that form field gets corrupted by the code that follows.
1817 unset($element);
1819 $js = '
1820 <script type="text/javascript">
1821 //<![CDATA[
1823 var skipClientValidation = false;
1825 function qf_errorHandler(element, _qfMsg) {
1826 div = element.parentNode;
1828 if ((div == undefined) || (element.name == undefined)) {
1829 //no checking can be done for undefined elements so let server handle it.
1830 return true;
1833 if (_qfMsg != \'\') {
1834 var errorSpan = document.getElementById(\'id_error_\'+element.name);
1835 if (!errorSpan) {
1836 errorSpan = document.createElement("span");
1837 errorSpan.id = \'id_error_\'+element.name;
1838 errorSpan.className = "error";
1839 element.parentNode.insertBefore(errorSpan, element.parentNode.firstChild);
1842 while (errorSpan.firstChild) {
1843 errorSpan.removeChild(errorSpan.firstChild);
1846 errorSpan.appendChild(document.createTextNode(_qfMsg.substring(3)));
1847 errorSpan.appendChild(document.createElement("br"));
1849 if (div.className.substr(div.className.length - 6, 6) != " error"
1850 && div.className != "error") {
1851 div.className += " error";
1854 return false;
1855 } else {
1856 var errorSpan = document.getElementById(\'id_error_\'+element.name);
1857 if (errorSpan) {
1858 errorSpan.parentNode.removeChild(errorSpan);
1861 if (div.className.substr(div.className.length - 6, 6) == " error") {
1862 div.className = div.className.substr(0, div.className.length - 6);
1863 } else if (div.className == "error") {
1864 div.className = "";
1867 return true;
1870 $validateJS = '';
1871 foreach ($test as $elementName => $jsandelement) {
1872 // Fix for bug displaying errors for elements in a group
1873 //unset($element);
1874 list($jsArr,$element)=$jsandelement;
1875 //end of fix
1876 $escapedElementName = preg_replace_callback(
1877 '/[_\[\]]/',
1878 create_function('$matches', 'return sprintf("_%2x",ord($matches[0]));'),
1879 $elementName);
1880 $js .= '
1881 function validate_' . $this->_formName . '_' . $escapedElementName . '(element) {
1882 if (undefined == element) {
1883 //required element was not found, then let form be submitted without client side validation
1884 return true;
1886 var value = \'\';
1887 var errFlag = new Array();
1888 var _qfGroups = {};
1889 var _qfMsg = \'\';
1890 var frm = element.parentNode;
1891 if ((undefined != element.name) && (frm != undefined)) {
1892 while (frm && frm.nodeName.toUpperCase() != "FORM") {
1893 frm = frm.parentNode;
1895 ' . join("\n", $jsArr) . '
1896 return qf_errorHandler(element, _qfMsg);
1897 } else {
1898 //element name should be defined else error msg will not be displayed.
1899 return true;
1903 $validateJS .= '
1904 ret = validate_' . $this->_formName . '_' . $escapedElementName.'(frm.elements[\''.$elementName.'\']) && ret;
1905 if (!ret && !first_focus) {
1906 first_focus = true;
1907 frm.elements[\''.$elementName.'\'].focus();
1911 // Fix for bug displaying errors for elements in a group
1912 //unset($element);
1913 //$element =& $this->getElement($elementName);
1914 //end of fix
1915 $valFunc = 'validate_' . $this->_formName . '_' . $escapedElementName . '(this)';
1916 $onBlur = $element->getAttribute('onBlur');
1917 $onChange = $element->getAttribute('onChange');
1918 $element->updateAttributes(array('onBlur' => $onBlur . $valFunc,
1919 'onChange' => $onChange . $valFunc));
1921 // do not rely on frm function parameter, because htmlarea breaks it when overloading the onsubmit method
1922 $js .= '
1923 function validate_' . $this->_formName . '(frm) {
1924 if (skipClientValidation) {
1925 return true;
1927 var ret = true;
1929 var frm = document.getElementById(\''. $this->_attributes['id'] .'\')
1930 var first_focus = false;
1931 ' . $validateJS . ';
1932 return ret;
1934 //]]>
1935 </script>';
1936 return $js;
1937 } // end func getValidationScript
1938 function _setDefaultRuleMessages(){
1939 foreach ($this->_rules as $field => $rulesarr){
1940 foreach ($rulesarr as $key => $rule){
1941 if ($rule['message']===null){
1942 $a=new stdClass();
1943 $a->format=$rule['format'];
1944 $str=get_string('err_'.$rule['type'], 'form', $a);
1945 if (strpos($str, '[[')!==0){
1946 $this->_rules[$field][$key]['message']=$str;
1953 function getLockOptionObject(){
1954 $result = array();
1955 foreach ($this->_dependencies as $dependentOn => $conditions){
1956 $result[$dependentOn] = array();
1957 foreach ($conditions as $condition=>$values) {
1958 $result[$dependentOn][$condition] = array();
1959 foreach ($values as $value=>$dependents) {
1960 $result[$dependentOn][$condition][$value] = array();
1961 $i = 0;
1962 foreach ($dependents as $dependent) {
1963 $elements = $this->_getElNamesRecursive($dependent);
1964 if (empty($elements)) {
1965 // probably element inside of some group
1966 $elements = array($dependent);
1968 foreach($elements as $element) {
1969 if ($element == $dependentOn) {
1970 continue;
1972 $result[$dependentOn][$condition][$value][] = $element;
1978 return array($this->getAttribute('id'), $result);
1982 * @param mixed $element
1983 * @return array
1985 function _getElNamesRecursive($element) {
1986 if (is_string($element)) {
1987 if (!$this->elementExists($element)) {
1988 return array();
1990 $element = $this->getElement($element);
1993 if (is_a($element, 'HTML_QuickForm_group')) {
1994 $elsInGroup = $element->getElements();
1995 $elNames = array();
1996 foreach ($elsInGroup as $elInGroup){
1997 if (is_a($elInGroup, 'HTML_QuickForm_group')) {
1998 // not sure if this would work - groups nested in groups
1999 $elNames = array_merge($elNames, $this->_getElNamesRecursive($elInGroup));
2000 } else {
2001 $elNames[] = $element->getElementName($elInGroup->getName());
2005 } else if (is_a($element, 'HTML_QuickForm_header')) {
2006 return array();
2008 } else if (is_a($element, 'HTML_QuickForm_hidden')) {
2009 return array();
2011 } else if (method_exists($element, 'getPrivateName') &&
2012 !($element instanceof HTML_QuickForm_advcheckbox)) {
2013 // The advcheckbox element implements a method called getPrivateName,
2014 // but in a way that is not compatible with the generic API, so we
2015 // have to explicitly exclude it.
2016 return array($element->getPrivateName());
2018 } else {
2019 $elNames = array($element->getName());
2022 return $elNames;
2026 * Adds a dependency for $elementName which will be disabled if $condition is met.
2027 * If $condition = 'notchecked' (default) then the condition is that the $dependentOn element
2028 * is not checked. If $condition = 'checked' then the condition is that the $dependentOn element
2029 * is checked. If $condition is something else (like "eq" for equals) then it is checked to see if the value
2030 * of the $dependentOn element is $condition (such as equal) to $value.
2032 * @param string $elementName the name of the element which will be disabled
2033 * @param string $dependentOn the name of the element whose state will be checked for
2034 * condition
2035 * @param string $condition the condition to check
2036 * @param mixed $value used in conjunction with condition.
2038 function disabledIf($elementName, $dependentOn, $condition = 'notchecked', $value='1'){
2039 if (!array_key_exists($dependentOn, $this->_dependencies)) {
2040 $this->_dependencies[$dependentOn] = array();
2042 if (!array_key_exists($condition, $this->_dependencies[$dependentOn])) {
2043 $this->_dependencies[$dependentOn][$condition] = array();
2045 if (!array_key_exists($value, $this->_dependencies[$dependentOn][$condition])) {
2046 $this->_dependencies[$dependentOn][$condition][$value] = array();
2048 $this->_dependencies[$dependentOn][$condition][$value][] = $elementName;
2051 function registerNoSubmitButton($buttonname){
2052 $this->_noSubmitButtons[]=$buttonname;
2056 * @param string $buttonname
2057 * @return mixed
2059 function isNoSubmitButton($buttonname){
2060 return (array_search($buttonname, $this->_noSubmitButtons)!==FALSE);
2064 * @param string $buttonname
2066 function _registerCancelButton($addfieldsname){
2067 $this->_cancelButtons[]=$addfieldsname;
2070 * Displays elements without HTML input tags.
2071 * This method is different to freeze() in that it makes sure no hidden
2072 * elements are included in the form.
2073 * Note: If you want to make sure the submitted value is ignored, please use setDefaults().
2075 * This function also removes all previously defined rules.
2077 * @param mixed $elementList array or string of element(s) to be frozen
2078 * @access public
2080 function hardFreeze($elementList=null)
2082 if (!isset($elementList)) {
2083 $this->_freezeAll = true;
2084 $elementList = array();
2085 } else {
2086 if (!is_array($elementList)) {
2087 $elementList = preg_split('/[ ]*,[ ]*/', $elementList);
2089 $elementList = array_flip($elementList);
2092 foreach (array_keys($this->_elements) as $key) {
2093 $name = $this->_elements[$key]->getName();
2094 if ($this->_freezeAll || isset($elementList[$name])) {
2095 $this->_elements[$key]->freeze();
2096 $this->_elements[$key]->setPersistantFreeze(false);
2097 unset($elementList[$name]);
2099 // remove all rules
2100 $this->_rules[$name] = array();
2101 // if field is required, remove the rule
2102 $unset = array_search($name, $this->_required);
2103 if ($unset !== false) {
2104 unset($this->_required[$unset]);
2109 if (!empty($elementList)) {
2110 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);
2112 return true;
2115 * Hard freeze all elements in a form except those whose names are in $elementList or hidden elements in a form.
2117 * This function also removes all previously defined rules of elements it freezes.
2119 * throws HTML_QuickForm_Error
2121 * @param array $elementList array or string of element(s) not to be frozen
2122 * @access public
2124 function hardFreezeAllVisibleExcept($elementList)
2126 $elementList = array_flip($elementList);
2127 foreach (array_keys($this->_elements) as $key) {
2128 $name = $this->_elements[$key]->getName();
2129 $type = $this->_elements[$key]->getType();
2131 if ($type == 'hidden'){
2132 // leave hidden types as they are
2133 } elseif (!isset($elementList[$name])) {
2134 $this->_elements[$key]->freeze();
2135 $this->_elements[$key]->setPersistantFreeze(false);
2137 // remove all rules
2138 $this->_rules[$name] = array();
2139 // if field is required, remove the rule
2140 $unset = array_search($name, $this->_required);
2141 if ($unset !== false) {
2142 unset($this->_required[$unset]);
2146 return true;
2149 * Tells whether the form was already submitted
2151 * This is useful since the _submitFiles and _submitValues arrays
2152 * may be completely empty after the trackSubmit value is removed.
2154 * @access public
2155 * @return bool
2157 function isSubmitted()
2159 return parent::isSubmitted() && (!$this->isFrozen());
2165 * A renderer for MoodleQuickForm that only uses XHTML and CSS and no
2166 * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless
2168 * Stylesheet is part of standard theme and should be automatically included.
2170 * @package moodlecore
2171 * @copyright Jamie Pratt <me@jamiep.org>
2172 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2174 class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{
2177 * Element template array
2178 * @var array
2179 * @access private
2181 var $_elementTemplates;
2183 * Template used when opening a hidden fieldset
2184 * (i.e. a fieldset that is opened when there is no header element)
2185 * @var string
2186 * @access private
2188 var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\"><div>";
2190 * Header Template string
2191 * @var string
2192 * @access private
2194 var $_headerTemplate =
2195 "\n\t\t<legend class=\"ftoggler\">{header}</legend>\n\t\t<div class=\"advancedbutton\">{advancedimg}{button}</div><div class=\"fcontainer clearfix\">\n\t\t";
2198 * Template used when opening a fieldset
2199 * @var string
2200 * @access private
2202 var $_openFieldsetTemplate = "\n\t<fieldset class=\"clearfix\" {id}>";
2205 * Template used when closing a fieldset
2206 * @var string
2207 * @access private
2209 var $_closeFieldsetTemplate = "\n\t\t</div></fieldset>";
2212 * Required Note template string
2213 * @var string
2214 * @access private
2216 var $_requiredNoteTemplate =
2217 "\n\t\t<div class=\"fdescription required\">{requiredNote}</div>";
2219 var $_advancedElements = array();
2222 * Whether to display advanced elements (on page load)
2224 * @var integer 1 means show 0 means hide
2226 var $_showAdvanced;
2228 function MoodleQuickForm_Renderer(){
2229 // switch next two lines for ol li containers for form items.
2230 // $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>');
2231 $this->_elementTemplates = array(
2232 'default'=>"\n\t\t".'<div id="{id}" class="fitem {advanced}<!-- BEGIN required --> required<!-- END required --> fitem_{type}"><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>',
2234 'fieldset'=>"\n\t\t".'<div id="{id}" class="fitem {advanced}<!-- BEGIN required --> required<!-- END required --> fitem_{type}"><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>',
2236 '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>',
2238 'warning'=>"\n\t\t".'<div class="fitem {advanced}">{element}</div>',
2240 'nodisplay'=>'');
2242 parent::HTML_QuickForm_Renderer_Tableless();
2246 * @param array $elements
2248 function setAdvancedElements($elements){
2249 $this->_advancedElements = $elements;
2253 * What to do when starting the form
2255 * @param object $form MoodleQuickForm
2257 function startForm(&$form){
2258 $this->_reqHTML = $form->getReqHTML();
2259 $this->_elementTemplates = str_replace('{req}', $this->_reqHTML, $this->_elementTemplates);
2260 $this->_advancedHTML = $form->getAdvancedHTML();
2261 $this->_showAdvanced = $form->getShowAdvanced();
2262 parent::startForm($form);
2263 if ($form->isFrozen()){
2264 $this->_formTemplate = "\n<div class=\"mform frozen\">\n{content}\n</div>";
2265 } else {
2266 $this->_formTemplate = "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{content}\n</form>";
2267 $this->_hiddenHtml .= $form->_pageparams;
2274 * @param object $group Passed by reference
2275 * @param mixed $required
2276 * @param mixed $error
2278 function startGroup(&$group, $required, $error){
2279 // Make sure the element has an id.
2280 $group->_generateId();
2282 if (method_exists($group, 'getElementTemplateType')){
2283 $html = $this->_elementTemplates[$group->getElementTemplateType()];
2284 }else{
2285 $html = $this->_elementTemplates['default'];
2288 if ($this->_showAdvanced){
2289 $advclass = ' advanced';
2290 } else {
2291 $advclass = ' advanced hide';
2293 if (isset($this->_advancedElements[$group->getName()])){
2294 $html =str_replace(' {advanced}', $advclass, $html);
2295 $html =str_replace('{advancedimg}', $this->_advancedHTML, $html);
2296 } else {
2297 $html =str_replace(' {advanced}', '', $html);
2298 $html =str_replace('{advancedimg}', '', $html);
2300 if (method_exists($group, 'getHelpButton')){
2301 $html =str_replace('{help}', $group->getHelpButton(), $html);
2302 }else{
2303 $html =str_replace('{help}', '', $html);
2305 $html =str_replace('{id}', 'fgroup_' . $group->getAttribute('id'), $html);
2306 $html =str_replace('{name}', $group->getName(), $html);
2307 $html =str_replace('{type}', 'fgroup', $html);
2309 $this->_templates[$group->getName()]=$html;
2310 // Fix for bug in tableless quickforms that didn't allow you to stop a
2311 // fieldset before a group of elements.
2312 // if the element name indicates the end of a fieldset, close the fieldset
2313 if ( in_array($group->getName(), $this->_stopFieldsetElements)
2314 && $this->_fieldsetsOpen > 0
2316 $this->_html .= $this->_closeFieldsetTemplate;
2317 $this->_fieldsetsOpen--;
2319 parent::startGroup($group, $required, $error);
2322 * @param object $element
2323 * @param mixed $required
2324 * @param mixed $error
2326 function renderElement(&$element, $required, $error){
2327 // Make sure the element has an id.
2328 $element->_generateId();
2330 //adding stuff to place holders in template
2331 //check if this is a group element first
2332 if (($this->_inGroup) and !empty($this->_groupElementTemplate)) {
2333 // so it gets substitutions for *each* element
2334 $html = $this->_groupElementTemplate;
2336 elseif (method_exists($element, 'getElementTemplateType')){
2337 $html = $this->_elementTemplates[$element->getElementTemplateType()];
2338 }else{
2339 $html = $this->_elementTemplates['default'];
2341 if ($this->_showAdvanced){
2342 $advclass = ' advanced';
2343 } else {
2344 $advclass = ' advanced hide';
2346 if (isset($this->_advancedElements[$element->getName()])){
2347 $html =str_replace(' {advanced}', $advclass, $html);
2348 } else {
2349 $html =str_replace(' {advanced}', '', $html);
2351 if (isset($this->_advancedElements[$element->getName()])||$element->getName() == 'mform_showadvanced'){
2352 $html =str_replace('{advancedimg}', $this->_advancedHTML, $html);
2353 } else {
2354 $html =str_replace('{advancedimg}', '', $html);
2356 $html =str_replace('{id}', 'fitem_' . $element->getAttribute('id'), $html);
2357 $html =str_replace('{type}', 'f'.$element->getType(), $html);
2358 $html =str_replace('{name}', $element->getName(), $html);
2359 if (method_exists($element, 'getHelpButton')){
2360 $html = str_replace('{help}', $element->getHelpButton(), $html);
2361 }else{
2362 $html = str_replace('{help}', '', $html);
2365 if (($this->_inGroup) and !empty($this->_groupElementTemplate)) {
2366 $this->_groupElementTemplate = $html;
2368 elseif (!isset($this->_templates[$element->getName()])) {
2369 $this->_templates[$element->getName()] = $html;
2372 parent::renderElement($element, $required, $error);
2376 * @global moodle_page $PAGE
2377 * @param object $form Passed by reference
2379 function finishForm(&$form){
2380 global $PAGE;
2381 if ($form->isFrozen()){
2382 $this->_hiddenHtml = '';
2384 parent::finishForm($form);
2385 if (!$form->isFrozen()) {
2386 $args = $form->getLockOptionObject();
2387 if (count($args[1]) > 0) {
2388 $PAGE->requires->js_init_call('M.form.initFormDependencies', $args, true, moodleform::get_js_module());
2393 * Called when visiting a header element
2395 * @param object $header An HTML_QuickForm_header element being visited
2396 * @access public
2397 * @return void
2398 * @global moodle_page $PAGE
2400 function renderHeader(&$header) {
2401 global $PAGE;
2403 $name = $header->getName();
2405 $id = empty($name) ? '' : ' id="' . $name . '"';
2406 $id = preg_replace(array('/\]/', '/\[/'), array('', '_'), $id);
2407 if (is_null($header->_text)) {
2408 $header_html = '';
2409 } elseif (!empty($name) && isset($this->_templates[$name])) {
2410 $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
2411 } else {
2412 $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
2415 if (isset($this->_advancedElements[$name])){
2416 $header_html =str_replace('{advancedimg}', $this->_advancedHTML, $header_html);
2417 $elementName='mform_showadvanced';
2418 if ($this->_showAdvanced==0){
2419 $buttonlabel = get_string('showadvanced', 'form');
2420 } else {
2421 $buttonlabel = get_string('hideadvanced', 'form');
2423 $button = '<input name="'.$elementName.'" class="showadvancedbtn" value="'.$buttonlabel.'" type="submit" />';
2424 $PAGE->requires->js_init_call('M.form.initShowAdvanced', array(), false, moodleform::get_js_module());
2425 $header_html = str_replace('{button}', $button, $header_html);
2426 } else {
2427 $header_html =str_replace('{advancedimg}', '', $header_html);
2428 $header_html = str_replace('{button}', '', $header_html);
2431 if ($this->_fieldsetsOpen > 0) {
2432 $this->_html .= $this->_closeFieldsetTemplate;
2433 $this->_fieldsetsOpen--;
2436 $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate);
2437 if ($this->_showAdvanced){
2438 $advclass = ' class="advanced"';
2439 } else {
2440 $advclass = ' class="advanced hide"';
2442 if (isset($this->_advancedElements[$name])){
2443 $openFieldsetTemplate = str_replace('{advancedclass}', $advclass, $openFieldsetTemplate);
2444 } else {
2445 $openFieldsetTemplate = str_replace('{advancedclass}', '', $openFieldsetTemplate);
2447 $this->_html .= $openFieldsetTemplate . $header_html;
2448 $this->_fieldsetsOpen++;
2449 } // end func renderHeader
2451 function getStopFieldsetElements(){
2452 return $this->_stopFieldsetElements;
2457 * Required elements validation
2458 * This class overrides QuickForm validation since it allowed space or empty tag as a value
2460 class MoodleQuickForm_Rule_Required extends HTML_QuickForm_Rule {
2462 * Checks if an element is not empty.
2463 * This is a server-side validation, it works for both text fields and editor fields
2465 * @param string $value Value to check
2466 * @param mixed $options Not used yet
2467 * @return boolean true if value is not empty
2469 function validate($value, $options = null) {
2470 global $CFG;
2471 if (is_array($value) && array_key_exists('text', $value)) {
2472 $value = $value['text'];
2474 if (is_array($value)) {
2475 // nasty guess - there has to be something in the array, hopefully nobody invents arrays in arrays
2476 $value = implode('', $value);
2478 $stripvalues = array(
2479 '#</?(?!img|canvas|hr).*?>#im', // all tags except img, canvas and hr
2480 '#(\xc2|\xa0|\s|&nbsp;)#', //any whitespaces actually
2482 if (!empty($CFG->strictformsrequired)) {
2483 $value = preg_replace($stripvalues, '', (string)$value);
2485 if ((string)$value == '') {
2486 return false;
2488 return true;
2492 * This function returns Javascript code used to build client-side validation.
2493 * It checks if an element is not empty.
2495 * @param int $format
2496 * @return array
2498 function getValidationScript($format = null) {
2499 global $CFG;
2500 if (!empty($CFG->strictformsrequired)) {
2501 if (!empty($format) && $format == FORMAT_HTML) {
2502 return array('', "{jsVar}.replace(/(<[^img|hr|canvas]+>)|&nbsp;|\s+/ig, '') == ''");
2503 } else {
2504 return array('', "{jsVar}.replace(/^\s+$/g, '') == ''");
2506 } else {
2507 return array('', "{jsVar} == ''");
2513 * @global object $GLOBALS['_HTML_QuickForm_default_renderer']
2514 * @name $_HTML_QuickForm_default_renderer
2516 $GLOBALS['_HTML_QuickForm_default_renderer'] = new MoodleQuickForm_Renderer();
2518 /** Please keep this list in alphabetical order. */
2519 MoodleQuickForm::registerElementType('advcheckbox', "$CFG->libdir/form/advcheckbox.php", 'MoodleQuickForm_advcheckbox');
2520 MoodleQuickForm::registerElementType('button', "$CFG->libdir/form/button.php", 'MoodleQuickForm_button');
2521 MoodleQuickForm::registerElementType('cancel', "$CFG->libdir/form/cancel.php", 'MoodleQuickForm_cancel');
2522 MoodleQuickForm::registerElementType('searchableselector', "$CFG->libdir/form/searchableselector.php", 'MoodleQuickForm_searchableselector');
2523 MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox');
2524 MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector');
2525 MoodleQuickForm::registerElementType('date_time_selector', "$CFG->libdir/form/datetimeselector.php", 'MoodleQuickForm_date_time_selector');
2526 MoodleQuickForm::registerElementType('duration', "$CFG->libdir/form/duration.php", 'MoodleQuickForm_duration');
2527 MoodleQuickForm::registerElementType('editor', "$CFG->libdir/form/editor.php", 'MoodleQuickForm_editor');
2528 MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'MoodleQuickForm_file');
2529 MoodleQuickForm::registerElementType('filemanager', "$CFG->libdir/form/filemanager.php", 'MoodleQuickForm_filemanager');
2530 MoodleQuickForm::registerElementType('filepicker', "$CFG->libdir/form/filepicker.php", 'MoodleQuickForm_filepicker');
2531 MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format');
2532 MoodleQuickForm::registerElementType('grading', "$CFG->libdir/form/grading.php", 'MoodleQuickForm_grading');
2533 MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group');
2534 MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", 'MoodleQuickForm_header');
2535 MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden');
2536 MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor');
2537 MoodleQuickForm::registerElementType('modgrade', "$CFG->libdir/form/modgrade.php", 'MoodleQuickForm_modgrade');
2538 MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible');
2539 MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password');
2540 MoodleQuickForm::registerElementType('passwordunmask', "$CFG->libdir/form/passwordunmask.php", 'MoodleQuickForm_passwordunmask');
2541 MoodleQuickForm::registerElementType('questioncategory', "$CFG->libdir/form/questioncategory.php", 'MoodleQuickForm_questioncategory');
2542 MoodleQuickForm::registerElementType('radio', "$CFG->libdir/form/radio.php", 'MoodleQuickForm_radio');
2543 MoodleQuickForm::registerElementType('recaptcha', "$CFG->libdir/form/recaptcha.php", 'MoodleQuickForm_recaptcha');
2544 MoodleQuickForm::registerElementType('select', "$CFG->libdir/form/select.php", 'MoodleQuickForm_select');
2545 MoodleQuickForm::registerElementType('selectgroups', "$CFG->libdir/form/selectgroups.php", 'MoodleQuickForm_selectgroups');
2546 MoodleQuickForm::registerElementType('selectwithlink', "$CFG->libdir/form/selectwithlink.php", 'MoodleQuickForm_selectwithlink');
2547 MoodleQuickForm::registerElementType('selectyesno', "$CFG->libdir/form/selectyesno.php", 'MoodleQuickForm_selectyesno');
2548 MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static');
2549 MoodleQuickForm::registerElementType('submit', "$CFG->libdir/form/submit.php", 'MoodleQuickForm_submit');
2550 MoodleQuickForm::registerElementType('submitlink', "$CFG->libdir/form/submitlink.php", 'MoodleQuickForm_submitlink');
2551 MoodleQuickForm::registerElementType('tags', "$CFG->libdir/form/tags.php", 'MoodleQuickForm_tags');
2552 MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text');
2553 MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea');
2554 MoodleQuickForm::registerElementType('url', "$CFG->libdir/form/url.php", 'MoodleQuickForm_url');
2555 MoodleQuickForm::registerElementType('warning', "$CFG->libdir/form/warning.php", 'MoodleQuickForm_warning');
2557 MoodleQuickForm::registerRule('required', null, 'MoodleQuickForm_Rule_Required', "$CFG->libdir/formslib.php");