SCORM AICC MDL-31039 update external AICC url correctly - thanks to Albert Rhemrev...
[moodle.git] / mod / scorm / mod_form.php
blob9b6af66212b94559b5d5f2af6b3ce1f3c76c6696
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
7 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
9 class mod_scorm_mod_form extends moodleform_mod {
11 function definition() {
12 global $CFG, $COURSE, $OUTPUT;
13 $cfg_scorm = get_config('scorm');
15 $mform = $this->_form;
17 if (!$CFG->slasharguments) {
18 $mform->addElement('static', '', '',$OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
20 //-------------------------------------------------------------------------------
21 $mform->addElement('header', 'general', get_string('general', 'form'));
23 // Name
24 $mform->addElement('text', 'name', get_string('name'));
25 if (!empty($CFG->formatstringstriptags)) {
26 $mform->setType('name', PARAM_TEXT);
27 } else {
28 $mform->setType('name', PARAM_CLEANHTML);
30 $mform->addRule('name', null, 'required', null, 'client');
32 // Summary
33 $this->add_intro_editor(true);
35 // Scorm types
36 $options = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
38 if ($cfg_scorm->allowtypeexternal) {
39 $options[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
42 if ($cfg_scorm->allowtypelocalsync) {
43 $options[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
46 if (!empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
47 $options[SCORM_TYPE_IMSREPOSITORY] = get_string('typeimsrepository', 'scorm');
50 // Reference
51 if (count($options) > 1) {
52 $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $options);
53 $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
54 $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
55 $mform->setType('packageurl', PARAM_RAW);
56 $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
57 $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
58 } else {
59 $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
62 // New local package upload
63 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
64 $mform->setMaxFileSize($maxbytes);
65 $mform->addElement('filepicker', 'packagefile', get_string('package','scorm'));
66 $mform->addHelpButton('packagefile', 'package', 'scorm');
67 $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
69 //-------------------------------------------------------------------------------
70 // Time restrictions
71 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
73 $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"),array('optional' => true));
74 $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"),array('optional' => true));
75 //-------------------------------------------------------------------------------
76 // Other Settings
77 $mform->addElement('header', 'advanced', get_string('othersettings', 'form'));
79 // Grade Method
80 $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
81 $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
82 $mform->setDefault('grademethod', $cfg_scorm->grademethod);
84 // Maximum Grade
85 for ($i=0; $i<=100; $i++) {
86 $grades[$i] = "$i";
88 $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
89 $mform->setDefault('maxgrade', $cfg_scorm->maxgrade);
90 $mform->disabledIf('maxgrade', 'grademethod','eq', GRADESCOES);
92 // Attempts
93 $mform->addElement('static', '', '' ,'<hr />');
95 // Max Attempts
96 $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
97 $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
98 $mform->setDefault('maxattempt', $cfg_scorm->maxattempts);
100 // What Grade
101 $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
102 $mform->disabledIf('whatgrade', 'maxattempt','eq',1);
103 $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
104 $mform->setDefault('whatgrade', $cfg_scorm->whatgrade);
105 $mform->setAdvanced('whatgrade');
107 // Display attempt status
108 $mform->addElement('selectyesno', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'));
109 $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
110 $mform->setDefault('displayattemptstatus', $cfg_scorm->displayattemptstatus);
112 // Force completed
113 $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
114 $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
115 $mform->setDefault('forcecompleted', $cfg_scorm->forcecompleted);
116 $mform->setAdvanced('forcecompleted');
118 // Force new attempt
119 $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
120 $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
121 $mform->setDefault('forcenewattempt', $cfg_scorm->forcenewattempt);
122 $mform->setAdvanced('forcenewattempt');
124 // Last attempt lock - lock the enter button after the last available attempt has been made
125 $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
126 $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
127 $mform->setDefault('lastattemptlock', $cfg_scorm->lastattemptlock);
128 $mform->setAdvanced('lastattemptlock');
130 // Activation period
131 /* $mform->addElement('static', '', '' ,'<hr />');
132 $mform->addElement('static', 'activation', get_string('activation','scorm'));
133 $datestartgrp = array();
134 $datestartgrp[] = &$mform->createElement('date_time_selector', 'startdate');
135 $datestartgrp[] = &$mform->createElement('checkbox', 'startdisabled', null, get_string('disable'));
136 $mform->addGroup($datestartgrp, 'startdategrp', get_string('from'), ' ', false);
137 $mform->setDefault('startdate', 0);
138 $mform->setDefault('startdisabled', 1);
139 $mform->disabledIf('startdategrp', 'startdisabled', 'checked');
141 $dateendgrp = array();
142 $dateendgrp[] = &$mform->createElement('date_time_selector', 'enddate');
143 $dateendgrp[] = &$mform->createElement('checkbox', 'enddisabled', null, get_string('disable'));
144 $mform->addGroup($dateendgrp, 'dateendgrp', get_string('to'), ' ', false);
145 $mform->setDefault('enddate', 0);
146 $mform->setDefault('enddisabled', 1);
147 $mform->disabledIf('dateendgrp', 'enddisabled', 'checked');
150 // Framed / Popup Window
151 $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
152 $mform->setDefault('popup', $cfg_scorm->popup);
153 $mform->setAdvanced('popup');
155 // Width
156 $mform->addElement('text', 'width', get_string('width','scorm'), 'maxlength="5" size="5"');
157 $mform->setDefault('width', $cfg_scorm->framewidth);
158 $mform->setType('width', PARAM_INT);
159 $mform->setAdvanced('width');
160 $mform->disabledIf('width', 'popup', 'eq', 0);
162 // Height
163 $mform->addElement('text', 'height', get_string('height','scorm'), 'maxlength="5" size="5"');
164 $mform->setDefault('height', $cfg_scorm->frameheight);
165 $mform->setType('height', PARAM_INT);
166 $mform->setAdvanced('height');
167 $mform->disabledIf('height', 'popup', 'eq', 0);
169 // Window Options
170 $winoptgrp = array();
171 foreach(scorm_get_popup_options_array() as $key => $value){
172 $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
173 $mform->setDefault($key, $value);
175 $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options','scorm'), '<br />', false);
176 $mform->setAdvanced('winoptgrp');
177 $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
179 // Skip view page
180 $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'),scorm_get_skip_view_array());
181 $mform->addHelpButton('skipview', 'skipview', 'scorm');
182 $mform->setDefault('skipview', $cfg_scorm->skipview);
183 $mform->setAdvanced('skipview');
185 // Hide Browse
186 $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
187 $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
188 $mform->setDefault('hidebrowse', $cfg_scorm->hidebrowse);
189 $mform->setAdvanced('hidebrowse');
191 // Display course structure
192 $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
193 $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
194 $mform->setDefault('displaycoursestructure', $cfg_scorm->displaycoursestructure);
195 $mform->setAdvanced('displaycoursestructure');
197 // Toc display
198 $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
199 $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
200 $mform->setDefault('hidetoc', $cfg_scorm->hidetoc);
201 $mform->setAdvanced('hidetoc');
203 // Hide Navigation panel
204 $mform->addElement('selectyesno', 'hidenav', get_string('hidenav', 'scorm'));
205 $mform->setDefault('hidenav', $cfg_scorm->hidenav);
206 $mform->setAdvanced('hidenav');
207 $mform->disabledIf('hidenav', 'hidetoc', 'noteq', 0);
209 // Autocontinue
210 $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
211 $mform->addHelpButton('auto', 'autocontinue', 'scorm');
212 $mform->setDefault('auto', $cfg_scorm->auto);
213 $mform->setAdvanced('auto');
215 // Update packages timing
216 $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
217 $mform->setDefault('updatefreq', $cfg_scorm->updatefreq);
218 $mform->setAdvanced('updatefreq');
220 //-------------------------------------------------------------------------------
221 // Hidden Settings
222 $mform->addElement('hidden', 'datadir', null);
223 $mform->setType('datadir', PARAM_RAW);
224 $mform->addElement('hidden', 'pkgtype', null);
225 $mform->setType('pkgtype', PARAM_RAW);
226 $mform->addElement('hidden', 'launch', null);
227 $mform->setType('launch', PARAM_RAW);
228 $mform->addElement('hidden', 'redirect', null);
229 $mform->setType('redirect', PARAM_RAW);
230 $mform->addElement('hidden', 'redirecturl', null);
231 $mform->setType('redirecturl', PARAM_RAW);
234 //-------------------------------------------------------------------------------
235 $this->standard_coursemodule_elements();
236 //-------------------------------------------------------------------------------
237 // buttons
238 $this->add_action_buttons();
242 function data_preprocessing(&$default_values) {
243 global $COURSE;
245 if (isset($default_values['popup']) && ($default_values['popup'] == 1) && isset($default_values['options'])) {
246 if (!empty($default_values['options'])) {
247 $options = explode(',',$default_values['options']);
248 foreach ($options as $option) {
249 list($element,$value) = explode('=',$option);
250 $element = trim($element);
251 $default_values[$element] = trim($value);
255 if (isset($default_values['grademethod'])) {
256 $default_values['grademethod'] = intval($default_values['grademethod']);
258 if (isset($default_values['width']) && (strpos($default_values['width'],'%') === false) && ($default_values['width'] <= 100)) {
259 $default_values['width'] .= '%';
261 if (isset($default_values['width']) && (strpos($default_values['height'],'%') === false) && ($default_values['height'] <= 100)) {
262 $default_values['height'] .= '%';
264 $scorms = get_all_instances_in_course('scorm', $COURSE);
265 $coursescorm = current($scorms);
267 $draftitemid = file_get_submitted_draft_itemid('packagefile');
268 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0);
269 $default_values['packagefile'] = $draftitemid;
271 if (($COURSE->format == 'scorm') && ((count($scorms) == 0) || ($default_values['instance'] == $coursescorm->id))) {
272 $default_values['redirect'] = 'yes';
273 $default_values['redirecturl'] = '../course/view.php?id='.$default_values['course'];
274 } else {
275 $default_values['redirect'] = 'no';
276 $default_values['redirecturl'] = '../mod/scorm/view.php?id='.$default_values['coursemodule'];
278 if (isset($default_values['version'])) {
279 $default_values['pkgtype'] = (substr($default_values['version'],0,5) == 'SCORM') ? 'scorm':'aicc';
281 if (isset($default_values['instance'])) {
282 $default_values['datadir'] = $default_values['instance'];
284 if (empty($default_values['timeopen'])) {
285 $default_values['timeopen'] = 0;
287 if (empty($default_values['timeclose'])) {
288 $default_values['timeclose'] = 0;
292 function validation($data, $files) {
293 global $CFG;
294 $errors = parent::validation($data, $files);
296 $type = $data['scormtype'];
298 if ($type === SCORM_TYPE_LOCAL) {
299 if (!empty($data['update'])) {
300 //ok, not required
302 } else if (empty($data['packagefile'])) {
303 $errors['packagefile'] = get_string('required');
305 } else {
306 $files = $this->get_draft_files('packagefile');
307 if (count($files)<1) {
308 $errors['packagefile'] = get_string('required');
309 return $errors;
311 $file = reset($files);
312 $filename = $CFG->dataroot.'/temp/scormimport/scrom_'.time();
313 make_upload_directory('temp/scormimport');
314 $file->copy_content_to($filename);
316 $packer = get_file_packer('application/zip');
318 $filelist = $packer->list_files($filename);
319 if (!is_array($filelist)) {
320 $errors['packagefile'] = 'Incorrect file package - not an archive'; //TODO: localise
321 } else {
322 $manifestpresent = false;
323 $aiccfound = false;
324 foreach ($filelist as $info) {
325 if ($info->pathname == 'imsmanifest.xml') {
326 $manifestpresent = true;
327 break;
329 if (preg_match('/\.cst$/', $info->pathname)) {
330 $aiccfound = true;
331 break;
334 if (!$manifestpresent and !$aiccfound) {
335 $errors['packagefile'] = 'Incorrect file package - missing imsmanifest.xml or AICC structure'; //TODO: localise
338 unlink($filename);
341 } else if ($type === SCORM_TYPE_EXTERNAL) {
342 $reference = $data['packageurl'];
343 if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
344 $errors['packageurl'] = get_string('required'); // TODO: improve help
347 } else if ($type === 'packageurl') {
348 $reference = $data['reference'];
349 if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
350 $errors['packageurl'] = get_string('required'); // TODO: improve help
353 } else if ($type === SCORM_TYPE_IMSREPOSITORY) {
354 $reference = $data['packageurl'];
355 if (stripos($reference, '#') !== 0) {
356 $errors['packageurl'] = get_string('required');
360 return $errors;
363 //need to translate the "options" and "reference" field.
364 function set_data($default_values) {
365 $default_values = (array)$default_values;
367 if (isset($default_values['scormtype']) and isset($default_values['reference'])) {
368 switch ($default_values['scormtype']) {
369 case SCORM_TYPE_LOCALSYNC :
370 case SCORM_TYPE_EXTERNAL:
371 case SCORM_TYPE_IMSREPOSITORY:
372 case SCORM_TYPE_AICCURL:
373 $default_values['packageurl'] = $default_values['reference'];
376 unset($default_values['reference']);
378 if (!empty($default_values['options'])) {
379 $options = explode(',', $default_values['options']);
380 foreach ($options as $option) {
381 $opt = explode('=', $option);
382 if (isset($opt[1])) {
383 $default_values[$opt[0]] = $opt[1];
388 $this->data_preprocessing($default_values);
389 parent::set_data($default_values);