2002-10-04 02:59:05 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Library of function for module quiz
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
/// CONSTANTS ///////////////////////////////////////////////////////////////////
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
define("GRADEHIGHEST", "1");
|
|
|
|
define("GRADEAVERAGE", "2");
|
|
|
|
define("ATTEMPTFIRST", "3");
|
|
|
|
define("ATTEMPTLAST", "4");
|
|
|
|
$QUIZ_GRADE_METHOD = array ( GRADEHIGHEST => get_string("gradehighest", "quiz"),
|
|
|
|
GRADEAVERAGE => get_string("gradeaverage", "quiz"),
|
|
|
|
ATTEMPTFIRST => get_string("attemptfirst", "quiz"),
|
|
|
|
ATTEMPTLAST => get_string("attemptlast", "quiz"));
|
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
define("SHORTANSWER", "1");
|
|
|
|
define("TRUEFALSE", "2");
|
|
|
|
define("MULTICHOICE", "3");
|
|
|
|
define("RANDOM", "4");
|
|
|
|
define("MATCH", "5");
|
|
|
|
define("RANDOMSAMATCH", "6");
|
|
|
|
|
|
|
|
$QUIZ_QUESTION_TYPE = array ( MULTICHOICE => get_string("multichoice", "quiz"),
|
|
|
|
TRUEFALSE => get_string("truefalse", "quiz"),
|
|
|
|
SHORTANSWER => get_string("shortanswer", "quiz"),
|
|
|
|
MATCH => get_string("match", "quiz"),
|
2003-04-09 06:32:51 +00:00
|
|
|
RANDOM => get_string("random", "quiz"),
|
2003-03-30 16:46:50 +00:00
|
|
|
RANDOMSAMATCH => get_string("randomsamatch", "quiz") );
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
$QUIZ_FILE_FORMAT = array ( "custom" => get_string("custom", "quiz"),
|
|
|
|
"webct" => get_string("webct", "quiz"),
|
|
|
|
"qti" => get_string("qti", "quiz"),
|
|
|
|
"missingword" => get_string("missingword", "quiz") );
|
|
|
|
|
2003-01-20 15:43:09 +00:00
|
|
|
define("QUIZ_PICTURE_DEFAULT_HEIGHT", "200");
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
define("QUIZ_MAX_NUMBER_ANSWERS", "8");
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
/// FUNCTIONS ///////////////////////////////////////////////////////////////////
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
function quiz_add_instance($quiz) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Given an object containing all the necessary data,
|
|
|
|
/// (defined by the form in mod.html) this function
|
|
|
|
/// will create a new instance and return the id number
|
|
|
|
/// of the new instance.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->created = time();
|
2002-10-04 02:59:05 +00:00
|
|
|
$quiz->timemodified = time();
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday,
|
2002-12-30 05:10:01 +00:00
|
|
|
$quiz->openhour, $quiz->openminute, 0);
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday,
|
2002-12-30 05:10:01 +00:00
|
|
|
$quiz->closehour, $quiz->closeminute, 0);
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
if (!$quiz->id = insert_record("quiz", $quiz)) {
|
|
|
|
return false; // some error occurred
|
|
|
|
}
|
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
// The grades for every question in this quiz are stored in an array
|
2002-10-14 12:21:18 +00:00
|
|
|
if ($quiz->grades) {
|
|
|
|
foreach ($quiz->grades as $question => $grade) {
|
2002-10-15 12:54:11 +00:00
|
|
|
if ($question and $grade) {
|
|
|
|
unset($questiongrade);
|
|
|
|
$questiongrade->quiz = $quiz->id;
|
|
|
|
$questiongrade->question = $question;
|
|
|
|
$questiongrade->grade = $grade;
|
|
|
|
if (!insert_record("quiz_question_grades", $questiongrade)) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-14 12:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
return $quiz->id;
|
2002-10-04 02:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_update_instance($quiz) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Given an object containing all the necessary data,
|
|
|
|
/// (defined by the form in mod.html) this function
|
|
|
|
/// will update an existing instance with new data.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
$quiz->timemodified = time();
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday,
|
2002-12-30 05:10:01 +00:00
|
|
|
$quiz->openhour, $quiz->openminute, 0);
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday,
|
2002-12-30 05:10:01 +00:00
|
|
|
$quiz->closehour, $quiz->closeminute, 0);
|
2002-10-04 02:59:05 +00:00
|
|
|
$quiz->id = $quiz->instance;
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
if (!update_record("quiz", $quiz)) {
|
|
|
|
return false; // some error occurred
|
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
// The grades for every question in this quiz are stored in an array
|
2002-10-14 12:21:18 +00:00
|
|
|
// Insert or update records as appropriate
|
|
|
|
|
|
|
|
$existing = get_records("quiz_question_grades", "quiz", $quiz->id, "", "question,grade,id");
|
|
|
|
|
|
|
|
if ($quiz->grades) {
|
|
|
|
foreach ($quiz->grades as $question => $grade) {
|
2002-10-15 12:54:11 +00:00
|
|
|
if ($question and $grade) {
|
|
|
|
unset($questiongrade);
|
|
|
|
$questiongrade->quiz = $quiz->id;
|
|
|
|
$questiongrade->question = $question;
|
|
|
|
$questiongrade->grade = $grade;
|
|
|
|
if (isset($existing[$question])) {
|
|
|
|
if ($existing[$question]->grade != $grade) {
|
|
|
|
$questiongrade->id = $existing[$question]->id;
|
|
|
|
if (!update_record("quiz_question_grades", $questiongrade)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!insert_record("quiz_question_grades", $questiongrade)) {
|
2002-10-14 12:21:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2002-10-04 02:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_delete_instance($id) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Given an ID of an instance of this module,
|
|
|
|
/// this function will permanently delete the instance
|
|
|
|
/// and any data that depends on it.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
if (! $quiz = get_record("quiz", "id", "$id")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
if ($attempts = get_record("quiz_attempts", "quiz", "$quiz->id")) {
|
|
|
|
foreach ($attempts as $attempt) {
|
|
|
|
if (! delete_records("quiz_responses", "attempt", "$attempt->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! delete_records("quiz_attempts", "quiz", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! delete_records("quiz_grades", "quiz", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! delete_records("quiz_question_grades", "quiz", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
if (! delete_records("quiz", "id", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_user_outline($course, $user, $mod, $quiz) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Return a small object with summary information about what a
|
|
|
|
/// user has done with a given particular instance of this module
|
|
|
|
/// Used for user activity reports.
|
|
|
|
/// $return->time = the time they did it
|
|
|
|
/// $return->info = a short text description
|
2002-12-23 09:39:26 +00:00
|
|
|
if ($grade = get_record("quiz_grades", "userid", $user->id, "quiz", $quiz->id)) {
|
2002-10-17 08:28:18 +00:00
|
|
|
|
|
|
|
if ($grade->grade) {
|
|
|
|
$result->info = get_string("grade").": $grade->grade";
|
|
|
|
}
|
|
|
|
$result->time = $grade->timemodified;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return NULL;
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_user_complete($course, $user, $mod, $quiz) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Print a detailed representation of what a user has done with
|
|
|
|
/// a given particular instance of this module, for user activity reports.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_print_recent_activity(&$logs, $isteacher=false) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Given a list of logs, assumed to be those since the last login
|
|
|
|
/// this function prints a short list of changes related to this module
|
|
|
|
/// If isteacher is true then perhaps additional information is printed.
|
|
|
|
/// This function is called from course/lib.php: print_recent_activity()
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
global $CFG, $COURSE_TEACHER_COLOR;
|
|
|
|
|
2002-12-29 17:32:32 +00:00
|
|
|
$content = "";
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
return $content; // True if anything was printed, otherwise false
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_cron () {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Function to be run periodically according to the moodle cron
|
|
|
|
/// This function searches for things that need to be done, such
|
|
|
|
/// as sending out mail, toggling flags etc ...
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-10-17 07:23:51 +00:00
|
|
|
function quiz_grades($quizid) {
|
2002-10-17 07:55:54 +00:00
|
|
|
/// Must return an array of grades, indexed by user, and a max grade.
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
$return->grades = get_records_menu("quiz_grades", "quiz", $quizid, "", "userid,grade");
|
2002-10-17 07:55:54 +00:00
|
|
|
$return->maxgrade = get_field("quiz", "grade", "id", "$quizid");
|
|
|
|
return $return;
|
2002-10-17 07:23:51 +00:00
|
|
|
}
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-12-22 13:55:30 +00:00
|
|
|
/// SQL FUNCTIONS ////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function quiz_move_questions($category1, $category2) {
|
|
|
|
global $CFG;
|
|
|
|
return execute_sql("UPDATE {$CFG->prefix}quiz_questions
|
|
|
|
SET category = '$category2'
|
|
|
|
WHERE category = '$category1'",
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_get_question_grades($quizid, $questionlist) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return get_records_sql("SELECT question,grade
|
|
|
|
FROM {$CFG->prefix}quiz_question_grades
|
|
|
|
WHERE quiz = '$quizid'
|
|
|
|
AND question IN ($questionlist)");
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
function quiz_get_random_categories($questionlist) {
|
|
|
|
/// Given an array of questions, this function looks for random
|
|
|
|
/// questions among them and returns a list of categories with
|
|
|
|
/// an associated count of random questions for each.
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return get_records_sql_menu("SELECT category,count(*)
|
|
|
|
FROM {$CFG->prefix}quiz_questions
|
|
|
|
WHERE id IN ($questionlist)
|
|
|
|
AND qtype = '".RANDOM."'
|
|
|
|
GROUP BY category ");
|
|
|
|
}
|
|
|
|
|
2002-12-22 13:55:30 +00:00
|
|
|
function quiz_get_grade_records($quiz) {
|
|
|
|
/// Gets all info required to display the table of quiz results
|
|
|
|
/// for report.php
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return get_records_sql("SELECT qg.*, u.firstname, u.lastname, u.picture
|
|
|
|
FROM {$CFG->prefix}quiz_grades qg,
|
|
|
|
{$CFG->prefix}user u
|
|
|
|
WHERE qg.quiz = '$quiz->id'
|
2002-12-23 09:39:26 +00:00
|
|
|
AND qg.userid = u.id");
|
2002-12-22 13:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_get_answers($question) {
|
2003-04-09 06:32:51 +00:00
|
|
|
// Given a question, returns the correct answers for a given question
|
2002-12-22 13:55:30 +00:00
|
|
|
global $CFG;
|
2003-02-24 10:37:56 +00:00
|
|
|
|
2003-01-03 16:01:48 +00:00
|
|
|
switch ($question->qtype) {
|
2003-02-24 10:37:56 +00:00
|
|
|
case SHORTANSWER: // Could be multiple answers
|
2003-04-09 06:32:51 +00:00
|
|
|
return get_records_sql("SELECT a.*, sa.usecase
|
2002-12-22 13:55:30 +00:00
|
|
|
FROM {$CFG->prefix}quiz_shortanswer sa,
|
2003-04-09 06:32:51 +00:00
|
|
|
{$CFG->prefix}quiz_answers a
|
2002-12-22 13:55:30 +00:00
|
|
|
WHERE sa.question = '$question->id'
|
2003-04-09 06:32:51 +00:00
|
|
|
AND sa.question = a.question ");
|
2002-12-22 13:55:30 +00:00
|
|
|
break;
|
|
|
|
|
2003-02-24 10:37:56 +00:00
|
|
|
case TRUEFALSE: // Should be always two answers
|
2003-04-09 06:32:51 +00:00
|
|
|
return get_records("quiz_answers", "question", $question->id);
|
2002-12-22 13:55:30 +00:00
|
|
|
break;
|
|
|
|
|
2003-02-24 10:37:56 +00:00
|
|
|
case MULTICHOICE: // Should be multiple answers
|
2003-04-09 06:32:51 +00:00
|
|
|
return get_records_sql("SELECT a.*, mc.single
|
2002-12-22 13:55:30 +00:00
|
|
|
FROM {$CFG->prefix}quiz_multichoice mc,
|
2003-04-09 06:32:51 +00:00
|
|
|
{$CFG->prefix}quiz_answers a
|
2002-12-22 13:55:30 +00:00
|
|
|
WHERE mc.question = '$question->id'
|
2003-04-09 06:32:51 +00:00
|
|
|
AND mc.question = a.question ");
|
2002-12-22 13:55:30 +00:00
|
|
|
break;
|
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
case MATCH:
|
2003-04-09 06:32:51 +00:00
|
|
|
return get_records("quiz_match_sub", "question", $question->id);
|
2003-03-30 16:46:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMSAMATCH: // Could be any of many answers, return them all
|
2003-04-09 06:32:51 +00:00
|
|
|
return get_records_sql("SELECT a.*
|
2003-02-24 10:37:56 +00:00
|
|
|
FROM {$CFG->prefix}quiz_questions q,
|
2003-04-09 06:32:51 +00:00
|
|
|
{$CFG->prefix}quiz_answers a
|
2003-02-24 10:37:56 +00:00
|
|
|
WHERE q.category = '$question->category'
|
|
|
|
AND q.qtype = ".SHORTANSWER."
|
2003-04-09 06:32:51 +00:00
|
|
|
AND q.id = a.question ");
|
2003-02-24 10:37:56 +00:00
|
|
|
break;
|
|
|
|
|
2002-12-22 13:55:30 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-20 07:28:53 +00:00
|
|
|
function quiz_get_attempt_responses($attempt, $quiz) {
|
2002-12-22 13:55:30 +00:00
|
|
|
// Given an attempt object, this function gets all the
|
|
|
|
// stored responses and returns them in a format suitable
|
|
|
|
// for regrading using quiz_grade_attempt_results()
|
|
|
|
global $CFG;
|
|
|
|
|
2003-02-24 10:37:56 +00:00
|
|
|
if (!$responses = get_records_sql("SELECT q.id, q.qtype, q.category, r.answer
|
2002-12-22 13:55:30 +00:00
|
|
|
FROM {$CFG->prefix}quiz_responses r,
|
|
|
|
{$CFG->prefix}quiz_questions q
|
|
|
|
WHERE r.attempt = '$attempt->id'
|
2003-04-09 06:32:51 +00:00
|
|
|
AND q.id = r.question")) {
|
2002-12-22 13:55:30 +00:00
|
|
|
notify("Could not find any responses for that attempt!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
foreach ($responses as $key => $response) {
|
|
|
|
if ($response->qtype == RANDOM) {
|
|
|
|
$responses[$key]->random = $response->answer;
|
|
|
|
$responses[$key]->answer = explode(",",$responses[$response->answer]->answer);
|
|
|
|
$responses[$response->answer]->delete = true;
|
|
|
|
} else {
|
|
|
|
$responses[$key]->answer = explode(",",$response->answer);
|
|
|
|
}
|
|
|
|
}
|
2002-12-22 13:55:30 +00:00
|
|
|
foreach ($responses as $key => $response) {
|
2003-04-09 06:32:51 +00:00
|
|
|
if (!empty($response->delete)) {
|
|
|
|
unset($responses[$key]);
|
|
|
|
}
|
2002-12-22 13:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $responses;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Any other quiz functions go here. Each of them must have a name that
|
|
|
|
/// starts with quiz_
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-16 09:35:04 +00:00
|
|
|
function quiz_print_comment($text) {
|
|
|
|
global $THEME;
|
|
|
|
|
2003-01-08 09:08:05 +00:00
|
|
|
echo "<SPAN CLASS=feedbacktext>".text_to_html($text, true, false)."</SPAN>";
|
2002-10-16 09:35:04 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 05:35:25 +00:00
|
|
|
function quiz_print_correctanswer($text) {
|
|
|
|
global $THEME;
|
|
|
|
|
|
|
|
echo "<P ALIGN=RIGHT><SPAN CLASS=highlight>$text</SPAN></P>";
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
function quiz_print_question_icon($question, $editlink=true) {
|
2002-10-20 17:15:39 +00:00
|
|
|
// Prints a question icon
|
2002-10-23 12:10:38 +00:00
|
|
|
|
|
|
|
global $QUIZ_QUESTION_TYPE;
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
if ($editlink) {
|
|
|
|
echo "<A HREF=\"question.php?id=$question->id\" TITLE=\"".$QUIZ_QUESTION_TYPE[$question->qtype]."\">";
|
|
|
|
}
|
2003-01-03 16:01:48 +00:00
|
|
|
switch ($question->qtype) {
|
2002-10-20 17:15:39 +00:00
|
|
|
case SHORTANSWER:
|
2002-10-23 12:10:38 +00:00
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/sa.gif\">";
|
2002-10-20 17:15:39 +00:00
|
|
|
break;
|
|
|
|
case TRUEFALSE:
|
2002-10-23 12:10:38 +00:00
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/tf.gif\">";
|
2002-10-20 17:15:39 +00:00
|
|
|
break;
|
|
|
|
case MULTICHOICE:
|
2002-10-23 12:10:38 +00:00
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/mc.gif\">";
|
2002-10-20 17:15:39 +00:00
|
|
|
break;
|
|
|
|
case RANDOM:
|
2002-10-23 12:10:38 +00:00
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/rs.gif\">";
|
2002-10-20 17:15:39 +00:00
|
|
|
break;
|
2003-03-30 16:46:50 +00:00
|
|
|
case MATCH:
|
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/ma.gif\">";
|
|
|
|
break;
|
|
|
|
case RANDOMSAMATCH:
|
2003-02-24 10:37:56 +00:00
|
|
|
echo "<IMG BORDER=0 HEIGHT=16 WIDTH=16 SRC=\"pix/rm.gif\">";
|
|
|
|
break;
|
2002-10-20 17:15:39 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
if ($editlink) {
|
|
|
|
echo "</A>\n";
|
|
|
|
}
|
2002-10-20 17:15:39 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
function quiz_print_question($number, $question, $grade, $courseid,
|
|
|
|
$feedback=NULL, $response=NULL, $actualgrade=NULL, $correct=NULL,
|
2003-04-09 13:57:08 +00:00
|
|
|
$realquestion=NULL, $shuffleanswers=false) {
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
/// Prints a quiz question, any format
|
|
|
|
/// $question is provided as an object
|
2002-10-06 17:06:54 +00:00
|
|
|
|
2003-01-03 16:01:48 +00:00
|
|
|
if (empty($actualgrade)) {
|
|
|
|
$actualgrade = 0;
|
|
|
|
}
|
|
|
|
|
2002-10-06 17:06:54 +00:00
|
|
|
$stranswer = get_string("answer", "quiz");
|
|
|
|
$strmarks = get_string("marks", "quiz");
|
|
|
|
|
|
|
|
echo "<TABLE WIDTH=100% CELLSPACING=10><TR><TD NOWRAP WIDTH=100 VALIGN=top>";
|
2002-10-16 09:35:04 +00:00
|
|
|
echo "<P ALIGN=CENTER><B>$number</B></P>";
|
2002-10-16 15:52:29 +00:00
|
|
|
if ($feedback or $response) {
|
2002-10-16 09:35:04 +00:00
|
|
|
echo "<P ALIGN=CENTER><FONT SIZE=1>$strmarks: $actualgrade/$grade</FONT></P>";
|
|
|
|
} else {
|
|
|
|
echo "<P ALIGN=CENTER><FONT SIZE=1>$grade $strmarks</FONT></P>";
|
|
|
|
}
|
2002-10-06 17:06:54 +00:00
|
|
|
print_spacer(1,100);
|
2003-04-09 13:57:08 +00:00
|
|
|
echo "</TD><TD VALIGN=TOP>";
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
if (empty($realquestion)) {
|
|
|
|
$realquestion->id = $question->id;
|
|
|
|
} else { // Add a marker to connect this question to the actual random parent
|
|
|
|
echo "<input type=\"hidden\" name=\"q{$realquestion->id}rq$question->id\" value=\"x\">\n";
|
|
|
|
}
|
2002-10-06 17:06:54 +00:00
|
|
|
|
2003-01-03 16:01:48 +00:00
|
|
|
switch ($question->qtype) {
|
2003-04-09 06:32:51 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
case SHORTANSWER:
|
2002-10-06 17:06:54 +00:00
|
|
|
if (!$options = get_record("quiz_shortanswer", "question", $question->id)) {
|
|
|
|
notify("Error: Missing question options!");
|
|
|
|
}
|
2002-10-15 10:04:28 +00:00
|
|
|
echo text_to_html($question->questiontext);
|
2002-10-06 17:06:54 +00:00
|
|
|
if ($question->image) {
|
2003-01-20 15:43:09 +00:00
|
|
|
print_file_picture($question->image, $courseid, QUIZ_PICTURE_DEFAULT_HEIGHT);
|
2002-10-06 17:06:54 +00:00
|
|
|
}
|
2002-10-16 09:35:04 +00:00
|
|
|
if ($response) {
|
|
|
|
$value = "VALUE=\"$response[0]\"";
|
2003-01-03 16:01:48 +00:00
|
|
|
} else {
|
|
|
|
$value = "";
|
2002-10-16 09:35:04 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
echo "<P ALIGN=RIGHT>$stranswer: <INPUT TYPE=TEXT NAME=q$realquestion->id SIZE=20 $value></P>";
|
2002-10-16 09:35:04 +00:00
|
|
|
if ($feedback) {
|
|
|
|
quiz_print_comment("<P ALIGN=right>$feedback[0]</P>");
|
|
|
|
}
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($correct) {
|
2002-10-20 17:15:39 +00:00
|
|
|
$correctanswers = implode(", ", $correct);
|
2002-10-20 05:35:25 +00:00
|
|
|
quiz_print_correctanswer($correctanswers);
|
|
|
|
}
|
2002-10-06 17:06:54 +00:00
|
|
|
break;
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
case TRUEFALSE:
|
2002-10-06 17:06:54 +00:00
|
|
|
if (!$options = get_record("quiz_truefalse", "question", $question->id)) {
|
|
|
|
notify("Error: Missing question options!");
|
|
|
|
}
|
2003-01-03 16:01:48 +00:00
|
|
|
if (!$true = get_record("quiz_answers", "id", $options->trueanswer)) {
|
2002-10-06 17:06:54 +00:00
|
|
|
notify("Error: Missing question answers!");
|
|
|
|
}
|
2003-01-03 16:01:48 +00:00
|
|
|
if (!$false = get_record("quiz_answers", "id", $options->falseanswer)) {
|
2002-10-06 17:06:54 +00:00
|
|
|
notify("Error: Missing question answers!");
|
|
|
|
}
|
|
|
|
if (!$true->answer) {
|
|
|
|
$true->answer = get_string("true", "quiz");
|
|
|
|
}
|
|
|
|
if (!$false->answer) {
|
|
|
|
$false->answer = get_string("false", "quiz");
|
|
|
|
}
|
2002-10-15 10:04:28 +00:00
|
|
|
echo text_to_html($question->questiontext);
|
2002-10-06 17:06:54 +00:00
|
|
|
if ($question->image) {
|
2003-01-20 15:43:09 +00:00
|
|
|
print_file_picture($question->image, $courseid, QUIZ_PICTURE_DEFAULT_HEIGHT);
|
2002-10-06 17:06:54 +00:00
|
|
|
}
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-01-03 16:01:48 +00:00
|
|
|
$truechecked = "";
|
|
|
|
$falsechecked = "";
|
|
|
|
|
|
|
|
if (!empty($response[$true->id])) {
|
2002-10-16 09:35:04 +00:00
|
|
|
$truechecked = "CHECKED";
|
|
|
|
$feedbackid = $true->id;
|
2003-01-03 16:01:48 +00:00
|
|
|
} else if (!empty($response[$false->id])) {
|
2002-10-16 09:35:04 +00:00
|
|
|
$falsechecked = "CHECKED";
|
|
|
|
$feedbackid = $false->id;
|
|
|
|
}
|
2003-01-03 16:01:48 +00:00
|
|
|
|
|
|
|
$truecorrect = "";
|
|
|
|
$falsecorrect = "";
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($correct) {
|
2003-01-03 16:01:48 +00:00
|
|
|
if (!empty($correct[$true->id])) {
|
2002-10-20 05:35:25 +00:00
|
|
|
$truecorrect = "CLASS=highlight";
|
|
|
|
}
|
2003-01-03 16:01:48 +00:00
|
|
|
if (!empty($correct[$false->id])) {
|
2002-10-20 05:35:25 +00:00
|
|
|
$falsecorrect = "CLASS=highlight";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "<TABLE ALIGN=right cellpadding=5><TR><TD align=right>$stranswer: ";
|
|
|
|
echo "<TD $truecorrect>";
|
2003-04-09 06:32:51 +00:00
|
|
|
echo "<INPUT $truechecked TYPE=RADIO NAME=\"q$realquestion->id\" VALUE=\"$true->id\">$true->answer";
|
2002-10-20 05:35:25 +00:00
|
|
|
echo "</TD><TD $falsecorrect>";
|
2003-04-11 14:44:12 +00:00
|
|
|
echo "<INPUT $falsechecked TYPE=RADIO NAME=\"q$realquestion->id\" VALUE=\"$false->id\">$false->answer";
|
2002-10-20 05:35:25 +00:00
|
|
|
echo "</TD></TR></TABLE><BR CLEAR=ALL>";
|
2002-10-16 09:35:04 +00:00
|
|
|
if ($feedback) {
|
|
|
|
quiz_print_comment("<P ALIGN=right>$feedback[$feedbackid]</P>");
|
|
|
|
}
|
|
|
|
|
2002-10-06 17:06:54 +00:00
|
|
|
break;
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
case MULTICHOICE:
|
2002-10-06 17:06:54 +00:00
|
|
|
if (!$options = get_record("quiz_multichoice", "question", $question->id)) {
|
|
|
|
notify("Error: Missing question options!");
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
if (!$answers = get_records_list("quiz_answers", "id", $options->answers)) {
|
2002-10-06 17:06:54 +00:00
|
|
|
notify("Error: Missing question answers!");
|
|
|
|
}
|
2002-10-15 10:04:28 +00:00
|
|
|
echo text_to_html($question->questiontext);
|
2002-10-06 17:06:54 +00:00
|
|
|
if ($question->image) {
|
2003-01-20 15:43:09 +00:00
|
|
|
print_file_picture($question->image, $courseid, QUIZ_PICTURE_DEFAULT_HEIGHT);
|
2002-10-06 17:06:54 +00:00
|
|
|
}
|
|
|
|
echo "<TABLE ALIGN=right>";
|
|
|
|
echo "<TR><TD valign=top>$stranswer: </TD><TD>";
|
2002-11-12 17:20:04 +00:00
|
|
|
echo "<TABLE>";
|
2002-10-06 17:06:54 +00:00
|
|
|
$answerids = explode(",", $options->answers);
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-04-09 13:57:08 +00:00
|
|
|
if ($shuffleanswers) {
|
|
|
|
$answerids = swapshuffle($answerids);
|
|
|
|
}
|
|
|
|
|
2002-10-06 17:06:54 +00:00
|
|
|
foreach ($answerids as $key => $answerid) {
|
|
|
|
$answer = $answers[$answerid];
|
2003-02-16 08:03:17 +00:00
|
|
|
$qnumchar = chr(ord('a') + $key);
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-01-01 14:47:11 +00:00
|
|
|
if (empty($feedback) or empty($response[$answerid])) {
|
2002-10-16 09:35:04 +00:00
|
|
|
$checked = "";
|
2003-01-01 14:47:11 +00:00
|
|
|
} else {
|
|
|
|
$checked = "CHECKED";
|
2002-10-16 09:35:04 +00:00
|
|
|
}
|
2002-10-06 17:06:54 +00:00
|
|
|
echo "<TR><TD valign=top>";
|
2002-10-13 07:17:48 +00:00
|
|
|
if ($options->single) {
|
2003-04-09 06:32:51 +00:00
|
|
|
echo "<INPUT $checked TYPE=RADIO NAME=q$realquestion->id VALUE=\"$answer->id\">";
|
2002-10-06 17:06:54 +00:00
|
|
|
} else {
|
2003-04-09 06:32:51 +00:00
|
|
|
echo "<INPUT $checked TYPE=CHECKBOX NAME=q$realquestion->id"."a$answer->id VALUE=\"$answer->id\">";
|
2002-10-06 17:06:54 +00:00
|
|
|
}
|
|
|
|
echo "</TD>";
|
2003-01-01 14:47:11 +00:00
|
|
|
if (empty($feedback) or empty($correct[$answer->id])) {
|
2003-02-16 08:03:17 +00:00
|
|
|
echo "<TD valign=top>$qnumchar. $answer->answer</TD>";
|
2003-01-01 14:47:11 +00:00
|
|
|
} else {
|
2003-02-16 08:03:17 +00:00
|
|
|
echo "<TD valign=top CLASS=highlight>$qnumchar. $answer->answer</TD>";
|
2002-10-20 05:35:25 +00:00
|
|
|
}
|
2003-01-01 14:47:11 +00:00
|
|
|
if (!empty($feedback)) {
|
2002-10-16 09:35:04 +00:00
|
|
|
echo "<TD valign=top> ";
|
2003-01-01 14:47:11 +00:00
|
|
|
if (!empty($response[$answerid])) {
|
2002-10-16 09:35:04 +00:00
|
|
|
quiz_print_comment($feedback[$answerid]);
|
|
|
|
}
|
|
|
|
echo "</TD>";
|
|
|
|
}
|
2002-10-06 17:06:54 +00:00
|
|
|
echo "</TR>";
|
|
|
|
}
|
|
|
|
echo "</TABLE>";
|
|
|
|
echo "</TABLE>";
|
|
|
|
break;
|
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
case MATCH:
|
|
|
|
if (!$options = get_record("quiz_match", "question", $question->id)) {
|
|
|
|
notify("Error: Missing question options!");
|
|
|
|
}
|
|
|
|
if (!$subquestions = get_records_list("quiz_match_sub", "id", $options->subquestions)) {
|
|
|
|
notify("Error: Missing subquestions for this question!");
|
|
|
|
}
|
2003-04-09 13:57:08 +00:00
|
|
|
if ($shuffleanswers) {
|
|
|
|
$subquestions = draw_rand_array($subquestions, count($subquestions));
|
|
|
|
}
|
2003-03-30 16:46:50 +00:00
|
|
|
echo text_to_html($question->questiontext);
|
|
|
|
if ($question->image) {
|
|
|
|
print_file_picture($question->image, $courseid, QUIZ_PICTURE_DEFAULT_HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($subquestions as $subquestion) {
|
|
|
|
$answers[$subquestion->id] = $subquestion->answertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
$answers = draw_rand_array($answers, count($answers));
|
|
|
|
|
|
|
|
echo "<table border=0 cellpadding=10 align=right>";
|
|
|
|
foreach ($subquestions as $key => $subquestion) {
|
|
|
|
echo "<tr><td align=left valign=top>";
|
|
|
|
echo $subquestion->questiontext;
|
|
|
|
echo "</td>";
|
|
|
|
if (empty($response)) {
|
|
|
|
echo "<td align=right valign=top>";
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($answers, "q$realquestion->id"."r$subquestion->id");
|
2003-03-30 16:46:50 +00:00
|
|
|
} else {
|
|
|
|
if (empty($response[$key])) {
|
|
|
|
echo "<td align=right valign=top>";
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($answers, "q$realquestion->id"."r$subquestion->id");
|
2003-03-30 16:46:50 +00:00
|
|
|
} else {
|
|
|
|
if ($response[$key] == $correct[$key]) {
|
|
|
|
echo "<td align=right valign=top class=highlight>";
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($answers, "q$realquestion->id"."r$subquestion->id", $response[$key]);
|
2003-03-30 16:46:50 +00:00
|
|
|
} else {
|
|
|
|
echo "<td align=right valign=top>";
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($answers, "q$realquestion->id"."r$subquestion->id", $response[$key]);
|
2003-03-30 16:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($feedback[$key])) {
|
|
|
|
quiz_print_comment($feedback[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "</td></tr>";
|
|
|
|
}
|
|
|
|
echo "</table>";
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMSAMATCH:
|
|
|
|
if (!$options = get_record("quiz_randomsamatch", "question", $question->id)) {
|
2003-02-24 10:37:56 +00:00
|
|
|
notify("Error: Missing question options!");
|
|
|
|
}
|
|
|
|
echo text_to_html($question->questiontext);
|
|
|
|
if ($question->image) {
|
|
|
|
print_file_picture($question->image, $courseid, QUIZ_PICTURE_DEFAULT_HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// First, get all the questions available
|
|
|
|
|
|
|
|
$allquestions = get_records_select("quiz_questions",
|
|
|
|
"category = $question->category AND qtype = ".SHORTANSWER);
|
|
|
|
if (count($allquestions) < $options->choose) {
|
|
|
|
notify("Error: could not find enough Short Answer questions in the database!");
|
|
|
|
notify("Found ".count($allquestions).", need $options->choose.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($response)) { // Randomly pick the questions
|
|
|
|
if (!$randomquestions = draw_rand_array($allquestions, $options->choose)) {
|
|
|
|
notify("Error choosing $options->choose random questions");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { // Use existing questions
|
|
|
|
$randomquestions = array();
|
|
|
|
foreach ($response as $key => $rrr) {
|
|
|
|
$rrr = explode("-", $rrr);
|
|
|
|
$randomquestions[$key] = $allquestions[$key];
|
|
|
|
$responseanswer[$key] = $rrr[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For each selected, find the best matching answers
|
|
|
|
|
|
|
|
foreach ($randomquestions as $randomquestion) {
|
|
|
|
$shortanswerquestion = get_record("quiz_shortanswer", "question", $randomquestion->id);
|
|
|
|
$questionanswers = get_records_list("quiz_answers", "id", $shortanswerquestion->answers);
|
|
|
|
$bestfraction = 0;
|
|
|
|
$bestanswer = NULL;
|
|
|
|
foreach ($questionanswers as $questionanswer) {
|
|
|
|
if ($questionanswer->fraction > $bestfraction) {
|
|
|
|
$bestanswer = $questionanswer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($bestanswer)) {
|
|
|
|
notify("Error: Could not find the best answer for question: ".$randomquestions->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$randomanswers[$bestanswer->id] = trim($bestanswer->answer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$randomanswers = draw_rand_array($randomanswers, $options->choose)) { // Mix them up
|
|
|
|
notify("Error randomising answers!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "<table border=0 cellpadding=10>";
|
|
|
|
foreach ($randomquestions as $key => $randomquestion) {
|
|
|
|
echo "<tr><td align=left valign=top>";
|
|
|
|
echo $randomquestion->questiontext;
|
|
|
|
echo "</td>";
|
|
|
|
echo "<td align=right valign=top>";
|
|
|
|
if (empty($response)) {
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($randomanswers, "q$realquestion->id"."r$randomquestion->id");
|
2003-02-24 10:37:56 +00:00
|
|
|
} else {
|
|
|
|
if (!empty($correct[$key])) {
|
|
|
|
if ($randomanswers[$responseanswer[$key]] == $correct[$key]) {
|
|
|
|
echo "<span=highlight>";
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($randomanswers, "q$realquestion->id"."r$randomquestion->id", $responseanswer[$key]);
|
2003-02-24 10:37:56 +00:00
|
|
|
echo "</span><br \>";
|
|
|
|
} else {
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($randomanswers, "q$realquestion->id"."r$randomquestion->id", $responseanswer[$key]);
|
2003-02-24 10:37:56 +00:00
|
|
|
quiz_print_correctanswer($correct[$key]);
|
|
|
|
}
|
|
|
|
} else {
|
2003-04-09 06:32:51 +00:00
|
|
|
choose_from_menu($randomanswers, "q$realquestion->id"."r$randomquestion->id", $responseanswer[$key]);
|
2003-02-24 10:37:56 +00:00
|
|
|
}
|
|
|
|
if (!empty($feedback[$key])) {
|
|
|
|
quiz_print_comment($feedback[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "</td></tr>";
|
|
|
|
}
|
|
|
|
echo "</table>";
|
|
|
|
break;
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
case RANDOM:
|
|
|
|
echo "<P>Random questions should not be printed this way!</P>";
|
|
|
|
break;
|
2002-10-20 05:35:25 +00:00
|
|
|
|
2002-10-06 17:06:54 +00:00
|
|
|
default:
|
|
|
|
notify("Error: Unknown question type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "</TD></TR></TABLE>";
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
function quiz_print_quiz_questions($quiz, $results=NULL, $questions=NULL) {
|
2002-10-13 07:17:48 +00:00
|
|
|
// Prints a whole quiz on one page.
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
/// Get the questions
|
|
|
|
|
|
|
|
if (!$questions) {
|
2003-04-09 07:23:42 +00:00
|
|
|
if (empty($quiz->questions)) {
|
|
|
|
notify("No questions have been defined!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
if (!$questions = get_records_list("quiz_questions", "id", $quiz->questions, "")) {
|
2003-04-09 07:23:42 +00:00
|
|
|
notify("Error when reading questions from the database!");
|
2003-04-09 06:32:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-04-09 13:57:08 +00:00
|
|
|
|
|
|
|
if (!empty($quiz->shufflequestions)) {
|
|
|
|
$questions = swapshuffle_assoc($questions);
|
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
}
|
|
|
|
|
2003-04-09 07:23:42 +00:00
|
|
|
if (!$grades = get_records_list("quiz_question_grades", "question", $quiz->questions, "", "question,grade")) {
|
|
|
|
notify("No grades were found for these questions!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
/// Examine the set of questions for random questions, and retrieve them
|
|
|
|
|
2003-04-09 07:23:42 +00:00
|
|
|
if (empty($results)) { // Choose some new random questions
|
2003-04-09 06:32:51 +00:00
|
|
|
if ($randomcats = quiz_get_random_categories($quiz->questions)) {
|
|
|
|
foreach ($randomcats as $randomcat => $randomdraw) {
|
|
|
|
/// Get the appropriate amount of random questions from this category
|
|
|
|
if (!$catquestions[$randomcat] = quiz_choose_random_questions($randomcat, $randomdraw)) {
|
2003-04-09 07:23:42 +00:00
|
|
|
notify(get_string("toomanyrandom", "quiz", $randomcat));
|
2003-04-09 06:32:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-09 07:23:42 +00:00
|
|
|
} else { // Get the previously chosen questions
|
|
|
|
$chosen = array();
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
if (isset($question->random)) {
|
|
|
|
$chosen[] = $question->random;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($chosen) {
|
|
|
|
$chosenlist = implode(",", $chosen);
|
|
|
|
if (!$chosen = get_records_list("quiz_questions", "id", $chosenlist, "")) {
|
|
|
|
notify("Error when reading questions from the database!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
|
2002-12-09 08:20:19 +00:00
|
|
|
$strconfirmattempt = addslashes(get_string("readytosend", "quiz"));
|
|
|
|
|
|
|
|
echo "<FORM METHOD=POST ACTION=attempt.php onsubmit=\"return confirm('$strconfirmattempt');\">";
|
2002-10-13 07:17:48 +00:00
|
|
|
echo "<INPUT TYPE=hidden NAME=q VALUE=\"$quiz->id\">";
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
$count = 0;
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
$count++;
|
|
|
|
|
2002-10-20 05:35:25 +00:00
|
|
|
$feedback = NULL;
|
|
|
|
$response = NULL;
|
|
|
|
$actualgrades = NULL;
|
|
|
|
$correct = NULL;
|
2003-04-09 06:32:51 +00:00
|
|
|
$randomquestion = NULL;
|
|
|
|
|
|
|
|
if (empty($results)) {
|
|
|
|
if ($question->qtype == RANDOM ) { // Set up random questions
|
|
|
|
$randomquestion = $question;
|
|
|
|
$question = array_pop($catquestions[$randomquestion->category]);
|
|
|
|
$grades[$question->id]->grade = $grades[$randomquestion->id]->grade;
|
2003-01-01 14:47:11 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
} else {
|
|
|
|
if (!empty($results->feedback[$question->id])) {
|
|
|
|
$feedback = $results->feedback[$question->id];
|
2003-01-01 14:47:11 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
if (!empty($results->response[$question->id])) {
|
|
|
|
$response = $results->response[$question->id];
|
|
|
|
}
|
|
|
|
if (!empty($results->grades[$question->id])) {
|
|
|
|
$actualgrades = $results->grades[$question->id];
|
2003-01-01 14:47:11 +00:00
|
|
|
}
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($quiz->correctanswers) {
|
2003-04-09 06:32:51 +00:00
|
|
|
if (!empty($results->correct[$question->id])) {
|
|
|
|
$correct = $results->correct[$question->id];
|
2003-01-01 14:47:11 +00:00
|
|
|
}
|
2002-10-20 05:35:25 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
if (!empty($question->random)) {
|
|
|
|
$randomquestion = $question;
|
2003-04-09 07:23:42 +00:00
|
|
|
$question = $chosen[$question->random];
|
2003-04-09 06:32:51 +00:00
|
|
|
$grades[$question->id]->grade = $grades[$randomquestion->id]->grade;
|
|
|
|
}
|
2002-10-16 09:35:04 +00:00
|
|
|
}
|
2002-10-20 05:35:25 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
print_simple_box_start("CENTER", "90%");
|
2003-04-09 06:32:51 +00:00
|
|
|
quiz_print_question($count, $question, $grades[$question->id]->grade, $quiz->course,
|
2003-04-09 13:57:08 +00:00
|
|
|
$feedback, $response, $actualgrades, $correct,
|
|
|
|
$randomquestion, $quiz->shuffleanswers);
|
2002-10-13 07:17:48 +00:00
|
|
|
print_simple_box_end();
|
|
|
|
echo "<BR>";
|
|
|
|
}
|
2002-10-16 09:35:04 +00:00
|
|
|
|
2003-01-01 14:47:11 +00:00
|
|
|
if (empty($results)) {
|
2002-10-16 09:35:04 +00:00
|
|
|
echo "<CENTER><INPUT TYPE=submit VALUE=\"".get_string("savemyanswers", "quiz")."\"></CENTER>";
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
echo "</FORM>";
|
2002-10-14 15:57:33 +00:00
|
|
|
|
|
|
|
return true;
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
|
2002-10-14 09:07:13 +00:00
|
|
|
|
|
|
|
function quiz_get_default_category($courseid) {
|
2003-04-09 06:32:51 +00:00
|
|
|
/// Returns the current category
|
|
|
|
|
2002-10-14 09:07:13 +00:00
|
|
|
if ($categories = get_records("quiz_categories", "course", $courseid, "id")) {
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
return $category; // Return the first one (lowest id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to make one
|
2002-10-14 15:57:33 +00:00
|
|
|
$category->name = get_string("default", "quiz");
|
|
|
|
$category->info = get_string("defaultinfo", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
$category->course = $courseid;
|
|
|
|
$category->publish = 0;
|
|
|
|
|
|
|
|
if (!$category->id = insert_record("quiz_categories", $category)) {
|
|
|
|
notify("Error creating a default category!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $category;
|
|
|
|
}
|
|
|
|
|
2002-10-20 17:15:39 +00:00
|
|
|
function quiz_get_category_menu($courseid, $published=false) {
|
2003-04-10 13:11:34 +00:00
|
|
|
/// Returns the list of categories
|
|
|
|
$publish = "";
|
2002-10-20 17:15:39 +00:00
|
|
|
if ($published) {
|
|
|
|
$publish = "OR publish = '1'";
|
|
|
|
}
|
2002-12-22 13:55:30 +00:00
|
|
|
return get_records_select_menu("quiz_categories", "course='$courseid' $publish", "name ASC", "id,name");
|
2002-10-20 17:15:39 +00:00
|
|
|
}
|
|
|
|
|
2002-10-14 09:07:13 +00:00
|
|
|
function quiz_print_category_form($course, $current) {
|
|
|
|
// Prints a form to choose categories
|
|
|
|
|
2002-12-22 13:55:30 +00:00
|
|
|
if (!$categories = get_records_select("quiz_categories", "course='$course->id' OR publish = '1'", "name ASC")) {
|
2002-10-14 09:07:13 +00:00
|
|
|
if (!$category = quiz_get_default_category($course->id)) {
|
|
|
|
notify("Error creating a default category!");
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-26 05:17:53 +00:00
|
|
|
$categories[$category->id] = $category;
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
2002-10-15 12:54:11 +00:00
|
|
|
foreach ($categories as $key => $category) {
|
|
|
|
if ($category->publish) {
|
2002-10-20 17:41:24 +00:00
|
|
|
if ($catcourse = get_record("course", "id", $category->course)) {
|
|
|
|
$category->name .= " ($catcourse->shortname)";
|
2002-10-15 12:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-20 17:41:24 +00:00
|
|
|
$catmenu[$category->id] = $category->name;
|
2002-10-15 12:54:11 +00:00
|
|
|
}
|
2002-10-14 09:07:13 +00:00
|
|
|
$strcategory = get_string("category", "quiz");
|
|
|
|
$strshow = get_string("show", "quiz");
|
2002-10-15 16:45:39 +00:00
|
|
|
$streditcats = get_string("editcategories", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TABLE width=\"100%\"><TR><TD NOWRAP>";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<FORM METHOD=POST ACTION=edit.php>";
|
|
|
|
echo "<B>$strcategory:</B> ";
|
2002-10-20 17:41:24 +00:00
|
|
|
choose_from_menu($catmenu, "cat", "$current");
|
2002-10-15 17:32:50 +00:00
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$strshow\">";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "</FORM>";
|
2002-10-15 16:45:39 +00:00
|
|
|
echo "</TD><TD align=right>";
|
|
|
|
echo "<FORM METHOD=GET ACTION=category.php>";
|
|
|
|
echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
|
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$streditcats\">";
|
|
|
|
echo "</FORM>";
|
|
|
|
echo "</TD></TR></TABLE>";
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
function quiz_choose_random_questions($category, $draws) {
|
|
|
|
/// Given a question category and a number of draws, this function
|
|
|
|
/// creates a random subset of that size - returned as an array of questions
|
|
|
|
|
|
|
|
if (!$pool = get_records_select_menu("quiz_questions",
|
|
|
|
"category = '$category' AND qtype <> ".RANDOM, "", "id,qtype")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$countpool = count($pool);
|
|
|
|
|
|
|
|
if ($countpool == $draws) {
|
|
|
|
$chosen = $pool;
|
|
|
|
} else if ($countpool < $draws) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$chosen = draw_rand_array($pool, $draws);
|
|
|
|
}
|
|
|
|
|
|
|
|
$chosenlist = implode(",", array_keys($chosen));
|
|
|
|
return get_records_list("quiz_questions", "id", $chosenlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
function quiz_get_all_question_grades($questionlist, $quizid) {
|
|
|
|
// Given a list of question IDs, finds grades or invents them to
|
|
|
|
// create an array of matching grades
|
|
|
|
|
2003-04-14 13:52:59 +00:00
|
|
|
if (empty($questionlist)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2002-12-22 13:55:30 +00:00
|
|
|
$questions = quiz_get_question_grades($quizid, $questionlist);
|
2002-10-14 12:21:18 +00:00
|
|
|
|
|
|
|
$list = explode(",", $questionlist);
|
|
|
|
$grades = array();
|
|
|
|
|
|
|
|
foreach ($list as $qid) {
|
|
|
|
if (isset($questions[$qid])) {
|
|
|
|
$grades[$qid] = $questions[$qid]->grade;
|
|
|
|
} else {
|
|
|
|
$grades[$qid] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_print_question_list($questionlist, $grades) {
|
2002-10-14 09:07:13 +00:00
|
|
|
// Prints a list of quiz questions in a small layout form with knobs
|
2002-10-14 12:21:18 +00:00
|
|
|
// $questionlist is comma-separated list
|
|
|
|
// $grades is an array of corresponding grades
|
2002-10-14 09:07:13 +00:00
|
|
|
|
|
|
|
global $THEME;
|
|
|
|
|
|
|
|
if (!$questionlist) {
|
|
|
|
echo "<P align=center>";
|
|
|
|
print_string("noquestions", "quiz");
|
|
|
|
echo "</P>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = explode(",", $questionlist);
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
if (!$questions = get_records_list("quiz_questions", "id", $questionlist)) {
|
2002-10-14 09:07:13 +00:00
|
|
|
error("No questions were found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$strorder = get_string("order");
|
|
|
|
$strquestionname = get_string("questionname", "quiz");
|
|
|
|
$strgrade = get_string("grade");
|
|
|
|
$strdelete = get_string("delete");
|
|
|
|
$stredit = get_string("edit");
|
|
|
|
$strmoveup = get_string("moveup");
|
|
|
|
$strmovedown = get_string("movedown");
|
|
|
|
$strsavegrades = get_string("savegrades", "quiz");
|
2002-10-20 17:15:39 +00:00
|
|
|
$strtype = get_string("type", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
for ($i=10; $i>=0; $i--) {
|
2002-10-14 12:21:18 +00:00
|
|
|
$gradesmenu[$i] = $i;
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
|
|
|
$count = 0;
|
|
|
|
$sumgrade = 0;
|
|
|
|
$total = count($order);
|
|
|
|
echo "<FORM METHOD=post ACTION=edit.php>";
|
|
|
|
echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TR><TH WIDTH=\"*\" COLSPAN=3 NOWRAP>$strorder</TH><TH align=left WIDTH=\"100%\" NOWRAP>$strquestionname</TH><TH width=\"*\" NOWRAP>$strtype</TH><TH WIDTH=\"*\" NOWRAP>$strgrade</TH><TH WIDTH=\"*\" NOWRAP>$stredit</TH></TR>";
|
2002-10-14 09:07:13 +00:00
|
|
|
foreach ($order as $qnum) {
|
2003-02-24 10:37:56 +00:00
|
|
|
if (empty($questions[$qnum])) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-14 09:07:13 +00:00
|
|
|
$count++;
|
|
|
|
echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
|
|
|
|
echo "<TD>$count</TD>";
|
|
|
|
echo "<TD>";
|
|
|
|
if ($count != 1) {
|
|
|
|
echo "<A TITLE=\"$strmoveup\" HREF=\"edit.php?up=$qnum\"><IMG
|
|
|
|
SRC=\"../../pix/t/up.gif\" BORDER=0></A>";
|
|
|
|
}
|
|
|
|
echo "</TD>";
|
|
|
|
echo "<TD>";
|
|
|
|
if ($count != $total) {
|
|
|
|
echo "<A TITLE=\"$strmovedown\" HREF=\"edit.php?down=$qnum\"><IMG
|
|
|
|
SRC=\"../../pix/t/down.gif\" BORDER=0></A>";
|
|
|
|
}
|
|
|
|
echo "</TD>";
|
|
|
|
echo "<TD>".$questions[$qnum]->name."</TD>";
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TD ALIGN=CENTER>";
|
2002-10-20 17:15:39 +00:00
|
|
|
quiz_print_question_icon($questions[$qnum]);
|
|
|
|
echo "</TD>";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<TD>";
|
2002-10-15 12:54:11 +00:00
|
|
|
choose_from_menu($gradesmenu, "q$qnum", (string)$grades[$qnum], "");
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<TD>";
|
|
|
|
echo "<A TITLE=\"$strdelete\" HREF=\"edit.php?delete=$qnum\"><IMG
|
|
|
|
SRC=\"../../pix/t/delete.gif\" BORDER=0></A> ";
|
|
|
|
echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$qnum\"><IMG
|
|
|
|
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
|
|
|
|
echo "</TD>";
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
$sumgrade += $grades[$qnum];
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
2002-10-20 17:15:39 +00:00
|
|
|
echo "<TR><TD COLSPAN=5 ALIGN=right>";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">";
|
2002-10-15 12:54:11 +00:00
|
|
|
echo "<INPUT TYPE=hidden NAME=setgrades VALUE=\"save\">";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<TD ALIGN=LEFT BGCOLOR=\"$THEME->cellcontent\">";
|
|
|
|
echo "<B>$sumgrade</B>";
|
|
|
|
echo "</TD><TD></TD></TR>";
|
|
|
|
echo "</TABLE>";
|
|
|
|
echo "</FORM>";
|
2002-10-14 15:57:33 +00:00
|
|
|
|
|
|
|
return $sumgrade;
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_print_cat_question_list($categoryid) {
|
|
|
|
// Prints a form to choose categories
|
|
|
|
|
|
|
|
global $THEME, $QUIZ_QUESTION_TYPE;
|
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
$strcategory = get_string("category", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
$strquestion = get_string("question", "quiz");
|
2003-02-16 07:08:57 +00:00
|
|
|
$straddquestions = get_string("addquestions", "quiz");
|
|
|
|
$strimportquestions = get_string("importquestions", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
$strnoquestions = get_string("noquestions", "quiz");
|
|
|
|
$strselect = get_string("select", "quiz");
|
2003-02-17 18:13:41 +00:00
|
|
|
$strselectall = get_string("selectall", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
$strcreatenewquestion = get_string("createnewquestion", "quiz");
|
|
|
|
$strquestionname = get_string("questionname", "quiz");
|
|
|
|
$strdelete = get_string("delete");
|
|
|
|
$stredit = get_string("edit");
|
|
|
|
$straddselectedtoquiz = get_string("addselectedtoquiz", "quiz");
|
2002-10-20 17:15:39 +00:00
|
|
|
$strtype = get_string("type", "quiz");
|
2003-04-10 17:55:57 +00:00
|
|
|
$strcreatemultiple = get_string("createmultiple", "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
|
|
|
|
if (!$categoryid) {
|
|
|
|
echo "<P align=center>";
|
|
|
|
print_string("selectcategoryabove", "quiz");
|
|
|
|
echo "</P>";
|
|
|
|
return;
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2002-10-14 09:07:13 +00:00
|
|
|
if (!$category = get_record("quiz_categories", "id", "$categoryid")) {
|
|
|
|
notify("Category not found!");
|
|
|
|
return;
|
|
|
|
}
|
2002-10-15 12:54:11 +00:00
|
|
|
echo "<CENTER>";
|
2002-10-14 15:57:33 +00:00
|
|
|
echo text_to_html($category->info);
|
2002-10-14 09:07:13 +00:00
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
echo "<TABLE><TR>";
|
|
|
|
echo "<TD valign=top><B>$straddquestions:</B></TD>";
|
|
|
|
echo "<TD valign=top align=right>";
|
2002-10-14 15:57:33 +00:00
|
|
|
echo "<FORM METHOD=GET ACTION=question.php>";
|
2003-01-03 16:01:48 +00:00
|
|
|
choose_from_menu($QUIZ_QUESTION_TYPE, "qtype", "", "");
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
|
2003-01-20 15:43:09 +00:00
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$strcreatenewquestion\">";
|
2002-10-22 10:38:00 +00:00
|
|
|
helpbutton("questiontypes", $strcreatenewquestion, "quiz");
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "</FORM>";
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
echo "<FORM METHOD=GET ACTION=import.php>";
|
|
|
|
echo "<INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
|
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$strimportquestions\">";
|
|
|
|
helpbutton("import", $strimportquestions, "quiz");
|
|
|
|
echo "</FORM>";
|
|
|
|
|
2003-04-10 17:55:57 +00:00
|
|
|
echo "<FORM METHOD=GET ACTION=multiple.php>";
|
|
|
|
echo "<INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
|
|
|
|
echo "<INPUT TYPE=submit VALUE=\"$strcreatemultiple\">";
|
|
|
|
helpbutton("createmultiple", $strcreatemultiple, "quiz");
|
|
|
|
echo "</FORM>";
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
echo "</TR></TABLE>";
|
|
|
|
|
2002-10-15 12:54:11 +00:00
|
|
|
echo "</CENTER>";
|
2002-10-14 09:07:13 +00:00
|
|
|
|
2003-03-01 05:11:44 +00:00
|
|
|
if (!$questions = get_records("quiz_questions", "category", $category->id, "qtype ASC")) {
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<P align=center>";
|
|
|
|
print_string("noquestions", "quiz");
|
|
|
|
echo "</P>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-10-14 15:57:33 +00:00
|
|
|
$canedit = isteacher($category->course);
|
|
|
|
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "<FORM METHOD=post ACTION=edit.php>";
|
|
|
|
echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TR><TH width=\"*\" NOWRAP>$strselect</TH><TH width=\"100%\" align=left NOWRAP>$strquestionname</TH><TH WIDTH=\"*\" NOWRAP>$strtype</TH>";
|
2002-10-14 15:57:33 +00:00
|
|
|
if ($canedit) {
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TH width=\"*\" NOWRAP>$stredit</TH>";
|
2002-10-14 15:57:33 +00:00
|
|
|
}
|
|
|
|
echo "</TR>";
|
2002-10-14 09:07:13 +00:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
|
|
|
|
echo "<TD ALIGN=CENTER>";
|
|
|
|
echo "<INPUT TYPE=CHECKBOX NAME=q$question->id VALUE=\"1\">";
|
|
|
|
echo "</TD>";
|
|
|
|
echo "<TD>".$question->name."</TD>";
|
2002-12-07 08:39:24 +00:00
|
|
|
echo "<TD ALIGN=CENTER>";
|
2002-10-20 17:15:39 +00:00
|
|
|
quiz_print_question_icon($question);
|
|
|
|
echo "</TD>";
|
2002-10-14 15:57:33 +00:00
|
|
|
if ($canedit) {
|
|
|
|
echo "<TD>";
|
2002-10-21 15:00:51 +00:00
|
|
|
echo "<A TITLE=\"$strdelete\" HREF=\"question.php?id=$question->id&delete=$question->id\"><IMG
|
2002-10-14 15:57:33 +00:00
|
|
|
SRC=\"../../pix/t/delete.gif\" BORDER=0></A> ";
|
|
|
|
echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$question->id\"><IMG
|
|
|
|
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
|
|
|
|
echo "</TD></TR>";
|
|
|
|
}
|
|
|
|
echo "</TR>";
|
2002-10-14 09:07:13 +00:00
|
|
|
}
|
|
|
|
echo "<TR><TD COLSPAN=3>";
|
2003-02-17 18:13:41 +00:00
|
|
|
echo "<INPUT TYPE=submit NAME=add VALUE=\"<< $straddselectedtoquiz\">";
|
|
|
|
//echo "<INPUT TYPE=submit NAME=delete VALUE=\"XX Delete selected\">";
|
|
|
|
echo "<INPUT type=button onclick=\"checkall()\" value=\"$strselectall\">";
|
2002-10-14 09:07:13 +00:00
|
|
|
echo "</TD></TR>";
|
|
|
|
echo "</TABLE>";
|
|
|
|
echo "</FORM>";
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2002-10-06 03:23:16 +00:00
|
|
|
|
2002-10-18 16:05:21 +00:00
|
|
|
function quiz_start_attempt($quizid, $userid, $numattempt) {
|
|
|
|
$attempt->quiz = $quizid;
|
2002-12-23 09:39:26 +00:00
|
|
|
$attempt->userid = $userid;
|
2002-10-18 16:05:21 +00:00
|
|
|
$attempt->attempt = $numattempt;
|
|
|
|
$attempt->timestart = time();
|
|
|
|
$attempt->timefinish = 0;
|
|
|
|
$attempt->timemodified = time();
|
|
|
|
|
|
|
|
return insert_record("quiz_attempts", $attempt);
|
|
|
|
}
|
|
|
|
|
|
|
|
function quiz_get_user_attempt_unfinished($quizid, $userid) {
|
|
|
|
// Returns an object containing an unfinished attempt (if there is one)
|
2002-12-23 09:39:26 +00:00
|
|
|
return get_record("quiz_attempts", "quiz", $quizid, "userid", $userid, "timefinish", 0);
|
2002-10-18 16:05:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-06 03:23:16 +00:00
|
|
|
function quiz_get_user_attempts($quizid, $userid) {
|
2002-10-13 07:17:48 +00:00
|
|
|
// Returns a list of all attempts by a user
|
2002-12-23 09:39:26 +00:00
|
|
|
return get_records_select("quiz_attempts", "quiz = '$quizid' AND userid = '$userid' AND timefinish > 0",
|
2002-12-22 13:55:30 +00:00
|
|
|
"attempt ASC");
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 12:54:11 +00:00
|
|
|
|
|
|
|
function quiz_get_user_attempts_string($quiz, $attempts, $bestgrade) {
|
|
|
|
/// Returns a simple little comma-separated list of all attempts,
|
2002-10-22 04:25:58 +00:00
|
|
|
/// with each grade linked to the feedback report and with the best grade highlighted
|
2002-10-15 12:54:11 +00:00
|
|
|
|
|
|
|
$bestgrade = format_float($bestgrade);
|
|
|
|
foreach ($attempts as $attempt) {
|
|
|
|
$attemptgrade = format_float(($attempt->sumgrades / $quiz->sumgrades) * $quiz->grade);
|
|
|
|
if ($attemptgrade == $bestgrade) {
|
2002-10-22 04:25:58 +00:00
|
|
|
$userattempts[] = "<SPAN CLASS=highlight><A HREF=\"report.php?q=$quiz->id&attempt=$attempt->id\">$attemptgrade</A></SPAN>";
|
2002-10-15 12:54:11 +00:00
|
|
|
} else {
|
2002-10-22 04:25:58 +00:00
|
|
|
$userattempts[] = "<A HREF=\"report.php?q=$quiz->id&attempt=$attempt->id\">$attemptgrade</A>";
|
2002-10-15 12:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(",", $userattempts);
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
function quiz_get_best_grade($quizid, $userid) {
|
|
|
|
/// Get the best current grade for a particular user in a quiz
|
2002-12-23 09:39:26 +00:00
|
|
|
if (!$grade = get_record("quiz_grades", "quiz", $quizid, "userid", $userid)) {
|
2002-10-06 03:23:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-21 13:13:33 +00:00
|
|
|
return (round($grade->grade,0));
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 06:52:23 +00:00
|
|
|
function quiz_save_best_grade($quiz, $userid) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Calculates the best grade out of all attempts at a quiz for a user,
|
|
|
|
/// and then saves that grade in the quiz_grades table.
|
|
|
|
|
2002-10-22 06:52:23 +00:00
|
|
|
if (!$attempts = quiz_get_user_attempts($quiz->id, $userid)) {
|
2002-10-21 12:39:46 +00:00
|
|
|
notify("Could not find any user attempts");
|
2002-10-13 07:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$bestgrade = quiz_calculate_best_grade($quiz, $attempts);
|
|
|
|
$bestgrade = (($bestgrade / $quiz->sumgrades) * $quiz->grade);
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
if ($grade = get_record("quiz_grades", "quiz", $quiz->id, "userid", $userid)) {
|
2003-01-03 16:05:44 +00:00
|
|
|
$grade->grade = round($bestgrade, 2);
|
2002-10-13 07:17:48 +00:00
|
|
|
$grade->timemodified = time();
|
|
|
|
if (!update_record("quiz_grades", $grade)) {
|
2002-10-21 12:39:46 +00:00
|
|
|
notify("Could not update best grade");
|
2002-10-13 07:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$grade->quiz = $quiz->id;
|
2002-12-23 09:39:26 +00:00
|
|
|
$grade->userid = $userid;
|
2002-10-21 13:02:14 +00:00
|
|
|
$grade->grade = round($bestgrade, 2);
|
2002-10-13 07:17:48 +00:00
|
|
|
$grade->timemodified = time();
|
|
|
|
if (!insert_record("quiz_grades", $grade)) {
|
2002-10-21 12:39:46 +00:00
|
|
|
notify("Could not insert new best grade");
|
2002-10-13 07:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-06 03:23:16 +00:00
|
|
|
function quiz_calculate_best_grade($quiz, $attempts) {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Calculate the best grade for a quiz given a number of attempts by a particular user.
|
2002-10-06 03:23:16 +00:00
|
|
|
|
|
|
|
switch ($quiz->grademethod) {
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
case ATTEMPTFIRST:
|
2002-10-06 03:23:16 +00:00
|
|
|
foreach ($attempts as $attempt) {
|
2002-10-13 07:17:48 +00:00
|
|
|
return $attempt->sumgrades;
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ATTEMPTLAST:
|
|
|
|
foreach ($attempts as $attempt) {
|
|
|
|
$final = $attempt->sumgrades;
|
|
|
|
}
|
|
|
|
return $final;
|
2002-10-06 03:23:16 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
case GRADEAVERAGE:
|
2002-10-06 03:23:16 +00:00
|
|
|
$sum = 0;
|
|
|
|
$count = 0;
|
|
|
|
foreach ($attempts as $attempt) {
|
2002-10-13 07:17:48 +00:00
|
|
|
$sum += $attempt->sumgrades;
|
2002-10-06 03:23:16 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
return (float)$sum/$count;
|
|
|
|
|
|
|
|
default:
|
2002-10-13 07:17:48 +00:00
|
|
|
case GRADEHIGHEST:
|
|
|
|
$max = 0;
|
2002-10-06 03:23:16 +00:00
|
|
|
foreach ($attempts as $attempt) {
|
2002-10-13 07:17:48 +00:00
|
|
|
if ($attempt->sumgrades > $max) {
|
|
|
|
$max = $attempt->sumgrades;
|
|
|
|
}
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
function quiz_save_attempt($quiz, $questions, $result, $attemptnum) {
|
|
|
|
/// Given a quiz, a list of attempted questions and a total grade
|
|
|
|
/// this function saves EVERYTHING so it can be reconstructed later
|
|
|
|
/// if necessary.
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2002-10-18 16:05:21 +00:00
|
|
|
// First find the attempt in the database (start of attempt)
|
|
|
|
|
|
|
|
if (!$attempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
|
|
|
|
notify("Trying to save an attempt that was not started!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attempt->attempt != $attemptnum) { // Double check.
|
|
|
|
notify("Number of this attempt is different to the unfinished one!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now let's complete this record and save it
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
$attempt->sumgrades = $result->sumgrades;
|
2002-10-18 16:05:21 +00:00
|
|
|
$attempt->timefinish = time();
|
2002-10-13 07:17:48 +00:00
|
|
|
$attempt->timemodified = time();
|
|
|
|
|
2002-10-18 16:05:21 +00:00
|
|
|
if (! update_record("quiz_attempts", $attempt)) {
|
2002-10-15 17:13:11 +00:00
|
|
|
notify("Error while saving attempt");
|
2002-10-13 07:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now let's save all the questions for this attempt
|
|
|
|
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
$response->attempt = $attempt->id;
|
|
|
|
$response->question = $question->id;
|
|
|
|
$response->grade = $result->grades[$question->id];
|
2003-04-09 06:32:51 +00:00
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if (!empty($question->random)) {
|
2003-04-09 06:32:51 +00:00
|
|
|
// First save the response of the random question
|
|
|
|
// the answer is the id of the REAL response
|
|
|
|
$response->answer = $question->random;
|
|
|
|
if (!insert_record("quiz_responses", $response)) {
|
|
|
|
notify("Error while saving response");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$response->question = $question->random;
|
|
|
|
}
|
|
|
|
|
2003-01-24 07:48:54 +00:00
|
|
|
if (!empty($question->answer)) {
|
2002-10-13 07:17:48 +00:00
|
|
|
$response->answer = implode(",",$question->answer);
|
|
|
|
} else {
|
|
|
|
$response->answer = "";
|
|
|
|
}
|
|
|
|
if (!insert_record("quiz_responses", $response)) {
|
2002-10-15 17:13:11 +00:00
|
|
|
notify("Error while saving response");
|
2002-10-13 07:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
return true;
|
2002-10-06 03:23:16 +00:00
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
function quiz_grade_attempt_results($quiz, $questions) {
|
|
|
|
/// Given a list of questions (including answers for each one)
|
|
|
|
/// this function does all the hard work of calculating the
|
|
|
|
/// grades for each question, as well as a total grade for
|
|
|
|
/// for the whole quiz. It returns everything in a structure
|
|
|
|
/// that looks like:
|
|
|
|
/// $result->sumgrades (sum of all grades for all questions)
|
|
|
|
/// $result->percentage (Percentage of grades that were correct)
|
|
|
|
/// $result->grade (final grade result for the whole quiz)
|
|
|
|
/// $result->grades[] (array of grades, indexed by question id)
|
2002-10-16 09:35:04 +00:00
|
|
|
/// $result->response[] (array of response arrays, indexed by question id)
|
2002-10-13 07:17:48 +00:00
|
|
|
/// $result->feedback[] (array of feedback arrays, indexed by question id)
|
2002-10-20 05:35:25 +00:00
|
|
|
/// $result->correct[] (array of feedback arrays, indexed by question id)
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
if (!$questions) {
|
|
|
|
error("No questions!");
|
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
if (!$grades = get_records_menu("quiz_question_grades", "quiz", $quiz->id, "", "question,grade")) {
|
|
|
|
error("No grades defined for these quiz questions!");
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
$result->sumgrades = 0;
|
|
|
|
|
|
|
|
foreach ($questions as $question) {
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
if (!empty($question->random)) { // This question has been randomly chosen
|
|
|
|
$randomquestion = $question; // Save it for later
|
|
|
|
if (!$question = get_record("quiz_questions", "id", $question->random)) {
|
|
|
|
error("Could not find the real question behind this random question!");
|
|
|
|
}
|
|
|
|
$question->answer = $randomquestion->answer;
|
|
|
|
$question->grade = $grades[$randomquestion->id];
|
|
|
|
} else {
|
|
|
|
$question->grade = $grades[$question->id];
|
|
|
|
}
|
|
|
|
|
2002-10-15 10:04:28 +00:00
|
|
|
if (!$answers = quiz_get_answers($question)) {
|
|
|
|
error("No answers defined for question id $question->id!");
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$grade = 0; // default
|
2003-04-09 06:32:51 +00:00
|
|
|
$correct = array();
|
2002-10-16 15:52:29 +00:00
|
|
|
$feedback = array();
|
|
|
|
$response = array();
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2003-01-03 16:01:48 +00:00
|
|
|
switch ($question->qtype) {
|
2002-10-13 07:17:48 +00:00
|
|
|
case SHORTANSWER:
|
|
|
|
if ($question->answer) {
|
2003-01-16 15:19:49 +00:00
|
|
|
$question->answer = trim(stripslashes($question->answer[0]));
|
2002-10-13 07:17:48 +00:00
|
|
|
} else {
|
2002-10-16 15:52:29 +00:00
|
|
|
$question->answer = "";
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
2002-10-16 09:35:04 +00:00
|
|
|
$response[0] = $question->answer;
|
2003-01-03 16:01:48 +00:00
|
|
|
$bestshortanswer = 0;
|
2002-10-13 07:17:48 +00:00
|
|
|
foreach($answers as $answer) { // There might be multiple right answers
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($answer->fraction > $bestshortanswer) {
|
|
|
|
$correct[$answer->id] = $answer->answer;
|
2003-01-03 16:01:48 +00:00
|
|
|
$bestshortanswer = $answer->fraction;
|
2002-10-20 05:35:25 +00:00
|
|
|
}
|
2002-10-15 10:04:28 +00:00
|
|
|
if (!$answer->usecase) { // Don't compare case
|
2002-10-13 07:17:48 +00:00
|
|
|
$answer->answer = strtolower($answer->answer);
|
|
|
|
$question->answer = strtolower($question->answer);
|
|
|
|
}
|
|
|
|
if ($question->answer == $answer->answer) {
|
2002-10-16 09:35:04 +00:00
|
|
|
$feedback[0] = $answer->feedback;
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade = (float)$answer->fraction * $question->grade;
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case TRUEFALSE:
|
|
|
|
if ($question->answer) {
|
|
|
|
$question->answer = $question->answer[0];
|
|
|
|
} else {
|
|
|
|
$question->answer = NULL;
|
|
|
|
}
|
|
|
|
foreach($answers as $answer) { // There should be two answers (true and false)
|
|
|
|
$feedback[$answer->id] = $answer->feedback;
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($answer->fraction > 0) {
|
|
|
|
$correct[$answer->id] = true;
|
|
|
|
}
|
2002-10-13 07:17:48 +00:00
|
|
|
if ($question->answer == $answer->id) {
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade = (float)$answer->fraction * $question->grade;
|
2002-10-16 09:35:04 +00:00
|
|
|
$response[$answer->id] = true;
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case MULTICHOICE:
|
|
|
|
foreach($answers as $answer) { // There will be multiple answers, perhaps more than one is right
|
|
|
|
$feedback[$answer->id] = $answer->feedback;
|
2002-10-20 05:35:25 +00:00
|
|
|
if ($answer->fraction > 0) {
|
|
|
|
$correct[$answer->id] = true;
|
|
|
|
}
|
2003-01-24 07:48:54 +00:00
|
|
|
if (!empty($question->answer)) {
|
2002-10-13 07:17:48 +00:00
|
|
|
foreach ($question->answer as $questionanswer) {
|
|
|
|
if ($questionanswer == $answer->id) {
|
2003-02-24 10:37:56 +00:00
|
|
|
$response[$answer->id] = true;
|
2002-10-13 07:17:48 +00:00
|
|
|
if ($answer->single) {
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade = (float)$answer->fraction * $question->grade;
|
2002-10-13 07:17:48 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade += (float)$answer->fraction * $question->grade;
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2003-02-24 10:37:56 +00:00
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
case MATCH:
|
|
|
|
$matchcount = $totalcount = 0;
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
foreach ($question->answer as $questionanswer) { // Each answer is "subquestionid-answerid"
|
2003-03-30 16:46:50 +00:00
|
|
|
$totalcount++;
|
2003-04-09 06:32:51 +00:00
|
|
|
$qarr = explode('-', $questionanswer); // Extract subquestion/answer.
|
|
|
|
$subquestionid = $qarr[0];
|
|
|
|
$subanswerid = $qarr[1];
|
|
|
|
if (($subquestionid == $subanswerid) or
|
|
|
|
($answers[$subquestionid]->answertext == $answers[$subanswerid]->answertext)) {
|
|
|
|
// Either the ids match exactly, or the answertexts match exactly
|
|
|
|
// (in case two subquestions had the same answer)
|
2003-03-30 16:46:50 +00:00
|
|
|
$matchcount++;
|
2003-04-09 06:32:51 +00:00
|
|
|
$correct[$subquestionid] = true;
|
2003-03-30 16:46:50 +00:00
|
|
|
} else {
|
2003-04-09 06:32:51 +00:00
|
|
|
$correct[$subquestionid] = false;
|
2003-03-30 16:46:50 +00:00
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
$response[$subquestionid] = $subanswerid;
|
2003-03-30 16:46:50 +00:00
|
|
|
}
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade = $question->grade * $matchcount / $totalcount;
|
2003-03-30 16:46:50 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMSAMATCH:
|
2003-02-24 10:37:56 +00:00
|
|
|
$bestanswer = array();
|
|
|
|
foreach ($answers as $answer) { // Loop through them all looking for correct answers
|
|
|
|
if (empty($bestanswer[$answer->question])) {
|
|
|
|
$bestanswer[$answer->question] = 0;
|
|
|
|
$correct[$answer->question] = "";
|
|
|
|
}
|
|
|
|
if ($answer->fraction > $bestanswer[$answer->question]) {
|
|
|
|
$bestanswer[$answer->question] = $answer->fraction;
|
|
|
|
$correct[$answer->question] = $answer->answer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$answerfraction = 1.0 / (float) count($question->answer);
|
|
|
|
foreach ($question->answer as $questionanswer) { // For each random answered question
|
|
|
|
$rqarr = explode('-', $questionanswer); // Extract question/answer.
|
|
|
|
$rquestion = $rqarr[0];
|
|
|
|
$ranswer = $rqarr[1];
|
|
|
|
$response[$rquestion] = $questionanswer;
|
|
|
|
if (isset($answers[$ranswer])) { // If the answer exists in the list
|
|
|
|
$answer = $answers[$ranswer];
|
|
|
|
$feedback[$rquestion] = $answer->feedback;
|
|
|
|
if ($answer->question == $rquestion) { // Check that this answer matches the question
|
2003-04-09 06:32:51 +00:00
|
|
|
$grade += (float)$answer->fraction * $question->grade * $answerfraction;
|
2003-02-24 10:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-20 05:35:25 +00:00
|
|
|
break;
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
}
|
2003-04-09 06:32:51 +00:00
|
|
|
|
|
|
|
if (!empty($randomquestion)) { // This question has been randomly chosen
|
|
|
|
$question = $randomquestion; // Restore the question->id
|
|
|
|
unset($randomquestion);
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
if ($grade < 0.0) { // No negative grades
|
|
|
|
$grade = 0.0;
|
|
|
|
}
|
2002-10-14 15:57:33 +00:00
|
|
|
|
2002-10-26 06:11:43 +00:00
|
|
|
$result->grades[$question->id] = round($grade, 2);
|
2002-10-13 07:17:48 +00:00
|
|
|
$result->sumgrades += $grade;
|
|
|
|
$result->feedback[$question->id] = $feedback;
|
2002-10-16 09:35:04 +00:00
|
|
|
$result->response[$question->id] = $response;
|
2002-10-20 05:35:25 +00:00
|
|
|
$result->correct[$question->id] = $correct;
|
2002-10-13 07:17:48 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 12:54:11 +00:00
|
|
|
$fraction = (float)($result->sumgrades / $quiz->sumgrades);
|
|
|
|
$result->percentage = format_float($fraction * 100.0);
|
|
|
|
$result->grade = format_float($fraction * $quiz->grade);
|
2002-10-26 06:11:43 +00:00
|
|
|
$result->sumgrades = round($result->sumgrades, 2);
|
2002-10-13 07:17:48 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2002-10-22 04:25:58 +00:00
|
|
|
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
function quiz_save_question_options($question) {
|
|
|
|
/// Given some question info and some data about the the answers
|
|
|
|
/// this function parses, organises and saves the question
|
|
|
|
/// It is used by question.php when saving new data from a
|
|
|
|
/// form, and also by import.php when importing questions
|
2003-04-15 14:43:29 +00:00
|
|
|
///
|
|
|
|
/// If this is an update, and old answers already exist, then
|
|
|
|
/// these are overwritten using an update(). To do this, it
|
|
|
|
/// it is assumed that the IDs in quiz_answers are in the same
|
|
|
|
/// sort order as the new answers being saved. This should always
|
|
|
|
/// be true, but it's something to keep in mind if fiddling with
|
|
|
|
/// question.php
|
2003-02-16 07:08:57 +00:00
|
|
|
///
|
|
|
|
/// Returns $result->error or $result->notice
|
2002-10-13 07:17:48 +00:00
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
switch ($question->qtype) {
|
|
|
|
case SHORTANSWER:
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC"); // might be NULL
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
$answers = array();
|
|
|
|
$maxfraction = -1;
|
|
|
|
|
|
|
|
// Insert all the new answers
|
|
|
|
foreach ($question->answer as $key => $dataanswer) {
|
|
|
|
if ($dataanswer != "") {
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
|
|
|
|
$answer = $oldanswer;
|
|
|
|
$answer->answer = $dataanswer;
|
|
|
|
$answer->fraction = $question->fraction[$key];
|
|
|
|
$answer->feedback = $question->feedback[$key];
|
|
|
|
if (!update_record("quiz_answers", $answer)) {
|
|
|
|
$result->error = "Could not update quiz answer! (id=$answer->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else { // This is a completely new answer
|
|
|
|
unset($answer);
|
|
|
|
$answer->answer = $dataanswer;
|
|
|
|
$answer->question = $question->id;
|
|
|
|
$answer->fraction = $question->fraction[$key];
|
|
|
|
$answer->feedback = $question->feedback[$key];
|
|
|
|
if (!$answer->id = insert_record("quiz_answers", $answer)) {
|
|
|
|
$result->error = "Could not insert quiz answer!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
$answers[] = $answer->id;
|
|
|
|
if ($question->fraction[$key] > $maxfraction) {
|
|
|
|
$maxfraction = $question->fraction[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($options = get_record("quiz_shortanswer", "question", $question->id)) {
|
|
|
|
$options->answers = implode(",",$answers);
|
|
|
|
$options->usecase = $question->usecase;
|
|
|
|
if (!update_record("quiz_shortanswer", $options)) {
|
|
|
|
$result->error = "Could not update quiz shortanswer options! (id=$options->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($options);
|
|
|
|
$options->question = $question->id;
|
|
|
|
$options->answers = implode(",",$answers);
|
|
|
|
$options->usecase = $question->usecase;
|
|
|
|
if (!insert_record("quiz_shortanswer", $options)) {
|
|
|
|
$result->error = "Could not insert quiz shortanswer options!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform sanity checks on fractional grades
|
|
|
|
if ($maxfraction != 1) {
|
|
|
|
$maxfraction = $maxfraction * 100;
|
|
|
|
$result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
break;
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
case TRUEFALSE:
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC"); // might be NULL
|
|
|
|
|
|
|
|
if ($true = array_shift($oldanswers)) { // Existing answer, so reuse it
|
|
|
|
$true->fraction = $question->answer;
|
|
|
|
$true->feedback = $question->feedbacktrue;
|
|
|
|
if (!update_record("quiz_answers", $true)) {
|
|
|
|
$result->error = "Could not update quiz answer \"true\")!";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($true);
|
|
|
|
$true->answer = get_string("true", "quiz");
|
|
|
|
$true->question = $question->id;
|
|
|
|
$true->fraction = $question->answer;
|
|
|
|
$true->feedback = $question->feedbacktrue;
|
|
|
|
if (!$true->id = insert_record("quiz_answers", $true)) {
|
|
|
|
$result->error = "Could not insert quiz answer \"true\")!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($false = array_shift($oldanswers)) { // Existing answer, so reuse it
|
|
|
|
$false->fraction = 1 - (int)$question->answer;
|
|
|
|
$false->feedback = $question->feedbackfalse;
|
|
|
|
if (!update_record("quiz_answers", $false)) {
|
|
|
|
$result->error = "Could not insert quiz answer \"false\")!";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($false);
|
|
|
|
$false->answer = get_string("false", "quiz");
|
|
|
|
$false->question = $question->id;
|
|
|
|
$false->fraction = 1 - (int)$question->answer;
|
|
|
|
$false->feedback = $question->feedbackfalse;
|
|
|
|
if (!$false->id = insert_record("quiz_answers", $false)) {
|
|
|
|
$result->error = "Could not insert quiz answer \"false\")!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($options = get_record("quiz_truefalse", "question", $question->id)) {
|
|
|
|
// No need to do anything, since the answer IDs won't have changed
|
|
|
|
// But we'll do it anyway, just for robustness
|
|
|
|
$options->trueanswer = $true->id;
|
|
|
|
$options->falseanswer = $false->id;
|
|
|
|
if (!update_record("quiz_truefalse", $options)) {
|
|
|
|
$result->error = "Could not update quiz truefalse options! (id=$options->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($options);
|
|
|
|
$options->question = $question->id;
|
|
|
|
$options->trueanswer = $true->id;
|
|
|
|
$options->falseanswer = $false->id;
|
|
|
|
if (!insert_record("quiz_truefalse", $options)) {
|
|
|
|
$result->error = "Could not insert quiz truefalse options!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
break;
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
case MULTICHOICE:
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC"); // might be NULL
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
$totalfraction = 0;
|
|
|
|
$maxfraction = -1;
|
|
|
|
|
|
|
|
$answers = array();
|
|
|
|
|
|
|
|
// Insert all the new answers
|
|
|
|
foreach ($question->answer as $key => $dataanswer) {
|
|
|
|
if ($dataanswer != "") {
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($answer = array_shift($oldanswers)) { // Existing answer, so reuse it
|
|
|
|
$answer->answer = $dataanswer;
|
|
|
|
$answer->fraction = $question->fraction[$key];
|
|
|
|
$answer->feedback = $question->feedback[$key];
|
|
|
|
if (!update_record("quiz_answers", $answer)) {
|
|
|
|
$result->error = "Could not update quiz answer! (id=$answer->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($answer);
|
|
|
|
$answer->answer = $dataanswer;
|
|
|
|
$answer->question = $question->id;
|
|
|
|
$answer->fraction = $question->fraction[$key];
|
|
|
|
$answer->feedback = $question->feedback[$key];
|
|
|
|
if (!$answer->id = insert_record("quiz_answers", $answer)) {
|
|
|
|
$result->error = "Could not insert quiz answer! ";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
$answers[] = $answer->id;
|
|
|
|
|
|
|
|
if ($question->fraction[$key] > 0) { // Sanity checks
|
|
|
|
$totalfraction += $question->fraction[$key];
|
|
|
|
}
|
|
|
|
if ($question->fraction[$key] > $maxfraction) {
|
|
|
|
$maxfraction = $question->fraction[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($options = get_record("quiz_multichoice", "question", $question->id)) {
|
|
|
|
$options->answers = implode(",",$answers);
|
|
|
|
$options->single = $question->single;
|
|
|
|
if (!update_record("quiz_multichoice", $options)) {
|
|
|
|
$result->error = "Could not update quiz multichoice options! (id=$options->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($options);
|
|
|
|
$options->question = $question->id;
|
|
|
|
$options->answers = implode(",",$answers);
|
|
|
|
$options->single = $question->single;
|
|
|
|
if (!insert_record("quiz_multichoice", $options)) {
|
|
|
|
$result->error = "Could not insert quiz multichoice options!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform sanity checks on fractional grades
|
|
|
|
if ($options->single) {
|
|
|
|
if ($maxfraction != 1) {
|
|
|
|
$maxfraction = $maxfraction * 100;
|
|
|
|
$result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$totalfraction = round($totalfraction,2);
|
|
|
|
if ($totalfraction != 1) {
|
|
|
|
$totalfraction = $totalfraction * 100;
|
|
|
|
$result->notice = get_string("fractionsaddwrong", "quiz", $totalfraction);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2003-02-24 10:37:56 +00:00
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
case MATCH:
|
2003-04-15 14:43:29 +00:00
|
|
|
|
|
|
|
$oldsubquestions = get_records("quiz_match_sub", "question", $question->id, "id ASC"); // might be NULL
|
2003-03-30 16:46:50 +00:00
|
|
|
|
|
|
|
$subquestions = array();
|
|
|
|
|
|
|
|
// Insert all the new question+answer pairs
|
|
|
|
foreach ($question->subquestions as $key => $questiontext) {
|
|
|
|
$answertext = $question->subanswers[$key];
|
|
|
|
if (!empty($questiontext) and !empty($answertext)) {
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($subquestion = array_shift($oldsubquestions)) { // Existing answer, so reuse it
|
|
|
|
$subquestion->questiontext = $questiontext;
|
|
|
|
$subquestion->answertext = $answertext;
|
|
|
|
if (!update_record("quiz_match_sub", $subquestion)) {
|
|
|
|
$result->error = "Could not insert quiz match subquestion! (id=$subquestion->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($subquestion);
|
|
|
|
$subquestion->question = $question->id;
|
|
|
|
$subquestion->questiontext = $questiontext;
|
|
|
|
$subquestion->answertext = $answertext;
|
|
|
|
if (!$subquestion->id = insert_record("quiz_match_sub", $subquestion)) {
|
|
|
|
$result->error = "Could not insert quiz match subquestion!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-03-30 16:46:50 +00:00
|
|
|
}
|
|
|
|
$subquestions[] = $subquestion->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($subquestions) < 3) {
|
|
|
|
$result->notice = get_string("notenoughsubquestions", "quiz");
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
if ($options = get_record("quiz_match", "question", $question->id)) {
|
|
|
|
$options->subquestions = implode(",",$subquestions);
|
|
|
|
if (!update_record("quiz_match", $options)) {
|
|
|
|
$result->error = "Could not update quiz match options! (id=$options->id)";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($options);
|
|
|
|
$options->question = $question->id;
|
|
|
|
$options->subquestions = implode(",",$subquestions);
|
|
|
|
if (!insert_record("quiz_match", $options)) {
|
|
|
|
$result->error = "Could not insert quiz match options!";
|
|
|
|
return $result;
|
|
|
|
}
|
2003-03-30 16:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2003-04-15 14:43:29 +00:00
|
|
|
|
2003-03-30 16:46:50 +00:00
|
|
|
case RANDOMSAMATCH:
|
2003-02-24 10:37:56 +00:00
|
|
|
$options->question = $question->id;
|
|
|
|
$options->choose = $question->choose;
|
2003-03-30 16:46:50 +00:00
|
|
|
if ($existing = get_record("quiz_randomsamatch", "question", $options->question)) {
|
2003-02-24 10:37:56 +00:00
|
|
|
$options->id = $existing->id;
|
2003-03-30 16:46:50 +00:00
|
|
|
if (!update_record("quiz_randomsamatch", $options)) {
|
|
|
|
$result->error = "Could not update quiz randomsamatch options!";
|
2003-02-24 10:37:56 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
} else {
|
2003-03-30 16:46:50 +00:00
|
|
|
if (!insert_record("quiz_randomsamatch", $options)) {
|
|
|
|
$result->error = "Could not insert quiz randomsamatch options!";
|
2003-02-24 10:37:56 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-04-09 06:32:51 +00:00
|
|
|
case RANDOM:
|
|
|
|
break;
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
default:
|
|
|
|
$result->error = "Unsupported question type ($question->qtype)!";
|
|
|
|
return $result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-24 10:37:56 +00:00
|
|
|
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
?>
|