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