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