PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/question/editlib.php

http://github.com/moodle/moodle
PHP | 659 lines | 308 code | 84 blank | 267 comment | 67 complexity | d4506881f0c2bd461a20a1ff3f09c04f 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. /**
  17. * Functions used to show question editing interface
  18. *
  19. * @package moodlecore
  20. * @subpackage questionbank
  21. * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. use core_question\bank\search\category_condition;
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir . '/questionlib.php');
  27. define('DEFAULT_QUESTIONS_PER_PAGE', 20);
  28. define('MAXIMUM_QUESTIONS_PER_PAGE', 1000);
  29. function get_module_from_cmid($cmid) {
  30. global $CFG, $DB;
  31. if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
  32. FROM {course_modules} cm,
  33. {modules} md
  34. WHERE cm.id = ? AND
  35. md.id = cm.module", array($cmid))){
  36. print_error('invalidcoursemodule');
  37. } elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
  38. print_error('invalidcoursemodule');
  39. }
  40. $modrec->instance = $modrec->id;
  41. $modrec->cmid = $cmrec->id;
  42. $cmrec->name = $modrec->name;
  43. return array($modrec, $cmrec);
  44. }
  45. /**
  46. * Function to read all questions for category into big array
  47. *
  48. * @param int $category category number
  49. * @param bool $noparent if true only questions with NO parent will be selected
  50. * @param bool $recurse include subdirectories
  51. * @param bool $export set true if this is called by questionbank export
  52. */
  53. function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
  54. global $DB;
  55. // Build sql bit for $noparent
  56. $npsql = '';
  57. if ($noparent) {
  58. $npsql = " and parent='0' ";
  59. }
  60. // Get list of categories
  61. if ($recurse) {
  62. $categorylist = question_categorylist($category->id);
  63. } else {
  64. $categorylist = array($category->id);
  65. }
  66. // Get the list of questions for the category
  67. list($usql, $params) = $DB->get_in_or_equal($categorylist);
  68. $questions = $DB->get_records_select('question', "category {$usql} {$npsql}", $params, 'category, qtype, name');
  69. // Iterate through questions, getting stuff we need
  70. $qresults = array();
  71. foreach($questions as $key => $question) {
  72. $question->export_process = $export;
  73. $qtype = question_bank::get_qtype($question->qtype, false);
  74. if ($export && $qtype->name() == 'missingtype') {
  75. // Unrecognised question type. Skip this question when exporting.
  76. continue;
  77. }
  78. $qtype->get_question_options($question);
  79. $qresults[] = $question;
  80. }
  81. return $qresults;
  82. }
  83. /**
  84. * Checks whether this is the only child of a top category in a context.
  85. *
  86. * @param int $categoryid a category id.
  87. * @return bool
  88. */
  89. function question_is_only_child_of_top_category_in_context($categoryid) {
  90. global $DB;
  91. return 1 == $DB->count_records_sql("
  92. SELECT count(*)
  93. FROM {question_categories} c
  94. JOIN {question_categories} p ON c.parent = p.id
  95. JOIN {question_categories} s ON s.parent = c.parent
  96. WHERE c.id = ? AND p.parent = 0", array($categoryid));
  97. }
  98. /**
  99. * Checks whether the category is a "Top" category (with no parent).
  100. *
  101. * @param int $categoryid a category id.
  102. * @return bool
  103. */
  104. function question_is_top_category($categoryid) {
  105. global $DB;
  106. return 0 == $DB->get_field('question_categories', 'parent', array('id' => $categoryid));
  107. }
  108. /**
  109. * Ensures that this user is allowed to delete this category.
  110. *
  111. * @param int $todelete a category id.
  112. */
  113. function question_can_delete_cat($todelete) {
  114. global $DB;
  115. if (question_is_top_category($todelete)) {
  116. print_error('cannotdeletetopcat', 'question');
  117. } else if (question_is_only_child_of_top_category_in_context($todelete)) {
  118. print_error('cannotdeletecate', 'question');
  119. } else {
  120. $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
  121. require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
  122. }
  123. }
  124. /**
  125. * Base class for representing a column in a {@link question_bank_view}.
  126. *
  127. * @copyright 2009 Tim Hunt
  128. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  129. * @deprecated since Moodle 2.7 MDL-40457
  130. */
  131. class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
  132. /**
  133. * A column with a checkbox for each question with name q{questionid}.
  134. *
  135. * @copyright 2009 Tim Hunt
  136. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  137. * @deprecated since Moodle 2.7 MDL-40457
  138. */
  139. class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
  140. /**
  141. * A column type for the name of the question type.
  142. *
  143. * @copyright 2009 Tim Hunt
  144. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  145. * @deprecated since Moodle 2.7 MDL-40457
  146. */
  147. class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
  148. /**
  149. * A column type for the name of the question name.
  150. *
  151. * @copyright 2009 Tim Hunt
  152. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  153. * @deprecated since Moodle 2.7 MDL-40457
  154. */
  155. class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
  156. /**
  157. * A column type for the name of the question creator.
  158. *
  159. * @copyright 2009 Tim Hunt
  160. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  161. * @deprecated since Moodle 2.7 MDL-40457
  162. */
  163. class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
  164. /**
  165. * A column type for the name of the question last modifier.
  166. *
  167. * @copyright 2009 Tim Hunt
  168. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  169. * @deprecated since Moodle 2.7 MDL-40457
  170. */
  171. class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
  172. /**
  173. * A base class for actions that are an icon that lets you manipulate the question in some way.
  174. *
  175. * @copyright 2009 Tim Hunt
  176. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  177. * @deprecated since Moodle 2.7 MDL-40457
  178. */
  179. class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
  180. /**
  181. * Base class for question bank columns that just contain an action icon.
  182. *
  183. * @copyright 2009 Tim Hunt
  184. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  185. * @deprecated since Moodle 2.7 MDL-40457
  186. */
  187. class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
  188. /**
  189. * Question bank column for the duplicate action icon.
  190. *
  191. * @copyright 2013 The Open University
  192. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  193. * @deprecated since Moodle 2.7 MDL-40457
  194. */
  195. class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
  196. /**
  197. * Question bank columns for the preview action icon.
  198. *
  199. * @copyright 2009 Tim Hunt
  200. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  201. * @deprecated since Moodle 2.7 MDL-40457
  202. */
  203. class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
  204. /**
  205. * action to delete (or hide) a question, or restore a previously hidden question.
  206. *
  207. * @copyright 2009 Tim Hunt
  208. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  209. * @deprecated since Moodle 2.7 MDL-40457
  210. */
  211. class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
  212. /**
  213. * Base class for 'columns' that are actually displayed as a row following the main question row.
  214. *
  215. * @copyright 2009 Tim Hunt
  216. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  217. * @deprecated since Moodle 2.7 MDL-40457
  218. */
  219. class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
  220. /**
  221. * A column type for the name of the question name.
  222. *
  223. * @copyright 2009 Tim Hunt
  224. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  225. * @deprecated since Moodle 2.7 MDL-40457
  226. */
  227. class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
  228. /**
  229. * @copyright 2009 Tim Hunt
  230. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  231. * @deprecated since Moodle 2.7 MDL-40457
  232. */
  233. class_alias('core_question\bank\view', 'question_bank_view', true);
  234. /**
  235. * Common setup for all pages for editing questions.
  236. * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
  237. * @param string $edittab code for this edit tab
  238. * @param bool $requirecmid require cmid? default false
  239. * @param bool $unused no longer used, do no pass
  240. * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
  241. */
  242. function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
  243. global $PAGE;
  244. if ($unused !== null) {
  245. debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER);
  246. }
  247. $params = [];
  248. if ($requirecmid) {
  249. $params['cmid'] = required_param('cmid', PARAM_INT);
  250. } else {
  251. $params['cmid'] = optional_param('cmid', null, PARAM_INT);
  252. }
  253. if (!$params['cmid']) {
  254. $params['courseid'] = required_param('courseid', PARAM_INT);
  255. }
  256. $params['qpage'] = optional_param('qpage', null, PARAM_INT);
  257. // Pass 'cat' from page to page and when 'category' comes from a drop down menu
  258. // then we also reset the qpage so we go to page 1 of
  259. // a new cat.
  260. $params['cat'] = optional_param('cat', null, PARAM_SEQUENCE); // If empty will be set up later.
  261. $params['category'] = optional_param('category', null, PARAM_SEQUENCE);
  262. $params['qperpage'] = optional_param('qperpage', null, PARAM_INT);
  263. // Question table sorting options.
  264. for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
  265. $param = 'qbs' . $i;
  266. if ($sort = optional_param($param, '', PARAM_TEXT)) {
  267. $params[$param] = $sort;
  268. } else {
  269. break;
  270. }
  271. }
  272. // Display options.
  273. $params['recurse'] = optional_param('recurse', null, PARAM_BOOL);
  274. $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL);
  275. $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL);
  276. // Category list page.
  277. $params['cpage'] = optional_param('cpage', null, PARAM_INT);
  278. $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT);
  279. $PAGE->set_pagelayout('admin');
  280. return question_build_edit_resources($edittab, $baseurl, $params);
  281. }
  282. /**
  283. * Common function for building the generic resources required by the
  284. * editing questions pages.
  285. *
  286. * Either a cmid or a course id must be provided as keys in $params or
  287. * an exception will be thrown. All other params are optional and will have
  288. * sane default applied if not provided.
  289. *
  290. * The acceptable keys for $params are:
  291. * [
  292. * 'cmid' => PARAM_INT,
  293. * 'courseid' => PARAM_INT,
  294. * 'qpage' => PARAM_INT,
  295. * 'cat' => PARAM_SEQUENCE,
  296. * 'category' => PARAM_SEQUENCE,
  297. * 'qperpage' => PARAM_INT,
  298. * 'recurse' => PARAM_INT,
  299. * 'showhidden' => PARAM_INT,
  300. * 'qbshowtext' => PARAM_INT,
  301. * 'cpage' => PARAM_INT,
  302. * 'recurse' => PARAM_BOOL,
  303. * 'showhidden' => PARAM_BOOL,
  304. * 'qbshowtext' => PARAM_BOOL,
  305. * 'qtagids' => [PARAM_INT], (array of integers)
  306. * 'qbs1' => PARAM_TEXT,
  307. * 'qbs2' => PARAM_TEXT,
  308. * 'qbs3' => PARAM_TEXT,
  309. * ... and more qbs keys up to question_bank_view::MAX_SORTS ...
  310. * ];
  311. *
  312. * @param string $edittab Code for this edit tab
  313. * @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'.
  314. * @param array $params The provided parameters to construct the resources with.
  315. * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
  316. */
  317. function question_build_edit_resources($edittab, $baseurl, $params) {
  318. global $DB, $PAGE, $CFG;
  319. $thispageurl = new moodle_url($baseurl);
  320. $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
  321. $cleanparams = [
  322. 'qsorts' => [],
  323. 'qtagids' => []
  324. ];
  325. $paramtypes = [
  326. 'cmid' => PARAM_INT,
  327. 'courseid' => PARAM_INT,
  328. 'qpage' => PARAM_INT,
  329. 'cat' => PARAM_SEQUENCE,
  330. 'category' => PARAM_SEQUENCE,
  331. 'qperpage' => PARAM_INT,
  332. 'recurse' => PARAM_INT,
  333. 'showhidden' => PARAM_INT,
  334. 'qbshowtext' => PARAM_INT,
  335. 'cpage' => PARAM_INT,
  336. 'recurse' => PARAM_BOOL,
  337. 'showhidden' => PARAM_BOOL,
  338. 'qbshowtext' => PARAM_BOOL
  339. ];
  340. foreach ($paramtypes as $name => $type) {
  341. if (isset($params[$name])) {
  342. $cleanparams[$name] = clean_param($params[$name], $type);
  343. } else {
  344. $cleanparams[$name] = null;
  345. }
  346. }
  347. if (!empty($params['qtagids'])) {
  348. $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT);
  349. }
  350. $cmid = $cleanparams['cmid'];
  351. $courseid = $cleanparams['courseid'];
  352. $qpage = $cleanparams['qpage'] ?: -1;
  353. $cat = $cleanparams['cat'] ?: 0;
  354. $category = $cleanparams['category'] ?: 0;
  355. $qperpage = $cleanparams['qperpage'];
  356. $recurse = $cleanparams['recurse'];
  357. $showhidden = $cleanparams['showhidden'];
  358. $qbshowtext = $cleanparams['qbshowtext'];
  359. $cpage = $cleanparams['cpage'] ?: 1;
  360. $recurse = $cleanparams['recurse'];
  361. $showhidden = $cleanparams['showhidden'];
  362. $qbshowtext = $cleanparams['qbshowtext'];
  363. $qsorts = $cleanparams['qsorts'];
  364. $qtagids = $cleanparams['qtagids'];
  365. if (is_null($cmid) && is_null($courseid)) {
  366. throw new \moodle_exception('Must provide a cmid or courseid');
  367. }
  368. if ($cmid) {
  369. list($module, $cm) = get_module_from_cmid($cmid);
  370. $courseid = $cm->course;
  371. $thispageurl->params(compact('cmid'));
  372. require_login($courseid, false, $cm);
  373. $thiscontext = context_module::instance($cmid);
  374. } else {
  375. $module = null;
  376. $cm = null;
  377. $thispageurl->params(compact('courseid'));
  378. require_login($courseid, false);
  379. $thiscontext = context_course::instance($courseid);
  380. }
  381. if ($thiscontext){
  382. $contexts = new question_edit_contexts($thiscontext);
  383. $contexts->require_one_edit_tab_cap($edittab);
  384. } else {
  385. $contexts = null;
  386. }
  387. $pagevars['qpage'] = $qpage;
  388. // Pass 'cat' from page to page and when 'category' comes from a drop down menu
  389. // then we also reset the qpage so we go to page 1 of
  390. // a new cat.
  391. if ($category && $category != $cat) { // Is this a move to a new category?
  392. $pagevars['cat'] = $category;
  393. $pagevars['qpage'] = 0;
  394. } else {
  395. $pagevars['cat'] = $cat; // If empty will be set up later.
  396. }
  397. if ($pagevars['cat']){
  398. $thispageurl->param('cat', $pagevars['cat']);
  399. }
  400. if (strpos($baseurl, '/question/') === 0) {
  401. navigation_node::override_active_url($thispageurl);
  402. }
  403. // This need to occur after the override_active_url call above because
  404. // these values change on the page request causing the URLs to mismatch
  405. // when trying to work out the active node.
  406. for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
  407. $param = 'qbs' . $i;
  408. if (isset($params[$param])) {
  409. $value = clean_param($params[$param], PARAM_TEXT);
  410. } else {
  411. break;
  412. }
  413. $thispageurl->param($param, $value);
  414. }
  415. if ($pagevars['qpage'] > -1) {
  416. $thispageurl->param('qpage', $pagevars['qpage']);
  417. } else {
  418. $pagevars['qpage'] = 0;
  419. }
  420. $pagevars['qperpage'] = question_set_or_get_user_preference(
  421. 'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
  422. $defaultcategory = question_make_default_categories($contexts->all());
  423. $contextlistarr = [];
  424. foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
  425. $contextlistarr[] = "'{$context->id}'";
  426. }
  427. $contextlist = join(' ,', $contextlistarr);
  428. if (!empty($pagevars['cat'])){
  429. $catparts = explode(',', $pagevars['cat']);
  430. if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
  431. !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
  432. print_error('invalidcategory', 'question');
  433. }
  434. } else {
  435. $category = $defaultcategory;
  436. $pagevars['cat'] = "{$category->id},{$category->contextid}";
  437. }
  438. // Display options.
  439. $pagevars['recurse'] = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
  440. $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
  441. $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
  442. // Category list page.
  443. $pagevars['cpage'] = $cpage;
  444. if ($pagevars['cpage'] != 1){
  445. $thispageurl->param('cpage', $pagevars['cpage']);
  446. }
  447. $pagevars['qtagids'] = $qtagids;
  448. foreach ($pagevars['qtagids'] as $index => $qtagid) {
  449. $thispageurl->param("qtagids[{$index}]", $qtagid);
  450. }
  451. return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
  452. }
  453. /**
  454. * Get the category id from $pagevars.
  455. * @param array $pagevars from {@link question_edit_setup()}.
  456. * @return int the category id.
  457. */
  458. function question_get_category_id_from_pagevars(array $pagevars) {
  459. list($questioncategoryid) = explode(',', $pagevars['cat']);
  460. return $questioncategoryid;
  461. }
  462. /**
  463. * Get a particular question preference that is also stored as a user preference.
  464. * If the the value is given in the GET/POST request, then that value is used,
  465. * and the user preference is updated to that value. Otherwise, the last set
  466. * value of the user preference is used, or if it has never been set the default
  467. * passed to this function.
  468. *
  469. * @param string $param the param name. The URL parameter set, and the GET/POST
  470. * parameter read. The user_preference name is 'question_bank_' . $param.
  471. * @param mixed $default The default value to use, if not otherwise set.
  472. * @param int $type one of the PARAM_... constants.
  473. * @param moodle_url $thispageurl if the value has been explicitly set, we add
  474. * it to this URL.
  475. * @return mixed the parameter value to use.
  476. */
  477. function question_get_display_preference($param, $default, $type, $thispageurl) {
  478. $submittedvalue = optional_param($param, null, $type);
  479. return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
  480. }
  481. /**
  482. * Get a user preference by name or set the user preference to a given value.
  483. *
  484. * If $value is null then the function will only attempt to retrieve the
  485. * user preference requested by $name. If no user preference is found then the
  486. * $default value will be returned. In this case the user preferences are not
  487. * modified and nor are the params on $thispageurl.
  488. *
  489. * If $value is anything other than null then the function will set the user
  490. * preference $name to the provided $value and will also set it as a param
  491. * on $thispageurl.
  492. *
  493. * @param string $name The user_preference name is 'question_bank_' . $name.
  494. * @param mixed $value The preference value.
  495. * @param mixed $default The default value to use, if not otherwise set.
  496. * @param moodle_url $thispageurl if the value has been explicitly set, we add
  497. * it to this URL.
  498. * @return mixed the parameter value to use.
  499. */
  500. function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
  501. if (is_null($value)) {
  502. return get_user_preferences('question_bank_' . $name, $default);
  503. }
  504. set_user_preference('question_bank_' . $name, $value);
  505. $thispageurl->param($name, $value);
  506. return $value;
  507. }
  508. /**
  509. * Make sure user is logged in as required in this context.
  510. */
  511. function require_login_in_context($contextorid = null){
  512. global $DB, $CFG;
  513. if (!is_object($contextorid)){
  514. $context = context::instance_by_id($contextorid, IGNORE_MISSING);
  515. } else {
  516. $context = $contextorid;
  517. }
  518. if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
  519. require_login($context->instanceid);
  520. } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
  521. if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
  522. if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
  523. print_error('invalidcourseid');
  524. }
  525. require_course_login($course, true, $cm);
  526. } else {
  527. print_error('invalidcoursemodule');
  528. }
  529. } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
  530. if (!empty($CFG->forcelogin)) {
  531. require_login();
  532. }
  533. } else {
  534. require_login();
  535. }
  536. }
  537. /**
  538. * Print a form to let the user choose which question type to add.
  539. * When the form is submitted, it goes to the question.php script.
  540. * @param $hiddenparams hidden parameters to add to the form, in addition to
  541. * the qtype radio buttons.
  542. * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
  543. * those qtypes will be shown. Example value array('description', 'multichoice').
  544. */
  545. function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
  546. global $CFG, $PAGE, $OUTPUT;
  547. $chooser = core_question\output\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
  548. $renderer = $PAGE->get_renderer('question', 'bank');
  549. return $renderer->render($chooser);
  550. }
  551. /**
  552. * Print a button for creating a new question. This will open question/addquestion.php,
  553. * which in turn goes to question/question.php before getting back to $params['returnurl']
  554. * (by default the question bank screen).
  555. *
  556. * @param int $categoryid The id of the category that the new question should be added to.
  557. * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
  558. * $params['courseid'], and you should probably set $params['returnurl']
  559. * @param string $caption the text to display on the button.
  560. * @param string $tooltip a tooltip to add to the button (optional).
  561. * @param bool $disabled if true, the button will be disabled.
  562. */
  563. function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
  564. global $CFG, $PAGE, $OUTPUT;
  565. static $choiceformprinted = false;
  566. $params['category'] = $categoryid;
  567. $url = new moodle_url('/question/addquestion.php', $params);
  568. echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
  569. if (!$choiceformprinted) {
  570. echo '<div id="qtypechoicecontainer">';
  571. echo print_choose_qtype_to_add_form(array());
  572. echo "</div>\n";
  573. $choiceformprinted = true;
  574. }
  575. }