Merge branch 'master_MDL-37660' of git://github.com/danmarsden/moodle
[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         $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
89         $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
91         // display Settings
92         $mform->addElement('header', 'displaysettings', get_string('displaysettings', 'scorm'));
93         // Framed / Popup Window
94         $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
95         $mform->setDefault('popup', $cfg_scorm->popup);
96         $mform->setAdvanced('popup', $cfg_scorm->popup_adv);
98         // Width
99         $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
100         $mform->setDefault('width', $cfg_scorm->framewidth);
101         $mform->setType('width', PARAM_INT);
102         $mform->setAdvanced('width', $cfg_scorm->framewidth_adv);
103         $mform->disabledIf('width', 'popup', 'eq', 0);
105         // Height
106         $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
107         $mform->setDefault('height', $cfg_scorm->frameheight);
108         $mform->setType('height', PARAM_INT);
109         $mform->setAdvanced('height', $cfg_scorm->frameheight_adv);
110         $mform->disabledIf('height', 'popup', 'eq', 0);
112         // Window Options
113         $winoptgrp = array();
114         foreach (scorm_get_popup_options_array() as $key => $value) {
115             $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
116             $mform->setDefault($key, $value);
117         }
118         $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
119         $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
120         $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv);
122         // Skip view page
123         $skipviewoptions = scorm_get_skip_view_array();
124         if ($COURSE->format == 'scorm') { // Remove option that would cause a constant redirect.
125             unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
126             if ($cfg_scorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
127                 $cfg_scorm->skipview = SCORM_SKIPVIEW_FIRST;
128             }
129         }
130         $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
131         $mform->addHelpButton('skipview', 'skipview', 'scorm');
132         $mform->setDefault('skipview', $cfg_scorm->skipview);
133         $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv);
135         // Hide Browse
136         $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
137         $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
138         $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
139         $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv);
141         // Display course structure
142         $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
143         $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
144         $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
145         $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv);
147         // Toc display
148         $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
149         $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
150         $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
151         $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv);
153         // Hide Navigation panel
154         $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
155         $mform->setDefault('hidenav', $cfg_scorm->hidenav);
156         $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv);
157         $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
159         //-------------------------------------------------------------------------------
160         // grade Settings
161         $mform->addElement('header', 'gradesettings', get_string('gradesettings', 'scorm'));
163         // Grade Method
164         $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
165         $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
166         $mform->setDefault('grademethod', $cfg_scorm->grademethod);
167         $mform->setAdvanced('grademethod', $cfg_scorm->grademethod_adv);
169         // Maximum Grade
170         for ($i=0; $i<=100; $i++) {
171             $grades[$i] = "$i";
172         }
173         $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
174         $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
175         $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
176         $mform->setAdvanced('maxgrade', $cfg_scorm->maxgrade_adv);
178         $mform->addElement('header', 'othersettings', get_string('othersettings', 'scorm'));
180         // Max Attempts
181         $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
182         $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
183         $mform->setDefault('maxattempt', $cfg_scorm->maxattempt);
184         $mform->setAdvanced('maxattempt', $cfg_scorm->maxattempt_adv);
186         // What Grade
187         $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
188         $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
189         $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
190         $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
191         $mform->setAdvanced('whatgrade', $cfg_scorm->whatgrade_adv);
193         // Display attempt status
194         $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'), scorm_get_attemptstatus_array());
195         $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
196         $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
197         $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv);
199         // Force completed
200         $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
201         $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
202         $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
203         $mform->setAdvanced('forcecompleted', $cfg_scorm->forcecompleted_adv);
205         // Force new attempt
206         $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
207         $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
208         $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
209         $mform->setAdvanced('forcenewattempt', $cfg_scorm->forcenewattempt_adv);
211         // Last attempt lock - lock the enter button after the last available attempt has been made
212         $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
213         $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
214         $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
215         $mform->setAdvanced('lastattemptlock', $cfg_scorm->lastattemptlock_adv);
217         // Activation period
218 /*        $mform->addElement('static', '', '' ,'<hr />');
219         $mform->addElement('static', 'activation', get_string('activation','scorm'));
220         $datestartgrp = array();
221         $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
222         $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
223         $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
224         $mform->setDefault('startdate', 0);
225         $mform->setDefault('startdisabled', 1);
226         $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
228         $dateendgrp = array();
229         $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
230         $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
231         $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
232         $mform->setDefault('enddate', 0);
233         $mform->setDefault('enddisabled', 1);
234         $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
235 */
237         // Autocontinue
238         $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
239         $mform->addHelpButton('auto', 'autocontinue', 'scorm');
240         $mform->setDefault('auto', $cfg_scorm->auto);
241         $mform->setAdvanced('auto', $cfg_scorm->auto_adv);
243         if (count($scormtypes) > 1) {
244             // Update packages timing
245             $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
246             $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
247             $mform->setAdvanced('updatefreq', $cfg_scorm->updatefreq_adv);
248             $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
249             $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
250         } else {
251             $mform->addElement('hidden', 'updatefreq', 0);
252         }
253         //-------------------------------------------------------------------------------
254         // Hidden Settings
255         $mform->addElement('hidden', 'datadir', null);
256         $mform->setType('datadir', PARAM_RAW);
257         $mform->addElement('hidden', 'pkgtype', null);
258         $mform->setType('pkgtype', PARAM_RAW);
259         $mform->addElement('hidden', 'launch', null);
260         $mform->setType('launch', PARAM_RAW);
261         $mform->addElement('hidden', 'redirect', null);
262         $mform->setType('redirect', PARAM_RAW);
263         $mform->addElement('hidden', 'redirecturl', null);
264         $mform->setType('redirecturl', PARAM_RAW);
266         //-------------------------------------------------------------------------------
267         $this->standard_coursemodule_elements();
268         //-------------------------------------------------------------------------------
269         // buttons
270         $this->add_action_buttons();
271     }
273     function data_preprocessing(&$default_values) {
274         global $COURSE;
276         if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
277             if (!empty($default_values['options'])) {
278                 $options = explode(',', $default_values['options']);
279                 foreach ($options as $option) {
280                     list($element, $value) = explode('=', $option);
281                     $element = trim($element);
282                     $default_values[$element] = trim($value);
283                 }
284             }
285         }
286         if (isset($default_values['grademethod'])) {
287             $default_values['grademethod'] = intval($default_values['grademethod']);
288         }
289         if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
290             $default_values['width'] .= '%';
291         }
292         if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
293             $default_values['height'] .= '%';
294         }
295         $scorms = get_all_instances_in_course('scorm', $COURSE);
296         $coursescorm = current($scorms);
298         $draftitemid = file_get_submitted_draft_itemid('packagefile');
299         file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
300         $default_values['packagefile'] = $draftitemid;
302         if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
303             $default_values['redirect'] = 'yes';
304             $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
305         } else {
306             $default_values['redirect'] = 'no';
307             $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
308         }
309         if (isset($default_values['version'])) {
310             $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
311         }
312         if (isset($default_values['instance'])) {
313             $default_values['datadir'] = $default_values['instance'];
314         }
315         if (empty($default_values['timeopen'])) {
316             $default_values['timeopen'] = 0;
317         }
318         if (empty($default_values['timeclose'])) {
319             $default_values['timeclose'] = 0;
320         }
322         // Set some completion default data
323         if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
324             // Unpack values
325             $cvalues = array();
326             foreach (scorm_status_options() as $key => $value) {
327                 if (($default_values['completionstatusrequired'] & $key) == $key) {
328                     $cvalues[$key] = 1;
329                 }
330             }
332             $default_values['completionstatusrequired'] = $cvalues;
333         }
335         if (!isset($default_values['completionscorerequired']) || !strlen($default_values['completionscorerequired'])) {
336             $default_values['completionscoredisabled'] = 1;
337         }
339     }
341     function validation($data, $files) {
342         global $CFG;
343         $errors = parent::validation($data, $files);
345         $type = $data['scormtype'];
347         if ($type === SCORM_TYPE_LOCAL) {
348             if (!empty($data['update'])) {
349                 //ok, not required
351             } else if (empty($data['packagefile'])) {
352                 $errors['packagefile'] = get_string('required');
354             } else {
355                 $files = $this->get_draft_files('packagefile');
356                 if (count($files)<1) {
357                     $errors['packagefile'] = get_string('required');
358                     return $errors;
359                 }
360                 $file = reset($files);
361                 $filename = $CFG->tempdir.'/scormimport/scrom_'.time();
362                 make_temp_directory('scormimport');
363                 $file->copy_content_to($filename);
365                 $packer = get_file_packer('application/zip');
367                 $filelist = $packer->list_files($filename);
368                 if (!is_array($filelist)) {
369                     $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
370                 } else {
371                     $manifestpresent = false;
372                     $aiccfound       = false;
373                     foreach ($filelist as $info) {
374                         if ($info->pathname == 'imsmanifest.xml') {
375                             $manifestpresent = true;
376                             break;
377                         }
378                         if (preg_match('/\.cst$/', $info->pathname)) {
379                             $aiccfound = true;
380                             break;
381                         }
382                     }
383                     if (!$manifestpresent and !$aiccfound) {
384                         $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
385                     }
386                 }
387                 unlink($filename);
388             }
390         } else if ($type === SCORM_TYPE_EXTERNAL) {
391             $reference = $data['packageurl'];
392             // Syntax check.
393             if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
394                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
395             } else {
396                 // Availability check.
397                 $result = scorm_check_url($reference);
398                 if (is_string($result)) {
399                     $errors['packageurl'] = $result;
400                 }
401             }
403         } else if ($type === 'packageurl') {
404             $reference = $data['reference'];
405             // Syntax check.
406             if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
407                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
408             } else {
409                 // Availability check.
410                 $result = scorm_check_url($reference);
411                 if (is_string($result)) {
412                     $errors['packageurl'] = $result;
413                 }
414             }
416         } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
417             $reference = $data['packageurl'];
418             if (stripos($reference, '#') !== 0) {
419                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
420             }
422         } else if ($type === SCORM_TYPE_AICCURL) {
423             $reference = $data['packageurl'];
424             // Syntax check.
425             if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
426                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
427             } else {
428                 // Availability check.
429                 $result = scorm_check_url($reference);
430                 if (is_string($result)) {
431                     $errors['packageurl'] = $result;
432                 }
433             }
435         }
437         return $errors;
438     }
440     //need to translate the "options" and "reference" field.
441     function set_data($default_values) {
442         $default_values = (array)$default_values;
444         if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
445             switch ($default_values['scormtype']) {
446                 case SCORM_TYPE_LOCALSYNC :
447                 case SCORM_TYPE_EXTERNAL:
448                 case SCORM_TYPE_IMSREPOSITORY:
449                 case SCORM_TYPE_AICCURL:
450                     $default_values['packageurl'] = $default_values['reference'];
451             }
452         }
453         unset($default_values['reference']);
455         if (!empty($default_values['options'])) {
456             $options = explode(',', $default_values['options']);
457             foreach ($options as $option) {
458                 $opt = explode('=', $option);
459                 if (isset($opt[1])) {
460                     $default_values[$opt[0]] = $opt[1];
461                 }
462             }
463         }
465         $this->data_preprocessing($default_values);
466         parent::set_data($default_values);
467     }
469     function add_completion_rules() {
470         $mform =& $this->_form;
471         $items = array();
473         // Require score
474         $group = array();
475         $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
476         $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
477         $mform->setType('completionscorerequired', PARAM_INT);
478         $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
479         $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
480         $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
481         $mform->setDefault('completionscorerequired', 0);
483         $items[] = 'completionscoregroup';
486         // Require status
487         $first = true;
488         $firstkey = null;
489         foreach (scorm_status_options(true) as $key => $value) {
490             $name = null;
491             $key = 'completionstatusrequired['.$key.']';
492             if ($first) {
493                 $name = get_string('completionstatusrequired', 'scorm');
494                 $first = false;
495                 $firstkey = $key;
496             }
497             $mform->addElement('checkbox', $key, $name, $value);
498             $mform->setType($key, PARAM_BOOL);
499             $items[] = $key;
500         }
501         $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
503         return $items;
504     }
506     function completion_rule_enabled($data) {
507         $status = !empty($data['completionstatusrequired']);
508         $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
510         return $status || $score;
511     }
513     function get_data($slashed = true) {
514         $data = parent::get_data($slashed);
516         if (!$data) {
517             return false;
518         }
520         // Turn off completion settings if the checkboxes aren't ticked
521         $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
523         if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
524             $total = 0;
525             foreach (array_keys($data->completionstatusrequired) as $state) {
526                 $total |= $state;
527             }
529             $data->completionstatusrequired = $total;
530         }
532         if (!$autocompletion) {
533             $data->completionstatusrequired = null;
534         }
536         if (!empty($data->completionscoredisabled) || !$autocompletion) {
537             $data->completionscorerequired = null;
538         }
540         return $data;
541     }