PageRenderTime 81ms CodeModel.GetById 47ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/scorm/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 540 lines | 388 code | 86 blank | 66 comment | 92 complexity | cc272dc53c0da12ae586b9eea471090d MD5 | raw file
  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.
 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
 17if (!defined('MOODLE_INTERNAL')) {
 18    die('Direct access to this script is forbidden.');    //  It must be included from a Moodle page.
 19}
 20
 21require_once($CFG->dirroot.'/course/moodleform_mod.php');
 22require_once($CFG->dirroot.'/mod/scorm/locallib.php');
 23
 24class mod_scorm_mod_form extends moodleform_mod {
 25
 26    public function definition() {
 27        global $CFG, $COURSE, $OUTPUT;
 28        $cfgscorm = get_config('scorm');
 29
 30        $mform = $this->_form;
 31
 32        if (!$CFG->slasharguments) {
 33            $mform->addElement('static', '', '', $OUTPUT->notification(get_string('slashargs', 'scorm'), 'notifyproblem'));
 34        }
 35
 36        $mform->addElement('header', 'general', get_string('general', 'form'));
 37
 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);
 44        }
 45        $mform->addRule('name', null, 'required', null, 'client');
 46        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 47
 48        // Summary.
 49        $this->add_intro_editor(true);
 50
 51        // Package.
 52        $mform->addElement('header', 'packagehdr', get_string('packagehdr', 'scorm'));
 53        $mform->setExpanded('packagehdr', true);
 54
 55        // Scorm types.
 56        $scormtypes = array(SCORM_TYPE_LOCAL => get_string('typelocal', 'scorm'));
 57
 58        if ($cfgscorm->allowtypeexternal) {
 59            $scormtypes[SCORM_TYPE_EXTERNAL] = get_string('typeexternal', 'scorm');
 60        }
 61
 62        if ($cfgscorm->allowtypelocalsync) {
 63            $scormtypes[SCORM_TYPE_LOCALSYNC] = get_string('typelocalsync', 'scorm');
 64        }
 65
 66        if ($cfgscorm->allowtypeexternalaicc) {
 67            $scormtypes[SCORM_TYPE_AICCURL] = get_string('typeaiccurl', 'scorm');
 68        }
 69
 70        // Reference.
 71        if (count($scormtypes) > 1) {
 72            $mform->addElement('select', 'scormtype', get_string('scormtype', 'scorm'), $scormtypes);
 73            $mform->setType('scormtype', PARAM_ALPHA);
 74            $mform->addHelpButton('scormtype', 'scormtype', 'scorm');
 75            $mform->addElement('text', 'packageurl', get_string('packageurl', 'scorm'), array('size'=>60));
 76            $mform->setType('packageurl', PARAM_RAW);
 77            $mform->addHelpButton('packageurl', 'packageurl', 'scorm');
 78            $mform->disabledIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
 79        } else {
 80            $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
 81            $mform->setType('scormtype', PARAM_ALPHA);
 82        }
 83
 84        // Update packages timing.
 85        $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
 86        $mform->setType('updatefreq', PARAM_INT);
 87        $mform->setDefault('updatefreq', $cfgscorm->updatefreq);
 88        $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
 89
 90        // New local package upload.
 91        $filemanageroptions = array();
 92        $filemanageroptions['accepted_types'] = array('.zip', '.xml');
 93        $filemanageroptions['maxbytes'] = 0;
 94        $filemanageroptions['maxfiles'] = 1;
 95        $filemanageroptions['subdirs'] = 0;
 96
 97        $mform->addElement('filemanager', 'packagefile', get_string('package', 'scorm'), null, $filemanageroptions);
 98        $mform->addHelpButton('packagefile', 'package', 'scorm');
 99        $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
