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