Cleaned up the whole interface (more consistent now) and lots of labguage stuff
[moodle.git] / mod / survey / lib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Graph size
551b0b98 4$SURVEY_GHEIGHT = 500;
5$SURVEY_GWIDTH = 900;
f9903ed0 6
551b0b98 7$SURVEY_QTYPE = array (
f9903ed0 8 "-3" => "Virtual Actual and Preferred",
9 "-2" => "Virtual Preferred",
10 "-1" => "Virtual Actual",
11 "0" => "Text",
12 "1" => "Actual",
13 "2" => "Preferred",
14 "3" => "Actual and Preferred",
15 );
16
04eba58f 17// FUNCTIONS ////////////////////////////////////////////////////////
18
19function survey_add_instance($survey) {
20// Given an object containing all the necessary data,
21// (defined by the form in mod.html) this function
22// will create a new instance and return the id number
23// of the new instance.
24
25 if (!$template = get_record("survey", "id", $survey->template)) {
26 return 0;
27 }
28
29 $survey->questions = $template->questions;
30 $survey->timecreated = time();
31 $survey->timemodified = $survey->timecreated;
32
33 return insert_record("survey", $survey);
34
35}
36
37
38function survey_update_instance($survey) {
39// Given an object containing all the necessary data,
40// (defined by the form in mod.html) this function
41// will update an existing instance with new data.
42
43 if (!$template = get_record("survey", "id", $survey->template)) {
44 return 0;
45 }
46
47 $survey->id = $survey->instance;
48 $survey->questions = $template->questions;
49 $survey->timemodified = time();
50
51 return update_record("survey", $survey);
52}
53
54function survey_delete_instance($id) {
55// Given an ID of an instance of this module,
56// this function will permanently delete the instance
57// and any data that depends on it.
58
59 if (! $survey = get_record("survey", "id", "$id")) {
60 return false;
61 }
62
63 $result = true;
64
65 if (! delete_records("survey_analysis", "survey", "$survey->id")) {
66 $result = false;
67 }
68
69 if (! delete_records("survey_answers", "survey", "$survey->id")) {
70 $result = false;
71 }
72
73 if (! delete_records("survey", "id", "$survey->id")) {
74 $result = false;
75 }
76
77 return $result;
78}
79
80
f9903ed0 81function survey_already_done($survey, $user) {
82 return record_exists_sql("SELECT * FROM survey_answers WHERE survey='$survey' AND user='$user'");
83}
84
551b0b98 85function survey_get_status($survey) {
f9903ed0 86
87 $timenow = time();
88 if ($survey->locked) {
89 if (($survey->timeopen <= $timenow) && ($timenow <= $survey->timeclose)) {
90 return "released";
91 } else if ($survey->timenow >= $survey->timeclose) {
92 return "finished";
93 } else {
94 return "error";
95 }
96 } else {
97 return "editing";
98 }
99
100}
101
551b0b98 102function survey_get_responses($survey) {
103 return get_records_sql("SELECT a.time as time, count(*) as numanswers, u.*
104 FROM survey_answers AS a, user AS u
105 WHERE a.answer1 <> '0' AND a.answer2 <> '0'
106 AND a.survey = $survey
107 AND a.user = u.id
108 GROUP BY a.user ORDER BY a.time ASC");
109}
f9903ed0 110
362f9c9c 111function survey_count_responses($survey) {
551b0b98 112 if ($responses = survey_get_responses($survey)) {
113 return count($responses);
114 } else {
115 return 0;
116 }
f9903ed0 117}
118
b416a1c3 119
551b0b98 120function survey_print_all_responses($survey, $results) {
b416a1c3 121 global $THEME;
122
123 echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
124 echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
125
126 foreach ($results as $a) {
127
128 echo "<TR>";
129 echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
7a302afc 130 echo "<TD>".userdate($a->time, "%e %B %Y, %I:%M %p")."</TD>";
b416a1c3 131 echo "<TD align=right>$a->numanswers</TD>";
132 echo "</TR>";
133 }
134 echo "</TABLE>";
135}
136
b416a1c3 137
551b0b98 138function survey_get_template_name($templateid) {
f9903ed0 139 global $db;
140
141 if ($templateid) {
142 if ($ss = $db->Execute("SELECT name FROM surveys WHERE id = $templateid")) {
143 return $ss->fields["name"];
144 }
145 } else {
146 return "";
147 }
148}
149
0d22e7cc 150
551b0b98 151function survey_get_analysis($survey, $user) {
152 global $db;
153
154 return get_record_sql("SELECT notes from survey_analysis WHERE survey='$survey' and user='$user'");
155}
156
157function survey_update_analysis($survey, $user, $notes) {
f9903ed0 158 global $db;
159
160 return $db->Execute("UPDATE survey_analysis SET notes='$notes' WHERE survey='$survey' and user='$user'");
161}
162
0d22e7cc 163
551b0b98 164function survey_add_analysis($survey, $user, $notes) {
f9903ed0 165 global $db;
166
167 return $db->Execute("INSERT INTO survey_analysis SET notes='$notes', survey='$survey', user='$user'");
168}
169
0e30c987 170function survey_shorten_name ($name, $numwords) {
171 $words = explode(" ", $name);
172 for ($i=0; $i < $numwords; $i++) {
173 $output .= $words[$i]." ";
174 }
175 return $output;
176}
f9903ed0 177
0d22e7cc 178function survey_user_summary($course, $user, $mod, $survey) {
179 global $CFG;
180}
181
182
183function survey_user_outline($course, $user, $mod, $survey) {
184 if ($answers = get_records_sql("SELECT * FROM survey_answers WHERE survey='$survey->id' AND user='$user->id'")) {
185
186 $lastanswer = array_pop($answers);
187
188 $result->info = "Done";
189 $result->time = $lastanswer->time;
190 return $result;
0d22e7cc 191 }
192 return NULL;
193}
194
195
196function survey_user_complete($course, $user, $mod, $survey) {
3f4deb2b 197 global $CFG;
0d22e7cc 198
199 if (survey_already_done($survey->id, $user->id)) {
200 echo "<IMG SRC=\"$CFG->wwwroot/mod/survey/graph.php?id=$mod->id&sid=$user->id&type=student.png\">";
201 } else {
202 echo "Not done yet";
203 }
204}
f9903ed0 205
0e30c987 206
207function survey_print_multi($question) {
208 GLOBAL $db, $qnum, $checklist, $THEME;
209
210
211 echo "<P>&nbsp</P>\n";
212 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
213
214 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
215
216 $options = explode( ",", $question->options);
217 $numoptions = count($options);
218
219 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
220 if ($question->type == 2) {
221 $P = "P";
222 } else {
223 $P = "";
224 }
225
226 if ($oneanswer) {
227 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
228 } else {
229 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
230 }
231
232 while (list ($key, $val) = each ($options)) {
233 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
234 }
235 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
236
237 $subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
238
239 foreach ($subquestions as $q) {
240 $qnum++;
241 $bgcolor = survey_question_color($qnum);
242
243 echo "<TR BGCOLOR=$bgcolor>";
244 if ($oneanswer) {
245 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
246 echo "<TD VALIGN=top><P>$q->text</P></TD>";
247 for ($i=1;$i<=$numoptions;$i++) {
248 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
249 }
250 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
251 $checklist["q$P$q->id"] = $numoptions;
252
253 } else {
254 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
255 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I prefer that&nbsp;</FONT></P></TD>";
256 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
257 for ($i=1;$i<=$numoptions;$i++) {
258 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
259 }
260 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
261 echo "</TR>";
262
263 echo "<TR BGCOLOR=$bgcolor>";
264 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I found that&nbsp;</P></TD>";
265 for ($i=1;$i<=$numoptions;$i++) {
266 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
267 }
268 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
269 $checklist["qP$q->id"] = $numoptions;
270 $checklist["q$q->id"] = $numoptions;
271 }
272 echo "</TR>\n";
273 }
274 echo "</TABLE>";
275}
276
277
278
279function survey_print_single($question) {
280 GLOBAL $db, $qnum;
281
282 $bgcolor = survey_question_color(0);
283
284 $qnum++;
285
286 echo "<P>&nbsp</P>\n";
287 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
288 echo "<TR BGCOLOR=$bgcolor>";
289 echo "<TD VALIGN=top><B>$qnum</B></TD>";
290 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
291 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
292
293
294 if ($question->type == 0) { // Plain text field
295 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
296
297 } else if ($question->type > 0) { // Choose one of a number
298 echo "<SELECT NAME=$question->id>";
299 echo "<OPTION VALUE=0 SELECTED>Choose...</OPTION>";
300 $options = explode( ",", $question->options);
301 foreach ($options as $key => $val) {
302 $key++;
303 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
304 }
305 echo "</SELECT>";
306
307 } else if ($question->type < 0) { // Choose several of a number
308 $options = explode( ",", $question->options);
309 echo "<P>THIS TYPE OF QUESTION NOT SUPPORTED YET</P>";
310 }
311
312 echo "</FONT></TD></TR></TABLE>";
313
314}
315
316function survey_question_color($qnum) {
317 global $THEME;
318
319 if ($qnum) {
320 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
321 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
322 } else {
323 return $THEME->cellcontent;
324 }
325}
326
f9903ed0 327?>