From f4b879ddeaaae4246eccbb006481b90ce9dbd86b Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 19 Jan 2009 05:30:01 +0000 Subject: [PATCH] question bank: MDL-17302 refactor question bank display code into a class. Step 1. Just convert the existing functions quiz_question_showbank and question_showbank and friends into methods of a class and subclass. More to come. --- mod/quiz/edit.php | 43 ++- mod/quiz/editlib.php | 829 ++++++++++++++++++++------------------- question/edit.php | 41 +- question/editlib.php | 896 +++++++++++++++++++++++-------------------- 4 files changed, 961 insertions(+), 848 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 3a87d7000c..5421b827c2 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -1,4 +1,28 @@ dirroot.'/mod/quiz/editlib.php'); require_once($CFG->dirroot."/question/category_class.php"); @@ -134,6 +154,7 @@ if ($quiz_qbanktool > -1) { } else { $quiz_qbanktool = get_user_preferences("quiz_qbanktool_open",0); } +$questionbank = new quiz_question_bank_view(); //will be set further down in the code $quiz_has_attempts=false; @@ -543,13 +564,13 @@ if ($significantchangemade) { redirect($thispageurl->out()); } -question_showbank_actions($thispageurl, $cm); +$questionbank->process_actions($thispageurl, $cm); /// all commands have been dealt with, now print the page if (isset($quiz->instance) and $DB->record_exists_select('quiz_attempts', "quiz = ? AND preview = '0'", array($quiz->instance))){ - $quiz_has_attempts=true; + $questionbank->set_quiz_has_attempts(true); } // Print the header. @@ -610,14 +631,12 @@ echo ''; echo '
'; echo '
'; echo '
'; -$cmoptions = new stdClass; -$cmoptions->hasattempts=$quiz_has_attempts; -quiz_question_showbank('editq', $contexts, $thispageurl, $cm, +$questionbank->display('editq', $contexts, $thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'], $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], - $pagevars['showquestiontext'],$cmoptions); + $pagevars['showquestiontext']); echo '
'; echo '
'; echo '
'; diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 4cf30b66f8..4bdd528996 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -1,13 +1,34 @@ '; - + $reordercontrols2top='
'. get_string("moveselectedonpage","quiz", $a) . 'out().'" class="configurerandomquestion">'.get_string("configurerandomquestion","quiz").''; } - + ?>
@@ -759,7 +779,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool $a = new stdClass; $a->arrow = $THEME->rarrow; $strshowcategorycontents=get_string('showcategorycontents','quiz', $a); - + echo '
'; echo '1, @@ -768,7 +788,6 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool echo ''. quiz_question_preview_button($quiz, $question). ''; - echo "
"; @@ -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 } } +/** + * Subclass to customise the view of the question bank for the quiz editing screen. + */ +class quiz_question_bank_view extends question_bank_view { + protected $quizhasattempts = false; + 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 - * - * @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. - */ -function quiz_question_showbank($tabname, $contexts, $pageurl, $cm, - $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, - $showhidden, $showquestiontext, $cmoptions){ - 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; + /** + * 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'); + } + 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_string("selectcategoryabove", "quiz"); + echo "

"; + print_box_end(); + return; + } + + if (!$category = $DB->get_record('question_categories', + array('id' => $categoryid, 'contextid' => $contextid))) { + print_box_start('generalbox questionbank'); + notify('Category not found!'); + print_box_end(); + return; + } + $formatoptions = new stdClass; + $formatoptions->noclean = true; + $strcategory = get_string('category', 'quiz'); + echo '
'. + $strcategory; + echo ': '; + echo shorten_text(strip_tags(format_text($category->name, FORMAT_MOODLE, + $formatoptions, $COURSE->id)),60); + echo '
'; + echo shorten_text(strip_tags(format_text($category->info, FORMAT_MOODLE, + $formatoptions, $COURSE->id)),200); + echo '
'; + + print_box_start('generalbox questionbank'); - // Add an explanation about questions in use - if ($inuse) { - $questionnames .= '
'.get_string('questionsinuse', 'quiz'); + $this->display_category_form($contexts->having_one_edit_tab_cap($tabname), + $pageurl, $cat, $recurse, $showhidden, $showquestiontext); + // continues with list of questions + + $this->display_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')); + + echo '
'; + echo "
"; + echo $pageurl->hidden_params_out(array('recurse', 'showhidden', + 'showquestiontext')); + $this->display_category_form_checkbox('recurse', $recurse); + $this->display_category_form_checkbox('showhidden', $showhidden); + echo '
'; + + print_box_end(); } - 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); + + } + /** + * prints a form to choose categories + * A changed copy of the function at question/editlib.php; to be refactored. + * + */ + function display_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.":"); + } + + /** + * 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 + */ + 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("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) { - print_box_start('generalbox questionbank'); - quiz_question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext); echo "

