Revert "MDL-35380 SCORM: improve check for imsmanifest file and consolidate into...
[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
MD
17if (!defined('MOODLE_INTERNAL')) {
18 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
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
26 function definition() {
6aff538a 27 global $CFG, $COURSE, $OUTPUT;
30fc6e2d 28 $cfg_scorm = 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 }
ca4eda13 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.
ac3668bf 49 $this->add_intro_editor(true);
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
30fc6e2d 58 if ($cfg_scorm->allowtypeexternal) {
8aba9cda 59 $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
9528568b 60 }
61
30fc6e2d 62 if ($cfg_scorm->allowtypelocalsync) {
8aba9cda 63 $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
9528568b 64 }
65
4388bd45 66 if ($cfg_scorm->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
DM
74 $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
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');
9528568b 78 $mform->disabledIf('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
20b51a11
FM
84 if (count($scormtypes) > 1) {
85 // Update packages timing.
86 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
87 $mform->setType('updatefreq', PARAM_INT);
88 $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
20b51a11
FM
89 $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
90 $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
91 } else {
92 $mform->addElement('hidden', 'updatefreq', 0);
93 $mform->setType('updatefreq', PARAM_INT);
94 }
95
6a75660e 96 // New local package upload.
ca4eda13 97 $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm'));
427bcd4d 98 $mform->addHelpButton('packagefile', 'package', 'scorm');
9528568b 99 $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
a4813c36 100
20b51a11
FM
101 // Display Settings.
102 $mform->addElement('header', 'displaysettings', get_string('appearance'));
61fbdb0e 103
6a75660e 104 // Framed / Popup Window.
0ffaa76b
DM
105 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
106 $mform->setDefault('popup', $cfg_scorm->popup);
107 $mform->setAdvanced('popup', $cfg_scorm->popup_adv);
108
6a75660e 109 // Width.
0ffaa76b
DM
110 $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
111 $mform->setDefault('width', $cfg_scorm->framewidth);
112 $mform->setType('width', PARAM_INT);
113 $mform->setAdvanced('width', $cfg_scorm->framewidth_adv);
114 $mform->disabledIf('width', 'popup', 'eq', 0);
115
6a75660e 116 // Height.
0ffaa76b
DM
117 $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
118 $mform->setDefault('height', $cfg_scorm->frameheight);
119 $mform->setType('height', PARAM_INT);
120 $mform->setAdvanced('height', $cfg_scorm->frameheight_adv);
121 $mform->disabledIf('height', 'popup', 'eq', 0);
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);
130 $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
131 $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv);
132
6a75660e 133 // Skip view page.
bb5cc0e9
DM
134 $skipviewoptions = scorm_get_skip_view_array();
135 if ($COURSE->format == 'scorm') { // Remove option that would cause a constant redirect.
136 unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
137 if ($cfg_scorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
138 $cfg_scorm->skipview = SCORM_SKIPVIEW_FIRST;
139 }
140 }
141 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
0ffaa76b
DM
142 $mform->addHelpButton('skipview', 'skipview', 'scorm');
143 $mform->setDefault('skipview', $cfg_scorm->skipview);
144 $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv);
145
6a75660e 146 // Hide Browse.
0ffaa76b
DM
147 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
148 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
149 $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
150 $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv);
151
6a75660e 152 // Display course structure.
0ffaa76b
DM
153 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
154 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
155 $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
156 $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv);
157
6a75660e 158 // Toc display.
0ffaa76b
DM
159 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
160 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
161 $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
162 $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv);
f2b3e82b 163 $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
0ffaa76b 164
6a75660e 165 // Hide Navigation panel.
0ffaa76b
DM
166 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
167 $mform->setDefault('hidenav', $cfg_scorm->hidenav);
168 $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv);
93e46331 169 $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
0ffaa76b 170
6a75660e 171 // Display attempt status.
20b51a11
FM
172 $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'), scorm_get_attemptstatus_array());
173 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
174 $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
175 $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv);
176
177 // Availability.
178 $mform->addElement('header', 'availability', get_string('availability'));
179
180 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
181 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
182
0ffaa76b 183 //-------------------------------------------------------------------------------
20b51a11
FM
184 // Grade Settings.
185 $mform->addElement('header', 'gradesettings', get_string('grade'));
a4813c36 186
6a75660e 187 // Grade Method.
30fc6e2d 188 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
c329e370 189 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
30fc6e2d 190 $mform->setDefault('grademethod', $cfg_scorm->grademethod);
3268cf99 191
6a75660e 192 // Maximum Grade.
a4813c36 193 for ($i=0; $i<=100; $i++) {
ca4eda13 194 $grades[$i] = "$i";
a4813c36 195 }
196 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
30fc6e2d 197 $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
ca4eda13 198 $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
a4813c36 199
20b51a11
FM
200 // Attempts management.
201 $mform->addElement('header', 'attemptsmanagementhdr', get_string('attemptsmanagement', 'scorm'));
a4813c36 202
6a75660e 203 // Max Attempts.
30fc6e2d 204 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
c329e370 205 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
e0d90c54 206 $mform->setDefault('maxattempt', $cfg_scorm->maxattempt);
3268cf99 207
6a75660e 208 // What Grade.
87db13b5 209 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
ca4eda13 210 $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
c329e370 211 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
87db13b5 212 $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
87db13b5 213
6a75660e 214 // Force new attempt.
6381fa56 215 $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
c329e370 216 $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
30fc6e2d 217 $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
3268cf99 218
6a75660e 219 // Last attempt lock - lock the enter button after the last available attempt has been made.
6381fa56 220 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
c329e370 221 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
30fc6e2d 222 $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
3268cf99 223
20b51a11
FM
224 // Compatibility settings.
225 $mform->addElement('header', 'compatibilitysettingshdr', get_string('compatibilitysettings', 'scorm'));
226
6a75660e 227 // Force completed.
20b51a11
FM
228 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
229 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
230 $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
20b51a11 231
6a75660e 232 // Autocontinue.
a4813c36 233 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
c329e370 234 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
30fc6e2d 235 $mform->setDefault('auto', $cfg_scorm->auto);
a4813c36 236
ca4eda13 237 //-------------------------------------------------------------------------------
6a75660e 238 // Hidden Settings.
a4813c36 239 $mform->addElement('hidden', 'datadir', null);
d18e0fe6 240 $mform->setType('datadir', PARAM_RAW);
a4813c36 241 $mform->addElement('hidden', 'pkgtype', null);
d18e0fe6 242 $mform->setType('pkgtype', PARAM_RAW);
a4813c36 243 $mform->addElement('hidden', 'launch', null);
d18e0fe6 244 $mform->setType('launch', PARAM_RAW);
a4813c36 245 $mform->addElement('hidden', 'redirect', null);
d18e0fe6 246 $mform->setType('redirect', PARAM_RAW);
a4813c36 247 $mform->addElement('hidden', 'redirecturl', null);
d18e0fe6 248 $mform->setType('redirecturl', PARAM_RAW);
a4813c36 249
ca4eda13 250 //-------------------------------------------------------------------------------
42f103be 251 $this->standard_coursemodule_elements();
ca4eda13 252 //-------------------------------------------------------------------------------
6a75660e 253 // Buttons.
a4813c36 254 $this->add_action_buttons();
a4813c36 255 }
256
728d0599 257 function data_preprocessing(&$default_values) {
a4813c36 258 global $COURSE;
259
260 if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
1adc77e6 261 if (!empty($default_values['options'])) {
ca4eda13 262 $options = explode(',', $default_values['options']);
1adc77e6 263 foreach ($options as $option) {
ca4eda13 264 list($element, $value) = explode('=', $option);
1adc77e6 265 $element = trim($element);
266 $default_values[$element] = trim($value);
267 }
a4813c36 268 }
269 }
270 if (isset($default_values['grademethod'])) {
adefe70e 271 $default_values['grademethod'] = intval($default_values['grademethod']);
a4813c36 272 }
ca4eda13 273 if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
efe95a6f 274 $default_values['width'] .= '%';
a4813c36 275 }
ca4eda13 276 if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
efe95a6f 277 $default_values['height'] .= '%';
a4813c36 278 }
279 $scorms = get_all_instances_in_course('scorm', $COURSE);
280 $coursescorm = current($scorms);
b76f2e60
DC
281
282 $draftitemid = file_get_submitted_draft_itemid('packagefile');
783f1486 283 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
b76f2e60
DC
284 $default_values['packagefile'] = $draftitemid;
285
a4813c36 286 if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
287 $default_values['redirect'] = 'yes';
9528568b 288 $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
a4813c36 289 } else {
290 $default_values['redirect'] = 'no';
291 $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
292 }
293 if (isset($default_values['version'])) {
ca4eda13 294 $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
a4813c36 295 }
296 if (isset($default_values['instance'])) {
297 $default_values['datadir'] = $default_values['instance'];
298 }
d54e2145 299 if (empty($default_values['timeopen'])) {
413900b4
DM
300 $default_values['timeopen'] = 0;
301 }
302 if (empty($default_values['timeclose'])) {
303 $default_values['timeclose'] = 0;
d54e2145 304 }
94db2749 305
6a75660e 306 // Set some completion default data.
94db2749 307 if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
6a75660e 308 // Unpack values.
94db2749
AB
309 $cvalues = array();
310 foreach (scorm_status_options() as $key => $value) {
311 if (($default_values['completionstatusrequired'] & $key) == $key) {
312 $cvalues[$key] = 1;
313 }
314 }
315
316 $default_values['completionstatusrequired'] = $cvalues;
317 }
318
319 if (!isset($default_values['completionscorerequired']) || !strlen($default_values['completionscorerequired'])) {
320 $default_values['completionscoredisabled'] = 1;
321 }
322
a4813c36 323 }
324
a78890d5 325 function validation($data, $files) {
d3d98a3a 326 global $CFG;
a78890d5 327 $errors = parent::validation($data, $files);
60243313 328
9528568b 329 $type = $data['scormtype'];
330
331 if ($type === SCORM_TYPE_LOCAL) {
332 if (!empty($data['update'])) {
6a75660e 333 // OK, not required.
a679d64d 334
d3d98a3a 335 } else if (empty($data['packagefile'])) {
9528568b 336 $errors['packagefile'] = get_string('required');
337
338 } else {
d3d98a3a
DC
339 $files = $this->get_draft_files('packagefile');
340 if (count($files)<1) {
341 $errors['packagefile'] = get_string('required');
9eb8dccc 342 return $errors;
d3d98a3a
DC
343 }
344 $file = reset($files);
7aa06e6d 345 $filename = $CFG->tempdir.'/scormimport/scrom_'.time();
af9b1444 346 make_temp_directory('scormimport');
d3d98a3a
DC
347 $file->copy_content_to($filename);
348
aee8151f
DW
349 $packer = get_file_packer('application/zip');
350
351 $filelist = $packer->list_files($filename);
352 if (!is_array($filelist)) {
353 $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
354 } else {
355 $manifestpresent = false;
356 $aiccfound = false;
357 foreach ($filelist as $info) {
358 if ($info->pathname == 'imsmanifest.xml') {
359 $manifestpresent = true;
360 break;
361 }
362 if (preg_match('/\.cst$/', $info->pathname)) {
363 $aiccfound = true;
364 break;
365 }
366 }
367 if (!$manifestpresent and !$aiccfound) {
368 $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
369 }
370 }
d3d98a3a 371 unlink($filename);
9528568b 372 }
373
374 } else if ($type === SCORM_TYPE_EXTERNAL) {
375 $reference = $data['packageurl'];
f833171f 376 // Syntax check.
9528568b 377 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
4388bd45 378 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
379 } else {
380 // Availability check.
381 $result = scorm_check_url($reference);
382 if (is_string($result)) {
383 $errors['packageurl'] = $result;
384 }
9528568b 385 }
386
387 } else if ($type === 'packageurl') {
388 $reference = $data['reference'];
f833171f 389 // Syntax check.
9528568b 390 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
4388bd45 391 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
392 } else {
393 // Availability check.
394 $result = scorm_check_url($reference);
395 if (is_string($result)) {
396 $errors['packageurl'] = $result;
397 }
9528568b 398 }
399
4388bd45
DM
400 } else if ($type === SCORM_TYPE_AICCURL) {
401 $reference = $data['packageurl'];
f833171f 402 // Syntax check.
4388bd45
DM
403 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
404 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
405 } else {
406 // Availability check.
407 $result = scorm_check_url($reference);
408 if (is_string($result)) {
409 $errors['packageurl'] = $result;
410 }
9528568b 411 }
f833171f 412
60243313 413 }
414
a78890d5 415 return $errors;
a679d64d 416 }
9528568b 417
6a75660e 418 // Need to translate the "options" and "reference" field.
ea38a6b9 419 function set_data($default_values) {
9528568b 420 $default_values = (array)$default_values;
421
422 if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
423 switch ($default_values['scormtype']) {
424 case SCORM_TYPE_LOCALSYNC :
425 case SCORM_TYPE_EXTERNAL:
047b4e83 426 case SCORM_TYPE_AICCURL:
9528568b 427 $default_values['packageurl'] = $default_values['reference'];
428 }
429 }
430 unset($default_values['reference']);
431
432 if (!empty($default_values['options'])) {
433 $options = explode(',', $default_values['options']);
434 foreach ($options as $option) {
435 $opt = explode('=', $option);
436 if (isset($opt[1])) {
9b426d03 437 $default_values[$opt[0]] = $opt[1];
ea38a6b9 438 }
439 }
ea38a6b9 440 }
9528568b 441
ea38a6b9 442 $this->data_preprocessing($default_values);
9528568b 443 parent::set_data($default_values);
ea38a6b9 444 }
94db2749
AB
445
446 function add_completion_rules() {
447 $mform =& $this->_form;
448 $items = array();
449
6a75660e 450 // Require score.
94db2749
AB
451 $group = array();
452 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
453 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
454 $mform->setType('completionscorerequired', PARAM_INT);
455 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
456 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
457 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
458 $mform->setDefault('completionscorerequired', 0);
459
460 $items[] = 'completionscoregroup';
461
462
6a75660e 463 // Require status.
94db2749
AB
464 $first = true;
465 $firstkey = null;
466 foreach (scorm_status_options(true) as $key => $value) {
467 $name = null;
468 $key = 'completionstatusrequired['.$key.']';
469 if ($first) {
470 $name = get_string('completionstatusrequired', 'scorm');
471 $first = false;
472 $firstkey = $key;
473 }
474 $mform->addElement('checkbox', $key, $name, $value);
475 $mform->setType($key, PARAM_BOOL);
476 $items[] = $key;
477 }
478 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
479
480 return $items;
481 }
482
483 function completion_rule_enabled($data) {
484 $status = !empty($data['completionstatusrequired']);
485 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
486
487 return $status || $score;
488 }
489
490 function get_data($slashed = true) {
491 $data = parent::get_data($slashed);
492
493 if (!$data) {
494 return false;
495 }
496
82bd0c66
MS
497 // Convert completionstatusrequired to a proper integer, if any.
498 $total = 0;
499 if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
500 foreach (array_keys($data->completionstatusrequired) as $state) {
501 $total |= $state;
502 }
503 $data->completionstatusrequired = $total;
504 }
505
a9f5fc15 506 if (!empty($data->completionunlocked)) {
82bd0c66 507 // Turn off completion settings if the checkboxes aren't ticked.
a9f5fc15 508 $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
509
82bd0c66
MS
510 if (isset($data->completionstatusrequired) && $autocompletion) {
511 // Do nothing: completionstatusrequired has been already converted
512 // into a correct integer representation.
a9f5fc15 513 } else {
514 $data->completionstatusrequired = null;
94db2749
AB
515 }
516
a9f5fc15 517 if (!empty($data->completionscoredisabled) || !$autocompletion) {
518 $data->completionscorerequired = null;
519 }
94db2749
AB
520 }
521
522 return $data;
523 }
a4813c36 524}