PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mod_form.php

https://github.com/KieranRBriggs/moodle-mod_hotpot
PHP | 752 lines | 490 code | 84 blank | 178 comment | 65 complexity | 55771f30b58b158ae6f24f6c6fcf990d 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. /**
  17. * The main hotpot configuration form
  18. *
  19. * It uses the standard core Moodle formslib. For more info about them, please
  20. * visit: http://docs.moodle.org/en/Development:lib/formslib.php
  21. *
  22. * @package mod-hotpot
  23. * @copyright 2010 Gordon Bateson <gordon.bateson@gmail.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  28. require_once(dirname(__FILE__) . '/locallib.php');
  29. require_once($CFG->libdir . '/filelib.php');
  30. /**
  31. * mod_hotpot_mod_form
  32. *
  33. * @copyright 2010 Gordon Bateson
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. * @since Moodle 2.0
  36. */
  37. class mod_hotpot_mod_form extends moodleform_mod {
  38. /**
  39. * Detects if we are adding a new HotPot activity
  40. * as opposed to updating an existing one
  41. *
  42. * Note: we could use any of the following to detect add:
  43. * - empty($this->_instance | _cm)
  44. * - empty($this->current->add | id | coursemodule | instance)
  45. *
  46. * @return bool True if we are adding an new activity instance, false otherwise
  47. */
  48. public function is_add() {
  49. if (empty($this->current->instance)) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * Detects if we are updating a new HotPot activity
  57. * as opposed to adding an new one
  58. *
  59. * @return bool True if we are adding an new activity instance, false otherwise
  60. */
  61. public function is_update() {
  62. if (empty($this->current->instance)) {
  63. return false;
  64. } else {
  65. return true;
  66. }
  67. }
  68. /**
  69. * Detects if the current activity instance has a grade item in the Moodle gradebook
  70. *
  71. * Note: could make this general purpose, if we use $this->_cm->modname for 'itemmodule'
  72. *
  73. * @return bool True if the current activity has a grade item, false otherwise
  74. */
  75. protected function has_grade_item() {
  76. global $DB;
  77. if ($this->is_add()) {
  78. return false;
  79. } else {
  80. return $DB->record_exists('grade_items', array('itemtype'=>'mod', 'itemmodule'=>'hotpot', 'iteminstance'=>$this->current->instance));
  81. }
  82. }
  83. /**
  84. * return a field value from the original record
  85. * this function is useful to see if a value has changed
  86. *
  87. * @return bool the field value if it exists, false otherwise
  88. */
  89. public function get_original_value($fieldname, $default) {
  90. if (isset($this->current) && isset($this->current->$fieldname)) {
  91. return $this->current->$fieldname;
  92. } else {
  93. return $default;
  94. }
  95. }
  96. /**
  97. * Defines the hotpot instance configuration form
  98. *
  99. * @return void
  100. */
  101. function definition() {
  102. global $CFG;
  103. $hotpotconfig = get_config('hotpot');
  104. $mform = $this->_form;
  105. // General --------------------------------------------------------------------
  106. $mform->addElement('header', 'general', get_string('general', 'form'));
  107. //-----------------------------------------------------------------------------
  108. // Hotpot name
  109. if ($this->is_add()) {
  110. $elements = array(
  111. $mform->createElement('select', 'namesource', '', hotpot::available_namesources_list()),
  112. $mform->createElement('text', 'name', '', array('size' => '40'))
  113. );
  114. $mform->addGroup($elements, 'name_elements', get_string('name'), array(' '), false);
  115. $mform->disabledIf('name_elements', 'namesource', 'ne', hotpot::TEXTSOURCE_SPECIFIC);
  116. $mform->setDefault('namesource', get_user_preferences('hotpot_namesource', hotpot::TEXTSOURCE_FILE));
  117. $mform->addHelpButton('name_elements', 'nameadd', 'hotpot');
  118. } else {
  119. $mform->addElement('text', 'name', get_string('name'), array('size' => '40'));
  120. $mform->addElement('hidden', 'namesource', hotpot::TEXTSOURCE_SPECIFIC);
  121. $mform->addHelpButton('name', 'nameedit', 'hotpot');
  122. $mform->addRule('name', null, 'required', null, 'client');
  123. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  124. }
  125. $mform->setType('namesource', PARAM_INT);
  126. if (empty($CFG->formatstringstriptags)) {
  127. $mform->setType('name', PARAM_CLEAN);
  128. } else {
  129. $mform->setType('name', PARAM_TEXT);
  130. }
  131. // Reference
  132. // $mform->addElement('filepicker', 'sourceitemid', get_string('sourcefile', 'hotpot'));
  133. // $mform->addRule('sourceitemid', null, 'required', null, 'client');
  134. // $mform->addHelpButton('sourceitemid', 'sourcefile', 'hotpot');
  135. $options = array('subdirs' => 1, 'maxbytes' => 0, 'maxfiles' => -1, 'mainfile' => true, 'accepted_types' => '*');
  136. $mform->addElement('filemanager', 'sourceitemid', get_string('sourcefile', 'hotpot'), null, $options);
  137. $mform->addRule('sourceitemid', null, 'required', null, 'client');
  138. $mform->addHelpButton('sourceitemid', 'sourcefile', 'hotpot');
  139. // legacy field from Moodle 1.9 - it will probably be removed someday
  140. $mform->addElement('hidden', 'sourcelocation', isset($this->current->sourcelocation) ? $this->current->sourcelocation : 0);
  141. // Add quiz chain (this setting is not implemented in Moodle 2.0)
  142. $mform->addElement('hidden', 'quizchain', 0);
  143. //if ($this->is_add()) {
  144. // $mform->addElement('selectyesno', 'quizchain', get_string('addquizchain', 'hotpot'));
  145. // $mform->setDefault('quizchain', get_user_preferences('hotpot_add_quizchain', 0));
  146. // $mform->addHelpButton('quizchain', 'addquizchain', 'hotpot');
  147. // $mform->setAdvanced('quizchain');
  148. //} else {
  149. // $mform->addElement('hidden', 'quizchain', 0);
  150. //}
  151. // Entry page -----------------------------------------------------------------
  152. $mform->addElement('header', 'entrypagehdr', get_string('entrypagehdr', 'hotpot'));
  153. //-----------------------------------------------------------------------------
  154. // Entry page text editor
  155. $this->add_hotpot_text_editor('entry');
  156. // Entry page options
  157. $elements = array(
  158. $mform->createElement('checkbox', 'entry_title', '', get_string('title', 'hotpot')),
  159. $mform->createElement('checkbox', 'entry_grading', '', get_string('entry_grading', 'hotpot')),
  160. $mform->createElement('checkbox', 'entry_dates', '', get_string('entry_dates', 'hotpot')),
  161. $mform->createElement('checkbox', 'entry_attempts', '', get_string('entry_attempts', 'hotpot'))
  162. );
  163. $mform->addGroup($elements, 'entryoptions_elements', get_string('entryoptions', 'hotpot'), html_writer::empty_tag('br'), false);
  164. $mform->setAdvanced('entryoptions_elements');
  165. $mform->addHelpButton('entryoptions_elements', 'entryoptions', 'hotpot');
  166. $mform->disabledIf('entryoptions_elements', 'entrypage', 'ne', 1);
  167. // Exit page ------------------------------------------------------------------
  168. $mform->addElement('header', 'exitpagehdr', get_string('exitpagehdr', 'hotpot'));
  169. //-----------------------------------------------------------------------------
  170. // Exit page text editor
  171. $this->add_hotpot_text_editor('exit');
  172. // Exit page options (feedback)
  173. $elements = array(
  174. $mform->createElement('checkbox', 'exit_title', '', get_string('title', 'hotpot')),
  175. $mform->createElement('checkbox', 'exit_encouragement', '', get_string('exit_encouragement', 'hotpot')),
  176. $mform->createElement('checkbox', 'exit_attemptscore', '', get_string('exit_attemptscore', 'hotpot', '...')),
  177. $mform->createElement('checkbox', 'exit_hotpotgrade', '', get_string('exit_hotpotgrade', 'hotpot', '...'))
  178. );
  179. $mform->addGroup($elements, 'exit_feedback', get_string('exit_feedback', 'hotpot'), html_writer::empty_tag('br'), false);
  180. $mform->setAdvanced('exit_feedback');
  181. $mform->disabledIf('exit_feedback', 'exitpage', 'ne', 1);
  182. $mform->addHelpButton('exit_feedback', 'exit_feedback', 'hotpot');
  183. // Exit page options (links)
  184. $elements = array(
  185. $mform->createElement('checkbox', 'exit_retry', '', get_string('exit_retry', 'hotpot').': '.get_string('exit_retry_text', 'hotpot')),
  186. $mform->createElement('checkbox', 'exit_index', '', get_string('exit_index', 'hotpot').': '.get_string('exit_index_text', 'hotpot')),
  187. $mform->createElement('checkbox', 'exit_course', '', get_string('exit_course', 'hotpot').': '.get_string('exit_course_text', 'hotpot')),
  188. $mform->createElement('checkbox', 'exit_grades', '', get_string('exit_grades', 'hotpot').': '.get_string('exit_grades_text', 'hotpot')),
  189. );
  190. $mform->addGroup($elements, 'exit_links', get_string('exit_links', 'hotpot'), html_writer::empty_tag('br'), false);
  191. $mform->setAdvanced('exit_links');
  192. $mform->disabledIf('exit_links', 'exitpage', 'ne', 1);
  193. $mform->addHelpButton('exit_links', 'exit_links', 'hotpot');
  194. // Next activity
  195. $this->add_activity_list('exit');
  196. // Display --------------------------------------------------------------------
  197. $mform->addElement('header', 'displayhdr', get_string('display', 'form'));
  198. //-----------------------------------------------------------------------------
  199. // Output format
  200. if (empty($this->current->sourcetype)) {
  201. $sourcetype = ''; // add
  202. } else {
  203. $sourcetype = $this->current->sourcetype;
  204. }
  205. $mform->addElement('select', 'outputformat', get_string('outputformat', 'hotpot'), hotpot::available_outputformats_list($sourcetype));
  206. $mform->setDefault('outputformat', get_user_preferences('hotpot_outputformat', ''));
  207. $mform->addHelpButton('outputformat', 'outputformat', 'hotpot');
  208. // Navigation
  209. $mform->addElement('select', 'navigation', get_string('navigation', 'hotpot'), hotpot::available_navigations_list());
  210. $mform->setDefault('navigation', get_user_preferences('hotpot_navigation', hotpot::NAVIGATION_MOODLE));
  211. $mform->addHelpButton('navigation', 'navigation', 'hotpot');
  212. // Title
  213. $mform->addElement('select', 'title', get_string('title', 'hotpot'), hotpot::available_titles_list());
  214. $mform->setDefault('title', get_user_preferences('hotpot_title', hotpot::TEXTSOURCE_SPECIFIC));
  215. $mform->addHelpButton('title', 'title', 'hotpot');
  216. $mform->setAdvanced('title');
  217. // Show stop button
  218. $options = array(
  219. 'hotpot_giveup' => get_string('giveup', 'hotpot'),
  220. 'specific' => get_string('stopbutton_specific', 'hotpot')
  221. );
  222. $elements = array(
  223. $mform->createElement('selectyesno', 'stopbutton_yesno', ''),
  224. $mform->createElement('select', 'stopbutton_type', '', $options),
  225. $mform->createElement('text', 'stopbutton_text', '', array('size' => '20'))
  226. );
  227. $mform->addGroup($elements, 'stopbutton_elements', get_string('stopbutton', 'hotpot'), ' ', false);
  228. $mform->addHelpButton('stopbutton_elements', 'stopbutton', 'hotpot');
  229. $mform->setAdvanced('stopbutton_elements');
  230. $mform->setType('stopbutton_yesno', PARAM_INT);
  231. $mform->setType('stopbutton_type', PARAM_ALPHAEXT);
  232. $mform->setType('stopbutton_text', PARAM_TEXT);
  233. $mform->disabledIf('stopbutton_elements', 'stopbutton_yesno', 'ne', '1');
  234. $mform->disabledIf('stopbutton_text', 'stopbutton_type', 'ne', 'specific');
  235. // Use filters
  236. $mform->addElement('selectyesno', 'usefilters', get_string('usefilters', 'hotpot'));
  237. $mform->setType('usefilters', PARAM_INT);
  238. $mform->setDefault('usefilters', get_user_preferences('hotpot_quiz_usefilters', 1));
  239. $mform->addHelpButton('usefilters', 'usefilters', 'hotpot');
  240. $mform->setAdvanced('usefilters');
  241. // Use glossary
  242. $mform->addElement('selectyesno', 'useglossary', get_string('useglossary', 'hotpot'));
  243. $mform->setType('useglossary', PARAM_INT);
  244. $mform->setDefault('useglossary', get_user_preferences('hotpot_quiz_useglossary', 1));
  245. $mform->addHelpButton('useglossary', 'useglossary', 'hotpot');
  246. $mform->setAdvanced('useglossary');
  247. // Use media filters
  248. $mform->addElement('select', 'usemediafilter', get_string('usemediafilter', 'hotpot'), hotpot::available_mediafilters_list());
  249. $mform->setType('usemediafilter', PARAM_SAFEDIR); // [a-zA-Z0-9_-]
  250. $mform->setDefault('usemediafilter', get_user_preferences('hotpot_quiz_usemediafilter', 'moodle'));
  251. $mform->addHelpButton('usemediafilter', 'usemediafilter', 'hotpot');
  252. $mform->setAdvanced('usemediafilter');
  253. // Student feedback
  254. $elements = array(
  255. $mform->createElement('select', 'studentfeedback', '', hotpot::available_feedbacks_list()),
  256. $mform->createElement('text', 'studentfeedbackurl', '', array('size'=>'40'))
  257. );
  258. $mform->addGroup($elements, 'studentfeedback_elements', get_string('studentfeedback', 'hotpot'), array(' '), false);
  259. $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', hotpot::FEEDBACK_NONE);
  260. $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', hotpot::FEEDBACK_MOODLEFORUM);
  261. $mform->disabledIf('studentfeedback_elements', 'studentfeedback', 'eq', hotpot::FEEDBACK_MOODLEMESSAGING);
  262. $mform->addHelpButton('studentfeedback_elements', 'studentfeedback', 'hotpot');
  263. $mform->setAdvanced('studentfeedback_elements');
  264. $mform->setType('studentfeedback', PARAM_INT);
  265. $mform->setType('studentfeedbackurl', PARAM_URL);
  266. // Access control -------------------------------------------------------------
  267. $mform->addElement('header', 'accesscontrolhdr', get_string('accesscontrol', 'lesson'));
  268. //-----------------------------------------------------------------------------
  269. // Previous activity
  270. $this->add_activity_list('entry');
  271. // Open time
  272. $mform->addElement('date_time_selector', 'timeopen', get_string('timeopen', 'hotpot'), array('optional' => true));
  273. $mform->addHelpButton('timeopen', 'timeopenclose', 'hotpot');
  274. $mform->setAdvanced('timeopen');
  275. // Close time
  276. $mform->addElement('date_time_selector', 'timeclose', get_string('timeclose', 'hotpot'), array('optional' => true));
  277. $mform->addHelpButton('timeclose', 'timeopenclose', 'hotpot');
  278. $mform->setAdvanced('timeclose');
  279. // Time limit
  280. $options = array(
  281. hotpot::TIME_SPECIFIC => get_string('timelimitspecific', 'hotpot'),
  282. hotpot::TIME_TEMPLATE => get_string('timelimittemplate', 'hotpot'),
  283. hotpot::TIME_DISABLE => get_string('disable')
  284. );
  285. $elements = array(
  286. $mform->createElement('static', '', '', get_string('timelimitsummary', 'hotpot')),
  287. $mform->createElement('static', '', '', html_writer::empty_tag('br')),
  288. $mform->createElement('select', 'timelimit', '', $options),
  289. $mform->createElement('duration', 'timelimitspecific', '', array('optional'=>0, 'defaultunit'=>1))
  290. );
  291. $mform->addGroup($elements, 'timelimit_elements', get_string('timelimit', 'hotpot'), '', false);
  292. $mform->addHelpButton('timelimit_elements', 'timelimit', 'hotpot');
  293. $mform->setAdvanced('timelimit_elements');
  294. $mform->setType('timelimit', PARAM_INT);
  295. $mform->disabledIf('timelimitspecific[number]', 'timelimit', 'ne', hotpot::TIME_SPECIFIC);
  296. $mform->disabledIf('timelimitspecific[timeunit]', 'timelimit', 'ne', hotpot::TIME_SPECIFIC);
  297. // Delay 1
  298. $elements = array(
  299. $mform->createElement('static', '', '', get_string('delay1summary', 'hotpot')),
  300. $mform->createElement('static', '', '', html_writer::empty_tag('br')),
  301. $mform->createElement('duration', 'delay1', '', array('optional'=>1, 'defaultunit'=>1))
  302. );
  303. $mform->addGroup($elements, 'delay1_elements', get_string('delay1', 'hotpot'), '', false);
  304. $mform->addHelpButton('delay1_elements', 'delay1', 'hotpot');
  305. $mform->setAdvanced('delay1_elements');
  306. // the standard disabledIf for the "enable" checkbox doesn't work because we are in group, so ...
  307. $mform->disabledIf('delay1[number]', 'delay1[enabled]', 'notchecked', '');
  308. $mform->disabledIf('delay1[timeunit]', 'delay1[enabled]', 'notchecked', '');
  309. // Delay 2
  310. $elements = array(
  311. $mform->createElement('static', '', '', get_string('delay2summary', 'hotpot')),
  312. $mform->createElement('static', '', '', html_writer::empty_tag('br')),
  313. $mform->createElement('duration', 'delay2', '', array('optional'=>1, 'defaultunit'=>1))
  314. );
  315. $mform->addGroup($elements, 'delay2_elements', get_string('delay2', 'hotpot'), '', false);
  316. $mform->addHelpButton('delay2_elements', 'delay2', 'hotpot');
  317. $mform->setAdvanced('delay2_elements');
  318. // the standard disabledIf for the "enable" checkbox doesn't work because we are in group, so ...
  319. $mform->disabledIf('delay2[number]', 'delay2[enabled]', 'notchecked', '');
  320. $mform->disabledIf('delay2[timeunit]', 'delay2[enabled]', 'notchecked', '');
  321. // Delay 3
  322. $options = array(
  323. hotpot::TIME_SPECIFIC => get_string('delay3specific', 'hotpot'),
  324. hotpot::TIME_TEMPLATE => get_string('delay3template', 'hotpot'),
  325. hotpot::TIME_AFTEROK => get_string('delay3afterok', 'hotpot'),
  326. hotpot::TIME_DISABLE => get_string('delay3disable', 'hotpot')
  327. );
  328. $elements = array(
  329. $mform->createElement('static', '', '', get_string('delay3summary', 'hotpot')),
  330. $mform->createElement('static', '', '', html_writer::empty_tag('br')),
  331. $mform->createElement('select', 'delay3', '', $options),
  332. $mform->createElement('duration', 'delay3specific', '', array('optional'=>0, 'defaultunit'=>1))
  333. );
  334. $mform->addGroup($elements, 'delay3_elements', get_string('delay3', 'hotpot'), '', false);
  335. $mform->addHelpButton('delay3_elements', 'delay3', 'hotpot');
  336. $mform->setAdvanced('delay3_elements');
  337. $mform->setType('delay3', PARAM_INT);
  338. $mform->disabledIf('delay3specific[number]', 'delay3', 'ne', hotpot::TIME_SPECIFIC);
  339. $mform->disabledIf('delay3specific[timeunit]', 'delay3', 'ne', hotpot::TIME_SPECIFIC);
  340. // Allow review?
  341. $mform->addElement('selectyesno', 'allowreview', get_string('allowreview', 'hotpot'));
  342. $mform->setDefault('allowreview', get_user_preferences('hotpot_review', 1));
  343. $mform->addHelpButton('allowreview', 'allowreview', 'hotpot');
  344. $mform->setAdvanced('allowreview');
  345. // Security -------------------------------------------------------------------
  346. $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
  347. //-----------------------------------------------------------------------------
  348. // Maximum number of attempts
  349. $mform->addElement('select', 'attemptlimit', get_string('attemptsallowed', 'quiz'), hotpot::available_attemptlimits_list());
  350. $mform->setDefault('attemptlimit', get_user_preferences('hotpot_attempts', 0)); // 0=unlimited
  351. $mform->setAdvanced('attemptlimit');
  352. $mform->addHelpButton('attemptlimit', 'attemptlimit', 'hotpot');
  353. // Password
  354. $mform->addElement('text', 'password', get_string('requirepassword', 'quiz'));
  355. $mform->setType('password', PARAM_TEXT);
  356. $mform->addHelpButton('password', 'requirepassword', 'quiz');
  357. $mform->setAdvanced('password');
  358. // IP address.
  359. $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
  360. $mform->setType('subnet', PARAM_TEXT);
  361. $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
  362. $mform->setAdvanced('subnet');
  363. // Grades ---------------------------------------------------------------------
  364. $mform->addElement('header', 'gradeshdr', get_string('grades', 'grades'));
  365. //-----------------------------------------------------------------------------
  366. // Grading method
  367. $mform->addElement('select', 'grademethod', get_string('grademethod', 'hotpot'), hotpot::available_grademethods_list());
  368. $mform->setDefault('grademethod', get_user_preferences('hotpot_grademethod', hotpot::GRADEMETHOD_HIGHEST));
  369. $mform->addHelpButton('grademethod', 'grademethod', 'hotpot');
  370. // $mform->setAdvanced('grademethod');
  371. // Grade weighting
  372. $mform->addElement('select', 'gradeweighting', get_string('gradeweighting', 'hotpot'), hotpot::available_gradeweightings_list());
  373. $mform->setDefault('gradeweighting', get_user_preferences('hotpot_gradeweighting', 100));
  374. $mform->addHelpButton('gradeweighting', 'gradeweighting', 'hotpot');
  375. $mform->setAdvanced('gradeweighting');
  376. // Remove grade item
  377. if ($this->is_add() || ! $this->has_grade_item()) {
  378. $mform->addElement('hidden', 'removegradeitem', 0);
  379. $mform->setType('removegradeitem', PARAM_INT);
  380. } else {
  381. $mform->addElement('selectyesno', 'removegradeitem', get_string('removegradeitem', 'hotpot'));
  382. $mform->addHelpButton('removegradeitem', 'removegradeitem', 'hotpot');
  383. $mform->setType('removegradeitem', PARAM_INT);
  384. $mform->setAdvanced('removegradeitem');
  385. // this element is only available if gradeweighting==0
  386. $mform->disabledIf('removegradeitem', 'gradeweighting', 'selected', 0);
  387. }
  388. // Standard settings (groups etc), common to all modules ----------------------
  389. $this->standard_coursemodule_elements();
  390. // Standard buttons, common to all modules ------------------------------------
  391. $this->add_action_buttons();
  392. }
  393. /**
  394. * add_hotpot_text_editor
  395. *
  396. * @param xxx $type
  397. */
  398. function add_hotpot_text_editor($type) {
  399. $mform = $this->_form;
  400. if ($this->is_add()) {
  401. $options = array(
  402. hotpot::TEXTSOURCE_FILE => get_string('textsourcefile', 'hotpot'),
  403. hotpot::TEXTSOURCE_SPECIFIC => get_string('textsourcespecific', 'hotpot')
  404. );
  405. $elements = array(
  406. $mform->createElement('selectyesno', $type.'page'),
  407. $mform->createElement('select', $type.'textsource', '', $options)
  408. );
  409. $mform->addGroup($elements, $type.'page_elements', get_string($type.'page', 'hotpot'), array(' '), false);
  410. $mform->setDefault($type.'page', get_user_preferences('hotpot_'.$type.'page', 0));
  411. $mform->setAdvanced($type.'page_elements');
  412. $mform->addHelpButton($type.'page_elements', $type.'page', 'hotpot');
  413. $mform->disabledIf($type.'page_elements', $type.'page', 'ne', 1);
  414. } else {
  415. $mform->addElement('selectyesno', $type.'page', get_string($type.'page', 'hotpot'));
  416. $mform->setType($type.'page', PARAM_INT);
  417. $mform->addHelpButton($type.'page', $type.'page', 'hotpot');
  418. $mform->addElement('hidden', $type.'textsource', hotpot::TEXTSOURCE_SPECIFIC);
  419. }
  420. $mform->setType($type.'page', PARAM_INT);
  421. $mform->setType($type.'textsource', PARAM_INT);
  422. $options = hotpot::text_editors_options($this->context);
  423. $mform->addElement('editor', $type.'editor', get_string($type.'text', 'hotpot'), null, $options);
  424. $mform->setType($type.'editor', PARAM_RAW); // no XSS prevention here, users must be trusted
  425. $mform->setAdvanced($type.'editor');
  426. $mform->disabledIf($type.'editor[text]', $type.'page', 'ne', 1);
  427. $mform->disabledIf($type.'editor[format]', $type.'page', 'ne', 1);
  428. if ($this->is_add()) {
  429. $mform->disabledIf($type.'editor[text]', $type.'textsource', 'ne', hotpot::TEXTSOURCE_SPECIFIC);
  430. $mform->disabledIf($type.'editor[format]', $type.'textsource', 'ne', hotpot::TEXTSOURCE_SPECIFIC);
  431. }
  432. }
  433. /**
  434. * add_activity_list
  435. *
  436. * @param xxx $type
  437. */
  438. function add_activity_list($type) {
  439. global $PAGE;
  440. // if activity name is longer than $namelength, it will be truncated
  441. // to first $headlength chars + " ... " + last $taillength chars
  442. $namelength = 40;
  443. $headlength = 16;
  444. $taillength = 16;
  445. $mform = $this->_form;
  446. $optgroups = array(
  447. get_string('none') => array(
  448. hotpot::ACTIVITY_NONE => get_string('none')
  449. ),
  450. get_string($type=='entry' ? 'previous' : 'next') => array(
  451. hotpot::ACTIVITY_COURSE_ANY => get_string($type.'cmcourse', 'hotpot'),
  452. hotpot::ACTIVITY_SECTION_ANY => get_string($type.'cmsection', 'hotpot'),
  453. hotpot::ACTIVITY_COURSE_HOTPOT => get_string($type.'hotpotcourse', 'hotpot'),
  454. hotpot::ACTIVITY_SECTION_HOTPOT => get_string($type.'hotpotsection', 'hotpot')
  455. )
  456. );
  457. if ($modinfo = get_fast_modinfo($PAGE->course)) {
  458. switch ($PAGE->course->format) {
  459. case 'weeks': $strsection = get_string('strftimedateshort'); break;
  460. case 'topics': $strsection = get_string('topic'); break;
  461. default: $strsection = get_string('section');
  462. }
  463. $sectionnum = -1;
  464. foreach ($modinfo->cms as $cmid=>$mod) {
  465. if ($mod->modname=='label') {
  466. continue; // ignore labels
  467. }
  468. if ($type=='entry' && $mod->modname=='resource') {
  469. continue; // ignore resources as entry activities
  470. }
  471. if (isset($form->update) && $form->update==$cmid) {
  472. continue; // ignore this hotpot
  473. }
  474. if ($sectionnum==$mod->sectionnum) {
  475. // do nothing (same section)
  476. } else {
  477. // start new optgroup for this course section
  478. $sectionnum = $mod->sectionnum;
  479. if ($sectionnum==0) {
  480. $optgroup = get_string('activities');
  481. } else if ($PAGE->course->format=='weeks') {
  482. $date = $PAGE->course->startdate + 7200 + ($sectionnum * 604800);
  483. $optgroup = userdate($date, $strsection).' - '.userdate($date + 518400, $strsection);
  484. } else {
  485. $optgroup = $strsection.': '.$sectionnum;
  486. }
  487. if (empty($options[$optgroup])) {
  488. $options[$optgroup] = array();
  489. }
  490. }
  491. $name = format_string($mod->name);
  492. $strlen = hotpot_textlib('strlen', $name);
  493. if ($strlen > $namelength) {
  494. $head = hotpot_textlib('substr', $name, 0, $headlength);
  495. $tail = hotpot_textlib('substr', $name, $strlen - $taillength, $taillength);
  496. $name = $head.' ... '.$tail;
  497. }
  498. $optgroups[$optgroup][$cmid] = $name;
  499. }
  500. }
  501. $options = array();
  502. for ($i=100; $i>=0; $i--) {
  503. $options[$i] = $i.'%';
  504. }
  505. $elements = array(
  506. $mform->createElement('selectgroups', $type.'cm', '', $optgroups),
  507. $mform->createElement('select', $type.'grade', '', $options)
  508. );
  509. $mform->addGroup($elements, $type.'cm_elements', get_string($type.'cm', 'hotpot'), array(' '), false);
  510. $mform->addHelpButton($type.'cm_elements', $type.'cm', 'hotpot');
  511. if ($type=='entry') {
  512. $defaultcm = hotpot::ACTIVITY_NONE;
  513. $defaultgrade = 100;
  514. } else {
  515. $defaultcm = hotpot::ACTIVITY_SECTION_HOTPOT;
  516. $defaultgrade = 0;
  517. }
  518. $mform->setDefault($type.'cm', get_user_preferences('hotpot_'.$type.'cm', $defaultcm));
  519. $mform->setDefault($type.'grade', get_user_preferences('hotpot_'.$type.'grade', $defaultgrade));
  520. $mform->disabledIf($type.'cm_elements', $type.'cm', 'eq', 0);
  521. if ($type=='entry') {
  522. $mform->setAdvanced($type.'cm_elements');
  523. }
  524. // add module icons, if possible
  525. if ($modinfo) {
  526. $element = reset($mform->getElement($type.'cm_elements')->getElements());
  527. for ($i=0; $i<count($element->_optGroups); $i++) {
  528. $optgroup = &$element->_optGroups[$i];
  529. for ($ii=0; $ii<count($optgroup['options']); $ii++) {
  530. $option = &$optgroup['options'][$ii];
  531. if (isset($option['attr']['value']) && $option['attr']['value']>0) {
  532. $cmid = $option['attr']['value'];
  533. $url = $PAGE->theme->pix_url('icon', $modinfo->cms[$cmid]->modname)->out();
  534. $option['attr']['style'] = "background-image: url($url); background-repeat: no-repeat; background-position: 1px 2px; min-height: 20px;";
  535. }
  536. }
  537. }
  538. }
  539. }
  540. /**
  541. * Prepares the form before data are set
  542. *
  543. * Additional wysiwyg editors are prepared here
  544. * along with the stopbutton switch, type and text
  545. *
  546. * @param array $data to be set
  547. * @return void
  548. */
  549. function data_preprocessing(&$data) {
  550. // Note: if you call "file_prepare_draft_area()" without setting itemid
  551. // (the first argument), then it will be assigned automatically, and the files
  552. // for this context will be transferred automatically, which is what we want
  553. $data['sourceitemid'] = 0;
  554. if ($this->is_add()) {
  555. $contextid = null;
  556. } else {
  557. $contextid = $this->context->id;
  558. }
  559. $options = hotpot::sourcefile_options(); // array('subdirs' => 1, 'maxbytes' => 0, 'maxfiles' => -1);
  560. file_prepare_draft_area($data['sourceitemid'], $contextid, 'mod_hotpot', 'sourcefile', 0, $options);
  561. if ($this->is_add()) {
  562. // set fields from user preferences, where possible
  563. foreach (hotpot::user_preferences_fieldnames() as $fieldname) {
  564. if (! isset($data[$fieldname])) {
  565. $data[$fieldname] = get_user_preferences('hotpot_'.$fieldname, '');
  566. }
  567. }
  568. }
  569. // set entry/exit page settings
  570. foreach (hotpot::text_page_types() as $type) {
  571. // extract boolean switches for page options
  572. foreach (hotpot::text_page_options($type) as $name => $mask) {
  573. $data[$type.'_'.$name] = $data[$type.'options'] & $mask;
  574. }
  575. // setup custom wysiwyg editor
  576. $draftitemid = 0;
  577. if ($this->is_add()) {
  578. // adding a new hotpot instance
  579. $data[$type.'editor'] = array(
  580. 'text' => file_prepare_draft_area($draftitemid, $contextid, 'mod_hotpot', $type, 0),
  581. 'format' => editors_get_preferred_format(),
  582. 'itemid' => file_get_submitted_draft_itemid($type)
  583. );
  584. } else {
  585. // editing an existing hotpot
  586. $data[$type.'editor'] = array(
  587. 'text' => file_prepare_draft_area($draftitemid, $contextid, 'mod_hotpot', $type, 0, $options, $data[$type.'text']),
  588. 'format' => $data[$type.'format'],
  589. 'itemid' => file_get_submitted_draft_itemid($type)
  590. );
  591. }
  592. }
  593. // timelimit
  594. if ($data['timelimit']>0) {
  595. $data['timelimitspecific'] = $data['timelimit'];
  596. $data['timelimit'] = hotpot::TIME_SPECIFIC;
  597. } else {
  598. $data['timelimitspecific'] = 0;
  599. }
  600. // delay3
  601. if ($data['delay3']>0) {
  602. $data['delay3specific'] = $data['delay3'];
  603. $data['delay3'] = hotpot::TIME_SPECIFIC;
  604. } else {
  605. $data['delay3specific'] = 0;
  606. }
  607. // set stopbutton options
  608. switch ($data['stopbutton']) {
  609. case hotpot::STOPBUTTON_SPECIFIC:
  610. $data['stopbutton_yesno'] = 1;
  611. $data['stopbutton_type'] = 'specific';
  612. $data['stopbutton_text'] = $data['stoptext'];
  613. break;
  614. case hotpot::STOPBUTTON_LANGPACK:
  615. $data['stopbutton_yesno'] = 1;
  616. $data['stopbutton_type'] = $data['stoptext'];
  617. $data['stopbutton_text'] = '';
  618. break;
  619. case hotpot::STOPBUTTON_NONE:
  620. default:
  621. $data['stopbutton_yesno'] = 0;
  622. $data['stopbutton_type'] = '';
  623. $data['stopbutton_text'] = '';
  624. }
  625. }
  626. /**
  627. * validation
  628. *
  629. * @param xxx $data
  630. * @return xxx
  631. */
  632. function validation($data, $files) {
  633. global $USER;
  634. // http://docs.moodle.org/en/Development:lib/formslib.php_Validation
  635. // Note: see "lang/en/error.php" for a list of common messages
  636. $errors = array();
  637. // get the $files specified in the form
  638. $usercontext = hotpot_get_context(CONTEXT_USER, $USER->id);
  639. $fs = get_file_storage();
  640. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['sourceitemid'], 'sortorder, id', 0); // files only, no dirs
  641. // check we have at least one file
  642. // (and set mainfile marker, if necessary)
  643. if (empty($files)) {
  644. $errors['sourceitemid'] = get_string('required');
  645. // $errors['sourceitemid'] = get_string('nofile', 'error');
  646. } else {
  647. $mainfile = false;
  648. foreach ($files as $file) {
  649. if ($file->get_sortorder()==1) {
  650. $mainfile = true;
  651. break;
  652. }
  653. }
  654. if (! $mainfile) {
  655. $file = reset($files); // first file in the list
  656. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), 1);
  657. }
  658. }
  659. // studentfeedbackurl
  660. if ($data['studentfeedback']==hotpot::FEEDBACK_WEBPAGE || $data['studentfeedback']==hotpot::FEEDBACK_FORMMAIL) {
  661. if (empty($data['studentfeedbackurl']) || ! preg_match('/^https?:\/\/.+/', $data['studentfeedbackurl'])) {
  662. // empty or invalid url
  663. $errors['studentfeedback_elements']= get_string('invalidurl', 'error');
  664. }
  665. }
  666. return $errors;
  667. }
  668. }