2ae22002 |
1 | <?php // $Id$ |
da6f8763 |
2 | /** |
3 | * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms. |
da1320da |
4 | * |
5 | * To use formslib then you will want to create a new file purpose_form.php eg. edit_form.php |
6 | * and you want to name your class something like {modulename}_{purpose}_form. Your class will |
7 | * extend moodleform overriding abstract classes definition and optionally defintion_after_data |
8 | * and validation. |
9 | * |
10 | * See examples of use of this library in course/edit.php and course/edit_form.php |
11 | * |
12 | * A few notes : |
13 | * form defintion is used for both printing of form and processing and should be the same |
14 | * for both or you may lose some submitted data which won't be let through. |
15 | * you should be using setType for every form element except select, radio or checkbox |
16 | * elements, these elements clean themselves. |
17 | * |
18 | * |
da6f8763 |
19 | * @author Jamie Pratt |
20 | * @version $Id$ |
21 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
22 | */ |
23 | |
24 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else. |
25 | if (FALSE===strstr(ini_get('include_path'), $CFG->libdir.'/pear' )){ |
26 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); |
27 | } |
28 | require_once 'HTML/QuickForm.php'; |
29 | require_once 'HTML/QuickForm/DHTMLRulesTableless.php'; |
30 | require_once 'HTML/QuickForm/Renderer/Tableless.php'; |
31 | |
49292f8c |
32 | require_once $CFG->libdir.'/uploadlib.php'; |
33 | |
a23f0aaf |
34 | /** |
35 | * Callback called when PEAR throws an error |
36 | * |
37 | * @param PEAR_Error $error |
38 | */ |
39 | function pear_handle_error($error){ |
40 | echo '<strong>'.$error->GetMessage().'</strong> '.$error->getUserInfo(); |
41 | echo '<br /> <strong>Backtrace </strong>:'; |
42 | print_object($error->backtrace); |
43 | } |
44 | |
864cc1de |
45 | if ($CFG->debug >= DEBUG_ALL){ |
a23f0aaf |
46 | PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_handle_error'); |
864cc1de |
47 | } |
48 | |
f07b9627 |
49 | |
05f5c40c |
50 | /** |
da1320da |
51 | * Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly |
52 | * use this class you should write a class defintion which extends this class or a more specific |
53 | * subclass such a moodleform_mod for each form you want to display and/or process with formslib. |
54 | * |
55 | * You will write your own definition() method which performs the form set up. |
05f5c40c |
56 | */ |
7f40a229 |
57 | class moodleform { |
49292f8c |
58 | var $_formname; // form name |
3c7656b4 |
59 | /** |
60 | * quickform object definition |
61 | * |
62 | * @var MoodleQuickForm |
63 | */ |
64 | var $_form; |
65 | /** |
66 | * globals workaround |
67 | * |
68 | * @var array |
69 | */ |
70 | var $_customdata; |
71 | /** |
72 | * file upload manager |
73 | * |
74 | * @var upload_manager |
75 | */ |
76 | var $_upload_manager; // |
cbdb3705 |
77 | |
78 | |
ebd3c7ac |
79 | |
da1320da |
80 | /** |
81 | * The constructor function calls the abstract function definition() and it will then |
82 | * process and clean and attempt to validate incoming data. |
83 | * |
84 | * It will call your custom validate method to validate data and will also check any rules |
85 | * you have specified in definition using addRule |
86 | * |
87 | * The name of the form (id attribute of the form) is automatically generated depending on |
88 | * the name you gave the class extending moodleform. You should call your class something |
89 | * like |
90 | * |
cbdb3705 |
91 | * @param string $action the action attribute for the form. If empty defaults to auto detect the |
92 | * current url. |
da1320da |
93 | * @param array $customdata if your form defintion method needs access to data such as $course |
94 | * $cm, etc. to construct the form definition then pass it in this array. You can |
95 | * use globals for somethings. |
96 | * @param string $method if you set this to anything other than 'post' then _GET and _POST will |
97 | * be merged and used as incoming data to the form. |
98 | * @param string $target target frame for form submission. You will rarely use this. Don't use |
99 | * it if you don't need to as the target attribute is deprecated in xhtml |
100 | * strict. |
101 | * @param mixed $attributes you can pass a string of html attributes here or an array. |
102 | * @return moodleform |
103 | */ |
cbdb3705 |
104 | function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null) { |
a23f0aaf |
105 | if (empty($action)){ |
106 | $action = strip_querystring(qualified_me()); |
107 | } |
f07b9627 |
108 | |
72f46d11 |
109 | $this->_formname = get_class($this); // '_form' suffix kept in order to prevent collisions of form id and other element |
7f40a229 |
110 | $this->_customdata = $customdata; |
5bc97c98 |
111 | $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); |
feaf5d06 |
112 | $this->set_upload_manager(new upload_manager()); |
7f40a229 |
113 | |
114 | $this->definition(); |
115 | |
116 | $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection |
117 | $this->_form->setDefault('sesskey', sesskey()); |
5bc97c98 |
118 | $this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker |
119 | $this->_form->setDefault('_qf__'.$this->_formname, 1); |
120 | $this->_form->_setDefaultRuleMessages(); |
7f40a229 |
121 | |
122 | // we have to know all input types before processing submission ;-) |
123 | $this->_process_submission($method); |
cbdb3705 |
124 | |
125 | // update form definition based on final data |
126 | $this->definition_after_data(); |
7f40a229 |
127 | } |
05f5c40c |
128 | |
2c412890 |
129 | /** |
da1320da |
130 | * To autofocus on first form element or first element with error. |
2c412890 |
131 | * |
8dec2253 |
132 | * @param string $name if this is set then the focus is forced to a field with this name |
133 | * |
2c412890 |
134 | * @return string javascript to select form element with first error or |
da1320da |
135 | * first element if no errors. Use this as a parameter |
136 | * when calling print_header |
2c412890 |
137 | */ |
8dec2253 |
138 | function focus($name=NULL){ |
9403060a |
139 | $form =& $this->_form; |
5c52df67 |
140 | $elkeys=array_keys($form->_elementIndex); |
9403060a |
141 | if (isset($form->_errors) && 0 != count($form->_errors)){ |
142 | $errorkeys = array_keys($form->_errors); |
143 | $elkeys = array_intersect($elkeys, $errorkeys); |
2c412890 |
144 | } |
9403060a |
145 | $names=null; |
146 | while (!$names){ |
8dec2253 |
147 | $el = array_shift($elkeys); |
148 | $names = $form->_getElNamesRecursive($el); |
149 | } |
150 | if (empty($name)) { |
151 | $name=array_shift($names); |
9403060a |
152 | } |
9403060a |
153 | $focus='forms[\''.$this->_form->getAttribute('id').'\'].elements[\''.$name.'\']'; |
154 | return $focus; |
155 | } |
7f40a229 |
156 | |
05f5c40c |
157 | /** |
158 | * Internal method. Alters submitted data to be suitable for quickforms processing. |
159 | * Must be called when the form is fully set up. |
160 | */ |
7f40a229 |
161 | function _process_submission($method) { |
162 | $submission = array(); |
163 | if ($method == 'post') { |
164 | if (!empty($_POST)) { |
165 | $submission = $_POST; |
166 | } |
167 | } else { |
168 | $submission = array_merge_recursive($_GET, $_POST); // emulate handling of parameters in xxxx_param() |
169 | } |
170 | |
171 | // following trick is needed to enable proper sesskey checks when using GET forms |
5bc97c98 |
172 | // the _qf__.$this->_formname serves as a marker that form was actually submitted |
173 | if (array_key_exists('_qf__'.$this->_formname, $submission) and $submission['_qf__'.$this->_formname] == 1) { |
7f40a229 |
174 | if (!confirm_sesskey()) { |
175 | error('Incorrect sesskey submitted, form not accepted!'); |
176 | } |
05f5c40c |
177 | $files = $_FILES; |
7f40a229 |
178 | } else { |
179 | $submission = array(); |
05f5c40c |
180 | $files = array(); |
7f40a229 |
181 | } |
182 | |
05f5c40c |
183 | $this->_form->updateSubmission($submission, $files); |
7f40a229 |
184 | } |
185 | |
05f5c40c |
186 | /** |
187 | * Internal method. Validates all uploaded files. |
188 | */ |
49292f8c |
189 | function _validate_files() { |
190 | if (empty($_FILES)) { |
191 | // we do not need to do any checks because no files were submitted |
192 | // TODO: find out why server side required rule does not work for uploaded files; |
193 | // testing is easily done by always returning true from this function and adding |
194 | // $mform->addRule('soubor', get_string('required'), 'required', null, 'server'); |
195 | // and submitting form without selected file |
196 | return true; |
197 | } |
198 | $errors = array(); |
199 | $mform =& $this->_form; |
200 | |
49292f8c |
201 | // check the files |
202 | $status = $this->_upload_manager->preprocess_files(); |
203 | |
204 | // now check that we really want each file |
205 | foreach ($_FILES as $elname=>$file) { |
206 | if ($mform->elementExists($elname) and $mform->getElementType($elname)=='file') { |
207 | $required = $mform->isElementRequired($elname); |
208 | if (!empty($this->_upload_manager->files[$elname]['uploadlog']) and empty($this->_upload_manager->files[$elname]['clear'])) { |
209 | if (!$required and $file['error'] == UPLOAD_ERR_NO_FILE) { |
210 | // file not uploaded and not required - ignore it |
211 | continue; |
212 | } |
213 | $errors[$elname] = $this->_upload_manager->files[$elname]['uploadlog']; |
214 | } |
215 | } else { |
feaf5d06 |
216 | error('Incorrect upload attempt!'); |
49292f8c |
217 | } |
218 | } |
219 | |
220 | // return errors if found |
221 | if ($status and 0 == count($errors)){ |
222 | return true; |
223 | } else { |
224 | return $errors; |
225 | } |
226 | } |
227 | |
05f5c40c |
228 | /** |
da1320da |
229 | * Load in existing data as form defaults. Usually new entry defaults are stored directly in |
230 | * form definition (new entry form); this function is used to load in data where values |
231 | * already exist and data is being edited (edit entry form). |
05f5c40c |
232 | * |
233 | * @param mixed $default_values object or array of default values |
234 | * @param bool $slased true if magic quotes applied to data values |
235 | */ |
beac4717 |
236 | function set_data($default_values, $slashed=false) { |
7f40a229 |
237 | if (is_object($default_values)) { |
238 | $default_values = (array)$default_values; |
239 | } |
240 | $filter = $slashed ? 'stripslashes' : NULL; |
241 | $this->_form->setDefaults($default_values, $filter); |
cbdb3705 |
242 | //update form definition when data changed |
243 | $this->definition_after_data(); |
7f40a229 |
244 | } |
245 | |
c80a13c7 |
246 | /** |
feaf5d06 |
247 | * Set custom upload manager. |
c80a13c7 |
248 | * Must be used BEFORE creating of file element! |
249 | * |
feaf5d06 |
250 | * @param object $um - custom upload manager |
c80a13c7 |
251 | */ |
feaf5d06 |
252 | function set_upload_manager($um=false) { |
253 | if ($um === false) { |
254 | $um = new upload_manager(); |
c80a13c7 |
255 | } |
feaf5d06 |
256 | $this->_upload_manager = $um; |
c80a13c7 |
257 | |
feaf5d06 |
258 | $this->_form->setMaxFileSize($um->config->maxbytes); |
c80a13c7 |
259 | } |
260 | |
05f5c40c |
261 | /** |
262 | * Check that form was submitted. Does not check validity of submitted data. |
263 | * |
264 | * @return bool true if form properly submitted |
265 | */ |
7f40a229 |
266 | function is_submitted() { |
267 | return $this->_form->isSubmitted(); |
268 | } |
269 | |
a23f0aaf |
270 | function no_submit_button_pressed(){ |
271 | static $nosubmit = null; // one check is enough |
272 | if (!is_null($nosubmit)){ |
273 | return $nosubmit; |
274 | } |
275 | $mform =& $this->_form; |
276 | $nosubmit = false; |
f07b9627 |
277 | if (!$this->is_submitted()){ |
278 | return false; |
279 | } |
a23f0aaf |
280 | foreach ($mform->_noSubmitButtons as $nosubmitbutton){ |
281 | if (optional_param($nosubmitbutton, 0, PARAM_RAW)){ |
282 | $nosubmit = true; |
283 | break; |
284 | } |
285 | } |
286 | return $nosubmit; |
287 | } |
288 | |
289 | |
05f5c40c |
290 | /** |
291 | * Check that form data is valid. |
292 | * |
293 | * @return bool true if form data valid |
294 | */ |
7f40a229 |
295 | function is_validated() { |
49292f8c |
296 | static $validated = null; // one validation is enough |
3ba2c187 |
297 | $mform =& $this->_form; |
f07b9627 |
298 | |
9aa022fe |
299 | if ($this->no_submit_button_pressed()){ |
300 | return false; |
301 | } elseif ($validated === null) { |
3ba2c187 |
302 | $internal_val = $mform->validate(); |
303 | $moodle_val = $this->validation($mform->exportValues(null, true)); |
7f40a229 |
304 | if ($moodle_val !== true) { |
a23f0aaf |
305 | if ((is_array($moodle_val) && count($moodle_val)!==0)) { |
7f40a229 |
306 | foreach ($moodle_val as $element=>$msg) { |
3ba2c187 |
307 | $mform->setElementError($element, $msg); |
7f40a229 |
308 | } |
a23f0aaf |
309 | $moodle_val = false; |
310 | } else { |
311 | $moodle_val = true; |
7f40a229 |
312 | } |
7f40a229 |
313 | } |
49292f8c |
314 | $file_val = $this->_validate_files(); |
315 | if ($file_val !== true) { |
316 | if (!empty($file_val)) { |
317 | foreach ($file_val as $element=>$msg) { |
3ba2c187 |
318 | $mform->setElementError($element, $msg); |
49292f8c |
319 | } |
320 | } |
321 | $file_val = false; |
322 | } |
323 | $validated = ($internal_val and $moodle_val and $file_val); |
7f40a229 |
324 | } |
9aa022fe |
325 | return $validated; |
7f40a229 |
326 | } |
327 | |
19110c57 |
328 | /** |
329 | * Return true if a cancel button has been pressed resulting in the form being submitted. |
330 | * |
331 | * @return boolean true if a cancel button has been pressed |
332 | */ |
333 | function is_cancelled(){ |
334 | $mform =& $this->_form; |
a23f0aaf |
335 | if ($mform->isSubmitted()){ |
336 | foreach ($mform->_cancelButtons as $cancelbutton){ |
337 | if (optional_param($cancelbutton, 0, PARAM_RAW)){ |
338 | return true; |
339 | } |
19110c57 |
340 | } |
341 | } |
342 | return false; |
343 | } |
344 | |
05f5c40c |
345 | /** |
da1320da |
346 | * Return submitted data if properly submitted or returns NULL if validation fails or |
347 | * if there is no submitted data. |
05f5c40c |
348 | * |
349 | * @param bool $slashed true means return data with addslashes applied |
350 | * @return object submitted data; NULL if not valid or not submitted |
351 | */ |
beac4717 |
352 | function get_data($slashed=true) { |
19110c57 |
353 | $mform =& $this->_form; |
3ba2c187 |
354 | |
7f40a229 |
355 | if ($this->is_submitted() and $this->is_validated()) { |
19110c57 |
356 | $data = $mform->exportValues(null, $slashed); |
5bc97c98 |
357 | unset($data['sesskey']); // we do not need to return sesskey |
358 | unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too |
7f40a229 |
359 | if (empty($data)) { |
360 | return NULL; |
361 | } else { |
362 | return (object)$data; |
363 | } |
364 | } else { |
365 | return NULL; |
366 | } |
367 | } |
368 | |
05f5c40c |
369 | /** |
370 | * Save verified uploaded files into directory. Upload process can be customised from definition() |
38f394b2 |
371 | * method by creating instance of upload manager and storing it in $this->_upload_form |
05f5c40c |
372 | * |
373 | * @param string $destination where to store uploaded files |
374 | * @return bool success |
375 | */ |
49292f8c |
376 | function save_files($destination) { |
49292f8c |
377 | if ($this->is_submitted() and $this->is_validated()) { |
378 | return $this->_upload_manager->save_files($destination); |
379 | } |
380 | return false; |
381 | } |
2b63df96 |
382 | |
feaf5d06 |
383 | /** |
384 | * If we're only handling one file (if inputname was given in the constructor) |
385 | * this will return the (possibly changed) filename of the file. |
386 | * @return mixed false in case of failure, string if ok |
387 | */ |
388 | function get_new_filename() { |
389 | return $this->_upload_manager->get_new_filename(); |
390 | } |
391 | |
05f5c40c |
392 | /** |
393 | * Print html form. |
394 | */ |
7f40a229 |
395 | function display() { |
396 | $this->_form->display(); |
397 | } |
398 | |
49292f8c |
399 | /** |
05f5c40c |
400 | * Abstract method - always override! |
49292f8c |
401 | * |
402 | * If you need special handling of uploaded files, create instance of $this->_upload_manager here. |
403 | */ |
7f40a229 |
404 | function definition() { |
405 | error('Abstract form_definition() method in class '.get_class($this).' must be overriden, please fix the code.'); |
406 | } |
2c412890 |
407 | |
c08ac016 |
408 | /** |
05f5c40c |
409 | * Dummy stub method - override if you need to setup the form depending on current |
beac4717 |
410 | * values. This method is called after definition(), data submission and set_data(). |
05f5c40c |
411 | * All form setup that is dependent on form values should go in here. |
c08ac016 |
412 | */ |
413 | function definition_after_data(){ |
c08ac016 |
414 | } |
7f40a229 |
415 | |
05f5c40c |
416 | /** |
417 | * Dummy stub method - override if you needed to perform some extra validation. |
418 | * If there are errors return array of errors ("fieldname"=>"error message"), |
419 | * otherwise true if ok. |
38f394b2 |
420 | * |
05f5c40c |
421 | * @param array $data array of ("fieldname"=>value) of submitted data |
38f394b2 |
422 | * @return bool array of errors or true if ok |
05f5c40c |
423 | */ |
7f40a229 |
424 | function validation($data) { |
cbdb3705 |
425 | return true; |
7f40a229 |
426 | } |
ebd3c7ac |
427 | |
cbdb3705 |
428 | |
429 | |
430 | |
431 | |
616b549a |
432 | /** |
433 | * Method to add a repeating group of elements to a form. |
434 | * |
435 | * @param array $elementobjs Array of elements or groups of elements that are to be repeated |
436 | * @param integer $repeats no of times to repeat elements initially |
437 | * @param array $options Array of options to apply to elements. Array keys are element names. |
438 | * This is an array of arrays. The second sets of keys are the option types |
439 | * for the elements : |
440 | * 'default' - default value is value |
441 | * 'type' - PARAM_* constant is value |
442 | * 'helpbutton' - helpbutton params array is value |
443 | * 'disabledif' - last three moodleform::disabledIf() |
444 | * params are value as an array |
445 | * @param string $repeathiddenname name for hidden element storing no of repeats in this form |
446 | * @param string $addfieldsname name for button to add more fields |
447 | * @param int $addfieldsno how many fields to add at a time |
271ffe3f |
448 | * @param string $addstring name of button, {no} is replaced by no of blanks that will be added. |
a23f0aaf |
449 | * @return int no of repeats of element in this page |
616b549a |
450 | */ |
271ffe3f |
451 | function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname, $addfieldsname, $addfieldsno=5, $addstring=null){ |
452 | if ($addstring===null){ |
453 | $addstring = get_string('addfields', 'form', $addfieldsno); |
454 | } else { |
455 | $addstring = str_ireplace('{no}', $addfieldsno, $addstring); |
456 | } |
ebd3c7ac |
457 | $repeats = optional_param($repeathiddenname, $repeats, PARAM_INT); |
458 | $addfields = optional_param($addfieldsname, '', PARAM_TEXT); |
459 | if (!empty($addfields)){ |
460 | $repeats += $addfieldsno; |
461 | } |
ebd3c7ac |
462 | $mform =& $this->_form; |
a23f0aaf |
463 | $mform->registerNoSubmitButton($addfieldsname); |
ebd3c7ac |
464 | $mform->addElement('hidden', $repeathiddenname, $repeats); |
465 | //value not to be overridden by submitted value |
466 | $mform->setConstants(array($repeathiddenname=>$repeats)); |
467 | for ($i=0; $i<$repeats; $i++) { |
468 | foreach ($elementobjs as $elementobj){ |
271ffe3f |
469 | $elementclone = clone($elementobj); |
7b41a4a9 |
470 | $name = $elementclone->getName(); |
86aab05c |
471 | if (!empty($name)){ |
472 | $elementclone->setName($name."[$i]"); |
473 | } |
ebd3c7ac |
474 | if (is_a($elementclone, 'HTML_QuickForm_header')){ |
475 | $value=$elementclone->_text; |
271ffe3f |
476 | $elementclone->setValue(str_replace('{no}', ($i+1), $value)); |
477 | |
478 | } else { |
479 | $value=$elementclone->getLabel(); |
480 | $elementclone->setLabel(str_replace('{no}', ($i+1), $value)); |
ebd3c7ac |
481 | |
482 | } |
7b41a4a9 |
483 | |
ebd3c7ac |
484 | $mform->addElement($elementclone); |
485 | } |
486 | } |
487 | for ($i=0; $i<$repeats; $i++) { |
488 | foreach ($options as $elementname => $elementoptions){ |
489 | $pos=strpos($elementname, '['); |
490 | if ($pos!==FALSE){ |
491 | $realelementname = substr($elementname, 0, $pos+1)."[$i]"; |
492 | $realelementname .= substr($elementname, $pos+1); |
493 | }else { |
494 | $realelementname = $elementname."[$i]"; |
495 | } |
496 | foreach ($elementoptions as $option => $params){ |
497 | |
498 | switch ($option){ |
499 | case 'default' : |
500 | $mform->setDefault($realelementname, $params); |
501 | break; |
ebd3c7ac |
502 | case 'helpbutton' : |
503 | $mform->setHelpButton($realelementname, $params); |
504 | break; |
505 | case 'disabledif' : |
9aa022fe |
506 | $params = array_merge(array($realelementname), $params); |
507 | call_user_func_array(array(&$mform, 'disabledIf'), $params); |
508 | break; |
509 | case 'rule' : |
510 | if (is_string($params)){ |
511 | $params = array(null, $params, null, 'client'); |
512 | } |
513 | $params = array_merge(array($realelementname), $params); |
514 | call_user_func_array(array(&$mform, 'addRule'), $params); |
ebd3c7ac |
515 | break; |
516 | |
517 | } |
518 | } |
519 | } |
520 | } |
271ffe3f |
521 | $mform->addElement('submit', $addfieldsname, $addstring); |
a23f0aaf |
522 | |
523 | $mform->closeHeaderBefore($addfieldsname); |
ebd3c7ac |
524 | |
19194f82 |
525 | return $repeats; |
ebd3c7ac |
526 | } |
a23f0aaf |
527 | /** |
1d284fbd |
528 | * Use this method to a cancel and submit button to the end of your form. Pass a param of false |
a23f0aaf |
529 | * if you don't want a cancel button in your form. If you have a cancel button make sure you |
530 | * check for it being pressed using is_cancelled() and redirecting if it is true before trying to |
beac4717 |
531 | * get data with get_data(). |
a23f0aaf |
532 | * |
533 | * @param boolean $cancel whether to show cancel button, default true |
534 | * @param boolean $revert whether to show revert button, default true |
535 | * @param string $submitlabel label for submit button, defaults to get_string('savechanges') |
536 | */ |
1d284fbd |
537 | function add_action_buttons($cancel = true, $submitlabel=null){ |
a23f0aaf |
538 | if (is_null($submitlabel)){ |
539 | $submitlabel = get_string('savechanges'); |
540 | } |
541 | $mform =& $this->_form; |
1d284fbd |
542 | if ($cancel){ |
543 | //when two elements we need a group |
a23f0aaf |
544 | $buttonarray=array(); |
545 | $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel); |
1d284fbd |
546 | $buttonarray[] = &$mform->createElement('cancel'); |
a23f0aaf |
547 | $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); |
cbdb3705 |
548 | $mform->closeHeaderBefore('buttonar'); |
a23f0aaf |
549 | } else { |
550 | //no group needed |
551 | $mform->addElement('submit', 'submitbutton', $submitlabel); |
cbdb3705 |
552 | $mform->closeHeaderBefore('submitbutton'); |
a23f0aaf |
553 | } |
554 | } |
7f40a229 |
555 | } |
556 | |
da1320da |
557 | /** |
558 | * You never extend this class directly. The class methods of this class are available from |
559 | * the private $this->_form property on moodleform and it's children. You generally only |
560 | * call methods on this class from within abstract methods that you override on moodleform such |
561 | * as definition and definition_after_data |
562 | * |
563 | */ |
7f40a229 |
564 | class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { |
565 | var $_types = array(); |
50ef8eb9 |
566 | var $_dependencies = array(); |
19110c57 |
567 | /** |
568 | * Array of buttons that if pressed do not result in the processing of the form. |
569 | * |
570 | * @var array |
571 | */ |
572 | var $_noSubmitButtons=array(); |
573 | /** |
574 | * Array of buttons that if pressed do not result in the processing of the form. |
575 | * |
576 | * @var array |
577 | */ |
578 | var $_cancelButtons=array(); |
7f40a229 |
579 | |
19194f82 |
580 | /** |
581 | * Array whose keys are element names. If the key exists this is a advanced element |
582 | * |
583 | * @var array |
584 | */ |
585 | var $_advancedElements = array(); |
586 | |
587 | /** |
588 | * Whether to display advanced elements (on page load) |
589 | * |
590 | * @var boolean |
591 | */ |
592 | var $_showAdvanced = null; |
593 | |
f07b9627 |
594 | /** |
595 | * The form name is derrived from the class name of the wrapper minus the trailing form |
596 | * It is a name with words joined by underscores whereas the id attribute is words joined by |
597 | * underscores. |
598 | * |
599 | * @var unknown_type |
600 | */ |
601 | var $_formName = ''; |
43914931 |
602 | |
da6f8763 |
603 | /** |
604 | * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless |
605 | * @param string $formName Form's name. |
606 | * @param string $method (optional)Form's method defaults to 'POST' |
cbdb3705 |
607 | * @param string $action (optional)Form's action |
da6f8763 |
608 | * @param string $target (optional)Form's target defaults to none |
609 | * @param mixed $attributes (optional)Extra attributes for <form> tag |
610 | * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field |
611 | * @access public |
612 | */ |
7f40a229 |
613 | function MoodleQuickForm($formName, $method, $action, $target='', $attributes=null){ |
dcf6d93c |
614 | global $CFG; |
615 | |
271ffe3f |
616 | static $formcounter = 1; |
7f40a229 |
617 | |
da6f8763 |
618 | HTML_Common::HTML_Common($attributes); |
da6f8763 |
619 | $target = empty($target) ? array() : array('target' => $target); |
f07b9627 |
620 | $this->_formName = $formName; |
da6f8763 |
621 | //no 'name' atttribute for form in xhtml strict : |
271ffe3f |
622 | $attributes = array('action'=>$action, 'method'=>$method, 'id'=>'mform'.$formcounter) + $target; |
623 | $formcounter++; |
da6f8763 |
624 | $this->updateAttributes($attributes); |
da6f8763 |
625 | |
7f40a229 |
626 | //this is custom stuff for Moodle : |
da6f8763 |
627 | $oldclass= $this->getAttribute('class'); |
628 | if (!empty($oldclass)){ |
629 | $this->updateAttributes(array('class'=>$oldclass.' mform')); |
630 | }else { |
80f962df |
631 | $this->updateAttributes(array('class'=>'mform')); |
da6f8763 |
632 | } |
653f890d |
633 | $this->_reqHTML = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.get_string('requiredelement', 'form').'" src="'.$CFG->pixpath.'/req.gif'.'" />'; |
634 | $this->_advancedHTML = '<img class="adv" title="'.get_string('advancedelement', 'form').'" alt="'.get_string('advancedelement', 'form').'" src="'.$CFG->pixpath.'/adv.gif'.'" />'; |
5bff0855 |
635 | $this->setRequiredNote(get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').'" src="'.$CFG->pixpath.'/req.gif'.'" />')); |
636 | //(Help file doesn't add anything) helpbutton('requiredelement', get_string('requiredelement', 'form'), 'moodle', true, false, '', true)); |
19194f82 |
637 | } |
638 | |
a23f0aaf |
639 | /** |
640 | * Use this method to indicate an element in a form is an advanced field. If items in a form |
641 | * are marked as advanced then 'Hide/Show Advanced' buttons will automatically be displayed in the |
642 | * form so the user can decide whether to display advanced form controls. |
643 | * |
644 | * If you set a header element to advanced then all elements it contains will also be set as advanced. |
645 | * |
646 | * @param string $elementName group or element name (not the element name of something inside a group). |
647 | * @param boolean $advanced default true sets the element to advanced. False removes advanced mark. |
648 | */ |
649 | function setAdvanced($elementName, $advanced=true){ |
650 | if ($advanced){ |
651 | $this->_advancedElements[$elementName]=''; |
652 | } elseif (isset($this->_advancedElements[$elementName])) { |
653 | unset($this->_advancedElements[$elementName]); |
654 | } |
655 | if ($advanced && $this->getElementType('mform_showadvanced_last')===false){ |
656 | $this->setShowAdvanced(); |
657 | $this->registerNoSubmitButton('mform_showadvanced'); |
658 | |
659 | $this->addElement('hidden', 'mform_showadvanced_last'); |
660 | } |
661 | } |
662 | /** |
663 | * Set whether to show advanced elements in the form on first displaying form. Default is not to |
664 | * display advanced elements in the form until 'Show Advanced' is pressed. |
665 | * |
666 | * You can get the last state of the form and possibly save it for this user by using |
667 | * value 'mform_showadvanced_last' in submitted data. |
668 | * |
669 | * @param boolean $showadvancedNow |
670 | */ |
671 | function setShowAdvanced($showadvancedNow = null){ |
672 | if ($showadvancedNow === null){ |
673 | if ($this->_showAdvanced !== null){ |
674 | return; |
675 | } else { //if setShowAdvanced is called without any preference |
676 | //make the default to not show advanced elements. |
f07b9627 |
677 | $showadvancedNow = get_user_preferences( |
678 | moodle_strtolower($this->_formName.'_showadvanced', 0)); |
a23f0aaf |
679 | } |
a23f0aaf |
680 | } |
681 | //value of hidden element |
682 | $hiddenLast = optional_param('mform_showadvanced_last', -1, PARAM_INT); |
683 | //value of button |
684 | $buttonPressed = optional_param('mform_showadvanced', 0, PARAM_RAW); |
685 | //toggle if button pressed or else stay the same |
686 | if ($hiddenLast == -1) { |
687 | $next = $showadvancedNow; |
688 | } elseif ($buttonPressed) { //toggle on button press |
689 | $next = !$hiddenLast; |
690 | } else { |
691 | $next = $hiddenLast; |
692 | } |
693 | $this->_showAdvanced = $next; |
f07b9627 |
694 | if ($showadvancedNow != $next){ |
695 | set_user_preference($this->_formName.'_showadvanced', $next); |
696 | } |
a23f0aaf |
697 | $this->setConstants(array('mform_showadvanced_last'=>$next)); |
19194f82 |
698 | } |
699 | function getShowAdvanced(){ |
700 | return $this->_showAdvanced; |
701 | } |
702 | |
19194f82 |
703 | |
704 | /** |
705 | * Accepts a renderer |
706 | * |
707 | * @param HTML_QuickForm_Renderer An HTML_QuickForm_Renderer object |
708 | * @since 3.0 |
709 | * @access public |
710 | * @return void |
711 | */ |
712 | function accept(&$renderer) |
713 | { |
714 | if (method_exists($renderer, 'setAdvancedElements')){ |
715 | //check for visible fieldsets where all elements are advanced |
716 | //and mark these headers as advanced as well. |
717 | //And mark all elements in a advanced header as advanced |
718 | $stopFields = $renderer->getStopFieldSetElements(); |
719 | $lastHeader = null; |
720 | $lastHeaderAdvanced = false; |
721 | $anyAdvanced = false; |
722 | foreach (array_keys($this->_elements) as $elementIndex){ |
723 | $element =& $this->_elements[$elementIndex]; |
724 | if ($element->getType()=='header' || in_array($element->getName(), $stopFields)){ |
725 | if ($anyAdvanced && ($lastHeader!==null)){ |
726 | $this->setAdvanced($lastHeader->getName()); |
727 | } |
728 | $lastHeaderAdvanced = false; |
729 | } elseif ($lastHeaderAdvanced) { |
730 | $this->setAdvanced($element->getName()); |
731 | } |
732 | if ($element->getType()=='header'){ |
733 | $lastHeader =& $element; |
734 | $anyAdvanced = false; |
735 | $lastHeaderAdvanced = isset($this->_advancedElements[$element->getName()]); |
736 | } elseif (isset($this->_advancedElements[$element->getName()])){ |
737 | $anyAdvanced = true; |
738 | } |
739 | } |
740 | $renderer->setAdvancedElements($this->_advancedElements); |
19194f82 |
741 | |
19194f82 |
742 | } |
743 | parent::accept($renderer); |
744 | } |
745 | |
19194f82 |
746 | |
19194f82 |
747 | |
748 | function closeHeaderBefore($elementName){ |
749 | $renderer =& $this->defaultRenderer(); |
750 | $renderer->addStopFieldsetElements($elementName); |
da6f8763 |
751 | } |
bb40325e |
752 | |
da1320da |
753 | /** |
754 | * Should be used for all elements of a form except for select, radio and checkboxes which |
755 | * clean their own data. |
756 | * |
757 | * @param string $elementname |
758 | * @param integer $paramtype use the constants PARAM_*. |
759 | * * PARAM_CLEAN is deprecated and you should try to use a more specific type. |
760 | * * PARAM_TEXT should be used for cleaning data that is expected to be plain text. |
761 | * It will strip all html tags. But will still let tags for multilang support |
762 | * through. |
763 | * * PARAM_RAW means no cleaning whatsoever, it is used mostly for data from the |
764 | * html editor. Data from the editor is later cleaned before display using |
765 | * format_text() function. PARAM_RAW can also be used for data that is validated |
766 | * by some other way or printed by p() or s(). |
767 | * * PARAM_INT should be used for integers. |
768 | * * PARAM_ACTION is an alias of PARAM_ALPHA and is used for hidden fields specifying |
769 | * form actions. |
770 | */ |
7f40a229 |
771 | function setType($elementname, $paramtype) { |
772 | $this->_types[$elementname] = $paramtype; |
773 | } |
49292f8c |
774 | |
da1320da |
775 | /** |
776 | * See description of setType above. This can be used to set several types at once. |
777 | * |
778 | * @param array $paramtypes |
779 | */ |
c56f1826 |
780 | function setTypes($paramtypes) { |
781 | $this->_types = $paramtypes + $this->_types; |
782 | } |
49292f8c |
783 | |
784 | function updateSubmission($submission, $files) { |
785 | $this->_flagSubmitted = false; |
786 | |
7f40a229 |
787 | if (empty($submission)) { |
788 | $this->_submitValues = array(); |
7f40a229 |
789 | } else { |
790 | foreach ($submission as $key=>$s) { |
791 | if (array_key_exists($key, $this->_types)) { |
792 | $submission[$key] = clean_param($s, $this->_types[$key]); |
793 | } |
794 | } |
795 | $this->_submitValues = $this->_recursiveFilter('stripslashes', $submission); |
796 | $this->_flagSubmitted = true; |
797 | } |
798 | |
49292f8c |
799 | if (empty($files)) { |
800 | $this->_submitFiles = array(); |
801 | } else { |
802 | if (1 == get_magic_quotes_gpc()) { |
3a298174 |
803 | foreach (array_keys($files) as $elname) { |
49292f8c |
804 | // dangerous characters in filenames are cleaned later in upload_manager |
805 | $files[$elname]['name'] = stripslashes($files[$elname]['name']); |
806 | } |
807 | } |
808 | $this->_submitFiles = $files; |
809 | $this->_flagSubmitted = true; |
810 | } |
811 | |
2c412890 |
812 | // need to tell all elements that they need to update their value attribute. |
813 | foreach (array_keys($this->_elements) as $key) { |
814 | $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this); |
815 | } |
7f40a229 |
816 | } |
817 | |
da6f8763 |
818 | function getReqHTML(){ |
819 | return $this->_reqHTML; |
820 | } |
7f40a229 |
821 | |
19194f82 |
822 | function getAdvancedHTML(){ |
823 | return $this->_advancedHTML; |
824 | } |
825 | |
7f40a229 |
826 | /** |
da1320da |
827 | * Initializes a default form value. Used to specify the default for a new entry where |
beac4717 |
828 | * no data is loaded in using moodleform::set_data() |
7f40a229 |
829 | * |
830 | * @param string $elementname element name |
831 | * @param mixed $values values for that element name |
832 | * @param bool $slashed the default value is slashed |
833 | * @access public |
834 | * @return void |
835 | */ |
836 | function setDefault($elementName, $defaultValue, $slashed=false){ |
837 | $filter = $slashed ? 'stripslashes' : NULL; |
838 | $this->setDefaults(array($elementName=>$defaultValue), $filter); |
839 | } // end func setDefault |
da6f8763 |
840 | /** |
c56f1826 |
841 | * Add an array of buttons to the form |
7f40a229 |
842 | * @param array $buttons An associative array representing help button to attach to |
da6f8763 |
843 | * to the form. keys of array correspond to names of elements in form. |
7f40a229 |
844 | * |
da6f8763 |
845 | * @access public |
846 | */ |
d4fe14d3 |
847 | function setHelpButtons($buttons, $suppresscheck=false, $function='helpbutton'){ |
7f40a229 |
848 | |
c56f1826 |
849 | foreach ($buttons as $elementname => $button){ |
d4fe14d3 |
850 | $this->setHelpButton($elementname, $button, $suppresscheck, $function); |
da6f8763 |
851 | } |
852 | } |
c56f1826 |
853 | /** |
da1320da |
854 | * Add a single button. |
c56f1826 |
855 | * |
856 | * @param string $elementname name of the element to add the item to |
d4fe14d3 |
857 | * @param array $button - arguments to pass to function $function |
c56f1826 |
858 | * @param boolean $suppresscheck - whether to throw an error if the element |
859 | * doesn't exist. |
d4fe14d3 |
860 | * @param string $function - function to generate html from the arguments in $button |
c56f1826 |
861 | */ |
d4fe14d3 |
862 | function setHelpButton($elementname, $button, $suppresscheck=false, $function='helpbutton'){ |
c56f1826 |
863 | if (array_key_exists($elementname, $this->_elementIndex)){ |
864 | //_elements has a numeric index, this code accesses the elements by name |
865 | $element=&$this->_elements[$this->_elementIndex[$elementname]]; |
866 | if (method_exists($element, 'setHelpButton')){ |
d4fe14d3 |
867 | $element->setHelpButton($button, $function); |
c56f1826 |
868 | }else{ |
869 | $a=new object(); |
870 | $a->name=$element->getName(); |
871 | $a->classname=get_class($element); |
872 | print_error('nomethodforaddinghelpbutton', 'form', '', $a); |
873 | } |
874 | }elseif (!$suppresscheck){ |
875 | print_error('nonexistentformelements', 'form', '', $elementname); |
2c412890 |
876 | } |
c56f1826 |
877 | } |
7f40a229 |
878 | |
42f248e6 |
879 | function exportValues($elementList= null, $addslashes=true){ |
0ffb4cc7 |
880 | $unfiltered = array(); |
881 | if (null === $elementList) { |
882 | // iterate over all elements, calling their exportValue() methods |
98af2d1d |
883 | $emptyarray = array(); |
0ffb4cc7 |
884 | foreach (array_keys($this->_elements) as $key) { |
885 | if ($this->_elements[$key]->isFrozen() && !$this->_elements[$key]->_persistantFreeze){ |
98af2d1d |
886 | $value = $this->_elements[$key]->exportValue($emptyarray, true); |
0ffb4cc7 |
887 | } else { |
888 | $value = $this->_elements[$key]->exportValue($this->_submitValues, true); |
889 | } |
890 | |
891 | if (is_array($value)) { |
892 | // This shit throws a bogus warning in PHP 4.3.x |
893 | $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value); |
894 | } |
895 | } |
896 | } else { |
897 | if (!is_array($elementList)) { |
898 | $elementList = array_map('trim', explode(',', $elementList)); |
899 | } |
900 | foreach ($elementList as $elementName) { |
901 | $value = $this->exportValue($elementName); |
902 | if (PEAR::isError($value)) { |
903 | return $value; |
904 | } |
905 | $unfiltered[$elementName] = $value; |
906 | } |
907 | } |
7f40a229 |
908 | |
da6f8763 |
909 | if ($addslashes){ |
0ffb4cc7 |
910 | return $this->_recursiveFilter('addslashes', $unfiltered); |
da6f8763 |
911 | } else { |
912 | return $unfiltered; |
913 | } |
914 | } |
f07b9627 |
915 | /** |
916 | * Adds a validation rule for the given field |
917 | * |
918 | * If the element is in fact a group, it will be considered as a whole. |
919 | * To validate grouped elements as separated entities, |
920 | * use addGroupRule instead of addRule. |
921 | * |
922 | * @param string $element Form element name |
923 | * @param string $message Message to display for invalid data |
924 | * @param string $type Rule type, use getRegisteredRules() to get types |
925 | * @param string $format (optional)Required for extra rule data |
926 | * @param string $validation (optional)Where to perform validation: "server", "client" |
927 | * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error? |
928 | * @param boolean $force Force the rule to be applied, even if the target form element does not exist |
929 | * @since 1.0 |
930 | * @access public |
931 | * @throws HTML_QuickForm_Error |
932 | */ |
933 | function addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false) |
934 | { |
935 | parent::addRule($element, $message, $type, $format, $validation, $reset, $force); |
936 | if ($validation == 'client') { |
937 | $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);')); |
938 | } |
939 | |
940 | } // end func addRule |
941 | /** |
942 | * Adds a validation rule for the given group of elements |
943 | * |
944 | * Only groups with a name can be assigned a validation rule |
945 | * Use addGroupRule when you need to validate elements inside the group. |
946 | * Use addRule if you need to validate the group as a whole. In this case, |
947 | * the same rule will be applied to all elements in the group. |
948 | * Use addRule if you need to validate the group against a function. |
949 | * |
950 | * @param string $group Form group name |
951 | * @param mixed $arg1 Array for multiple elements or error message string for one element |
952 | * @param string $type (optional)Rule type use getRegisteredRules() to get types |
953 | * @param string $format (optional)Required for extra rule data |
954 | * @param int $howmany (optional)How many valid elements should be in the group |
955 | * @param string $validation (optional)Where to perform validation: "server", "client" |
956 | * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed. |
957 | * @since 2.5 |
958 | * @access public |
959 | * @throws HTML_QuickForm_Error |
960 | */ |
961 | function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false) |
962 | { |
963 | parent::addGroupRule($group, $arg1, $type, $format, $howmany, $validation, $reset); |
964 | if (is_array($arg1)) { |
3a298174 |
965 | foreach ($arg1 as $rules) { |
f07b9627 |
966 | foreach ($rules as $rule) { |
967 | $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server'; |
968 | |
969 | if ('client' == $validation) { |
970 | $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);')); |
971 | } |
972 | } |
973 | } |
974 | } elseif (is_string($arg1)) { |
975 | |
976 | if ($validation == 'client') { |
3a298174 |
977 | $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);')); |
f07b9627 |
978 | } |
979 | } |
980 | } // end func addGroupRule |
981 | |
982 | // }}} |
5bc97c98 |
983 | /** |
984 | * Returns the client side validation script |
985 | * |
986 | * The code here was copied from HTML_QuickForm_DHTMLRulesTableless who copied it from HTML_QuickForm |
987 | * and slightly modified to run rules per-element |
988 | * Needed to override this because of an error with client side validation of grouped elements. |
989 | * |
990 | * @access public |
991 | * @return string Javascript to perform validation, empty string if no 'client' rules were added |
992 | */ |
993 | function getValidationScript() |
994 | { |
995 | if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) { |
996 | return ''; |
997 | } |
998 | |
999 | include_once('HTML/QuickForm/RuleRegistry.php'); |
1000 | $registry =& HTML_QuickForm_RuleRegistry::singleton(); |
1001 | $test = array(); |
1002 | $js_escape = array( |
1003 | "\r" => '\r', |
1004 | "\n" => '\n', |
1005 | "\t" => '\t', |
1006 | "'" => "\\'", |
1007 | '"' => '\"', |
1008 | '\\' => '\\\\' |
1009 | ); |
1010 | |
1011 | foreach ($this->_rules as $elementName => $rules) { |
1012 | foreach ($rules as $rule) { |
1013 | if ('client' == $rule['validation']) { |
da1320da |
1014 | unset($element); //TODO: find out how to properly initialize it |
5bc97c98 |
1015 | |
1016 | $dependent = isset($rule['dependent']) && is_array($rule['dependent']); |
1017 | $rule['message'] = strtr($rule['message'], $js_escape); |
1018 | |
1019 | if (isset($rule['group'])) { |
1020 | $group =& $this->getElement($rule['group']); |
1021 | // No JavaScript validation for frozen elements |
1022 | if ($group->isFrozen()) { |
1023 | continue 2; |
1024 | } |
1025 | $elements =& $group->getElements(); |
1026 | foreach (array_keys($elements) as $key) { |
1027 | if ($elementName == $group->getElementName($key)) { |
1028 | $element =& $elements[$key]; |
1029 | break; |
1030 | } |
1031 | } |
1032 | } elseif ($dependent) { |
1033 | $element = array(); |
1034 | $element[] =& $this->getElement($elementName); |
3a298174 |
1035 | foreach ($rule['dependent'] as $elName) { |
5bc97c98 |
1036 | $element[] =& $this->getElement($elName); |
1037 | } |
1038 | } else { |
1039 | $element =& $this->getElement($elementName); |
1040 | } |
1041 | // No JavaScript validation for frozen elements |
1042 | if (is_object($element) && $element->isFrozen()) { |
1043 | continue 2; |
1044 | } elseif (is_array($element)) { |
1045 | foreach (array_keys($element) as $key) { |
1046 | if ($element[$key]->isFrozen()) { |
1047 | continue 3; |
1048 | } |
1049 | } |
1050 | } |
1051 | // Fix for bug displaying errors for elements in a group |
1052 | //$test[$elementName][] = $registry->getValidationScript($element, $elementName, $rule); |
1053 | $test[$elementName][0][] = $registry->getValidationScript($element, $elementName, $rule); |
1054 | $test[$elementName][1]=$element; |
1055 | //end of fix |
1056 | } |
1057 | } |
1058 | } |
7c77033f |
1059 | |
1060 | // Fix for MDL-9524. If you don't do this, then $element may be left as a reference to one of the fields in |
1061 | // the form, and then that form field gets corrupted by the code that follows. |
1062 | unset($element); |
1063 | |
5bc97c98 |
1064 | $js = ' |
1065 | <script type="text/javascript"> |
1066 | //<![CDATA[ |
1cbb09f1 |
1067 | |
1068 | var skipClientValidation = false; |
1069 | |
5bc97c98 |
1070 | function qf_errorHandler(element, _qfMsg) { |
1071 | div = element.parentNode; |
1072 | if (_qfMsg != \'\') { |
e35c9eeb |
1073 | var errorSpan = document.getElementById(\'id_error_\'+element.name); |
e7004d05 |
1074 | if (!errorSpan) { |
1075 | errorSpan = document.createElement("span"); |
e35c9eeb |
1076 | errorSpan.id = \'id_error_\'+element.name; |
1077 | errorSpan.className = "error"; |
fed13a5e |
1078 | element.parentNode.insertBefore(errorSpan, element.parentNode.firstChild); |
5bc97c98 |
1079 | } |
fed13a5e |
1080 | |
e7004d05 |
1081 | while (errorSpan.firstChild) { |
1082 | errorSpan.removeChild(errorSpan.firstChild); |
5bc97c98 |
1083 | } |
2c412890 |
1084 | |
e7004d05 |
1085 | errorSpan.appendChild(document.createTextNode(_qfMsg.substring(3))); |
e35c9eeb |
1086 | errorSpan.appendChild(document.createElement("br")); |
5bc97c98 |
1087 | |
1088 | if (div.className.substr(div.className.length - 6, 6) != " error" |
1089 | && div.className != "error") { |
1090 | div.className += " error"; |
1091 | } |
1092 | |
1093 | return false; |
1094 | } else { |
e35c9eeb |
1095 | var errorSpan = document.getElementById(\'id_error_\'+element.name); |
e7004d05 |
1096 | if (errorSpan) { |
1097 | errorSpan.parentNode.removeChild(errorSpan); |
5bc97c98 |
1098 | } |
1099 | |
1100 | if (div.className.substr(div.className.length - 6, 6) == " error") { |
1101 | div.className = div.className.substr(0, div.className.length - 6); |
1102 | } else if (div.className == "error") { |
1103 | div.className = ""; |
1104 | } |
1105 | |
1106 | return true; |
1107 | } |
1108 | }'; |
1109 | $validateJS = ''; |
1110 | foreach ($test as $elementName => $jsandelement) { |
1111 | // Fix for bug displaying errors for elements in a group |
1112 | //unset($element); |
1113 | list($jsArr,$element)=$jsandelement; |
1114 | //end of fix |
1115 | $js .= ' |
f07b9627 |
1116 | function validate_' . $this->_formName . '_' . $elementName . '(element) { |
5bc97c98 |
1117 | var value = \'\'; |
1118 | var errFlag = new Array(); |
1119 | var _qfGroups = {}; |
1120 | var _qfMsg = \'\'; |
1121 | var frm = element.parentNode; |
1122 | while (frm && frm.nodeName != "FORM") { |
1123 | frm = frm.parentNode; |
1124 | } |
1125 | ' . join("\n", $jsArr) . ' |
1126 | return qf_errorHandler(element, _qfMsg); |
1127 | } |
1128 | '; |
1129 | $validateJS .= ' |
2ef7c374 |
1130 | ret = validate_' . $this->_formName . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret; |
1131 | if (!ret && !first_focus) { |
1132 | first_focus = true; |
1133 | frm.elements[\''.$elementName.'\'].focus(); |
1134 | } |
1135 | '; |
cbdb3705 |
1136 | |
5bc97c98 |
1137 | // Fix for bug displaying errors for elements in a group |
1138 | //unset($element); |
1139 | //$element =& $this->getElement($elementName); |
1140 | //end of fix |
f07b9627 |
1141 | $valFunc = 'validate_' . $this->_formName . '_' . $elementName . '(this)'; |
5bc97c98 |
1142 | $onBlur = $element->getAttribute('onBlur'); |
1143 | $onChange = $element->getAttribute('onChange'); |
1144 | $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, |
1145 | 'onChange' => $onChange . $valFunc)); |
1146 | } |
e7004d05 |
1147 | // do not rely on frm function parameter, because htmlarea breaks it when overloading the onsubmit method |
5bc97c98 |
1148 | $js .= ' |
f07b9627 |
1149 | function validate_' . $this->_formName . '(frm) { |
1cbb09f1 |
1150 | if (skipClientValidation) { |
1151 | return true; |
1152 | } |
5bc97c98 |
1153 | var ret = true; |
cbdb3705 |
1154 | |
0befbdfd |
1155 | var frm = document.getElementById(\''. $this->_attributes['id'] .'\') |
2ef7c374 |
1156 | var first_focus = false; |
5bc97c98 |
1157 | ' . $validateJS . '; |
1158 | return ret; |
1159 | } |
1160 | //]]> |
1161 | </script>'; |
1162 | return $js; |
1163 | } // end func getValidationScript |
1164 | function _setDefaultRuleMessages(){ |
1165 | foreach ($this->_rules as $field => $rulesarr){ |
1166 | foreach ($rulesarr as $key => $rule){ |
1167 | if ($rule['message']===null){ |
1168 | $a=new object(); |
1169 | $a->format=$rule['format']; |
1170 | $str=get_string('err_'.$rule['type'], 'form', $a); |
1171 | if (strpos($str, '[[')!==0){ |
1172 | $this->_rules[$field][$key]['message']=$str; |
2c412890 |
1173 | } |
5bc97c98 |
1174 | } |
1175 | } |
1176 | } |
1177 | } |
bb40325e |
1178 | |
d01a38cb |
1179 | function getLockOptionEndScript(){ |
dd07bbac |
1180 | |
1181 | $iname = $this->getAttribute('id').'items'; |
f4ba7e1a |
1182 | $js = '<script type="text/javascript">'."\n"; |
5e87b920 |
1183 | $js .= '//<![CDATA['."\n"; |
dd07bbac |
1184 | $js .= "var $iname = Array();\n"; |
1185 | |
1186 | foreach ($this->_dependencies as $dependentOn => $conditions){ |
1187 | $js .= "{$iname}['$dependentOn'] = Array();\n"; |
1188 | foreach ($conditions as $condition=>$values) { |
1189 | $js .= "{$iname}['$dependentOn']['$condition'] = Array();\n"; |
1190 | foreach ($values as $value=>$dependents) { |
1191 | $js .= "{$iname}['$dependentOn']['$condition']['$value'] = Array();\n"; |
1192 | $i = 0; |
1193 | foreach ($dependents as $dependent) { |
1194 | $elements = $this->_getElNamesRecursive($dependent); |
1195 | foreach($elements as $element) { |
1196 | if ($element == $dependentOn) { |
1197 | continue; |
1198 | } |
1199 | $js .= "{$iname}['$dependentOn']['$condition']['$value'][$i]='$element';\n"; |
1200 | $i++; |
1201 | } |
11f260f4 |
1202 | } |
1203 | } |
50ef8eb9 |
1204 | } |
dd07bbac |
1205 | } |
d01a38cb |
1206 | $js .="lockoptionsallsetup('".$this->getAttribute('id')."');\n"; |
5e87b920 |
1207 | $js .='//]]>'."\n"; |
50ef8eb9 |
1208 | $js .='</script>'."\n"; |
1209 | return $js; |
bb40325e |
1210 | } |
d01a38cb |
1211 | |
dd07bbac |
1212 | function _getElNamesRecursive($element, $group=null){ |
d01a38cb |
1213 | if ($group==null){ |
9403060a |
1214 | $el = $this->getElement($element); |
d01a38cb |
1215 | } else { |
9403060a |
1216 | $el = &$element; |
d01a38cb |
1217 | } |
1218 | if (is_a($el, 'HTML_QuickForm_group')){ |
9403060a |
1219 | $group = $el; |
1220 | $elsInGroup = $group->getElements(); |
1221 | $elNames = array(); |
d01a38cb |
1222 | foreach ($elsInGroup as $elInGroup){ |
1223 | $elNames = array_merge($elNames, $this->_getElNamesRecursive($elInGroup, $group)); |
1224 | } |
1225 | }else{ |
9403060a |
1226 | if ($group != null){ |
1227 | $elNames = array($group->getElementName($el->getName())); |
1228 | } elseif (is_a($el, 'HTML_QuickForm_header')) { |
1229 | return null; |
988d1202 |
1230 | } elseif (is_a($el, 'HTML_QuickForm_hidden')) { |
1231 | return null; |
2dbd6409 |
1232 | } elseif (method_exists($el, 'getPrivateName')) { |
1233 | return array($el->getPrivateName()); |
d01a38cb |
1234 | } else { |
9403060a |
1235 | $elNames = array($el->getName()); |
d01a38cb |
1236 | } |
1237 | } |
1238 | return $elNames; |
1239 | |
50ef8eb9 |
1240 | } |
6e372b25 |
1241 | /** |
1242 | * Adds a dependency for $elementName which will be disabled if $condition is met. |
9403060a |
1243 | * If $condition = 'notchecked' (default) then the condition is that the $dependentOn element |
1244 | * is not checked. If $condition = 'checked' then the condition is that the $dependentOn element |
6e372b25 |
1245 | * is checked. If $condition is something else then it is checked to see if the value |
1246 | * of the $dependentOn element is equal to $condition. |
1247 | * |
1248 | * @param string $elementName the name of the element which will be disabled |
1249 | * @param string $dependentOn the name of the element whose state will be checked for |
1250 | * condition |
1251 | * @param string $condition the condition to check |
19110c57 |
1252 | * @param mixed $value used in conjunction with condition. |
6e372b25 |
1253 | */ |
dd07bbac |
1254 | function disabledIf($elementName, $dependentOn, $condition = 'notchecked', $value='1'){ |
1255 | if (!array_key_exists($dependentOn, $this->_dependencies)) { |
1256 | $this->_dependencies[$dependentOn] = array(); |
1257 | } |
1258 | if (!array_key_exists($condition, $this->_dependencies[$dependentOn])) { |
1259 | $this->_dependencies[$dependentOn][$condition] = array(); |
1260 | } |
1261 | if (!array_key_exists($value, $this->_dependencies[$dependentOn][$condition])) { |
1262 | $this->_dependencies[$dependentOn][$condition][$value] = array(); |
1263 | } |
1264 | $this->_dependencies[$dependentOn][$condition][$value][] = $elementName; |
bb40325e |
1265 | } |
dd07bbac |
1266 | |
a23f0aaf |
1267 | function registerNoSubmitButton($buttonname){ |
1268 | $this->_noSubmitButtons[]=$buttonname; |
1269 | } |
dd07bbac |
1270 | |
a23f0aaf |
1271 | function isNoSubmitButton($buttonname){ |
1272 | return (array_search($buttonname, $this->_noSubmitButtons)!==FALSE); |
19110c57 |
1273 | } |
dd07bbac |
1274 | |
19110c57 |
1275 | function _registerCancelButton($addfieldsname){ |
1276 | $this->_cancelButtons[]=$addfieldsname; |
1277 | } |
acc9c3e0 |
1278 | /** |
1279 | * Displays elements without HTML input tags. |
1280 | * This method is different to freeze() in that it makes sure no hidden |
1281 | * elements are included in the form. And a 'hardFrozen' element's submitted value is |
1282 | * ignored. |
1283 | * |
4af06dda |
1284 | * This function also removes all previously defined rules. |
1285 | * |
acc9c3e0 |
1286 | * @param mixed $elementList array or string of element(s) to be frozen |
1287 | * @since 1.0 |
1288 | * @access public |
1289 | * @throws HTML_QuickForm_Error |
1290 | */ |
1291 | function hardFreeze($elementList=null) |
1292 | { |
1293 | if (!isset($elementList)) { |
1294 | $this->_freezeAll = true; |
1295 | $elementList = array(); |
1296 | } else { |
1297 | if (!is_array($elementList)) { |
1298 | $elementList = preg_split('/[ ]*,[ ]*/', $elementList); |
1299 | } |
1300 | $elementList = array_flip($elementList); |
1301 | } |
1302 | |
1303 | foreach (array_keys($this->_elements) as $key) { |
1304 | $name = $this->_elements[$key]->getName(); |
1305 | if ($this->_freezeAll || isset($elementList[$name])) { |
1306 | $this->_elements[$key]->freeze(); |
1307 | $this->_elements[$key]->setPersistantFreeze(false); |
1308 | unset($elementList[$name]); |
4af06dda |
1309 | |
1310 | // remove all rules |
1311 | $this->_rules[$name] = array(); |
1312 | // if field is required, remove the rule |
1313 | $unset = array_search($name, $this->_required); |
1314 | if ($unset !== false) { |
1315 | unset($this->_required[$unset]); |
1316 | } |
acc9c3e0 |
1317 | } |
1318 | } |
1319 | |
1320 | if (!empty($elementList)) { |
1321 | 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); |
1322 | } |
1323 | return true; |
cbdb3705 |
1324 | } // end func hardFreeze |
56015454 |
1325 | |
cbdb3705 |
1326 | // }}} |
da6f8763 |
1327 | } |
1328 | |
e24b7f85 |
1329 | |
da6f8763 |
1330 | /** |
7f40a229 |
1331 | * A renderer for MoodleQuickForm that only uses XHTML and CSS and no |
da6f8763 |
1332 | * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless |
7f40a229 |
1333 | * |
da6f8763 |
1334 | * Stylesheet is part of standard theme and should be automatically included. |
1335 | * |
1336 | * @author Jamie Pratt <me@jamiep.org> |
1337 | * @license gpl license |
1338 | */ |
7f40a229 |
1339 | class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ |
da6f8763 |
1340 | |
1341 | /** |
1342 | * Element template array |
1343 | * @var array |
1344 | * @access private |
1345 | */ |
1346 | var $_elementTemplates; |
49c53687 |
1347 | /** |
1348 | * Template used when opening a hidden fieldset |
1349 | * (i.e. a fieldset that is opened when there is no header element) |
1350 | * @var string |
1351 | * @access private |
1352 | */ |
1353 | var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">"; |
19194f82 |
1354 | /** |
1355 | * Header Template string |
1356 | * @var string |
1357 | * @access private |
1358 | */ |
1359 | var $_headerTemplate = |
a23f0aaf |
1360 | "\n\t\t<legend>{header}</legend>\n\t\t<div class=\"advancedbutton\">{advancedimg}{button}</div>\n\t\t"; |
7f40a229 |
1361 | |
49c53687 |
1362 | /** |
bc9ec4a6 |
1363 | * Template used when opening a fieldset |
1364 | * @var string |
1365 | * @access private |
1366 | */ |
1367 | var $_openFieldsetTemplate = "\n\t<fieldset class=\"clearfix\" {id}>"; |
1368 | |
1369 | /** |
49c53687 |
1370 | * Template used when closing a fieldset |
1371 | * @var string |
1372 | * @access private |
1373 | */ |
bc9ec4a6 |
1374 | var $_closeFieldsetTemplate = "\n\t\t</fieldset>"; |
42f248e6 |
1375 | |
49c53687 |
1376 | /** |
1377 | * Required Note template string |
1378 | * @var string |
1379 | * @access private |
1380 | */ |
7f40a229 |
1381 | var $_requiredNoteTemplate = |
6ba2c73d |
1382 | "\n\t\t<div class=\"fdescription required\">{requiredNote}</div>"; |
7f40a229 |
1383 | |
19194f82 |
1384 | var $_advancedElements = array(); |
1385 | |
1386 | /** |
1387 | * Whether to display advanced elements (on page load) |
1388 | * |
1389 | * @var integer 1 means show 0 means hide |
1390 | */ |
1391 | var $_showAdvanced; |
1392 | |
7f40a229 |
1393 | function MoodleQuickForm_Renderer(){ |
42f248e6 |
1394 | // switch next two lines for ol li containers for form items. |
49c7f3a8 |
1395 | // $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 |
1396 | $this->_elementTemplates = array( |
620cd35d |
1397 | 'default'=>"\n\t\t".'<div class="fitem {advanced}<!-- BEGIN required --> required<!-- END required -->"><div class="fitemtitle"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} </label>{help}</div><div class="felement {type}<!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div></div>', |
49c7f3a8 |
1398 | |
cbdb3705 |
1399 | 'fieldset'=>"\n\t\t".'<div class="fitem {advanced}<!-- BEGIN required --> required<!-- END required -->"><div class="fitemtitle"><div class="fgrouplabel">{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} </div>{help}</div><fieldset class="felement {type}<!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</fieldset></div>', |
43914931 |
1400 | |
cbdb3705 |
1401 | 'static'=>"\n\t\t".'<div class="fitem {advanced}<!-- BEGIN required --> required<!-- END required -->"><div class="fitemtitle"><div class="fstaticlabel">{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg} </div>{help}</div><div class="felement {type}<!-- BEGIN error --> error<!-- END error -->"><!-- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div></div>'); |
da6f8763 |
1402 | |
1403 | parent::HTML_QuickForm_Renderer_Tableless(); |
1404 | } |
7f40a229 |
1405 | |
19194f82 |
1406 | function setAdvancedElements($elements){ |
1407 | $this->_advancedElements = $elements; |
1408 | } |
1409 | |
1410 | /** |
1411 | * What to do when starting the form |
1412 | * |
1413 | * @param MoodleQuickForm $form |
1414 | */ |
da6f8763 |
1415 | function startForm(&$form){ |
9403060a |
1416 | $this->_reqHTML = $form->getReqHTML(); |
1417 | $this->_elementTemplates = str_replace('{req}', $this->_reqHTML, $this->_elementTemplates); |
19194f82 |
1418 | $this->_advancedHTML = $form->getAdvancedHTML(); |
1419 | $this->_showAdvanced = $form->getShowAdvanced(); |
da6f8763 |
1420 | parent::startForm($form); |
1421 | } |
7f40a229 |
1422 | |
da6f8763 |
1423 | function startGroup(&$group, $required, $error){ |
1424 | if (method_exists($group, 'getElementTemplateType')){ |
e249661f |
1425 | $html = $this->_elementTemplates[$group->getElementTemplateType()]; |
da6f8763 |
1426 | }else{ |
1427 | $html = $this->_elementTemplates['default']; |
7f40a229 |
1428 | |
da6f8763 |
1429 | } |
19194f82 |
1430 | if ($this->_showAdvanced){ |
1431 | $advclass = ' advanced'; |
1432 | } else { |
1433 | $advclass = ' advanced hide'; |
1434 | } |
1435 | if (isset($this->_advancedElements[$group->getName()])){ |
1436 | $html =str_replace(' {advanced}', $advclass, $html); |
1437 | $html =str_replace('{advancedimg}', $this->_advancedHTML, $html); |
1438 | } else { |
1439 | $html =str_replace(' {advanced}', '', $html); |
1440 | $html =str_replace('{advancedimg}', '', $html); |
1441 | } |
da6f8763 |
1442 | if (method_exists($group, 'getHelpButton')){ |
1443 | $html =str_replace('{help}', $group->getHelpButton(), $html); |
1444 | }else{ |
1445 | $html =str_replace('{help}', '', $html); |
da6f8763 |
1446 | } |
e7004d05 |
1447 | $html =str_replace('{name}', $group->getName(), $html); |
49c53687 |
1448 | $html =str_replace('{type}', 'fgroup', $html); |
7f40a229 |
1449 | |
da6f8763 |
1450 | $this->_templates[$group->getName()]=$html; |
1451 | // Fix for bug in tableless quickforms that didn't allow you to stop a |
1452 | // fieldset before a group of elements. |
1453 | // if the element name indicates the end of a fieldset, close the fieldset |
1454 | if ( in_array($group->getName(), $this->_stopFieldsetElements) |
1455 | && $this->_fieldsetsOpen > 0 |
1456 | ) { |
1457 | $this->_html .= $this->_closeFieldsetTemplate; |
1458 | $this->_fieldsetsOpen--; |
1459 | } |
1460 | parent::startGroup($group, $required, $error); |
1461 | } |
7f40a229 |
1462 | |
da6f8763 |
1463 | function renderElement(&$element, $required, $error){ |
86aab05c |
1464 | //manipulate id of all elements before rendering |
1465 | if (!is_null($element->getAttribute('id'))) { |
1466 | $id = $element->getAttribute('id'); |
1467 | } else { |
1468 | $id = $element->getName(); |
1469 | } |
1470 | //strip qf_ prefix and replace '[' with '_' and strip ']' |
1471 | $id = preg_replace(array('/^qf_|\]/', '/\[/'), array('', '_'), $id); |
1472 | if (strpos($id, 'id_') !== 0){ |
1473 | $element->updateAttributes(array('id'=>'id_'.$id)); |
1474 | } |
1475 | |
1476 | //adding stuff to place holders in template |
da6f8763 |
1477 | if (method_exists($element, 'getElementTemplateType')){ |
1478 | $html = $this->_elementTemplates[$element->getElementTemplateType()]; |
1479 | }else{ |
1480 | $html = $this->_elementTemplates['default']; |
19194f82 |
1481 | } |
1482 | if ($this->_showAdvanced){ |
1483 | $advclass = ' advanced'; |
1484 | } else { |
1485 | $advclass = ' advanced hide'; |
1486 | } |
1487 | if (isset($this->_advancedElements[$element->getName()])){ |
1488 | $html =str_replace(' {advanced}', $advclass, $html); |
1489 | } else { |
1490 | $html =str_replace(' {advanced}', '', $html); |
1491 | } |
1492 | if (isset($this->_advancedElements[$element->getName()])||$element->getName() == 'mform_showadvanced'){ |
1493 | $html =str_replace('{advancedimg}', $this->_advancedHTML, $html); |
1494 | } else { |
1495 | $html =str_replace('{advancedimg}', '', $html); |
da6f8763 |
1496 | } |
49c53687 |
1497 | $html =str_replace('{type}', 'f'.$element->getType(), $html); |
e7004d05 |
1498 | $html =str_replace('{name}', $element->getName(), $html); |
da6f8763 |
1499 | if (method_exists($element, 'getHelpButton')){ |
9403060a |
1500 | $html = str_replace('{help}', $element->getHelpButton(), $html); |
da6f8763 |
1501 | }else{ |
9403060a |
1502 | $html = str_replace('{help}', '', $html); |
7f40a229 |
1503 | |
da6f8763 |
1504 | } |
86aab05c |
1505 | |
9403060a |
1506 | $this->_templates[$element->getName()] = $html; |
86aab05c |
1507 | |
da6f8763 |
1508 | parent::renderElement($element, $required, $error); |
1509 | } |
19194f82 |
1510 | |
bb40325e |
1511 | function finishForm(&$form){ |
1512 | parent::finishForm($form); |
cbdb3705 |
1513 | // add a lockoptions script |
1514 | if ('' != ($script = $form->getLockOptionEndScript())) { |
bb40325e |
1515 | $this->_html = $this->_html . "\n" . $script; |
1516 | } |
1517 | } |
19194f82 |
1518 | /** |
1519 | * Called when visiting a header element |
1520 | * |
1521 | * @param object An HTML_QuickForm_header element being visited |
1522 | * @access public |
1523 | * @return void |
1524 | */ |
1525 | function renderHeader(&$header) { |
1526 | $name = $header->getName(); |
1527 | |
1528 | $id = empty($name) ? '' : ' id="' . $name . '"'; |
78354cec |
1529 | $id = preg_replace(array('/\]/', '/\[/'), array('', '_'), $id); |
19194f82 |
1530 | if (is_null($header->_text)) { |
1531 | $header_html = ''; |
1532 | } elseif (!empty($name) && isset($this->_templates[$name])) { |
1533 | $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]); |
1534 | } else { |
1535 | $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate); |
1536 | } |
1537 | |
1538 | if (isset($this->_advancedElements[$name])){ |
1539 | $header_html =str_replace('{advancedimg}', $this->_advancedHTML, $header_html); |
1540 | } else { |
1541 | $header_html =str_replace('{advancedimg}', '', $header_html); |
1542 | } |
1543 | $elementName='mform_showadvanced'; |
1544 | if ($this->_showAdvanced==0){ |
1545 | $buttonlabel = get_string('showadvanced', 'form'); |
1546 | } else { |
1547 | $buttonlabel = get_string('hideadvanced', 'form'); |
1548 | } |
1549 | |
1550 | if (isset($this->_advancedElements[$name])){ |
1551 | $showtext="'".get_string('showadvanced', 'form')."'"; |
1552 | $hidetext="'".get_string('hideadvanced', 'form')."'"; |
1553 | //onclick returns false so if js is on then page is not submitted. |
1554 | $onclick = 'return showAdvancedOnClick(this, '.$hidetext.', '.$showtext.');'; |
1555 | $button = '<input name="'.$elementName.'" value="'.$buttonlabel.'" type="submit" onclick="'.$onclick.'" />'; |
1556 | $header_html =str_replace('{button}', $button, $header_html); |
1557 | } else { |
1558 | $header_html =str_replace('{button}', '', $header_html); |
1559 | } |
1560 | |
1561 | if ($this->_fieldsetsOpen > 0) { |
1562 | $this->_html .= $this->_closeFieldsetTemplate; |
1563 | $this->_fieldsetsOpen--; |
1564 | } |
1565 | |
1566 | $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate); |
1567 | if ($this->_showAdvanced){ |
1568 | $advclass = ' class="advanced"'; |
1569 | } else { |
1570 | $advclass = ' class="advanced hide"'; |
1571 | } |
1572 | if (isset($this->_advancedElements[$name])){ |
1573 | $openFieldsetTemplate = str_replace('{advancedclass}', $advclass, $openFieldsetTemplate); |
1574 | } else { |
1575 | $openFieldsetTemplate = str_replace('{advancedclass}', '', $openFieldsetTemplate); |
1576 | } |
1577 | $this->_html .= $openFieldsetTemplate . $header_html; |
1578 | $this->_fieldsetsOpen++; |
1579 | } // end func renderHeader |
1580 | |
1581 | function getStopFieldsetElements(){ |
1582 | return $this->_stopFieldsetElements; |
1583 | } |
da6f8763 |
1584 | } |
1585 | |
da6f8763 |
1586 | |
9403060a |
1587 | $GLOBALS['_HTML_QuickForm_default_renderer'] =& new MoodleQuickForm_Renderer(); |
da6f8763 |
1588 | |
7f40a229 |
1589 | MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox'); |
1590 | MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'MoodleQuickForm_file'); |
1591 | MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group'); |
1592 | MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password'); |
cbdb3705 |
1593 | MoodleQuickForm::registerElementType('passwordreveal', "$CFG->libdir/form/passwordreveal.php", 'MoodleQuickForm_passwordreveal'); |
7f40a229 |
1594 | MoodleQuickForm::registerElementType('radio', "$CFG->libdir/form/radio.php", 'MoodleQuickForm_radio'); |
1595 | MoodleQuickForm::registerElementType('select', "$CFG->libdir/form/select.php", 'MoodleQuickForm_select'); |
1596 | MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text'); |
1597 | MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea'); |
1598 | MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector'); |
1599 | MoodleQuickForm::registerElementType('date_time_selector', "$CFG->libdir/form/datetimeselector.php", 'MoodleQuickForm_date_time_selector'); |
1600 | MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor'); |
effa85f4 |
1601 | MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format'); |
7f40a229 |
1602 | MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static'); |
1603 | MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden'); |
e2294b98 |
1604 | MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible'); |
1605 | MoodleQuickForm::registerElementType('modgroupmode', "$CFG->libdir/form/modgroupmode.php", 'MoodleQuickForm_modgroupmode'); |
e0f40684 |
1606 | MoodleQuickForm::registerElementType('selectyesno', "$CFG->libdir/form/selectyesno.php", 'MoodleQuickForm_selectyesno'); |
d4fe14d3 |
1607 | MoodleQuickForm::registerElementType('modgrade', "$CFG->libdir/form/modgrade.php", 'MoodleQuickForm_modgrade'); |
19110c57 |
1608 | MoodleQuickForm::registerElementType('cancel', "$CFG->libdir/form/cancel.php", 'MoodleQuickForm_cancel'); |
3c7656b4 |
1609 | MoodleQuickForm::registerElementType('button', "$CFG->libdir/form/button.php", 'MoodleQuickForm_button'); |
1610 | MoodleQuickForm::registerElementType('choosecoursefile', "$CFG->libdir/form/choosecoursefile.php", 'MoodleQuickForm_choosecoursefile'); |
6f05b1e1 |
1611 | MoodleQuickForm::registerElementType('choosecoursefileorimsrepo', "$CFG->libdir/form/choosecoursefileorimsrepo.php", 'MoodleQuickForm_choosecoursefileorimsrepo'); |
a23f0aaf |
1612 | MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", 'MoodleQuickForm_header'); |
1613 | MoodleQuickForm::registerElementType('submit', "$CFG->libdir/form/submit.php", 'MoodleQuickForm_submit'); |
9e93222d |
1614 | MoodleQuickForm::registerElementType('questioncategory', "$CFG->libdir/form/questioncategory.php", 'MoodleQuickForm_questioncategory'); |
111be766 |
1615 | MoodleQuickForm::registerElementType('advcheckbox', "$CFG->libdir/form/advcheckbox.php", 'MoodleQuickForm_advcheckbox'); |
49c7f3a8 |
1616 | ?> |