"; print_string("selectcategoryabove", "quiz"); echo "

"; - print_box_end(); return; } if (!$category = $DB->get_record('question_categories', array('id' => $categoryid, 'contextid' => $contextid))) { - print_box_start('generalbox questionbank'); notify('Category not found!'); - print_box_end(); return; } - $formatoptions = new stdClass; - $formatoptions->noclean = true; - $strcategory = get_string('category', 'quiz'); - echo '
'. - $strcategory; - echo ': '; - echo shorten_text(strip_tags(format_text($category->name, FORMAT_MOODLE, - $formatoptions, $COURSE->id)),60); - echo '
'; - echo shorten_text(strip_tags(format_text($category->info, FORMAT_MOODLE, - $formatoptions, $COURSE->id)),200); - echo '
'; - - print_box_start('generalbox questionbank'); - - 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 '
'; - echo "
"; - echo $pageurl->hidden_params_out(array('recurse', 'showhidden', - 'showquestiontext')); - question_category_form_checkbox('recurse', $recurse); - question_category_form_checkbox('showhidden', $showhidden); - 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.":"); -} - - - -/** -* 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 "

"; - print_string("selectcategoryabove", "quiz"); - 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); + $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); - 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. + 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. - $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); - } + $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); + } - $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id; + $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id; - // hide-feature - $showhidden = $showhidden ? '' : " AND hidden = '0'"; - echo '
'; - if ($canadd) { - popup_form ($questionurl->out(false, array('category' => $category->id)). - '&qtype=', $qtypemenu, "addquestion_$page", "", "choose", "", - "", false, "self", "$strcreatenewquestion"); - helpbutton("questiontypes", $strcreatenewquestion, "quiz"); - } - else { - print_string('nopermissionadd', 'question'); - } - echo '
'; - - - $categorylist_array = explode(',', $categorylist); + // hide-feature + $showhidden = $showhidden ? '' : " AND hidden = '0'"; + echo '
'; + if ($canadd) { + popup_form ($questionurl->out(false, array('category' => $category->id)). + '&qtype=', $qtypemenu, "addquestion_$page", "", "choose", "", + "", false, "self", "$strcreatenewquestion"); + helpbutton("questiontypes", $strcreatenewquestion, "quiz"); + } + else { + print_string('nopermissionadd', 'question'); + } + echo '
'; - list($usql, $params) = $DB->get_in_or_equal($categorylist_array); - if (!$totalnumber = $DB->count_records_select('question', - "category $usql AND parent = '0' $showhidden", $params)) { - echo '
'; - print_string("noquestions", "quiz"); - echo "
"; - return; - } - if (!$questions = $DB->get_records_select('question', - "category $usql AND parent = '0' $showhidden", $params, $sortorderdecoded, - '*', $page*$perpage, $perpage)) { + $categorylist_array = explode(',', $categorylist); - // There are no questions on the requested page. - $page = 0; - if (!$questionsatall = $DB->get_records_select('question', - "category $usql AND parent = '0' $showhidden", $params, $sortorderdecoded, - '*', 0, $perpage)) { - // There are no questions at all + list($usql, $params) = $DB->get_in_or_equal($categorylist_array); + if (!$totalnumber = $DB->count_records_select('question', + "category $usql AND parent = '0' $showhidden", $params)) { echo '
'; print_string("noquestions", "quiz"); echo "
"; return; } - } + 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 (!$questionsatall = $DB->get_records_select('question', + "category $usql AND parent = '0' $showhidden", $params, $sortorderdecoded, + '*', 0, $perpage)) { + // There are no questions at all + echo '
'; + print_string("noquestions", "quiz"); + echo "
"; + return; + } + } - echo '
'; - echo question_sort_options($pageurl, $sortorder); - echo '
'; - echo '
'; - print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage'); - echo '
'; - echo '
'; - echo '
'; - echo ''; - echo $pageurl->hidden_params_out(); - echo '
'; - echo ''; - echo ""; + echo '
'; + $this->display_question_sort_options($pageurl, $sortorder); + echo '
'; + echo '
'; + print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage'); + echo '
'; - echo ""; - echo ""; - echo "\n"; - foreach ($questions as $question) { - $nameclass = ''; - $textclass = ''; - if ($question->hidden) { - $nameclass = 'dimmed_text'; - $textclass = 'dimmed_text'; - } - if ($showquestiontext) { - $nameclass .= ' header'; - } - if ($nameclass) { - $nameclass = 'class="' . $nameclass . '"'; - } - if ($textclass) { - $textclass = 'class="' . $textclass . '"'; - } + echo ''; + echo '
'; + echo ''; + echo $pageurl->hidden_params_out(); + echo '
'; + echo '
$straction$strquestion
'; + echo ""; - echo "\n"; + echo ""; + echo "\n"; + foreach ($questions as $question) { + $nameclass = ''; + $textclass = ''; + if ($question->hidden) { + $nameclass = 'dimmed_text'; + $textclass = 'dimmed_text'; + } + if ($showquestiontext) { + $nameclass .= ' header'; + } + if ($nameclass) { + $nameclass = 'class="' . $nameclass . '"'; + } + if ($textclass) { + $textclass = 'class="' . $textclass . '"'; + } - $canuseq = question_has_capability_on($question, 'use', - $question->category); - if (function_exists('module_specific_actions')) { - echo module_specific_actions($pageurl, $question->id, $cm->id, - $canuseq,$cmoptions); - } + echo "\n\n"; - - echo "\n"; - echo ""; + $canuseq = question_has_capability_on($question, 'use', + $question->category); + if (function_exists('module_specific_actions')) { + $cmoptions = new stdClass; + $cmoptions->hasattempts = $this->quizhasattempts; + echo module_specific_actions($pageurl, $question->id, $cm->id, + $canuseq, $cmoptions); + } - echo "\n"; - if($showquestiontext){ - echo '\n"; - } - } - echo "
$straction
\n"; + echo "$strquestion
\n"; - if ($caneditall || $canmoveall || $canuseall){ - echo "id\" id=\"checkq$question->id\" value=\"1\" />"; - } - echo "
"; - $questionstring=quiz_question_tostring($question,false,true,true); - echo ""; - - - - echo "
"; - // edit, hide, delete question, using question capabilities, not quiz capabilieies - if (question_has_capability_on($question, 'edit', $question->category) || - question_has_capability_on($question, 'move', - $question->category)) { - echo "out(false, - array('id'=>$question->id))."\"> pixpath/t/edit.gif\" alt=\"$stredit\" />"; - } elseif (question_has_capability_on($question, 'view', - $question->category)){ - - echo "out(false, - array('id'=>$question->id))."\">pixpath/i/info.gif\" alt=\"$strview\" />"; - } - // preview - if ($canuseq) { - $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$COURSE->id); - link_to_popup_window('/question/preview.php?id=' . $question->id . - $quizorcourseid, 'questionpreview', - "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />", - 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS); - } - echo "
'; - $formatoptions = new stdClass; - $formatoptions->noclean = true; - $formatoptions->para = false; - echo format_text($question->questiontext, - $question->questiontextformat, - $formatoptions, $COURSE->id); - echo "
\n"; + if ($caneditall || $canmoveall || $canuseall){ + echo "id\" id=\"checkq$question->id\" value=\"1\" />"; + } + echo "\n"; + echo "
"; + $questionstring=quiz_question_tostring($question,false,true,true); + echo ""; - echo '
'; - $paging = print_paging_bar($totalnumber, $page, $perpage, - $pageurl, 'qpage', false, true); - if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) { - if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) { - $showall = ''.get_string('showall', 'moodle', $totalnumber).''; - } else { - $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; - } - if ($paging) { - $paging = substr($paging, 0, strrpos($paging, '
')); - $paging .= "
$showall
"; - } else { - $paging = "
$showall
"; + echo "\n"; + echo ""; + // edit, hide, delete question, using question capabilities, not quiz capabilieies + if (question_has_capability_on($question, 'edit', $question->category) || + question_has_capability_on($question, 'move', + $question->category)) { + echo "out(false, + array('id'=>$question->id))."\"> pixpath/t/edit.gif\" alt=\"$stredit\" />"; + } elseif (question_has_capability_on($question, 'view', + $question->category)){ + + echo "out(false, + array('id'=>$question->id))."\">pixpath/i/info.gif\" alt=\"$strview\" />"; + } + // preview + if ($canuseq) { + $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$COURSE->id); + link_to_popup_window('/question/preview.php?id=' . $question->id . + $quizorcourseid, 'questionpreview', + "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />", + 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS); + } + echo ""; + + echo "\n"; + if($showquestiontext){ + echo ''; + $formatoptions = new stdClass; + $formatoptions->noclean = true; + $formatoptions->para = false; + echo format_text($question->questiontext, + $question->questiontextformat, + $formatoptions, $COURSE->id); + echo "\n"; + } } - } - echo $paging; - echo ''; - echo '
'; - if ($caneditall || $canmoveall || $canuseall){ - echo ''.$strselectall.' /'. - ' '.$strselectnone.''; - echo '
'; - } - echo "
\n"; - echo '
'; - if ($caneditall || $canmoveall || $canuseall){ - echo ' '.get_string('withselected', 'quiz').':
'; - if (function_exists('module_specific_buttons')) { - echo module_specific_buttons($cm->id,$cmoptions); + echo "
\n"; + + + + + echo '
'; + $paging = print_paging_bar($totalnumber, $page, $perpage, + $pageurl, 'qpage', false, true); + if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) { + if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) { + $showall = ''.get_string('showall', 'moodle', $totalnumber).''; + } else { + $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; + } + if ($paging) { + $paging = substr($paging, 0, strrpos($paging, '
')); + $paging .= "
$showall"; + } else { + $paging = "
$showall
"; + } } - // print delete and move selected question - if ($caneditall) { - echo '\n"; + echo $paging; + echo ''; + echo '
'; + if ($caneditall || $canmoveall || $canuseall){ + echo ''.$strselectall.' /'. + ' '.$strselectnone.''; + echo '
'; } - if (function_exists('module_specific_controls') && $canuseall) { - $modulespecific=module_specific_controls($totalnumber, $recurse, $category, - $cm->id,$cmoptions); - if(!empty($modulespecific)){ - echo "
$modulespecific"; + echo "
\n"; + echo '
'; + if ($caneditall || $canmoveall || $canuseall){ + echo ' '.get_string('withselected', 'quiz').':
'; + if (function_exists('module_specific_buttons')) { + echo module_specific_buttons($cm->id,$cmoptions); + } + // print delete and move selected question + if ($caneditall) { + echo '\n"; + } + if (function_exists('module_specific_controls') && $canuseall) { + $cmoptions = new stdClass; + $cmoptions->hasattempts = $this->quizhasattempts; + $modulespecific=module_specific_controls($totalnumber, $recurse, $category, + $cm->id,$cmoptions); + if(!empty($modulespecific)){ + echo "
$modulespecific"; + } } } - } - echo "
\n"; + echo "\n"; - echo '
'; - echo "
\n"; + echo ''; + echo "\n"; + } } - /** * Add an arbitrary element to array at a specified index, pushing the rest * back. @@ -1451,6 +1477,7 @@ function array_add_at($array,$value,$at){ $result=array_merge($beginpart,$endpart); return $result; } + /** * Prints the form for setting a quiz' overall grade * @@ -1478,12 +1505,12 @@ function quiz_print_grading_form($quiz, $pageurl, $tabindex){ echo "\n"; return $tabindex+1; } + /** * Print the status bar * * @param object $quiz The quiz object of the quiz in question */ - function quiz_print_status_bar($quiz){ global $CFG; $numberofquestions=quiz_number_of_questions_in_quiz($quiz->questions); diff --git a/question/edit.php b/question/edit.php index ab82c42fdc..53804e0916 100644 --- a/question/edit.php +++ b/question/edit.php @@ -1,22 +1,43 @@ process_actions($thispageurl, $cm); - question_showbank_actions($thispageurl, $cm); + // TODO log this page view. $context = $contexts->lowest(); $streditingquestions = get_string('editquestions', "quiz"); @@ -48,12 +69,12 @@ include('tabs.php'); } - echo ''; echo ''; echo '
'; - question_showbank('questions', $contexts, $thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'], - $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], $pagevars['showquestiontext']); + $questionbank->display('questions', $contexts, $thispageurl, $cm, $pagevars['qpage'], + $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'], + $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], $pagevars['showquestiontext']); echo '
'; diff --git a/question/editlib.php b/question/editlib.php index 006e92d300..96ff9698da 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() { + + } + /** + * 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 (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, ','); -/// Get all the existing categories now - $catmenu = question_category_options($contexts, false, 0, true); + // 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()); - $strcategory = get_string('category', 'quiz'); - $strshow = get_string('show', 'quiz'); - $streditcats = get_string('editcategories', 'quiz'); + echo ''; + echo ''; + print_footer($COURSE); + exit; + } - popup_form ('edit.php?'.$pageurl->get_query_string().'&category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "$strcategory"); - echo '
'; - echo "
"; - echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext')); - question_category_form_checkbox('recurse', $recurse); - question_category_form_checkbox('showhidden', $showhidden); - question_category_form_checkbox('showquestiontext', $showquestiontext); - echo '
'; -} + // starts with category selection form + print_box_start('generalbox questionbank'); + print_heading(get_string('questionbank', 'question'), '', 2); + $this->display_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext); -/** - * Private funciton to help the preceeding function. - */ -function question_category_form_checkbox($name, $checked) { - echo '
'; - echo ''; - echo '
\n"; -} + // continues with list of questions + $this->display_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')); -/** -* 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 "

