MDL-20353 adding missing data types of hidden form fields
[moodle.git] / mod / hotpot / mod_form.php
1 <?php
2 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
3 require_once ($CFG->dirroot.'/mod/hotpot/lib.php');
5 $HOTPOT_TEXTSOURCE = array(
6     HOTPOT_TEXTSOURCE_QUIZ => get_string('textsourcequiz', 'hotpot'),
7     HOTPOT_TEXTSOURCE_FILENAME => get_string('textsourcefilename', 'hotpot'),
8     HOTPOT_TEXTSOURCE_FILEPATH => get_string('textsourcefilepath', 'hotpot'),
9     HOTPOT_TEXTSOURCE_SPECIFIC => get_string('textsourcespecific', 'hotpot')
10 );
12 class mod_hotpot_mod_form extends moodleform_mod {
13     // documentation on formslib.php here:
14     // http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition
16     function definition() {
17         // TO DO
18         // =====
19         // $mform->setType('name', PARAM_xxx);
20         // $mform->setDefault('name', array('elementhelpfilename', get_string('helpicontitlestring', 'hotpot'), 'hotpot'));
22         global $CFG, $COURSE;
23         global $HOTPOT_FEEDBACK, $HOTPOT_GRADEMETHOD, $HOTPOT_LOCATION;
24         global $HOTPOT_NAVIGATION, $HOTPOT_OUTPUTFORMAT, $HOTPOT_TEXTSOURCE;
26         $mform =&$this->_form;
28         // initialize values for $hours, $minutes and $seconds
29         $hours = array();
30         $minutes = array();
31         $seconds = array();
32         for ($i=0; $i<60; $i++) {
33             $str = sprintf('%02d', $i);
34             if ($i<24) {
35                 $hours[$i] = $str;
36             }
37             $minutes[$i] = $str;
38             $seconds[$i] = $str;
39         }
41 //-----------------------------------------------------------------------------------------------
42         $mform->addElement('header', 'general', get_string('general', 'form'));
43 //-----------------------------------------------------------------------------------------------
45 // Name
46         global $form;
47         if (isset($form->add)) {
48             // new HotPot
49             $elements = array();
50             $elements[] = &$mform->createElement('select', 'namesource', '', $HOTPOT_TEXTSOURCE);
51             $elements[] = &$mform->createElement('text', 'name', '', array('size' => '40'));
52             $mform->addGroup($elements, 'name_elements', get_string('name'), array(' '), false);
53             $mform->disabledIf('name_elements', 'namesource', 'ne', HOTPOT_TEXTSOURCE_SPECIFIC);
54             // $mform->setAdvanced('name_elements');
55         } else {
56             // existing HotPot
57             $mform->addElement('hidden', 'namesource', HOTPOT_TEXTSOURCE_SPECIFIC);
58             $mform->setType('namesource', PARAM_RAW);
59             $mform->addElement('text', 'name', get_string('name'), array('size' => '40'));
60         }
61         $mform->setType('namesource', PARAM_INT);
62         if (!empty($CFG->formatstringstriptags)) {
63             $mform->setType('name', PARAM_TEXT);
64         } else {
65             $mform->setType('name', PARAM_CLEAN);
66         }
68 // Location
69         $sitecontext = get_context_instance(CONTEXT_SYSTEM);
70         if (has_capability('moodle/course:managefiles', $sitecontext)) {
71             $site = get_site();
72             if ($COURSE->id==$site->id) {
73                 $id = $site->id;
74                 $location = HOTPOT_LOCATION_SITEFILES;
75             } else {
76                 $id = "'+(getObjValue(this.form.location)==".HOTPOT_LOCATION_SITEFILES."?".$site->id.":".$COURSE->id.")+'";
77                 $location = '';
78             }
79         } else { // ordinary teacher or content creator
80                 $id = $COURSE->id;
81                 $location = HOTPOT_LOCATION_COURSEFILES;
82         }
83         if (array_key_exists($location, $HOTPOT_LOCATION)) {
84             $mform->addElement('hidden', 'location', $location);
85             $mform->setType('location', PARAM_RAW);
86         } else { // admin can select from "site" or "course" files
87            $mform->addElement('select', 'location', get_string('location', 'hotpot'), $HOTPOT_LOCATION);
88         }
89         $mform->setType('location', PARAM_INT);
91 // Reference
92         // create "Choose file" button
93         $choosefile_button = $mform->createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
95         // create a 'reference' group of form elements, comprising text box + buttons
96         $elements = array();
97         $elements[] = $mform->createElement('text', 'reference', '', array('size'=>'60'));
98         $elements[] = &$choosefile_button;
99         $mform->addGroup($elements, 'reference_elements', get_string('filename', 'resource'), ' ' , false);
101         // set attributes on the button
102         if ($choosefile_button) {
103             $wdir = "'+getDir(this.form.reference.value)+'";
104             $url="/files/index.php?id=$id&wdir=$wdir&choose=id_reference";
105             $options = 'menubar=0,location=0,scrollbars,resizable,width=750,height=500';
106             $attributes = array(
107                 'title'=>get_string('chooseafile', 'resource'),
108                 'onclick'=>"return openpopup('$url', '".$choosefile_button->getName()."', '$options', 0);"
109             );
110             $choosefile_button->updateAttributes($attributes);
111         }
112         $mform->setType('reference', PARAM_TEXT);
114 // Summary
115         if (isset($form->add)) {
116             // new HotPot
117             $elements = array();
118             $elements[] = &$mform->createElement('select', 'summarysource', '', $HOTPOT_TEXTSOURCE);
119             $elements[] = &$mform->createElement('htmleditor', 'summary', '');
120             $mform->addGroup($elements, 'summary_elements', get_string('summary'), array(' '), false);
121             $mform->setAdvanced('summary_elements');
122         } else {
123             // existing HotPot
124             $mform->addElement('hidden', 'summarysource', HOTPOT_TEXTSOURCE_SPECIFIC);
125             $mform->setType('summarysource', PARAM_RAW);
126             $mform->addElement('htmleditor', 'summary', get_string('summary'));
127             $mform->setType('summary', PARAM_RAW);
128             $mform->setHelpButton('summary', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
129             $mform->setAdvanced('summary');
130         }
131         $mform->setType('summarysource', PARAM_INT);
132         $mform->setType('summary', PARAM_RAW);
134 // Add/Update quiz chain?
135         if (isset($form->add)) {
136             $quizchain = 'addquizchain';
137         } else {
138             $quizchain = 'updatequizchain';
139         }
140         $mform->addElement('selectyesno', 'quizchain', get_string($quizchain, 'hotpot'));
141         $mform->setDefault('quizchain', get_user_preferences("hotpot_$quizchain", HOTPOT_NO));
142         $mform->setHelpButton('quizchain', array($quizchain, get_string($quizchain, 'hotpot'), 'hotpot'));
143         // $mform->setAdvanced('quizchain');
145 //-----------------------------------------------------------------------------------------------
146         $mform->addElement('header', 'displayhdr', get_string('display', 'form'));
147 //-----------------------------------------------------------------------------------------------
149 // Output format
150         $mform->addElement('select', 'outputformat', get_string('outputformat', 'hotpot'), $HOTPOT_OUTPUTFORMAT);
151         $mform->setDefault('outputformat', get_user_preferences('hotpot_outputformat', HOTPOT_OUTPUTFORMAT_BEST));
152         $mform->setHelpButton('outputformat', array('outputformat', get_string('outputformat', 'hotpot'), 'hotpot'));
154 // Navigation
155         $mform->addElement('select', 'navigation', get_string('navigation', 'hotpot'), $HOTPOT_NAVIGATION);
156         $mform->setDefault('navigation', get_user_preferences('hotpot_navigation', HOTPOT_NAVIGATION_BAR));
157         $mform->setHelpButton('navigation', array('navigation', get_string('navigation', 'hotpot'), 'hotpot'));
159 // Use Moode player ?
160         $mform->addElement('selectyesno', 'forceplugins', get_string('forceplugins', 'hotpot'));
161         $mform->setDefault('forceplugins', get_user_preferences('hotpot_forceplugins', HOTPOT_NO));
162         $mform->setHelpButton('forceplugins', array('forceplugins', get_string('forceplugins', 'hotpot'), 'hotpot'));
163         // $mform->setAdvanced('forceplugins');
165 // Student feedback
166         $elements = array();
167         $elements[] = &$mform->createElement('select', 'studentfeedback', '', $HOTPOT_FEEDBACK);
168         $elements[] = &$mform->createElement('text', 'studentfeedbackurl', '', array('size'=>'50'));
169         $mform->addGroup($elements, 'studentfeedback_elements', get_string('studentfeedback', 'hotpot'), array(' '), false);
170         $mform->setHelpButton('studentfeedback_elements', array('studentfeedback', get_string('studentfeedback', 'hotpot'), 'hotpot'));
171         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_NONE);
172         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_MOODLEFORUM);
173         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_MOODLEMESSAGING);
174         // $mform->setAdvanced('studentfeedback_elements');
175         $mform->setType('studentfeedbackurl', PARAM_URL);
177 // Show next quiz ?
178         $mform->addElement('selectyesno', 'shownextquiz', get_string('shownextquiz', 'hotpot'));
179         $mform->setDefault('shownextquiz', get_user_preferences('hotpot_shownextquiz', HOTPOT_NO));
180         $mform->setHelpButton('shownextquiz', array('shownextquiz', get_string('shownextquiz', 'hotpot'), 'hotpot'));
181         // $mform->setAdvanced('forceplugins');
183 //-----------------------------------------------------------------------------------------------
184         $mform->addElement('header', 'accesscontrolhdr', get_string('accesscontrol', 'lesson'));
185 //-----------------------------------------------------------------------------------------------
187 // Open time
188         $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional'=>true));
189         $mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
191 // Close time
192         $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional'=>true));
193         $mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
195 // Password
196         $mform->addElement('text', 'password', get_string('requirepassword', 'quiz'));
197         $mform->setType('password', PARAM_TEXT);
198         $mform->setHelpButton('password', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
199         // $mform->setAdvanced('password');
201 // Subnet
202         $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
203         $mform->setType('subnet', PARAM_TEXT);
204         $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz'));
205         $mform->setDefault('subnet', get_user_preferences('hotpot_subnet'));
206         // $mform->setAdvanced('subnet');
208 // Allow review?
209         $mform->addElement('selectyesno', 'review', get_string('allowreview', 'quiz'));
210         $mform->setDefault('review', get_user_preferences('hotpot_review', HOTPOT_YES));
211         $mform->setHelpButton('review', array('review', get_string('allowreview', 'quiz'), 'quiz'));
212         // $mform->setAdvanced('forceplugins');
214 // Maximum number of attempts
215         $options = array(
216             0 => get_string("attemptsunlimited", "quiz"),
217             1 => '1 '.strtolower(get_string("attempt", "quiz"))
218         );
219         for ($i=2; $i<=10; $i++) {
220             $options[$i] = "$i ".strtolower(get_string("attempts", "quiz"));
221         }
222         $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $options);
223         $mform->setDefault('attempts', get_user_preferences('hotpot_attempts', 0)); // 0=unlimited
224         $mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed', 'quiz'), 'quiz'));
225         // $mform->setAdvanced('forceplugins');
227 //-----------------------------------------------------------------------------------------------
228         $mform->addElement('header', 'gradeshdr', get_string('grades', 'grades'));
229 //-----------------------------------------------------------------------------------------------
231 // Maximum grade
232         $options = array();
233         for ($i=100; $i>=1; $i--) {
234             $options[$i] = $i;
235         }
236         $options[0] = get_string("nograde");
237         $mform->addElement('select', 'grade', get_string('maximumgrade'), $options);
238         $mform->setDefault('grade', get_user_preferences('hotpot_grade', 100));
239         $mform->setHelpButton('grade', array('maxgrade', get_string('maximumgrade'), 'quiz'));
240         // $mform->setAdvanced('forceplugins');
242 // Maximum grading method
243         $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), $HOTPOT_GRADEMETHOD);
244         $mform->setDefault('grademethod', get_user_preferences('hotpot_grademethod', HOTPOT_GRADEMETHOD_HIGHEST));
245         $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod', 'quiz'), 'quiz'));
246         // $mform->setAdvanced('forceplugins');
248 //-----------------------------------------------------------------------------------------------
249         $mform->addElement('header', 'reportshdr', get_string('reports'));
250 //-----------------------------------------------------------------------------------------------
252 // Enable click reporting?
253         $mform->addElement('selectyesno', 'clickreporting', get_string('clickreporting', 'hotpot'));
254         $mform->setDefault('clickreporting', get_user_preferences('hotpot_clickreporting', HOTPOT_NO));
255         $mform->setHelpButton('clickreporting', array('clickreporting', get_string('clickreporting', 'hotpot'), 'hotpot'));
256         // $mform->setAdvanced('forceplugins');
258 //----------------------------------------------
259         $this->standard_coursemodule_elements();
260 //----------------------------------------------
262         $this->add_action_buttons();
264         $js = '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mod_form.js"></script>';
265         $mform->addElement('static', 'hotpot_mod_form_js', '', $js);
266     }
268     function data_preprocessing(&$defaults){
269     }
271     function validation($data, $files) {
272         global $CFG, $USER, $DB, $COURSE;
274         $errors = parent::validation($data, $files);
276 // location
277         if (empty($data['location'])) {
278             // this shouldn't happen
279             $data['location'] = $COURSE->id;
280         } else {
281             if ($data['location']==$COURSE->id) {
282                 // this is normal
283             } else if ($data['location']==SITEID && has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_SYSTEM))) {
284                 // admin can access site files
285             } else {
286                 // location is invalid or missing, so set to default
287                 $data['location'] = $COURSE->id;
288             }
289         }
291 // reference
292         if (isset($data['reference'])) {
293             $data['reference'] = trim($data['reference']);
294         }
295         if (empty($data['reference'])) {
296             $errors['reference_elements'] = get_string('error_nofilename', 'hotpot');
297         } else {
298             if (preg_match('|^https?://|', $data['reference'])) {
299                 // URL
300                 $errors['reference_elements'] = 'Sorry, handling of URLs is not implemented yet';
301             } else {
302                 // course files
303                 $filepath = $CFG->dataroot.'/'.$data['location'].'/'.$data['reference'];
304                 if (! file_exists($filepath)) {
305                     $errors['reference_elements'] = get_string('error_pathdoesnotexist', 'hotpot', $filepath);
306                 } else if (! $data['quizchain'] && ! is_file($filepath)) {
307                     $errors['reference_elements'] = get_string('error_folderwithoutquizchain', 'hotpot');
308                 }
309             }
310         }
312 // studentfeedbackurl
313         if (empty($data['studentfeedbackurl']) || $data['studentfeedbackurl']=='http://') {
314             $data['studentfeedbackurl'] = '';
315             $error = false;
316             if ($data['studentfeedback']==HOTPOT_FEEDBACK_WEBPAGE) {
317                 $error = true;
318             }
319             if ($data['studentfeedback']==HOTPOT_FEEDBACK_FORMMAIL) {
320                 $error = true;
321             }
322             if ($error) {
323                 $errors['studentfeedback_elements']= get_string('error_nofeedbackurlformmail', 'hotpot');
324             }
325         }
327         return $errors;
328     }
330 ?>