libdir.'/pear' )){ ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); } require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/DHTMLRulesTableless.php'; require_once 'HTML/QuickForm/Renderer/Tableless.php'; if ($CFG->debug >= DEBUG_ALL){ PEAR::setErrorHandling(PEAR_ERROR_PRINT); } class moodleform { var $_formname; // form name var $_form; // quickform object definition var $_customdata; // globals workaround function moodleform($action, $customdata=null, $method='post', $target='', $attributes=null) { $this->_formname = rtrim(get_class($this), '_form'); $this->_customdata = $customdata; $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); $this->definition(); $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection $this->_form->setDefault('sesskey', sesskey()); $this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker $this->_form->setDefault('_qf__'.$this->_formname, 1); $this->_form->_setDefaultRuleMessages(); // we have to know all input types before processing submission ;-) $this->_process_submission($method); } /** * To autofocus on first form element with error. * * @return string javascript to select form element with first error or * '' if no errors. */ function focus(){ $form=$this->_form; if (isset($form->_errors) && 0!=count($form->_errors)){ $errorkeys=array_keys($form->_errors); $elkeys=array_keys($form->_elementIndex); $keyinorder=array_intersect($elkeys, $errorkeys); $el='getElementById(\'id_'.array_shift($keyinorder).'\')'; return $el; } else{ return ''; } } function _process_submission($method) { $submission = array(); if ($method == 'post') { if (!empty($_POST)) { $submission = $_POST; } } else { $submission = array_merge_recursive($_GET, $_POST); // emulate handling of parameters in xxxx_param() } // following trick is needed to enable proper sesskey checks when using GET forms // the _qf__.$this->_formname serves as a marker that form was actually submitted if (array_key_exists('_qf__'.$this->_formname, $submission) and $submission['_qf__'.$this->_formname] == 1) { if (!confirm_sesskey()) { error('Incorrect sesskey submitted, form not accepted!'); } } else { $submission = array(); } $this->_form->updateSubmission($submission); $this->definition_after_data(); } function set_defaults($default_values, $slashed=false) { if (is_object($default_values)) { $default_values = (array)$default_values; } $filter = $slashed ? 'stripslashes' : NULL; $this->_form->setDefaults($default_values, $filter); } function is_submitted() { return $this->_form->isSubmitted(); } function is_validated() { static $validated = null; if ($validated === null) { $internal_val = $this->_form->validate(); $moodle_val = $this->validation($this->_form->exportValues(null, true)); if ($moodle_val !== true) { if (!empty($moodle_val)) { foreach ($moodle_val as $element=>$msg) { $this->_form->setElementError($element, $msg); } } $moodle_val = false; } $validated = ($internal_val and $moodle_val); } return $validated; } function data_submitted($slashed=true) { if ($this->is_submitted() and $this->is_validated()) { $data = $this->_form->exportValues(null, $slashed); unset($data['sesskey']); // we do not need to return sesskey unset($data['_qf__'.$this->_formname]); // we do not need the submission marker too if (empty($data)) { return NULL; } else { return (object)$data; } } else { return NULL; } } function display() { $this->_form->display(); } // abstract method - always override function definition() { error('Abstract form_definition() method in class '.get_class($this).' must be overriden, please fix the code.'); } /** * Another abstract function. This one is called after submitted data has * been processed and is available. All form setup that is dependent on form values * should go in here. * */ function definition_after_data(){ } // dummy stub method - override if needed function validation($data) { // return array of errors ("fieldname"=>"error message") or true if ok return true; } } class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { var $_types = array(); /** * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless * @param string $formName Form's name. * @param string $method (optional)Form's method defaults to 'POST' * @param string $action (optional)Form's action * @param string $target (optional)Form's target defaults to none * @param mixed $attributes (optional)Extra attributes for