"; - print_string("selectcategoryabove", "quiz"); - 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); - - 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); + print_box_end(); } - echo '
'; - $formatoptions = new stdClass; - $formatoptions->noclean = true; - echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $COURSE->id); - echo ''; + /** + * prints a form to choose categories + */ + function display_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) { + global $CFG; + - if ($canadd) { - echo ''; - } - else { - echo ''; - } + /// Get all the existing categories now + $catmenu = question_category_options($contexts, false, 0, true); - echo '
'; - popup_form ($questionurl->out(false, array('category' => $category->id)).'&qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "$strcreatenewquestion"); - echo ''; - helpbutton("questiontypes", $strcreatenewquestion, "quiz"); - echo ''; - print_string('nopermissionadd', 'question'); - echo '
'; - echo '
'; + $strcategory = get_string('category', 'quiz'); + $strshow = get_string('show', 'quiz'); + $streditcats = get_string('editcategories', 'quiz'); - $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id; + popup_form('edit.php?'.$pageurl->get_query_string().'&category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "$strcategory"); - // hide-feature - $showhidden = $showhidden ? '' : " AND hidden = '0'"; - $categorylist_array = explode(',', $categorylist); - list($usql, $params) = $DB->get_in_or_equal($categorylist_array); - if (!$totalnumber = $DB->count_records_select('question', "category $usql AND parent = '0' $showhidden", $params)) { - echo "

