Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / survey / lib.php
blob26e3a5496519e2c464279e669431e5086b3210b4
1 <?php // $Id$
3 // Graph size
4 $SURVEY_GHEIGHT = 500;
5 $SURVEY_GWIDTH = 900;
7 $SURVEY_QTYPE = array (
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",
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)) {
34 return 0;
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)) {
52 return 0;
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")) {
68 return false;
71 $result = true;
73 if (! delete_records("survey_analysis", "survey", "$survey->id")) {
74 $result = false;
77 if (! delete_records("survey_answers", "survey", "$survey->id")) {
78 $result = false;
81 if (! delete_records("survey", "id", "$survey->id")) {
82 $result = false;
85 return $result;
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;
95 return $result;
97 return NULL;
101 function survey_user_complete($course, $user, $mod, $survey) {
102 global $CFG;
104 if (survey_already_done($survey->id, $user->id)) {
105 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
106 $table = NULL;
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";
118 } else {
119 $answertext = "No answer";
121 $table->data[] = array("<b>$questiontext</b>", $answertext);
123 print_table($table);
125 } else {
127 survey_print_graph("id=$mod->id&amp;sid=$user->id&amp;type=student.png");
130 } else {
131 print_string("notdone", "survey");
135 function survey_print_recent_activity($course, $isteacher, $timestart) {
136 global $CFG;
138 $content = false;
139 $surveys = NULL;
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;
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
156 if ($modvisible) {
157 $surveys[$log->id] = survey_log_info($log);
158 $surveys[$log->id]->time = $log->time;
159 $surveys[$log->id]->url = str_replace('&', '&amp;', $log->url);
163 if ($surveys) {
164 $content = true;
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);
172 return $content;
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)
179 global $CFG;
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
186 u.id = a.userid");
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
192 u.id = a.userid");
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;
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) {
208 global $CFG;
209 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
210 FROM {$CFG->prefix}survey s,
211 {$CFG->prefix}user u
212 WHERE s.id = '$log->info'
213 AND u.id = '$log->userid'");
216 function survey_get_responses($surveyid, $groupid) {
217 global $CFG;
219 if ($groupid) {
220 $groupsdb = ', '. groups_members_from_sql();
221 $groupsql = 'AND'.groups_members_where_sql($groupid, 'u.id');
222 } else {
223 $groupsdb = "";
224 $groupsql = "";
227 return get_records_sql("SELECT MAX(a.time) as time,
228 u.id, u.firstname, u.lastname, u.picture
229 FROM {$CFG->prefix}survey_answers a,
230 {$CFG->prefix}user u $groupsdb
231 WHERE a.survey = $surveyid
232 AND a.userid = u.id $groupsql
233 GROUP BY u.id, u.firstname, u.lastname, u.picture
234 ORDER BY time ASC");
237 function survey_get_analysis($survey, $user) {
238 global $CFG;
240 return get_record_sql("SELECT notes
241 FROM {$CFG->prefix}survey_analysis
242 WHERE survey='$survey'
243 AND userid='$user'");
246 function survey_update_analysis($survey, $user, $notes) {
247 global $CFG;
249 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
250 SET notes='$notes'
251 WHERE survey='$survey'
252 AND userid='$user'");
256 function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
257 global $CFG;
259 if ($groupid) {
260 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
261 } else {
262 $groupsql = "";
265 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
266 FROM {$CFG->prefix}survey_answers sa,
267 {$CFG->prefix}user u,
268 {$CFG->prefix}groups_members gm
269 WHERE sa.survey = '$surveyid'
270 AND sa.question = $questionid
271 AND u.id = sa.userid $groupsql
272 ORDER BY $sort");
275 function survey_get_user_answer($surveyid, $questionid, $userid) {
276 global $CFG;
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'");
285 // MODULE FUNCTIONS ////////////////////////////////////////////////////////
287 function survey_add_analysis($survey, $user, $notes) {
288 global $CFG;
290 $record->survey = $survey;
291 $record->userid = $user;
292 $record->notes = $notes;
294 return insert_record("survey_analysis", $record, false);
297 function survey_already_done($survey, $user) {
298 return record_exists("survey_answers", "survey", $survey, "userid", $user);
301 function survey_count_responses($surveyid, $groupid) {
302 if ($responses = survey_get_responses($surveyid, $groupid)) {
303 return count($responses);
304 } else {
305 return 0;
310 function survey_print_all_responses($cmid, $results, $courseid) {
312 $table->head = array ("", get_string("name"), get_string("time"));
313 $table->align = array ("", "left", "left");
314 $table->size = array (35, "", "" );
316 foreach ($results as $a) {
317 $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
318 "<a href=\"report.php?action=student&amp;student=$a->id&amp;id=$cmid\">".fullname($a)."</a>",
319 userdate($a->time));
322 print_table($table);
326 function survey_get_template_name($templateid) {
327 global $db;
329 if ($templateid) {
330 if ($ss = get_record("surveys", "id", $templateid)) {
331 return $ss->name;
333 } else {
334 return "";
340 function survey_shorten_name ($name, $numwords) {
341 $words = explode(" ", $name);
342 for ($i=0; $i < $numwords; $i++) {
343 $output .= $words[$i]." ";
345 return $output;
350 function survey_print_multi($question) {
351 GLOBAL $USER, $db, $qnum, $checklist;
353 $stripreferthat = get_string("ipreferthat", "survey");
354 $strifoundthat = get_string("ifoundthat", "survey");
355 echo "<br />\n";
356 echo "<span class='questiontext'><b>$question->text</b></span><br />";
357 echo "<div style=\"text-align:center\">";
358 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
360 $options = explode( ",", $question->options);
361 $numoptions = count($options);
363 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
364 if ($question->type == 2) {
365 $P = "P";
366 } else {
367 $P = "";
370 if ($oneanswer) {
371 echo "<tr><td colspan=\"2\">$question->intro</td>";
372 } else {
373 echo "<tr><td colspan=\"3\">$question->intro</td>";
376 while (list ($key, $val) = each ($options)) {
377 echo "<td class=\"smalltextcell\"><span class='smalltext'>$val</span></td>\n";
379 echo "<td align=\"center\">&nbsp;</td></tr>\n";
381 $subquestions = get_records_list("survey_questions", "id", $question->multi);
383 foreach ($subquestions as $q) {
384 $qnum++;
385 $rowclass = survey_question_rowclass($qnum);
386 if ($q->text) {
387 $q->text = get_string($q->text, "survey");
390 echo "<tr class=\"$rowclass\">";
391 if ($oneanswer) {
393 echo "<td class=\"qnumtopcell\"><b>$qnum</b></td>";
394 echo "<td valign=\"top\">$q->text</td>";
395 for ($i=1;$i<=$numoptions;$i++) {
396 $screenreader = !empty($USER->screenreader)?"<label for=\"q$P" . $q->id . "_$i\">".$options[$i-1]."</label><br/>":'';
397 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_$i\" value=\"$i\" alt=\"$i\" /></td>";
399 echo "<td class=\"whitecell\"><input type=\"radio\" name=\"q$P$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
400 $checklist["q$P$q->id"] = $numoptions;
402 } else {
403 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
404 echo "<td class=\"qnummiddlecell\"><b>$qnum</b></td>";
405 $qnum++;
406 echo "<td class=\"preferthat\"><span class='smalltext'>$stripreferthat&nbsp;</span></td>";
407 echo "<td class=\"optioncell\">$q->text</td>";
408 for ($i=1;$i<=$numoptions;$i++) {
409 $screenreader = !empty($USER->screenreader)?"<label for=\"qP" . $q->id . "_$i\">".$options[$i-1]."</label><br/>":'';
410 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"qP$q->id\" id=\"qP" . $q->id . "_$i\" value=\"$i\" alt=\"$i\"/></td>";
412 echo "<td><input type=\"radio\" name=\"qP$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
413 echo "</tr>";
415 echo "<tr class=\"$rowclass\">";
416 echo "<td class=\"qnumtopcell\"><b>$qnum</b></td>";
417 echo "<td class=\"foundthat\"><span class='smalltext'>$strifoundthat&nbsp;</span></td>";
418 echo "<td class=\"optioncell\">$q->text</td>";
419 for ($i=1;$i<=$numoptions;$i++) {
420 $screenreader = !empty($USER->screenreader)?"<label for=\"q" . $q->id . "_$i\">".$options[$i-1]."</label><br/>":'';
421 echo "<td class=\"screenreadertext\">".$screenreader."<input type=\"radio\" name=\"q$q->id\" id=\"q" . $q->id . "_$i\" value=\"$i\" alt=\"$i\" /></td>";
423 echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
424 $checklist["qP$q->id"] = $numoptions;
425 $checklist["q$q->id"] = $numoptions;
427 echo "</tr>\n";
430 echo "</table>";
431 echo "</div>";
436 function survey_print_single($question) {
437 GLOBAL $db, $qnum;
439 $rowclass = survey_question_rowclass(0);
441 $qnum++;
443 echo "<br />\n";
444 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
445 echo "<tr class=\"$rowclass\">";
446 echo "<td valign=\"top\"><b>$qnum</b></td>";
447 echo "<td class=\"questioncell\">$question->text</td>\n";
448 echo "<td class=\"questioncell\"><span class='smalltext'>\n";
451 if ($question->type == 0) { // Plain text field
452 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\">$question->options</textarea>";
454 } else if ($question->type > 0) { // Choose one of a number
455 $strchoose = get_string("choose");
456 echo "<select name=\"q$question->id\">";
457 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
458 $options = explode( ",", $question->options);
459 foreach ($options as $key => $val) {
460 $key++;
461 echo "<option value=\"$key\">$val</option>";
463 echo "</select>";
465 } else if ($question->type < 0) { // Choose several of a number
466 $options = explode( ",", $question->options);
467 notify("This question type not supported yet");
470 echo "</span></td></tr></table>";
474 function survey_question_rowclass($qnum) {
476 if ($qnum) {
477 return $qnum % 2 ? 'r0' : 'r1';
478 } else {
479 return 'r0';
483 function survey_print_graph($url) {
484 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
486 if (empty($CFG->gdversion)) {
487 echo "(".get_string("gdneed").")";
489 } else {
490 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
491 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
495 function survey_get_view_actions() {
496 return array('download','view all','view form','view graph','view report');
499 function survey_get_post_actions() {
500 return array('submit');