d1290cec |
1 | <?php // $Id$ |
f9903ed0 |
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 | |
839f2456 |
127 | survey_print_graph("id=$mod->id&sid=$user->id&type=student.png"); |
b6b3deb4 |
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 | |
8f7dc7f1 |
141 | if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '. |
142 | 'course = \''.$course->id.'\' AND '. |
143 | 'module = \'survey\' AND '. |
144 | 'action = \'submit\' ', 'time ASC')) { |
1b5910c4 |
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; |
cfa56cff |
159 | $surveys[$log->id]->url = str_replace('&', '&', $log->url); |
54634efc |
160 | } |
161 | } |
162 | |
163 | if ($surveys) { |
164 | $content = true; |
8f7dc7f1 |
165 | print_headline(get_string('newsurveyresponses', 'survey').':'); |
54634efc |
166 | foreach ($surveys as $survey) { |
8f7dc7f1 |
167 | print_recent_activity_note($survey->time, $survey, $isteacher, $survey->name, |
168 | $CFG->wwwroot.'/mod/survey/'.$survey->url); |
54634efc |
169 | } |
170 | } |
171 | |
172 | return $content; |
173 | } |
04eba58f |
174 | |
05855091 |
175 | function survey_get_participants($surveyid) { |
176 | //Returns the users with data in one survey |
177 | //(users with records in survey_analysis and survey_answers, students) |
178 | |
179 | global $CFG; |
180 | |
181 | //Get students from survey_analysis |
95e72c12 |
182 | $st_analysis = get_records_sql("SELECT DISTINCT u.id, u.id |
05855091 |
183 | FROM {$CFG->prefix}user u, |
184 | {$CFG->prefix}survey_analysis a |
185 | WHERE a.survey = '$surveyid' and |
186 | u.id = a.userid"); |
187 | //Get students from survey_answers |
95e72c12 |
188 | $st_answers = get_records_sql("SELECT DISTINCT u.id, u.id |
05855091 |
189 | FROM {$CFG->prefix}user u, |
190 | {$CFG->prefix}survey_answers a |
191 | WHERE a.survey = '$surveyid' and |
192 | u.id = a.userid"); |
193 | |
194 | //Add st_answers to st_analysis |
195 | if ($st_answers) { |
196 | foreach ($st_answers as $st_answer) { |
197 | $st_analysis[$st_answer->id] = $st_answer; |
198 | } |
199 | } |
200 | //Return st_analysis array (it contains an array of unique users) |
201 | return ($st_analysis); |
202 | } |
66ea15f3 |
203 | |
e323955d |
204 | // SQL FUNCTIONS //////////////////////////////////////////////////////// |
66ea15f3 |
205 | |
f9903ed0 |
206 | |
e323955d |
207 | function survey_log_info($log) { |
208 | global $CFG; |
7c8c335f |
209 | return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture |
e323955d |
210 | FROM {$CFG->prefix}survey s, |
211 | {$CFG->prefix}user u |
212 | WHERE s.id = '$log->info' |
ebc3bd2b |
213 | AND u.id = '$log->userid'"); |
e323955d |
214 | } |
f9903ed0 |
215 | |
a9ccbf60 |
216 | function survey_get_responses($surveyid, $groupid) { |
e323955d |
217 | global $CFG; |
a9ccbf60 |
218 | |
219 | if ($groupid) { |
840c1781 |
220 | $groupsdb = ", {$CFG->prefix}groups_members AS gm"; |
a9ccbf60 |
221 | $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; |
222 | } else { |
840c1781 |
223 | $groupsdb = ""; |
a9ccbf60 |
224 | $groupsql = ""; |
225 | } |
226 | |
734d2c98 |
227 | return get_records_sql("SELECT MAX(a.time) as time, |
7c8c335f |
228 | u.id, u.firstname, u.lastname, u.picture |
e323955d |
229 | FROM {$CFG->prefix}survey_answers AS a, |
840c1781 |
230 | {$CFG->prefix}user AS u $groupsdb |
a9ccbf60 |
231 | WHERE a.survey = $surveyid |
232 | AND a.userid = u.id $groupsql |
90c6ded2 |
233 | GROUP BY u.id, u.firstname, u.lastname, u.picture |
734d2c98 |
234 | ORDER BY time ASC"); |
e323955d |
235 | } |
236 | |
237 | function survey_get_analysis($survey, $user) { |
ebc3bd2b |
238 | global $CFG; |
e323955d |
239 | |
240 | return get_record_sql("SELECT notes |
241 | FROM {$CFG->prefix}survey_analysis |
242 | WHERE survey='$survey' |
ebc3bd2b |
243 | AND userid='$user'"); |
e323955d |
244 | } |
245 | |
246 | function survey_update_analysis($survey, $user, $notes) { |
ebc3bd2b |
247 | global $CFG; |
e323955d |
248 | |
ebc3bd2b |
249 | return execute_sql("UPDATE {$CFG->prefix}survey_analysis |
e323955d |
250 | SET notes='$notes' |
251 | WHERE survey='$survey' |
ebc3bd2b |
252 | AND userid='$user'"); |
e323955d |
253 | } |
254 | |
255 | |
a9ccbf60 |
256 | function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") { |
e323955d |
257 | global $CFG; |
258 | |
a9ccbf60 |
259 | if ($groupid) { |
260 | $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; |
261 | } else { |
262 | $groupsql = ""; |
263 | } |
264 | |
e323955d |
265 | return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture |
ebc3bd2b |
266 | FROM {$CFG->prefix}survey_answers sa, |
a9ccbf60 |
267 | {$CFG->prefix}user u, |
268 | {$CFG->prefix}groups_members gm |
e323955d |
269 | WHERE sa.survey = '$surveyid' |
270 | AND sa.question = $questionid |
a9ccbf60 |
271 | AND u.id = sa.userid $groupsql |
7c8c335f |
272 | ORDER BY $sort"); |
273 | } |
274 | |
275 | function survey_get_user_answer($surveyid, $questionid, $userid) { |
276 | global $CFG; |
277 | |
278 | return get_record_sql("SELECT sa.* |
279 | FROM {$CFG->prefix}survey_answers sa |
280 | WHERE sa.survey = '$surveyid' |
281 | AND sa.question = '$questionid' |
282 | AND sa.userid = '$userid'"); |
e323955d |
283 | } |
284 | |
285 | // MODULE FUNCTIONS //////////////////////////////////////////////////////// |
286 | |
ebc3bd2b |
287 | function survey_add_analysis($survey, $user, $notes) { |
288 | global $CFG; |
289 | |
290 | $record->survey = $survey; |
291 | $record->userid = $user; |
292 | $record->notes = $notes; |
293 | |
294 | return insert_record("survey_analysis", $record, false); |
295 | } |
296 | |
e323955d |
297 | function survey_already_done($survey, $user) { |
ebc3bd2b |
298 | return record_exists("survey_answers", "survey", $survey, "userid", $user); |
551b0b98 |
299 | } |
f9903ed0 |
300 | |
a9ccbf60 |
301 | function survey_count_responses($surveyid, $groupid) { |
302 | if ($responses = survey_get_responses($surveyid, $groupid)) { |
551b0b98 |
303 | return count($responses); |
304 | } else { |
305 | return 0; |
306 | } |
f9903ed0 |
307 | } |
308 | |
b416a1c3 |
309 | |
7c8c335f |
310 | function survey_print_all_responses($cmid, $results, $courseid) { |
b416a1c3 |
311 | |
a30f997e |
312 | $table->head = array ("", get_string("name"), get_string("time")); |
313 | $table->align = array ("", "left", "left"); |
314 | $table->size = array (35, "", "" ); |
b416a1c3 |
315 | |
316 | foreach ($results as $a) { |
7c8c335f |
317 | $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false), |
839f2456 |
318 | "<a href=\"report.php?action=student&student=$a->id&id=$cmid\">".fullname($a)."</a>", |
a30f997e |
319 | userdate($a->time)); |
b416a1c3 |
320 | } |
7c8c335f |
321 | |
322 | print_table($table); |
b416a1c3 |
323 | } |
324 | |
b416a1c3 |
325 | |
551b0b98 |
326 | function survey_get_template_name($templateid) { |
f9903ed0 |
327 | global $db; |
328 | |
329 | if ($templateid) { |
e323955d |
330 | if ($ss = get_record("surveys", "id", $templateid)) { |
331 | return $ss->name; |
f9903ed0 |
332 | } |
333 | } else { |
334 | return ""; |
335 | } |
336 | } |
337 | |
0d22e7cc |
338 | |
f9903ed0 |
339 | |
0e30c987 |
340 | function survey_shorten_name ($name, $numwords) { |
341 | $words = explode(" ", $name); |
342 | for ($i=0; $i < $numwords; $i++) { |
343 | $output .= $words[$i]." "; |
344 | } |
345 | return $output; |
346 | } |
f9903ed0 |
347 | |
348 | |
0e30c987 |
349 | |
350 | function survey_print_multi($question) { |
f1c1d2f1 |
351 | GLOBAL $db, $qnum, $checklist; |
0e30c987 |
352 | |
353 | |
98899fc0 |
354 | $stripreferthat = get_string("ipreferthat", "survey"); |
355 | $strifoundthat = get_string("ifoundthat", "survey"); |
caf261b0 |
356 | echo "<br />\n"; |
8a12a132 |
357 | echo "<font size=\"4\"><b>$question->text</b></font><br />"; |
0e30c987 |
358 | |
8a12a132 |
359 | echo "<table align=\"center\" width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">"; |
0e30c987 |
360 | |
361 | $options = explode( ",", $question->options); |
362 | $numoptions = count($options); |
363 | |
364 | $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false; |
8a12a132 |
365 | if ($question->type == 2) { |
366 | $P = "P"; |
367 | } else { |
368 | $P = ""; |
369 | } |
0e30c987 |
370 | |
371 | if ($oneanswer) { |
caf261b0 |
372 | echo "<tr><td colspan=\"2\">$question->intro</td>"; |
0e30c987 |
373 | } else { |
caf261b0 |
374 | echo "<tr><td colspan=\"3\">$question->intro</td>"; |
0e30c987 |
375 | } |
376 | |
377 | while (list ($key, $val) = each ($options)) { |
caf261b0 |
378 | echo "<td width=\"10%\" align=\"center\"><font size=\"1\">$val</font></td>\n"; |
0e30c987 |
379 | } |
f1c1d2f1 |
380 | echo "<td align=\"center\"> </td></tr>\n"; |
0e30c987 |
381 | |
e323955d |
382 | $subquestions = get_records_list("survey_questions", "id", $question->multi); |
0e30c987 |
383 | |
384 | foreach ($subquestions as $q) { |
385 | $qnum++; |
f1c1d2f1 |
386 | $rowclass = survey_question_rowclass($qnum); |
0e30c987 |
387 | |
f762c448 |
388 | if ($q->text) { |
389 | $q->text = get_string($q->text, "survey"); |
390 | } |
391 | |
f1c1d2f1 |
392 | echo "<tr class=\"$rowclass\">"; |
0e30c987 |
393 | if ($oneanswer) { |
caf261b0 |
394 | echo "<td width=\"10\" valign=\"top\"><b>$qnum</b></td>"; |
395 | echo "<td valign=\"top\">$q->text</td>"; |
0e30c987 |
396 | for ($i=1;$i<=$numoptions;$i++) { |
caf261b0 |
397 | echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$P$q->id\" value=\"$i\" alt=\"$i\" /></td>"; |
0e30c987 |
398 | } |
caf261b0 |
399 | echo "<td bgcolor=\"white\"><input type=\"radio\" name=\"q$P$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>"; |
0e30c987 |
400 | $checklist["q$P$q->id"] = $numoptions; |
401 | |
402 | } else { |
caf261b0 |
403 | echo "<td width=\"10\" valign=\"middle\" rowspan=\"2\"><b>$qnum</b></td>"; |
404 | echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$stripreferthat </font></td>"; |
405 | echo "<td width=\"40%\" valign=\"middle\" rowspan=\"2\">$q->text</td>"; |
0e30c987 |
406 | for ($i=1;$i<=$numoptions;$i++) { |
caf261b0 |
407 | echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"qP$q->id\" value=\"$i\" alt=\"$i\"/></td>"; |
0e30c987 |
408 | } |
f1c1d2f1 |
409 | echo "<td><input type=\"radio\" name=\"qP$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>"; |
d1290cec |
410 | echo "</tr>"; |
0e30c987 |
411 | |
f1c1d2f1 |
412 | echo "<tr class=\"$rowclass\">"; |
caf261b0 |
413 | echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$strifoundthat </font></td>"; |
0e30c987 |
414 | for ($i=1;$i<=$numoptions;$i++) { |
caf261b0 |
415 | echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$q->id\" value=\"$i\" alt=\"$i\" /></td>"; |
0e30c987 |
416 | } |
f1c1d2f1 |
417 | echo "<td width=\"5%\"><input type=\"radio\" name=\"q$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>"; |
0e30c987 |
418 | $checklist["qP$q->id"] = $numoptions; |
419 | $checklist["q$q->id"] = $numoptions; |
420 | } |
d1290cec |
421 | echo "</tr>\n"; |
0e30c987 |
422 | } |
d1290cec |
423 | echo "</table>"; |
0e30c987 |
424 | } |
425 | |
426 | |
427 | |
428 | function survey_print_single($question) { |
429 | GLOBAL $db, $qnum; |
430 | |
f1c1d2f1 |
431 | $rowclass = survey_question_rowclass(0); |
0e30c987 |
432 | |
433 | $qnum++; |
434 | |
caf261b0 |
435 | echo "<br />\n"; |
d1290cec |
436 | echo "<table align=\"center\" width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n"; |
f1c1d2f1 |
437 | echo "<tr class=\"$rowclass\">"; |
d1290cec |
438 | echo "<td valign=\"top\"><b>$qnum</b></td>"; |
caf261b0 |
439 | echo "<td width=\"50%\" valign=\"top\">$question->text</td>\n"; |
440 | echo "<td width=\"50%\" valign=\"top\"><font size=\"+1\">\n"; |
0e30c987 |
441 | |
442 | |
443 | if ($question->type == 0) { // Plain text field |
caf261b0 |
444 | echo "<textarea rows=\"3\" cols=\"30\" name=\"$question->id\">$question->options</textarea>"; |
0e30c987 |
445 | |
446 | } else if ($question->type > 0) { // Choose one of a number |
98899fc0 |
447 | $strchoose = get_string("choose"); |
caf261b0 |
448 | echo "<select name=\"$question->id\">"; |
e0b7b090 |
449 | echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>"; |
0e30c987 |
450 | $options = explode( ",", $question->options); |
451 | foreach ($options as $key => $val) { |
452 | $key++; |
d1290cec |
453 | echo "<option value=\"$key\">$val</option>"; |
0e30c987 |
454 | } |
d1290cec |
455 | echo "</select>"; |
0e30c987 |
456 | |
457 | } else if ($question->type < 0) { // Choose several of a number |
458 | $options = explode( ",", $question->options); |
f762c448 |
459 | notify("This question type not supported yet"); |
0e30c987 |
460 | } |
461 | |
d1290cec |
462 | echo "</font></td></tr></table>"; |
0e30c987 |
463 | |
464 | } |
465 | |
f1c1d2f1 |
466 | function survey_question_rowclass($qnum) { |
0e30c987 |
467 | |
468 | if ($qnum) { |
f1c1d2f1 |
469 | return $qnum % 2 ? 'r0' : 'r1'; |
0e30c987 |
470 | } else { |
f1c1d2f1 |
471 | return 'r0'; |
0e30c987 |
472 | } |
473 | } |
474 | |
607809b3 |
475 | function survey_print_graph($url) { |
476 | global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH; |
477 | |
478 | if (empty($CFG->gdversion)) { |
479 | echo "(".get_string("gdneed").")"; |
480 | |
481 | } else { |
e80a8db7 |
482 | echo "<img height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\" border=\"1\"". |
839f2456 |
483 | " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"\" />"; |
607809b3 |
484 | } |
485 | } |
486 | |
f9903ed0 |
487 | ?> |