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