From: tjhunt Date: Thu, 22 Jan 2009 05:38:18 +0000 (+0000) Subject: question bank: MDL-17871 Kill most of the duplicated code between mod/quiz/editlib... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=971c6fca062226201a5b14e8016c52fddc4d475d;p=moodle.git question bank: MDL-17871 Kill most of the duplicated code between mod/quiz/editlib.php and question/editlib.php --- diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 18920133e9..bb60aab447 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -52,31 +52,6 @@ require_once($CFG->dirroot.'/mod/quiz/editlib.php'); require_once($CFG->dirroot."/question/category_class.php"); require_js(array('yui_yahoo','yui_dom-event','yui_container','yui_dragdrop')); -/** - * Callback function called from question_list() function (which is called - * from showbank()) - * Displays action icon as first action for each question. - */ -function module_specific_actions($pageurl, $questionid, $cmid, $canuse, $cmoptions){ - global $CFG; - if ($canuse && !$cmoptions->hasattempts){ - // for RTL languages: switch right and left arrows /****/ - if (right_to_left()) { - $movearrow = 'removeright.gif'; - } else { - $movearrow = 'moveleft.gif'; - } - $straddtoquiz = get_string("addtoquiz", "quiz"); - $out = "get_query_string(). - "&addquestion=$questionid&sesskey=". - sesskey()."\">pixpath/t/$movearrow\" - alt=\"$straddtoquiz\" /> "; - return $out; - } else { - return ''; - } -} /** * Callback function called from question_list() function * (which is called from showbank()) @@ -154,7 +129,6 @@ if ($quiz_qbanktool > -1) { } else { $quiz_qbanktool = get_user_preferences("quiz_qbanktool_open",0); } -$questionbank = new quiz_question_bank_view($contexts, $thispageurl, $cm); //will be set further down in the code $quiz_has_attempts=false; @@ -180,6 +154,8 @@ if (! $course = $DB->get_record('course', array('id' => $quiz->course))) { print_error('invalidcourseid', 'error'); } +$questionbank = new quiz_question_bank_view($contexts, $thispageurl, $course, $cm); + // Log this visit. add_to_log($cm->course, 'quiz', 'editquestions', "view.php?id=$cm->id", "$quiz->id", $cm->id); diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 3e5c93158a..8e251c6374 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -1008,14 +1008,79 @@ function quiz_question_tostring(&$question,$showicon=false,$showquestiontext=tru } } +/** + * A column type for the add this question to the quiz. + */ +class question_bank_add_to_quiz_action_column extends question_bank_action_column_base { + protected $stradd; + + public function init() { + parent::init(); + $this->stradd = get_string('addtoquiz', 'quiz'); + } + + public function get_name() { + return 'addtoquizaction'; + } + + protected function display_content($question, $rowclasses) { + // for RTL languages: switch right and left arrows + if (right_to_left()) { + $movearrow = 'removeright.gif'; + } else { + $movearrow = 'moveleft.gif'; + } + $this->print_icon($movearrow, $this->stradd, $this->qbank->add_to_quiz_url($question->id)); + } + + public function get_required_fields() { + return array('q.id'); + } +} + +/** + * A column type for the name followed by the start of the question text. + */ +class question_bank_question_name_text_column extends question_bank_question_name_column { + public function get_name() { + return 'questionnametext'; + } + + protected function display_content($question, $rowclasses) { + echo '
'; + $labelfor = $this->label_for($question); + if ($labelfor) { + echo ''; + } + echo '
'; + } + + public function get_required_fields() { + $fields = parent::get_required_fields(); + $fields[] = 'q.questiontext'; + return $fields; + } +} + /** * 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($contexts, $pageurl, $cm = null) { - parent::__construct($contexts, $pageurl, $cm); + protected function know_field_types() { + $types = parent::know_field_types(); + $types[] = new question_bank_add_to_quiz_action_column($this); + $types[] = new question_bank_question_name_text_column($this); + return $types; + } + + protected function wanted_columns() { + return array('addtoquizaction', 'checkbox', 'qtype', 'questionnametext', 'editaction', 'previewaction'); } /** @@ -1026,525 +1091,76 @@ class quiz_question_bank_view extends question_bank_view { */ public function set_quiz_has_attempts($quizhasattempts) { $this->quizhasattempts = $quizhasattempts; + if ($quizhasattempts && isset($this->visiblecolumns['addtoquizaction'])) { + unset($this->visiblecolumns['addtoquizaction']); + } + } + + public function add_to_quiz_url($questionid) { + global $CFG; + return $CFG->wwwroot . '/mod/quiz/edit.php?' . $pageurl->get_query_string() . + '&addquestion=' . $questionid . '&sesskey=' . sesskey(); } - /** - * 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 - */ public function display($tabname, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){ - global $COURSE, $DB; if ($this->process_actions_needing_ui()) { return; } - // 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(); + // Display the current category. + if (!$category = $this->get_current_category($cat)) { return; } - - if (!$category = $DB->get_record('question_categories', - array('id' => $categoryid, 'contextid' => $contextid))) { - print_box_start('generalbox questionbank'); - notify('Category not found!'); - 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 '
'; + $this->print_category_info($category); print_box_start('generalbox questionbank'); $this->display_category_form($this->contexts->having_one_edit_tab_cap($tabname), - $this->baseurl, $cat, $recurse, $showhidden, $showquestiontext); + $this->baseurl, $cat); // continues with list of questions $this->display_question_list($this->contexts->having_one_edit_tab_cap($tabname), $this->baseurl, $cat, $this->cm, $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext, $this->contexts->having_cap('moodle/question:add')); - echo '
'; - echo "
"; - echo $this->baseurl->hidden_params_out(array('recurse', 'showhidden', - 'showquestiontext')); - $this->display_category_form_checkbox('recurse', $recurse); - $this->display_category_form_checkbox('showhidden', $showhidden); - echo '
'; - + $this->display_options($recurse, $showhidden, $showquestiontext); print_box_end(); } - /** - * prints a form to choose categories - */ - protected 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 - * - * @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 - */ - protected 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 $CFG, $COURSE, $DB; - - list($categoryid, $contextid)= explode(',', $categoryandcontext); - - $cmoptions = new stdClass; - $cmoptions->hasattempts = $this->quizhasattempts; - - $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); - - if ($cm AND $cm->modname == 'quiz') { - $quizid = $cm->instance; - } else { - $quizid = 0; - } - - // Create the url of the new question page to forward to. - $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 '
'; - if ($canadd) { - popup_form($questionurl->out(false, array('category' => $category->id)). - '&qtype=', $qtypemenu, "addquestion", "", "choose", "", - "", false, "self", "$strcreatenewquestion"); - helpbutton("questiontypes", $strcreatenewquestion, "quiz"); - } else { - print_string('nopermissionadd', 'question'); - } - echo '
'; - - $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id; - $categorylist_array = explode(',', $categorylist); - - $showhidden = $showhidden ? '' : " AND hidden = '0'"; - - 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 (!$questions = $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 '
'; - $this->display_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 ""; - 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 "\n\n"; - - echo "\n"; - - echo "\n"; - - echo "\n"; - if($showquestiontext){ - echo '\n"; - } - } - echo "
$straction$strquestion
\n"; - - $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); - } - - if ($caneditall || $canmoveall || $canuseall){ - echo "id\" id=\"checkq$question->id\" value=\"1\" />"; - } - echo "
"; - $questionstring = quiz_question_tostring($question,false,true,true); - echo ""; - echo "
\n"; - - // edit, hide, delete question, using question capabilities, not quiz capabilities - 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"; - - 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 $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); - } - - // print delete and move selected question - if ($caneditall) { - echo '\n"; - } - - 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 '
'; - echo "
\n"; - } - - protected function display_question_sort_options($pageurl, $sortorder){ - //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; + protected function print_choose_category_message($categoryandcontext) { + print_box_start('generalbox questionbank'); + $this->display_category_form($this->contexts->having_one_edit_tab_cap('edit'), $this->baseurl, $categoryandcontext); + echo "

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

