MDL-27094 fix incorrect assignment form validation method name
[moodle.git] / mod / assignment / mod_form.php
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
4 }
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
8 class mod_assignment_mod_form extends moodleform_mod {
9     protected $_assignmentinstance = null;
11     function definition() {
12         global $CFG, $DB;
13         $mform =& $this->_form;
15         // this hack is needed for different settings of each subtype
16         if (!empty($this->_instance)) {
17             if($ass = $DB->get_record('assignment', array('id'=>$this->_instance))) {
18                 $type = $ass->assignmenttype;
19             } else {
20                 print_error('invalidassignment', 'assignment');
21             }
22         } else {
23             $type = required_param('type', PARAM_ALPHA);
24         }
25         $mform->addElement('hidden', 'assignmenttype', $type);
26         $mform->setType('assignmenttype', PARAM_ALPHA);
27         $mform->setDefault('assignmenttype', $type);
28         $mform->addElement('hidden', 'type', $type);
29         $mform->setType('type', PARAM_ALPHA);
30         $mform->setDefault('type', $type);
32         require_once($CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php');
33         $assignmentclass = 'assignment_'.$type;
34         $assignmentinstance = new $assignmentclass();
36 //-------------------------------------------------------------------------------
37         $mform->addElement('header', 'general', get_string('general', 'form'));
39 //        $mform->addElement('static', 'statictype', get_string('assignmenttype', 'assignment'), get_string('type'.$type,'assignment'));
41         $mform->addElement('text', 'name', get_string('assignmentname', 'assignment'), array('size'=>'64'));
42         if (!empty($CFG->formatstringstriptags)) {
43             $mform->setType('name', PARAM_TEXT);
44         } else {
45             $mform->setType('name', PARAM_CLEANHTML);
46         }
47         $mform->addRule('name', null, 'required', null, 'client');
49         $this->add_intro_editor(true, get_string('description', 'assignment'));
51         $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'assignment'), array('optional'=>true));
52         $mform->setDefault('timeavailable', time());
53         $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'assignment'), array('optional'=>true));
54         $mform->setDefault('timedue', time()+7*24*3600);
56         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
58         $mform->addElement('select', 'preventlate', get_string('preventlate', 'assignment'), $ynoptions);
59         $mform->setDefault('preventlate', 0);
61         // hack to support pluggable assignment type titles
62         if (get_string_manager()->string_exists('type'.$type, 'assignment')) {
63             $typetitle = get_string('type'.$type, 'assignment');
64         } else {
65             $typetitle  = get_string('type'.$type, 'assignment_'.$type);
66         }
68         $this->standard_grading_coursemodule_elements();
70         $mform->addElement('header', 'typedesc', $typetitle);
72         $assignmentinstance->setup_elements($mform);
74         $this->standard_coursemodule_elements();
76         $this->add_action_buttons();
77     }
79     // Needed by plugin assignment types if they include a filemanager element in the settings form
80     function has_instance() {
81         return ($this->_instance != NULL);
82     }
84     // Needed by plugin assignment types if they include a filemanager element in the settings form
85     function get_context() {
86         return $this->context;
87     }
89     protected function get_assignment_instance() {
90         global $CFG, $DB;
92         if ($this->_assignmentinstance) {
93             return $this->_assignmentinstance;
94         }
95         if (!empty($this->_instance)) {
96             if($ass = $DB->get_record('assignment', array('id'=>$this->_instance))) {
97                 $type = $ass->assignmenttype;
98             } else {
99                 print_error('invalidassignment', 'assignment');
100             }
101         } else {
102             $type = required_param('type', PARAM_ALPHA);
103         }
104         require_once($CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php');
105         $assignmentclass = 'assignment_'.$type;
106         $this->assignmentinstance = new $assignmentclass();
107         return $this->assignmentinstance;
108     }
111     function data_preprocessing(&$default_values) {
112         // Allow plugin assignment types to preprocess form data (needed if they include any filemanager elements)
113         $this->get_assignment_instance()->form_data_preprocessing($default_values, $this);
114     }
117     function validation($data, $files) {
118         // Allow plugin assignment types to do any extra validation after the form has been submitted
119         $errors = parent::validation($data, $files);
120         $errors = array_merge($errors, $this->get_assignment_instance()->form_validation($data, $files));
121         return $errors;
122     }