A start on the page that actually prints the questions and stores the answers
[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
3a506ca2 103function quiz_print_question($number, $questionid) {
104 echo "<P><B>$number</B></P>";
105 echo "<UL>";
106 echo "<P>XXXXXX</P>";
107 echo "</UL>";
108 echo "<HR>";
109}
110
111
112function quiz_get_user_attempts($quizid, $userid) {
113 return get_records_sql("SELECT * FROM quiz_attempts WHERE quiz = '$quizid' and user = '$userid' ORDER by attempt ASC");
114}
115
116function quiz_get_grade($quizid, $userid) {
117 if (!$grade = get_record_sql("SELECT * FROM quiz_grades WHERE quiz = '$quizid' and user = '$userid'")) {
118 return 0;
119 }
120
121 return $grade->grade;
122}
123
124function quiz_calculate_best_grade($quiz, $attempts) {
125// Calculate the best grade for a quiz given a number of attempts by a particular user.
126
127 switch ($quiz->grademethod) {
128 case "1": // Use highest score
129 $max = 0;
130 foreach ($attempts as $attempt) {
131 if ($attempt->grade > $max) {
132 $max = $attempt->grade;
133 }
134 }
135 return $max;
136
137 case "2": // Use average score
138 $sum = 0;
139 $count = 0;
140 foreach ($attempts as $attempt) {
141 $sum += $attempt->grade;
142 $count++;
143 }
144 return (float)$sum/$count;
145
146 case "3": // Use first attempt
147 foreach ($attempts as $attempt) {
148 return $attempt->attempt;
149 }
150 break;
151
152 default:
153 case "4": // Use last attempt
154 foreach ($attempts as $attempt) {
155 $final = $attempt->attempt;
156 }
157 return $final;
158 }
159}
730fd187 160
161?>