Merge branch 'm23_MDL-34233' of git://github.com/danmarsden/moodle into MOODLE_23_STABLE
[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');
47         // Summary
48         $this->add_intro_editor(true);
50         // Scorm types
51         $scormtypes = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
53         if ($cfg_scorm->allowtypeexternal) {
54             $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
55         }
57         if ($cfg_scorm->allowtypelocalsync) {
58             $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
59         }
61         if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
62             $scormtypes[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
63         }
65         if ($cfg_scorm->allowtypeexternalaicc) {
66             $scormtypes[SCORM_TYPE_AICCURL] = get_string('typeaiccurl', 'scorm');
67         }
69         // Reference
70         if (count($scormtypes) > 1) {
71             $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
72             $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
73             $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
74             $mform->setType('packageurl', PARAM_RAW);
75             $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
76             $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
77         } else {
78             $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
79         }
81         // New local package upload
82         $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
83         $mform->setMaxFileSize($maxbytes);
84         $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm'));
85         $mform->addHelpButton('packagefile', 'package', 'scorm');
86         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
88         //-------------------------------------------------------------------------------
89         // Time restrictions
90         $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
92         $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
93         $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
94         //-------------------------------------------------------------------------------
95         // display Settings
96         $mform->addElement('header', 'displaysettings', get_string('displaysettings', 'scorm'));
97         // Framed / Popup Window
98         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
99         $mform->setDefault('popup', $cfg_scorm->popup);
100         $mform->setAdvanced('popup', $cfg_scorm->popup_adv);
102         // Width
103         $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
104         $mform->setDefault('width', $cfg_scorm->framewidth);
105         $mform->setType('width', PARAM_INT);
106         $mform->setAdvanced('width', $cfg_scorm->framewidth_adv);
107         $mform->disabledIf('width', 'popup', 'eq', 0);
109         // Height
110         $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
111         $mform->setDefault('height', $cfg_scorm->frameheight);
112         $mform->setType('height', PARAM_INT);
113         $mform->setAdvanced('height', $cfg_scorm->frameheight_adv);
114         $mform->disabledIf('height', 'popup', 'eq', 0);
116         // Window Options
117         $winoptgrp = array();
118         foreach (scorm_get_popup_options_array() as $key => $value) {
119             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
120             $mform->setDefault($key, $value);
121         }
122         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
123         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
124         $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv);
126         // Skip view page
127         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), scorm_get_skip_view_array());
128         $mform->addHelpButton('skipview', 'skipview', 'scorm');
129         $mform->setDefault('skipview', $cfg_scorm->skipview);
130         $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv);
132         // Hide Browse
133         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
134         $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
135         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
136         $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv);
138         // Display course structure
139         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
140         $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
141         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
142         $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv);
144         // Toc display
145         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
146         $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
147         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
148         $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv);
150         // Hide Navigation panel
151         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
152         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
153         $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv);
154         $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
156         //-------------------------------------------------------------------------------
157         // grade Settings
158         $mform->addElement('header', 'gradesettings', get_string('gradesettings', 'scorm'));
160         // Grade Method
161         $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
162         $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
163         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
164         $mform->setAdvanced('grademethod', $cfg_scorm->grademethod_adv);
166         // Maximum Grade
167         for ($i=0; $i<=100; $i++) {
168             $grades[$i] = "$i";
169         }
170         $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
171         $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
172         $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
173         $mform->setAdvanced('maxgrade', $cfg_scorm->maxgrade_adv);
175         $mform->addElement('header', 'othersettings', get_string('othersettings', 'scorm'));
177         // Max Attempts
178         $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
179         $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
180         $mform->setDefault('maxattempt', $cfg_scorm->maxattempt);
181         $mform->setAdvanced('maxattempt', $cfg_scorm->maxattempt_adv);
183         // What Grade
184         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
185         $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
186         $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
187         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
188         $mform->setAdvanced('whatgrade', $cfg_scorm->whatgrade_adv);
190         // Display attempt status
191         $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
192         $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
193         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
194         $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv);
196         // Force completed
197         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
198         $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
199         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
200         $mform->setAdvanced('forcecompleted', $cfg_scorm->forcecompleted_adv);
202         // Force new attempt
203         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
204         $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
205         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
206         $mform->setAdvanced('forcenewattempt', $cfg_scorm->forcenewattempt_adv);
208         // Last attempt lock - lock the enter button after the last available attempt has been made
209         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
210         $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
211         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
212         $mform->setAdvanced('lastattemptlock', $cfg_scorm->lastattemptlock_adv);
214         // Activation period
215 /*        $mform->addElement('static', '', '' ,'<hr />');
216         $mform->addElement('static', 'activation', get_string('activation','scorm'));
217         $datestartgrp = array();
218         $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
219         $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
220         $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
221         $mform->setDefault('startdate', 0);
222         $mform->setDefault('startdisabled', 1);
223         $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
225         $dateendgrp = array();
226         $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
227         $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
228         $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
229         $mform->setDefault('enddate', 0);
230         $mform->setDefault('enddisabled', 1);
231         $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
232 */
234         // Autocontinue
235         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
236         $mform->addHelpButton('auto', 'autocontinue', 'scorm');
237         $mform->setDefault('auto', $cfg_scorm->auto);
238         $mform->setAdvanced('auto', $cfg_scorm->auto_adv);
240         if (count($scormtypes) > 1) {
241             // Update packages timing
242             $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
243             $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
244             $mform->setAdvanced('updatefreq', $cfg_scorm->updatefreq_adv);
245             $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
246             $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
247         } else {
248             $mform->addElement('hidden', 'updatefreq', 0);
249         }
250         //-------------------------------------------------------------------------------
251         // Hidden Settings
252         $mform->addElement('hidden', 'datadir', null);
253         $mform->setType('datadir', PARAM_RAW);
254         $mform->addElement('hidden', 'pkgtype', null);
255         $mform->setType('pkgtype', PARAM_RAW);
256         $mform->addElement('hidden', 'launch', null);
257         $mform->setType('launch', PARAM_RAW);
258         $mform->addElement('hidden', 'redirect', null);
259         $mform->setType('redirect', PARAM_RAW);
260         $mform->addElement('hidden', 'redirecturl', null);
261         $mform->setType('redirecturl', PARAM_RAW);
263         //-------------------------------------------------------------------------------
264         $this->standard_coursemodule_elements();
265         //-------------------------------------------------------------------------------
266         // buttons
267         $this->add_action_buttons();
268     }
270     function data_preprocessing(&$default_values) {
271         global $COURSE;
273         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
274             if (!empty($default_values['options'])) {
275                 $options = explode(',', $default_values['options']);
276                 foreach ($options as $option) {
277                     list($element, $value) = explode('=', $option);
278                     $element = trim($element);
279                     $default_values[$element] = trim($value);
280                 }
281             }
282         }
283         if (isset($default_values['grademethod'])) {
284             $default_values['grademethod'] = intval($default_values['grademethod']);
285         }
286         if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
287             $default_values['width'] .= '%';
288         }
289         if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
290             $default_values['height'] .= '%';
291         }
292         $scorms = get_all_instances_in_course('scorm', $COURSE);
293         $coursescorm = current($scorms);
295         $draftitemid = file_get_submitted_draft_itemid('packagefile');
296         file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
297         $default_values['packagefile'] = $draftitemid;
299         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
300             $default_values['redirect'] = 'yes';
301             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
302         } else {
303             $default_values['redirect'] = 'no';
304             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
305         }
306         if (isset($default_values['version'])) {
307             $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
308         }
309         if (isset($default_values['instance'])) {
310             $default_values['datadir'] = $default_values['instance'];
311         }
312         if (empty($default_values['timeopen'])) {
313             $default_values['timeopen'] = 0;
314         }
315         if (empty($default_values['timeclose'])) {
316             $default_values['timeclose'] = 0;
317         }
319         // Set some completion default data
320         if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
321             // Unpack values
322             $cvalues = array();
323             foreach (scorm_status_options() as $key => $value) {
324                 if (($default_values['completionstatusrequired'] & $key) == $key) {
325                     $cvalues[$key] = 1;
326                 }
327             }
329             $default_values['completionstatusrequired'] = $cvalues;
330         }
332         if (!isset($default_values['completionscorerequired']) || !strlen($default_values['completionscorerequired'])) {
333             $default_values['completionscoredisabled'] = 1;
334         }
336     }
338     function validation($data, $files) {
339         global $CFG;
340         $errors = parent::validation($data, $files);
342         $type = $data['scormtype'];
344         if ($type === SCORM_TYPE_LOCAL) {
345             if (!empty($data['update'])) {
346                 //ok, not required
348             } else if (empty($data['packagefile'])) {
349                 $errors['packagefile'] = get_string('required');
351             } else {
352                 $files = $this->get_draft_files('packagefile');
353                 if (count($files)<1) {
354                     $errors['packagefile'] = get_string('required');
355                     return $errors;
356                 }
357                 $file = reset($files);
358                 $filename = $CFG->tempdir.'/scormimport/scrom_'.time();
359                 make_temp_directory('scormimport');
360                 $file->copy_content_to($filename);
362                 $packer = get_file_packer('application/zip');
364                 $filelist = $packer->list_files($filename);
365                 if (!is_array($filelist)) {
366                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
367                 } else {
368                     $manifestpresent = false;
369                     $aiccfound       = false;
370                     foreach ($filelist as $info) {
371                         if ($info->pathname == 'imsmanifest.xml') {
372                             $manifestpresent = true;
373                             break;
374                         }
375                         if (preg_match('/\.cst$/', $info->pathname)) {
376                             $aiccfound = true;
377                             break;
378                         }
379                     }
380                     if (!$manifestpresent and !$aiccfound) {
381                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
382                     }
383                 }
384                 unlink($filename);
385             }
387         } else if ($type === SCORM_TYPE_EXTERNAL) {
388             $reference = $data['packageurl'];
389             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
390                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
391             }
393         } else if ($type === 'packageurl') {
394             $reference = $data['reference'];
395             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
396                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
397             }
399         } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
400             $reference = $data['packageurl'];
401             if (stripos($reference, '#') !== 0) {
402                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
403             }
404         } else if ($type === SCORM_TYPE_AICCURL) {
405             $reference = $data['packageurl'];
406             if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
407                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
408             }
409         }
411         return $errors;
412     }
414     //need to translate the "options" and "reference" field.
415     function set_data($default_values) {
416         $default_values = (array)$default_values;
418         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
419             switch ($default_values['scormtype']) {
420                 case SCORM_TYPE_LOCALSYNC :
421                 case SCORM_TYPE_EXTERNAL:
422                 case SCORM_TYPE_IMSREPOSITORY:
423                 case SCORM_TYPE_AICCURL:
424                     $default_values['packageurl'] = $default_values['reference'];
425             }
426         }
427         unset($default_values['reference']);
429         if (!empty($default_values['options'])) {
430             $options = explode(',', $default_values['options']);
431             foreach ($options as $option) {
432                 $opt = explode('=', $option);
433                 if (isset($opt[1])) {
434                     $default_values[$opt[0]] = $opt[1];
435                 }
436             }
437         }
439         $this->data_preprocessing($default_values);
440         parent::set_data($default_values);
441     }
443     function add_completion_rules() {
444         $mform =& $this->_form;
445         $items = array();
447         // Require score
448         $group = array();
449         $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
450         $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
451         $mform->setType('completionscorerequired', PARAM_INT);
452         $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
453         $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
454         $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
455         $mform->setDefault('completionscorerequired', 0);
457         $items[] = 'completionscoregroup';
460         // Require status
461         $first = true;
462         $firstkey = null;
463         foreach (scorm_status_options(true) as $key => $value) {
464             $name = null;
465             $key = 'completionstatusrequired['.$key.']';
466             if ($first) {
467                 $name = get_string('completionstatusrequired', 'scorm');
468                 $first = false;
469                 $firstkey = $key;
470             }
471             $mform->addElement('checkbox', $key, $name, $value);
472             $mform->setType($key, PARAM_BOOL);
473             $items[] = $key;
474         }
475         $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
477         return $items;
478     }
480     function completion_rule_enabled($data) {
481         $status = !empty($data['completionstatusrequired']);
482         $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
484         return $status || $score;
485     }
487     function get_data($slashed = true) {
488         $data = parent::get_data($slashed);
490         if (!$data) {
491             return false;
492         }
494         // Turn off completion settings if the checkboxes aren't ticked
495         $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
497         if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
498             $total = 0;
499             foreach (array_keys($data->completionstatusrequired) as $state) {
500                 $total |= $state;
501             }
503             $data->completionstatusrequired = $total;
504         }
506         if (!$autocompletion) {
507             $data->completionstatusrequired = null;
508         }
510         if (!empty($data->completionscoredisabled) || !$autocompletion) {
511             $data->completionscorerequired = null;
512         }
514         return $data;
515     }