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