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