2 // This file is part of Moodle - http://moodle.org/
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.
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.
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
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'));
35 //-------------------------------------------------------------------------------
36 $mform->addElement('header', 'general', get_string('general', 'form'));
39 $mform->addElement('text', 'name', get_string('name'));
40 if (!empty($CFG->formatstringstriptags
)) {
41 $mform->setType('name', PARAM_TEXT
);
43 $mform->setType('name', PARAM_CLEANHTML
);
45 $mform->addRule('name', null, 'required', null, 'client');
46 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
49 $this->add_intro_editor(true);
52 $scormtypes = array(SCORM_TYPE_LOCAL
=> get_string('typelocal', 'scorm'));
54 if ($cfg_scorm->allowtypeexternal
) {
55 $scormtypes[SCORM_TYPE_EXTERNAL
] = get_string('typeexternal', 'scorm');
58 if ($cfg_scorm->allowtypelocalsync
) {
59 $scormtypes[SCORM_TYPE_LOCALSYNC
] = get_string('typelocalsync', 'scorm');
62 if (!empty($CFG->repositoryactivate
) and $cfg_scorm->allowtypeimsrepository
) {
63 $scormtypes[SCORM_TYPE_IMSREPOSITORY
] = get_string('typeimsrepository', 'scorm');
66 if ($cfg_scorm->allowtypeexternalaicc
) {
67 $scormtypes[SCORM_TYPE_AICCURL
] = get_string('typeaiccurl', 'scorm');
71 if (count($scormtypes) > 1) {
72 $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
73 $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
74 $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
75 $mform->setType('packageurl', PARAM_RAW
);
76 $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
77 $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL
);
79 $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL
);
82 // New local package upload.
83 $maxbytes = get_max_upload_file_size($CFG->maxbytes
, $COURSE->maxbytes
);
84 $mform->setMaxFileSize($maxbytes);
85 $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm'));
86 $mform->addHelpButton('packagefile', 'package', 'scorm');
87 $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL
);
89 //-------------------------------------------------------------------------------
91 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
93 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
94 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
95 //-------------------------------------------------------------------------------
97 $mform->addElement('header', 'displaysettings', get_string('displaysettings', 'scorm'));
98 // Framed / Popup Window.
99 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
100 $mform->setDefault('popup', $cfg_scorm->popup
);
101 $mform->setAdvanced('popup', $cfg_scorm->popup_adv
);
104 $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
105 $mform->setDefault('width', $cfg_scorm->framewidth
);
106 $mform->setType('width', PARAM_INT
);
107 $mform->setAdvanced('width', $cfg_scorm->framewidth_adv
);
108 $mform->disabledIf('width', 'popup', 'eq', 0);
111 $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
112 $mform->setDefault('height', $cfg_scorm->frameheight
);
113 $mform->setType('height', PARAM_INT
);
114 $mform->setAdvanced('height', $cfg_scorm->frameheight_adv
);
115 $mform->disabledIf('height', 'popup', 'eq', 0);
118 $winoptgrp = array();
119 foreach (scorm_get_popup_options_array() as $key => $value) {
120 $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
121 $mform->setDefault($key, $value);
123 $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
124 $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
125 $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv
);
128 $skipviewoptions = scorm_get_skip_view_array();
129 if ($COURSE->format
== 'scorm') { // Remove option that would cause a constant redirect.
130 unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS
]);
131 if ($cfg_scorm->skipview
== SCORM_SKIPVIEW_ALWAYS
) {
132 $cfg_scorm->skipview
= SCORM_SKIPVIEW_FIRST
;
135 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
136 $mform->addHelpButton('skipview', 'skipview', 'scorm');
137 $mform->setDefault('skipview', $cfg_scorm->skipview
);
138 $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv
);
141 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
142 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
143 $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse
);
144 $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv
);
146 // Display course structure.
147 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
148 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
149 $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure
);
150 $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv
);
153 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
154 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
155 $mform->setDefault('hidetoc', $cfg_scorm->hidetoc
);
156 $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv
);
158 // Hide Navigation panel.
159 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
160 $mform->setDefault('hidenav', $cfg_scorm->hidenav
);
161 $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv
);
162 $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
164 //-------------------------------------------------------------------------------
166 $mform->addElement('header', 'gradesettings', get_string('gradesettings', 'scorm'));
169 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
170 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
171 $mform->setDefault('grademethod', $cfg_scorm->grademethod
);
172 $mform->setAdvanced('grademethod', $cfg_scorm->grademethod_adv
);
175 for ($i=0; $i<=100; $i++
) {
178 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
179 $mform->setDefault('maxgrade', $cfg_scorm->maxgrade
);
180 $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES
);
181 $mform->setAdvanced('maxgrade', $cfg_scorm->maxgrade_adv
);
183 $mform->addElement('header', 'othersettings', get_string('othersettings', 'scorm'));
186 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
187 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
188 $mform->setDefault('maxattempt', $cfg_scorm->maxattempt
);
189 $mform->setAdvanced('maxattempt', $cfg_scorm->maxattempt_adv
);
192 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
193 $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
194 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
195 $mform->setDefault('whatgrade', $cfg_scorm->whatgrade
);
196 $mform->setAdvanced('whatgrade', $cfg_scorm->whatgrade_adv
);
198 // Display attempt status.
199 $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
200 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
201 $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus
);
202 $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv
);
205 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
206 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
207 $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted
);
208 $mform->setAdvanced('forcecompleted', $cfg_scorm->forcecompleted_adv
);
210 // Force new attempt.
211 $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
212 $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
213 $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt
);
214 $mform->setAdvanced('forcenewattempt', $cfg_scorm->forcenewattempt_adv
);
216 // Last attempt lock - lock the enter button after the last available attempt has been made.
217 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
218 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
219 $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock
);
220 $mform->setAdvanced('lastattemptlock', $cfg_scorm->lastattemptlock_adv
);
222 // Activation period.
223 /* $mform->addElement('static', '', '' ,'<hr />');
224 $mform->addElement('static', 'activation', get_string('activation','scorm'));
225 $datestartgrp = array();
226 $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
227 $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
228 $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
229 $mform->setDefault('startdate', 0);
230 $mform->setDefault('startdisabled', 1);
231 $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
233 $dateendgrp = array();
234 $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
235 $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
236 $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
237 $mform->setDefault('enddate', 0);
238 $mform->setDefault('enddisabled', 1);
239 $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
243 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
244 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
245 $mform->setDefault('auto', $cfg_scorm->auto
);
246 $mform->setAdvanced('auto', $cfg_scorm->auto_adv
);
248 if (count($scormtypes) > 1) {
249 // Update packages timing.
250 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
251 $mform->setDefault('updatefreq', $cfg_scorm->updatefreq
);
252 $mform->setAdvanced('updatefreq', $cfg_scorm->updatefreq_adv
);
253 $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
254 $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL
);
256 $mform->addElement('hidden', 'updatefreq', 0);
258 //-------------------------------------------------------------------------------
260 $mform->addElement('hidden', 'datadir', null);
261 $mform->setType('datadir', PARAM_RAW
);
262 $mform->addElement('hidden', 'pkgtype', null);
263 $mform->setType('pkgtype', PARAM_RAW
);
264 $mform->addElement('hidden', 'launch', null);
265 $mform->setType('launch', PARAM_RAW
);
266 $mform->addElement('hidden', 'redirect', null);
267 $mform->setType('redirect', PARAM_RAW
);
268 $mform->addElement('hidden', 'redirecturl', null);
269 $mform->setType('redirecturl', PARAM_RAW
);
271 //-------------------------------------------------------------------------------
272 $this->standard_coursemodule_elements();
273 //-------------------------------------------------------------------------------
275 $this->add_action_buttons();
278 function data_preprocessing(&$default_values) {
281 if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
282 if (!empty($default_values['options'])) {
283 $options = explode(',', $default_values['options']);
284 foreach ($options as $option) {
285 list($element, $value) = explode('=', $option);
286 $element = trim($element);
287 $default_values[$element] = trim($value);
291 if (isset($default_values['grademethod'])) {
292 $default_values['grademethod'] = intval($default_values['grademethod']);
294 if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
295 $default_values['width'] .= '%';
297 if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
298 $default_values['height'] .= '%';
300 $scorms = get_all_instances_in_course('scorm', $COURSE);
301 $coursescorm = current($scorms);
303 $draftitemid = file_get_submitted_draft_itemid('packagefile');
304 file_prepare_draft_area($draftitemid, $this->context
->id
, 'mod_scorm', 'package', 0);
305 $default_values['packagefile'] = $draftitemid;
307 if (($COURSE->format
== 'scorm') && ((count($scorms) == 0) ||
($default_values['instance'] == $coursescorm->id
))) {
308 $default_values['redirect'] = 'yes';
309 $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
311 $default_values['redirect'] = 'no';
312 $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
314 if (isset($default_values['version'])) {
315 $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ?
'scorm':'aicc';
317 if (isset($default_values['instance'])) {
318 $default_values['datadir'] = $default_values['instance'];
320 if (empty($default_values['timeopen'])) {
321 $default_values['timeopen'] = 0;
323 if (empty($default_values['timeclose'])) {
324 $default_values['timeclose'] = 0;
327 // Set some completion default data.
328 if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
331 foreach (scorm_status_options() as $key => $value) {
332 if (($default_values['completionstatusrequired'] & $key) == $key) {
337 $default_values['completionstatusrequired'] = $cvalues;
340 if (!isset($default_values['completionscorerequired']) ||
!strlen($default_values['completionscorerequired'])) {
341 $default_values['completionscoredisabled'] = 1;
346 function validation($data, $files) {
348 $errors = parent
::validation($data, $files);
350 $type = $data['scormtype'];
352 if ($type === SCORM_TYPE_LOCAL
) {
353 if (!empty($data['update'])) {
356 } else if (empty($data['packagefile'])) {
357 $errors['packagefile'] = get_string('required');
360 $files = $this->get_draft_files('packagefile');
361 if (count($files)<1) {
362 $errors['packagefile'] = get_string('required');
365 $file = reset($files);
366 $filename = $CFG->tempdir
.'/scormimport/scrom_'.time();
367 make_temp_directory('scormimport');
368 $file->copy_content_to($filename);
370 $packer = get_file_packer('application/zip');
372 $filelist = $packer->list_files($filename);
373 if (!is_array($filelist)) {
374 $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
376 $manifestpresent = false;
378 foreach ($filelist as $info) {
379 if ($info->pathname
== 'imsmanifest.xml') {
380 $manifestpresent = true;
383 if (preg_match('/\.cst$/', $info->pathname
)) {
388 if (!$manifestpresent and !$aiccfound) {
389 $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
395 } else if ($type === SCORM_TYPE_EXTERNAL
) {
396 $reference = $data['packageurl'];
397 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
398 $errors['packageurl'] = get_string('invalidurl', 'scorm');
401 } else if ($type === 'packageurl') {
402 $reference = $data['reference'];
403 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
404 $errors['packageurl'] = get_string('invalidurl', 'scorm');
407 } else if ($type === SCORM_TYPE_IMSREPOSITORY
) {
408 $reference = $data['packageurl'];
409 if (stripos($reference, '#') !== 0) {
410 $errors['packageurl'] = get_string('invalidurl', 'scorm');
412 } else if ($type === SCORM_TYPE_AICCURL
) {
413 $reference = $data['packageurl'];
414 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
415 $errors['packageurl'] = get_string('invalidurl', 'scorm');
422 // Need to translate the "options" and "reference" field.
423 function set_data($default_values) {
424 $default_values = (array)$default_values;
426 if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
427 switch ($default_values['scormtype']) {
428 case SCORM_TYPE_LOCALSYNC
:
429 case SCORM_TYPE_EXTERNAL
:
430 case SCORM_TYPE_IMSREPOSITORY
:
431 case SCORM_TYPE_AICCURL
:
432 $default_values['packageurl'] = $default_values['reference'];
435 unset($default_values['reference']);
437 if (!empty($default_values['options'])) {
438 $options = explode(',', $default_values['options']);
439 foreach ($options as $option) {
440 $opt = explode('=', $option);
441 if (isset($opt[1])) {
442 $default_values[$opt[0]] = $opt[1];
447 $this->data_preprocessing($default_values);
448 parent
::set_data($default_values);
451 function add_completion_rules() {
452 $mform =& $this->_form
;
457 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
458 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
459 $mform->setType('completionscorerequired', PARAM_INT
);
460 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
461 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
462 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
463 $mform->setDefault('completionscorerequired', 0);
465 $items[] = 'completionscoregroup';
471 foreach (scorm_status_options(true) as $key => $value) {
473 $key = 'completionstatusrequired['.$key.']';
475 $name = get_string('completionstatusrequired', 'scorm');
479 $mform->addElement('checkbox', $key, $name, $value);
480 $mform->setType($key, PARAM_BOOL
);
483 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
488 function completion_rule_enabled($data) {
489 $status = !empty($data['completionstatusrequired']);
490 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
492 return $status ||
$score;
495 function get_data($slashed = true) {
496 $data = parent
::get_data($slashed);
502 // Convert completionstatusrequired to a proper integer, if any.
504 if (isset($data->completionstatusrequired
) && is_array($data->completionstatusrequired
)) {
505 foreach (array_keys($data->completionstatusrequired
) as $state) {
508 $data->completionstatusrequired
= $total;
511 if (!empty($data->completionunlocked
)) {
512 // Turn off completion settings if the checkboxes aren't ticked.
513 $autocompletion = isset($data->completion
) && $data->completion
== COMPLETION_TRACKING_AUTOMATIC
;
515 if (isset($data->completionstatusrequired
) && $autocompletion) {
516 // Do nothing: completionstatusrequired has been already converted
517 // into a correct integer representation.
519 $data->completionstatusrequired
= null;
522 if (!empty($data->completionscoredisabled
) ||
!$autocompletion) {
523 $data->completionscorerequired
= null;