";
@@ -806,7 +825,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool
if ($questioncount>$randomquestionlistsize){
echo "... ";
}
-
+
$a = new stdClass;
$a->arrow = $THEME->rarrow;
$strshowcategorycontents=get_string("showcategorycontents","quiz",$a);
@@ -815,7 +834,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool
echo ' 1,"cat"=>$category->id.','.$category->contextid)).
'">'.$strshowcategorycontents.'';
-
+
echo "";
echo "";
}
@@ -934,7 +953,7 @@ function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz){
$reordercheckboxlabelclose.'';
echo '';
echo quiz_question_preview_button($quiz, $question,false);
-
+
echo '';
echo "";
@@ -989,452 +1008,459 @@ function quiz_question_tostring(&$question,$showicon=false,$showquestiontext=tru
}
}
-
-
-
-
/**
- * Shows the question bank editing interface.
- * A changed copy of the function at question/editlib.php; to be refactored.
- *
- * The function also processes a number of actions:
- *
- * Actions affecting the question pool:
- * move Moves a question to a different category
- * deleteselected Deletes the selected questions from the category
- * Other actions:
- * category Chooses the category
- * displayoptions Sets display options
- *
- * @author Martin Dougiamas and many others. This has recently been extensively
- * rewritten by Gustav Delius and other members of the Serving Mathematics project
- * {@link http://maths.york.ac.uk/serving_maths}. Partially
- * rewritten by Olli Savolainen as a part of the Quiz UI Redesign
- * project in Summer 2008
- * {@link http://docs.moodle.org/en/Development:Quiz_UI_redesign}.
- * @param moodle_url $pageurl object representing this pages url.
+ * Subclass to customise the view of the question bank for the quiz editing screen.
*/
-function quiz_question_showbank($tabname, $contexts, $pageurl, $cm,
- $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse,
- $showhidden, $showquestiontext, $cmoptions){
- global $COURSE,$DB;
+class quiz_question_bank_view extends question_bank_view {
+ protected $quizhasattempts = false;
- if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
- // make a list of all the questions that are selected
- $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
- $questionlist = ''; // comma separated list of ids of questions to be deleted
- $questionnames = ''; // string with names of questions separated by with
- // an asterix in front of those that are in use
- $inuse = false; // set to true if at least one of the questions is in use
- foreach ($rawquestions as $key => $value) { // Parse input for question ids
- if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
- $key = $matches[1];
- $questionlist .= $key.',';
- question_require_capability_on($key, 'edit');
- if ($DB->record_exists('quiz_question_instances', array('question'=>$key))) {
- $questionnames .= '* ';
- $inuse = true;
+ public function __construct() {
+
+ }
+
+ /**
+ * Let the question bank display know whether the quiz has been attempted,
+ * hence whether some bits of UI, like the add this question to the quiz icon,
+ * should be displayed.
+ * @param boolean $quizhasattempts whether the quiz has attempts.
+ */
+ public function set_quiz_has_attempts($quizhasattempts) {
+ $this->quizhasattempts = $quizhasattempts;
+ }
+
+ /**
+ * Shows the question bank editing interface.
+ * A changed copy of the function at question/editlib.php; to be refactored.
+ *
+ * The function also processes a number of actions:
+ *
+ * Actions affecting the question pool:
+ * move Moves a question to a different category
+ * deleteselected Deletes the selected questions from the category
+ * Other actions:
+ * category Chooses the category
+ * displayoptions Sets display options
+ */
+ function display($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder,
+ $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
+ global $COURSE,$DB;
+
+ if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
+ // make a list of all the questions that are selected
+ $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
+ $questionlist = ''; // comma separated list of ids of questions to be deleted
+ $questionnames = ''; // string with names of questions separated by with
+ // an asterix in front of those that are in use
+ $inuse = false; // set to true if at least one of the questions is in use
+ foreach ($rawquestions as $key => $value) { // Parse input for question ids
+ if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
+ $key = $matches[1];
+ $questionlist .= $key.',';
+ question_require_capability_on($key, 'edit');
+ if ($DB->record_exists('quiz_question_instances', array('question'=>$key))) {
+ $questionnames .= '* ';
+ $inuse = true;
+ }
+ $questionnames .= $DB->get_field('question', 'name', array('id'=>$key)).
+ ' ';
}
- $questionnames .= $DB->get_field('question', 'name', array('id'=>$key)).
- ' ';
}
- }
- if (!$questionlist) { // no questions were selected
- redirect($pageurl->out());
- }
- $questionlist = rtrim($questionlist, ',');
+ if (!$questionlist) { // no questions were selected
+ redirect($pageurl->out());
+ }
+ $questionlist = rtrim($questionlist, ',');
- // Add an explanation about questions in use
- if ($inuse) {
- $questionnames .= ' '.get_string('questionsinuse', 'quiz');
+ // Add an explanation about questions in use
+ if ($inuse) {
+ $questionnames .= ' '.get_string('questionsinuse', 'quiz');
+ }
+ notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
+ $pageurl->out_action(array('deleteselected'=>$questionlist,
+ 'confirm'=>md5($questionlist))),
+ $pageurl->out_action());
+ }else{
+ //actual question bank
+ // starts with category selection form
+ list($categoryid, $contextid)= explode(',', $cat);
+
+ if (!$categoryid) {
+ print_box_start('generalbox questionbank');
+ $this->display_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
+ echo "
';
- print_box_start('generalbox questionbank');
+ $catcontext = get_context_instance_by_id($contextid);
+ $canadd = has_capability('moodle/question:add', $catcontext);
+ //check for capabilities on all questions in category, will also apply to sub cats.
+ $caneditall =has_capability('moodle/question:editall', $catcontext);
+ $canuseall =has_capability('moodle/question:useall', $catcontext);
+ $canmoveall =has_capability('moodle/question:moveall', $catcontext);
- quiz_question_category_form($contexts->having_one_edit_tab_cap($tabname),
- $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
- // continues with list of questions
-
- quiz_question_list($contexts->having_one_edit_tab_cap($tabname),
- $pageurl,
- $cat,
- isset($cm) ? $cm : null,
- $recurse,
- $page,
- $perpage,
- $showhidden,
- $sortorder,
- $sortorderdecoded,
- $showquestiontext,
- $contexts->having_cap('moodle/question:add'),
- $cmoptions);
-
- echo '';
-
- print_box_end();
- }
-
-}
-/**
- * prints a form to choose categories
- * A changed copy of the function at question/editlib.php; to be refactored.
- *
- */
-function quiz_question_category_form($contexts, $pageurl, $current, $recurse=1,
- $showhidden=false, $showquestiontext=false) {
- global $CFG;
-
-/// Get all the existing categories now
- $catmenu = question_category_options($contexts, false, 0, true);
-
- $strcategory = get_string('category', 'quiz');
- $strselectcategory = get_string('selectcategory', 'quiz');
- $strshow = get_string('show', 'quiz');
- $streditcats = get_string('editcategories', 'quiz');
-
- popup_form ('edit.php?'.$pageurl->get_query_string().'&category=',
- $catmenu, 'catmenu', $current, '', '', '', false, 'self',
- $strselectcategory.":");
-}
+ if ($cm AND $cm->modname == 'quiz') {
+ $quizid = $cm->instance;
+ } else {
+ $quizid = 0;
+ }
+ //create the url of the new question page to forward to. return url is given
+ //as a parameter and automatically urlencoded.
-/**
-* Prints the table of questions in a category with interactions
-* A changed copy of the function at question/editlib.php; to be refactored.
-*
-* @param object $course The course object
-* @param int $categoryid The id of the question category to be displayed
-* @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
-* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
-* @param int $page The number of the page to be displayed
-* @param int $perpage Number of questions to show per page
-* @param boolean $showhidden True if also hidden questions should be displayed
-* @param boolean $showquestiontext whether the text of each question should be shown in the list
-* @param object $cmoptions Options to be passed on to the callbacks called from this function
-*/
-function quiz_question_list($contexts, $pageurl, $categoryandcontext,
- $cm = null, $recurse=1, $page=0, $perpage=100, $showhidden=false,
- $sortorder='typename', $sortorderdecoded='qtype, name ASC',
- $showquestiontext = false, $addcontexts = array(), $cmoptions) {
- global $USER, $CFG, $THEME, $COURSE, $DB;
- list($categoryid, $contextid)= explode(',', $categoryandcontext);
-
- $qtypemenu = question_type_menu();
-
- $strcategory = get_string("category", "quiz");
- $strquestion = get_string("question", "quiz");
- $straddquestions = get_string("addquestions", "quiz");
- $strimportquestions = get_string("importquestions", "quiz");
- $strexportquestions = get_string("exportquestions", "quiz");
- $strnoquestions = get_string("noquestions", "quiz");
- $strselect = get_string("select", "quiz");
- $strselectall = get_string("selectall", "quiz");
- $strselectnone = get_string("selectnone", "quiz");
- $strcreatenewquestion = get_string("createnewquestion", "quiz");
- $strquestion = get_string("question", "quiz");
- $strdelete = get_string("delete");
- $stredit = get_string("edit");
- $strmove = get_string('moveqtoanothercontext', 'question');
- $strview = get_string("view");
- $straction = get_string("action");
- $strrestore = get_string('restore');
-
- $strtype = get_string("type", "quiz");
- $strcreatemultiple = get_string("createmultiple", "quiz");
- $strpreview = get_string("preview","quiz");
-
- if (!$categoryid) {
- echo "
';
diff --git a/question/editlib.php b/question/editlib.php
index 006e92d300d..96ff9698da3 100644
--- a/question/editlib.php
+++ b/question/editlib.php
@@ -1,20 +1,40 @@
libdir.'/questionlib.php');
define('DEFAULT_QUESTIONS_PER_PAGE', 20);
-function get_module_from_cmid($cmid){
+function get_module_from_cmid($cmid) {
global $CFG, $DB;
if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
FROM {course_modules} cm,
@@ -106,491 +126,517 @@ function question_can_delete_cat($todelete) {
require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
}
}
+
/**
- * prints a form to choose categories
+ * This class prints a view of the question bank, including
+ * + Some controls to allow users to to select what is displayed.
+ * + A list of questions as a table.
+ * + Further controls to do things with the questions.
+ *
+ * This class gives a basic view, and provides plenty of hooks where subclasses
+ * can override parts of the display.
+ *
+ * The list of questions presented as a table is generated by creating a list of
+ * question_bank_column objects, one for each 'column' to be displayed. These
+ * manage
+ * + outputting the contents of that column, given a $question object, but also
+ * + generating the right fragments of SQL to ensure the necessary data is present,
+ * and sorted in the right order.
+ * + outputting table headers.
*/
-function question_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
- global $CFG;
+class question_bank_view {
+ public function __construct() {
-
-/// Get all the existing categories now
- $catmenu = question_category_options($contexts, false, 0, true);
-
- $strcategory = get_string('category', 'quiz');
- $strshow = get_string('show', 'quiz');
- $streditcats = get_string('editcategories', 'quiz');
-
- popup_form ('edit.php?'.$pageurl->get_query_string().'&category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "$strcategory");
-
- echo '
';
- echo "
';
-}
-
-/**
- * Private funciton to help the preceeding function.
- */
-function question_category_form_checkbox($name, $checked) {
- echo '
';
- echo '';
- echo '
\n";
-}
-
-/**
-* Prints the table of questions in a category with interactions
-*
-* @param object $course The course object
-* @param int $categoryid The id of the question category to be displayed
-* @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
-* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
-* @param int $page The number of the page to be displayed
-* @param int $perpage Number of questions to show per page
-* @param boolean $showhidden True if also hidden questions should be displayed
-* @param boolean $showquestiontext whether the text of each question should be shown in the list
-*/
-function question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
- $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
- $showquestiontext = false, $addcontexts = array()) {
- global $USER, $CFG, $THEME, $COURSE, $DB;
-
- list($categoryid, $contextid)= explode(',', $categoryandcontext);
-
- $qtypemenu = question_type_menu();
-
- $strcategory = get_string("category", "quiz");
- $strquestion = get_string("question", "quiz");
- $straddquestions = get_string("addquestions", "quiz");
- $strimportquestions = get_string("importquestions", "quiz");
- $strexportquestions = get_string("exportquestions", "quiz");
- $strnoquestions = get_string("noquestionsincategory", "quiz");
- $strselect = get_string("select", "quiz");
- $strselectall = get_string("selectall", "quiz");
- $strselectnone = get_string("selectnone", "quiz");
- $strcreatenewquestion = get_string("createnewquestion", "quiz");
- $strquestionname = get_string("questionname", "quiz");
- $strdelete = get_string("delete");
- $stredit = get_string("edit");
- $strmove = get_string('moveqtoanothercontext', 'question');
- $strview = get_string("view");
- $straction = get_string("action");
- $strrestore = get_string('restore');
-
- $strtype = get_string("type", "quiz");
- $strcreatemultiple = get_string("createmultiple", "quiz");
- $strpreview = get_string("preview","quiz");
-
- if (!$categoryid) {
- echo "
";
- return;
}
- if (!$category = $DB->get_record('question_categories', array('id' => $categoryid, 'contextid' => $contextid))) {
- notify('Category not found!');
- return;
- }
- $catcontext = get_context_instance_by_id($contextid);
- $canadd = has_capability('moodle/question:add', $catcontext);
- //check for capabilities on all questions in category, will also apply to sub cats.
- $caneditall =has_capability('moodle/question:editall', $catcontext);
- $canuseall =has_capability('moodle/question:useall', $catcontext);
- $canmoveall =has_capability('moodle/question:moveall', $catcontext);
+ /**
+ * Shows the question bank editing interface.
+ *
+ * The function also processes a number of actions:
+ *
+ * Actions affecting the question pool:
+ * move Moves a question to a different category
+ * deleteselected Deletes the selected questions from the category
+ * Other actions:
+ * category Chooses the category
+ * displayoptions Sets display options
+ *
+ * @param moodle_url $pageurl object representing this pages url.
+ */
+ function display($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder,
+ $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
+ global $COURSE, $DB;
- if ($cm AND $cm->modname == 'quiz') {
- $quizid = $cm->instance;
- } else {
- $quizid = 0;
- }
- $returnurl = $pageurl->out();
- $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
- array('returnurl' => $returnurl));
- if ($cm!==null){
- $questionurl->param('cmid', $cm->id);
- } else {
- $questionurl->param('courseid', $COURSE->id);
- }
- $questionmoveurl = new moodle_url("$CFG->wwwroot/question/contextmoveq.php",
- array('returnurl' => $returnurl));
- if ($cm!==null){
- $questionmoveurl->param('cmid', $cm->id);
- } else {
- $questionmoveurl->param('courseid', $COURSE->id);
- }
- echo '
';
- $formatoptions = new stdClass;
- $formatoptions->noclean = true;
- echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $COURSE->id);
+ if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
+ // make a list of all the questions that are selected
+ $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
+ $questionlist = ''; // comma separated list of ids of questions to be deleted
+ $questionnames = ''; // string with names of questions separated by with
+ // an asterix in front of those that are in use
+ $inuse = false; // set to true if at least one of the questions is in use
+ foreach ($rawquestions as $key => $value) { // Parse input for question ids
+ if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
+ $key = $matches[1];
+ $questionlist .= $key.',';
+ question_require_capability_on($key, 'edit');
+ if ($DB->record_exists('quiz_question_instances', array('question' => $key))) {
+ $questionnames .= '* ';
+ $inuse = true;
+ }
+ $questionnames .= $DB->get_field('question', 'name', array('id' => $key)).' ';
+ }
+ }
+ if (!$questionlist) { // no questions were selected
+ redirect($pageurl->out());
+ }
+ $questionlist = rtrim($questionlist, ',');
- echo '
';
+ // Add an explanation about questions in use
+ if ($inuse) {
+ $questionnames .= ' '.get_string('questionsinuse', 'quiz');
+ }
+ notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
+ $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
+ $pageurl->out_action());
- if ($canadd) {
- echo '
';
}
- if (!$questions = $DB->get_records_select('question', "category $usql AND parent = '0' $showhidden", $params, $sortorderdecoded, '*', $page*$perpage, $perpage)) {
- // There are no questions on the requested page.
- $page = 0;
- if (!$questions = $DB->get_records_select('question', "category $usql AND parent = '0' $showhidden", $params, $sortorderdecoded, '*', 0, $perpage)) {
- // There are no questions at all
+ /**
+ * Private funciton to help the preceeding function.
+ */
+ protected function display_category_form_checkbox($name, $checked) {
+ echo '
';
+ echo '';
+ echo '
\n";
+ }
+
+ /**
+ * Prints the table of questions in a category with interactions
+ *
+ * @param object $course The course object
+ * @param int $categoryid The id of the question category to be displayed
+ * @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
+ * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
+ * @param int $page The number of the page to be displayed
+ * @param int $perpage Number of questions to show per page
+ * @param boolean $showhidden True if also hidden questions should be displayed
+ * @param boolean $showquestiontext whether the text of each question should be shown in the list
+ */
+ function display_question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
+ $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
+ $showquestiontext = false, $addcontexts = array()) {
+ global $USER, $CFG, $THEME, $COURSE, $DB;
+
+ list($categoryid, $contextid)= explode(',', $categoryandcontext);
+
+ $qtypemenu = question_type_menu();
+
+ $strcategory = get_string("category", "quiz");
+ $strquestion = get_string("question", "quiz");
+ $straddquestions = get_string("addquestions", "quiz");
+ $strimportquestions = get_string("importquestions", "quiz");
+ $strexportquestions = get_string("exportquestions", "quiz");
+ $strnoquestions = get_string("noquestionsincategory", "quiz");
+ $strselect = get_string("select", "quiz");
+ $strselectall = get_string("selectall", "quiz");
+ $strselectnone = get_string("selectnone", "quiz");
+ $strcreatenewquestion = get_string("createnewquestion", "quiz");
+ $strquestionname = get_string("questionname", "quiz");
+ $strdelete = get_string("delete");
+ $stredit = get_string("edit");
+ $strmove = get_string('moveqtoanothercontext', 'question');
+ $strview = get_string("view");
+ $straction = get_string("action");
+ $strrestore = get_string('restore');
+
+ $strtype = get_string("type", "quiz");
+ $strcreatemultiple = get_string("createmultiple", "quiz");
+ $strpreview = get_string("preview","quiz");
+
+ if (!$categoryid) {
+ echo "
\n";
}
- if (optional_param('deleteselected', false, PARAM_BOOL)) { // delete selected questions from the category
- if (($confirm = optional_param('confirm', '', PARAM_ALPHANUM)) and confirm_sesskey()) { // teacher has already confirmed the action
- $deleteselected = required_param('deleteselected');
- if ($confirm == md5($deleteselected)) {
- if ($questionlist = explode(',', $deleteselected)) {
- // for each question either hide it if it is in use or delete it
- foreach ($questionlist as $questionid) {
- question_require_capability_on($questionid, 'edit');
- if ($DB->record_exists('quiz_question_instances', array('question' => $questionid))) {
- if (!$DB->set_field('question', 'hidden', 1, array('id' => $questionid))) {
- question_require_capability_on($questionid, 'edit');
- print_error('cannothidequestion', 'question');
- }
- } else {
- delete_question($questionid);
- }
+ function display_question_sort_options($pageurl, $sortorder){
+ global $USER;
+ //sort options
+ $html = "
";
+ // POST method should only be used for parameters that change data
+ // or if POST method has to be used, the user must be redirected immediately to
+ // non-POSTed page to not break the back button
+ $html .= '
';
+ $html .= '
';
+ $html .= '';
+ $html .= $pageurl->hidden_params_out(array('qsortorder'));
+ //choose_from_menu concatenates the form name with
+ //"menu" so the label is for menuqsortorder
+ $sortoptions = array('alpha' => get_string("qname", "quiz"),
+ 'typealpha' => get_string("qtypename", "quiz"),
+ 'age' => get_string("age", "quiz"));
+ $a = choose_from_menu($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
+ $html .= '';
+ $html .= '';
+ $html .= '
';
+ $html .= "
\n";
+ $html .= "
\n";
+ echo $html;
+ }
+
+ function process_actions($pageurl, $cm){
+ global $CFG, $COURSE, $DB;
+ /// Now, check for commands on this page and modify variables as necessary
+ if (optional_param('move', false, PARAM_BOOL) and confirm_sesskey()) { /// Move selected questions to new category
+ $category = required_param('category', PARAM_SEQUENCE);
+ list($tocategoryid, $contextid) = explode(',', $category);
+ if (! $tocategory = $DB->get_record('question_categories', array('id' => $tocategoryid, 'contextid' => $contextid))) {
+ print_error('cannotfindcate', 'question');
+ }
+ $tocontext = get_context_instance_by_id($contextid);
+ require_capability('moodle/question:add', $tocontext);
+ $rawdata = (array) data_submitted();
+ $questionids = array();
+ foreach ($rawdata as $key => $value) { // Parse input for question ids
+ if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
+ $key = $matches[1];
+ $questionids[] = $key;
+ }
+ }
+ if ($questionids){
+ list($usql, $params) = $DB->get_in_or_equal($questionids);
+ $sql = "SELECT q.*, c.contextid FROM {question} q, {question_categories} c WHERE q.id $usql AND c.id = q.category";
+ if (!$questions = $DB->get_records_sql($sql, $params)){
+ print_error('questiondoesnotexist', 'question', $pageurl->out());
+ }
+ $checkforfiles = false;
+ foreach ($questions as $question){
+ //check capabilities
+ question_require_capability_on($question, 'move');
+ $fromcontext = get_context_instance_by_id($question->contextid);
+ if (get_filesdir_from_context($fromcontext) != get_filesdir_from_context($tocontext)){
+ $checkforfiles = true;
}
}
- redirect($pageurl->out());
- } else {
- print_error('invalidconfirm', 'question');
- }
- }
- }
-
- // Unhide a question
- if(($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
- question_require_capability_on($unhide, 'edit');
- if(!$DB->set_field('question', 'hidden', 0, array('id', $unhide))) {
- print_error('cannotunhidequestion', 'question');
- }
- redirect($pageurl->out());
- }
-}
-
-/**
- * Shows the question bank editing interface.
- *
- * The function also processes a number of actions:
- *
- * Actions affecting the question pool:
- * move Moves a question to a different category
- * deleteselected Deletes the selected questions from the category
- * Other actions:
- * category Chooses the category
- * displayoptions Sets display options
- *
- * @param moodle_url $pageurl object representing this pages url.
- */
-function question_showbank($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
- global $COURSE, $DB;
-
- if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
- // make a list of all the questions that are selected
- $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
- $questionlist = ''; // comma separated list of ids of questions to be deleted
- $questionnames = ''; // string with names of questions separated by with
- // an asterix in front of those that are in use
- $inuse = false; // set to true if at least one of the questions is in use
- foreach ($rawquestions as $key => $value) { // Parse input for question ids
- if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
- $key = $matches[1];
- $questionlist .= $key.',';
- question_require_capability_on($key, 'edit');
- if ($DB->record_exists('quiz_question_instances', array('question' => $key))) {
- $questionnames .= '* ';
- $inuse = true;
+ $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid"));
+ if (!$checkforfiles){
+ if (!question_move_questions_to_category(implode(',', $questionids), $tocategory->id)) {
+ print_error('errormovingquestions', 'question', $returnurl, $questionids);
+ }
+ redirect($returnurl);
+ } else {
+ $movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
+ array('returnurl' => $returnurl,
+ 'ids'=>$questionidlist,
+ 'tocatid'=> $tocategoryid));
+ if ($cm){
+ $movecontexturl->param('cmid', $cm->id);
+ } else {
+ $movecontexturl->param('courseid', $COURSE->id);
+ }
+ redirect($movecontexturl->out());
}
- $questionnames .= $DB->get_field('question', 'name', array('id' => $key)).' ';
}
}
- if (!$questionlist) { // no questions were selected
+
+ if (optional_param('deleteselected', false, PARAM_BOOL)) { // delete selected questions from the category
+ if (($confirm = optional_param('confirm', '', PARAM_ALPHANUM)) and confirm_sesskey()) { // teacher has already confirmed the action
+ $deleteselected = required_param('deleteselected');
+ if ($confirm == md5($deleteselected)) {
+ if ($questionlist = explode(',', $deleteselected)) {
+ // for each question either hide it if it is in use or delete it
+ foreach ($questionlist as $questionid) {
+ question_require_capability_on($questionid, 'edit');
+ if ($DB->record_exists('quiz_question_instances', array('question' => $questionid))) {
+ if (!$DB->set_field('question', 'hidden', 1, array('id' => $questionid))) {
+ question_require_capability_on($questionid, 'edit');
+ print_error('cannothidequestion', 'question');
+ }
+ } else {
+ delete_question($questionid);
+ }
+ }
+ }
+ redirect($pageurl->out());
+ } else {
+ print_error('invalidconfirm', 'question');
+ }
+ }
+ }
+
+ // Unhide a question
+ if(($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
+ question_require_capability_on($unhide, 'edit');
+ if(!$DB->set_field('question', 'hidden', 0, array('id', $unhide))) {
+ print_error('cannotunhidequestion', 'question');
+ }
redirect($pageurl->out());
}
- $questionlist = rtrim($questionlist, ',');
-
- // Add an explanation about questions in use
- if ($inuse) {
- $questionnames .= ' '.get_string('questionsinuse', 'quiz');
- }
- notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
- $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
- $pageurl->out_action());
-
- echo '';
- echo '';
- print_footer($COURSE);
- exit;
}
-
-
- // starts with category selection form
- print_box_start('generalbox questionbank');
- print_heading(get_string('questionbank', 'question'), '', 2);
- question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
-
- // continues with list of questions
- question_list($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, isset($cm) ? $cm : null,
- $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
- $contexts->having_cap('moodle/question:add'));
-
- print_box_end();
}
+
/**
* Common setup for all pages for editing questions.
* @param string $edittab code for this edit tab