Many little fixes and cleanups for robustness

This commit is contained in:
moodler 2002-12-30 05:10:01 +00:00
parent b9b8ab696f
commit c04c41c7d2
9 changed files with 264 additions and 176 deletions

View File

@ -61,6 +61,8 @@ $string['guestsno'] = "Sorry, guests can not see or attempt quizzes";
$string['imagedisplay'] = "Image to display";
$string['introduction'] = "Introduction";
$string['marks'] = "Marks";
$string['missingname'] = "Missing question name";
$string['missingquestiontext'] = "Missing question text";
$string['multichoice'] = "Multiple Choice";
$string['noanswers'] = "No answers were selected!";
$string['noattempts'] = "No attempts have been made on this quiz";

View File

@ -69,14 +69,8 @@
}
}
if ($course->format == "weeks" and $quiz->days) {
$timenow = time();
$timestart = $course->startdate + (($cw->section - 1) * 608400);
$timefinish = $timestart + (3600 * 24 * $quiz->days);
$available = ($timestart < $timenow and $timenow < $timefinish);
} else {
$available = true;
}
$available = ($quiz->timeopen < $timenow and $timenow < $quiz->timeclose);
/// Check to see if they are submitting answers
if (match_referer() && isset($HTTP_POST_VARS)) {

View File

@ -83,7 +83,7 @@
echo "<P ALIGN=CENTER><FONT SIZE=3>";
echo $streditcategories;
helpbutton("categories", $strcategories, "quiz");
helpbutton("categories", $streditcategories, "quiz");
echo "</FONT></P>";
/// If data submitted, then process and store.

View File

@ -5,10 +5,14 @@
require_login();
if (empty($destination)) {
$destination = "";
}
if (match_referer($destination) && isset($course) && isset($HTTP_POST_VARS)) { // form submitted from mod.html
$modform = (object)$HTTP_POST_VARS;
if (!$modform->name or !$modform->intro) {
if (empty($modform->name) or empty($modform->intro)) {
error(get_string("filloutallfields"), $HTTP_REFERER);
}
@ -34,14 +38,14 @@
error("You can't modify this course!");
}
if (! $modform->grades) { // Construct an array to hold all the grades.
if (empty($modform->grades)) { // Construct an array to hold all the grades.
$modform->grades = quiz_get_all_question_grades($modform->questions, $modform->instance);
}
// Now, check for commands on this page and modify variables as necessary
if ($up) { /// Move the given question up a slot
if (!empty($up)) { /// Move the given question up a slot
$questions = explode(",", $modform->questions);
if ($questions[0] <> $up) {
foreach ($questions as $key => $question) {
@ -56,7 +60,7 @@
}
}
if ($down) { /// Move the given question down a slot
if (!empty($down)) { /// Move the given question down a slot
$questions = explode(",", $modform->questions);
if ($questions[count($questions)-1] <> $down) {
foreach ($questions as $key => $question) {
@ -71,9 +75,9 @@
}
}
if ($add) { /// Add a question to the current quiz
if (!empty($add)) { /// Add a question to the current quiz
$rawquestions = $HTTP_POST_VARS;
if ($modform->questions) {
if (!empty($modform->questions)) {
$questions = explode(",", $modform->questions);
}
foreach ($rawquestions as $key => $value) { // Parse input for question ids
@ -90,14 +94,14 @@
$modform->grades[$key] = 1; // default score
}
}
if ($questions) {
if (!empty($questions)) {
$modform->questions = implode(",", $questions);
} else {
$modform->questions = "";
}
}
if ($delete) { /// Delete a question from the list
if (!empty($delete)) { /// Delete a question from the list
$questions = explode(",", $modform->questions);
foreach ($questions as $key => $question) {
if ($question == $delete) {
@ -108,7 +112,7 @@
$modform->questions = implode(",", $questions);
}
if ($setgrades) { /// The grades have been updated, so update our internal list
if (!empty($setgrades)) { /// The grades have been updated, so update our internal list
$rawgrades = $HTTP_POST_VARS;
unset($modform->grades);
foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
@ -119,12 +123,16 @@
}
}
if ($cat) { //-----------------------------------------------------------
if (!empty($cat)) { //-----------------------------------------------------------
$modform->category = $cat;
}
if (empty($modform->category)) {
$modform->category = "";
}
$modform->sumgrades = 0;
if ($modform->grades) {
if (!empty($modform->grades)) {
foreach ($modform->grades as $grade) {
$modform->sumgrades += $grade;
}

View File

@ -34,9 +34,9 @@ function quiz_add_instance($quiz) {
$quiz->created = time();
$quiz->timemodified = time();
$quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday,
$quiz->openhour, $quiz->openminute, $quiz->opensecond);
$quiz->openhour, $quiz->openminute, 0);
$quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday,
$quiz->closehour, $quiz->closeminute, $quiz->closesecond);
$quiz->closehour, $quiz->closeminute, 0);
if (!$quiz->id = insert_record("quiz", $quiz)) {
return false; // some error occurred
@ -68,9 +68,9 @@ function quiz_update_instance($quiz) {
$quiz->timemodified = time();
$quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday,
$quiz->openhour, $quiz->openminute, $quiz->opensecond);
$quiz->openhour, $quiz->openminute, 0);
$quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday,
$quiz->closehour, $quiz->closeminute, $quiz->closesecond);
$quiz->closehour, $quiz->closeminute, 0);
$quiz->id = $quiz->instance;
if (!update_record("quiz", $quiz)) {

View File

@ -11,11 +11,17 @@
<TD align=right><P><B><? print_string("questionname", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="name" size=40 value="<? p($question->name) ?>">
<? if (isset($err["name"])) formerr($err["name"]); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<? if (isset($err["questiontext"])) {
formerr($err["questiontext"]);
echo "<BR \>";
}
?>
<textarea name="questiontext" rows=5 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>
@ -35,10 +41,10 @@
<TD align=right><P><B><? print_string("answerhowmany", "quiz") ?>:</B></P></TD>
<TD>
<?
unset($menu);
$menu[0] = get_string("answersingleno", "quiz");
$menu[1] = get_string("answersingleyes", "quiz");
choose_from_menu($menu, "single", "$options->single", "");
unset($menu);
?>
</TD>
</TR>

View File

@ -51,7 +51,7 @@
print_header("$course->shortname: $streditingquestion", "$course->shortname: $streditingquestion",
"<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
-> <A HREF=\"$HTTP_REFERER\">$streditingquiz</A> -> $streditingquestion");
-> <A HREF=\"edit.php\">$streditingquiz</A> -> $streditingquestion");
if (isset($delete)) {
if (isset($confirm)) {
@ -104,7 +104,9 @@
$question->image = $form->image;
$question->category = $form->category;
if ($question->id) { // Question already exists
if (!$err = formcheck($question)) {
if (!empty($question->id)) { // Question already exists
if (!update_record("quiz_questions", $question)) {
error("Could not update question!");
}
@ -253,7 +255,7 @@
}
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.10,0.05,0);
@ -284,38 +286,88 @@
// 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 = "";
}
switch ($type) {
case SHORTANSWER:
$options = get_record("quiz_shortanswer", "question", "$question->id");// OK to fail
$answersraw = get_records_list("quiz_answers", "id", "$options->answers");// OK to fail
print_heading_with_help(get_string("editingshortanswer", "quiz"), "shortanswer", "quiz");
if ($answersraw) {
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[] = $answer; // to renumber index 0,1,2...
$answers[$i] = $answer; // insert answers into slots
$i++;
}
}
print_heading_with_help(get_string("editingshortanswer", "quiz"), "shortanswer", "quiz");
require("shortanswer.html");
break;
case TRUEFALSE:
$options = get_record("quiz_truefalse", "question", "$question->id"); // OK to fail
$true = get_record("quiz_answers", "id", "$options->true"); // OK to fail
$false = get_record("quiz_answers", "id", "$options->false"); // OK to fail
if (!empty($question->id)) {
$options = get_record("quiz_truefalse", "question", "$question->id");
}
if (!empty($options->true)) {
$true = get_record("quiz_answers", "id", "$options->true");
} else {
$true->fraction = 1;
$true->feedback = "";
}
if (!empty($options->false)) {
$false = get_record("quiz_answers", "id", "$options->false");
} 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:
$options = get_record("quiz_multichoice", "question", "$question->id");// OK to fail
$answersraw = get_records_list("quiz_answers", "id", "$options->answers");// OK to fail
if ($answersraw) {
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<6; $i++) {
$answers[] = ""; // Make answer slots, default as blank
}
if (!empty($answersraw)) {
$i=0;
foreach ($answersraw as $answer) {
$answers[] = $answer; // to renumber index 0,1,2...
$answers[$i] = $answer; // insert answers into slots
$i++;
}
}
print_heading_with_help(get_string("editingmultichoice", "quiz"), "multichoice", "quiz");
@ -333,4 +385,18 @@
}
print_footer($course);
function formcheck($question) {
$err = array();
if (empty($question->name)) {
$err["name"] = get_string("missingname", "quiz");
}
if (empty($question->questiontext)) {
$err["questiontext"] = get_string("missingquestiontext", "quiz");
}
return $err;
}
?>

View File

@ -11,11 +11,17 @@
<TD align=right><P><B><? print_string("questionname", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="name" size=50 value="<? p($question->name) ?>">
<? if (isset($err["name"])) formerr($err["name"]); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<? if (isset($err["questiontext"])) {
formerr($err["questiontext"]);
echo "<BR \>";
}
?>
<textarea name="questiontext" rows=6 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>

View File

@ -11,11 +11,17 @@
<TD align=right><P><B><? print_string("questionname", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="name" size=50 value="<? p($question->name) ?>">
<? if (isset($err["name"])) formerr($err["name"]); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<? if (isset($err["questiontext"])) {
formerr($err["questiontext"]);
echo "<BR \>";
}
?>
<textarea name="questiontext" rows=6 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>