SCORM MDL-28111 tidy up formatting of code
[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
23a7718b 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) {
6aff538a 33 $mform->addElement('static', '', '',$OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
01e6c030 34 }
edf13ee8 35 $zlib = ini_get('zlib.output_compression'); //check for zlib compression - if used, throw error because of IE bug. - SEE MDL-16185
36 if (isset($zlib) && $zlib) {
6aff538a 37 $mform->addElement('static', '', '',$OUTPUT->notification(get_string('zlibwarning', 'scorm'), 'notifyproblem'));
edf13ee8 38 }
a4813c36 39//-------------------------------------------------------------------------------
40 $mform->addElement('header', 'general', get_string('general', 'form'));
41
42// Name
43 $mform->addElement('text', 'name', get_string('name'));
8eb1d25f 44 if (!empty($CFG->formatstringstriptags)) {
45 $mform->setType('name', PARAM_TEXT);
46 } else {
b8ea3041 47 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 48 }
a4813c36 49 $mform->addRule('name', null, 'required', null, 'client');
50
51// Summary
ac3668bf 52 $this->add_intro_editor(true);
a4813c36 53
9528568b 54// Scorm types
55 $options = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
56
30fc6e2d 57 if ($cfg_scorm->allowtypeexternal) {
9528568b 58 $options[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
59 }
60
30fc6e2d 61 if ($cfg_scorm->allowtypelocalsync) {
9528568b 62 $options[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
63 }
64
30fc6e2d 65 if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
9528568b 66 $options[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
67 }
68
a4813c36 69// Reference
9528568b 70 if (count($options) > 1) {
e73d5557 71 $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $options);
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
81// New local package upload
560417ff 82 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
83 $mform->setMaxFileSize($maxbytes);
d3d98a3a 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
d54e2145 88//-------------------------------------------------------------------------------
89// Time restrictions
90 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
d54e2145 91
413900b4
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));
a4813c36 94//-------------------------------------------------------------------------------
95// Other Settings
3b631eda 96 $mform->addElement('header', 'advanced', get_string('othersettings', 'form'));
a4813c36 97
a4813c36 98// Grade Method
30fc6e2d 99 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
c329e370 100 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
30fc6e2d 101 $mform->setDefault('grademethod', $cfg_scorm->grademethod);
3268cf99 102
a4813c36 103// Maximum Grade
104 for ($i=0; $i<=100; $i++) {
105 $grades[$i] = "$i";
106 }
107 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
30fc6e2d 108 $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
9528568b 109 $mform->disabledIf('maxgrade', 'grademethod','eq', GRADESCOES);
a4813c36 110
111// Attempts
112 $mform->addElement('static', '', '' ,'<hr />');
a4813c36 113
114// Max Attempts
30fc6e2d 115 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
c329e370 116 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
30fc6e2d 117 $mform->setDefault('maxattempt', $cfg_scorm->maxattempts);
3268cf99 118
87db13b5
PH
119// What Grade
120 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
121 $mform->disabledIf('whatgrade', 'maxattempt','eq',1);
c329e370 122 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
87db13b5
PH
123 $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
124 $mform->setAdvanced('whatgrade');
125
6381fa56 126// Display attempt status
127 $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
c329e370 128 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
30fc6e2d 129 $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
3268cf99 130
6381fa56 131// Force completed
132 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
c329e370 133 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
30fc6e2d 134 $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
6381fa56 135 $mform->setAdvanced('forcecompleted');
136
137// Force new attempt
138 $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
c329e370 139 $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
30fc6e2d 140 $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
6381fa56 141 $mform->setAdvanced('forcenewattempt');
3268cf99 142
6381fa56 143// Last attempt lock - lock the enter button after the last available attempt has been made
144 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
c329e370 145 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
30fc6e2d 146 $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
6381fa56 147 $mform->setAdvanced('lastattemptlock');
3268cf99 148
a4813c36 149// Activation period
2b90f941 150/* $mform->addElement('static', '', '' ,'<hr />');
a4813c36 151 $mform->addElement('static', 'activation', get_string('activation','scorm'));
152 $datestartgrp = array();
153 $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
154 $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
155 $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
156 $mform->setDefault('startdate', 0);
157 $mform->setDefault('startdisabled', 1);
158 $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
159
160 $dateendgrp = array();
161 $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
162 $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
163 $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
164 $mform->setDefault('enddate', 0);
165 $mform->setDefault('enddisabled', 1);
166 $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
2b90f941 167*/
3268cf99 168
a73e2664
DM
169// Framed / Popup Window
170 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
171 $mform->setDefault('popup', $cfg_scorm->popup);
172 $mform->setAdvanced('popup');
173
a4813c36 174// Width
9528568b 175 $mform->addElement('text', 'width', get_string('width','scorm'), 'maxlength="5" size="5"');
30fc6e2d 176 $mform->setDefault('width', $cfg_scorm->framewidth);
a4813c36 177 $mform->setType('width', PARAM_INT);
a73e2664
DM
178 $mform->setAdvanced('width');
179 $mform->disabledIf('width', 'popup', 'eq', 0);
9528568b 180
a4813c36 181// Height
9528568b 182 $mform->addElement('text', 'height', get_string('height','scorm'), 'maxlength="5" size="5"');
30fc6e2d 183 $mform->setDefault('height', $cfg_scorm->frameheight);
a4813c36 184 $mform->setType('height', PARAM_INT);
a73e2664
DM
185 $mform->setAdvanced('height');
186 $mform->disabledIf('height', 'popup', 'eq', 0);
a4813c36 187
188// Window Options
189 $winoptgrp = array();
30fc6e2d 190 foreach(scorm_get_popup_options_array() as $key => $value){
191 $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
192 $mform->setDefault($key, $value);
193 }
4b07d3a8 194 $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options','scorm'), '<br />', false);
a4813c36 195 $mform->setAdvanced('winoptgrp');
196 $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
197
198// Skip view page
30fc6e2d 199 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'),scorm_get_skip_view_array());
c329e370 200 $mform->addHelpButton('skipview', 'skipview', 'scorm');
30fc6e2d 201 $mform->setDefault('skipview', $cfg_scorm->skipview);
a4813c36 202 $mform->setAdvanced('skipview');
203
204// Hide Browse
205 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
c329e370 206 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
30fc6e2d 207 $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
a4813c36 208 $mform->setAdvanced('hidebrowse');
209
87db13b5
PH
210// Display course structure
211 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
c329e370 212 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
87db13b5
PH
213 $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
214 $mform->setAdvanced('displaycoursestructure');
c329e370 215
a4813c36 216// Toc display
30fc6e2d 217 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
c329e370 218 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
30fc6e2d 219 $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
a4813c36 220 $mform->setAdvanced('hidetoc');
221
222// Hide Navigation panel
223 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
30fc6e2d 224 $mform->setDefault('hidenav', $cfg_scorm->hidenav);
a4813c36 225 $mform->setAdvanced('hidenav');
226
227// Autocontinue
228 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
c329e370 229 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
30fc6e2d 230 $mform->setDefault('auto', $cfg_scorm->auto);
a4813c36 231 $mform->setAdvanced('auto');
232
a679d64d 233// Update packages timing
30fc6e2d 234 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
235 $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
a679d64d 236 $mform->setAdvanced('updatefreq');
a4813c36 237
238//-------------------------------------------------------------------------------
239// Hidden Settings
240 $mform->addElement('hidden', 'datadir', null);
d18e0fe6 241 $mform->setType('datadir', PARAM_RAW);
a4813c36 242 $mform->addElement('hidden', 'pkgtype', null);
d18e0fe6 243 $mform->setType('pkgtype', PARAM_RAW);
a4813c36 244 $mform->addElement('hidden', 'launch', null);
d18e0fe6 245 $mform->setType('launch', PARAM_RAW);
a4813c36 246 $mform->addElement('hidden', 'redirect', null);
d18e0fe6 247 $mform->setType('redirect', PARAM_RAW);
a4813c36 248 $mform->addElement('hidden', 'redirecturl', null);
d18e0fe6 249 $mform->setType('redirecturl', PARAM_RAW);
a4813c36 250
251
252//-------------------------------------------------------------------------------
42f103be 253 $this->standard_coursemodule_elements();
a4813c36 254//-------------------------------------------------------------------------------
255 // buttons
256 $this->add_action_buttons();
257
258 }
259
728d0599 260 function data_preprocessing(&$default_values) {
a4813c36 261 global $COURSE;
262
263 if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
1adc77e6 264 if (!empty($default_values['options'])) {
265 $options = explode(',',$default_values['options']);
266 foreach ($options as $option) {
267 list($element,$value) = explode('=',$option);
268 $element = trim($element);
269 $default_values[$element] = trim($value);
270 }
a4813c36 271 }
272 }
273 if (isset($default_values['grademethod'])) {
adefe70e 274 $default_values['grademethod'] = intval($default_values['grademethod']);
a4813c36 275 }
efe95a6f
PS
276 if (isset($default_values['width']) && (strpos($default_values['width'],'%') === false) && ($default_values['width'] <= 100)) {
277 $default_values['width'] .= '%';
a4813c36 278 }
efe95a6f
PS
279 if (isset($default_values['width']) && (strpos($default_values['height'],'%') === false) && ($default_values['height'] <= 100)) {
280 $default_values['height'] .= '%';
a4813c36 281 }
282 $scorms = get_all_instances_in_course('scorm', $COURSE);
283 $coursescorm = current($scorms);
b76f2e60
DC
284
285 $draftitemid = file_get_submitted_draft_itemid('packagefile');
783f1486 286 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
b76f2e60
DC
287 $default_values['packagefile'] = $draftitemid;
288
a4813c36 289 if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
290 $default_values['redirect'] = 'yes';
9528568b 291 $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
a4813c36 292 } else {
293 $default_values['redirect'] = 'no';
294 $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
295 }
296 if (isset($default_values['version'])) {
297 $default_values['pkgtype'] = (substr($default_values['version'],0,5) == 'SCORM') ? 'scorm':'aicc';
298 }
299 if (isset($default_values['instance'])) {
300 $default_values['datadir'] = $default_values['instance'];
301 }
d54e2145 302 if (empty($default_values['timeopen'])) {
413900b4
DM
303 $default_values['timeopen'] = 0;
304 }
305 if (empty($default_values['timeclose'])) {
306 $default_values['timeclose'] = 0;
d54e2145 307 }
a4813c36 308 }
309
a78890d5 310 function validation($data, $files) {
d3d98a3a 311 global $CFG;
a78890d5 312 $errors = parent::validation($data, $files);
60243313 313
9528568b 314 $type = $data['scormtype'];
315
316 if ($type === SCORM_TYPE_LOCAL) {
317 if (!empty($data['update'])) {
318 //ok, not required
a679d64d 319
d3d98a3a 320 } else if (empty($data['packagefile'])) {
9528568b 321 $errors['packagefile'] = get_string('required');
322
323 } else {
d3d98a3a
DC
324 $files = $this->get_draft_files('packagefile');
325 if (count($files)<1) {
326 $errors['packagefile'] = get_string('required');
9eb8dccc 327 return $errors;
d3d98a3a
DC
328 }
329 $file = reset($files);
330 $filename = $CFG->dataroot.'/temp/scormimport/scrom_'.time();
331 make_upload_directory('temp/scormimport');
332 $file->copy_content_to($filename);
333
9528568b 334 $packer = get_file_packer('application/zip');
335
d3d98a3a 336 $filelist = $packer->list_files($filename);
9528568b 337 if (!is_array($filelist)) {
338 $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
339 } else {
340 $manifestpresent = false;
341 $aiccfound = false;
342 foreach ($filelist as $info) {
343 if ($info->pathname == 'imsmanifest.xml') {
344 $manifestpresent = true;
345 break;
346 }
347 if (preg_match('/\.cst$/', $info->pathname)) {
348 $aiccfound = true;
349 break;
350 }
351 }
352 if (!$manifestpresent and !$aiccfound) {
353 $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
354 }
355 }
d3d98a3a 356 unlink($filename);
9528568b 357 }
358
359 } else if ($type === SCORM_TYPE_EXTERNAL) {
360 $reference = $data['packageurl'];
361 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
362 $errors['packageurl'] = get_string('required'); // TODO: improve help
363 }
364
365 } else if ($type === 'packageurl') {
366 $reference = $data['reference'];
367 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
368 $errors['packageurl'] = get_string('required'); // TODO: improve help
369 }
370
371 } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
372 $reference = $data['packageurl'];
373 if (stripos($reference, '#') !== 0) {
374 $errors['packageurl'] = get_string('required');
375 }
60243313 376 }
377
a78890d5 378 return $errors;
a679d64d 379 }
9528568b 380
381 //need to translate the "options" and "reference" field.
ea38a6b9 382 function set_data($default_values) {
9528568b 383 $default_values = (array)$default_values;
384
385 if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
386 switch ($default_values['scormtype']) {
387 case SCORM_TYPE_LOCALSYNC :
388 case SCORM_TYPE_EXTERNAL:
389 case SCORM_TYPE_IMSREPOSITORY:
390 $default_values['packageurl'] = $default_values['reference'];
391 }
392 }
393 unset($default_values['reference']);
394
395 if (!empty($default_values['options'])) {
396 $options = explode(',', $default_values['options']);
397 foreach ($options as $option) {
398 $opt = explode('=', $option);
399 if (isset($opt[1])) {
9b426d03 400 $default_values[$opt[0]] = $opt[1];
ea38a6b9 401 }
402 }
ea38a6b9 403 }
9528568b 404
ea38a6b9 405 $this->data_preprocessing($default_values);
9528568b 406 parent::set_data($default_values);
ea38a6b9 407 }
a4813c36 408}
e5dd8e3b 409