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