"; - echo $strnoquestions; - echo "

"; - return; + echo '
'; + echo "
"; + echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext')); + $this->display_category_form_checkbox('recurse', $recurse); + $this->display_category_form_checkbox('showhidden', $showhidden); + $this->display_category_form_checkbox('showquestiontext', $showquestiontext); + 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 - echo "

"; - echo $strnoquestions; - echo "

"; - return; + /** + * Private funciton to help the preceeding function. + */ + protected function display_category_form_checkbox($name, $checked) { + echo '
'; + echo ''; + echo '
\n"; } - print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage'); - echo question_sort_options($pageurl, $sortorder); - - - echo '
'; - echo '
'; - echo ''; - echo $pageurl->hidden_params_out(); - echo ''; - echo ""; + /** + * 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 "

"; + print_string("selectcategoryabove", "quiz"); + echo "

"; + return; + } - echo " - "; - echo "\n"; - foreach ($questions as $question) { - $nameclass = ''; - $textclass = ''; - if ($question->hidden) { - $nameclass = 'dimmed_text'; - $textclass = 'dimmed_text'; + if (!$category = $DB->get_record('question_categories', array('id' => $categoryid, 'contextid' => $contextid))) { + notify('Category not found!'); + return; } - if ($showquestiontext) { - $nameclass .= ' header'; + $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); + + if ($cm AND $cm->modname == 'quiz') { + $quizid = $cm->instance; + } else { + $quizid = 0; } - if ($nameclass) { - $nameclass = 'class="' . $nameclass . '"'; + $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); } - if ($textclass) { - $textclass = 'class="' . $textclass . '"'; + $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); - echo "
\n\n"; - echo "\n"; - echo "\n"; + print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage'); + $this->display_question_sort_options($pageurl, $sortorder); + + echo ''; + echo '
'; + echo ''; + echo $pageurl->hidden_params_out(); + echo '
$straction$strquestionname$strtype
\n"; + echo ''; - $canuseq = question_has_capability_on($question, 'use', $question->category); - if (function_exists('module_specific_actions')) { - echo module_specific_actions($pageurl, $question->id, $cm->id, $canuseq); + if ($canadd) { + echo ''; } - - // preview - if ($canuseq) { - $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$COURSE->id); - link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview', - "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />", - 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS); - } - // edit, hide, delete question, using question capabilities, not quiz capabilieies - if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) { - echo "out(false, array('id'=>$question->id))."\">pixpath/t/edit.gif\" alt=\"$stredit\" /> "; - } elseif (question_has_capability_on($question, 'view', $question->category)){ - echo "out(false, array('id'=>$question->id))."\">pixpath/i/info.gif\" alt=\"$strview\" /> "; + else { + echo ''; } - if (question_has_capability_on($question, 'move', $question->category) && question_has_capability_on($question, 'view', $question->category)) { - echo "out(false, array('id'=>$question->id, 'movecontext'=>1))."\">pixpath/t/move.gif\" alt=\"$strmove\" /> "; + echo '
'; + popup_form ($questionurl->out(false, array('category' => $category->id)).'&qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "$strcreatenewquestion"); + echo ''; + helpbutton("questiontypes", $strcreatenewquestion, "quiz"); + echo ''; + print_string('nopermissionadd', 'question'); + echo '
'; + echo ''; + + $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id; + + // hide-feature + $showhidden = $showhidden ? '' : " AND hidden = '0'"; + $categorylist_array = explode(',', $categorylist); + list($usql, $params) = $DB->get_in_or_equal($categorylist_array); + if (!$totalnumber = $DB->count_records_select('question', "category $usql AND parent = '0' $showhidden", $params)) { + echo "

"; + echo $strnoquestions; + echo "

"; + return; } - if (question_has_capability_on($question, 'edit', $question->category)) { - // hide-feature - if($question->hidden) { - echo "get_query_string()."&unhide=$question->id&sesskey=".sesskey()."\">pixpath/t/restore.gif\" alt=\"$strrestore\" />"; - } else { - echo "get_query_string()."&deleteselected=$question->id&q$question->id=1\">pixpath/t/delete.gif\" alt=\"$strdelete\" />"; + 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 + echo "

"; + echo $strnoquestions; + echo "

"; + return; } } - if ($caneditall || $canmoveall || $canuseall){ - echo " id\" value=\"1\" />"; - } - echo "
" . format_string($question->name) . "\n"; - print_question_icon($question); - echo "
'; + echo ""; + + echo " + "; echo "\n"; - if($showquestiontext){ - echo '\n"; - } - } - echo "
$straction$strquestionname$strtype
'; - $formatoptions = new stdClass; - $formatoptions->noclean = true; - $formatoptions->para = false; - echo format_text($question->questiontext, $question->questiontextformat, - $formatoptions, $COURSE->id); - echo "
\n"; + foreach ($questions as $question) { + $nameclass = ''; + $textclass = ''; + if ($question->hidden) { + $nameclass = 'dimmed_text'; + $textclass = 'dimmed_text'; + } + if ($showquestiontext) { + $nameclass .= ' header'; + } + if ($nameclass) { + $nameclass = 'class="' . $nameclass . '"'; + } + if ($textclass) { + $textclass = 'class="' . $textclass . '"'; + } - $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true); - if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) { - if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) { - $showall = ''.get_string('showall', 'moodle', $totalnumber).''; - } else { - $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; - } - if ($paging) { - $paging = substr($paging, 0, strrpos($paging, '')); - $paging .= "
$showall"; - } else { - $paging = "
$showall
"; - } - } - echo $paging; + echo "\n\n"; - if ($caneditall || $canmoveall || $canuseall){ - echo ''.$strselectall.' /'. - ' '.$strselectnone.''; - echo '
'; - echo ' '.get_string('withselected', 'quiz').':
'; + $canuseq = question_has_capability_on($question, 'use', $question->category); + if (function_exists('module_specific_actions')) { + echo module_specific_actions($pageurl, $question->id, $cm->id, $canuseq); + } - if (function_exists('module_specific_buttons')) { - echo module_specific_buttons($cm->id); - } - // print delete and move selected question - if ($caneditall) { - echo '\n"; - } - if ($canmoveall && count($addcontexts)) { - echo '\n"; - question_category_select_menu($addcontexts, false, 0, "$category->id,$category->contextid"); - } + // preview + if ($canuseq) { + $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$COURSE->id); + link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview', + "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />", + 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS); + } + // edit, hide, delete question, using question capabilities, not quiz capabilieies + if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) { + echo "out(false, array('id'=>$question->id))."\">pixpath/t/edit.gif\" alt=\"$stredit\" /> "; + } elseif (question_has_capability_on($question, 'view', $question->category)){ + echo "out(false, array('id'=>$question->id))."\">pixpath/i/info.gif\" alt=\"$strview\" /> "; + } - if (function_exists('module_specific_controls') && $canuseall) { - echo module_specific_controls($totalnumber, $recurse, $category, $cm->id); + if (question_has_capability_on($question, 'move', $question->category) && question_has_capability_on($question, 'view', $question->category)) { + echo "out(false, array('id'=>$question->id, 'movecontext'=>1))."\">pixpath/t/move.gif\" alt=\"$strmove\" /> "; + } + + if (question_has_capability_on($question, 'edit', $question->category)) { + // hide-feature + if($question->hidden) { + echo "get_query_string()."&unhide=$question->id&sesskey=".sesskey()."\">pixpath/t/restore.gif\" alt=\"$strrestore\" />"; + } else { + echo "get_query_string()."&deleteselected=$question->id&q$question->id=1\">pixpath/t/delete.gif\" alt=\"$strdelete\" />"; + } + } + if ($caneditall || $canmoveall || $canuseall){ + echo " id\" value=\"1\" />"; + } + echo "\n"; + + echo "" . format_string($question->name) . "\n"; + echo "\n"; + print_question_icon($question); + echo "\n"; + echo "\n"; + if($showquestiontext){ + echo ''; + $formatoptions = new stdClass; + $formatoptions->noclean = true; + $formatoptions->para = false; + echo format_text($question->questiontext, $question->questiontextformat, + $formatoptions, $COURSE->id); + echo "\n"; + } } - } - echo '
'; - echo "
\n"; -} -function 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"; - return $html; -} + echo "\n"; -function question_showbank_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'); + $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true); + if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) { + if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) { + $showall = ''.get_string('showall', 'moodle', $totalnumber).''; + } else { + $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; + } + if ($paging) { + $paging = substr($paging, 0, strrpos($paging, '')); + $paging .= "
$showall"; + } else { + $paging = "
$showall
"; + } } - $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; + echo $paging; + + if ($caneditall || $canmoveall || $canuseall){ + echo ''.$strselectall.' /'. + ' '.$strselectnone.''; + echo '
'; + echo ' '.get_string('withselected', 'quiz').':
'; + + if (function_exists('module_specific_buttons')) { + echo module_specific_buttons($cm->id); + } + // print delete and move selected question + if ($caneditall) { + echo '\n"; + } + if ($canmoveall && count($addcontexts)) { + echo '\n"; + question_category_select_menu($addcontexts, false, 0, "$category->id,$category->contextid"); + } + + if (function_exists('module_specific_controls') && $canuseall) { + echo module_specific_controls($totalnumber, $recurse, $category, $cm->id); } } - 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()); + echo ''; + echo "\n"; + } + + 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'); } - $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; + $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; } } - $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); + 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()); } - 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); + $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; + } + } + $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->param('courseid', $COURSE->id); + $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()); } - redirect($movecontexturl->out()); } } - } - 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'); + 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); } - } else { - delete_question($questionid); } } + redirect($pageurl->out()); + } else { + print_error('invalidconfirm', 'question'); } - 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; - } - $questionnames .= $DB->get_field('question', 'name', array('id' => $key)).'
'; + // 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'); } - } - 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'); - } - 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 -- 2.39.5