MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / mod / scorm / 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/scorm/locallib.php');
9 class mod_scorm_mod_form extends moodleform_mod {
11     function definition() {
12         global $CFG, $COURSE, $OUTPUT;
13         $cfg_scorm = get_config('scorm');
15         $mform = $this->_form;
17         if (!$CFG->slasharguments) {
18             $mform->addElement('static', '', '',$OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
19         }
20         $zlib = ini_get('zlib.output_compression'); //check for zlib compression - if used, throw error because of IE bug. - SEE MDL-16185
21         if (isset($zlib) && $zlib) {
22             $mform->addElement('static', '', '',$OUTPUT->notification(get_string('zlibwarning', 'scorm'), 'notifyproblem'));
23         }
24 //-------------------------------------------------------------------------------
25         $mform->addElement('header', 'general', get_string('general', 'form'));
27 // Name
28         $mform->addElement('text', 'name', get_string('name'));
29         if (!empty($CFG->formatstringstriptags)) {
30             $mform->setType('name', PARAM_TEXT);
31         } else {
32             $mform->setType('name', PARAM_CLEAN);
33         }
34         $mform->addRule('name', null, 'required', null, 'client');
36 // Summary
37         $this->add_intro_editor(true);
39 // Scorm types
40         $options = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
42         if ($cfg_scorm->allowtypeexternal) {
43             $options[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
44         }
46         if ($cfg_scorm->allowtypelocalsync) {
47             $options[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
48         }
50         if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
51             $options[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
52         }
54         $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $options);
56 // Reference
57         if (count($options) > 1) {
58             $mform->addElement('text', 'packageurl', get_string('url', 'scorm'), array('size'=>60));
59             $mform->setType('packageurl', PARAM_RAW);
60             $mform->addHelpButton('packageurl', 'package', 'scorm');
61             $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
62         }
64 // New local package upload
65         $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
66         $mform->setMaxFileSize($maxbytes);
67         $mform->addElement('file', 'packagefile', get_string('package','scorm'));
68         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
70 //-------------------------------------------------------------------------------
71 // Time restrictions
72         $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
73         $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', '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->addHelpButton('grademethod', 'grademethod', 'scorm');
89         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
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->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
105         $mform->setDefault('maxattempt', $cfg_scorm->maxattempts);
107 // What Grade
108         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
109         $mform->disabledIf('whatgrade', 'maxattempt','eq',1);
110         $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
111         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
112         $mform->setAdvanced('whatgrade');
114 // Display attempt status
115         $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
116         $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
117         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
119 // Force completed
120         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
121         $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
122         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
123         $mform->setAdvanced('forcecompleted');
125 // Force new attempt
126         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
127         $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
128         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
129         $mform->setAdvanced('forcenewattempt');
131 // Last attempt lock - lock the enter button after the last available attempt has been made
132         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
133         $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
134         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
135         $mform->setAdvanced('lastattemptlock');
137 // Activation period
138 /*        $mform->addElement('static', '', '' ,'<hr />');
139         $mform->addElement('static', 'activation', get_string('activation','scorm'));
140         $datestartgrp = array();
141         $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
142         $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
143         $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
144         $mform->setDefault('startdate', 0);
145         $mform->setDefault('startdisabled', 1);
146         $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
148         $dateendgrp = array();
149         $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
150         $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
151         $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
152         $mform->setDefault('enddate', 0);
153         $mform->setDefault('enddisabled', 1);
154         $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
155 */
157 // Stage Size
158         $mform->addElement('static', '', '' ,'<hr />');
159         $mform->addElement('static', 'stagesize', get_string('stagesize','scorm'));
160         $mform->addHelpButton('stagesize', 'stagesize', 'scorm');
161 // Width
162         $mform->addElement('text', 'width', get_string('width','scorm'), 'maxlength="5" size="5"');
163         $mform->setDefault('width', $cfg_scorm->framewidth);
164         $mform->setType('width', PARAM_INT);
166 // Height
167         $mform->addElement('text', 'height', get_string('height','scorm'), 'maxlength="5" size="5"');
168         $mform->setDefault('height', $cfg_scorm->frameheight);
169         $mform->setType('height', PARAM_INT);
171 // Framed / Popup Window
172         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
173         $mform->setDefault('popup', $cfg_scorm->popup);
174         $mform->setAdvanced('popup');
176 // Window Options
177         $winoptgrp = array();
178         foreach(scorm_get_popup_options_array() as $key => $value){
179             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
180             $mform->setDefault($key, $value);
181         }
182         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options','scorm'), '<br />', false);
183         $mform->setAdvanced('winoptgrp');
184         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
186 // Skip view page
187         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'),scorm_get_skip_view_array());
188         $mform->addHelpButton('skipview', 'skipview', 'scorm');
189         $mform->setDefault('skipview', $cfg_scorm->skipview);
190         $mform->setAdvanced('skipview');
192 // Hide Browse
193         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
194         $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
195         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
196         $mform->setAdvanced('hidebrowse');
198 // Display course structure
199         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
200         $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
201         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
202         $mform->setAdvanced('displaycoursestructure');
204 // Toc display
205         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
206         $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
207         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
208         $mform->setAdvanced('hidetoc');
210 // Hide Navigation panel
211         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
212         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
213         $mform->setAdvanced('hidenav');
215 // Autocontinue
216         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
217         $mform->addHelpButton('auto', 'autocontinue', 'scorm');
218         $mform->setDefault('auto', $cfg_scorm->auto);
219         $mform->setAdvanced('auto');
221 // Update packages timing
222         $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
223         $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
224         $mform->setAdvanced('updatefreq');
226 //-------------------------------------------------------------------------------
227 // Hidden Settings
228         $mform->addElement('hidden', 'datadir', null);
229         $mform->setType('datadir', PARAM_RAW);
230         $mform->addElement('hidden', 'pkgtype', null);
231         $mform->setType('pkgtype', PARAM_RAW);
232         $mform->addElement('hidden', 'launch', null);
233         $mform->setType('launch', PARAM_RAW);
234         $mform->addElement('hidden', 'redirect', null);
235         $mform->setType('redirect', PARAM_RAW);
236         $mform->addElement('hidden', 'redirecturl', null);
237         $mform->setType('redirecturl', PARAM_RAW);
240 //-------------------------------------------------------------------------------
241         $this->standard_coursemodule_elements();
242 //-------------------------------------------------------------------------------
243         // buttons
244         $this->add_action_buttons();
246     }
248     function data_preprocessing(&$default_values) {
249         global $COURSE;
251         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
252             if (!empty($default_values['options'])) {
253                 $options = explode(',',$default_values['options']);
254                 foreach ($options as $option) {
255                     list($element,$value) = explode('=',$option);
256                     $element = trim($element);
257                     $default_values[$element] = trim($value);
258                 }
259             }
260         }
261         if (isset($default_values['grademethod'])) {
262             $default_values['grademethod'] = intval($default_values['grademethod']);
263         }
264         if (isset($default_value['width']) && (strpos($default_value['width'],'%') === false) && ($default_value['width'] <= 100)) {
265             $default_value['width'] .= '%';
266         }
267         if (isset($default_value['width']) && (strpos($default_value['height'],'%') === false) && ($default_value['height'] <= 100)) {
268             $default_value['height'] .= '%';
269         }
270         $scorms = get_all_instances_in_course('scorm', $COURSE);
271         $coursescorm = current($scorms);
272         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
273             $default_values['redirect'] = 'yes';
274             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
275         } else {
276             $default_values['redirect'] = 'no';
277             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
278         }
279         if (isset($default_values['version'])) {
280             $default_values['pkgtype'] = (substr($default_values['version'],0,5) == 'SCORM') ? 'scorm':'aicc';
281         }
282         if (isset($default_values['instance'])) {
283             $default_values['datadir'] = $default_values['instance'];
284         }
285         if (empty($default_values['timeopen'])) {
286             $default_values['timerestrict'] = 0;
287         } else {
288             $default_values['timerestrict'] = 1;
289         }
290     }
292     function validation($data, $files) {
293         $errors = parent::validation($data, $files);
295         $type = $data['scormtype'];
297         if ($type === SCORM_TYPE_LOCAL) {
298             if (!empty($data['update'])) {
299                 //ok, not required
301             } else if (empty($files['packagefile'])) {
302                 $errors['packagefile'] = get_string('required');
304             } else {
305                 $packer = get_file_packer('application/zip');
307                 $filelist = $packer->list_files($files['packagefile']);
308                 if (!is_array($filelist)) {
309                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
310                 } else {
311                     $manifestpresent = false;
312                     $aiccfound       = false;
313                     foreach ($filelist as $info) {
314                         if ($info->pathname == 'imsmanifest.xml') {
315                             $manifestpresent = true;
316                             break;
317                         }
318                         if (preg_match('/\.cst$/', $info->pathname)) {
319                             $aiccfound = true;
320                             break;
321                         }
322                     }
323                     if (!$manifestpresent and !$aiccfound) {
324                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
325                     }
326                 }
327             }
329         } else if ($type === SCORM_TYPE_EXTERNAL) {
330             $reference = $data['packageurl'];
331             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
332                 $errors['packageurl'] = get_string('required'); // TODO: improve help
333             }
335         } else if ($type === 'packageurl') {
336             $reference = $data['reference'];
337             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
338                 $errors['packageurl'] = get_string('required'); // TODO: improve help
339             }
341         } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
342             $reference = $data['packageurl'];
343             if (stripos($reference, '#') !== 0) {
344                 $errors['packageurl'] = get_string('required');
345             }
346         }
348         return $errors;
349     }
351     //need to translate the "options" and "reference" field.
352     function set_data($default_values) {
353         $default_values = (array)$default_values;
355         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
356             switch ($default_values['scormtype']) {
357                 case SCORM_TYPE_LOCALSYNC :
358                 case SCORM_TYPE_EXTERNAL:
359                 case SCORM_TYPE_IMSREPOSITORY:
360                     $default_values['packageurl'] = $default_values['reference'];
361             }
362         }
363         unset($default_values['reference']);
365         if (!empty($default_values['options'])) {
366             $options = explode(',', $default_values['options']);
367             foreach ($options as $option) {
368                 $opt = explode('=', $option);
369                 if (isset($opt[1])) {
370                     $default_values[$opt[0]] = $opt[1];
371                 }
372             }
373         }
375         $this->data_preprocessing($default_values);
376         parent::set_data($default_values);
377     }