More moving functions around and removal of hardcoded SQL
[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
66ea15f3 17// STANDARD FUNCTIONS ////////////////////////////////////////////////////////
04eba58f 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
66ea15f3 80function survey_user_outline($course, $user, $mod, $survey) {
81 if ($answers = get_records_sql("SELECT * FROM survey_answers WHERE survey='$survey->id' AND user='$user->id'")) {
82
83 $lastanswer = array_pop($answers);
84
85 $result->info = get_string("done", "survey");
86 $result->time = $lastanswer->time;
87 return $result;
88 }
89 return NULL;
90}
91
92
93function survey_user_complete($course, $user, $mod, $survey) {
94 global $CFG;
95
96 if (survey_already_done($survey->id, $user->id)) {
97 echo "<IMG SRC=\"$CFG->wwwroot/mod/survey/graph.php?id=$mod->id&sid=$user->id&type=student.png\">";
98 } else {
99 print_string("notdone", "survey");
100 }
101}
102
54634efc 103function survey_print_recent_activity(&$logs, $isteacher=false) {
104 global $CFG, $COURSE_TEACHER_COLOR;
105
106 $content = false;
107 $surveys = NULL;
108
109 foreach ($logs as $log) {
110 if ($log->module == "survey" and $log->action == "submit") {
111 $surveys[$log->id] = get_record_sql("SELECT s.name, u.firstname, u.lastname
112 FROM survey s, user u
a30cfe16 113 WHERE s.id = '$log->info' AND u.id = '$log->user'");
7bca7957 114 $surveys[$log->id]->time = $log->time;
115 $surveys[$log->id]->url = $log->url;
54634efc 116 }
117 }
118
119 if ($surveys) {
120 $content = true;
121 print_headline(get_string("newsurveyresponses", "survey").":");
122 foreach ($surveys as $survey) {
62c13a2f 123 $date = userdate($survey->time, "%d %b, %H:%M");
54634efc 124 echo "<P><FONT SIZE=1>$date - $survey->firstname $survey->lastname<BR>";
125 echo "\"<A HREF=\"$CFG->wwwroot/mod/survey/$survey->url\">";
126 echo "$survey->name";
127 echo "</A>\"</FONT></P>";
128 }
129 }
130
131 return $content;
132}
04eba58f 133
66ea15f3 134
135// MODULE FUNCTIONS ////////////////////////////////////////////////////////
136
f9903ed0 137function survey_already_done($survey, $user) {
138 return record_exists_sql("SELECT * FROM survey_answers WHERE survey='$survey' AND user='$user'");
139}
140
f9903ed0 141
551b0b98 142function survey_get_responses($survey) {
143 return get_records_sql("SELECT a.time as time, count(*) as numanswers, u.*
144 FROM survey_answers AS a, user AS u
145 WHERE a.answer1 <> '0' AND a.answer2 <> '0'
146 AND a.survey = $survey
147 AND a.user = u.id
148 GROUP BY a.user ORDER BY a.time ASC");
149}
f9903ed0 150
362f9c9c 151function survey_count_responses($survey) {
551b0b98 152 if ($responses = survey_get_responses($survey)) {
153 return count($responses);
154 } else {
155 return 0;
156 }
f9903ed0 157}
158
b416a1c3 159
551b0b98 160function survey_print_all_responses($survey, $results) {
b416a1c3 161 global $THEME;
162
163 echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
164 echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
165
166 foreach ($results as $a) {
167
168 echo "<TR>";
169 echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
62c13a2f 170 echo "<TD>".userdate($a->time, "%d %B %Y, %I:%M %p")."</TD>";
b416a1c3 171 echo "<TD align=right>$a->numanswers</TD>";
172 echo "</TR>";
173 }
174 echo "</TABLE>";
175}
176
b416a1c3 177
551b0b98 178function survey_get_template_name($templateid) {
f9903ed0 179 global $db;
180
181 if ($templateid) {
182 if ($ss = $db->Execute("SELECT name FROM surveys WHERE id = $templateid")) {
183 return $ss->fields["name"];
184 }
185 } else {
186 return "";
187 }
188}
189
0d22e7cc 190
551b0b98 191function survey_get_analysis($survey, $user) {
192 global $db;
193
194 return get_record_sql("SELECT notes from survey_analysis WHERE survey='$survey' and user='$user'");
195}
196
197function survey_update_analysis($survey, $user, $notes) {
f9903ed0 198 global $db;
199
200 return $db->Execute("UPDATE survey_analysis SET notes='$notes' WHERE survey='$survey' and user='$user'");
201}
202
0d22e7cc 203
551b0b98 204function survey_add_analysis($survey, $user, $notes) {
f9903ed0 205 global $db;
206
207 return $db->Execute("INSERT INTO survey_analysis SET notes='$notes', survey='$survey', user='$user'");
208}
209
0e30c987 210function survey_shorten_name ($name, $numwords) {
211 $words = explode(" ", $name);
212 for ($i=0; $i < $numwords; $i++) {
213 $output .= $words[$i]." ";
214 }
215 return $output;
216}
f9903ed0 217
218
0e30c987 219
220function survey_print_multi($question) {
221 GLOBAL $db, $qnum, $checklist, $THEME;
222
223
98899fc0 224 $stripreferthat = get_string("ipreferthat", "survey");
225 $strifoundthat = get_string("ifoundthat", "survey");
0e30c987 226 echo "<P>&nbsp</P>\n";
227 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
228
229 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
230
231 $options = explode( ",", $question->options);
232 $numoptions = count($options);
233
234 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
235 if ($question->type == 2) {
236 $P = "P";
237 } else {
238 $P = "";
239 }
240
241 if ($oneanswer) {
242 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
243 } else {
244 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
245 }
246
247 while (list ($key, $val) = each ($options)) {
248 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
249 }
250 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
251
252 $subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
253
254 foreach ($subquestions as $q) {
255 $qnum++;
256 $bgcolor = survey_question_color($qnum);
257
f762c448 258 if ($q->text) {
259 $q->text = get_string($q->text, "survey");
260 }
261
0e30c987 262 echo "<TR BGCOLOR=$bgcolor>";
263 if ($oneanswer) {
264 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
265 echo "<TD VALIGN=top><P>$q->text</P></TD>";
266 for ($i=1;$i<=$numoptions;$i++) {
267 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
268 }
269 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
270 $checklist["q$P$q->id"] = $numoptions;
271
272 } else {
273 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
98899fc0 274 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat&nbsp;</FONT></P></TD>";
0e30c987 275 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
276 for ($i=1;$i<=$numoptions;$i++) {
277 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
278 }
279 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
280 echo "</TR>";
281
282 echo "<TR BGCOLOR=$bgcolor>";
98899fc0 283 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat&nbsp;</P></TD>";
0e30c987 284 for ($i=1;$i<=$numoptions;$i++) {
285 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
286 }
287 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
288 $checklist["qP$q->id"] = $numoptions;
289 $checklist["q$q->id"] = $numoptions;
290 }
291 echo "</TR>\n";
292 }
293 echo "</TABLE>";
294}
295
296
297
298function survey_print_single($question) {
299 GLOBAL $db, $qnum;
300
301 $bgcolor = survey_question_color(0);
302
303 $qnum++;
304
305 echo "<P>&nbsp</P>\n";
306 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
307 echo "<TR BGCOLOR=$bgcolor>";
308 echo "<TD VALIGN=top><B>$qnum</B></TD>";
309 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
310 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
311
312
313 if ($question->type == 0) { // Plain text field
314 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
315
316 } else if ($question->type > 0) { // Choose one of a number
98899fc0 317 $strchoose = get_string("choose");
0e30c987 318 echo "<SELECT NAME=$question->id>";
98899fc0 319 echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
0e30c987 320 $options = explode( ",", $question->options);
321 foreach ($options as $key => $val) {
322 $key++;
323 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
324 }
325 echo "</SELECT>";
326
327 } else if ($question->type < 0) { // Choose several of a number
328 $options = explode( ",", $question->options);
f762c448 329 notify("This question type not supported yet");
0e30c987 330 }
331
332 echo "</FONT></TD></TR></TABLE>";
333
334}
335
336function survey_question_color($qnum) {
337 global $THEME;
338
339 if ($qnum) {
340 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
341 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
342 } else {
343 return $THEME->cellcontent;
344 }
345}
346
f9903ed0 347?>