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 | |
b6b3deb4 |
17 | |
18 | define("SURVEY_COLLES_ACTUAL", "1"); |
19 | define("SURVEY_COLLES_PREFERRED", "2"); |
20 | define("SURVEY_COLLES_PREFERRED_ACTUAL", "3"); |
21 | define("SURVEY_ATTLS", "4"); |
22 | define("SURVEY_CIQ", "5"); |
23 | |
24 | |
66ea15f3 |
25 | // STANDARD FUNCTIONS //////////////////////////////////////////////////////// |
04eba58f |
26 | |
27 | function survey_add_instance($survey) { |
28 | // Given an object containing all the necessary data, |
29 | // (defined by the form in mod.html) this function |
30 | // will create a new instance and return the id number |
31 | // of the new instance. |
32 | |
33 | if (!$template = get_record("survey", "id", $survey->template)) { |
34 | return 0; |
35 | } |
36 | |
37 | $survey->questions = $template->questions; |
38 | $survey->timecreated = time(); |
39 | $survey->timemodified = $survey->timecreated; |
40 | |
41 | return insert_record("survey", $survey); |
42 | |
43 | } |
44 | |
45 | |
46 | function survey_update_instance($survey) { |
47 | // Given an object containing all the necessary data, |
48 | // (defined by the form in mod.html) this function |
49 | // will update an existing instance with new data. |
50 | |
51 | if (!$template = get_record("survey", "id", $survey->template)) { |
52 | return 0; |
53 | } |
54 | |
55 | $survey->id = $survey->instance; |
56 | $survey->questions = $template->questions; |
57 | $survey->timemodified = time(); |
58 | |
59 | return update_record("survey", $survey); |
60 | } |
61 | |
62 | function survey_delete_instance($id) { |
63 | // Given an ID of an instance of this module, |
64 | // this function will permanently delete the instance |
65 | // and any data that depends on it. |
66 | |
67 | if (! $survey = get_record("survey", "id", "$id")) { |
68 | return false; |
69 | } |
70 | |
71 | $result = true; |
72 | |
73 | if (! delete_records("survey_analysis", "survey", "$survey->id")) { |
74 | $result = false; |
75 | } |
76 | |
77 | if (! delete_records("survey_answers", "survey", "$survey->id")) { |
78 | $result = false; |
79 | } |
80 | |
81 | if (! delete_records("survey", "id", "$survey->id")) { |
82 | $result = false; |
83 | } |
84 | |
85 | return $result; |
86 | } |
87 | |
66ea15f3 |
88 | function survey_user_outline($course, $user, $mod, $survey) { |
ebc3bd2b |
89 | if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$user->id'")) { |
66ea15f3 |
90 | |
91 | $lastanswer = array_pop($answers); |
92 | |
93 | $result->info = get_string("done", "survey"); |
94 | $result->time = $lastanswer->time; |
95 | return $result; |
96 | } |
97 | return NULL; |
98 | } |
99 | |
100 | |
101 | function survey_user_complete($course, $user, $mod, $survey) { |
102 | global $CFG; |
103 | |
104 | if (survey_already_done($survey->id, $user->id)) { |
b6b3deb4 |
105 | if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents |
106 | $table = NULL; |
107 | $table->align = array("left", "left"); |
108 | |
109 | $questions = get_records_list("survey_questions", "id", $survey->questions); |
110 | $questionorder = explode(",", $survey->questions); |
111 | |
112 | foreach ($questionorder as $key=>$val) { |
113 | $question = $questions[$val]; |
114 | $questiontext = get_string($question->shorttext, "survey"); |
115 | |
116 | if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) { |
117 | $answertext = "$answer->answer1"; |
118 | } else { |
119 | $answertext = "No answer"; |
120 | } |
121 | $table->data[] = array("<b>$questiontext</b>", $answertext); |
122 | } |
123 | print_table($table); |
124 | |
125 | } else { |
126 | |
127 | survey_print_graph("id=$mod->id&sid=$user->id&type=student.png"); |
128 | } |
129 | |
66ea15f3 |
130 | } else { |
131 | print_string("notdone", "survey"); |
132 | } |
133 | } |
134 | |
1b5910c4 |
135 | function survey_print_recent_activity($course, $isteacher, $timestart) { |
3d891989 |
136 | global $CFG; |
54634efc |
137 | |
138 | $content = false; |
139 | $surveys = NULL; |
140 | |
1b5910c4 |
141 | if (!$logs = get_records_select("log", "time > '$timestart' AND ". |
142 | "course = '$course->id' AND ". |
143 | "module = 'survey' AND ". |
144 | "action = 'submit' ", "time ASC")) { |
145 | return false; |
146 | } |
147 | |
148 | foreach ($logs as $log) { |
149 | //Create a temp valid module structure (course,id) |
150 | $tempmod->course = $log->course; |
151 | $tempmod->id = $log->info; |
152 | //Obtain the visible property from the instance |
153 | $modvisible = instance_is_visible($log->module,$tempmod); |
154 | |
155 | //Only if the mod is visible |
156 | if ($modvisible) { |
157 | $surveys[$log->id] = survey_log_info($log); |
158 | $surveys[$log->id]->time = $log->time; |
159 | $surveys[$log->id]->url = $log->url; |
54634efc |
160 | } |
161 | } |
162 | |
163 | if ($surveys) { |
164 | $content = true; |
dcde9f02 |
165 | $strftimerecent = get_string("strftimerecent"); |
54634efc |
166 | print_headline(get_string("newsurveyresponses", "survey").":"); |
167 | foreach ($surveys as $survey) { |
dcde9f02 |
168 | $date = userdate($survey->time, $strftimerecent); |
d7143408 |
169 | echo "<p><font size=1>$date - ".fullname($survey)."<br />"; |
1b5910c4 |
170 | echo "\"<a href=\"$CFG->wwwroot/mod/survey/$survey->url\">"; |
54634efc |
171 | echo "$survey->name"; |
1b5910c4 |
172 | echo "</a>\"</font></p>"; |
54634efc |
173 | } |
174 | } |
175 | |
176 | return $content; |
177 | } |
04eba58f |
178 | |
05855091 |
179 | function survey_get_participants($surveyid) { |
180 | //Returns the users with data in one survey |
181 | //(users with records in survey_analysis and survey_answers, students) |
182 | |
183 | global $CFG; |
184 | |
185 | //Get students from survey_analysis |
186 | $st_analysis = get_records_sql("SELECT DISTINCT u.* |
187 | FROM {$CFG->prefix}user u, |
188 | {$CFG->prefix}survey_analysis a |
189 | WHERE a.survey = '$surveyid' and |
190 | u.id = a.userid"); |
191 | //Get students from survey_answers |
192 | $st_answers = get_records_sql("SELECT DISTINCT u.* |
193 | FROM {$CFG->prefix}user u, |
194 | {$CFG->prefix}survey_answers a |
195 | WHERE a.survey = '$surveyid' and |
196 | u.id = a.userid"); |
197 | |
198 | //Add st_answers to st_analysis |
199 | if ($st_answers) { |
200 | foreach ($st_answers as $st_answer) { |
201 | $st_analysis[$st_answer->id] = $st_answer; |
202 | } |
203 | } |
204 | //Return st_analysis array (it contains an array of unique users) |
205 | return ($st_analysis); |
206 | } |
66ea15f3 |
207 | |
e323955d |
208 | // SQL FUNCTIONS //////////////////////////////////////////////////////// |
66ea15f3 |
209 | |
f9903ed0 |
210 | |
e323955d |
211 | function survey_log_info($log) { |
212 | global $CFG; |
7c8c335f |
213 | return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture |
e323955d |
214 | FROM {$CFG->prefix}survey s, |
215 | {$CFG->prefix}user u |
216 | WHERE s.id = '$log->info' |
ebc3bd2b |
217 | AND u.id = '$log->userid'"); |
e323955d |
218 | } |
f9903ed0 |
219 | |
a9ccbf60 |
220 | function survey_get_responses($surveyid, $groupid) { |
e323955d |
221 | global $CFG; |
a9ccbf60 |
222 | |
223 | if ($groupid) { |
840c1781 |
224 | $groupsdb = ", {$CFG->prefix}groups_members AS gm"; |
a9ccbf60 |
225 | $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; |
226 | } else { |
840c1781 |
227 | $groupsdb = ""; |
a9ccbf60 |
228 | $groupsql = ""; |
229 | } |
230 | |
734d2c98 |
231 | return get_records_sql("SELECT MAX(a.time) as time, |
7c8c335f |
232 | u.id, u.firstname, u.lastname, u.picture |
e323955d |
233 | FROM {$CFG->prefix}survey_answers AS a, |
840c1781 |
234 | {$CFG->prefix}user AS u $groupsdb |
a9ccbf60 |
235 | WHERE a.survey = $surveyid |
236 | AND a.userid = u.id $groupsql |
734d2c98 |
237 | GROUP BY u.id, u.firstname, u.lastname |
238 | ORDER BY time ASC"); |
e323955d |
239 | } |
240 | |
241 | function survey_get_analysis($survey, $user) { |
ebc3bd2b |
242 | global $CFG; |
e323955d |
243 | |
244 | return get_record_sql("SELECT notes |
245 | FROM {$CFG->prefix}survey_analysis |
246 | WHERE survey='$survey' |
ebc3bd2b |
247 | AND userid='$user'"); |
e323955d |
248 | } |
249 | |
250 | function survey_update_analysis($survey, $user, $notes) { |
ebc3bd2b |
251 | global $CFG; |
e323955d |
252 | |
ebc3bd2b |
253 | return execute_sql("UPDATE {$CFG->prefix}survey_analysis |
e323955d |
254 | SET notes='$notes' |
255 | WHERE survey='$survey' |
ebc3bd2b |
256 | AND userid='$user'"); |
e323955d |
257 | } |
258 | |
259 | |
a9ccbf60 |
260 | function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") { |
e323955d |
261 | global $CFG; |
262 | |
a9ccbf60 |
263 | if ($groupid) { |
264 | $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; |
265 | } else { |
266 | $groupsql = ""; |
267 | } |
268 | |
e323955d |
269 | return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture |
ebc3bd2b |
270 | FROM {$CFG->prefix}survey_answers sa, |
a9ccbf60 |
271 | {$CFG->prefix}user u, |
272 | {$CFG->prefix}groups_members gm |
e323955d |
273 | WHERE sa.survey = '$surveyid' |
274 | AND sa.question = $questionid |
a9ccbf60 |
275 | AND u.id = sa.userid $groupsql |
7c8c335f |
276 | ORDER BY $sort"); |
277 | } |
278 | |
279 | function survey_get_user_answer($surveyid, $questionid, $userid) { |
280 | global $CFG; |
281 | |
282 | return get_record_sql("SELECT sa.* |
283 | FROM {$CFG->prefix}survey_answers sa |
284 | WHERE sa.survey = '$surveyid' |
285 | AND sa.question = '$questionid' |
286 | AND sa.userid = '$userid'"); |
e323955d |
287 | } |
288 | |
289 | // MODULE FUNCTIONS //////////////////////////////////////////////////////// |
290 | |
ebc3bd2b |
291 | function survey_add_analysis($survey, $user, $notes) { |
292 | global $CFG; |
293 | |
294 | $record->survey = $survey; |
295 | $record->userid = $user; |
296 | $record->notes = $notes; |
297 | |
298 | return insert_record("survey_analysis", $record, false); |
299 | } |
300 | |
e323955d |
301 | function survey_already_done($survey, $user) { |
ebc3bd2b |
302 | return record_exists("survey_answers", "survey", $survey, "userid", $user); |
551b0b98 |
303 | } |
f9903ed0 |
304 | |
a9ccbf60 |
305 | function survey_count_responses($surveyid, $groupid) { |
306 | if ($responses = survey_get_responses($surveyid, $groupid)) { |
551b0b98 |
307 | return count($responses); |
308 | } else { |
309 | return 0; |
310 | } |
f9903ed0 |
311 | } |
312 | |
b416a1c3 |
313 | |
7c8c335f |
314 | function survey_print_all_responses($cmid, $results, $courseid) { |
b416a1c3 |
315 | global $THEME; |
316 | |
a30f997e |
317 | $table->head = array ("", get_string("name"), get_string("time")); |
318 | $table->align = array ("", "left", "left"); |
319 | $table->size = array (35, "", "" ); |
b416a1c3 |
320 | |
321 | foreach ($results as $a) { |
7c8c335f |
322 | $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false), |
323 | "<a href=\"report.php?action=student&student=$a->id&id=$cmid\">".fullname($a)."</a>", |
a30f997e |
324 | userdate($a->time)); |
b416a1c3 |
325 | } |
7c8c335f |
326 | |
327 | print_table($table); |
b416a1c3 |
328 | } |
329 | |
b416a1c3 |
330 | |
551b0b98 |
331 | function survey_get_template_name($templateid) { |
f9903ed0 |
332 | global $db; |
333 | |
334 | if ($templateid) { |
e323955d |
335 | if ($ss = get_record("surveys", "id", $templateid)) { |
336 | return $ss->name; |
f9903ed0 |
337 | } |
338 | } else { |
339 | return ""; |
340 | } |
341 | } |
342 | |
0d22e7cc |
343 | |
f9903ed0 |
344 | |
0e30c987 |
345 | function survey_shorten_name ($name, $numwords) { |
346 | $words = explode(" ", $name); |
347 | for ($i=0; $i < $numwords; $i++) { |
348 | $output .= $words[$i]." "; |
349 | } |
350 | return $output; |
351 | } |
f9903ed0 |
352 | |
353 | |
0e30c987 |
354 | |
355 | function survey_print_multi($question) { |
356 | GLOBAL $db, $qnum, $checklist, $THEME; |
357 | |
358 | |
98899fc0 |
359 | $stripreferthat = get_string("ipreferthat", "survey"); |
360 | $strifoundthat = get_string("ifoundthat", "survey"); |
0e30c987 |
361 | echo "<P> </P>\n"; |
362 | echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>"; |
363 | |
364 | echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>"; |
365 | |
366 | $options = explode( ",", $question->options); |
367 | $numoptions = count($options); |
368 | |
369 | $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false; |
370 | if ($question->type == 2) { |
371 | $P = "P"; |
372 | } else { |
373 | $P = ""; |
374 | } |
375 | |
376 | if ($oneanswer) { |
377 | echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>"; |
378 | } else { |
379 | echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>"; |
380 | } |
381 | |
382 | while (list ($key, $val) = each ($options)) { |
383 | echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n"; |
384 | } |
385 | echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\"> </TD></TR>\n"; |
386 | |
e323955d |
387 | $subquestions = get_records_list("survey_questions", "id", $question->multi); |
0e30c987 |
388 | |
389 | foreach ($subquestions as $q) { |
390 | $qnum++; |
391 | $bgcolor = survey_question_color($qnum); |
392 | |
f762c448 |
393 | if ($q->text) { |
394 | $q->text = get_string($q->text, "survey"); |
395 | } |
396 | |
0e30c987 |
397 | echo "<TR BGCOLOR=$bgcolor>"; |
398 | if ($oneanswer) { |
399 | echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>"; |
400 | echo "<TD VALIGN=top><P>$q->text</P></TD>"; |
401 | for ($i=1;$i<=$numoptions;$i++) { |
a9ef4a63 |
402 | echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i /></TD>"; |
0e30c987 |
403 | } |
a9ef4a63 |
404 | echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked=\"checked\" /></TD>"; |
0e30c987 |
405 | $checklist["q$P$q->id"] = $numoptions; |
406 | |
407 | } else { |
408 | echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>"; |
98899fc0 |
409 | echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat </FONT></P></TD>"; |
0e30c987 |
410 | echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>"; |
411 | for ($i=1;$i<=$numoptions;$i++) { |
a9ef4a63 |
412 | echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i /></TD>"; |
0e30c987 |
413 | } |
a9ef4a63 |
414 | echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked=\"checked\" /></TD>"; |
0e30c987 |
415 | echo "</TR>"; |
416 | |
417 | echo "<TR BGCOLOR=$bgcolor>"; |
98899fc0 |
418 | echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat </P></TD>"; |
0e30c987 |
419 | for ($i=1;$i<=$numoptions;$i++) { |
a9ef4a63 |
420 | echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i /></TD>"; |
0e30c987 |
421 | } |
a9ef4a63 |
422 | echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked=\"checked\" /></TD>"; |
0e30c987 |
423 | $checklist["qP$q->id"] = $numoptions; |
424 | $checklist["q$q->id"] = $numoptions; |
425 | } |
426 | echo "</TR>\n"; |
427 | } |
428 | echo "</TABLE>"; |
429 | } |
430 | |
431 | |
432 | |
433 | function survey_print_single($question) { |
434 | GLOBAL $db, $qnum; |
435 | |
436 | $bgcolor = survey_question_color(0); |
437 | |
438 | $qnum++; |
439 | |
440 | echo "<P> </P>\n"; |
441 | echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n"; |
442 | echo "<TR BGCOLOR=$bgcolor>"; |
443 | echo "<TD VALIGN=top><B>$qnum</B></TD>"; |
444 | echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n"; |
445 | echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n"; |
446 | |
447 | |
448 | if ($question->type == 0) { // Plain text field |
449 | echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>"; |
450 | |
451 | } else if ($question->type > 0) { // Choose one of a number |
98899fc0 |
452 | $strchoose = get_string("choose"); |
0e30c987 |
453 | echo "<SELECT NAME=$question->id>"; |
98899fc0 |
454 | echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>"; |
0e30c987 |
455 | $options = explode( ",", $question->options); |
456 | foreach ($options as $key => $val) { |
457 | $key++; |
458 | echo "<OPTION VALUE=\"$key\">$val</OPTION>"; |
459 | } |
460 | echo "</SELECT>"; |
461 | |
462 | } else if ($question->type < 0) { // Choose several of a number |
463 | $options = explode( ",", $question->options); |
f762c448 |
464 | notify("This question type not supported yet"); |
0e30c987 |
465 | } |
466 | |
467 | echo "</FONT></TD></TR></TABLE>"; |
468 | |
469 | } |
470 | |
471 | function survey_question_color($qnum) { |
472 | global $THEME; |
473 | |
474 | if ($qnum) { |
475 | return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2; |
476 | //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF"; |
477 | } else { |
478 | return $THEME->cellcontent; |
479 | } |
480 | } |
481 | |
607809b3 |
482 | function survey_print_graph($url) { |
483 | global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH; |
484 | |
485 | if (empty($CFG->gdversion)) { |
486 | echo "(".get_string("gdneed").")"; |
487 | |
488 | } else { |
e80a8db7 |
489 | echo "<img height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\" border=\"1\"". |
a9ccbf60 |
490 | " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\">"; |
607809b3 |
491 | } |
492 | } |
493 | |
f9903ed0 |
494 | ?> |