questions); // only do something if this question exists if (!isset($questions[$id])) { return false; } $question = $questions[$id]; unset($questions[$id]); // If we deleted the question at the top and it was followed by // a page break then delete page break as well if ($id == 0 && count($questions) > 1 && $questions[1] == 0) { unset($questions[1]); } $quiz->questions = implode(",", $questions); // Avoid duplicate page breaks $quiz->questions = str_replace(',0,0', ',0', $quiz->questions); // save new questionlist in database if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) { error('Could not save question list'); } delete_records('quiz_question_instances', 'quiz', $quiz->instance, 'question', $question); return true; } /** * Add a question to a quiz * * Adds a question to a quiz by updating $quiz as well as the * quiz and quiz_question_instances tables. It also adds a page break * if required. * @return boolean false if the question was already in the quiz * @param int $id The id of the question to be added * @param object $quiz The extended quiz object as used by edit.php * This is updated by this function */ function quiz_add_quiz_question($id, &$quiz) { $questions = explode(",", $quiz->questions); if (in_array($id, $questions)) { return false; } // remove ending page break if it is not needed if ($breaks = array_keys($questions, 0)) { // determine location of the last two page breaks $end = end($breaks); $last = prev($breaks); $last = $last ? $last : -1; if (!$quiz->questionsperpage or (($end - $last -1) < $quiz->questionsperpage)) { array_pop($questions); } } // add question $questions[] = $id; // add ending page break $questions[] = 0; // Save new questionslist in database $quiz->questions = implode(",", $questions); if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) { error('Could not save question list'); } // update question grades $questionrecord = get_record("question", "id", $id); $quiz->grades[$id] = $questionrecord->defaultgrade; quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance); return true; } /** * Save changes to question instance * * Saves changes to the question grades in the quiz_question_instances table. * It does not update 'sumgrades' in the quiz table. * @return boolean Indicates success or failure. * @param integer grade The maximal grade for the question * @param integer $questionid The id of the question * @param integer $quizid The id of the quiz to update / add the instances for. */ function quiz_update_question_instance($grade, $questionid, $quizid) { if ($instance = get_record("quiz_question_instances", "quiz", $quizid, 'question', $questionid)) { $instance->grade = $grade; return update_record('quiz_question_instances', $instance); } else { unset($instance); $instance->quiz = $quizid; $instance->question = $questionid; $instance->grade = $grade; return insert_record("quiz_question_instances", $instance); } } /** * Prints a list of quiz questions in a small layout form with knobs * * @return int sum of maximum grades * @param object $quiz This is not the standard quiz object used elsewhere but * it contains the quiz layout in $quiz->questions and the grades in * $quiz->grades * @param boolean $allowdelete Indicates whether the delete icons should be displayed * @param boolean $showbreaks Indicates whether the page breaks should be displayed * @param boolean $showbreaks Indicates whether the reorder tool should be displayed */ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $showbreaks=true, $reordertool=false) { global $USER, $CFG, $QTYPES; $strorder = get_string("order"); $strquestionname = get_string("questionname", "quiz"); $strgrade = get_string("grade"); $strremove = get_string('remove', 'quiz'); $stredit = get_string("edit"); $strview = get_string("view"); $straction = get_string("action"); $strmoveup = get_string("moveup"); $strmovedown = get_string("movedown"); $strsavegrades = get_string("savegrades", "quiz"); $strtype = get_string("type", "quiz"); $strpreview = get_string("preview", "quiz"); if (!$quiz->questions) { echo "

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

"; return 0; } if (!$questions = get_records_sql("SELECT q.*,c.contextid FROM {$CFG->prefix}question q, {$CFG->prefix}question_categories c WHERE q.id in ($quiz->questions) AND q.category = c.id")) { echo "

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

