get_string("multichoice", "quiz"), TRUEFALSE => get_string("truefalse", "quiz"), SHORTANSWER => get_string("shortanswer", "quiz"), NUMERICAL => get_string("numerical", "quiz"), CALCULATED => get_string("calculated", "quiz"), MATCH => get_string("match", "quiz"), DESCRIPTION => get_string("description", "quiz"), RANDOMSAMATCH => get_string("randomsamatch", "quiz"), MULTIANSWER => get_string("multianswer", "quiz"), ESSAY => get_string("essay", "quiz") ); // add remote question types if ($rqp_types = get_records('quiz_rqp_types')) { foreach($rqp_types as $type) { $QUIZ_QUESTION_TYPE[100+$type->id] = $type->name; } } /** * Delete a question from a quiz * * Deletes a question or a pagebreak from a quiz by updating $modform * as well as the quiz, quiz_question_instances, quiz_attemtps, quiz_states * and quiz_newest_states tables. * @return boolean false if the question was not in the quiz * @param int $id The id of the question to be deleted * @param object $modform The extended quiz object as used by edit.php * This is updated by this function */ function quiz_delete_quiz_question($id, &$modform) { $questions = explode(",", $modform->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 and $questions[1] == 0) { unset($questions[1]); } // if what we deleted was not a page break but a question then also // delete associated grades, instances, attempts, states if ($question != 0) { unset($modform->grades[$question]); // Delete all states associated with all attempts for this question in the quiz. if ($attempts = get_records('quiz_attempts', 'quiz', $modform->instance)) { foreach ($attempts as $attempt) { delete_records('quiz_states', 'question', $question, 'attempt', $attempt->uniqueid); delete_records('quiz_newest_states', 'questionid', $question, 'attemptid', $attempt->uniqueid); } } // Delete all instances of the question in the quiz (there // should only be one) delete_records('quiz_question_instances', 'quiz', $modform->instance, 'question', $question); } $modform->questions = implode(",", $questions); // Avoid duplicate page breaks $modform->questions = str_replace(',0,0', ',0', $modform->questions); // save new questionlist in database if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { error('Could not save question list'); } } /** * Add a question to a quiz * * Adds a question to a quiz by updating $modform 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 $modform The extended quiz object as used by edit.php * This is updated by this function */ function quiz_add_quiz_question($id, &$modform) { $questions = explode(",", $modform->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 (!$modform->questionsperpage or (($end - $last -1) < $modform->questionsperpage)) { array_pop($questions); } } // add question $questions[] = $id; // add ending page break $questions[] = 0; // Save new questionslist in database $modform->questions = implode(",", $questions); if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->id)) { error('Could not save question list'); } // update question grades $questionrecord = get_record("quiz_questions", "id", $id); if (!empty($questionrecord->defaultgrade)) { $modform->grades[$id] = $questionrecord->defaultgrade; } else if ($questionrecord->qtype == DESCRIPTION){ $modform->grades[$id] = 0; } else { $modform->grades[$id] = 1; } quiz_update_question_instance($modform->grades[$id], $id, $modform->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 */ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { global $USER, $CFG, $QUIZ_QTYPES; $strorder = get_string("order"); $strquestionname = get_string("questionname", "quiz"); $strgrade = get_string("grade"); $strremove = get_string('remove', 'quiz'); $stredit = get_string("edit"); $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.course FROM {$CFG->prefix}quiz_questions q, {$CFG->prefix}quiz_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 "\n"; /// Form to choose to show pagebreaks and to repaginate quiz echo ''; return $sumgrade; } function quiz_print_category_form($course, $current, $recurse=1, $showhidden=false) { /// Prints a form to choose categories /// Make sure the default category exists for this course if (!$categories = get_records("quiz_categories", "course", $course->id, "id ASC")) { if (!$category = quiz_get_default_category($course->id)) { notify("Error creating a default category!"); } } /// Get all the existing categories now if (!$categories = get_records_select("quiz_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) { notify("Could not find any question categories!"); return false; // Something is really wrong } $categories = add_indented_names($categories); foreach ($categories as $key => $category) { if ($catcourse = get_record("course", "id", $category->course)) { if ($category->publish && $category->course != $course->id) { $category->indentedname .= " ($catcourse->shortname)"; } $catmenu[$category->id] = $category->indentedname; } } $strcategory = get_string("category", "quiz"); $strshow = get_string("show", "quiz"); $streditcats = get_string("editcategories", "quiz"); echo ""; echo "$strcategory: "; echo " | "; popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "", "", "", false, "self"); echo " | "; echo ""; echo ' |
"; print_string("selectcategoryabove", "quiz"); echo "
"; if ($quizid) { echo ""; print_string("addingquestions", "quiz"); echo "
"; } return; } if (!$category = get_record("quiz_categories", "id", "$categoryid")) { notify("Category not found!"); return; } echo "$strcreatenewquestion: | "; echo ''; popup_form ("question.php?category=$category->id&qtype=", $QUIZ_QUESTION_TYPE, "addquestion", "", "choose", "", "", false, "self"); echo ' | '; helpbutton("questiontypes", $strcreatenewquestion, "quiz"); echo ' |
'; print_string("publishedit","quiz"); echo ' | ||
'; if (isteacheredit($category->course)) { echo ''.$strimportquestions.''; helpbutton("import", $strimportquestions, "quiz"); echo ' | '; } echo "id}&courseid={$course->id}\">$strexportquestions"; helpbutton("export", $strexportquestions, "quiz"); echo ' |
"; print_string("noquestions", "quiz"); echo "
"; return; } if (!$questions = get_records_select('quiz_questions', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) { // There are no questions on the requested page. $page = 0; if (!$questions = get_records_select('quiz_questions', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) { // There are no questions at all echo ""; print_string("noquestions", "quiz"); echo "
"; return; } } print_paging_bar($totalnumber, $page, $perpage, "edit.php?perpage=$perpage&"); $canedit = isteacheredit($category->course); echo '\n"; } ?>