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