MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / scorm / mod_form.php
1 <?php
2 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
3 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
5 class mod_scorm_mod_form extends moodleform_mod {
7     function definition() {
8         global $CFG, $COURSE;
9         $cfg_scorm = get_config('scorm');
11         $mform = $this->_form;
13         if (!$CFG->slasharguments) {
14             $mform->addElement('static', '', '',notify(get_string('slashargs', 'scorm'), 'notifyproblem', 'center', true));
15         }
16         $zlib = ini_get('zlib.output_compression'); //check for zlib compression - if used, throw error because of IE bug. - SEE MDL-16185
17         if (isset($zlib) && $zlib) {
18             $mform->addElement('static', '', '',notify(get_string('zlibwarning', 'scorm'), 'notifyproblem', 'center', true));
19         }
20 //-------------------------------------------------------------------------------
21         $mform->addElement('header', 'general', get_string('general', 'form'));
23 // Name
24         $mform->addElement('text', 'name', get_string('name'));
25         if (!empty($CFG->formatstringstriptags)) {
26             $mform->setType('name', PARAM_TEXT);
27         } else {
28             $mform->setType('name', PARAM_CLEAN);
29         }
30         $mform->addRule('name', null, 'required', null, 'client');
32 // Summary
33         $mform->addElement('htmleditor', 'intro', get_string('summary'));
34         $mform->setType('intro', PARAM_RAW);
35         $mform->addRule('intro', get_string('required'), 'required', null, 'client');
36         $mform->setHelpButton('intro', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
38 // Scorm types
39         $options = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
41         if ($cfg_scorm->allowtypeexternal) {
42             $options[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
43         }
45         if ($cfg_scorm->allowtypelocalsync) {
46             $options[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
47         }
49         if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
50             $options[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
51         }
53         $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $options);
55 // Reference
56         if (count($options) > 1) {
57             $mform->addElement('text', 'packageurl', get_string('url', 'scorm'), array('size'=>60));
58             $mform->setType('packageurl', PARAM_RAW);
59             $mform->setHelpButton('packageurl', array('packagefile', get_string('package', 'scorm'), 'scorm'));
60             $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
61         }
63 // New local package upload
64         $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
65         $mform->setMaxFileSize($maxbytes);
66         $mform->addElement('file', 'packagefile', get_string('package','scorm'));
67         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
69 //-------------------------------------------------------------------------------
70 // Time restrictions
71         $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
72         $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'scorm'));
73         $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","scorm"), "scorm"));
76         $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"));
77         $mform->disabledIf('timeopen', 'timerestrict');
79         $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"));
80         $mform->disabledIf('timeclose', 'timerestrict');
82 //-------------------------------------------------------------------------------
83 // Other Settings
84         $mform->addElement('header', 'advanced', get_string('othersettings', 'form'));
86 // Grade Method
87         $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
88         $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod', 'scorm'),'scorm'));
89         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
90         
91 // Maximum Grade
92         for ($i=0; $i<=100; $i++) {
93           $grades[$i] = "$i";
94         }
95         $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
96         $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
97         $mform->disabledIf('maxgrade', 'grademethod','eq', GRADESCOES);
99 // Attempts
100         $mform->addElement('static', '', '' ,'<hr />');
102 // Max Attempts
103         $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
104         $mform->setHelpButton('maxattempt', array('maxattempt',get_string('maximumattempts', 'scorm'), 'scorm'));
105         $mform->setDefault('maxattempt', $cfg_scorm->maxattempts);
106         
107 // Display attempt status
108         $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
109         $mform->setHelpButton('displayattemptstatus', array('displayattemptstatus',get_string('displayattemptstatus', 'scorm'), 'scorm'));
110         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
111         
112 // Display course structure
113         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
114         $mform->setHelpButton('displaycoursestructure', array('displaycoursestructure',get_string('displaycoursestructure', 'scorm'), 'scorm'));
115         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
116         
117 // Force completed
118         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
119         $mform->setHelpButton('forcecompleted', array('forcecompleted',get_string('forcecompleted', 'scorm'), 'scorm'));
120         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
121         $mform->setAdvanced('forcecompleted');
123 // Force new attempt
124         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
125         $mform->setHelpButton('forcenewattempt', array('forcenewattempt',get_string('forcenewattempt', 'scorm'), 'scorm'));
126         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
127         $mform->setAdvanced('forcenewattempt');
128         
129 // Last attempt lock - lock the enter button after the last available attempt has been made
130         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
131         $mform->setHelpButton('lastattemptlock', array('lastattemptlock',get_string('lastattemptlock', 'scorm'), 'scorm'));
132         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
133         $mform->setAdvanced('lastattemptlock');
134         
135 // What Grade
136         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
137         $mform->disabledIf('whatgrade', 'maxattempt','eq',1);
138         $mform->setHelpButton('whatgrade', array('whatgrade',get_string('whatgrade', 'scorm'), 'scorm'));
139         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
140         $mform->setAdvanced('whatgrade');
142 // Activation period
143 /*        $mform->addElement('static', '', '' ,'<hr />');
144         $mform->addElement('static', 'activation', get_string('activation','scorm'));
145         $datestartgrp = array();
146         $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
147         $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
148         $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
149         $mform->setDefault('startdate', 0);
150         $mform->setDefault('startdisabled', 1);
151         $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
153         $dateendgrp = array();
154         $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
155         $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
156         $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
157         $mform->setDefault('enddate', 0);
158         $mform->setDefault('enddisabled', 1);
159         $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
160 */
161         
162 // Stage Size
163         $mform->addElement('static', '', '' ,'<hr />');
164         $mform->addElement('static', 'stagesize', get_string('stagesize','scorm'));
165         $mform->setHelpButton('stagesize', array('stagesize',get_string('stagesize', 'scorm'), 'scorm'));
166 // Width
167         $mform->addElement('text', 'width', get_string('width','scorm'), 'maxlength="5" size="5"');
168         $mform->setDefault('width', $cfg_scorm->framewidth);
169         $mform->setType('width', PARAM_INT);
171 // Height
172         $mform->addElement('text', 'height', get_string('height','scorm'), 'maxlength="5" size="5"');
173         $mform->setDefault('height', $cfg_scorm->frameheight);
174         $mform->setType('height', PARAM_INT);
176 // Framed / Popup Window
177         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
178         $mform->setDefault('popup', $cfg_scorm->popup);
179         $mform->setAdvanced('popup');
181 // Window Options
182         $winoptgrp = array();
183         foreach(scorm_get_popup_options_array() as $key => $value){
184             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
185             $mform->setDefault($key, $value);
186         }
187         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options','scorm'), '<br />', false);
188         $mform->setAdvanced('winoptgrp');
189         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
191 // Skip view page
192         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'),scorm_get_skip_view_array());
193         $mform->setHelpButton('skipview', array('skipview',get_string('skipview', 'scorm'), 'scorm'));
194         $mform->setDefault('skipview', $cfg_scorm->skipview);
195         $mform->setAdvanced('skipview');
197 // Hide Browse
198         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
199         $mform->setHelpButton('hidebrowse', array('hidebrowse',get_string('hidebrowse', 'scorm'), 'scorm'));
200         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
201         $mform->setAdvanced('hidebrowse');
203 // Toc display
204         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
205         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
206         $mform->setAdvanced('hidetoc');
208 // Hide Navigation panel
209         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
210         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
211         $mform->setAdvanced('hidenav');
213 // Autocontinue
214         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
215         $mform->setHelpButton('auto', array('autocontinue',get_string('autocontinue', 'scorm'), 'scorm'));
216         $mform->setDefault('auto', $cfg_scorm->auto);
217         $mform->setAdvanced('auto');
219 // Update packages timing
220         $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
221         $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
222         $mform->setAdvanced('updatefreq');
224 //-------------------------------------------------------------------------------
225 // Hidden Settings
226         $mform->addElement('hidden', 'datadir', null);
227         $mform->addElement('hidden', 'pkgtype', null);
228         $mform->addElement('hidden', 'launch', null);
229         $mform->addElement('hidden', 'redirect', null);
230         $mform->addElement('hidden', 'redirecturl', null);
233 //-------------------------------------------------------------------------------
234         $this->standard_coursemodule_elements();
235 //-------------------------------------------------------------------------------
236         // buttons
237         $this->add_action_buttons();
239     }
241     function data_preprocessing(&$default_values) {
242         global $COURSE;
244         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
245             if (!empty($default_values['options'])) {
246                 $options = explode(',',$default_values['options']);
247                 foreach ($options as $option) {
248                     list($element,$value) = explode('=',$option);
249                     $element = trim($element);
250                     $default_values[$element] = trim($value);
251                 }
252             }
253         }
254         if (isset($default_values['grademethod'])) {
255             $default_values['whatgrade'] = intval($default_values['grademethod'] / 10);
256             $default_values['grademethod'] = $default_values['grademethod'] % 10;
257         }
258         if (isset($default_value['width']) && (strpos($default_value['width'],'%') === false) && ($default_value['width'] <= 100)) {
259             $default_value['width'] .= '%';
260         }
261         if (isset($default_value['width']) && (strpos($default_value['height'],'%') === false) && ($default_value['height'] <= 100)) {
262             $default_value['height'] .= '%';
263         }
264         $scorms = get_all_instances_in_course('scorm', $COURSE);
265         $coursescorm = current($scorms);
266         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
267             $default_values['redirect'] = 'yes';
268             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
269         } else {
270             $default_values['redirect'] = 'no';
271             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
272         }
273         if (isset($default_values['version'])) {
274             $default_values['pkgtype'] = (substr($default_values['version'],0,5) == 'SCORM') ? 'scorm':'aicc';
275         }
276         if (isset($default_values['instance'])) {
277             $default_values['datadir'] = $default_values['instance'];
278         }
279         if (empty($default_values['timeopen'])) {
280             $default_values['timerestrict'] = 0;
281         } else {
282             $default_values['timerestrict'] = 1;
283         }
284     }
286     function validation($data, $files) {
287         $errors = parent::validation($data, $files);
289         $type = $data['scormtype'];
291         if ($type === SCORM_TYPE_LOCAL) {
292             if (!empty($data['update'])) {
293                 //ok, not required
295             } else if (empty($files['packagefile'])) {
296                 $errors['packagefile'] = get_string('required');
298             } else {
299                 $packer = get_file_packer('application/zip');
301                 $filelist = $packer->list_files($files['packagefile']);
302                 if (!is_array($filelist)) {
303                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
304                 } else {
305                     $manifestpresent = false;
306                     $aiccfound       = false;
307                     foreach ($filelist as $info) {
308                         if ($info->pathname == 'imsmanifest.xml') {
309                             $manifestpresent = true;
310                             break;
311                         }
312                         if (preg_match('/\.cst$/', $info->pathname)) {
313                             $aiccfound = true;
314                             break;
315                         }
316                     }
317                     if (!$manifestpresent and !$aiccfound) {
318                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
319                     }
320                 }
321             }
323         } else if ($type === SCORM_TYPE_EXTERNAL) {
324             $reference = $data['packageurl'];
325             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
326                 $errors['packageurl'] = get_string('required'); // TODO: improve help
327             }
329         } else if ($type === 'packageurl') {
330             $reference = $data['reference'];
331             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
332                 $errors['packageurl'] = get_string('required'); // TODO: improve help
333             }
335         } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
336             $reference = $data['packageurl'];
337             if (stripos($reference, '#') !== 0) {
338                 $errors['packageurl'] = get_string('required');
339             }
340         }
342         return $errors;
343     }
345     //need to translate the "options" and "reference" field.
346     function set_data($default_values) {
347         $default_values = (array)$default_values;
349         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
350             switch ($default_values['scormtype']) {
351                 case SCORM_TYPE_LOCALSYNC :
352                 case SCORM_TYPE_EXTERNAL:
353                 case SCORM_TYPE_IMSREPOSITORY:
354                     $default_values['packageurl'] = $default_values['reference'];
355             }
356         }
357         unset($default_values['reference']);
359         if (!empty($default_values['options'])) {
360             $options = explode(',', $default_values['options']);
361             foreach ($options as $option) {
362                 $opt = explode('=', $option);
363                 if (isset($opt[1])) {
364                     $default_values[$opt[0]] = $opt[1];
365                 }
366             }
367         }
369         $this->data_preprocessing($default_values);
370         parent::set_data($default_values);
371     }
373 ?>