Merge branch 'MDL-38315-m24' of git://github.com/sammarshallou/moodle into MOODLE_24_...
[moodle.git] / mod / scorm / mod_form.php
blobd674ef9f737ea44109593cbd3b8b99c27371f8d0
1 <?php
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.
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/>.
17 if (!defined('MOODLE_INTERNAL')) {
18 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
21 require_once($CFG->dirroot.'/course/moodleform_mod.php');
22 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
24 class mod_scorm_mod_form extends moodleform_mod {
26 function definition() {
27 global $CFG, $COURSE, $OUTPUT;
28 $cfg_scorm = get_config('scorm');
30 $mform = $this->_form;
32 if (!$CFG->slasharguments) {
33 $mform->addElement('static', '', '', $OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
35 //-------------------------------------------------------------------------------
36 $mform->addElement('header', 'general', get_string('general', 'form'));
38 // Name
39 $mform->addElement('text', 'name', get_string('name'));
40 if (!empty($CFG->formatstringstriptags)) {
41 $mform->setType('name', PARAM_TEXT);
42 } else {
43 $mform->setType('name', PARAM_CLEANHTML);
45 $mform->addRule('name', null, 'required', null, 'client');
46 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
48 // Summary
49 $this->add_intro_editor(true);
51 // Scorm types
52 $scormtypes = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
54 if ($cfg_scorm->allowtypeexternal) {
55 $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
58 if ($cfg_scorm->allowtypelocalsync) {
59 $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
62 if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
63 $scormtypes[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
66 if ($cfg_scorm->allowtypeexternalaicc) {
67 $scormtypes[SCORM_TYPE_AICCURL] = get_string('typeaiccurl', 'scorm');
70 // Reference
71 if (count($scormtypes) > 1) {
72 $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
73 $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
74 $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
75 $mform->setType('packageurl', PARAM_RAW);
76 $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
77 $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
78 } else {
79 $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
82 // New local package upload
83 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
84 $mform->setMaxFileSize($maxbytes);
85 $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm'));
86 $mform->addHelpButton('packagefile', 'package', 'scorm');
87 $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
89 //-------------------------------------------------------------------------------
90 // Time restrictions
91 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
93 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
94 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
95 //-------------------------------------------------------------------------------
96 // display Settings
97 $mform->addElement('header', 'displaysettings', get_string('displaysettings', 'scorm'));
98 // Framed / Popup Window
99 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
100 $mform->setDefault('popup', $cfg_scorm->popup);
101 $mform->setAdvanced('popup', $cfg_scorm->popup_adv);
103 // Width
104 $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
105 $mform->setDefault('width', $cfg_scorm->framewidth);
106 $mform->setType('width', PARAM_INT);
107 $mform->setAdvanced('width', $cfg_scorm->framewidth_adv);
108 $mform->disabledIf('width', 'popup', 'eq', 0);
110 // Height
111 $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
112 $mform->setDefault('height', $cfg_scorm->frameheight);
113 $mform->setType('height', PARAM_INT);
114 $mform->setAdvanced('height', $cfg_scorm->frameheight_adv);
115 $mform->disabledIf('height', 'popup', 'eq', 0);
117 // Window Options
118 $winoptgrp = array();
119 foreach (scorm_get_popup_options_array() as $key => $value) {
120 $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
121 $mform->setDefault($key, $value);
123 $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
124 $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
125 $mform->setAdvanced('winoptgrp', $cfg_scorm->winoptgrp_adv);
127 // Skip view page
128 $skipviewoptions = scorm_get_skip_view_array();
129 if ($COURSE->format == 'scorm') { // Remove option that would cause a constant redirect.
130 unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
131 if ($cfg_scorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
132 $cfg_scorm->skipview = SCORM_SKIPVIEW_FIRST;
135 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
136 $mform->addHelpButton('skipview', 'skipview', 'scorm');
137 $mform->setDefault('skipview', $cfg_scorm->skipview);
138 $mform->setAdvanced('skipview', $cfg_scorm->skipview_adv);
140 // Hide Browse
141 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
142 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
143 $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
144 $mform->setAdvanced('hidebrowse', $cfg_scorm->hidebrowse_adv);
146 // Display course structure
147 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
148 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
149 $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
150 $mform->setAdvanced('displaycoursestructure', $cfg_scorm->displaycoursestructure_adv);
152 // Toc display
153 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
154 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
155 $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
156 $mform->setAdvanced('hidetoc', $cfg_scorm->hidetoc_adv);
158 // Hide Navigation panel
159 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
160 $mform->setDefault('hidenav', $cfg_scorm->hidenav);
161 $mform->setAdvanced('hidenav', $cfg_scorm->hidenav_adv);
162 $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
164 //-------------------------------------------------------------------------------
165 // grade Settings
166 $mform->addElement('header', 'gradesettings', get_string('gradesettings', 'scorm'));
168 // Grade Method
169 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
170 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
171 $mform->setDefault('grademethod', $cfg_scorm->grademethod);
172 $mform->setAdvanced('grademethod', $cfg_scorm->grademethod_adv);
174 // Maximum Grade
175 for ($i=0; $i<=100; $i++) {
176 $grades[$i] = "$i";
178 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
179 $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
180 $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
181 $mform->setAdvanced('maxgrade', $cfg_scorm->maxgrade_adv);
183 $mform->addElement('header', 'othersettings', get_string('othersettings', 'scorm'));
185 // Max Attempts
186 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
187 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
188 $mform->setDefault('maxattempt', $cfg_scorm->maxattempt);
189 $mform->setAdvanced('maxattempt', $cfg_scorm->maxattempt_adv);
191 // What Grade
192 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
193 $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
194 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
195 $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
196 $mform->setAdvanced('whatgrade', $cfg_scorm->whatgrade_adv);
198 // Display attempt status
199 $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'), scorm_get_attemptstatus_array());
200 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
201 $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
202 $mform->setAdvanced('displayattemptstatus', $cfg_scorm->displayattemptstatus_adv);
204 // Force completed
205 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
206 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
207 $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
208 $mform->setAdvanced('forcecompleted', $cfg_scorm->forcecompleted_adv);
210 // Force new attempt
211 $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
212 $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
213 $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
214 $mform->setAdvanced('forcenewattempt', $cfg_scorm->forcenewattempt_adv);
216 // Last attempt lock - lock the enter button after the last available attempt has been made
217 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
218 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
219 $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
220 $mform->setAdvanced('lastattemptlock', $cfg_scorm->lastattemptlock_adv);
222 // Activation period
223 /* $mform->addElement('static', '', '' ,'<hr />');
224 $mform->addElement('static', 'activation', get_string('activation','scorm'));
225 $datestartgrp = array();
226 $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
227 $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
228 $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
229 $mform->setDefault('startdate', 0);
230 $mform->setDefault('startdisabled', 1);
231 $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
233 $dateendgrp = array();
234 $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
235 $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
236 $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
237 $mform->setDefault('enddate', 0);
238 $mform->setDefault('enddisabled', 1);
239 $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
242 // Autocontinue
243 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
244 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
245 $mform->setDefault('auto', $cfg_scorm->auto);
246 $mform->setAdvanced('auto', $cfg_scorm->auto_adv);
248 if (count($scormtypes) > 1) {
249 // Update packages timing
250 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
251 $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
252 $mform->setAdvanced('updatefreq', $cfg_scorm->updatefreq_adv);
253 $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
254 $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
255 } else {
256 $mform->addElement('hidden', 'updatefreq', 0);
258 //-------------------------------------------------------------------------------
259 // Hidden Settings
260 $mform->addElement('hidden', 'datadir', null);
261 $mform->setType('datadir', PARAM_RAW);
262 $mform->addElement('hidden', 'pkgtype', null);
263 $mform->setType('pkgtype', PARAM_RAW);
264 $mform->addElement('hidden', 'launch', null);
265 $mform->setType('launch', PARAM_RAW);
266 $mform->addElement('hidden', 'redirect', null);
267 $mform->setType('redirect', PARAM_RAW);
268 $mform->addElement('hidden', 'redirecturl', null);
269 $mform->setType('redirecturl', PARAM_RAW);
271 //-------------------------------------------------------------------------------
272 $this->standard_coursemodule_elements();
273 //-------------------------------------------------------------------------------
274 // buttons
275 $this->add_action_buttons();
278 function data_preprocessing(&$default_values) {
279 global $COURSE;
281 if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
282 if (!empty($default_values['options'])) {
283 $options = explode(',', $default_values['options']);
284 foreach ($options as $option) {
285 list($element, $value) = explode('=', $option);
286 $element = trim($element);
287 $default_values[$element] = trim($value);
291 if (isset($default_values['grademethod'])) {
292 $default_values['grademethod'] = intval($default_values['grademethod']);
294 if (isset($default_values['width']) && (strpos($default_values['width'], '%') === false) && ($default_values['width'] <= 100)) {
295 $default_values['width'] .= '%';
297 if (isset($default_values['width']) && (strpos($default_values['height'], '%') === false) && ($default_values['height'] <= 100)) {
298 $default_values['height'] .= '%';
300 $scorms = get_all_instances_in_course('scorm', $COURSE);
301 $coursescorm = current($scorms);
303 $draftitemid = file_get_submitted_draft_itemid('packagefile');
304 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
305 $default_values['packagefile'] = $draftitemid;
307 if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
308 $default_values['redirect'] = 'yes';
309 $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
310 } else {
311 $default_values['redirect'] = 'no';
312 $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
314 if (isset($default_values['version'])) {
315 $default_values['pkgtype'] = (substr($default_values['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
317 if (isset($default_values['instance'])) {
318 $default_values['datadir'] = $default_values['instance'];
320 if (empty($default_values['timeopen'])) {
321 $default_values['timeopen'] = 0;
323 if (empty($default_values['timeclose'])) {
324 $default_values['timeclose'] = 0;
327 // Set some completion default data
328 if (!empty($default_values['completionstatusrequired']) && !is_array($default_values['completionstatusrequired'])) {
329 // Unpack values
330 $cvalues = array();
331 foreach (scorm_status_options() as $key => $value) {
332 if (($default_values['completionstatusrequired'] & $key) == $key) {
333 $cvalues[$key] = 1;
337 $default_values['completionstatusrequired'] = $cvalues;
340 if (!isset($default_values['completionscorerequired']) || !strlen($default_values['completionscorerequired'])) {
341 $default_values['completionscoredisabled'] = 1;
346 function validation($data, $files) {
347 global $CFG;
348 $errors = parent::validation($data, $files);
350 $type = $data['scormtype'];
352 if ($type === SCORM_TYPE_LOCAL) {
353 if (!empty($data['update'])) {
354 //ok, not required
356 } else if (empty($data['packagefile'])) {
357 $errors['packagefile'] = get_string('required');
359 } else {
360 $files = $this->get_draft_files('packagefile');
361 if (count($files)<1) {
362 $errors['packagefile'] = get_string('required');
363 return $errors;
365 $file = reset($files);
366 $filename = $CFG->tempdir.'/scormimport/scrom_'.time();
367 make_temp_directory('scormimport');
368 $file->copy_content_to($filename);
370 $packer = get_file_packer('application/zip');
372 $filelist = $packer->list_files($filename);
373 if (!is_array($filelist)) {
374 $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
375 } else {
376 $manifestpresent = false;
377 $aiccfound = false;
378 foreach ($filelist as $info) {
379 if ($info->pathname == 'imsmanifest.xml') {
380 $manifestpresent = true;
381 break;
383 if (preg_match('/\.cst$/', $info->pathname)) {
384 $aiccfound = true;
385 break;
388 if (!$manifestpresent and !$aiccfound) {
389 $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
392 unlink($filename);
395 } else if ($type === SCORM_TYPE_EXTERNAL) {
396 $reference = $data['packageurl'];
397 // Syntax check.
398 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
399 $errors['packageurl'] = get_string('invalidurl', 'scorm');
400 } else {
401 // Availability check.
402 $result = scorm_check_url($reference);
403 if (is_string($result)) {
404 $errors['packageurl'] = $result;
408 } else if ($type === 'packageurl') {
409 $reference = $data['reference'];
410 // Syntax check.
411 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
412 $errors['packageurl'] = get_string('invalidurl', 'scorm');
413 } else {
414 // Availability check.
415 $result = scorm_check_url($reference);
416 if (is_string($result)) {
417 $errors['packageurl'] = $result;
421 } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
422 $reference = $data['packageurl'];
423 if (stripos($reference, '#') !== 0) {
424 $errors['packageurl'] = get_string('invalidurl', 'scorm');
427 } else if ($type === SCORM_TYPE_AICCURL) {
428 $reference = $data['packageurl'];
429 // Syntax check.
430 if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
431 $errors['packageurl'] = get_string('invalidurl', 'scorm');
432 } else {
433 // Availability check.
434 $result = scorm_check_url($reference);
435 if (is_string($result)) {
436 $errors['packageurl'] = $result;
442 return $errors;
445 //need to translate the "options" and "reference" field.
446 function set_data($default_values) {
447 $default_values = (array)$default_values;
449 if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
450 switch ($default_values['scormtype']) {
451 case SCORM_TYPE_LOCALSYNC :
452 case SCORM_TYPE_EXTERNAL:
453 case SCORM_TYPE_IMSREPOSITORY:
454 case SCORM_TYPE_AICCURL:
455 $default_values['packageurl'] = $default_values['reference'];
458 unset($default_values['reference']);
460 if (!empty($default_values['options'])) {
461 $options = explode(',', $default_values['options']);
462 foreach ($options as $option) {
463 $opt = explode('=', $option);
464 if (isset($opt[1])) {
465 $default_values[$opt[0]] = $opt[1];
470 $this->data_preprocessing($default_values);
471 parent::set_data($default_values);
474 function add_completion_rules() {
475 $mform =& $this->_form;
476 $items = array();
478 // Require score
479 $group = array();
480 $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
481 $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
482 $mform->setType('completionscorerequired', PARAM_INT);
483 $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
484 $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
485 $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
486 $mform->setDefault('completionscorerequired', 0);
488 $items[] = 'completionscoregroup';
491 // Require status
492 $first = true;
493 $firstkey = null;
494 foreach (scorm_status_options(true) as $key => $value) {
495 $name = null;
496 $key = 'completionstatusrequired['.$key.']';
497 if ($first) {
498 $name = get_string('completionstatusrequired', 'scorm');
499 $first = false;
500 $firstkey = $key;
502 $mform->addElement('checkbox', $key, $name, $value);
503 $mform->setType($key, PARAM_BOOL);
504 $items[] = $key;
506 $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
508 return $items;
511 function completion_rule_enabled($data) {
512 $status = !empty($data['completionstatusrequired']);
513 $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
515 return $status || $score;
518 function get_data($slashed = true) {
519 $data = parent::get_data($slashed);
521 if (!$data) {
522 return false;
525 if (!empty($data->completionunlocked)) {
526 // Turn off completion settings if the checkboxes aren't ticked
527 $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
529 if (isset($data->completionstatusrequired) &&
530 is_array($data->completionstatusrequired) && $autocompletion) {
531 $total = 0;
532 foreach (array_keys($data->completionstatusrequired) as $state) {
533 $total |= $state;
536 $data->completionstatusrequired = $total;
537 } else {
538 $data->completionstatusrequired = null;
541 if (!empty($data->completionscoredisabled) || !$autocompletion) {
542 $data->completionscorerequired = null;
546 return $data;