da6f8763 |
1 | <?php |
2 | /** |
3 | * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms. |
4 | * THIS IS NOT YET PART OF THE MOODLE API, IT IS HERE FOR TESTING ONLY |
5 | * @author Jamie Pratt |
6 | * @version $Id$ |
7 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
8 | */ |
9 | |
10 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else. |
11 | if (FALSE===strstr(ini_get('include_path'), $CFG->libdir.'/pear' )){ |
12 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); |
13 | } |
14 | require_once 'HTML/QuickForm.php'; |
15 | require_once 'HTML/QuickForm/DHTMLRulesTableless.php'; |
16 | require_once 'HTML/QuickForm/Renderer/Tableless.php'; |
17 | |
49292f8c |
18 | require_once $CFG->libdir.'/uploadlib.php'; |
19 | |
864cc1de |
20 | if ($CFG->debug >= DEBUG_ALL){ |
21 | PEAR::setErrorHandling(PEAR_ERROR_PRINT); |
22 | } |
23 | |
7f40a229 |
24 | class moodleform { |
49292f8c |
25 | var $_formname; // form name |
26 | var $_form; // quickform object definition |
27 | var $_customdata; // globals workaround |
28 | var $_upload_manager; // file upload manager |
7f40a229 |
29 | |
30 | function moodleform($action, $customdata=null, $method='post', $target='', $attributes=null) { |
5bc97c98 |
31 | $this->_formname = rtrim(get_class($this), '_form'); |
7f40a229 |
32 | $this->_customdata = $customdata; |
5bc97c98 |
33 | $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); |
7f40a229 |
34 | |
35 | $this->definition(); |
36 | |
37 | $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection |
38 | $this->_form->setDefault('sesskey', sesskey()); |
5bc97c98 |
39 | $this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker |
40 | $this->_form->setDefault('_qf__'.$this->_formname, 1); |
41 | $this->_form->_setDefaultRuleMessages(); |
7f40a229 |
42 | |
43 | // we have to know all input types before processing submission ;-) |
44 | $this->_process_submission($method); |
45 | |
46 | } |
2c412890 |
47 | /** |
48 | * To autofocus on first form element with error. |
49 | * |
50 | * @return string javascript to select form element with first error or |
5c52df67 |
51 | * first element if no errors. |
2c412890 |
52 | */ |
53 | function focus(){ |
54 | $form=$this->_form; |
5c52df67 |
55 | $elkeys=array_keys($form->_elementIndex); |
2c412890 |
56 | if (isset($form->_errors) && 0!=count($form->_errors)){ |
57 | $errorkeys=array_keys($form->_errors); |
2c412890 |
58 | $keyinorder=array_intersect($elkeys, $errorkeys); |
59 | $el='getElementById(\'id_'.array_shift($keyinorder).'\')'; |
60 | return $el; |
61 | } else{ |
5c52df67 |
62 | $el='getElementById(\'id_'.array_shift($elkeys).'\')'; |
63 | return $el; |
2c412890 |
64 | } |
65 | } |
7f40a229 |
66 | |
67 | function _process_submission($method) { |
68 | $submission = array(); |
49292f8c |
69 | $files = array(); |
7f40a229 |
70 | if ($method == 'post') { |
71 | if (!empty($_POST)) { |
72 | $submission = $_POST; |
73 | } |
74 | } else { |
75 | $submission = array_merge_recursive($_GET, $_POST); // emulate handling of parameters in xxxx_param() |
76 | } |
77 | |
78 | // following trick is needed to enable proper sesskey checks when using GET forms |
5bc97c98 |
79 | // the _qf__.$this->_formname serves as a marker that form was actually submitted |
80 | if (array_key_exists('_qf__'.$this->_formname, $submission) and $submission['_qf__'.$this->_formname] == 1) { |
7f40a229 |
81 | if (!confirm_sesskey()) { |
82 | error('Incorrect sesskey submitted, form not accepted!'); |
83 | } |
84 | } else { |
85 | $submission = array(); |
86 | } |
87 | |
49292f8c |
88 | $this->_form->updateSubmission($submission, $_FILES); |
c08ac016 |
89 | $this->definition_after_data(); |
7f40a229 |
90 | } |
91 | |
49292f8c |
92 | function _validate_files() { |
93 | if (empty($_FILES)) { |
94 | // we do not need to do any checks because no files were submitted |
95 | // TODO: find out why server side required rule does not work for uploaded files; |
96 | // testing is easily done by always returning true from this function and adding |
97 | // $mform->addRule('soubor', get_string('required'), 'required', null, 'server'); |
98 | // and submitting form without selected file |
99 | return true; |
100 | } |
101 | $errors = array(); |
102 | $mform =& $this->_form; |
103 | |
104 | // create default upload manager if not already created |
105 | if (empty($this->_upload_manager)) { |
106 | $this->_upload_manager = new upload_manager(); |
107 | } |
108 | |
109 | // check the files |
110 | $status = $this->_upload_manager->preprocess_files(); |
111 | |
112 | // now check that we really want each file |
113 | foreach ($_FILES as $elname=>$file) { |
114 | if ($mform->elementExists($elname) and $mform->getElementType($elname)=='file') { |
115 | $required = $mform->isElementRequired($elname); |
116 | if (!empty($this->_upload_manager->files[$elname]['uploadlog']) and empty($this->_upload_manager->files[$elname]['clear'])) { |
117 | if (!$required and $file['error'] == UPLOAD_ERR_NO_FILE) { |
118 | // file not uploaded and not required - ignore it |
119 | continue; |
120 | } |
121 | $errors[$elname] = $this->_upload_manager->files[$elname]['uploadlog']; |
122 | } |
123 | } else { |
124 | error('Incorrect upload attemp!'); |
125 | } |
126 | } |
127 | |
128 | // return errors if found |
129 | if ($status and 0 == count($errors)){ |
130 | return true; |
131 | } else { |
132 | return $errors; |
133 | } |
134 | } |
135 | |
7f40a229 |
136 | function set_defaults($default_values, $slashed=false) { |
137 | if (is_object($default_values)) { |
138 | $default_values = (array)$default_values; |
139 | } |
140 | $filter = $slashed ? 'stripslashes' : NULL; |
141 | $this->_form->setDefaults($default_values, $filter); |
142 | } |
143 | |
144 | function is_submitted() { |
145 | return $this->_form->isSubmitted(); |
146 | } |
147 | |
148 | function is_validated() { |
49292f8c |
149 | static $validated = null; // one validation is enough |
7f40a229 |
150 | |
151 | if ($validated === null) { |
152 | $internal_val = $this->_form->validate(); |
e7dcb0fc |
153 | $moodle_val = $this->validation($this->_form->exportValues(null, true)); |
7f40a229 |
154 | if ($moodle_val !== true) { |
155 | if (!empty($moodle_val)) { |
156 | foreach ($moodle_val as $element=>$msg) { |
157 | $this->_form->setElementError($element, $msg); |
158 | } |
159 | } |
160 | $moodle_val = false; |
161 | } |
49292f8c |
162 | $file_val = $this->_validate_files(); |
163 | if ($file_val !== true) { |
164 | if (!empty($file_val)) { |
165 | foreach ($file_val as $element=>$msg) { |
166 | $this->_form->setElementError($element, $msg); |
167 | } |
168 | } |
169 | $file_val = false; |
170 | } |
171 | $validated = ($internal_val and $moodle_val and $file_val); |
7f40a229 |
172 | } |
173 | return $validated; |
174 | } |
175 | |
176 | function data_submitted($slashed=true) { |
177 | if ($this->is_submitted() and $this->is_validated()) { |
178 | $data = $this->_form->exportValues(null, $slashed); |
5bc97c98 |
179 | unset($data['sesskey']); // we do not need to return sesskey |
180 | unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too |
7f40a229 |
181 | if (empty($data)) { |
182 | return NULL; |
183 | } else { |
184 | return (object)$data; |
185 | } |
186 | } else { |
187 | return NULL; |
188 | } |
189 | } |
190 | |
49292f8c |
191 | function save_files($destination) { |
192 | if (empty($this->_upload_manager)) { |
193 | return false; |
194 | } |
195 | if ($this->is_submitted() and $this->is_validated()) { |
196 | return $this->_upload_manager->save_files($destination); |
197 | } |
198 | return false; |
199 | } |
2b63df96 |
200 | |
7f40a229 |
201 | function display() { |
202 | $this->_form->display(); |
203 | } |
204 | |
49292f8c |
205 | /** |
206 | * abstract method - always override. |
207 | * |
208 | * If you need special handling of uploaded files, create instance of $this->_upload_manager here. |
209 | */ |
7f40a229 |
210 | function definition() { |
211 | error('Abstract form_definition() method in class '.get_class($this).' must be overriden, please fix the code.'); |
212 | } |
2c412890 |
213 | |
c08ac016 |
214 | /** |
215 | * Another abstract function. This one is called after submitted data has |
216 | * been processed and is available. All form setup that is dependent on form values |
217 | * should go in here. |
218 | * |
219 | */ |
220 | function definition_after_data(){ |
2c412890 |
221 | |
c08ac016 |
222 | } |
7f40a229 |
223 | |
224 | // dummy stub method - override if needed |
225 | function validation($data) { |
226 | // return array of errors ("fieldname"=>"error message") or true if ok |
227 | return true; |
228 | } |
229 | |
bb40325e |
230 | |
7f40a229 |
231 | } |
232 | |
233 | class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { |
234 | var $_types = array(); |
235 | |
236 | |
da6f8763 |
237 | /** |
238 | * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless |
239 | * @param string $formName Form's name. |
240 | * @param string $method (optional)Form's method defaults to 'POST' |
241 | * @param string $action (optional)Form's action |
242 | * @param string $target (optional)Form's target defaults to none |
243 | * @param mixed $attributes (optional)Extra attributes for <form> tag |
244 | * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field |
245 | * @access public |
246 | */ |
7f40a229 |
247 | function MoodleQuickForm($formName, $method, $action, $target='', $attributes=null){ |
da6f8763 |
248 | global $CFG; |
7f40a229 |
249 | |
da6f8763 |
250 | HTML_Common::HTML_Common($attributes); |
da6f8763 |
251 | $target = empty($target) ? array() : array('target' => $target); |
252 | //no 'name' atttribute for form in xhtml strict : |
253 | $attributes = array('action'=>$action, 'method'=>$method, 'id'=>$formName) + $target; |
254 | $this->updateAttributes($attributes); |
da6f8763 |
255 | |
7f40a229 |
256 | //this is custom stuff for Moodle : |
da6f8763 |
257 | $oldclass= $this->getAttribute('class'); |
258 | if (!empty($oldclass)){ |
259 | $this->updateAttributes(array('class'=>$oldclass.' mform')); |
260 | }else { |
80f962df |
261 | $this->updateAttributes(array('class'=>'mform')); |
da6f8763 |
262 | } |
7f40a229 |
263 | $this->_helpImageURL="$CFG->wwwroot/lib/form/req.gif"; |
264 | $this->_reqHTML = |
da6f8763 |
265 | helpbutton('requiredelement', get_string('requiredelement', 'form'),'moodle', |
266 | true, false, '', true, '<img alt="'.get_string('requiredelement', 'form').'" src="'. |
267 | $this->_helpImageURL.'" />'); |
268 | $this->setRequiredNote(get_string('denotesreq', 'form', $this->getReqHTML())); |
269 | } |
bb40325e |
270 | |
7f40a229 |
271 | function setType($elementname, $paramtype) { |
272 | $this->_types[$elementname] = $paramtype; |
273 | } |
49292f8c |
274 | |
c56f1826 |
275 | function setTypes($paramtypes) { |
276 | $this->_types = $paramtypes + $this->_types; |
277 | } |
49292f8c |
278 | |
279 | function updateSubmission($submission, $files) { |
280 | $this->_flagSubmitted = false; |
281 | |
7f40a229 |
282 | if (empty($submission)) { |
283 | $this->_submitValues = array(); |
7f40a229 |
284 | } else { |
285 | foreach ($submission as $key=>$s) { |
286 | if (array_key_exists($key, $this->_types)) { |
287 | $submission[$key] = clean_param($s, $this->_types[$key]); |
288 | } |
289 | } |
290 | $this->_submitValues = $this->_recursiveFilter('stripslashes', $submission); |
291 | $this->_flagSubmitted = true; |
292 | } |
293 | |
49292f8c |
294 | if (empty($files)) { |
295 | $this->_submitFiles = array(); |
296 | } else { |
297 | if (1 == get_magic_quotes_gpc()) { |
298 | foreach ($files as $elname=>$file) { |
299 | // dangerous characters in filenames are cleaned later in upload_manager |
300 | $files[$elname]['name'] = stripslashes($files[$elname]['name']); |
301 | } |
302 | } |
303 | $this->_submitFiles = $files; |
304 | $this->_flagSubmitted = true; |
305 | } |
306 | |
2c412890 |
307 | // need to tell all elements that they need to update their value attribute. |
308 | foreach (array_keys($this->_elements) as $key) { |
309 | $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this); |
310 | } |
7f40a229 |
311 | } |
312 | |
da6f8763 |
313 | function getReqHTML(){ |
314 | return $this->_reqHTML; |
315 | } |
7f40a229 |
316 | |
317 | /** |
318 | * Initializes a default form value |
319 | * |
320 | * @param string $elementname element name |
321 | * @param mixed $values values for that element name |
322 | * @param bool $slashed the default value is slashed |
323 | * @access public |
324 | * @return void |
325 | */ |
326 | function setDefault($elementName, $defaultValue, $slashed=false){ |
327 | $filter = $slashed ? 'stripslashes' : NULL; |
328 | $this->setDefaults(array($elementName=>$defaultValue), $filter); |
329 | } // end func setDefault |
da6f8763 |
330 | /** |
c56f1826 |
331 | * Add an array of buttons to the form |
7f40a229 |
332 | * @param array $buttons An associative array representing help button to attach to |
da6f8763 |
333 | * to the form. keys of array correspond to names of elements in form. |
7f40a229 |
334 | * |
da6f8763 |
335 | * @access public |
336 | */ |
42f248e6 |
337 | function setHelpButtons($buttons, $suppresscheck=false){ |
7f40a229 |
338 | |
c56f1826 |
339 | foreach ($buttons as $elementname => $button){ |
340 | $this->setHelpButton($elementname, $button, $suppresscheck); |
da6f8763 |
341 | } |
342 | } |
c56f1826 |
343 | /** |
344 | * Add a single button |
345 | * |
346 | * @param string $elementname name of the element to add the item to |
347 | * @param array $button - arguments to pass to setHelpButton |
348 | * @param boolean $suppresscheck - whether to throw an error if the element |
349 | * doesn't exist. |
350 | */ |
351 | function setHelpButton($elementname, $button, $suppresscheck=false){ |
352 | if (array_key_exists($elementname, $this->_elementIndex)){ |
353 | //_elements has a numeric index, this code accesses the elements by name |
354 | $element=&$this->_elements[$this->_elementIndex[$elementname]]; |
355 | if (method_exists($element, 'setHelpButton')){ |
356 | $element->setHelpButton($button); |
357 | }else{ |
358 | $a=new object(); |
359 | $a->name=$element->getName(); |
360 | $a->classname=get_class($element); |
361 | print_error('nomethodforaddinghelpbutton', 'form', '', $a); |
362 | } |
363 | }elseif (!$suppresscheck){ |
364 | print_error('nonexistentformelements', 'form', '', $elementname); |
2c412890 |
365 | } |
c56f1826 |
366 | } |
7f40a229 |
367 | |
42f248e6 |
368 | function exportValues($elementList= null, $addslashes=true){ |
da6f8763 |
369 | $unfiltered=parent::exportValues($elementList); |
7f40a229 |
370 | |
da6f8763 |
371 | if ($addslashes){ |
42f248e6 |
372 | return $this->_recursiveFilter('addslashes',$unfiltered); |
da6f8763 |
373 | } else { |
374 | return $unfiltered; |
375 | } |
376 | } |
5bc97c98 |
377 | /** |
378 | * Returns the client side validation script |
379 | * |
380 | * The code here was copied from HTML_QuickForm_DHTMLRulesTableless who copied it from HTML_QuickForm |
381 | * and slightly modified to run rules per-element |
382 | * Needed to override this because of an error with client side validation of grouped elements. |
383 | * |
384 | * @access public |
385 | * @return string Javascript to perform validation, empty string if no 'client' rules were added |
386 | */ |
387 | function getValidationScript() |
388 | { |
389 | if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) { |
390 | return ''; |
391 | } |
392 | |
393 | include_once('HTML/QuickForm/RuleRegistry.php'); |
394 | $registry =& HTML_QuickForm_RuleRegistry::singleton(); |
395 | $test = array(); |
396 | $js_escape = array( |
397 | "\r" => '\r', |
398 | "\n" => '\n', |
399 | "\t" => '\t', |
400 | "'" => "\\'", |
401 | '"' => '\"', |
402 | '\\' => '\\\\' |
403 | ); |
404 | |
405 | foreach ($this->_rules as $elementName => $rules) { |
406 | foreach ($rules as $rule) { |
407 | if ('client' == $rule['validation']) { |
49292f8c |
408 | $element = new object(); |
5bc97c98 |
409 | |
410 | $dependent = isset($rule['dependent']) && is_array($rule['dependent']); |
411 | $rule['message'] = strtr($rule['message'], $js_escape); |
412 | |
413 | if (isset($rule['group'])) { |
414 | $group =& $this->getElement($rule['group']); |
415 | // No JavaScript validation for frozen elements |
416 | if ($group->isFrozen()) { |
417 | continue 2; |
418 | } |
419 | $elements =& $group->getElements(); |
420 | foreach (array_keys($elements) as $key) { |
421 | if ($elementName == $group->getElementName($key)) { |
422 | $element =& $elements[$key]; |
423 | break; |
424 | } |
425 | } |
426 | } elseif ($dependent) { |
427 | $element = array(); |
428 | $element[] =& $this->getElement($elementName); |
429 | foreach ($rule['dependent'] as $idx => $elName) { |
430 | $element[] =& $this->getElement($elName); |
431 | } |
432 | } else { |
433 | $element =& $this->getElement($elementName); |
434 | } |
435 | // No JavaScript validation for frozen elements |
436 | if (is_object($element) && $element->isFrozen()) { |
437 | continue 2; |
438 | } elseif (is_array($element)) { |
439 | foreach (array_keys($element) as $key) { |
440 | if ($element[$key]->isFrozen()) { |
441 | continue 3; |
442 | } |
443 | } |
444 | } |
445 | // Fix for bug displaying errors for elements in a group |
446 | //$test[$elementName][] = $registry->getValidationScript($element, $elementName, $rule); |
447 | $test[$elementName][0][] = $registry->getValidationScript($element, $elementName, $rule); |
448 | $test[$elementName][1]=$element; |
449 | //end of fix |
450 | } |
451 | } |
452 | } |
453 | $js = ' |
454 | <script type="text/javascript"> |
455 | //<![CDATA[ |
456 | function qf_errorHandler(element, _qfMsg) { |
457 | div = element.parentNode; |
458 | if (_qfMsg != \'\') { |
459 | span = document.createElement("span"); |
460 | span.className = "error"; |
461 | span.appendChild(document.createTextNode(_qfMsg.substring(3))); |
462 | br = document.createElement("br"); |
463 | |
464 | var errorDiv = document.getElementById(element.name + \'_errorDiv\'); |
465 | if (!errorDiv) { |
466 | errorDiv = document.createElement("div"); |
467 | errorDiv.id = element.name + \'_errorDiv\'; |
468 | } |
469 | while (errorDiv.firstChild) { |
470 | errorDiv.removeChild(errorDiv.firstChild); |
471 | } |
2c412890 |
472 | |
5bc97c98 |
473 | errorDiv.insertBefore(br, errorDiv.firstChild); |
474 | errorDiv.insertBefore(span, errorDiv.firstChild); |
475 | element.parentNode.insertBefore(errorDiv, element.parentNode.firstChild); |
476 | |
477 | if (div.className.substr(div.className.length - 6, 6) != " error" |
478 | && div.className != "error") { |
479 | div.className += " error"; |
480 | } |
481 | |
482 | return false; |
483 | } else { |
484 | var errorDiv = document.getElementById(element.name + \'_errorDiv\'); |
485 | if (errorDiv) { |
486 | errorDiv.parentNode.removeChild(errorDiv); |
487 | } |
488 | |
489 | if (div.className.substr(div.className.length - 6, 6) == " error") { |
490 | div.className = div.className.substr(0, div.className.length - 6); |
491 | } else if (div.className == "error") { |
492 | div.className = ""; |
493 | } |
494 | |
495 | return true; |
496 | } |
497 | }'; |
498 | $validateJS = ''; |
499 | foreach ($test as $elementName => $jsandelement) { |
500 | // Fix for bug displaying errors for elements in a group |
501 | //unset($element); |
502 | list($jsArr,$element)=$jsandelement; |
503 | //end of fix |
504 | $js .= ' |
505 | function validate_' . $this->_attributes['id'] . '_' . $elementName . '(element) { |
506 | var value = \'\'; |
507 | var errFlag = new Array(); |
508 | var _qfGroups = {}; |
509 | var _qfMsg = \'\'; |
510 | var frm = element.parentNode; |
511 | while (frm && frm.nodeName != "FORM") { |
512 | frm = frm.parentNode; |
513 | } |
514 | ' . join("\n", $jsArr) . ' |
515 | return qf_errorHandler(element, _qfMsg); |
516 | } |
517 | '; |
518 | $validateJS .= ' |
519 | ret = validate_' . $this->_attributes['id'] . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret;'; |
520 | // Fix for bug displaying errors for elements in a group |
521 | //unset($element); |
522 | //$element =& $this->getElement($elementName); |
523 | //end of fix |
524 | $valFunc = 'validate_' . $this->_attributes['id'] . '_' . $elementName . '(this)'; |
525 | $onBlur = $element->getAttribute('onBlur'); |
526 | $onChange = $element->getAttribute('onChange'); |
527 | $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, |
528 | 'onChange' => $onChange . $valFunc)); |
529 | } |
530 | $js .= ' |
531 | function validate_' . $this->_attributes['id'] . '(frm) { |
532 | var ret = true; |
533 | ' . $validateJS . '; |
534 | return ret; |
535 | } |
536 | //]]> |
537 | </script>'; |
538 | return $js; |
539 | } // end func getValidationScript |
540 | function _setDefaultRuleMessages(){ |
541 | foreach ($this->_rules as $field => $rulesarr){ |
542 | foreach ($rulesarr as $key => $rule){ |
543 | if ($rule['message']===null){ |
544 | $a=new object(); |
545 | $a->format=$rule['format']; |
546 | $str=get_string('err_'.$rule['type'], 'form', $a); |
547 | if (strpos($str, '[[')!==0){ |
548 | $this->_rules[$field][$key]['message']=$str; |
2c412890 |
549 | } |
5bc97c98 |
550 | } |
551 | } |
552 | } |
553 | } |
bb40325e |
554 | function getLockOptionStartScript(){ |
555 | |
556 | return ''; |
557 | } |
558 | function getLockOptionEndScript(){ |
559 | |
560 | return ''; |
561 | } |
562 | |
563 | function addGroupmodeSetting($course) { |
5bc97c98 |
564 | |
bb40325e |
565 | if (! $course = get_record('course', 'id', $course)) { |
566 | error("This course doesn't exist"); |
567 | } |
568 | |
569 | if ($form->coursemodule) { |
570 | if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) { |
571 | error("This course module doesn't exist"); |
572 | } |
573 | } else { |
574 | $cm = null; |
575 | } |
576 | $groupmode = groupmode($course, $cm); |
577 | if ($course->groupmode or (!$course->groupmodeforce)) { |
49292f8c |
578 | $choices = new object(); |
bb40325e |
579 | $choices[NOGROUPS] = get_string('groupsnone'); |
580 | $choices[SEPARATEGROUPS] = get_string('groupsseparate'); |
581 | $choices[VISIBLEGROUPS] = get_string('groupsvisible'); |
582 | choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce); |
583 | |
584 | } |
585 | |
586 | } |
da6f8763 |
587 | } |
588 | |
589 | /** |
7f40a229 |
590 | * A renderer for MoodleQuickForm that only uses XHTML and CSS and no |
da6f8763 |
591 | * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless |
7f40a229 |
592 | * |
da6f8763 |
593 | * Stylesheet is part of standard theme and should be automatically included. |
594 | * |
595 | * @author Jamie Pratt <me@jamiep.org> |
596 | * @license gpl license |
597 | */ |
7f40a229 |
598 | class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ |
da6f8763 |
599 | |
600 | /** |
601 | * Element template array |
602 | * @var array |
603 | * @access private |
604 | */ |
605 | var $_elementTemplates; |
42f248e6 |
606 | |
49c53687 |
607 | // uncomment templates below and edit formslib.php for |
42f248e6 |
608 | // ol li containers for form items. |
609 | |
49c53687 |
610 | /** |
611 | * Template used when opening a hidden fieldset |
612 | * (i.e. a fieldset that is opened when there is no header element) |
613 | * @var string |
614 | * @access private |
615 | */ |
616 | var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">"; |
42f248e6 |
617 | // var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">\n\t\t<ol>"; |
618 | // /** |
619 | // * Header Template string |
620 | // * @var string |
621 | // * @access private |
622 | // */ |
7f40a229 |
623 | // var $_headerTemplate = |
42f248e6 |
624 | // "\n\t\t<legend>{header}</legend>\n\t\t<ol>"; |
7f40a229 |
625 | // var $_headerTemplate = |
49c53687 |
626 | // "\n\t\t<legend>{header}</legend>\n\t\t<ol>"; |
7f40a229 |
627 | |
49c53687 |
628 | /** |
629 | * Template used when closing a fieldset |
630 | * @var string |
631 | * @access private |
632 | */ |
633 | var $_closeFieldsetTemplate = "\n\t\t</fieldset>"; |
42f248e6 |
634 | // var $_closeFieldsetTemplate = "\n\t\t</ol>\n\t</fieldset>"; |
635 | |
49c53687 |
636 | /** |
637 | * Required Note template string |
638 | * @var string |
639 | * @access private |
640 | */ |
7f40a229 |
641 | var $_requiredNoteTemplate = |
49c53687 |
642 | "\n\t\t<div class=\"fdescription\">{requiredNote}</div>"; |
7f40a229 |
643 | |
644 | function MoodleQuickForm_Renderer(){ |
42f248e6 |
645 | // switch next two lines for ol li containers for form items. |
49c53687 |
646 | // $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>"); |
15fdf619 |
647 | $this->_elementTemplates=array('default'=>"\n\t\t<div class=\"fitem\"><label>{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class=\"felement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div>", |
648 | 'fieldset'=>"\n\t\t<div class=\"fitem\"><label>{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><fieldset class=\"felement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</fieldset></div>"); |
da6f8763 |
649 | |
650 | parent::HTML_QuickForm_Renderer_Tableless(); |
651 | } |
7f40a229 |
652 | |
da6f8763 |
653 | function startForm(&$form){ |
654 | $this->_reqHTML=$form->getReqHTML(); |
655 | $this->_elementTemplates=str_replace('{req}', $this->_reqHTML, $this->_elementTemplates); |
656 | parent::startForm($form); |
657 | } |
7f40a229 |
658 | |
da6f8763 |
659 | function startGroup(&$group, $required, $error){ |
660 | if (method_exists($group, 'getElementTemplateType')){ |
e249661f |
661 | $html = $this->_elementTemplates[$group->getElementTemplateType()]; |
da6f8763 |
662 | }else{ |
663 | $html = $this->_elementTemplates['default']; |
7f40a229 |
664 | |
da6f8763 |
665 | } |
666 | if (method_exists($group, 'getHelpButton')){ |
667 | $html =str_replace('{help}', $group->getHelpButton(), $html); |
668 | }else{ |
669 | $html =str_replace('{help}', '', $html); |
7f40a229 |
670 | |
da6f8763 |
671 | } |
49c53687 |
672 | $html =str_replace('{type}', 'fgroup', $html); |
7f40a229 |
673 | |
da6f8763 |
674 | $this->_templates[$group->getName()]=$html; |
675 | // Fix for bug in tableless quickforms that didn't allow you to stop a |
676 | // fieldset before a group of elements. |
677 | // if the element name indicates the end of a fieldset, close the fieldset |
678 | if ( in_array($group->getName(), $this->_stopFieldsetElements) |
679 | && $this->_fieldsetsOpen > 0 |
680 | ) { |
681 | $this->_html .= $this->_closeFieldsetTemplate; |
682 | $this->_fieldsetsOpen--; |
683 | } |
684 | parent::startGroup($group, $required, $error); |
685 | } |
7f40a229 |
686 | |
da6f8763 |
687 | function renderElement(&$element, $required, $error){ |
688 | if (method_exists($element, 'getElementTemplateType')){ |
689 | $html = $this->_elementTemplates[$element->getElementTemplateType()]; |
690 | }else{ |
691 | $html = $this->_elementTemplates['default']; |
7f40a229 |
692 | |
da6f8763 |
693 | } |
49c53687 |
694 | $html =str_replace('{type}', 'f'.$element->getType(), $html); |
da6f8763 |
695 | if (method_exists($element, 'getHelpButton')){ |
696 | $html=str_replace('{help}', $element->getHelpButton(), $html); |
697 | }else{ |
698 | $html=str_replace('{help}', '', $html); |
7f40a229 |
699 | |
da6f8763 |
700 | } |
701 | $this->_templates[$element->getName()]=$html; |
230a910a |
702 | if (!is_null($element->getAttribute('id'))) { |
703 | $id = $element->getAttribute('id'); |
704 | } else { |
705 | $id = $element->getName(); |
706 | } |
707 | $element->updateAttributes(array('id'=>'id_'.$id)); |
da6f8763 |
708 | parent::renderElement($element, $required, $error); |
709 | } |
bb40325e |
710 | function finishForm(&$form){ |
711 | parent::finishForm($form); |
712 | // add a validation script |
713 | if ('' != ($script = $form->getLockOptionStartScript())) { |
714 | $this->_html = $script . "\n" . $this->_html; |
715 | } |
716 | if ('' != ($script = $form->getLockOptionEndScript())) { |
717 | $this->_html = $this->_html . "\n" . $script; |
718 | } |
719 | } |
da6f8763 |
720 | } |
721 | |
da6f8763 |
722 | |
7f40a229 |
723 | $GLOBALS['_HTML_QuickForm_default_renderer']=& new MoodleQuickForm_Renderer(); |
da6f8763 |
724 | |
7f40a229 |
725 | MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox'); |
726 | MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'MoodleQuickForm_file'); |
727 | MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group'); |
728 | MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password'); |
729 | MoodleQuickForm::registerElementType('radio', "$CFG->libdir/form/radio.php", 'MoodleQuickForm_radio'); |
730 | MoodleQuickForm::registerElementType('select', "$CFG->libdir/form/select.php", 'MoodleQuickForm_select'); |
731 | MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text'); |
732 | MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea'); |
733 | MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector'); |
734 | MoodleQuickForm::registerElementType('date_time_selector', "$CFG->libdir/form/datetimeselector.php", 'MoodleQuickForm_date_time_selector'); |
735 | MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor'); |
effa85f4 |
736 | MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format'); |
7f40a229 |
737 | MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static'); |
738 | MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden'); |
864cc1de |
739 | |
da6f8763 |
740 | ?> |