MDL-28579 SCORM: coding guideline fixes
[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.
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
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
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);
cb029f33 88 $mform->setDefault('updatefreq', $cfgscorm->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 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);
0ffaa76b
DM
114 $mform->disabledIf('width', 'popup', 'eq', 0);
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);
0ffaa76b
DM
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);
cb029f33 131 $mform->setAdvanced('winoptgrp', $cfgscorm->winoptgrp_adv);
0ffaa76b 132
6a75660e 133 // Skip view page.
bb5cc0e9 134 $skipviewoptions = scorm_get_skip_view_array();
3d1808c6 135 if ($COURSE->format == 'singleactivity') { // Remove option that would cause a constant redirect.
bb5cc0e9 136 unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
cb029f33
DM
137 if ($cfgscorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
138 $cfgscorm->skipview = SCORM_SKIPVIEW_FIRST;
bb5cc0e9
DM
139 }
140 }
141 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
0ffaa76b 142 $mform->addHelpButton('skipview', 'skipview', 'scorm');
cb029f33
DM
143 $mform->setDefault('skipview', $cfgscorm->skipview);
144 $mform->setAdvanced('skipview', $cfgscorm->skipview_adv);
0ffaa76b 145
6a75660e 146 // Hide Browse.
0ffaa76b
DM
147 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
148 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
cb029f33
DM
149 $mform->setDefault('hidebrowse', $cfgscorm->hidebrowse);
150 $mform->setAdvanced('hidebrowse', $cfgscorm->hidebrowse_adv);
0ffaa76b 151
6a75660e 152 // Display course structure.
0ffaa76b
DM
153 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
154 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
cb029f33
DM
155 $mform->setDefault('displaycoursestructure', $cfgscorm->displaycoursestructure);
156 $mform->setAdvanced('displaycoursestructure', $cfgscorm->displaycoursestructure_adv);
0ffaa76b 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');
cb029f33
DM
161 $mform->setDefault('hidetoc', $cfgscorm->hidetoc);
162 $mform->setAdvanced('hidetoc', $cfgscorm->hidetoc_adv);
f2b3e82b 163 $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
0ffaa76b 164
6a75660e 165 // Hide Navigation panel.
0ffaa76b 166 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
cb029f33
DM
167 $mform->setDefault('hidenav', $cfgscorm->hidenav);
168 $mform->setAdvanced('hidenav', $cfgscorm->hidenav_adv);
93e46331 169 $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
0ffaa76b 170
6a75660e 171 // Display attempt status.
cb029f33
DM
172 $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'),
173 scorm_get_attemptstatus_array());
20b51a11 174 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
cb029f33
DM
175 $mform->setDefault('displayattemptstatus', $cfgscorm->displayattemptstatus);
176 $mform->setAdvanced('displayattemptstatus', $cfgscorm->displayattemptstatus_adv);
20b51a11
FM
177
178 // Availability.
179 $mform->addElement('header', 'availability', get_string('availability'));
180
181 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
182 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
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');
cb029f33 190 $mform->setDefault('grademethod', $cfgscorm->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);
cb029f33 197 $mform->setDefault('maxgrade', $cfgscorm->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');
cb029f33 206 $mform->setDefault('maxattempt', $cfgscorm->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');
cb029f33 212 $mform->setDefault('whatgrade', $cfgscorm->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');
cb029f33 217 $mform->setDefault('forcenewattempt', $cfgscorm->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');
cb029f33 222 $mform->setDefault('lastattemptlock', $cfgscorm->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');
cb029f33 230 $mform->setDefault('forcecompleted', $cfgscorm->forcecompleted);
20b51a11 231
6a75660e 232 // Autocontinue.
a4813c36 233 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
c329e370 234 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
cb029f33 235 $mform->setDefault('auto', $cfgscorm->auto);
a4813c36 236
6a75660e 237 // Hidden Settings.
a4813c36 238 $mform->addElement('hidden', 'datadir', null);
d18e0fe6 239 $mform->setType('datadir', PARAM_RAW);
a4813c36 240 $mform->addElement('hidden', 'pkgtype', null);
d18e0fe6 241 $mform->setType('pkgtype', PARAM_RAW);
a4813c36 242 $mform->addElement('hidden', 'launch', null);
d18e0fe6 243 $mform->setType('launch', PARAM_RAW);
a4813c36 244 $mform->addElement('hidden', 'redirect', null);
d18e0fe6 245 $mform->setType('redirect', PARAM_RAW);
a4813c36 246 $mform->addElement('hidden', 'redirecturl', null);
d18e0fe6 247 $mform->setType('redirecturl', PARAM_RAW);
a4813c36 248
42f103be 249 $this->standard_coursemodule_elements();
cb029f33 250
6a75660e 251 // Buttons.
a4813c36 252 $this->add_action_buttons();
a4813c36 253 }
254
cb029f33 255 public function data_preprocessing(&$defaultvalues) {
a4813c36 256 global $COURSE;
257
cb029f33
DM
258 if (isset($defaultvalues['popup']) && ($defaultvalues['popup'] == 1) && isset($defaultvalues['options'])) {
259 if (!empty($defaultvalues['options'])) {
260 $options = explode(',', $defaultvalues['options']);
1adc77e6 261 foreach ($options as $option) {
ca4eda13 262 list($element, $value) = explode('=', $option);
1adc77e6 263 $element = trim($element);
cb029f33 264 $defaultvalues[$element] = trim($value);
1adc77e6 265 }
a4813c36 266 }
267 }
cb029f33
DM
268 if (isset($defaultvalues['grademethod'])) {
269 $defaultvalues['grademethod'] = intval($defaultvalues['grademethod']);
a4813c36 270 }
cb029f33
DM
271 if (isset($defaultvalues['width']) && (strpos($defaultvalues['width'], '%') === false)
272 && ($defaultvalues['width'] <= 100)) {
273 $defaultvalues['width'] .= '%';
a4813c36 274 }
cb029f33
DM
275 if (isset($defaultvalues['width']) && (strpos($defaultvalues['height'], '%') === false)
276 && ($defaultvalues['height'] <= 100)) {
277 $defaultvalues['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);
cb029f33 284 $defaultvalues['packagefile'] = $draftitemid;
b76f2e60 285
cb029f33
DM
286 if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) {
287 $defaultvalues['redirect'] = 'yes';
288 $defaultvalues['redirecturl'] = '../course/view.php?id='.$defaultvalues['course'];
a4813c36 289 } else {
cb029f33
DM
290 $defaultvalues['redirect'] = 'no';
291 $defaultvalues['redirecturl'] = '../mod/scorm/view.php?id='.$defaultvalues['coursemodule'];
a4813c36 292 }
cb029f33
DM
293 if (isset($defaultvalues['version'])) {
294 $defaultvalues['pkgtype'] = (substr($defaultvalues['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
a4813c36 295 }
cb029f33
DM
296 if (isset($defaultvalues['instance'])) {
297 $defaultvalues['datadir'] = $defaultvalues['instance'];
a4813c36 298 }
cb029f33
DM
299 if (empty($defaultvalues['timeopen'])) {
300 $defaultvalues['timeopen'] = 0;
413900b4 301 }
cb029f33
DM
302 if (empty($defaultvalues['timeclose'])) {
303 $defaultvalues['timeclose'] = 0;
d54e2145 304 }
94db2749 305
6a75660e 306 // Set some completion default data.
cb029f33 307 if (!empty($defaultvalues['completionstatusrequired']) && !is_array($defaultvalues['completionstatusrequired'])) {
6a75660e 308 // Unpack values.
94db2749
AB
309 $cvalues = array();
310 foreach (scorm_status_options() as $key => $value) {
cb029f33 311 if (($defaultvalues['completionstatusrequired'] & $key) == $key) {
94db2749
AB
312 $cvalues[$key] = 1;
313 }
314 }
315
cb029f33 316 $defaultvalues['completionstatusrequired'] = $cvalues;
94db2749
AB
317 }
318
cb029f33
DM
319 if (!isset($defaultvalues['completionscorerequired']) || !strlen($defaultvalues['completionscorerequired'])) {
320 $defaultvalues['completionscoredisabled'] = 1;
94db2749
AB
321 }
322
a4813c36 323 }
324
cb029f33 325 public 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);
c7e6fb6c 345 $errors = array_merge($errors, scorm_validate_package($file));
9528568b 346 }
347
348 } else if ($type === SCORM_TYPE_EXTERNAL) {
349 $reference = $data['packageurl'];
f833171f 350 // Syntax check.
9528568b 351 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
4388bd45 352 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
353 } else {
354 // Availability check.
355 $result = scorm_check_url($reference);
356 if (is_string($result)) {
357 $errors['packageurl'] = $result;
358 }
9528568b 359 }
360
361 } else if ($type === 'packageurl') {
362 $reference = $data['reference'];
f833171f 363 // Syntax check.
9528568b 364 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
4388bd45 365 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
366 } else {
367 // Availability check.
368 $result = scorm_check_url($reference);
369 if (is_string($result)) {
370 $errors['packageurl'] = $result;
371 }
9528568b 372 }
373
4388bd45
DM
374 } else if ($type === SCORM_TYPE_AICCURL) {
375 $reference = $data['packageurl'];
f833171f 376 // Syntax check.
4388bd45
DM
377 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
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 }
f833171f 386
60243313 387 }
388
a78890d5 389 return $errors;
a679d64d 390 }
9528568b 391
6a75660e 392 // Need to translate the "options" and "reference" field.
cb029f33
DM
393 public function set_data($defaultvalues) {
394 $defaultvalues = (array)$defaultvalues;
9528568b 395
cb029f33
DM
396 if (isset($defaultvalues['scormtype']) and isset($defaultvalues['reference'])) {
397 switch ($defaultvalues['scormtype']) {
9528568b 398 case SCORM_TYPE_LOCALSYNC :
399 case SCORM_TYPE_EXTERNAL:
047b4e83 400 case SCORM_TYPE_AICCURL:
cb029f33 401 $defaultvalues['packageurl'] = $defaultvalues['reference'];
9528568b 402 }
403 }
cb029f33 404 unset($defaultvalues['reference']);
9528568b 405
cb029f33
DM
406 if (!empty($defaultvalues['options'])) {
407 $options = explode(',', $defaultvalues['options']);
9528568b 408 foreach ($options as $option) {
409 $opt = explode('=', $option);
410 if (isset($opt[1])) {
cb029f33 411 $defaultvalues[$opt[0]] = $opt[1];
ea38a6b9 412 }
413 }
ea38a6b9 414 }
9528568b 415
cb029f33
DM
416 $this->data_preprocessing($defaultvalues);
417 parent::set_data($defaultvalues);
ea38a6b9 418 }
94db2749 419
cb029f33 420 public function add_completion_rules() {
94db2749
AB
421 $mform =& $this->_form;
422 $items = array();
423
6a75660e 424 // Require score.
94db2749
AB
425 $group = array();
426 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
427 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
428 $mform->setType('completionscorerequired', PARAM_INT);
429 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
430 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
431 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
432 $mform->setDefault('completionscorerequired', 0);
433
434 $items[] = 'completionscoregroup';
435
436
6a75660e 437 // Require status.
94db2749
AB
438 $first = true;
439 $firstkey = null;
440 foreach (scorm_status_options(true) as $key => $value) {
441 $name = null;
442 $key = 'completionstatusrequired['.$key.']';
443 if ($first) {
444 $name = get_string('completionstatusrequired', 'scorm');
445 $first = false;
446 $firstkey = $key;
447 }
448 $mform->addElement('checkbox', $key, $name, $value);
449 $mform->setType($key, PARAM_BOOL);
450 $items[] = $key;
451 }
452 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
453
454 return $items;
455 }
456
457 function completion_rule_enabled($data) {
458 $status = !empty($data['completionstatusrequired']);
459 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
460
461 return $status || $score;
462 }
463
464 function get_data($slashed = true) {
465 $data = parent::get_data($slashed);
466
467 if (!$data) {
468 return false;
469 }
470
82bd0c66
MS
471 // Convert completionstatusrequired to a proper integer, if any.
472 $total = 0;
473 if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
474 foreach (array_keys($data->completionstatusrequired) as $state) {
475 $total |= $state;
476 }
477 $data->completionstatusrequired = $total;
478 }
479
a9f5fc15 480 if (!empty($data->completionunlocked)) {
82bd0c66 481 // Turn off completion settings if the checkboxes aren't ticked.
a9f5fc15 482 $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
483
82bd0c66
MS
484 if (isset($data->completionstatusrequired) && $autocompletion) {
485 // Do nothing: completionstatusrequired has been already converted
486 // into a correct integer representation.
a9f5fc15 487 } else {
488 $data->completionstatusrequired = null;
94db2749
AB
489 }
490
a9f5fc15 491 if (!empty($data->completionscoredisabled) || !$autocompletion) {
492 $data->completionscorerequired = null;
493 }
94db2749
AB
494 }
495
496 return $data;
497 }
a4813c36 498}