From 2a874d65ce8a22a8fbfcc88951beef0324d1116d Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 11 Feb 2009 10:08:07 +0000 Subject: [PATCH] quiz editing: MDL-17455 refactor the editing code to use the locallib functions to print the icons. Also, use the new method in the random question class to get the list of random questions reliably. --- mod/quiz/editlib.php | 304 +++++++++++++----------------------------- mod/quiz/locallib.php | 85 ++++++++---- question/editlib.php | 6 +- 3 files changed, 158 insertions(+), 237 deletions(-) diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 01e037b13b..29c275ee67 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -32,6 +32,8 @@ require_once("locallib.php"); +define('NUM_QS_TO_SHOW_IN_RANDOM', 3); + /** * Delete a question from a quiz * @@ -413,7 +415,6 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\"". " alt=\"$strmovedown\" />"; } - }else{ } if ($allowdelete && question_has_capability_on($question, 'use', $question->category)) { // remove from quiz, not question delete. @@ -442,7 +443,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, ?> -qtype,'random')===0){ +qtype == 'random') { echo ''.get_string("configurerandomquestion","quiz").''; } @@ -471,21 +472,17 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, ?>
qtype,'random')===0){ // it is a random question - if(!$reordertool){ - quiz_print_randomquestion($question, $pageurl, $quiz, - $quiz_qbanktool); - }else{ - quiz_print_randomquestion_reordertool($question, - $pageurl, $quiz); + if ($question->qtype == 'random') { // it is a random question + if (!$reordertool) { + quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool); + } else { + quiz_print_randomquestion_reordertool($question, $pageurl, $quiz); } - }else{ // it is a single question - if(!$reordertool){ - quiz_print_singlequestion($question, $questionurl, $quiz); - }else{ - quiz_print_singlequestion_reordertool($question, - $questionurl, $quiz); + } else { // it is a single question + if (!$reordertool) { + quiz_print_singlequestion($question, $returnurl, $quiz); + } else { + quiz_print_singlequestion_reordertool($question, $returnurl, $quiz); } } ?> @@ -667,80 +664,25 @@ function quiz_process_randomquestion_formdata(&$qcobject){ } -/** - * Print a simple question list of the questions in a question bank category. - * Used for random question display in the edit tab of edit.php - */ -function quiz_simple_question_list($pageurl, $categorylist, $numbertoshow=3, - $showhidden=false, $sortorderdecoded='qtype, name ASC', - $showquestiontext = true){ - global $DB; - - // hide-feature - $showhidden = $showhidden ? '' : " AND hidden = '0'"; - $categorylist_array = explode(',', $categorylist); - list($usql, $params) = $DB->get_in_or_equal($categorylist_array); - - if (!$questions = $DB->get_records_select('question', "category $usql AND parent = '0' $showhidden", - $params, $sortorderdecoded, 'qtype,name,questiontext,questiontextformat', 0, $numbertoshow)) { - } - foreach ($questions as $question) { - echo "
  • "; - quiz_question_tostring($question,true, $showquestiontext, false); - echo "
  • "; - } -} /** * Print a given single question in quiz for the edit tab of edit.php. * Meant to be used from quiz_print_question_list() * * @param object $question A question object from the database questions table - * @param object $questionurl The url of the question editing page as a moodle_url object + * @param object $returnurl The url to get back to this page, for example after editing. * @param object $quiz The quiz in the context of which the question is being displayed * */ -function quiz_print_singlequestion(&$question, &$questionurl, &$quiz){ - $stredit = get_string("edit"); - $strview = get_string("view"); - - global $COURSE,$QTYPES,$CFG; - ?> -
    - noclean = false; - $formatoptions->para = false; - $formatoptions->newlines = false; - if (question_has_capability_on($question, 'edit', $question->category) - || question_has_capability_on($question, 'move', - $question->category)) { - echo "out()."\">". - quiz_question_tostring($question,false). - ''. - "pixpath/t/edit.gif\" alt=\"". - get_string("edit")."\" />". - ""; - } - elseif (question_has_capability_on($question, 'view', - $question->category)){ - echo "out(false, array('id'=>$question->id))."\">". - quiz_question_tostring($question,false). - ''. - "pixpath/i/info.gif\" ". - "alt=\"$strview\" />". - ""; - }else{ - quiz_question_tostring($question,false,true,false); - } - echo ''; - $namestr = $QTYPES[$question->qtype]->menu_name(); - print_question_icon($question); - echo " $namestr"; - echo ''. - quiz_question_preview_button($quiz, $question).''; - ?> -
    '; + echo quiz_question_edit_button($quiz->cmid, $question, $returnurl, quiz_question_tostring($question) . ' '); + echo ''; + $namestr = $QTYPES[$question->qtype]->local_name(); + print_question_icon($question); + echo " $namestr"; + echo '' . quiz_question_preview_button($quiz, $question, true) . ''; + echo "
    \n"; } /** * Print a given random question in quiz for the edit tab of edit.php. @@ -752,7 +694,7 @@ function quiz_print_singlequestion(&$question, &$questionurl, &$quiz){ * @param boolean $quiz_qbanktool Indicate to this function if the question bank window open */ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool){ - global $DB, $THEME; + global $DB, $QTYPES, $THEME; check_theme_arrows(); echo '
    '; @@ -763,77 +705,72 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool echo '
    '; print_question_icon($question); - echo " ".get_string("xfromcategory",'quiz',get_string('random','quiz'))."
    "; + echo ' ' . get_string('xfromcategory', 'quiz', get_string('random', 'quiz')) . '
    '; $a = new stdClass; $a->arrow = $THEME->rarrow; - $strshowcategorycontents=get_string('showcategorycontents','quiz', $a); + $strshowcategorycontents = get_string('showcategorycontents', 'quiz', $a); - echo '
    '; - echo '1, - "cat"=>$category->id.','.$category->contextid)). - '" title="'.$strshowcategorycontents.'">'.$category->name.''; - echo ''. - quiz_question_preview_button($quiz, $question). - ''; + $openqbankurl = $pageurl->out(false, array('qbanktool' => 1, + 'cat' => $category->id . ',' . $category->contextid)); + $linkcategorycontents = ' ' . $strshowcategorycontents . ''; - echo "
    "; + echo '
    '; + echo '' . $category->name . ''; + echo '' . quiz_question_preview_button($quiz, $question, true) . ''; + echo '
    '; - $questioncount=$DB->count_records_select('question', - "category IN ($category->id) AND parent = '0' "); + $questionids = $QTYPES['random']->get_usable_questions_from_category( + $category->id, $question->questiontext == '1', '0'); + $questioncount = count($questionids); echo '
    '; - $randomquestionlistsize=3; - if(!$questioncount){ - //No questions in category, give an error plus instructions - //error + if ($questioncount == 0) { + // No questions in category, give an error plus instructions echo ''; - print_string("noquestionsnotinuse", "quiz"); + print_string('noquestionsnotinuse', 'quiz'); echo ''; echo '
    '; - //create link to open question bank - $linkcategorycontents=' 1, - "cat"=>$category->id.','.$category->contextid)). - '">'.$strshowcategorycontents.''; - - // embed the link into the string with instructions + // Embed the link into the string with instructions $a = new stdClass; $a->catname = '' . $category->name . ''; - $a->link = $linkcategorycontents; + $a->link = $linkcategorycontents; echo get_string('addnewquestionsqbank','quiz', $a); - }else{ - //Category has questions, list a sample of them - echo "'; } - echo "
    "; - + echo ''; echo '
    '; - echo "
    "; - - echo ""; + echo ''; + echo ''; } @@ -845,53 +782,15 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool * @param object $questionurl The url of the question editing page as a moodle_url object * @param object $quiz The quiz in the context of which the question is being displayed */ -function quiz_print_singlequestion_reordertool(&$question, &$questionurl, &$quiz){ - $stredit = get_string("edit"); - $strview = get_string("view"); - - global $COURSE,$QTYPES, $CFG; - $reordercheckboxlabel=''; - - ?> -
    - noclean = false; - $formatoptions->para = false; - $formatoptions->newlines = false; - echo $reordercheckboxlabel; - print_question_icon($question); - echo "$reordercheckboxlabelclose "; - $questiontext=strip_tags(format_text($question->questiontext, - FORMAT_MOODLE,$formatoptions, $COURSE->id)); - $editstring=""; - if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) { - echo "$reordercheckboxlabel ". - quiz_question_tostring($question,false). - $reordercheckboxlabelclose; - $editstring="out(false, array('id'=>$question->id)). - "\">pixpath/t/edit.gif\" alt=\"". - $stredit."\" />"; - } elseif (question_has_capability_on($question, 'view', - $question->category)){ - echo "$reordercheckboxlabel". - quiz_question_tostring($question,false). - "$reordercheckboxlabelclose"; - $editstring="out(false, - array('id'=>$question->id))."\">$questionstring pixpath/i/info.gif\" alt=\"$strview\" />"; - }else{ - echo "$reordercheckboxlabel". - quiz_question_tostring($question,false). - "$reordercheckboxlabelclose"; - } - echo ''.$editstring. - quiz_question_preview_button($quiz, $question, false). - ''; - ?> -
    '; + echo ''; + echo '' . + quiz_question_action_icons($quiz, $quiz->cmid, $question, $returnurl) . ''; + echo "\n"; } /** * Print a given random question in quiz for the reordertool tab of edit.php. @@ -903,54 +802,43 @@ function quiz_print_singlequestion_reordertool(&$question, &$questionurl, &$quiz */ function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz){ - global $CFG,$DB; - $stredit = get_string("edit"); - $strview = get_string("view"); - - echo '
    '; + global $DB; + // Load the category, and the number of available questions in it. if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) { notify('Random question category not found!'); return; } - echo '
    '; - $url=$pageurl->out(false,array("qbanktool"=>1, "cat"=>$category->id.','. - $category->contextid)); - $reordercheckboxlabel=''; + $questioncount = count($QTYPES['random']->get_usable_questions_from_category( + $category->id, $question->questiontext == '1', '0')); + $reordercheckboxlabel = ''; + + echo '
    '; + echo '
    '; echo $reordercheckboxlabel; print_question_icon($question); - $questioncount=$DB->count_records_select('question', - "category IN ($category->id) AND parent = '0' "); - $randomquestionlistsize=3; - if(!$questioncount){ + if ($questioncount == 0) { echo ''; - print_string("empty", "quiz"); + print_string('empty', 'quiz'); echo ' '; } - print_string('random','quiz'); + print_string('random', 'quiz'); echo ": $reordercheckboxlabelclose
    "; echo '
    '; - echo ''.$reordercheckboxlabel.$category->name. - $reordercheckboxlabelclose.''; + echo $reordercheckboxlabel . $category->name . $reordercheckboxlabelclose; echo ''; - echo quiz_question_preview_button($quiz, $question,false); - + echo quiz_question_preview_button($quiz, $question, false); echo ''; echo "
    "; - echo '
    '; - echo "
    "; - - echo "
    "; + echo '
    '; + echo '
    '; } /** @@ -1015,9 +903,9 @@ class question_bank_add_to_quiz_action_column extends question_bank_action_colum protected function display_content($question, $rowclasses) { // for RTL languages: switch right and left arrows if (right_to_left()) { - $movearrow = 'removeright.gif'; + $movearrow = 't/removeright.gif'; } else { - $movearrow = 'moveleft.gif'; + $movearrow = 't/moveleft.gif'; } $this->print_icon($movearrow, $this->stradd, $this->qbank->add_to_quiz_url($question->id)); } diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 3770cb66f1..ea7c8d4157 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -735,58 +735,91 @@ function quiz_upgrade_states($attempt) { } } } + /** - * Function that can be used in various parts of the quiz code. - * @param object $quiz - * @param integer $cmid - * @param object $question + * @param object $quiz the quiz. + * @param integer $cmid the course_module object for this quiz. + * @param object $question the question. * @param string $returnurl url to return to after action is done. * @return string html for a number of icons linked to action pages for a * question - preview and edit / view icons depending on user capabilities. */ -function quiz_question_action_icons($quiz, $cmid, $question, $returnurl){ +function quiz_question_action_icons($quiz, $cmid, $question, $returnurl) { + $html = quiz_question_preview_button($quiz, $question) . ' ' . + quiz_question_edit_button($cmid, $question, $returnurl); + return $html; +} + +/** + * @param integer $cmid the course_module.id for this quiz. + * @param object $question the question. + * @param string $returnurl url to return to after action is done. + * @param string $contentbeforeicon some HTML content to be added inside the link, before the icon. + * @return the HTML for an edit icon, view icon, or nothing for a question (depending on permissions). + */ +function quiz_question_edit_button($cmid, $question, $returnurl, $contentbeforeicon = '') { global $CFG; + + // Minor efficiency saving. Only get strings once, even if there are a lot of icons on one page. static $stredit = null; static $strview = null; if ($stredit === null){ $stredit = get_string('edit'); $strview = get_string('view'); } - $html =''; - $html .= quiz_question_preview_button($quiz, $question); - $questionparams = array('returnurl' => $returnurl, 'cmid'=>$cmid, 'id' => $question->id); - $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams); - if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) { - $html .= "out()."\"> - pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" />"; - } elseif (question_has_capability_on($question, 'view', $question->category)){ - $html .= "out(false, array('id'=>$question->id))."\">pixpath/i/info.gif\" alt=\"$strview\" /> "; + + // What sort of icon should we show? + $action = ''; + if (question_has_capability_on($question, 'edit', $question->category) || + question_has_capability_on($question, 'move', $question->category)) { + $action = $stredit; + $icon = '/t/edit'; + } else if (question_has_capability_on($question, 'view', $question->category)) { + $action = $strview; + $icon = '/i/info'; + } + + // Build the icon. + if ($action) { + $questionparams = array('returnurl' => $returnurl, 'cmid' => $cmid, 'id' => $question->id); + $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams); + return '' . $contentbeforeicon . + '' . $action . ''; + } else { + return $contentbeforeicon; } - return $html; } - /** * @param object $quiz the quiz * @param object $question the question * @param boolean $label if true, show the previewquestion label after the icon * @return the HTML for a preview question icon. */ -function quiz_question_preview_button($quiz, $question, $label=true) { +function quiz_question_preview_button($quiz, $question, $label = false) { global $CFG, $COURSE; - if (!question_has_capability_on($question, 'use', $question->category)){ + if (!question_has_capability_on($question, 'use', $question->category)) { return ''; } - $strpreview = get_string('previewquestion', 'quiz'); + + // Minor efficiency saving. Only get strings once, even if there are a lot of icons on one page. + static $strpreview = null; + static $strpreviewquestion = null; + if ($strpreview === null){ + $strpreview = get_string('preview', 'quiz'); + $strpreviewquestion = get_string('previewquestion', 'quiz'); + } + + // Do we want a label? $strpreviewlabel=""; - if($label){ - $strpreviewlabel = get_string('preview', 'quiz'); + if ($label) { + $strpreviewlabel = $strpreview; } - $quizorcourseid = $quiz->id?('&quizid=' . $quiz->id):('&courseid=' .$COURSE->id); - return link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview', - "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />$strpreviewlabel", - 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS, true); + + // Build the icon. + return link_to_popup_window('/question/preview.php?id=' . $question->id . '&quizid=' . $quiz->id, 'questionpreview', + "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreviewquestion\" /> $strpreviewlabel", + 0, 0, $strpreviewquestion, QUESTION_PREVIEW_POPUP_OPTIONS, true); } /** diff --git a/question/editlib.php b/question/editlib.php index f35046d695..b45eed6d40 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -575,7 +575,7 @@ abstract class question_bank_action_column_base extends question_bank_column_bas protected function print_icon($icon, $title, $url) { global $CFG; echo ' - ' . $title . ''; + ' . $title . ''; } public function get_required_fields() { @@ -600,9 +600,9 @@ class question_bank_edit_action_column extends question_bank_action_column_base protected function display_content($question, $rowclasses) { if (question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'move')) { - $this->print_icon('edit', $this->stredit, $this->qbank->edit_question_url($question->id)); + $this->print_icon('t/edit.gif', $this->stredit, $this->qbank->edit_question_url($question->id)); } else { - $this->print_icon('info', $this->strview, $this->qbank->edit_question_url($question->id)); + $this->print_icon('i/info.gif', $this->strview, $this->qbank->edit_question_url($question->id)); } } } -- 2.39.5