category = $category; } if (! $category = get_record("quiz_categories", "id", $question->category)) { error("This question doesn't belong to a valid category!"); } if (! $course = get_record("course", "id", $category->course)) { error("This question category doesn't belong to a valid course!"); } $qtype = $question->qtype; } else if ($category) { if (! $category = get_record("quiz_categories", "id", $category)) { error("This wasn't a valid category!"); } if (! $course = get_record("course", "id", $category->course)) { error("This category doesn't belong to a valid course!"); } $question->category = $category->id; $question->qtype = $qtype; } else { error("Must specify question id or category"); } require_login($course->id); if (!isteacheredit($course->id)) { error("You can't modify these questions!"); } $streditingquiz = get_string(isset($modform->instance) ? "editingquiz" : "editquestions", "quiz"); $streditingquestion = get_string("editingquestion", "quiz"); print_header("$course->shortname: $streditingquestion", "$course->shortname: $streditingquestion", "wwwroot/course/view.php?id=$course->id\">$course->shortname -> $streditingquiz -> $streditingquestion"); if (isset($delete)) { if (isset($confirm)) { if ($confirm == md5($delete)) { if (!delete_records("quiz_questions", "id", $question->id)) { error("An error occurred trying to delete question (id $question->id)"); } redirect("edit.php"); } else { error("Confirmation string was incorrect"); } } else { if ($category->publish) { $quizzes = get_records("quiz"); } else { $quizzes = get_records("quiz", "course", $course->id); } $beingused = array(); if ($quizzes) { foreach ($quizzes as $quiz) { $qqq = explode(",", $quiz->questions); foreach ($qqq as $key => $value) { if ($value == $delete) { $beingused[] = $quiz->name; } } } } if ($beingused) { $beingused = implode(", ", $beingused); $beingused = get_string("questioninuse", "quiz", "$question->name")."
".$beingused;
notice($beingused, "edit.php");
} else {
notice_yesno(get_string("deletequestioncheck", "quiz", $question->name),
"question.php?id=$question->id&delete=$delete&confirm=".md5($delete), "edit.php");
}
print_footer($course);
exit;
}
}
if ($form = data_submitted()) {
// First, save the basic question itself
$question->name = $form->name;
$question->questiontext = $form->questiontext;
if (empty($form->image)) {
$question->image = "";
} else {
$question->image = $form->image;
}
if (isset($form->defaultgrade)) {
$question->defaultgrade = $form->defaultgrade;
}
if ($err = formcheck($question)) {
notify(get_string("someerrorswerefound"));
} else {
if (!empty($question->id)) { // Question already exists
$question->version ++; // Update version number of question
if (!update_record("quiz_questions", $question)) {
error("Could not update question!");
}
} else { // Question is a new one
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question->version = 1;
if (!$question->id = insert_record("quiz_questions", $question)) {
error("Could not insert new question!");
}
}
// Now to save all the answers and type-specific options
$form->id = $question->id;
$form->qtype = $question->qtype;
$form->category = $question->category;
$result = quiz_save_question_options($form);
if (!empty($result->error)) {
error($result->error);
}
if (!empty($result->notice)) {
notice_yesno($result->notice, "question.php?id=$question->id", "edit.php");
print_footer($course);
exit;
}
redirect("edit.php");
}
}
$grades = array(1,0.9,0.8,0.75,0.70,0.66666,0.60,0.50,0.40,0.33333,0.30,0.25,0.20,0.16666,0.10,0.05,0);
foreach ($grades as $grade) {
$percentage = 100 * $grade;
$neggrade = -$grade;
$gradeoptions["$grade"] = "$percentage %";
$gradeoptionsfull["$grade"] = "$percentage %";
$gradeoptionsfull["$neggrade"] = -$percentage." %";
}
$gradeoptionsfull["0"] = $gradeoptions["0"] = get_string("none");
arsort($gradeoptions, SORT_NUMERIC);
arsort($gradeoptionsfull, SORT_NUMERIC);
if (!$categories = quiz_get_category_menu($course->id, true)) {
error("No categories!");
}
make_upload_directory("$course->id"); // Just in case
$coursefiles = get_directory_list("$CFG->dataroot/$course->id", $CFG->moddata);
foreach ($coursefiles as $filename) {
if (mimeinfo("icon", $filename) == "image.gif") {
$images["$filename"] = $filename;
}
}
// Print the question editing form
if (empty($question->id)) {
$question->id = "";
}
if (empty($question->name)) {
$question->name = "";
}
if (empty($question->questiontext)) {
$question->questiontext = "";
}
if (empty($question->image)) {
$question->image = "";
}
// Set up some Richtext editing if necessary
if ($usehtmleditor = can_use_richtext_editor()) {
$defaultformat = FORMAT_HTML;
$onsubmit = "onsubmit=\"copyrichtext(theform.questiontext);\"";
} else {
$defaultformat = FORMAT_MOODLE;
$onsubmit = "";
}
switch ($qtype) {
case SHORTANSWER:
if (!empty($question->id)) {
$options = get_record("quiz_shortanswer", "question", $question->id);
} else {
$options->usecase = 0;
}
if (!empty($options->answers)) {
$answersraw = get_records_list("quiz_answers", "id", $options->answers);
}
for ($i=0; $i<6; $i++) {
$answers[] = ""; // Make answer slots, default as blank
}
if (!empty($answersraw)) {
$i=0;
foreach ($answersraw as $answer) {
$answers[$i] = $answer; // insert answers into slots
$i++;
}
}
print_heading_with_help(get_string("editingshortanswer", "quiz"), "shortanswer", "quiz");
require("shortanswer.html");
break;
case TRUEFALSE:
if (!empty($question->id)) {
$options = get_record("quiz_truefalse", "question", "$question->id");
}
if (!empty($options->trueanswer)) {
$true = get_record("quiz_answers", "id", $options->trueanswer);
} else {
$true->fraction = 1;
$true->feedback = "";
}
if (!empty($options->falseanswer)) {
$false = get_record("quiz_answers", "id", "$options->falseanswer");
} else {
$false->fraction = 0;
$false->feedback = "";
}
if ($true->fraction > $false->fraction) {
$question->answer = 1;
} else {
$question->answer = 0;
}
print_heading_with_help(get_string("editingtruefalse", "quiz"), "truefalse", "quiz");
require("truefalse.html");
break;
case MULTICHOICE:
if (!empty($question->id)) {
$options = get_record("quiz_multichoice", "question", $question->id);
} else {
$options->single = "";
}
if (!empty($options->answers)) {
$answersraw = get_records_list("quiz_answers", "id", $options->answers);
}
for ($i=0; $i