100
101        // Display Settings.
102        $mform->addElement('header', 'displaysettings', get_string('appearance'));
103
104        // Framed / Popup Window.
105        $mform->addElement('select', 'popup', get_string('display', 'scorm'), scorm_get_popup_display_array());
106        $mform->setDefault('popup', $cfgscorm->popup);
107        $mform->setAdvanced('popup', $cfgscorm->popup_adv);
108
109        // Width.
110        $mform->addElement('text', 'width', get_string('width', 'scorm'), 'maxlength="5" size="5"');
111        $mform->setDefault('width', $cfgscorm->framewidth);
112        $mform->setType('width', PARAM_INT);
113        $mform->setAdvanced('width', $cfgscorm->framewidth_adv);
114        $mform->disabledIf('width', 'popup', 'eq', 0);
115
116        // Height.
117        $mform->addElement('text', 'height', get_string('height', 'scorm'), 'maxlength="5" size="5"');
118        $mform->setDefault('height', $cfgscorm->frameheight);
119        $mform->setType('height', PARAM_INT);
120        $mform->setAdvanced('height', $cfgscorm->frameheight_adv);
121        $mform->disabledIf('height', 'popup', 'eq', 0);
122
123        // Window Options.
124        $winoptgrp = array();
125        foreach (scorm_get_popup_options_array() as $key => $value) {
126            $winoptgrp[] = &$mform->createElement('checkbox', $key, '', get_string($key, 'scorm'));
127            $mform->setDefault($key, $value);
128        }
129        $mform->addGroup($winoptgrp, 'winoptgrp', get_string('options', 'scorm'), '<br />', false);
130        $mform->disabledIf('winoptgrp', 'popup', 'eq', 0);
131        $mform->setAdvanced('winoptgrp', $cfgscorm->winoptgrp_adv);
132
133        // Skip view page.
134        $skipviewoptions = scorm_get_skip_view_array();
135        if ($COURSE->format == 'singleactivity') { // Remove option that would cause a constant redirect.
136            unset($skipviewoptions[SCORM_SKIPVIEW_ALWAYS]);
137            if ($cfgscorm->skipview == SCORM_SKIPVIEW_ALWAYS) {
138                $cfgscorm->skipview = SCORM_SKIPVIEW_FIRST;
139            }
140        }
141        $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
142        $mform->addHelpButton('skipview', 'skipview', 'scorm');
143        $mform->setDefault('skipview', $cfgscorm->skipview);
144        $mform->setAdvanced('skipview', $cfgscorm->skipview_adv);
145
146        // Hide Browse.
147        $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
148        $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
149        $mform->setDefault('hidebrowse', $cfgscorm->hidebrowse);
150        $mform->setAdvanced('hidebrowse', $cfgscorm->hidebrowse_adv);
151
152        // Display course structure.
153        $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
154        $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
155        $mform->setDefault('displaycoursestructure', $cfgscorm->displaycoursestructure);
156        $mform->setAdvanced('displaycoursestructure', $cfgscorm->displaycoursestructure_adv);
157
158        // Toc display.
159        $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
160        $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
161        $mform->setDefault('hidetoc', $cfgscorm->hidetoc);
162        $mform->setAdvanced('hidetoc', $cfgscorm->hidetoc_adv);
163        $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
164
165        // Navigation panel display.
166        $mform->addElement('select', 'nav', get_string('nav', 'scorm'), scorm_get_navigation_display_array());
167        $mform->addHelpButton('nav', 'nav', 'scorm');
168        $mform->setDefault('nav', $cfgscorm->nav);
169        $mform->setAdvanced('nav', $cfgscorm->nav_adv);
170        $mform->disabledIf('nav', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
171
172        // Navigation panel position from left.
173        $mform->addElement('text', 'navpositionleft', get_string('fromleft', 'scorm'), 'maxlength="5" size="5"');
174        $mform->setDefault('navpositionleft', $cfgscorm->navpositionleft);
175        $mform->setType('navpositionleft', PARAM_INT);
176        $mform->setAdvanced('navpositionleft', $cfgscorm->navpositionleft_adv);
177        $mform->disabledIf('navpositionleft', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
178        $mform->disabledIf('navpositionleft', 'nav', 'noteq', SCORM_NAV_FLOATING);
179
180        // Navigation panel position from top.
181        $mform->addElement('text', 'navpositiontop', get_string('fromtop', 'scorm'), 'maxlength="5" size="5"');
182        $mform->setDefault('navpositiontop', $cfgscorm->navpositiontop);
183        $mform->setType('navpositiontop', PARAM_INT);
184        $mform->setAdvanced('navpositiontop', $cfgscorm->navpositiontop_adv);
185        $mform->disabledIf('navpositiontop', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
186        $mform->disabledIf('navpositiontop', 'nav', 'noteq', SCORM_NAV_FLOATING);
187
188        // Display attempt status.
189        $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'),
190                           scorm_get_attemptstatus_array());
191        $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
192        $mform->setDefault('displayattemptstatus', $cfgscorm->displayattemptstatus);
193        $mform->setAdvanced('displayattemptstatus', $cfgscorm->displayattemptstatus_adv);
194
195        // Availability.
196        $mform->addElement('header', 'availability', get_string('availability'));
197
198        $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
199        $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
200
201        // Grade Settings.
202        $mform->addElement('header', 'gradesettings', get_string('grade'));
203
204        // Grade Method.
205        $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
206        $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
207        $mform->setDefault('grademethod', $cfgscorm->grademethod);
208
209        // Maximum Grade.
210        for ($i=0; $i<=100; $i++) {
211            $grades[$i] = "$i";
212        }
213        $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
214        $mform->setDefault('maxgrade', $cfgscorm->maxgrade);
215        $mform->disabledIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
216
217        // Attempts management.
218        $mform->addElement('header', 'attemptsmanagementhdr', get_string('attemptsmanagement', 'scorm'));
219
220        // Max Attempts.
221        $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
222        $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
223        $mform->setDefault('maxattempt', $cfgscorm->maxattempt);
224
225        // What Grade.
226        $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'),  scorm_get_what_grade_array());
227        $mform->disabledIf('whatgrade', 'maxattempt', 'eq', 1);
228        $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
229        $mform->setDefault('whatgrade', $cfgscorm->whatgrade);
230
231        // Force new attempt.
232        $mform->addElement('selectyesno', 'forcenewattempt', get_string('forcenewattempt', 'scorm'));
233        $mform->addHelpButton('forcenewattempt', 'forcenewattempt', 'scorm');
234        $mform->setDefault('forcenewattempt', $cfgscorm->forcenewattempt);
235
236        // Last attempt lock - lock the enter button after the last available attempt has been made.
237        $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
238        $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
239        $mform->setDefault('lastattemptlock', $cfgscorm->lastattemptlock);
240
241        // Compatibility settings.
242        $mform->addElement('header', 'compatibilitysettingshdr', get_string('compatibilitysettings', 'scorm'));
243
244        // Force completed.
245        $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
246        $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
247        $mform->setDefault('forcecompleted', $cfgscorm->forcecompleted);
248
249        // Autocontinue.
250        $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
251        $mform->addHelpButton('auto', 'autocontinue', 'scorm');
252        $mform->setDefault('auto', $cfgscorm->auto);
253
254        // Hidden Settings.
255        $mform->addElement('hidden', 'datadir', null);
256        $mform->setType('datadir', PARAM_RAW);
257        $mform->addElement('hidden', 'pkgtype', null);
258        $mform->setType('pkgtype', PARAM_RAW);
259        $mform->addElement('hidden', 'launch', null);
260        $mform->setType('launch', PARAM_RAW);
261        $mform->addElement('hidden', 'redirect', null);
262        $mform->setType('redirect', PARAM_RAW);
263        $mform->addElement('hidden', 'redirecturl', null);
264        $mform->setType('redirecturl', PARAM_RAW);
265
266        $this->standard_coursemodule_elements();
267
268        // Buttons.
269        $this->add_action_buttons();
270    }
271
272    public function data_preprocessing(&$defaultvalues) {
273        global $COURSE;
274
275        if (isset($defaultvalues['popup']) && ($defaultvalues['popup'] == 1) && isset($defaultvalues['options'])) {
276            if (!empty($defaultvalues['options'])) {
277                $options = explode(',', $defaultvalues['options']);
278                foreach ($options as $option) {
279                    list($element, $value) = explode('=', $option);
280                    $element = trim($element);
281                    $defaultvalues[$element] = trim($value);
282                }
283            }
284        }
285        if (isset($defaultvalues['grademethod'])) {
286            $defaultvalues['grademethod'] = intval($defaultvalues['grademethod']);
287        }
288        if (isset($defaultvalues['width']) && (strpos($defaultvalues['width'], '%') === false)
289                                           && ($defaultvalues['width'] <= 100)) {
290            $defaultvalues['width'] .= '%';
291        }
292        if (isset($defaultvalues['width']) && (strpos($defaultvalues['height'], '%') === false)
293                                           && ($defaultvalues['height'] <= 100)) {
294            $defaultvalues['height'] .= '%';
295        }
296        $scorms = get_all_instances_in_course('scorm', $COURSE);
297        $coursescorm = current($scorms);
298
299        $draftitemid = file_get_submitted_draft_itemid('packagefile');
300        file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0,
301            array('subdirs' => 0, 'maxfiles' => 1));
302        $defaultvalues['packagefile'] = $draftitemid;
303
304        if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) {
305            $defaultvalues['redirect'] = 'yes';
306            $defaultvalues['redirecturl'] = '../course/view.php?id='.$defaultvalues['course'];
307        } else {
308            $defaultvalues['redirect'] = 'no';
309            $defaultvalues['redirecturl'] = '../mod/scorm/view.php?id='.$defaultvalues['coursemodule'];
310        }
311        if (isset($defaultvalues['version'])) {
312            $defaultvalues['pkgtype'] = (substr($defaultvalues['version'], 0, 5) == 'SCORM') ? 'scorm':'aicc';
313        }
314        if (isset($defaultvalues['instance'])) {
315            $defaultvalues['datadir'] = $defaultvalues['instance'];
316        }
317        if (empty($defaultvalues['timeopen'])) {
318            $defaultvalues['timeopen'] = 0;
319        }
320        if (empty($defaultvalues['timeclose'])) {
321            $defaultvalues['timeclose'] = 0;
322        }
323
324        // Set some completion default data.
325        if (!empty($defaultvalues['completionstatusrequired']) && !is_array($defaultvalues['completionstatusrequired'])) {
326            // Unpack values.
327            $cvalues = array();
328            foreach (scorm_status_options() as $key => $value) {
329                if (($defaultvalues['completionstatusrequired'] & $key) == $key) {
330                    $cvalues[$key] = 1;
331                }
332            }
333
334            $defaultvalues['completionstatusrequired'] = $cvalues;
335        }
336
337        if (!isset($defaultvalues['completionscorerequired']) || !strlen($defaultvalues['completionscorerequired'])) {
338            $defaultvalues['completionscoredisabled'] = 1;
339        }
340
341    }
342
343    public function validation($data, $files) {
344        global $CFG, $USER;
345        $errors = parent::validation($data, $files);
346
347        $type = $data['scormtype'];
348
349        if ($type === SCORM_TYPE_LOCAL) {
350            if (empty($data['packagefile'])) {
351                $errors['packagefile'] = get_string('required');
352
353            } else {
354                $draftitemid = file_get_submitted_draft_itemid('packagefile');
355
356                file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'packagefilecheck', null,
357                    array('subdirs' => 0, 'maxfiles' => 1));
358
359                // Get file from users draft area.
360                $usercontext = context_user::instance($USER->id);
361                $fs = get_file_storage();
362                $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
363
364                if (count($files)<1) {
365                    $errors['packagefile'] = get_string('required');
366                    return $errors;
367                }
368                $file = reset($files);
369                if (!$file->is_external_file() && !empty($data['updatefreq'])) {
370                    // Make sure updatefreq is not set if using normal local file.
371                    $errors['updatefreq'] = get_string('updatefreq_error', 'mod_scorm');
372                }
373                if (strtolower($file->get_filename()) == 'imsmanifest.xml') {
374                    if (!$file->is_external_file()) {
375                        $errors['packagefile'] = get_string('aliasonly', 'mod_scorm');
376                    } else {
377                        $repository = repository::get_repository_by_id($file->get_repository_id(), context_system::instance());
378                        if (!$repository->supports_relative_file()) {
379                            $errors['packagefile'] = get_string('repositorynotsupported', 'mod_scorm');
380                        }
381                    }
382                } else if (strtolower(substr($file->get_filename(), -3)) == 'xml') {
383                    $errors['packagefile'] = get_string('invalidmanifestname', 'mod_scorm');
384                } else {
385                    // Validate this SCORM package.
386                    $errors = array_merge($errors, scorm_validate_package($file));
387                }
388            }
389
390        } else if ($type === SCORM_TYPE_EXTERNAL) {
391            $reference = $data['packageurl'];
392            // Syntax check.
393            if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
394                $errors['packageurl'] = get_string('invalidurl', 'scorm');
395            } else {
396                // Availability check.
397                $result = scorm_check_url($reference);
398                if (is_string($result)) {
399                    $errors['packageurl'] = $result;
400                }
401            }
402
403        } else if ($type === 'packageurl') {
404            $reference = $data['reference'];
405            // Syntax check.
406            if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
407                $errors['packageurl'] = get_string('invalidurl', 'scorm');
408            } else {
409                // Availability check.
410                $result = scorm_check_url($reference);
411                if (is_string($result)) {
412                    $errors['packageurl'] = $result;
413                }
414            }
415
416        } else if ($type === SCORM_TYPE_AICCURL) {
417            $reference = $data['packageurl'];
418            // Syntax check.
419            if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
420                $errors['packageurl'] = get_string('invalidurl', 'scorm');
421            } else {
422                // Availability check.
423                $result = scorm_check_url($reference);
424                if (is_string($result)) {
425                    $errors['packageurl'] = $result;
426                }
427            }
428
429        }
430
431        return $errors;
432    }
433
434    // Need to translate the "options" and "reference" field.
435    public function set_data($defaultvalues) {
436        $defaultvalues = (array)$defaultvalues;
437
438        if (isset($defaultvalues['scormtype']) and isset($defaultvalues['reference'])) {
439            switch ($defaultvalues['scormtype']) {
440                case SCORM_TYPE_LOCALSYNC :
441                case SCORM_TYPE_EXTERNAL:
442                case SCORM_TYPE_AICCURL:
443                    $defaultvalues['packageurl'] = $defaultvalues['reference'];
444            }
445        }
446        unset($defaultvalues['reference']);
447
448        if (!empty($defaultvalues['options'])) {
449            $options = explode(',', $defaultvalues['options']);
450            foreach ($options as $option) {
451                $opt = explode('=', $option);
452                if (isset($opt[1])) {
453                    $defaultvalues[$opt[0]] = $opt[1];
454                }
455            }
456        }
457
458        $this->data_preprocessing($defaultvalues);
459        parent::set_data($defaultvalues);
460    }
461
462    public function add_completion_rules() {
463        $mform =& $this->_form;
464        $items = array();
465
466        // Require score.
467        $group = array();
468        $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
469        $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
470        $mform->setType('completionscorerequired', PARAM_INT);
471        $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
472        $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
473        $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
474        $mform->setDefault('completionscorerequired', 0);
475
476        $items[] = 'completionscoregroup';
477
478
479        // Require status.
480        $first = true;
481        $firstkey = null;
482        foreach (scorm_status_options(true) as $key => $value) {
483            $name = null;
484            $key = 'completionstatusrequired['.$key.']';
485            if ($first) {
486                $name = get_string('completionstatusrequired', 'scorm');
487                $first = false;
488                $firstkey = $key;
489            }
490            $mform->addElement('checkbox', $key, $name, $value);
491            $mform->setType($key, PARAM_BOOL);
492            $items[] = $key;
493        }
494        $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
495
496        return $items;
497    }
498
499    function completion_rule_enabled($data) {
500        $status = !empty($data['completionstatusrequired']);
501        $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
502
503        return $status || $score;
504    }
505
506    function get_data($slashed = true) {
507        $data = parent::get_data($slashed);
508
509        if (!$data) {
510            return false;
511        }
512
513        // Convert completionstatusrequired to a proper integer, if any.
514        $total = 0;
515        if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
516            foreach (array_keys($data->completionstatusrequired) as $state) {
517                $total |= $state;
518            }
519            $data->completionstatusrequired = $total;
520        }
521
522        if (!empty($data->completionunlocked)) {
523            // Turn off completion settings if the checkboxes aren't ticked.
524            $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
525
526            if (isset($data->completionstatusrequired) && $autocompletion) {
527                // Do nothing: completionstatusrequired has been already converted
528                //             into a correct integer representation.
529            } else {
530                $data->completionstatusrequired = null;
531            }
532
533            if (!empty($data->completionscoredisabled) || !$autocompletion) {
534                $data->completionscorerequired = null;
535            }
536        }
537
538        return $data;
539    }
540}