Revert "MDL-35380 SCORM: improve check for imsmanifest file and consolidate into...
[moodle.git] / mod / scorm / mod_form.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 if (!defined('MOODLE_INTERNAL')) {
18     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
19 }
21 require_once($CFG->dirroot.'/course/moodleform_mod.php');
22 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
24 class mod_scorm_mod_form extends moodleform_mod {
26     function definition() {
27         global $CFG, $COURSE, $OUTPUT;
28         $cfg_scorm = get_config('scorm');
30         $mform = $this->_form;
32         if (!$CFG->slasharguments) {
33             $mform->addElement('static', '', '', $OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
34         }
35         //-------------------------------------------------------------------------------
36         $mform->addElement('header', 'general', get_string('general', 'form'));
38         // Name.
39         $mform->addElement('text', 'name', get_string('name'));
40         if (!empty($CFG->formatstringstriptags)) {
41             $mform->setType('name', PARAM_TEXT);
42         } else {
43             $mform->setType('name', PARAM_CLEANHTML);
44         }
45         $mform->addRule('name', null, 'required', null, 'client');
46         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
48         // Summary.
49         $this->add_intro_editor(true);
51         // Package.
52         $mform->addElement('header', 'packagehdr', get_string('packagehdr', 'scorm'));
53         $mform->setExpanded('packagehdr', true);
55         // Scorm types.
56         $scormtypes = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
58         if ($cfg_scorm->allowtypeexternal) {
59             $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
60         }
62         if ($cfg_scorm->allowtypelocalsync) {
63             $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
64         }
66         if ($cfg_scorm->allowtypeexternalaicc) {
67             $scormtypes[SCORM_TYPE_AICCURL] = get_string('typeaiccurl', 'scorm');
68         }
70         // Reference.
71         if (count($scormtypes) > 1) {
72             $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
73             $mform->setType('scormtype', PARAM_ALPHA);
74             $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
75             $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
76             $mform->setType('packageurl', PARAM_RAW);
77             $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
78             $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
79         } else {
80             $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
81             $mform->setType('scormtype', PARAM_ALPHA);
82         }
84         if (count($scormtypes) > 1) {
85             // Update packages timing.
86             $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
87             $mform->setType('updatefreq', PARAM_INT);
88             $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
89             $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
90             $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
91         } else {
92             $mform->addElement('hidden', 'updatefreq', 0);
93             $mform->setType('updatefreq', PARAM_INT);
94         }
96         // New local package upload.
97         $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm'));
98         $mform->addHelpButton('packagefile', 'package', 'scorm');
99         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
101         // Display Settings.
102         $mform->addElement('header', 'displaysettings', get_string('appearance'));
104         // Framed / Popup Window.
105         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
106         $mform->setDefault('popup', $cfg_scorm->popup);
107         $mform->setAdvanced('popup', $cfg_scorm->popup_adv);
109         // Width.
110         $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
111         $mform->setDefault('width', $cfg_scorm->framewidth);
112         $mform->setType('width', PARAM_INT);
113         $mform->setAdvanced('width', $cfg_scorm->framewidth_adv);
114         $mform->disabledIf('width', 'popup', 'eq', 0);
116         // Height.
117         $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
118         $mform->setDefault('height', $cfg_scorm->frameheight);
119         $mform->setType('height', PARAM_INT);
120         $mform->setAdvanced('height', $cfg_scorm->frameheight_adv);
121         $mform->disabledIf('height', 'popup', 'eq', 0);
123         // Window Options.
124         $winoptgrp = array();
125         foreach (scorm_get_popup_options_array() as $key => $value) {
126             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
127             $mform->setDefault($key, $value);
128         }
129         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
130         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
131         $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv);
133         // Skip view page.
134         $skipviewoptions = scorm_get_skip_view_array();
135         if ($COURSE->format == 'scorm') { // Remove option that would cause a constant redirect.
136             unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
137             if ($cfg_scorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
138                 $cfg_scorm->skipview = SCORM_SKIPVIEW_FIRST;
139             }
140         }
141         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
142         $mform->addHelpButton('skipview', 'skipview', 'scorm');
143         $mform->setDefault('skipview', $cfg_scorm->skipview);
144         $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv);
146         // Hide Browse.
147         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
148         $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
149         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
150         $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv);
152         // Display course structure.
153         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
154         $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
155         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
156         $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv);
158         // Toc display.
159         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
160         $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
161         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
162         $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv);
163         $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
165         // Hide Navigation panel.
166         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
167         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
168         $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv);
169         $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
171         // Display attempt status.
172         $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'), scorm_get_attemptstatus_array());
173         $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
174         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
175         $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv);
177         // Availability.
178         $mform->addElement('header', 'availability', get_string('availability'));
180         $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
181         $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
183         //-------------------------------------------------------------------------------
184         // Grade Settings.
185         $mform->addElement('header', 'gradesettings', get_string('grade'));
187         // Grade Method.
188         $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
189         $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
190         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
192         // Maximum Grade.
193         for ($i=0; $i<=100; $i++) {
194             $grades[$i] = "$i";
195         }
196         $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
197         $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
198         $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
200         // Attempts management.
201         $mform->addElement('header', 'attemptsmanagementhdr', get_string('attemptsmanagement', 'scorm'));
203         // Max Attempts.
204         $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
205         $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
206         $mform->setDefault('maxattempt', $cfg_scorm->maxattempt);
208         // What Grade.
209         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
210         $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
211         $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
212         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
214         // Force new attempt.
215         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
216         $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
217         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
219         // Last attempt lock - lock the enter button after the last available attempt has been made.
220         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
221         $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
222         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
224         // Compatibility settings.
225         $mform->addElement('header', 'compatibilitysettingshdr', get_string('compatibilitysettings', 'scorm'));
227         // Force completed.
228         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
229         $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
230         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
232         // Autocontinue.
233         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
234         $mform->addHelpButton('auto', 'autocontinue', 'scorm');
235         $mform->setDefault('auto', $cfg_scorm->auto);
237         //-------------------------------------------------------------------------------
238         // Hidden Settings.
239         $mform->addElement('hidden', 'datadir', null);
240         $mform->setType('datadir', PARAM_RAW);
241         $mform->addElement('hidden', 'pkgtype', null);
242         $mform->setType('pkgtype', PARAM_RAW);
243         $mform->addElement('hidden', 'launch', null);
244         $mform->setType('launch', PARAM_RAW);
245         $mform->addElement('hidden', 'redirect', null);
246         $mform->setType('redirect', PARAM_RAW);
247         $mform->addElement('hidden', 'redirecturl', null);
248         $mform->setType('redirecturl', PARAM_RAW);
250         //-------------------------------------------------------------------------------
251         $this->standard_coursemodule_elements();
252         //-------------------------------------------------------------------------------
253         // Buttons.
254         $this->add_action_buttons();
255     }
257     function data_preprocessing(&$default_values) {
258         global $COURSE;
260         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
261             if (!empty($default_values['options'])) {
262                 $options = explode(',', $default_values['options']);
263                 foreach ($options as $option) {
264                     list($element, $value) = explode('=', $option);
265                     $element = trim($element);
266                     $default_values[$element] = trim($value);
267                 }
268             }
269         }
270         if (isset($default_values['grademethod'])) {
271             $default_values['grademethod'] = intval($default_values['grademethod']);
272         }
273         if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
274             $default_values['width'] .= '%';
275         }
276         if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
277             $default_values['height'] .= '%';
278         }
279         $scorms = get_all_instances_in_course('scorm', $COURSE);
280         $coursescorm = current($scorms);
282         $draftitemid = file_get_submitted_draft_itemid('packagefile');
283         file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
284         $default_values['packagefile'] = $draftitemid;
286         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
287             $default_values['redirect'] = 'yes';
288             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
289         } else {
290             $default_values['redirect'] = 'no';
291             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
292         }
293         if (isset($default_values['version'])) {
294             $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
295         }
296         if (isset($default_values['instance'])) {
297             $default_values['datadir'] = $default_values['instance'];
298         }
299         if (empty($default_values['timeopen'])) {
300             $default_values['timeopen'] = 0;
301         }
302         if (empty($default_values['timeclose'])) {
303             $default_values['timeclose'] = 0;
304         }
306         // Set some completion default data.
307         if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
308             // Unpack values.
309             $cvalues = array();
310             foreach (scorm_status_options() as $key => $value) {
311                 if (($default_values['completionstatusrequired'] & $key) == $key) {
312                     $cvalues[$key] = 1;
313                 }
314             }
316             $default_values['completionstatusrequired'] = $cvalues;
317         }
319         if (!isset($default_values['completionscorerequired']) || !strlen($default_values['completionscorerequired'])) {
320             $default_values['completionscoredisabled'] = 1;
321         }
323     }
325     function validation($data, $files) {
326         global $CFG;
327         $errors = parent::validation($data, $files);
329         $type = $data['scormtype'];
331         if ($type === SCORM_TYPE_LOCAL) {
332             if (!empty($data['update'])) {
333                 // OK, not required.
335             } else if (empty($data['packagefile'])) {
336                 $errors['packagefile'] = get_string('required');
338             } else {
339                 $files = $this->get_draft_files('packagefile');
340                 if (count($files)<1) {
341                     $errors['packagefile'] = get_string('required');
342                     return $errors;
343                 }
344                 $file = reset($files);
345                 $filename = $CFG->tempdir.'/scormimport/scrom_'.time();
346                 make_temp_directory('scormimport');
347                 $file->copy_content_to($filename);
349                 $packer = get_file_packer('application/zip');
351                 $filelist = $packer->list_files($filename);
352                 if (!is_array($filelist)) {
353                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
354                 } else {
355                     $manifestpresent = false;
356                     $aiccfound       = false;
357                     foreach ($filelist as $info) {
358                         if ($info->pathname == 'imsmanifest.xml') {
359                             $manifestpresent = true;
360                             break;
361                         }
362                         if (preg_match('/\.cst$/', $info->pathname)) {
363                             $aiccfound = true;
364                             break;
365                         }
366                     }
367                     if (!$manifestpresent and !$aiccfound) {
368                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
369                     }
370                 }
371                 unlink($filename);
372             }
374         } else if ($type === SCORM_TYPE_EXTERNAL) {
375             $reference = $data['packageurl'];
376             // Syntax check.
377             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
378                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
379             } else {
380                 // Availability check.
381                 $result = scorm_check_url($reference);
382                 if (is_string($result)) {
383                     $errors['packageurl'] = $result;
384                 }
385             }
387         } else if ($type === 'packageurl') {
388             $reference = $data['reference'];
389             // Syntax check.
390             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
391                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
392             } else {
393                 // Availability check.
394                 $result = scorm_check_url($reference);
395                 if (is_string($result)) {
396                     $errors['packageurl'] = $result;
397                 }
398             }
400         } else if ($type === SCORM_TYPE_AICCURL) {
401             $reference = $data['packageurl'];
402             // Syntax check.
403             if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
404                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
405             } else {
406                 // Availability check.
407                 $result = scorm_check_url($reference);
408                 if (is_string($result)) {
409                     $errors['packageurl'] = $result;
410                 }
411             }
413         }
415         return $errors;
416     }
418     // Need to translate the "options" and "reference" field.
419     function set_data($default_values) {
420         $default_values = (array)$default_values;
422         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
423             switch ($default_values['scormtype']) {
424                 case SCORM_TYPE_LOCALSYNC :
425                 case SCORM_TYPE_EXTERNAL:
426                 case SCORM_TYPE_AICCURL:
427                     $default_values['packageurl'] = $default_values['reference'];
428             }
429         }
430         unset($default_values['reference']);
432         if (!empty($default_values['options'])) {
433             $options = explode(',', $default_values['options']);
434             foreach ($options as $option) {
435                 $opt = explode('=', $option);
436                 if (isset($opt[1])) {
437                     $default_values[$opt[0]] = $opt[1];
438                 }
439             }
440         }
442         $this->data_preprocessing($default_values);
443         parent::set_data($default_values);
444     }
446     function add_completion_rules() {
447         $mform =& $this->_form;
448         $items = array();
450         // Require score.
451         $group = array();
452         $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
453         $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
454         $mform->setType('completionscorerequired', PARAM_INT);
455         $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
456         $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
457         $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
458         $mform->setDefault('completionscorerequired', 0);
460         $items[] = 'completionscoregroup';
463         // Require status.
464         $first = true;
465         $firstkey = null;
466         foreach (scorm_status_options(true) as $key => $value) {
467             $name = null;
468             $key = 'completionstatusrequired['.$key.']';
469             if ($first) {
470                 $name = get_string('completionstatusrequired', 'scorm');
471                 $first = false;
472                 $firstkey = $key;
473             }
474             $mform->addElement('checkbox', $key, $name, $value);
475             $mform->setType($key, PARAM_BOOL);
476             $items[] = $key;
477         }
478         $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
480         return $items;
481     }
483     function completion_rule_enabled($data) {
484         $status = !empty($data['completionstatusrequired']);
485         $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
487         return $status || $score;
488     }
490     function get_data($slashed = true) {
491         $data = parent::get_data($slashed);
493         if (!$data) {
494             return false;
495         }
497         // Convert completionstatusrequired to a proper integer, if any.
498         $total = 0;
499         if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
500             foreach (array_keys($data->completionstatusrequired) as $state) {
501                 $total |= $state;
502             }
503             $data->completionstatusrequired = $total;
504         }
506         if (!empty($data->completionunlocked)) {
507             // Turn off completion settings if the checkboxes aren't ticked.
508             $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
510             if (isset($data->completionstatusrequired) && $autocompletion) {
511                 // Do nothing: completionstatusrequired has been already converted
512                 //             into a correct integer representation.
513             } else {
514                 $data->completionstatusrequired = null;
515             }
517             if (!empty($data->completionscoredisabled) || !$autocompletion) {
518                 $data->completionscorerequired = null;
519             }
520         }
522         return $data;
523     }