Merge branch 'MDL-35971-master' of git://github.com/junpataleta/moodle
[moodle.git] / mod / scorm / mod_form.php
CommitLineData
a4813c36 1<?php
f7b5c6aa
DM
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/>.
16
bfebaf64 17if (!defined('MOODLE_INTERNAL')) {
cb029f33 18 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
bfebaf64
MD
19}
20
ca4eda13 21require_once($CFG->dirroot.'/course/moodleform_mod.php');
a4813c36 22require_once($CFG->dirroot.'/mod/scorm/locallib.php');
23
24class mod_scorm_mod_form extends moodleform_mod {
25
cb029f33 26 public function definition() {
6aff538a 27 global $CFG, $COURSE, $OUTPUT;
cb029f33 28 $cfgscorm = get_config('scorm');
a4813c36 29
9528568b 30 $mform = $this->_form;
31
9528568b 32 if (!$CFG->slasharguments) {
ca4eda13 33 $mform->addElement('static', '', '', $OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
01e6c030 34 }
cb029f33 35
a4813c36 36 $mform->addElement('header', 'general', get_string('general', 'form'));
37
6a75660e 38 // Name.
a4813c36 39 $mform->addElement('text', 'name', get_string('name'));
8eb1d25f 40 if (!empty($CFG->formatstringstriptags)) {
41 $mform->setType('name', PARAM_TEXT);
42 } else {
b8ea3041 43 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 44 }
a4813c36 45 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 46 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
a4813c36 47
6a75660e 48 // Summary.
6398ff53 49 $this->standard_intro_elements();
a4813c36 50
20b51a11
FM
51 // Package.
52 $mform->addElement('header', 'packagehdr', get_string('packagehdr', 'scorm'));
53 $mform->setExpanded('packagehdr', true);
54
6a75660e 55 // Scorm types.
8aba9cda 56 $scormtypes = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
9528568b 57
cb029f33 58 if ($cfgscorm->allowtypeexternal) {
8aba9cda 59 $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
9528568b 60 }
61
cb029f33 62 if ($cfgscorm->allowtypelocalsync) {
8aba9cda 63 $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
9528568b 64 }
65
cb029f33 66 if ($cfgscorm->allowtypeexternalaicc) {
8aba9cda 67 $scormtypes[SCORM_TYPE_AICCURL] = get_string('typeaiccurl', 'scorm');
4388bd45
DM
68 }
69
6a75660e 70 // Reference.
8aba9cda
DM
71 if (count($scormtypes) > 1) {
72 $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
1de23b7f 73 $mform->setType('scormtype', PARAM_ALPHA);
427bcd4d 74 $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
27b8872c 75 $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size' => 60));
9528568b 76 $mform->setType('packageurl', PARAM_RAW);
427bcd4d 77 $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
385247d9 78 $mform->hideIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
e73d5557
DM
79 } else {
80 $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
1de23b7f 81 $mform->setType('scormtype', PARAM_ALPHA);
9528568b 82 }
83
6a75660e 84 // New local package upload.
27b87c10 85 $filemanageroptions = array();
361a47d4 86 $filemanageroptions['accepted_types'] = array('.zip', '.xml');
27b87c10
DM
87 $filemanageroptions['maxbytes'] = 0;
88 $filemanageroptions['maxfiles'] = 1;
89 $filemanageroptions['subdirs'] = 0;
90
91 $mform->addElement('filemanager', 'packagefile', get_string('package', 'scorm'), null, $filemanageroptions);
427bcd4d 92 $mform->addHelpButton('packagefile', 'package', 'scorm');
385247d9 93 $mform->hideIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
a4813c36 94
39af2f41
N
95 // Update packages timing.
96 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
97 $mform->setType('updatefreq', PARAM_INT);
98 $mform->setDefault('updatefreq', $cfgscorm->updatefreq);
99 $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
100
20b51a11
FM
101 // Display Settings.
102 $mform->addElement('header', 'displaysettings', get_string('appearance'));
61fbdb0e 103
6a75660e 104 // Framed / Popup Window.
0ffaa76b 105 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
cb029f33
DM
106 $mform->setDefault('popup', $cfgscorm->popup);
107 $mform->setAdvanced('popup', $cfgscorm->popup_adv);
0ffaa76b 108
6a75660e 109 // Width.
0ffaa76b 110 $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
cb029f33 111 $mform->setDefault('width', $cfgscorm->framewidth);
0ffaa76b 112 $mform->setType('width', PARAM_INT);
cb029f33 113 $mform->setAdvanced('width', $cfgscorm->framewidth_adv);
385247d9 114 $mform->hideIf('width', 'popup', 'eq', 0);
0ffaa76b 115
6a75660e 116 // Height.
0ffaa76b 117 $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
cb029f33 118 $mform->setDefault('height', $cfgscorm->frameheight);
0ffaa76b 119 $mform->setType('height', PARAM_INT);
cb029f33 120 $mform->setAdvanced('height', $cfgscorm->frameheight_adv);
385247d9 121 $mform->hideIf('height', 'popup', 'eq', 0);
0ffaa76b 122
6a75660e 123 // Window Options.
0ffaa76b
DM
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);
385247d9 130 $mform->hideIf('winoptgrp', 'popup', 'eq', 0);
cb029f33 131 $mform->setAdvanced('winoptgrp', $cfgscorm->winoptgrp_adv);
0ffaa76b 132
74ecabb8
JC
133 // Display activity name.
134 $mform->addElement('advcheckbox', 'displayactivityname', get_string('displayactivityname', 'scorm'));
135 $mform->addHelpButton('displayactivityname', 'displayactivityname', 'scorm');
74d2a9df 136 $mform->setDefault('displayactivityname', $cfgscorm->displayactivityname);
74ecabb8 137
6a75660e 138 // Skip view page.
bb5cc0e9 139 $skipviewoptions = scorm_get_skip_view_array();
bb5cc0e9 140 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
0ffaa76b 141 $mform->addHelpButton('skipview', 'skipview', 'scorm');
cb029f33
DM
142 $mform->setDefault('skipview', $cfgscorm->skipview);
143 $mform->setAdvanced('skipview', $cfgscorm->skipview_adv);
0ffaa76b 144
6a75660e 145 // Hide Browse.
0ffaa76b
DM
146 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
147 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
cb029f33
DM
148 $mform->setDefault('hidebrowse', $cfgscorm->hidebrowse);
149 $mform->setAdvanced('hidebrowse', $cfgscorm->hidebrowse_adv);
0ffaa76b 150
6a75660e 151 // Display course structure.
0ffaa76b
DM
152 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
153 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
cb029f33
DM
154 $mform->setDefault('displaycoursestructure', $cfgscorm->displaycoursestructure);
155 $mform->setAdvanced('displaycoursestructure', $cfgscorm->displaycoursestructure_adv);
0ffaa76b 156
6a75660e 157 // Toc display.
0ffaa76b
DM
158 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
159 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
cb029f33
DM
160 $mform->setDefault('hidetoc', $cfgscorm->hidetoc);
161 $mform->setAdvanced('hidetoc', $cfgscorm->hidetoc_adv);
f2b3e82b 162 $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
0ffaa76b 163
df5c7086
MG
164 // Navigation panel display.
165 $mform->addElement('select', 'nav', get_string('nav', 'scorm'), scorm_get_navigation_display_array());
166 $mform->addHelpButton('nav', 'nav', 'scorm');
167 $mform->setDefault('nav', $cfgscorm->nav);
168 $mform->setAdvanced('nav', $cfgscorm->nav_adv);
385247d9 169 $mform->hideIf('nav', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
df5c7086
MG
170
171 // Navigation panel position from left.
172 $mform->addElement('text', 'navpositionleft', get_string('fromleft', 'scorm'), 'maxlength="5" size="5"');
173 $mform->setDefault('navpositionleft', $cfgscorm->navpositionleft);
174 $mform->setType('navpositionleft', PARAM_INT);
175 $mform->setAdvanced('navpositionleft', $cfgscorm->navpositionleft_adv);
385247d9
DS
176 $mform->hideIf('navpositionleft', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
177 $mform->hideIf('navpositionleft', 'nav', 'noteq', SCORM_NAV_FLOATING);
df5c7086
MG
178
179 // Navigation panel position from top.
180 $mform->addElement('text', 'navpositiontop', get_string('fromtop', 'scorm'), 'maxlength="5" size="5"');
181 $mform->setDefault('navpositiontop', $cfgscorm->navpositiontop);
182 $mform->setType('navpositiontop', PARAM_INT);
183 $mform->setAdvanced('navpositiontop', $cfgscorm->navpositiontop_adv);
385247d9
DS
184 $mform->hideIf('navpositiontop', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
185 $mform->hideIf('navpositiontop', 'nav', 'noteq', SCORM_NAV_FLOATING);
0ffaa76b 186
6a75660e 187 // Display attempt status.
cb029f33
DM
188 $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'),
189 scorm_get_attemptstatus_array());
20b51a11 190 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
cb029f33
DM
191 $mform->setDefault('displayattemptstatus', $cfgscorm->displayattemptstatus);
192 $mform->setAdvanced('displayattemptstatus', $cfgscorm->displayattemptstatus_adv);
20b51a11
FM
193
194 // Availability.
195 $mform->addElement('header', 'availability', get_string('availability'));
196
197 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
198 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
199
20b51a11
FM
200 // Grade Settings.
201 $mform->addElement('header', 'gradesettings', get_string('grade'));
a4813c36 202
6a75660e 203 // Grade Method.
30fc6e2d 204 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
c329e370 205 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
cb029f33 206 $mform->setDefault('grademethod', $cfgscorm->grademethod);
3268cf99 207
6a75660e 208 // Maximum Grade.
27b8872c 209 for ($i = 0; $i <= 100; $i++) {
ca4eda13 210 $grades[$i] = "$i";
a4813c36 211 }
212 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
cb029f33 213 $mform->setDefault('maxgrade', $cfgscorm->maxgrade);
385247d9 214 $mform->hideIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
a4813c36 215
20b51a11
FM
216 // Attempts management.
217 $mform->addElement('header', 'attemptsmanagementhdr', get_string('attemptsmanagement', 'scorm'));
a4813c36 218
6a75660e 219 // Max Attempts.
30fc6e2d 220 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
c329e370 221 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
cb029f33 222 $mform->setDefault('maxattempt', $cfgscorm->maxattempt);
3268cf99 223
6a75660e 224 // What Grade.
87db13b5 225 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
385247d9 226 $mform->hideIf('whatgrade', 'maxattempt', 'eq', 1);
c329e370 227 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
cb029f33 228 $mform->setDefault('whatgrade', $cfgscorm->whatgrade);
87db13b5 229
6a75660e 230 // Force new attempt.
fbfd69a8
DM
231 $newattemptselect = scorm_get_forceattempt_array();
232 $mform->addElement('select', 'forcenewattempt', get_string('forcenewattempts', 'scorm'), $newattemptselect);
233 $mform->addHelpButton('forcenewattempt', 'forcenewattempts', 'scorm');
cb029f33 234 $mform->setDefault('forcenewattempt', $cfgscorm->forcenewattempt);
3268cf99 235
6a75660e 236 // Last attempt lock - lock the enter button after the last available attempt has been made.
6381fa56 237 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
c329e370 238 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
cb029f33 239 $mform->setDefault('lastattemptlock', $cfgscorm->lastattemptlock);
3268cf99 240
20b51a11
FM
241 // Compatibility settings.
242 $mform->addElement('header', 'compatibilitysettingshdr', get_string('compatibilitysettings', 'scorm'));
243
6a75660e 244 // Force completed.
20b51a11
FM
245 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
246 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
cb029f33 247 $mform->setDefault('forcecompleted', $cfgscorm->forcecompleted);
20b51a11 248
6a75660e 249 // Autocontinue.
a4813c36 250 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
c329e370 251 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
cb029f33 252 $mform->setDefault('auto', $cfgscorm->auto);
a4813c36 253
6a6709ec
N
254 // Autocommit.
255 $mform->addElement('selectyesno', 'autocommit', get_string('autocommit', 'scorm'));
256 $mform->addHelpButton('autocommit', 'autocommit', 'scorm');
257 $mform->setDefault('autocommit', $cfgscorm->autocommit);
258
3e283642
DM
259 // Mastery score overrides status.
260 $mform->addElement('selectyesno', 'masteryoverride', get_string('masteryoverride', 'scorm'));
261 $mform->addHelpButton('masteryoverride', 'masteryoverride', 'scorm');
262 $mform->setDefault('masteryoverride', $cfgscorm->masteryoverride);
263
6a75660e 264 // Hidden Settings.
a4813c36 265 $mform->addElement('hidden', 'datadir', null);
d18e0fe6 266 $mform->setType('datadir', PARAM_RAW);
a4813c36 267 $mform->addElement('hidden', 'pkgtype', null);
d18e0fe6 268 $mform->setType('pkgtype', PARAM_RAW);
a4813c36 269 $mform->addElement('hidden', 'launch', null);
d18e0fe6 270 $mform->setType('launch', PARAM_RAW);
a4813c36 271 $mform->addElement('hidden', 'redirect', null);
d18e0fe6 272 $mform->setType('redirect', PARAM_RAW);
a4813c36 273 $mform->addElement('hidden', 'redirecturl', null);
d18e0fe6 274 $mform->setType('redirecturl', PARAM_RAW);
a4813c36 275
42f103be 276 $this->standard_coursemodule_elements();
cb029f33 277
6a75660e 278 // Buttons.
a4813c36 279 $this->add_action_buttons();
a4813c36 280 }
281
cb029f33 282 public function data_preprocessing(&$defaultvalues) {
a4813c36 283 global $COURSE;
284
cb029f33
DM
285 if (isset($defaultvalues['popup']) && ($defaultvalues['popup'] == 1) && isset($defaultvalues['options'])) {
286 if (!empty($defaultvalues['options'])) {
287 $options = explode(',', $defaultvalues['options']);
1adc77e6 288 foreach ($options as $option) {
ca4eda13 289 list($element, $value) = explode('=', $option);
1adc77e6 290 $element = trim($element);
cb029f33 291 $defaultvalues[$element] = trim($value);
1adc77e6 292 }
a4813c36 293 }
294 }
cb029f33
DM
295 if (isset($defaultvalues['grademethod'])) {
296 $defaultvalues['grademethod'] = intval($defaultvalues['grademethod']);
a4813c36 297 }
cb029f33
DM
298 if (isset($defaultvalues['width']) && (strpos($defaultvalues['width'], '%') === false)
299 && ($defaultvalues['width'] <= 100)) {
300 $defaultvalues['width'] .= '%';
a4813c36 301 }
3dc86847 302 if (isset($defaultvalues['height']) && (strpos($defaultvalues['height'], '%') === false)
cb029f33
DM
303 && ($defaultvalues['height'] <= 100)) {
304 $defaultvalues['height'] .= '%';
a4813c36 305 }
306 $scorms = get_all_instances_in_course('scorm', $COURSE);
307 $coursescorm = current($scorms);
b76f2e60
DC
308
309 $draftitemid = file_get_submitted_draft_itemid('packagefile');
27b87c10
DM
310 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0,
311 array('subdirs' => 0, 'maxfiles' => 1));
cb029f33 312 $defaultvalues['packagefile'] = $draftitemid;
b76f2e60 313
cb029f33
DM
314 if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) {
315 $defaultvalues['redirect'] = 'yes';
316 $defaultvalues['redirecturl'] = '../course/view.php?id='.$defaultvalues['course'];
a4813c36 317 } else {
cb029f33
DM
318 $defaultvalues['redirect'] = 'no';
319 $defaultvalues['redirecturl'] = '../mod/scorm/view.php?id='.$defaultvalues['coursemodule'];
a4813c36 320 }
cb029f33 321 if (isset($defaultvalues['version'])) {
27b8872c 322 $defaultvalues['pkgtype'] = (substr($defaultvalues['version'], 0, 5) == 'SCORM') ? 'scorm' : 'aicc';
a4813c36 323 }
cb029f33
DM
324 if (isset($defaultvalues['instance'])) {
325 $defaultvalues['datadir'] = $defaultvalues['instance'];
a4813c36 326 }
cb029f33
DM
327 if (empty($defaultvalues['timeopen'])) {
328 $defaultvalues['timeopen'] = 0;
413900b4 329 }
cb029f33
DM
330 if (empty($defaultvalues['timeclose'])) {
331 $defaultvalues['timeclose'] = 0;
d54e2145 332 }
94db2749 333
6a75660e 334 // Set some completion default data.
2e4c8b38
JP
335 $cvalues = array();
336 if (empty($this->_instance)) {
337 // When in add mode, set a default completion rule that requires the SCORM's status be set to "Completed".
338 $cvalues[4] = 1;
339 } else if (!empty($defaultvalues['completionstatusrequired']) && !is_array($defaultvalues['completionstatusrequired'])) {
6a75660e 340 // Unpack values.
94db2749 341 foreach (scorm_status_options() as $key => $value) {
cb029f33 342 if (($defaultvalues['completionstatusrequired'] & $key) == $key) {
94db2749
AB
343 $cvalues[$key] = 1;
344 }
345 }
2e4c8b38
JP
346 }
347 if (!empty($cvalues)) {
cb029f33 348 $defaultvalues['completionstatusrequired'] = $cvalues;
94db2749
AB
349 }
350
cb029f33
DM
351 if (!isset($defaultvalues['completionscorerequired']) || !strlen($defaultvalues['completionscorerequired'])) {
352 $defaultvalues['completionscoredisabled'] = 1;
94db2749 353 }
a4813c36 354 }
355
cb029f33 356 public function validation($data, $files) {
27b87c10 357 global $CFG, $USER;
a78890d5 358 $errors = parent::validation($data, $files);
60243313 359
9528568b 360 $type = $data['scormtype'];
361
362 if ($type === SCORM_TYPE_LOCAL) {
27b87c10 363 if (empty($data['packagefile'])) {
9528568b 364 $errors['packagefile'] = get_string('required');
365
366 } else {
27b87c10
DM
367 $draftitemid = file_get_submitted_draft_itemid('packagefile');
368
369 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'packagefilecheck', null,
370 array('subdirs' => 0, 'maxfiles' => 1));
371
372 // Get file from users draft area.
373 $usercontext = context_user::instance($USER->id);
374 $fs = get_file_storage();
375 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
376
27b8872c 377 if (count($files) < 1) {
d3d98a3a 378 $errors['packagefile'] = get_string('required');
9eb8dccc 379 return $errors;
d3d98a3a
DC
380 }
381 $file = reset($files);
27b87c10
DM
382 if (!$file->is_external_file() && !empty($data['updatefreq'])) {
383 // Make sure updatefreq is not set if using normal local file.
384 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_scorm');
385 }
361a47d4
DM
386 if (strtolower($file->get_filename()) == 'imsmanifest.xml') {
387 if (!$file->is_external_file()) {
388 $errors['packagefile'] = get_string('aliasonly', 'mod_scorm');
389 } else {
159c6e26 390 $repository = repository::get_repository_by_id($file->get_repository_id(), context_system::instance());
361a47d4
DM
391 if (!$repository->supports_relative_file()) {
392 $errors['packagefile'] = get_string('repositorynotsupported', 'mod_scorm');
393 }
394 }
395 } else if (strtolower(substr($file->get_filename(), -3)) == 'xml') {
396 $errors['packagefile'] = get_string('invalidmanifestname', 'mod_scorm');
397 } else {
398 // Validate this SCORM package.
399 $errors = array_merge($errors, scorm_validate_package($file));
400 }
9528568b 401 }
402
403 } else if ($type === SCORM_TYPE_EXTERNAL) {
404 $reference = $data['packageurl'];
f833171f 405 // Syntax check.
9528568b 406 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
4388bd45 407 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
408 } else {
409 // Availability check.
410 $result = scorm_check_url($reference);
411 if (is_string($result)) {
412 $errors['packageurl'] = $result;
413 }
9528568b 414 }
415
416 } else if ($type === 'packageurl') {
417 $reference = $data['reference'];
f833171f 418 // Syntax check.
9528568b 419 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
4388bd45 420 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
421 } else {
422 // Availability check.
423 $result = scorm_check_url($reference);
424 if (is_string($result)) {
425 $errors['packageurl'] = $result;
426 }
9528568b 427 }
428
4388bd45
DM
429 } else if ($type === SCORM_TYPE_AICCURL) {
430 $reference = $data['packageurl'];
f833171f 431 // Syntax check.
4388bd45
DM
432 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
433 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
434 } else {
435 // Availability check.
436 $result = scorm_check_url($reference);
437 if (is_string($result)) {
438 $errors['packageurl'] = $result;
439 }
9528568b 440 }
f833171f 441
60243313 442 }
443
9f83ce7e
JP
444 // Validate availability dates.
445 if ($data['timeopen'] && $data['timeclose']) {
446 if ($data['timeopen'] > $data['timeclose']) {
447 $errors['timeclose'] = get_string('closebeforeopen', 'scorm');
448 }
449 }
b30a884b
DM
450 if (!empty($data['completionstatusallscos'])) {
451 $requirestatus = false;
452 foreach (scorm_status_options(true) as $key => $value) {
453 if (!empty($data['completionstatusrequired'][$key])) {
454 $requirestatus = true;
455 }
456 }
457 if (!$requirestatus) {
458 $errors['completionstatusallscos'] = get_string('youmustselectastatus', 'scorm');
459 }
460 }
9f83ce7e 461
a78890d5 462 return $errors;
a679d64d 463 }
9528568b 464
6a75660e 465 // Need to translate the "options" and "reference" field.
cb029f33
DM
466 public function set_data($defaultvalues) {
467 $defaultvalues = (array)$defaultvalues;
9528568b 468
cb029f33
DM
469 if (isset($defaultvalues['scormtype']) and isset($defaultvalues['reference'])) {
470 switch ($defaultvalues['scormtype']) {
9528568b 471 case SCORM_TYPE_LOCALSYNC :
472 case SCORM_TYPE_EXTERNAL:
047b4e83 473 case SCORM_TYPE_AICCURL:
cb029f33 474 $defaultvalues['packageurl'] = $defaultvalues['reference'];
9528568b 475 }
476 }
cb029f33 477 unset($defaultvalues['reference']);
9528568b 478
cb029f33
DM
479 if (!empty($defaultvalues['options'])) {
480 $options = explode(',', $defaultvalues['options']);
9528568b 481 foreach ($options as $option) {
482 $opt = explode('=', $option);
483 if (isset($opt[1])) {
cb029f33 484 $defaultvalues[$opt[0]] = $opt[1];
ea38a6b9 485 }
486 }
ea38a6b9 487 }
9528568b 488
cb029f33 489 parent::set_data($defaultvalues);
ea38a6b9 490 }
94db2749 491
cb029f33 492 public function add_completion_rules() {
94db2749
AB
493 $mform =& $this->_form;
494 $items = array();
495
6a75660e 496 // Require score.
94db2749
AB
497 $group = array();
498 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
499 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
500 $mform->setType('completionscorerequired', PARAM_INT);
501 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
502 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
503 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
504 $mform->setDefault('completionscorerequired', 0);
505
506 $items[] = 'completionscoregroup';
507
6a75660e 508 // Require status.
94db2749
AB
509 $first = true;
510 $firstkey = null;
511 foreach (scorm_status_options(true) as $key => $value) {
512 $name = null;
513 $key = 'completionstatusrequired['.$key.']';
514 if ($first) {
515 $name = get_string('completionstatusrequired', 'scorm');
516 $first = false;
517 $firstkey = $key;
518 }
519 $mform->addElement('checkbox', $key, $name, $value);
520 $mform->setType($key, PARAM_BOOL);
521 $items[] = $key;
522 }
523 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
524
b30a884b
DM
525 $mform->addElement('checkbox', 'completionstatusallscos', get_string('completionstatusallscos', 'scorm'));
526 $mform->setType('completionstatusallscos', PARAM_BOOL);
527 $mform->addHelpButton('completionstatusallscos', 'completionstatusallscos', 'scorm');
528 $mform->setDefault('completionstatusallscos', 0);
529 $items[] = 'completionstatusallscos';
530
94db2749
AB
531 return $items;
532 }
533
27b8872c 534 public function completion_rule_enabled($data) {
94db2749
AB
535 $status = !empty($data['completionstatusrequired']);
536 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
537
538 return $status || $score;
539 }
540
06cdda46
MG
541 /**
542 * Allows module to modify the data returned by form get_data().
543 * This method is also called in the bulk activity completion form.
544 *
545 * Only available on moodleform_mod.
546 *
273d3106 547 * @param stdClass $data the form data to be modified.
06cdda46 548 */
273d3106 549 public function data_postprocessing($data) {
06cdda46 550 parent::data_postprocessing($data);
82bd0c66
MS
551 // Convert completionstatusrequired to a proper integer, if any.
552 $total = 0;
553 if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
554 foreach (array_keys($data->completionstatusrequired) as $state) {
555 $total |= $state;
556 }
557 $data->completionstatusrequired = $total;
558 }
559
a9f5fc15 560 if (!empty($data->completionunlocked)) {
82bd0c66 561 // Turn off completion settings if the checkboxes aren't ticked.
a9f5fc15 562 $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
563
27b8872c 564 if (!(isset($data->completionstatusrequired) && $autocompletion)) {
a9f5fc15 565 $data->completionstatusrequired = null;
94db2749 566 }
27b8872c
N
567 // Else do nothing: completionstatusrequired has been already converted
568 // into a correct integer representation.
94db2749 569
a9f5fc15 570 if (!empty($data->completionscoredisabled) || !$autocompletion) {
571 $data->completionscorerequired = null;
572 }
94db2749 573 }
94db2749 574 }
a4813c36 575}