PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/quiz/addrandomform.php

https://bitbucket.org/moodle/moodle
PHP | 148 lines | 77 code | 28 blank | 43 comment | 7 complexity | 526e791a8a38929c5e9ea62b5ceca8a0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. /**
  17. * Defines the Moodle forum used to add random questions to the quiz.
  18. *
  19. * @package mod_quiz
  20. * @copyright 2008 Olli Savolainen
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir.'/formslib.php');
  25. /**
  26. * The add random questions form.
  27. *
  28. * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class quiz_add_random_form extends moodleform {
  32. protected function definition() {
  33. global $OUTPUT, $PAGE, $CFG;
  34. $mform = $this->_form;
  35. $mform->setDisableShortforms();
  36. $contexts = $this->_customdata['contexts'];
  37. $usablecontexts = $contexts->having_cap('moodle/question:useall');
  38. // Random from existing category section.
  39. $mform->addElement('header', 'existingcategoryheader',
  40. get_string('randomfromexistingcategory', 'quiz'));
  41. $mform->addElement('questioncategory', 'category', get_string('category'),
  42. array('contexts' => $usablecontexts, 'top' => true));
  43. $mform->setDefault('category', $this->_customdata['cat']);
  44. $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
  45. $tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
  46. $mform->hideIf('includesubcategories', 'category', 'in', $tops);
  47. if ($CFG->usetags) {
  48. $tagstrings = array();
  49. $tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
  50. foreach ($tags as $tag) {
  51. $tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
  52. }
  53. $options = array(
  54. 'multiple' => true,
  55. 'noselectionstring' => get_string('anytags', 'quiz'),
  56. );
  57. $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
  58. $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
  59. }
  60. // TODO: in the past, the drop-down used to only show sensible choices for
  61. // number of questions to add. That is, if the currently selected filter
  62. // only matched 9 questions (not already in the quiz), then the drop-down would
  63. // only offer choices 1..9. This nice UI hint got lost when the UI became Ajax-y.
  64. // We should add it back.
  65. $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
  66. $this->get_number_of_questions_to_add_choices());
  67. $previewhtml = $OUTPUT->render_from_template('mod_quiz/random_question_form_preview', []);
  68. $mform->addElement('html', $previewhtml);
  69. $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
  70. // If the manage categories plugins is enabled, add the elements to create a new category in the form.
  71. if (\core\plugininfo\qbank::is_plugin_enabled(\qbank_managecategories\helper::PLUGINNAME)) {
  72. // Random from a new category section.
  73. $mform->addElement('header', 'newcategoryheader',
  74. get_string('randomquestionusinganewcategory', 'quiz'));
  75. $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
  76. $mform->setType('name', PARAM_TEXT);
  77. $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
  78. array('contexts' => $usablecontexts, 'top' => true));
  79. $mform->addHelpButton('parent', 'parentcategory', 'question');
  80. $mform->addElement('submit', 'newcategory',
  81. get_string('createcategoryandaddrandomquestion', 'quiz'));
  82. }
  83. // Cancel button.
  84. $mform->addElement('cancel');
  85. $mform->closeHeaderBefore('cancel');
  86. $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
  87. $mform->setType('addonpage', PARAM_SEQUENCE);
  88. $mform->addElement('hidden', 'cmid', 0);
  89. $mform->setType('cmid', PARAM_INT);
  90. $mform->addElement('hidden', 'returnurl', 0);
  91. $mform->setType('returnurl', PARAM_LOCALURL);
  92. // Add the javascript required to enhance this mform.
  93. $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [
  94. $mform->getAttribute('id'),
  95. $contexts->lowest()->id,
  96. $tops,
  97. $CFG->usetags
  98. ]);
  99. }
  100. public function validation($fromform, $files) {
  101. $errors = parent::validation($fromform, $files);
  102. if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
  103. $errors['name'] = get_string('categorynamecantbeblank', 'question');
  104. }
  105. return $errors;
  106. }
  107. /**
  108. * Return an arbitrary array for the dropdown menu
  109. *
  110. * @param int $maxrand
  111. * @return array of integers [1, 2, ..., 100] (or to the smaller of $maxrand and 100.)
  112. */
  113. private function get_number_of_questions_to_add_choices($maxrand = 100) {
  114. $randomcount = array();
  115. for ($i = 1; $i <= min(100, $maxrand); $i++) {
  116. $randomcount[$i] = $i;
  117. }
  118. return $randomcount;
  119. }
  120. }