"; + print_box_end(); } - public function process_actions() { - 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; - } - } - $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()); - } - } - } - - 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()); - } + protected function print_category_info($category) { + $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, $this->course->id)),60); + echo '
'; + echo shorten_text(strip_tags(format_text($category->info, FORMAT_MOODLE, + $formatoptions, $this->course->id)),200); + echo '
'; } - public function process_actions_needing_ui() { - if (optional_param('deleteselected', false, PARAM_BOOL)) { - // 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($this->baseurl->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()); - - return true; - } + protected function display_options($recurse = 1, $showhidden = false, $showquestiontext = false) { + echo '
'; + echo "
"; + echo $this->baseurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext')); + $this->display_category_form_checkbox('recurse', $recurse); + $this->display_category_form_checkbox('showhidden', $showhidden); + echo '
'; } } diff --git a/question/edit.php b/question/edit.php index de3bf6e9f5..13c21e57a3 100644 --- a/question/edit.php +++ b/question/edit.php @@ -34,7 +34,7 @@ require_once("editlib.php"); list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('questions'); - $questionbank = new question_bank_view($contexts, $thispageurl, $cm); + $questionbank = new question_bank_view($contexts, $thispageurl, $COURSE, $cm); $questionbank->process_actions(); // TODO log this page view. diff --git a/question/editlib.php b/question/editlib.php index 38fd056f7c..1eaaadbbe7 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -184,9 +184,7 @@ abstract class question_bank_column_base { * @param object $question the row from the $question table, augmented with extra information. * @param string $rowclasses CSS class names that should be applied to this row of output. */ - abstract protected function get_title() { - return ''; - } + abstract protected function get_title(); /** * @return string a fuller version of the name. Use this when get_title() returns @@ -416,6 +414,8 @@ class question_bank_question_type_column extends question_bank_column_base { * A column type for the name of the question name. */ class question_bank_question_name_column extends question_bank_column_base { + protected $checkboxespresent = null; + public function get_name() { return 'questionname'; } @@ -424,12 +424,30 @@ class question_bank_question_name_column extends question_bank_column_base { return get_string('question'); } + protected function label_for($question) { + if (is_null($this->checkboxespresent)) { + $this->checkboxespresent = $this->qbank->has_column('checkbox'); + } + if ($this->checkboxespresent) { + return 'checkq' . $question->id; + } else { + return ''; + } + } + protected function display_content($question, $rowclasses) { + $labelfor = $this->label_for($question); + if ($labelfor) { + echo ''; + } } public function get_required_fields() { - return array('q.name'); + return array('q.id', 'q.name'); } public function is_sortable() { @@ -655,6 +673,7 @@ class question_bank_view { protected $quizorcourseid; protected $contexts; protected $cm; + protected $course; protected $knowncolumntypes; protected $visiblecolumns; protected $requiredcolumns; @@ -662,17 +681,18 @@ class question_bank_view { protected $countsql; protected $loadsql; - public function __construct($contexts, $pageurl, $cm = null) { - global $CFG, $COURSE; + public function __construct($contexts, $pageurl, $course, $cm = null) { + global $CFG; $this->contexts = $contexts; $this->baseurl = $pageurl; + $this->course = $course; $this->cm = $cm; if (!empty($cm) && $cm->modname == 'quiz') { $this->quizorcourseid = '&quizid=' . $cm->instance; } else { - $this->quizorcourseid = '&courseid=' .$COURSE->id; + $this->quizorcourseid = '&courseid=' .$this->course->id; } // Create the url of the new question page to forward to. @@ -681,17 +701,21 @@ class question_bank_view { if ($cm !== null){ $this->editquestionurl->param('cmid', $cm->id); } else { - $this->editquestionurl->param('courseid', $COURSE->id); + $this->editquestionurl->param('courseid', $this->course->id); } $this->init_column_types(); - $this->init_columns(array('checkbox', 'qtype', 'questionname', 'creatorname', - 'modifiername', 'editaction', 'previewaction', 'moveaction', 'deleteaction')); + $this->init_columns($this->wanted_columns()); $this->init_sort(); } - protected function init_column_types() { - $types = array( + protected function wanted_columns() { + return array('checkbox', 'qtype', 'questionname', 'creatorname', + 'modifiername', 'editaction', 'previewaction', 'moveaction', 'deleteaction'); + } + + protected function know_field_types() { + return array( new question_bank_checkbox_column($this), new question_bank_question_type_column($this), new question_bank_question_name_column($this), @@ -702,8 +726,11 @@ class question_bank_view { new question_bank_move_action_column($this), new question_bank_delete_action_column($this), ); + } + + protected function init_column_types() { $this->knowncolumntypes = array(); - foreach ($types as $col) { + foreach ($this->know_field_types() as $col) { $this->knowncolumntypes[$col->get_name()] = $col; } } @@ -843,7 +870,6 @@ class question_bank_view { */ public function display($tabname, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){ - global $COURSE, $DB; if ($this->process_actions_needing_ui()) { return; @@ -854,7 +880,13 @@ class question_bank_view { print_heading(get_string('questionbank', 'question'), '', 2); $this->display_category_form($this->contexts->having_one_edit_tab_cap($tabname), - $this->baseurl, $cat, $recurse, $showhidden, $showquestiontext); + $this->baseurl, $cat); + $this->display_options($recurse, $showhidden, $showquestiontext); + + if (!$category = $this->get_current_category($cat)) { + return; + } + $this->print_category_info($category); // continues with list of questions $this->display_question_list($this->contexts->having_one_edit_tab_cap($tabname), $this->baseurl, $cat, $this->cm, @@ -864,27 +896,61 @@ class question_bank_view { print_box_end(); } + protected function print_choose_category_message($categoryandcontext) { + echo "

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

