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.
This commit is contained in:
tjhunt
2009-02-11 10:08:07 +00:00
parent 9137e95f82
commit 2a874d65ce
3 changed files with 161 additions and 240 deletions
+97 -209
View File
@@ -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\" /></a>";
}
}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,
?>
<input type="submit" class="pointssubmitbutton" value="<?php echo $strsave; ?>" />
</fieldset>
<?php if(strcmp($question->qtype,'random')===0){
<?php if ($question->qtype == 'random') {
echo '<a href="'.$questionurl->out().'" class="configurerandomquestion">'.get_string("configurerandomquestion","quiz").'</a>';
}
@@ -471,21 +472,17 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true,
?>
<div class="questioncontentcontainer">
<?php
//strcmp returns 0 if equal
if (strcmp($question->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 "<li>";
quiz_question_tostring($question,true, $showquestiontext, false);
echo "</li>";
}
}
/**
* 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;
?>
<div class="singlequestion">
<?php
$formatoptions = new stdClass;
$formatoptions->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 "<a title=\"$stredit\" href=\"".$questionurl->out()."\">".
quiz_question_tostring($question,false).
'<span class="editicon">'.
"<img src=\"$CFG->pixpath/t/edit.gif\" alt=\"".
get_string("edit")."\" /></span>".
"</a>";
}
elseif (question_has_capability_on($question, 'view',
$question->category)){
echo "<a title=\"$strview\" href=\"".
$questionurl->out(false, array('id'=>$question->id))."\">".
quiz_question_tostring($question,false).
'<span class="editicon">'.
"<img src=\"$CFG->pixpath/i/info.gif\" ".
"alt=\"$strview\" /></span>".
"</a>";
}else{
quiz_question_tostring($question,false,true,false);
}
echo '<span class="questiontype">';
$namestr = $QTYPES[$question->qtype]->menu_name();
print_question_icon($question);
echo " $namestr</span>";
echo '<span class="questionpreview">'.
quiz_question_preview_button($quiz, $question).'</span>';
?>
</div><?php
function quiz_print_singlequestion($question, $returnurl, $quiz){
global $QTYPES;
echo '<div class="singlequestion">';
echo quiz_question_edit_button($quiz->cmid, $question, $returnurl, quiz_question_tostring($question) . ' ');
echo '<span class="questiontype">';
$namestr = $QTYPES[$question->qtype]->local_name();
print_question_icon($question);
echo " $namestr</span>";
echo '<span class="questionpreview">' . quiz_question_preview_button($quiz, $question, true) . '</span>';
echo "</div>\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 '<div class="quiz_randomquestion">';
@@ -763,77 +705,72 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool
echo '<div class="randomquestionfromcategory">';
print_question_icon($question);
echo " ".get_string("xfromcategory",'quiz',get_string('random','quiz'))."</div>";
echo ' ' . get_string('xfromcategory', 'quiz', get_string('random', 'quiz')) . '</div>';
$a = new stdClass;
$a->arrow = $THEME->rarrow;
$strshowcategorycontents=get_string('showcategorycontents','quiz', $a);
$strshowcategorycontents = get_string('showcategorycontents', 'quiz', $a);
$openqbankurl = $pageurl->out(false, array('qbanktool' => 1,
'cat' => $category->id . ',' . $category->contextid));
$linkcategorycontents = ' <a href="' . $openqbankurl . '">' . $strshowcategorycontents . '</a>';
echo '<div class="randomquestioncategory">';
echo '<a href="'.
$pageurl->out(false,array("qbanktool"=>1,
"cat"=>$category->id.','.$category->contextid)).
'" title="'.$strshowcategorycontents.'">'.$category->name.'</a>';
echo '<span class="questionpreview">'.
quiz_question_preview_button($quiz, $question).
'</span>';
echo '<a href="' . $openqbankurl . '" title="' . $strshowcategorycontents . '">' . $category->name . '</a>';
echo '<span class="questionpreview">' . quiz_question_preview_button($quiz, $question, true) . '</span>';
echo '</div>';
echo "</div>";
$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 '<div class="randomquestionqlist">';
$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 '<span class="error">';
print_string("noquestionsnotinuse", "quiz");
print_string('noquestionsnotinuse', 'quiz');
echo '</span>';
echo '<br />';
//create link to open question bank
$linkcategorycontents=' <a href="'.
$pageurl->out(false,array("qbanktool"=>1,
"cat"=>$category->id.','.$category->contextid)).
'">'.$strshowcategorycontents.'</a>';
// embed the link into the string with instructions
// Embed the link into the string with instructions
$a = new stdClass;
$a->catname = '<strong>' . $category->name . '</strong>';
$a->link = $linkcategorycontents;
$a->link = $linkcategorycontents;
echo get_string('addnewquestionsqbank','quiz', $a);
}else{
//Category has questions, list a sample of them
echo "<ul>";
quiz_simple_question_list($pageurl, $question->category,
$randomquestionlistsize);
echo '<li class="totalquestionsinrandomqcategory">';
if ($questioncount>$randomquestionlistsize){
echo "... ";
} else {
// Category has questions
// Get a sample from the database,
$toshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
$questionidstoshow = array();
foreach ($toshow as $a) {
$questionidstoshow[] = $a->id;
}
$questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
'', 'qtype,name,questiontext,questiontextformat');
// list them,
echo '<ul>';
foreach ($questionstoshow as $question) {
echo '<li>' . quiz_question_tostring($question, true) . '</li>';
}
$a = new stdClass;
$a->arrow = $THEME->rarrow;
$strshowcategorycontents=get_string("showcategorycontents","quiz",$a);
print_string("totalquestionsinrandomqcategory","quiz",$questioncount);
echo ' <a href="'.
$pageurl->out(false,array("qbanktool"=>1,"cat"=>$category->id.','.$category->contextid)).
'">'.$strshowcategorycontents.'</a>';
echo "</li>";
echo "</ul>";
// and then display the total number.
echo '<li class="totalquestionsinrandomqcategory">';
if ($questioncount > NUM_QS_TO_SHOW_IN_RANDOM) {
echo '... ';
}
print_string('totalquestionsinrandomqcategory', 'quiz', $questioncount);
echo ' ' . $linkcategorycontents;
echo '</li>';
echo '</ul>';
}
echo "</div>";
echo '</div>';
echo '<div class="randomquestioncategorycount">';
echo "</div>";
echo "</div>";
echo '</div>';
echo '</div>';
}
@@ -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='<label for="s'.$question->id.'">';
$reordercheckboxlabelclose='</label>';
?>
<div class="singlequestion">
<?php
$formatoptions = new stdClass;
$formatoptions->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="<a title=\"$stredit\" href=\"".
$questionurl->out(false, array('id'=>$question->id)).
"\"><img src=\"$CFG->pixpath/t/edit.gif\" alt=\"".
$stredit."\" /></a>";
} elseif (question_has_capability_on($question, 'view',
$question->category)){
echo "$reordercheckboxlabel".
quiz_question_tostring($question,false).
"$reordercheckboxlabelclose";
$editstring="<a title=\"$strview\" href=\"".$questionurl->out(false,
array('id'=>$question->id))."\">$questionstring <img
src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a>";
}else{
echo "$reordercheckboxlabel".
quiz_question_tostring($question,false).
"$reordercheckboxlabelclose";
}
echo '<span class="questionpreview">'.$editstring.
quiz_question_preview_button($quiz, $question, false).
'</span>';
?>
</div><?php
function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz){
echo '<div class="singlequestion">';
echo '<label for="s' . $question->id . '">';
print_question_icon($question);
echo ' ' . quiz_question_tostring($question);
echo '</label>';
echo '<span class="questionpreview">' .
quiz_question_action_icons($quiz, $quiz->cmid, $question, $returnurl) . '</span>';
echo "</div>\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 '<div class="quiz_randomquestion">';
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 '<div class="randomquestionfromcategory">';
$url=$pageurl->out(false,array("qbanktool"=>1, "cat"=>$category->id.','.
$category->contextid));
$reordercheckboxlabel='<label for="s'.$question->id.'">';
$reordercheckboxlabelclose='</label>';
$questioncount = count($QTYPES['random']->get_usable_questions_from_category(
$category->id, $question->questiontext == '1', '0'));
$reordercheckboxlabel = '<label for="s'.$question->id.'">';
$reordercheckboxlabelclose = '</label>';
echo '<div class="quiz_randomquestion">';
echo '<div class="randomquestionfromcategory">';
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 '<span class="error">';
print_string("empty", "quiz");
print_string('empty', 'quiz');
echo '</span> ';
}
print_string('random','quiz');
print_string('random', 'quiz');
echo ": $reordercheckboxlabelclose</div>";
echo '<div class="randomquestioncategory">';
echo '<!--<a href="'.
$pageurl->out(false,array("qbanktool"=>1, "cat"=>$category->id.','.
$category->contextid)).
'">-->'.$reordercheckboxlabel.$category->name.
$reordercheckboxlabelclose.'<!--</a>-->';
echo $reordercheckboxlabel . $category->name . $reordercheckboxlabelclose;
echo '<span class="questionpreview">';
echo quiz_question_preview_button($quiz, $question,false);
echo quiz_question_preview_button($quiz, $question, false);
echo '</span>';
echo "</div>";
echo '<div class="randomquestioncategorycount">';
echo "</div>";
echo "</div>";
echo '</div>';
echo '</div>';
}
/**
@@ -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));
}
+61 -28
View File
@@ -735,37 +735,60 @@ 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 .= "<a title=\"$stredit\" href=\"".$questionurl->out()."\">
<img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a>";
} elseif (question_has_capability_on($question, 'view', $question->category)){
$html .= "<a title=\"$strview\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a>&nbsp;";
}
return $html;
}
// 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 '<a title="' . $action . '" href="' . $questionurl->out() . '">' . $contentbeforeicon .
'<img src="' . $CFG->pixpath . $icon . '.gif" alt="' . $action . '" /></a>';
} else {
return $contentbeforeicon;
}
}
/**
* @param object $quiz the quiz
@@ -773,20 +796,30 @@ function quiz_question_action_icons($quiz, $cmid, $question, $returnurl){
* @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');
$strpreviewlabel="";
if($label){
$strpreviewlabel = get_string('preview', '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');
}
$quizorcourseid = $quiz->id?('&amp;quizid=' . $quiz->id):('&amp;courseid=' .$COURSE->id);
return link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview',
"<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />$strpreviewlabel",
0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS, true);
// Do we want a label?
$strpreviewlabel="";
if ($label) {
$strpreviewlabel = $strpreview;
}
// Build the icon.
return link_to_popup_window('/question/preview.php?id=' . $question->id . '&amp;quizid=' . $quiz->id, 'questionpreview',
"<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreviewquestion\" /> $strpreviewlabel",
0, 0, $strpreviewquestion, QUESTION_PREVIEW_POPUP_OPTIONS, true);
}
/**
+3 -3
View File
@@ -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 '<a title="' . $title . '" href="' . $url . '">
<img src="' . $CFG->pixpath . '/t/' . $icon . '" class="iconsmall" alt="' . $title . '" /></a>';
<img src="' . $CFG->pixpath . '/' . $icon . '" class="iconsmall" alt="' . $title . '" /></a>';
}
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));
}
}
}