PageRenderTime 60ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/scorm/mod_form.php

http://github.com/moodle/moodle
PHP | 580 lines | 416 code | 85 blank | 79 comment | 97 complexity | 9545bfa315f4b6d56023cf0acf46b971 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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->standard_intro_elements();
  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->hideIf('packageurl', 'scormtype', 'eq', SCORM_TYPE_LOCAL);
  64. } else {
  65. $mform->addElement('hidden', 'scormtype', SCORM_TYPE_LOCAL);
  66. $mform->setType('scormtype', PARAM_ALPHA);
  67. }
  68. // New local package upload.
  69. $filemanageroptions = array();
  70. $filemanageroptions['accepted_types'] = array('.zip', '.xml');
  71. $filemanageroptions['maxbytes'] = 0;
  72. $filemanageroptions['maxfiles'] = 1;
  73. $filemanageroptions['subdirs'] = 0;
  74. $mform->addElement('filemanager', 'packagefile', get_string('package', 'scorm'), null, $filemanageroptions);
  75. $mform->addHelpButton('packagefile', 'package', 'scorm');
  76. $mform->hideIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
  77. // Update packages timing.
  78. $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array());
  79. $mform->setType('updatefreq', PARAM_INT);
  80. $mform->setDefault('updatefreq', $cfgscorm->updatefreq);
  81. $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm');
  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->hideIf('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->hideIf('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->hideIf('winoptgrp', 'popup', 'eq', 0);
  108. $mform->setAdvanced('winoptgrp', $cfgscorm->winoptgrp_adv);
  109. // Display activity name.
  110. $mform->addElement('advcheckbox', 'displayactivityname', get_string('displayactivityname', 'scorm'));
  111. $mform->addHelpButton('displayactivityname', 'displayactivityname', 'scorm');
  112. $mform->setDefault('displayactivityname', $cfgscorm->displayactivityname);
  113. // Skip view page.
  114. $skipviewoptions = scorm_get_skip_view_array();
  115. $mform->addElement('select', 'skipview', get_string('skipview', 'scorm'), $skipviewoptions);
  116. $mform->addHelpButton('skipview', 'skipview', 'scorm');
  117. $mform->setDefault('skipview', $cfgscorm->skipview);
  118. $mform->setAdvanced('skipview', $cfgscorm->skipview_adv);
  119. // Hide Browse.
  120. $mform->addElement('selectyesno', 'hidebrowse', get_string('hidebrowse', 'scorm'));
  121. $mform->addHelpButton('hidebrowse', 'hidebrowse', 'scorm');
  122. $mform->setDefault('hidebrowse', $cfgscorm->hidebrowse);
  123. $mform->setAdvanced('hidebrowse', $cfgscorm->hidebrowse_adv);
  124. // Display course structure.
  125. $mform->addElement('selectyesno', 'displaycoursestructure', get_string('displaycoursestructure', 'scorm'));
  126. $mform->addHelpButton('displaycoursestructure', 'displaycoursestructure', 'scorm');
  127. $mform->setDefault('displaycoursestructure', $cfgscorm->displaycoursestructure);
  128. $mform->setAdvanced('displaycoursestructure', $cfgscorm->displaycoursestructure_adv);
  129. // Toc display.
  130. $mform->addElement('select', 'hidetoc', get_string('hidetoc', 'scorm'), scorm_get_hidetoc_array());
  131. $mform->addHelpButton('hidetoc', 'hidetoc', 'scorm');
  132. $mform->setDefault('hidetoc', $cfgscorm->hidetoc);
  133. $mform->setAdvanced('hidetoc', $cfgscorm->hidetoc_adv);
  134. $mform->disabledIf('hidetoc', 'scormtype', 'eq', SCORM_TYPE_AICCURL);
  135. // Navigation panel display.
  136. $mform->addElement('select', 'nav', get_string('nav', 'scorm'), scorm_get_navigation_display_array());
  137. $mform->addHelpButton('nav', 'nav', 'scorm');
  138. $mform->setDefault('nav', $cfgscorm->nav);
  139. $mform->setAdvanced('nav', $cfgscorm->nav_adv);
  140. $mform->hideIf('nav', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
  141. // Navigation panel position from left.
  142. $mform->addElement('text', 'navpositionleft', get_string('fromleft', 'scorm'), 'maxlength="5" size="5"');
  143. $mform->setDefault('navpositionleft', $cfgscorm->navpositionleft);
  144. $mform->setType('navpositionleft', PARAM_INT);
  145. $mform->setAdvanced('navpositionleft', $cfgscorm->navpositionleft_adv);
  146. $mform->hideIf('navpositionleft', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
  147. $mform->hideIf('navpositionleft', 'nav', 'noteq', SCORM_NAV_FLOATING);
  148. // Navigation panel position from top.
  149. $mform->addElement('text', 'navpositiontop', get_string('fromtop', 'scorm'), 'maxlength="5" size="5"');
  150. $mform->setDefault('navpositiontop', $cfgscorm->navpositiontop);
  151. $mform->setType('navpositiontop', PARAM_INT);
  152. $mform->setAdvanced('navpositiontop', $cfgscorm->navpositiontop_adv);
  153. $mform->hideIf('navpositiontop', 'hidetoc', 'noteq', SCORM_TOC_SIDE);
  154. $mform->hideIf('navpositiontop', 'nav', 'noteq', SCORM_NAV_FLOATING);
  155. // Display attempt status.
  156. $mform->addElement('select', 'displayattemptstatus', get_string('displayattemptstatus', 'scorm'),
  157. scorm_get_attemptstatus_array());
  158. $mform->addHelpButton('displayattemptstatus', 'displayattemptstatus', 'scorm');
  159. $mform->setDefault('displayattemptstatus', $cfgscorm->displayattemptstatus);
  160. $mform->setAdvanced('displayattemptstatus', $cfgscorm->displayattemptstatus_adv);
  161. // Availability.
  162. $mform->addElement('header', 'availability', get_string('availability'));
  163. $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"), array('optional' => true));
  164. $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"), array('optional' => true));
  165. // Grade Settings.
  166. $mform->addElement('header', 'gradesettings', get_string('grade'));
  167. // Grade Method.
  168. $mform->addElement('select', 'grademethod', get_string('grademethod', 'scorm'), scorm_get_grade_method_array());
  169. $mform->addHelpButton('grademethod', 'grademethod', 'scorm');
  170. $mform->setDefault('grademethod', $cfgscorm->grademethod);
  171. // Maximum Grade.
  172. for ($i = 0; $i <= 100; $i++) {
  173. $grades[$i] = "$i";
  174. }
  175. $mform->addElement('select', 'maxgrade', get_string('maximumgrade'), $grades);
  176. $mform->setDefault('maxgrade', $cfgscorm->maxgrade);
  177. $mform->hideIf('maxgrade', 'grademethod', 'eq', GRADESCOES);
  178. // Attempts management.
  179. $mform->addElement('header', 'attemptsmanagementhdr', get_string('attemptsmanagement', 'scorm'));
  180. // Max Attempts.
  181. $mform->addElement('select', 'maxattempt', get_string('maximumattempts', 'scorm'), scorm_get_attempts_array());
  182. $mform->addHelpButton('maxattempt', 'maximumattempts', 'scorm');
  183. $mform->setDefault('maxattempt', $cfgscorm->maxattempt);
  184. // What Grade.
  185. $mform->addElement('select', 'whatgrade', get_string('whatgrade', 'scorm'), scorm_get_what_grade_array());
  186. $mform->hideIf('whatgrade', 'maxattempt', 'eq', 1);
  187. $mform->addHelpButton('whatgrade', 'whatgrade', 'scorm');
  188. $mform->setDefault('whatgrade', $cfgscorm->whatgrade);
  189. // Force new attempt.
  190. $newattemptselect = scorm_get_forceattempt_array();
  191. $mform->addElement('select', 'forcenewattempt', get_string('forcenewattempts', 'scorm'), $newattemptselect);
  192. $mform->addHelpButton('forcenewattempt', 'forcenewattempts', 'scorm');
  193. $mform->setDefault('forcenewattempt', $cfgscorm->forcenewattempt);
  194. // Last attempt lock - lock the enter button after the last available attempt has been made.
  195. $mform->addElement('selectyesno', 'lastattemptlock', get_string('lastattemptlock', 'scorm'));
  196. $mform->addHelpButton('lastattemptlock', 'lastattemptlock', 'scorm');
  197. $mform->setDefault('lastattemptlock', $cfgscorm->lastattemptlock);
  198. // Compatibility settings.
  199. $mform->addElement('header', 'compatibilitysettingshdr', get_string('compatibilitysettings', 'scorm'));
  200. // Force completed.
  201. $mform->addElement('selectyesno', 'forcecompleted', get_string('forcecompleted', 'scorm'));
  202. $mform->addHelpButton('forcecompleted', 'forcecompleted', 'scorm');
  203. $mform->setDefault('forcecompleted', $cfgscorm->forcecompleted);
  204. // Autocontinue.
  205. $mform->addElement('selectyesno', 'auto', get_string('autocontinue', 'scorm'));
  206. $mform->addHelpButton('auto', 'autocontinue', 'scorm');
  207. $mform->setDefault('auto', $cfgscorm->auto);
  208. // Autocommit.
  209. $mform->addElement('selectyesno', 'autocommit', get_string('autocommit', 'scorm'));
  210. $mform->addHelpButton('autocommit', 'autocommit', 'scorm');
  211. $mform->setDefault('autocommit', $cfgscorm->autocommit);
  212. // Mastery score overrides status.
  213. $mform->addElement('selectyesno', 'masteryoverride', get_string('masteryoverride', 'scorm'));
  214. $mform->addHelpButton('masteryoverride', 'masteryoverride', 'scorm');
  215. $mform->setDefault('masteryoverride', $cfgscorm->masteryoverride);
  216. // Hidden Settings.
  217. $mform->addElement('hidden', 'datadir', null);
  218. $mform->setType('datadir', PARAM_RAW);
  219. $mform->addElement('hidden', 'pkgtype', null);
  220. $mform->setType('pkgtype', PARAM_RAW);
  221. $mform->addElement('hidden', 'launch', null);
  222. $mform->setType('launch', PARAM_RAW);
  223. $mform->addElement('hidden', 'redirect', null);
  224. $mform->setType('redirect', PARAM_RAW);
  225. $mform->addElement('hidden', 'redirecturl', null);
  226. $mform->setType('redirecturl', PARAM_RAW);
  227. $this->standard_coursemodule_elements();
  228. // Buttons.
  229. $this->add_action_buttons();
  230. }
  231. public function data_preprocessing(&$defaultvalues) {
  232. global $COURSE;
  233. if (isset($defaultvalues['popup']) && ($defaultvalues['popup'] == 1) && isset($defaultvalues['options'])) {
  234. if (!empty($defaultvalues['options'])) {
  235. $options = explode(',', $defaultvalues['options']);
  236. foreach ($options as $option) {
  237. list($element, $value) = explode('=', $option);
  238. $element = trim($element);
  239. $defaultvalues[$element] = trim($value);
  240. }
  241. }
  242. }
  243. if (isset($defaultvalues['grademethod'])) {
  244. $defaultvalues['grademethod'] = intval($defaultvalues['grademethod']);
  245. }
  246. if (isset($defaultvalues['width']) && (strpos($defaultvalues['width'], '%') === false)
  247. && ($defaultvalues['width'] <= 100)) {
  248. $defaultvalues['width'] .= '%';
  249. }
  250. if (isset($defaultvalues['height']) && (strpos($defaultvalues['height'], '%') === false)
  251. && ($defaultvalues['height'] <= 100)) {
  252. $defaultvalues['height'] .= '%';
  253. }
  254. $scorms = get_all_instances_in_course('scorm', $COURSE);
  255. $coursescorm = current($scorms);
  256. $draftitemid = file_get_submitted_draft_itemid('packagefile');
  257. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0,
  258. array('subdirs' => 0, 'maxfiles' => 1));
  259. $defaultvalues['packagefile'] = $draftitemid;
  260. if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) {
  261. $defaultvalues['redirect'] = 'yes';
  262. $defaultvalues['redirecturl'] = '../course/view.php?id='.$defaultvalues['course'];
  263. } else {
  264. $defaultvalues['redirect'] = 'no';
  265. $defaultvalues['redirecturl'] = '../mod/scorm/view.php?id='.$defaultvalues['coursemodule'];
  266. }
  267. if (isset($defaultvalues['version'])) {
  268. $defaultvalues['pkgtype'] = (substr($defaultvalues['version'], 0, 5) == 'SCORM') ? 'scorm' : 'aicc';
  269. }
  270. if (isset($defaultvalues['instance'])) {
  271. $defaultvalues['datadir'] = $defaultvalues['instance'];
  272. }
  273. if (empty($defaultvalues['timeopen'])) {
  274. $defaultvalues['timeopen'] = 0;
  275. }
  276. if (empty($defaultvalues['timeclose'])) {
  277. $defaultvalues['timeclose'] = 0;
  278. }
  279. // Set some completion default data.
  280. $cvalues = array();
  281. if (empty($this->_instance)) {
  282. // When in add mode, set a default completion rule that requires the SCORM's status be set to "Completed".
  283. $cvalues[4] = 1;
  284. } else if (!empty($defaultvalues['completionstatusrequired']) && !is_array($defaultvalues['completionstatusrequired'])) {
  285. // Unpack values.
  286. foreach (scorm_status_options() as $key => $value) {
  287. if (($defaultvalues['completionstatusrequired'] & $key) == $key) {
  288. $cvalues[$key] = 1;
  289. }
  290. }
  291. }
  292. if (!empty($cvalues)) {
  293. $defaultvalues['completionstatusrequired'] = $cvalues;
  294. }
  295. if (!isset($defaultvalues['completionscorerequired']) || !strlen($defaultvalues['completionscorerequired'])) {
  296. $defaultvalues['completionscoredisabled'] = 1;
  297. }
  298. }
  299. public function validation($data, $files) {
  300. global $CFG, $USER;
  301. $errors = parent::validation($data, $files);
  302. $type = $data['scormtype'];
  303. if ($type === SCORM_TYPE_LOCAL) {
  304. if (empty($data['packagefile'])) {
  305. $errors['packagefile'] = get_string('required');
  306. } else {
  307. $draftitemid = file_get_submitted_draft_itemid('packagefile');
  308. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'packagefilecheck', null,
  309. array('subdirs' => 0, 'maxfiles' => 1));
  310. // Get file from users draft area.
  311. $usercontext = context_user::instance($USER->id);
  312. $fs = get_file_storage();
  313. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
  314. if (count($files) < 1) {
  315. $errors['packagefile'] = get_string('required');
  316. return $errors;
  317. }
  318. $file = reset($files);
  319. if (!$file->is_external_file() && !empty($data['updatefreq'])) {
  320. // Make sure updatefreq is not set if using normal local file.
  321. $errors['updatefreq'] = get_string('updatefreq_error', 'mod_scorm');
  322. }
  323. if (strtolower($file->get_filename()) == 'imsmanifest.xml') {
  324. if (!$file->is_external_file()) {
  325. $errors['packagefile'] = get_string('aliasonly', 'mod_scorm');
  326. } else {
  327. $repository = repository::get_repository_by_id($file->get_repository_id(), context_system::instance());
  328. if (!$repository->supports_relative_file()) {
  329. $errors['packagefile'] = get_string('repositorynotsupported', 'mod_scorm');
  330. }
  331. }
  332. } else if (strtolower(substr($file->get_filename(), -3)) == 'xml') {
  333. $errors['packagefile'] = get_string('invalidmanifestname', 'mod_scorm');
  334. } else {
  335. // Validate this SCORM package.
  336. $errors = array_merge($errors, scorm_validate_package($file));
  337. }
  338. }
  339. } else if ($type === SCORM_TYPE_EXTERNAL) {
  340. $reference = $data['packageurl'];
  341. // Syntax check.
  342. if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/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 === 'packageurl') {
  352. $reference = $data['reference'];
  353. // Syntax check.
  354. if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $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. } else if ($type === SCORM_TYPE_AICCURL) {
  364. $reference = $data['packageurl'];
  365. // Syntax check.
  366. if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
  367. $errors['packageurl'] = get_string('invalidurl', 'scorm');
  368. } else {
  369. // Availability check.
  370. $result = scorm_check_url($reference);
  371. if (is_string($result)) {
  372. $errors['packageurl'] = $result;
  373. }
  374. }
  375. }
  376. // Validate availability dates.
  377. if ($data['timeopen'] && $data['timeclose']) {
  378. if ($data['timeopen'] > $data['timeclose']) {
  379. $errors['timeclose'] = get_string('closebeforeopen', 'scorm');
  380. }
  381. }
  382. if (!empty($data['completionstatusallscos'])) {
  383. $requirestatus = false;
  384. foreach (scorm_status_options(true) as $key => $value) {
  385. if (!empty($data['completionstatusrequired'][$key])) {
  386. $requirestatus = true;
  387. }
  388. }
  389. if (!$requirestatus) {
  390. $errors['completionstatusallscos'] = get_string('youmustselectastatus', 'scorm');
  391. }
  392. }
  393. return $errors;
  394. }
  395. // Need to translate the "options" and "reference" field.
  396. public function set_data($defaultvalues) {
  397. $defaultvalues = (array)$defaultvalues;
  398. if (isset($defaultvalues['scormtype']) and isset($defaultvalues['reference'])) {
  399. switch ($defaultvalues['scormtype']) {
  400. case SCORM_TYPE_LOCALSYNC :
  401. case SCORM_TYPE_EXTERNAL:
  402. case SCORM_TYPE_AICCURL:
  403. $defaultvalues['packageurl'] = $defaultvalues['reference'];
  404. }
  405. }
  406. unset($defaultvalues['reference']);
  407. if (!empty($defaultvalues['options'])) {
  408. $options = explode(',', $defaultvalues['options']);
  409. foreach ($options as $option) {
  410. $opt = explode('=', $option);
  411. if (isset($opt[1])) {
  412. $defaultvalues[$opt[0]] = $opt[1];
  413. }
  414. }
  415. }
  416. parent::set_data($defaultvalues);
  417. }
  418. public function add_completion_rules() {
  419. $mform =& $this->_form;
  420. $items = array();
  421. // Require score.
  422. $group = array();
  423. $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
  424. $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
  425. $mform->setType('completionscorerequired', PARAM_INT);
  426. $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
  427. $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
  428. $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
  429. $mform->setDefault('completionscorerequired', 0);
  430. $items[] = 'completionscoregroup';
  431. // Require status.
  432. $first = true;
  433. $firstkey = null;
  434. foreach (scorm_status_options(true) as $key => $value) {
  435. $name = null;
  436. $key = 'completionstatusrequired['.$key.']';
  437. if ($first) {
  438. $name = get_string('completionstatusrequired', 'scorm');
  439. $first = false;
  440. $firstkey = $key;
  441. }
  442. $mform->addElement('checkbox', $key, $name, $value);
  443. $mform->setType($key, PARAM_BOOL);
  444. $items[] = $key;
  445. }
  446. $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
  447. $mform->addElement('checkbox', 'completionstatusallscos', get_string('completionstatusallscos', 'scorm'));
  448. $mform->setType('completionstatusallscos', PARAM_BOOL);
  449. $mform->addHelpButton('completionstatusallscos', 'completionstatusallscos', 'scorm');
  450. $mform->setDefault('completionstatusallscos', 0);
  451. $items[] = 'completionstatusallscos';
  452. return $items;
  453. }
  454. public function completion_rule_enabled($data) {
  455. $status = !empty($data['completionstatusrequired']);
  456. $score = empty($data['completionscoredisabled']) && strlen($data['completionscorerequired']);
  457. return $status || $score;
  458. }
  459. /**
  460. * Allows module to modify the data returned by form get_data().
  461. * This method is also called in the bulk activity completion form.
  462. *
  463. * Only available on moodleform_mod.
  464. *
  465. * @param stdClass $data the form data to be modified.
  466. */
  467. public function data_postprocessing($data) {
  468. parent::data_postprocessing($data);
  469. // Convert completionstatusrequired to a proper integer, if any.
  470. $total = 0;
  471. if (isset($data->completionstatusrequired) && is_array($data->completionstatusrequired)) {
  472. foreach ($data->completionstatusrequired as $state => $value) {
  473. if ($value) {
  474. $total |= $state;
  475. }
  476. }
  477. if (!$total) {
  478. $total = null;
  479. }
  480. $data->completionstatusrequired = $total;
  481. }
  482. if (!empty($data->completionunlocked)) {
  483. // Turn off completion settings if the checkboxes aren't ticked.
  484. $autocompletion = isset($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
  485. if (!(isset($data->completionstatusrequired) && $autocompletion)) {
  486. $data->completionstatusrequired = null;
  487. }
  488. // Else do nothing: completionstatusrequired has been already converted
  489. // into a correct integer representation.
  490. if (!empty($data->completionscoredisabled) || !$autocompletion) {
  491. $data->completionscorerequired = null;
  492. }
  493. }
  494. }
  495. }