7 $SURVEY_QTYPE = array (
8 "-3" => "Virtual Actual and Preferred",
9 "-2" => "Virtual Preferred",
10 "-1" => "Virtual Actual",
14 "3" => "Actual and Preferred",
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");
25 // STANDARD FUNCTIONS ////////////////////////////////////////////////////////
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.
33 if (!$template = get_record("survey", "id", $survey->template
)) {
37 $survey->questions
= $template->questions
;
38 $survey->timecreated
= time();
39 $survey->timemodified
= $survey->timecreated
;
41 return insert_record("survey", $survey);
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.
51 if (!$template = get_record("survey", "id", $survey->template
)) {
55 $survey->id
= $survey->instance
;
56 $survey->questions
= $template->questions
;
57 $survey->timemodified
= time();
59 return update_record("survey", $survey);
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.
67 if (! $survey = get_record("survey", "id", "$id")) {
73 if (! delete_records("survey_analysis", "survey", "$survey->id")) {
77 if (! delete_records("survey_answers", "survey", "$survey->id")) {
81 if (! delete_records("survey", "id", "$survey->id")) {
88 function survey_user_outline($course, $user, $mod, $survey) {
89 if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$user->id'")) {
91 $lastanswer = array_pop($answers);
93 $result->info
= get_string("done", "survey");
94 $result->time
= $lastanswer->time
;
101 function survey_user_complete($course, $user, $mod, $survey) {
104 if (survey_already_done($survey->id
, $user->id
)) {
105 if ($survey->template
== SURVEY_CIQ
) { // print out answers for critical incidents
107 $table->align
= array("left", "left");
109 $questions = get_records_list("survey_questions", "id", $survey->questions
);
110 $questionorder = explode(",", $survey->questions
);
112 foreach ($questionorder as $key=>$val) {
113 $question = $questions[$val];
114 $questiontext = get_string($question->shorttext
, "survey");
116 if ($answer = survey_get_user_answer($survey->id
, $question->id
, $user->id
)) {
117 $answertext = "$answer->answer1";
119 $answertext = "No answer";
121 $table->data
[] = array("<b>$questiontext</b>", $answertext);
127 survey_print_graph("id=$mod->id&sid=$user->id&type=student.png");
131 print_string("notdone", "survey");
135 function survey_print_recent_activity($course, $isteacher, $timestart) {
141 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
142 'course = \''.$course->id
.'\' AND '.
143 'module = \'survey\' AND '.
144 'action = \'submit\' ', 'time ASC')) {
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);
155 //Only if the mod is visible
157 $surveys[$log->id
] = survey_log_info($log);
158 $surveys[$log->id
]->time
= $log->time
;
159 $surveys[$log->id
]->url
= str_replace('&', '&', $log->url
);
165 print_headline(get_string('newsurveyresponses', 'survey').':');
166 foreach ($surveys as $survey) {
167 print_recent_activity_note($survey->time
, $survey, $survey->name
,
168 $CFG->wwwroot
.'/mod/survey/'.$survey->url
);
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)
181 //Get students from survey_analysis
182 $st_analysis = get_records_sql("SELECT DISTINCT u.id, u.id
183 FROM {$CFG->prefix}user u,
184 {$CFG->prefix}survey_analysis a
185 WHERE a.survey = '$surveyid' and
187 //Get students from survey_answers
188 $st_answers = get_records_sql("SELECT DISTINCT u.id, u.id
189 FROM {$CFG->prefix}user u,
190 {$CFG->prefix}survey_answers a
191 WHERE a.survey = '$surveyid' and
194 //Add st_answers to st_analysis
196 foreach ($st_answers as $st_answer) {
197 $st_analysis[$st_answer->id
] = $st_answer;
200 //Return st_analysis array (it contains an array of unique users)
201 return ($st_analysis);
204 // SQL FUNCTIONS ////////////////////////////////////////////////////////
207 function survey_log_info($log) {
209 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
210 FROM {$CFG->prefix}survey s,
212 WHERE s.id = '$log->info'
213 AND u.id = '$log->userid'");
216 function survey_get_responses($surveyid, $groupid, $groupingid) {
220 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid AND gm.groupid = '$groupid' ";
222 } else if ($groupingid) {
223 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
224 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
229 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, MAX(a.time) as time
230 FROM {$CFG->prefix}survey_answers a
231 INNER JOIN {$CFG->prefix}user u ON a.userid = u.id
233 WHERE a.survey = $surveyid
234 GROUP BY u.id, u.firstname, u.lastname, u.picture
238 function survey_get_analysis($survey, $user) {
241 return get_record_sql("SELECT notes
242 FROM {$CFG->prefix}survey_analysis
243 WHERE survey='$survey'
244 AND userid='$user'");
247 function survey_update_analysis($survey, $user, $notes) {
250 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
252 WHERE survey='$survey'
253 AND userid='$user'");
257 function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
261 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
266 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
267 FROM {$CFG->prefix}survey_answers sa,
268 {$CFG->prefix}user u,
269 {$CFG->prefix}groups_members gm
270 WHERE sa.survey = '$surveyid'
271 AND sa.question = $questionid
272 AND u.id = sa.userid $groupsql
276 function survey_get_user_answer($surveyid, $questionid, $userid) {
279 return get_record_sql("SELECT sa.*
280 FROM {$CFG->prefix}survey_answers sa
281 WHERE sa.survey = '$surveyid'
282 AND sa.question = '$questionid'
283 AND sa.userid = '$userid'");
286 // MODULE FUNCTIONS ////////////////////////////////////////////////////////
288 function survey_add_analysis($survey, $user, $notes) {
291 $record->survey
= $survey;
292 $record->userid
= $user;
293 $record->notes
= $notes;
295 return insert_record("survey_analysis", $record, false);
298 function survey_already_done($survey, $user) {
299 return record_exists("survey_answers", "survey", $survey, "userid", $user);
302 function survey_count_responses($surveyid, $groupid, $groupingid) {
303 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
304 return count($responses);
311 function survey_print_all_responses($cmid, $results, $courseid) {
313 $table->head
= array ("", get_string("name"), get_string("time"));
314 $table->align
= array ("", "left", "left");
315 $table->size
= array (35, "", "" );
317 foreach ($results as $a) {
318 $table->data
[] = array(print_user_picture($a->id
, $courseid, $a->picture
, false, true, false),
319 "<a href=\"report.php?action=student&student=$a->id&id=$cmid\">".fullname($a)."</a>",
327 function survey_get_template_name($templateid) {
331 if ($ss = get_record("surveys", "id", $templateid)) {
341 function survey_shorten_name ($name, $numwords) {
342 $words = explode(" ", $name);
343 for ($i=0; $i < $numwords; $i++
) {
344 $output .= $words[$i]." ";
351 function survey_print_multi($question) {
352 GLOBAL $USER, $db, $qnum, $checklist;
354 $stripreferthat = get_string("ipreferthat", "survey");
355 $strifoundthat = get_string("ifoundthat", "survey");
357 echo "<span class='questiontext'><b>$question->text</b></span><br />";
358 echo "<div style=\"text-align:center\">";
359 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
361 $options = explode( ",", $question->options
);
362 $numoptions = count($options);
364 $oneanswer = ($question->type
== 1 ||
$question->type
== 2) ?
true : false;
365 if ($question->type
== 2) {
372 echo "<tr><td colspan=\"2\">$question->intro</td>";
374 echo "<tr><td colspan=\"3\">$question->intro</td>";
377 while (list ($key, $val) = each ($options)) {
378 echo "<td class=\"smalltextcell\"><span class='smalltext'>$val</span></td>\n";
380 echo "<td align=\"center\"> </td></tr>\n";
382 $subquestions = get_records_list("survey_questions", "id", $question->multi
);
384 foreach ($subquestions as $q) {
386 $rowclass = survey_question_rowclass($qnum);
388 $q->text
= get_string($q->text
, "survey");
391 echo "<tr class=\"$rowclass\">";
394 echo "<td class=\"qnumtopcell\"><b>$qnum</b></td>";
395 echo "<td valign=\"top\">$q->text</td>";
396 for ($i=1;$i<=$numoptions;$i++
) {
397 $screenreader = !empty($USER->screenreader
)?
"<label for=\"q$P" . $q->id
. "_$i\">".$options[$i-1]."</label><br/>":'';
398 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id
. "_$i\" value=\"$i\" alt=\"$i\" /></td>";
400 echo "<td class=\"whitecell\"><input type=\"radio\" name=\"q$P$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
401 $checklist["q$P$q->id"] = $numoptions;
404 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
405 echo "<td class=\"qnummiddlecell\"><b>$qnum</b></td>";
407 echo "<td class=\"preferthat\"><span class='smalltext'>$stripreferthat </span></td>";
408 echo "<td class=\"optioncell\">$q->text</td>";
409 for ($i=1;$i<=$numoptions;$i++
) {
410 $screenreader = !empty($USER->screenreader
)?
"<label for=\"qP" . $q->id
. "_$i\">".$options[$i-1]."</label><br/>":'';
411 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"qP$q->id\" id=\"qP" . $q->id
. "_$i\" value=\"$i\" alt=\"$i\"/></td>";
413 echo "<td><input type=\"radio\" name=\"qP$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
416 echo "<tr class=\"$rowclass\">";
417 echo "<td class=\"qnumtopcell\"><b>$qnum</b></td>";
418 echo "<td class=\"foundthat\"><span class='smalltext'>$strifoundthat </span></td>";
419 echo "<td class=\"optioncell\">$q->text</td>";
420 for ($i=1;$i<=$numoptions;$i++
) {
421 $screenreader = !empty($USER->screenreader
)?
"<label for=\"q" . $q->id
. "_$i\">".$options[$i-1]."</label><br/>":'';
422 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"q$q->id\" id=\"q" . $q->id
. "_$i\" value=\"$i\" alt=\"$i\" /></td>";
424 echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
425 $checklist["qP$q->id"] = $numoptions;
426 $checklist["q$q->id"] = $numoptions;
437 function survey_print_single($question) {
440 $rowclass = survey_question_rowclass(0);
445 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
446 echo "<tr class=\"$rowclass\">";
447 echo "<td valign=\"top\"><b>$qnum</b></td>";
448 echo "<td class=\"questioncell\">$question->text</td>\n";
449 echo "<td class=\"questioncell\"><span class='smalltext'>\n";
452 if ($question->type
== 0) { // Plain text field
453 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\">$question->options</textarea>";
455 } else if ($question->type
> 0) { // Choose one of a number
456 $strchoose = get_string("choose");
457 echo "<select name=\"q$question->id\">";
458 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
459 $options = explode( ",", $question->options
);
460 foreach ($options as $key => $val) {
462 echo "<option value=\"$key\">$val</option>";
466 } else if ($question->type
< 0) { // Choose several of a number
467 $options = explode( ",", $question->options
);
468 notify("This question type not supported yet");
471 echo "</span></td></tr></table>";
475 function survey_question_rowclass($qnum) {
478 return $qnum %
2 ?
'r0' : 'r1';
484 function survey_print_graph($url) {
485 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
487 if (empty($CFG->gdversion
)) {
488 echo "(".get_string("gdneed").")";
491 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
492 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
496 function survey_get_view_actions() {
497 return array('download','view all','view form','view graph','view report');
500 function survey_get_post_actions() {
501 return array('submit');