weekly 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 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
27b87c10
DM
84 // Update packages timing.
85 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
86 $mform->setType('updatefreq', PARAM_INT);
87 $mform->setDefault('updatefreq', $cfgscorm->updatefreq);
88 $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
20b51a11 89
6a75660e 90 // New local package upload.
27b87c10
DM
91 $filemanageroptions = array();
92 $filemanageroptions['accepted_types'] = array('.zip');
93 $filemanageroptions['maxbytes'] = 0;
94 $filemanageroptions['maxfiles'] = 1;
95 $filemanageroptions['subdirs'] = 0;
96
97 $mform->addElement('filemanager', 'packagefile', get_string('package', 'scorm'), null, $filemanageroptions);
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');
27b87c10
DM
283 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0,
284 array('subdirs' => 0, 'maxfiles' => 1));
cb029f33 285 $defaultvalues['packagefile'] = $draftitemid;
b76f2e60 286
cb029f33
DM
287 if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) {
288 $defaultvalues['redirect'] = 'yes';
289 $defaultvalues['redirecturl'] = '../course/view.php?id='.$defaultvalues['course'];
a4813c36 290 } else {
cb029f33
DM
291 $defaultvalues['redirect'] = 'no';
292 $defaultvalues['redirecturl'] = '../mod/scorm/view.php?id='.$defaultvalues['coursemodule'];
a4813c36 293 }
cb029f33
DM
294 if (isset($defaultvalues['version'])) {
295 $defaultvalues['pkgtype'] = (substr($defaultvalues['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
a4813c36 296 }
cb029f33
DM
297 if (isset($defaultvalues['instance'])) {
298 $defaultvalues['datadir'] = $defaultvalues['instance'];
a4813c36 299 }
cb029f33
DM
300 if (empty($defaultvalues['timeopen'])) {
301 $defaultvalues['timeopen'] = 0;
413900b4 302 }
cb029f33
DM
303 if (empty($defaultvalues['timeclose'])) {
304 $defaultvalues['timeclose'] = 0;
d54e2145 305 }
94db2749 306
6a75660e 307 // Set some completion default data.
cb029f33 308 if (!empty($defaultvalues['completionstatusrequired']) && !is_array($defaultvalues['completionstatusrequired'])) {
6a75660e 309 // Unpack values.
94db2749
AB
310 $cvalues = array();
311 foreach (scorm_status_options() as $key => $value) {
cb029f33 312 if (($defaultvalues['completionstatusrequired'] & $key) == $key) {
94db2749
AB
313 $cvalues[$key] = 1;
314 }
315 }
316
cb029f33 317 $defaultvalues['completionstatusrequired'] = $cvalues;
94db2749
AB
318 }
319
cb029f33
DM
320 if (!isset($defaultvalues['completionscorerequired']) || !strlen($defaultvalues['completionscorerequired'])) {
321 $defaultvalues['completionscoredisabled'] = 1;
94db2749
AB
322 }
323
a4813c36 324 }
325
cb029f33 326 public function validation($data, $files) {
27b87c10 327 global $CFG, $USER;
a78890d5 328 $errors = parent::validation($data, $files);
60243313 329
9528568b 330 $type = $data['scormtype'];
331
332 if ($type === SCORM_TYPE_LOCAL) {
27b87c10 333 if (empty($data['packagefile'])) {
9528568b 334 $errors['packagefile'] = get_string('required');
335
336 } else {
27b87c10
DM
337 $draftitemid = file_get_submitted_draft_itemid('packagefile');
338
339 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'packagefilecheck', null,
340 array('subdirs' => 0, 'maxfiles' => 1));
341
342 // Get file from users draft area.
343 $usercontext = context_user::instance($USER->id);
344 $fs = get_file_storage();
345 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
346
d3d98a3a
DC
347 if (count($files)<1) {
348 $errors['packagefile'] = get_string('required');
9eb8dccc 349 return $errors;
d3d98a3a
DC
350 }
351 $file = reset($files);
27b87c10
DM
352 if (!$file->is_external_file() && !empty($data['updatefreq'])) {
353 // Make sure updatefreq is not set if using normal local file.
354 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_scorm');
355 }
c7e6fb6c 356 $errors = array_merge($errors, scorm_validate_package($file));
9528568b 357 }
358
359 } else if ($type === SCORM_TYPE_EXTERNAL) {
360 $reference = $data['packageurl'];
f833171f 361 // Syntax check.
9528568b 362 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
4388bd45 363 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
364 } else {
365 // Availability check.
366 $result = scorm_check_url($reference);
367 if (is_string($result)) {
368 $errors['packageurl'] = $result;
369 }
9528568b 370 }
371
372 } else if ($type === 'packageurl') {
373 $reference = $data['reference'];
f833171f 374 // Syntax check.
9528568b 375 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
4388bd45 376 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
377 } else {
378 // Availability check.
379 $result = scorm_check_url($reference);
380 if (is_string($result)) {
381 $errors['packageurl'] = $result;
382 }
9528568b 383 }
384
4388bd45
DM
385 } else if ($type === SCORM_TYPE_AICCURL) {
386 $reference = $data['packageurl'];
f833171f 387 // Syntax check.
4388bd45
DM
388 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
389 $errors['packageurl'] = get_string('invalidurl', 'scorm');
f833171f
MS
390 } else {
391 // Availability check.
392 $result = scorm_check_url($reference);
393 if (is_string($result)) {
394 $errors['packageurl'] = $result;
395 }
9528568b 396 }
f833171f 397
60243313 398 }
399
a78890d5 400 return $errors;
a679d64d 401 }
9528568b 402
6a75660e 403 // Need to translate the "options" and "reference" field.
cb029f33
DM
404 public function set_data($defaultvalues) {
405 $defaultvalues = (array)$defaultvalues;
9528568b 406
cb029f33
DM
407 if (isset($defaultvalues['scormtype']) and isset($defaultvalues['reference'])) {
408 switch ($defaultvalues['scormtype']) {
9528568b 409 case SCORM_TYPE_LOCALSYNC :
410 case SCORM_TYPE_EXTERNAL:
047b4e83 411 case SCORM_TYPE_AICCURL:
cb029f33 412 $defaultvalues['packageurl'] = $defaultvalues['reference'];
9528568b 413 }
414 }
cb029f33 415 unset($defaultvalues['reference']);
9528568b 416
cb029f33
DM
417 if (!empty($defaultvalues['options'])) {
418 $options = explode(',', $defaultvalues['options']);
9528568b 419 foreach ($options as $option) {
420 $opt = explode('=', $option);
421 if (isset($opt[1])) {
cb029f33 422 $defaultvalues[$opt[0]] = $opt[1];
ea38a6b9 423 }
424 }
ea38a6b9 425 }
9528568b 426
cb029f33
DM
427 $this->data_preprocessing($defaultvalues);
428 parent::set_data($defaultvalues);
ea38a6b9 429 }
94db2749 430
cb029f33 431 public function add_completion_rules() {
94db2749
AB
432 $mform =& $this->_form;
433 $items = array();
434
6a75660e 435 // Require score.
94db2749
AB
436 $group = array();
437 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
438 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
439 $mform->setType('completionscorerequired', PARAM_INT);
440 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
441 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
442 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
443 $mform->setDefault('completionscorerequired', 0);
444
445 $items[] = 'completionscoregroup';
446
447
6a75660e 448 // Require status.
94db2749
AB
449 $first = true;
450 $firstkey = null;
451 foreach (scorm_status_options(true) as $key => $value) {
452 $name = null;
453 $key = 'completionstatusrequired['.$key.']';
454 if ($first) {
455 $name = get_string('completionstatusrequired', 'scorm');
456 $first = false;
457 $firstkey = $key;
458 }
459 $mform->addElement('checkbox', $key, $name, $value);
460 $mform->setType($key, PARAM_BOOL);
461 $items[] = $key;
462 }
463 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
464
465 return $items;
466 }
467
468 function completion_rule_enabled($data) {
469 $status = !empty($data['completionstatusrequired']);
470 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
471
472 return $status || $score;
473 }
474
475 function get_data($slashed = true) {
476 $data = parent::get_data($slashed);
477
478 if (!$data) {
479 return false;
480 }
481
82bd0c66
MS
482 // Convert completionstatusrequired to a proper integer, if any.
483 $total = 0;
484 if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
485 foreach (array_keys($data->completionstatusrequired) as $state) {
486 $total |= $state;
487 }
488 $data->completionstatusrequired = $total;
489 }
490
a9f5fc15 491 if (!empty($data->completionunlocked)) {
82bd0c66 492 // Turn off completion settings if the checkboxes aren't ticked.
a9f5fc15 493 $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
494
82bd0c66
MS
495 if (isset($data->completionstatusrequired) && $autocompletion) {
496 // Do nothing: completionstatusrequired has been already converted
497 // into a correct integer representation.
a9f5fc15 498 } else {
499 $data->completionstatusrequired = null;
94db2749
AB
500 }
501
a9f5fc15 502 if (!empty($data->completionscoredisabled) || !$autocompletion) {
503 $data->completionscorerequired = null;
504 }
94db2749
AB
505 }
506
507 return $data;
508 }
a4813c36 509}