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