~ubuntu-branches/ubuntu/natty/moodle/natty

« back to all changes in this revision

Viewing changes to lib/formslib.php

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Muras
  • Date: 2010-10-30 12:19:28 UTC
  • mfrom: (1.1.12 upstream) (3.1.10 squeeze)
  • Revision ID: james.westby@ubuntu.com-20101030121928-qzobi6mctpnk4dif
Tags: 1.9.9.dfsg2-2
* Added Romanian translation
* Updated Japanese translation (closes: #596820)
* Backporting security fixes from Moodle 1.9.10 (closes: #601384)
   - Updated embedded CAS to 1.1.3
   - Added patch for MDL-24523:
     clean_text() not filtering text in markdown format
   - Added patch for MDL-24810 and upgraded customized HTML Purifier to 4.2.0 
   - Added patch for MDL-24258:
     students can delete their forum posts later than $CFG->maxeditingtime 
     under certain conditions
   - Added patch for MDL-23377:
     Can't delete quiz attempts in course without enrolled students

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php // $Id: formslib.php,v 1.129.2.19 2008/12/01 07:09:44 tjhunt Exp $
 
1
<?php // $Id: formslib.php,v 1.129.2.24 2010/05/13 01:40:36 moodler Exp $
2
2
/**
3
3
 * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms.
4
4
 *
20
20
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21
21
 */
22
22
 
 
23
if (!defined('MOODLE_INTERNAL')) {
 
24
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 
25
}
 
26
 
 
27
 
23
28
//setup.php icludes our hacked pear libs first
24
29
require_once 'HTML/QuickForm.php';
25
30
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
116
121
        $this->definition();
117
122
 
118
123
        $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection
 
124
        $this->_form->setType('sesskey', PARAM_RAW);
119
125
        $this->_form->setDefault('sesskey', sesskey());
120
126
        $this->_form->addElement('hidden', '_qf__'.$this->_formname, null);   // form submission marker
 
127
        $this->_form->setType('_qf__'.$this->_formname, PARAM_RAW);
121
128
        $this->_form->setDefault('_qf__'.$this->_formname, 1);
122
129
        $this->_form->_setDefaultRuleMessages();
123
130
 
300
307
 
301
308
    /**
302
309
     * Check that form data is valid.
 
310
     * You should almost always use this, rather than {@see validate_defined_fields}
303
311
     *
304
312
     * @return bool true if form data valid
305
313
     */
306
314
    function is_validated() {
 
315
        //finalize the form definition before any processing
 
316
        if (!$this->_definition_finalized) {
 
317
            $this->_definition_finalized = true;
 
318
            $this->definition_after_data();
 
319
        }
 
320
        return $this->validate_defined_fields();
 
321
    }
 
322
 
 
323
    /**
 
324
     * Validate the form.
 
325
     *
 
326
     * You almost always want to call {@see is_validated} instead of this
 
327
     * because it calls {@see definition_after_data} first, before validating the form,
 
328
     * which is what you want in 99% of cases.
 
329
     *
 
330
     * This is provided as a separate function for those special cases where
 
331
     * you want the form validated before definition_after_data is called
 
332
     * for example, to selectively add new elements depending on a no_submit_button press,
 
333
     * but only when the form is valid when the no_submit_button is pressed,
 
334
     *
 
335
     * @param boolean $validateonnosubmit optional, defaults to false.  The default behaviour
 
336
     *                is NOT to validate the form when a no submit button has been pressed.
 
337
     *                pass true here to override this behaviour
 
338
     *
 
339
     * @return bool true if form data valid
 
340
     */
 
341
    function validate_defined_fields($validateonnosubmit=false) {
307
342
        static $validated = null; // one validation is enough
308
343
        $mform =& $this->_form;
309
344
 
310
 
        //finalize the form definition before any processing
311
 
        if (!$this->_definition_finalized) {
312
 
            $this->_definition_finalized = true;
313
 
            $this->definition_after_data();
314
 
        }
315
 
 
316
 
        if ($this->no_submit_button_pressed()){
 
345
        if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
317
346
            return false;
318
347
        } elseif ($validated === null) {
319
348
            $internal_val = $mform->validate();
550
579
        $mform =& $this->_form;
551
580
        $mform->registerNoSubmitButton($addfieldsname);
552
581
        $mform->addElement('hidden', $repeathiddenname, $repeats);
 
582
        $mform->setType($repeathiddenname, PARAM_INT);
553
583
        //value not to be overridden by submitted value
554
584
        $mform->setConstants(array($repeathiddenname=>$repeats));
555
 
        for ($i=0; $i<$repeats; $i++) {
 
585
        $namecloned = array();
 
586
        for ($i = 0; $i < $repeats; $i++) {
556
587
            foreach ($elementobjs as $elementobj){
557
588
                $elementclone = fullclone($elementobj);
558
589
                $name = $elementclone->getName();
559
 
                if (!empty($name)){
 
590
                $namecloned[] = $name;
 
591
                if (!empty($name)) {
560
592
                    $elementclone->setName($name."[$i]");
561
593
                }
562
 
                if (is_a($elementclone, 'HTML_QuickForm_header')){
563
 
                    $value=$elementclone->_text;
 
594
                if (is_a($elementclone, 'HTML_QuickForm_header')) {
 
595
                    $value = $elementclone->_text;
564
596
                    $elementclone->setValue(str_replace('{no}', ($i+1), $value));
565
597
 
566
598
                } else {
591
623
                            $mform->setHelpButton($realelementname, $params);
592
624
                            break;
593
625
                        case 'disabledif' :
 
626
                            foreach ($namecloned as $num => $name){
 
627
                                if ($params[0] == $name){
 
628
                                    $params[0] = $params[0]."[$i]";
 
629
                                    break;
 
630
                                }
 
631
                            }
594
632
                            $params = array_merge(array($realelementname), $params);
595
633
                            call_user_func_array(array(&$mform, 'disabledIf'), $params);
596
634
                            break;
638
676
        }
639
677
 
640
678
        $mform->addElement('hidden', "checkbox_controller$groupid");
 
679
        $mform->setType("checkbox_controller$groupid", PARAM_INT);
641
680
        $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
642
681
 
643
682
        // Locate all checkboxes for this group and set their value, IF the optional param was given
794
833
            $this->_pageparams = '';
795
834
        }
796
835
        //no 'name' atttribute for form in xhtml strict :
797
 
        $attributes = array('action'=>$action, 'method'=>$method, 'id'=>'mform'.$formcounter) + $target;
 
836
        $attributes = array('action'=>$action, 'method'=>$method,
 
837
                'accept-charset'=>'utf-8', 'id'=>'mform'.$formcounter) + $target;
798
838
        $formcounter++;
799
839
        $this->updateAttributes($attributes);
800
840
 
832
872
            $this->registerNoSubmitButton('mform_showadvanced');
833
873
 
834
874
            $this->addElement('hidden', 'mform_showadvanced_last');
 
875
            $this->setType('mform_showadvanced_last', PARAM_INT);
835
876
        }
836
877
    }
837
878
    /**