Merge branch 'm20_MDL-26176_cleanup' of github.com:danmarsden/moodle
[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_CLEANHTML);
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 // Reference
55         if (count($options) > 1) {
56             $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $options);
57             $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
58             $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
59             $mform->setType('packageurl', PARAM_RAW);
60             $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
61             $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
62         } else {
63             $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
64         }
66 // New local package upload
67         $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
68         $mform->setMaxFileSize($maxbytes);
69         $mform->addElement('filepicker', 'packagefile', get_string('package','scorm'));
70         $mform->addHelpButton('packagefile', 'package', 'scorm');
71         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
73 //-------------------------------------------------------------------------------
74 // Time restrictions
75         $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
77         $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"),array('optional' => true));
78         $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"),array('optional' => true));
79 //-------------------------------------------------------------------------------
80 // Other Settings
81         $mform->addElement('header', 'advanced', get_string('othersettings', 'form'));
83 // Grade Method
84         $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
85         $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
86         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
88 // Maximum Grade
89         for ($i=0; $i<=100; $i++) {
90           $grades[$i] = "$i";
91         }
92         $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
93         $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
94         $mform->disabledIf('maxgrade', 'grademethod','eq', GRADESCOES);
96 // Attempts
97         $mform->addElement('static', '', '' ,'<hr />');
99 // Max Attempts
100         $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
101         $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
102         $mform->setDefault('maxattempt', $cfg_scorm->maxattempts);
104 // What Grade
105         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
106         $mform->disabledIf('whatgrade', 'maxattempt','eq',1);
107         $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
108         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
109         $mform->setAdvanced('whatgrade');
111 // Display attempt status
112         $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
113         $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
114         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
116 // Force completed
117         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
118         $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
119         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
120         $mform->setAdvanced('forcecompleted');
122 // Force new attempt
123         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
124         $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
125         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
126         $mform->setAdvanced('forcenewattempt');
128 // Last attempt lock - lock the enter button after the last available attempt has been made
129         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
130         $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
131         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
132         $mform->setAdvanced('lastattemptlock');
134 // Activation period
135 /*        $mform->addElement('static', '', '' ,'<hr />');
136         $mform->addElement('static', 'activation', get_string('activation','scorm'));
137         $datestartgrp = array();
138         $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
139         $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
140         $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
141         $mform->setDefault('startdate', 0);
142         $mform->setDefault('startdisabled', 1);
143         $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
145         $dateendgrp = array();
146         $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
147         $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
148         $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
149         $mform->setDefault('enddate', 0);
150         $mform->setDefault('enddisabled', 1);
151         $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
152 */
154 // Framed / Popup Window
155         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
156         $mform->setDefault('popup', $cfg_scorm->popup);
157         $mform->setAdvanced('popup');
159 // Width
160         $mform->addElement('text', 'width', get_string('width','scorm'), 'maxlength="5" size="5"');
161         $mform->setDefault('width', $cfg_scorm->framewidth);
162         $mform->setType('width', PARAM_INT);
163         $mform->setAdvanced('width');
164         $mform->disabledIf('width', 'popup', 'eq', 0);
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);
170         $mform->setAdvanced('height');
171         $mform->disabledIf('height', 'popup', 'eq', 0);
173 // Window Options
174         $winoptgrp = array();
175         foreach(scorm_get_popup_options_array() as $key => $value){
176             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
177             $mform->setDefault($key, $value);
178         }
179         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options','scorm'), '<br />', false);
180         $mform->setAdvanced('winoptgrp');
181         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
183 // Skip view page
184         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'),scorm_get_skip_view_array());
185         $mform->addHelpButton('skipview', 'skipview', 'scorm');
186         $mform->setDefault('skipview', $cfg_scorm->skipview);
187         $mform->setAdvanced('skipview');
189 // Hide Browse
190         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
191         $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
192         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
193         $mform->setAdvanced('hidebrowse');
195 // Display course structure
196         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
197         $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
198         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
199         $mform->setAdvanced('displaycoursestructure');
201 // Toc display
202         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
203         $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
204         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
205         $mform->setAdvanced('hidetoc');
207 // Hide Navigation panel
208         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
209         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
210         $mform->setAdvanced('hidenav');
212 // Autocontinue
213         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
214         $mform->addHelpButton('auto', 'autocontinue', 'scorm');
215         $mform->setDefault('auto', $cfg_scorm->auto);
216         $mform->setAdvanced('auto');
218 // Update packages timing
219         $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
220         $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
221         $mform->setAdvanced('updatefreq');
223 //-------------------------------------------------------------------------------
224 // Hidden Settings
225         $mform->addElement('hidden', 'datadir', null);
226         $mform->setType('datadir', PARAM_RAW);
227         $mform->addElement('hidden', 'pkgtype', null);
228         $mform->setType('pkgtype', PARAM_RAW);
229         $mform->addElement('hidden', 'launch', null);
230         $mform->setType('launch', PARAM_RAW);
231         $mform->addElement('hidden', 'redirect', null);
232         $mform->setType('redirect', PARAM_RAW);
233         $mform->addElement('hidden', 'redirecturl', null);
234         $mform->setType('redirecturl', PARAM_RAW);
237 //-------------------------------------------------------------------------------
238         $this->standard_coursemodule_elements();
239 //-------------------------------------------------------------------------------
240         // buttons
241         $this->add_action_buttons();
243     }
245     function data_preprocessing(&$default_values) {
246         global $COURSE;
248         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
249             if (!empty($default_values['options'])) {
250                 $options = explode(',',$default_values['options']);
251                 foreach ($options as $option) {
252                     list($element,$value) = explode('=',$option);
253                     $element = trim($element);
254                     $default_values[$element] = trim($value);
255                 }
256             }
257         }
258         if (isset($default_values['grademethod'])) {
259             $default_values['grademethod'] = intval($default_values['grademethod']);
260         }
261         if (isset($default_values['width']) && (strpos($default_values['width'],'%') === false) && ($default_values['width'] <= 100)) {
262             $default_values['width'] .= '%';
263         }
264         if (isset($default_values['width']) && (strpos($default_values['height'],'%') === false) && ($default_values['height'] <= 100)) {
265             $default_values['height'] .= '%';
266         }
267         $scorms = get_all_instances_in_course('scorm', $COURSE);
268         $coursescorm = current($scorms);
270         $draftitemid = file_get_submitted_draft_itemid('packagefile');
271         file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
272         $default_values['packagefile'] = $draftitemid;
274         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
275             $default_values['redirect'] = 'yes';
276             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
277         } else {
278             $default_values['redirect'] = 'no';
279             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
280         }
281         if (isset($default_values['version'])) {
282             $default_values['pkgtype'] = (substr($default_values['version'],0,5) == 'SCORM') ? 'scorm':'aicc';
283         }
284         if (isset($default_values['instance'])) {
285             $default_values['datadir'] = $default_values['instance'];
286         }
287         if (empty($default_values['timeopen'])) {
288             $default_values['timeopen'] = 0;
289         }
290         if (empty($default_values['timeclose'])) {
291             $default_values['timeclose'] = 0;
292         }
293     }
295     function validation($data, $files) {
296         global $CFG;
297         $errors = parent::validation($data, $files);
299         $type = $data['scormtype'];
301         if ($type === SCORM_TYPE_LOCAL) {
302             if (!empty($data['update'])) {
303                 //ok, not required
305             } else if (empty($data['packagefile'])) {
306                 $errors['packagefile'] = get_string('required');
308             } else {
309                 $files = $this->get_draft_files('packagefile');
310                 if (count($files)<1) {
311                     $errors['packagefile'] = get_string('required');
312                     return $errors;
313                 }
314                 $file = reset($files);
315                 $filename = $CFG->dataroot.'/temp/scormimport/scrom_'.time();
316                 make_upload_directory('temp/scormimport');
317                 $file->copy_content_to($filename);
319                 $packer = get_file_packer('application/zip');
321                 $filelist = $packer->list_files($filename);
322                 if (!is_array($filelist)) {
323                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
324                 } else {
325                     $manifestpresent = false;
326                     $aiccfound       = false;
327                     foreach ($filelist as $info) {
328                         if ($info->pathname == 'imsmanifest.xml') {
329                             $manifestpresent = true;
330                             break;
331                         }
332                         if (preg_match('/\.cst$/', $info->pathname)) {
333                             $aiccfound = true;
334                             break;
335                         }
336                     }
337                     if (!$manifestpresent and !$aiccfound) {
338                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
339                     }
340                 }
341                 unlink($filename);
342             }
344         } else if ($type === SCORM_TYPE_EXTERNAL) {
345             $reference = $data['packageurl'];
346             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
347                 $errors['packageurl'] = get_string('required'); // TODO: improve help
348             }
350         } else if ($type === 'packageurl') {
351             $reference = $data['reference'];
352             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
353                 $errors['packageurl'] = get_string('required'); // TODO: improve help
354             }
356         } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
357             $reference = $data['packageurl'];
358             if (stripos($reference, '#') !== 0) {
359                 $errors['packageurl'] = get_string('required');
360             }
361         }
363         return $errors;
364     }
366     //need to translate the "options" and "reference" field.
367     function set_data($default_values) {
368         $default_values = (array)$default_values;
370         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
371             switch ($default_values['scormtype']) {
372                 case SCORM_TYPE_LOCALSYNC :
373                 case SCORM_TYPE_EXTERNAL:
374                 case SCORM_TYPE_IMSREPOSITORY:
375                     $default_values['packageurl'] = $default_values['reference'];
376             }
377         }
378         unset($default_values['reference']);
380         if (!empty($default_values['options'])) {
381             $options = explode(',', $default_values['options']);
382             foreach ($options as $option) {
383                 $opt = explode('=', $option);
384                 if (isset($opt[1])) {
385                     $default_values[$opt[0]] = $opt[1];
386                 }
387             }
388         }
390         $this->data_preprocessing($default_values);
391         parent::set_data($default_values);
392     }