Tweaks to use new javascript-printing function etc
[moodle.git] / mod / quiz / lib.php
CommitLineData
730fd187 1<?PHP // $Id$
2
3// Library of function for module quiz
4
5$QUIZ_GRADE_METHOD = array ( "1" => get_string("gradehighest", "quiz"),
6 "2" => get_string("gradeaverage", "quiz"),
7 "3" => get_string("attemptfirst", "quiz"),
8 "4" => get_string("attemptlast", "quiz")
9 );
10
11
12function quiz_add_instance($quiz) {
13// Given an object containing all the necessary data,
14// (defined by the form in mod.html) this function
15// will create a new instance and return the id number
16// of the new instance.
17
18 $quiz->timemodified = time();
19
20 # May have to add extra stuff in here #
21
22 return insert_record("quiz", $quiz);
23}
24
25
26function quiz_update_instance($quiz) {
27// Given an object containing all the necessary data,
28// (defined by the form in mod.html) this function
29// will update an existing instance with new data.
30
31 $quiz->timemodified = time();
32 $quiz->id = $quiz->instance;
33
34 # May have to add extra stuff in here #
35
36 return update_record("quiz", $quiz);
37}
38
39
40function quiz_delete_instance($id) {
41// Given an ID of an instance of this module,
42// this function will permanently delete the instance
43// and any data that depends on it.
44
45 if (! $quiz = get_record("quiz", "id", "$id")) {
46 return false;
47 }
48
49 $result = true;
50
51 # Delete any dependent records here #
52
53 if (! delete_records("quiz", "id", "$quiz->id")) {
54 $result = false;
55 }
56
57 return $result;
58}
59
60function quiz_user_outline($course, $user, $mod, $quiz) {
61// Return a small object with summary information about what a
62// user has done with a given particular instance of this module
63// Used for user activity reports.
64// $return->time = the time they did it
65// $return->info = a short text description
66
67 return $return;
68}
69
70function quiz_user_complete($course, $user, $mod, $quiz) {
71// Print a detailed representation of what a user has done with
72// a given particular instance of this module, for user activity reports.
73
74 return true;
75}
76
77function quiz_print_recent_activity(&$logs, $isteacher=false) {
78// Given a list of logs, assumed to be those since the last login
79// this function prints a short list of changes related to this module
80// If isteacher is true then perhaps additional information is printed.
81// This function is called from course/lib.php: print_recent_activity()
82
83 global $CFG, $COURSE_TEACHER_COLOR;
84
85 return $content; // True if anything was printed, otherwise false
86}
87
88function quiz_cron () {
89// Function to be run periodically according to the moodle cron
90// This function searches for things that need to be done, such
91// as sending out mail, toggling flags etc ...
92
93 global $CFG;
94
95 return true;
96}
97
98
99//////////////////////////////////////////////////////////////////////////////////////
100// Any other quiz functions go here. Each of them must have a name that
101// starts with quiz_
102
14d8c0b4 103function quiz_print_question($number, $questionid, $grade, $courseid) {
104
105 if (!$question = get_record("quiz_questions", "id", $questionid)) {
106 notify("Error: Question not found!");
107 }
108
109 $stranswer = get_string("answer", "quiz");
110 $strmarks = get_string("marks", "quiz");
111
112 echo "<TABLE WIDTH=100% CELLSPACING=10><TR><TD NOWRAP WIDTH=100 VALIGN=top>";
113 echo "<P ALIGN=CENTER><B>$number</B><BR><FONT SIZE=1>$grade $strmarks</FONT></P>";
114 print_spacer(1,100);
115 echo "</TD><TD VALIGN=TOP>";
116
117 switch ($question->type) {
118 case 1: // shortanswer
119 if (!$options = get_record("quiz_shortanswer", "question", $question->id)) {
120 notify("Error: Missing question options!");
121 }
122 if (!$answer = get_record("quiz_answers", "id", $options->answer)) {
123 notify("Error: Missing question answers!");
124 }
125 echo "<P>$question->question</P>";
126 if ($question->image) {
127 print_file_picture($question->image, $courseid, 200);
128 }
129 echo "<P ALIGN=RIGHT>$stranswer: <INPUT TYPE=TEXT NAME=q$question->id SIZE=20></P>";
130 break;
131
132 case 2: // true-false
133 if (!$options = get_record("quiz_truefalse", "question", $question->id)) {
134 notify("Error: Missing question options!");
135 }
136 if (!$true = get_record("quiz_answers", "id", $options->true)) {
137 notify("Error: Missing question answers!");
138 }
139 if (!$false = get_record("quiz_answers", "id", $options->false)) {
140 notify("Error: Missing question answers!");
141 }
142 if (!$true->answer) {
143 $true->answer = get_string("true", "quiz");
144 }
145 if (!$false->answer) {
146 $false->answer = get_string("false", "quiz");
147 }
148 echo "<P>$question->question</P>";
149 if ($question->image) {
150 print_file_picture($question->image, $courseid, 200);
151 }
152 echo "<P ALIGN=RIGHT>$stranswer:&nbsp;&nbsp;";
153 echo "<INPUT TYPE=RADIO NAME=\"q$question->id\" VALUE=\"$true->id\">$true->answer";
154 echo "&nbsp;&nbsp;&nbsp;";
155 echo "<INPUT TYPE=RADIO NAME=\"q$question->id\" VALUE=\"$false->id\">$false->answer</P>";
156 break;
157
158 case 3: // multiple-choice
159 if (!$options = get_record("quiz_multichoice", "question", $question->id)) {
160 notify("Error: Missing question options!");
161 }
162 if (!$answers = get_records_sql("SELECT * from quiz_answers WHERE id in ($options->answers)")) {
163 notify("Error: Missing question answers!");
164 }
165 echo "<P>$question->question</P>";
166 if ($question->image) {
167 print_file_picture($question->image, $courseid, 200);
168 }
169 echo "<TABLE ALIGN=right>";
170 echo "<TR><TD valign=top>$stranswer:&nbsp;&nbsp;</TD><TD>";
171 echo "<TABLE ALIGN=right>";
172 $answerids = explode(",", $options->answers);
173 foreach ($answerids as $key => $answerid) {
174 $answer = $answers[$answerid];
175 $qnum = $key + 1;
176 echo "<TR><TD valign=top>";
177 if (!$options->single) {
178 echo "<INPUT TYPE=RADIO NAME=q$question->id VALUE=\"$answer->id\">";
179 } else {
180 echo "<INPUT TYPE=CHECKBOX NAME=q$question->id VALUE=\"$answer->id\">";
181 }
182 echo "</TD>";
183 echo "<TD valign=top>$qnum. $answer->answer</TD>";
184 echo "</TR>";
185 }
186 echo "</TABLE>";
187 echo "</TABLE>";
188 break;
189
190 default:
191 notify("Error: Unknown question type!");
192 }
193
194 echo "</TD></TR></TABLE>";
3a506ca2 195}
196
197
198function quiz_get_user_attempts($quizid, $userid) {
199 return get_records_sql("SELECT * FROM quiz_attempts WHERE quiz = '$quizid' and user = '$userid' ORDER by attempt ASC");
200}
201
202function quiz_get_grade($quizid, $userid) {
203 if (!$grade = get_record_sql("SELECT * FROM quiz_grades WHERE quiz = '$quizid' and user = '$userid'")) {
204 return 0;
205 }
206
207 return $grade->grade;
208}
209
210function quiz_calculate_best_grade($quiz, $attempts) {
211// Calculate the best grade for a quiz given a number of attempts by a particular user.
212
213 switch ($quiz->grademethod) {
214 case "1": // Use highest score
215 $max = 0;
216 foreach ($attempts as $attempt) {
217 if ($attempt->grade > $max) {
218 $max = $attempt->grade;
219 }
220 }
221 return $max;
222
223 case "2": // Use average score
224 $sum = 0;
225 $count = 0;
226 foreach ($attempts as $attempt) {
227 $sum += $attempt->grade;
228 $count++;
229 }
230 return (float)$sum/$count;
231
232 case "3": // Use first attempt
233 foreach ($attempts as $attempt) {
234 return $attempt->attempt;
235 }
236 break;
237
238 default:
239 case "4": // Use last attempt
240 foreach ($attempts as $attempt) {
241 $final = $attempt->attempt;
242 }
243 return $final;
244 }
245}
730fd187 246
247?>