"; return 0; } $count = 0; $qno = 1; $sumgrade = 0; $order = explode(',', $quiz->questions); $lastindex = count($order)-1; // If the list does not end with a pagebreak then add it on. if ($order[$lastindex] != 0) { $order[] = 0; $lastindex++; } echo "
"; echo '
'; echo "sesskey\" />"; echo $pageurl->hidden_params_out(); echo "\n"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; // for RTL languages: switch right and left arrows /****/ if (right_to_left()) { $movearrow = 'moveleft.gif'; } else { $movearrow = 'removeright.gif'; } foreach ($order as $i => $qnum) { if ($qnum and empty($questions[$qnum])) { continue; } // If the questiontype is missing change the question type if ($qnum and !array_key_exists($questions[$qnum]->qtype, $QTYPES)) { $questions[$qnum]->qtype = 'missingtype'; } // Show the re-ordering field if the tool is turned on. // But don't show it in front of pagebreaks if they are hidden. if ($reordertool) { if ($qnum or $showbreaks) { echo ''; } else { echo ''; } } else { echo ''; } if ($qnum == 0) { // This is a page break if ($showbreaks) { echo ''; echo ''; echo ''; } $count++; // missing here, if loop is broken, need to close the echo ""; continue; } $question = $questions[$qnum]; echo ""; echo ""; if (!$quiz->shufflequestions) { // Print and increment question number echo ''; $qno += $question->length; } else { echo ''; } echo ''; echo ""; echo '"; $count++; $sumgrade += $quiz->grades[$qnum]; } echo ""; echo "\n"; echo ""; echo "
$strorder#$strquestionname$strtype$strgrade$straction
 '; echo ''; echo ''; echo ''; echo '

Page break
'; if ($count > 1) { echo "out_action(array('up'=>$count))."\">pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" />"; } echo ' '; if ($count < $lastindex) { echo "out_action(array('down'=>$count))."\">pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" />"; echo "out_action(array('delete'=>$count))."\"> pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strremove\" />"; } echo '
 
"; if ($count != 0) { echo "out_action(array('up'=>$count))."\">pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" />"; } echo ""; if ($count < $lastindex-1) { echo "out_action(array('down'=>$count))."\">pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" />"; } echo "'.($question->length ? $qno : ' ').' ' . format_string($question->name) . '"; print_question_icon($question); echo "'; if ($question->qtype == 'description') { echo " \n"; } else { echo ''; } echo ''; if (($question->qtype != 'random')){ echo quiz_question_preview_button($quiz, $question); } $returnurl = $pageurl->out(); $questionparams = array('returnurl' => $returnurl, 'cmid'=>$quiz->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)) { echo "out()."\"> pixpath/t/edit.gif\" class=\"iconsmall\" 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 ($allowdelete && question_has_capability_on($question, 'use', $question->category)) { // remove from quiz, not question delete. echo "out_action(array('delete'=>$count))."\"> pixpath/t/$movearrow\" class=\"iconsmall\" alt=\"$strremove\" />"; } echo "
\n"; print_string('total'); echo ": \n"; echo "$sumgrade"; echo " \n
\n"; print_string('maximumgrade'); echo ": \n"; echo ''; echo ''; helpbutton("maxgrade", get_string("maximumgrade"), "quiz"); echo "
\n"; echo '
'; echo '
'; echo '
'; echo "
\n"; /// Form to choose to show pagebreaks and to repaginate quiz echo '
'; echo '
'; echo $pageurl->hidden_params_out(array('showbreaks', 'reordertool')); echo ''; echo ''; echo ''; print_string('showbreaks', 'quiz'); if ($showbreaks) { $perpage= array(); for ($i=0; $i<=50; ++$i) { $perpage[$i] = $i; } $perpage[0] = get_string('allinone', 'quiz'); echo '
  '; print_string('repaginate', 'quiz', choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true)); } echo '
'; echo ''; print_string('reordertool', 'quiz'); echo ' '; helpbutton('reorderingtool', get_string('reordertool', 'quiz'), 'quiz'); echo '
'; echo '
'; echo '
'; return $sumgrade; } ?>