"; + } + + protected function get_current_category($categoryandcontext) { + global $DB; + list($categoryid, $contextid) = explode(',', $categoryandcontext); + if (!$categoryid) { + $this->print_choose_category_message($categoryandcontext); + return false; + } + + 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 false; + } + + return $category; + } + + protected function print_category_info($category) { + $formatoptions = new stdClass; + $formatoptions->noclean = true; + echo '
'; + echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $this->course->id); + echo "
\n"; + } + /** * prints a form to choose categories */ - protected function display_category_form($contexts, $pageurl, $current, $recurse=1, - $showhidden=false, $showquestiontext=false) { + protected function display_category_form($contexts, $pageurl, $current) { global $CFG; /// Get all the existing categories now $catmenu = question_category_options($contexts, false, 0, true); - $strcategory = get_string('category', 'quiz'); + $strcategory = 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', - "$strcategory"); + $strcategory); + } + protected function display_options($recurse = 1, $showhidden = false, $showquestiontext = false) { echo '
'; echo "
"; - echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext')); + echo $this->baseurl->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); @@ -893,7 +959,7 @@ class question_bank_view { } /** - * Private funciton to help the preceeding function. + * Print a single option checkbox. Used by the preceeding. */ protected function display_category_form_checkbox($name, $checked) { echo '
'; @@ -923,10 +989,13 @@ class question_bank_view { $cm = null, $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC', $showquestiontext = false, $addcontexts = array()) { - global $CFG, $COURSE, $DB; + global $CFG, $DB; list($categoryid, $contextid)= explode(',', $categoryandcontext); + $cmoptions = new stdClass; + $cmoptions->hasattempts = !empty($this->quizhasattempts); + $qtypemenu = question_type_menu(); $strcategory = get_string("category", "quiz"); @@ -984,22 +1053,16 @@ class question_bank_view { if ($cm!==null){ $questionurl->param('cmid', $cm->id); } else { - $questionurl->param('courseid', $COURSE->id); + $questionurl->param('courseid', $this->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); + $questionmoveurl->param('courseid', $this->course->id); } - echo '
'; - $formatoptions = new stdClass; - $formatoptions->noclean = true; - echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $COURSE->id); - echo "
\n"; - echo '
'; if ($canadd) { popup_form($questionurl->out(false, array('category' => $category->id)). @@ -1082,7 +1145,7 @@ class question_bank_view { $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); + echo module_specific_actions($pageurl, $question->id, $this->cm->id, $canuseq, $cmoptions); } if ($caneditall || $canmoveall || $canuseall){ @@ -1109,7 +1172,7 @@ class question_bank_view { // preview if ($canuseq) { - $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$COURSE->id); + $quizorcourseid = $quizid?('&quizid=' . $quizid):('&courseid=' .$this->course->id); link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview', " pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />", @@ -1140,7 +1203,7 @@ class question_bank_view { $formatoptions->noclean = true; $formatoptions->para = false; echo format_text($question->questiontext, $question->questiontextformat, - $formatoptions, $COURSE->id); + $formatoptions, $this->course->id); echo "\n"; } } @@ -1177,7 +1240,7 @@ class question_bank_view { echo ' '.get_string('withselected', 'quiz').':
'; if (function_exists('module_specific_buttons')) { - echo module_specific_buttons($cm->id); + echo module_specific_buttons($this->cm->id,$cmoptions); } // print delete and move selected question @@ -1191,7 +1254,7 @@ class question_bank_view { } if (function_exists('module_specific_controls') && $canuseall) { - $modulespecific = module_specific_controls($totalnumber, $recurse, $category, $cm->id); + $modulespecific = module_specific_controls($totalnumber, $recurse, $category, $this->cm->id,$cmoptions); if(!empty($modulespecific)){ echo "
$modulespecific"; } @@ -1228,7 +1291,7 @@ class question_bank_view { } public function process_actions() { - global $CFG, $COURSE, $DB; + global $CFG, $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); @@ -1275,7 +1338,7 @@ class question_bank_view { if ($cm){ $movecontexturl->param('cmid', $cm->id); } else { - $movecontexturl->param('courseid', $COURSE->id); + $movecontexturl->param('courseid', $this->course->id); } redirect($movecontexturl->out()); } @@ -1363,7 +1426,7 @@ class question_bank_view { * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars */ function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true){ - global $COURSE, $QUESTION_EDITTABCAPS, $DB; + global $QUESTION_EDITTABCAPS, $DB; //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array //of parameters that are passed from page to page.