de15a8061ca2076fa8a60bee00cf9305d8e09c0f
[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->addElement('text', 'name', get_string('name'), array('size' => '40'));
59         }
60         $mform->setType('namesource', PARAM_INT);
61         if (!empty($CFG->formatstringstriptags)) {
62             $mform->setType('name', PARAM_TEXT);
63         } else {
64             $mform->setType('name', PARAM_CLEAN);
65         }
67 // Location
68         $sitecontext = get_context_instance(CONTEXT_SYSTEM);
69         if (has_capability('moodle/course:managefiles', $sitecontext)) {
70             $site = get_site();
71             if ($COURSE->id==$site->id) {
72                 $id = $site->id;
73                 $location = HOTPOT_LOCATION_SITEFILES;
74             } else {
75                 $id = "'+(getObjValue(this.form.location)==".HOTPOT_LOCATION_SITEFILES."?".$site->id.":".$COURSE->id.")+'";
76                 $location = '';
77             }
78         } else { // ordinary teacher or content creator
79                 $id = $COURSE->id;
80                 $location = HOTPOT_LOCATION_COURSEFILES;
81         }
82         if (array_key_exists($location, $HOTPOT_LOCATION)) {
83             $mform->addElement('hidden', 'location', $location);
84         } else { // admin can select from "site" or "course" files
85            $mform->addElement('select', 'location', get_string('location', 'hotpot'), $HOTPOT_LOCATION);
86         }
87         $mform->setType('location', PARAM_INT);
89 // Reference
90         // create "Choose file" button
91         $choosefile_button = $mform->createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
93         // create a 'reference' group of form elements, comprising text box + buttons
94         $elements = array();
95         $elements[] = $mform->createElement('text', 'reference', '', array('size'=>'60'));
96         $elements[] = &$choosefile_button;
97         $mform->addGroup($elements, 'reference_elements', get_string('filename', 'resource'), ' ' , false);
99         // set attributes on the button
100         if ($choosefile_button) {
101             $wdir = "'+getDir(this.form.reference.value)+'";
102             $url="/files/index.php?id=$id&wdir=$wdir&choose=id_reference";
103             $options = 'menubar=0,location=0,scrollbars,resizable,width=750,height=500';
104             $attributes = array(
105                 'title'=>get_string('chooseafile', 'resource'),
106                 'onclick'=>"return openpopup('$url', '".$choosefile_button->getName()."', '$options', 0);"
107             );
108             $choosefile_button->updateAttributes($attributes);
109         }
110         $mform->setType('reference', PARAM_TEXT);
112 // Summary
113         if (isset($form->add)) {
114             // new HotPot
115             $elements = array();
116             $elements[] = &$mform->createElement('select', 'summarysource', '', $HOTPOT_TEXTSOURCE);
117             $elements[] = &$mform->createElement('htmleditor', 'summary', '');
118             $mform->addGroup($elements, 'summary_elements', get_string('summary'), array(' '), false);
119             $mform->setAdvanced('summary_elements');
120         } else {
121             // existing HotPot
122             $mform->addElement('hidden', 'summarysource', HOTPOT_TEXTSOURCE_SPECIFIC);
123             $mform->addElement('htmleditor', 'summary', get_string('summary'));
124             $mform->setType('summary', PARAM_RAW);
125             $mform->setHelpButton('summary', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
126             $mform->setAdvanced('summary');
127         }
128         $mform->setType('summarysource', PARAM_INT);
129         $mform->setType('summary', PARAM_RAW);
131 // Add/Update quiz chain?
132         if (isset($form->add)) {
133             $quizchain = 'addquizchain';
134         } else {
135             $quizchain = 'updatequizchain';
136         }
137         $mform->addElement('selectyesno', 'quizchain', get_string($quizchain, 'hotpot'));
138         $mform->setDefault('quizchain', get_user_preferences("hotpot_$quizchain", HOTPOT_NO));
139         $mform->setHelpButton('quizchain', array($quizchain, get_string($quizchain, 'hotpot'), 'hotpot'));
140         // $mform->setAdvanced('quizchain');
142 //-----------------------------------------------------------------------------------------------
143         $mform->addElement('header', 'displayhdr', get_string('display', 'form'));
144 //-----------------------------------------------------------------------------------------------
146 // Output format
147         $mform->addElement('select', 'outputformat', get_string('outputformat', 'hotpot'), $HOTPOT_OUTPUTFORMAT);
148         $mform->setDefault('outputformat', get_user_preferences('hotpot_outputformat', HOTPOT_OUTPUTFORMAT_BEST));
149         $mform->setHelpButton('outputformat', array('outputformat', get_string('outputformat', 'hotpot'), 'hotpot'));
151 // Navigation
152         $mform->addElement('select', 'navigation', get_string('navigation', 'hotpot'), $HOTPOT_NAVIGATION);
153         $mform->setDefault('navigation', get_user_preferences('hotpot_navigation', HOTPOT_NAVIGATION_BAR));
154         $mform->setHelpButton('navigation', array('navigation', get_string('navigation', 'hotpot'), 'hotpot'));
156 // Use Moode player ?
157         $mform->addElement('selectyesno', 'forceplugins', get_string('forceplugins', 'hotpot'));
158         $mform->setDefault('forceplugins', get_user_preferences('hotpot_forceplugins', HOTPOT_NO));
159         $mform->setHelpButton('forceplugins', array('forceplugins', get_string('forceplugins', 'hotpot'), 'hotpot'));
160         // $mform->setAdvanced('forceplugins');
162 // Student feedback
163         $elements = array();
164         $elements[] = &$mform->createElement('select', 'studentfeedback', '', $HOTPOT_FEEDBACK);
165         $elements[] = &$mform->createElement('text', 'studentfeedbackurl', '', array('size'=>'50'));
166         $mform->addGroup($elements, 'studentfeedback_elements', get_string('studentfeedback', 'hotpot'), array(' '), false);
167         $mform->setHelpButton('studentfeedback_elements', array('studentfeedback', get_string('studentfeedback', 'hotpot'), 'hotpot'));
168         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_NONE);
169         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_MOODLEFORUM);
170         $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', HOTPOT_FEEDBACK_MOODLEMESSAGING);
171         // $mform->setAdvanced('studentfeedback_elements');
172         $mform->setType('studentfeedbackurl', PARAM_URL);
174 // Show next quiz ?
175         $mform->addElement('selectyesno', 'shownextquiz', get_string('shownextquiz', 'hotpot'));
176         $mform->setDefault('shownextquiz', get_user_preferences('hotpot_shownextquiz', HOTPOT_NO));
177         $mform->setHelpButton('shownextquiz', array('shownextquiz', get_string('shownextquiz', 'hotpot'), 'hotpot'));
178         // $mform->setAdvanced('forceplugins');
180 //-----------------------------------------------------------------------------------------------
181         $mform->addElement('header', 'accesscontrolhdr', get_string('accesscontrol', 'lesson'));
182 //-----------------------------------------------------------------------------------------------
184 // Open time
185         $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional'=>true));
186         $mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
188 // Close time
189         $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional'=>true));
190         $mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
192 // Password
193         $mform->addElement('text', 'password', get_string('requirepassword', 'quiz'));
194         $mform->setType('password', PARAM_TEXT);
195         $mform->setHelpButton('password', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
196         // $mform->setAdvanced('password');
198 // Subnet
199         $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
200         $mform->setType('subnet', PARAM_TEXT);
201         $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz'));
202         $mform->setDefault('subnet', get_user_preferences('hotpot_subnet'));
203         // $mform->setAdvanced('subnet');
205 // Allow review?
206         $mform->addElement('selectyesno', 'review', get_string('allowreview', 'quiz'));
207         $mform->setDefault('review', get_user_preferences('hotpot_review', HOTPOT_YES));
208         $mform->setHelpButton('review', array('review', get_string('allowreview', 'quiz'), 'quiz'));
209         // $mform->setAdvanced('forceplugins');
211 // Maximum number of attempts
212         $options = array(
213             0 => get_string("attemptsunlimited", "quiz"),
214             1 => '1 '.strtolower(get_string("attempt", "quiz"))
215         );
216         for ($i=2; $i<=10; $i++) {
217             $options[$i] = "$i ".strtolower(get_string("attempts", "quiz"));
218         }
219         $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $options);
220         $mform->setDefault('attempts', get_user_preferences('hotpot_attempts', 0)); // 0=unlimited
221         $mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed', 'quiz'), 'quiz'));
222         // $mform->setAdvanced('forceplugins');
224 //-----------------------------------------------------------------------------------------------
225         $mform->addElement('header', 'gradeshdr', get_string('grades', 'grades'));
226 //-----------------------------------------------------------------------------------------------
228 // Maximum grade
229         $options = array();
230         for ($i=100; $i>=1; $i--) {
231             $options[$i] = $i;
232         }
233         $options[0] = get_string("nograde");
234         $mform->addElement('select', 'grade', get_string('maximumgrade'), $options);
235         $mform->setDefault('grade', get_user_preferences('hotpot_grade', 100));
236         $mform->setHelpButton('grade', array('maxgrade', get_string('maximumgrade'), 'quiz'));
237         // $mform->setAdvanced('forceplugins');
239 // Maximum grading method
240         $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), $HOTPOT_GRADEMETHOD);
241         $mform->setDefault('grademethod', get_user_preferences('hotpot_grademethod', HOTPOT_GRADEMETHOD_HIGHEST));
242         $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod', 'quiz'), 'quiz'));
243         // $mform->setAdvanced('forceplugins');
245 //-----------------------------------------------------------------------------------------------
246         $mform->addElement('header', 'reportshdr', get_string('reports'));
247 //-----------------------------------------------------------------------------------------------
249 // Enable click reporting?
250         $mform->addElement('selectyesno', 'clickreporting', get_string('clickreporting', 'hotpot'));
251         $mform->setDefault('clickreporting', get_user_preferences('hotpot_clickreporting', HOTPOT_NO));
252         $mform->setHelpButton('clickreporting', array('clickreporting', get_string('clickreporting', 'hotpot'), 'hotpot'));
253         // $mform->setAdvanced('forceplugins');
255 //----------------------------------------------
256         $this->standard_coursemodule_elements();
257 //----------------------------------------------
259         $this->add_action_buttons();
261         $js = '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mod_form.js"></script>';
262         $mform->addElement('static', 'hotpot_mod_form_js', '', $js);
263     }
265     function data_preprocessing(&$defaults){
266     }
268     function validation($data, $files) {
269         global $CFG, $USER, $DB, $COURSE;
271         $errors = parent::validation($data, $files);
273 // location
274         if (empty($data['location'])) {
275             // this shouldn't happen
276             $data['location'] = $COURSE->id;
277         } else {
278             if ($data['location']==$COURSE->id) {
279                 // this is normal
280             } else if ($data['location']==SITEID && has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_SYSTEM))) {
281                 // admin can access site files
282             } else {
283                 // location is invalid or missing, so set to default
284                 $data['location'] = $COURSE->id;
285             }
286         }
288 // reference
289         if (isset($data['reference'])) {
290             $data['reference'] = trim($data['reference']);
291         }
292         if (empty($data['reference'])) {
293             $errors['reference_elements'] = get_string('error_nofilename', 'hotpot');
294         } else {
295             if (preg_match('|^https?://|', $data['reference'])) {
296                 // URL
297                 $errors['reference_elements'] = 'Sorry, handling of URLs is not implemented yet';
298             } else {
299                 // course files
300                 $filepath = $CFG->dataroot.'/'.$data['location'].'/'.$data['reference'];
301                 if (! file_exists($filepath)) {
302                     $errors['reference_elements'] = get_string('error_pathdoesnotexist', 'hotpot', $filepath);
303                 } else if (! $data['quizchain'] && ! is_file($filepath)) {
304                     $errors['reference_elements'] = get_string('error_folderwithoutquizchain', 'hotpot');
305                 }
306             }
307         }
309 // studentfeedbackurl
310         if (empty($data['studentfeedbackurl']) || $data['studentfeedbackurl']=='http://') {
311             $data['studentfeedbackurl'] = '';
312             $error = false;
313             if ($data['studentfeedback']==HOTPOT_FEEDBACK_WEBPAGE) {
314                 $error = true;
315             }
316             if ($data['studentfeedback']==HOTPOT_FEEDBACK_FORMMAIL) {
317                 $error = true;
318             }
319             if ($error) {
320                 $errors['studentfeedback_elements']= get_string('error_nofeedbackurlformmail', 'hotpot');
321             }
322         }
324         return $errors;
325     }
327 ?>