Commit | Line | Data |
---|---|---|
e5cc530b | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
f9903ed0 | 17 | |
e5cc530b | 18 | /** |
19 | * @package mod-survey | |
20 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
22 | */ | |
23 | ||
24 | /** | |
25 | * Graph size | |
26 | * @global int $SURVEY_GHEIGHT | |
27 | */ | |
17da2e6f | 28 | global $SURVEY_GHEIGHT; |
551b0b98 | 29 | $SURVEY_GHEIGHT = 500; |
e5cc530b | 30 | /** |
31 | * Graph size | |
32 | * @global int $SURVEY_GWIDTH | |
33 | */ | |
17da2e6f | 34 | global $SURVEY_GWIDTH; |
551b0b98 | 35 | $SURVEY_GWIDTH = 900; |
e5cc530b | 36 | /** |
37 | * Question Type | |
38 | * @global array $SURVEY_QTYPE | |
39 | */ | |
17da2e6f | 40 | global $SURVEY_QTYPE; |
551b0b98 | 41 | $SURVEY_QTYPE = array ( |
f9903ed0 | 42 | "-3" => "Virtual Actual and Preferred", |
43 | "-2" => "Virtual Preferred", | |
44 | "-1" => "Virtual Actual", | |
45 | "0" => "Text", | |
46 | "1" => "Actual", | |
47 | "2" => "Preferred", | |
48 | "3" => "Actual and Preferred", | |
49 | ); | |
50 | ||
b6b3deb4 | 51 | |
52 | define("SURVEY_COLLES_ACTUAL", "1"); | |
53 | define("SURVEY_COLLES_PREFERRED", "2"); | |
54 | define("SURVEY_COLLES_PREFERRED_ACTUAL", "3"); | |
55 | define("SURVEY_ATTLS", "4"); | |
56 | define("SURVEY_CIQ", "5"); | |
57 | ||
58 | ||
66ea15f3 | 59 | // STANDARD FUNCTIONS //////////////////////////////////////////////////////// |
e5cc530b | 60 | /** |
61 | * Given an object containing all the necessary data, | |
62 | * (defined by the form in mod_form.php) this function | |
63 | * will create a new instance and return the id number | |
64 | * of the new instance. | |
65 | * | |
66 | * @global object | |
67 | * @param object $survey | |
68 | * @return int|bool | |
69 | */ | |
04eba58f | 70 | function survey_add_instance($survey) { |
c18269c7 | 71 | global $DB; |
04eba58f | 72 | |
c18269c7 | 73 | if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) { |
04eba58f | 74 | return 0; |
75 | } | |
76 | ||
e5dd8e3b | 77 | $survey->questions = $template->questions; |
04eba58f | 78 | $survey->timecreated = time(); |
79 | $survey->timemodified = $survey->timecreated; | |
80 | ||
c18269c7 | 81 | return $DB->insert_record("survey", $survey); |
04eba58f | 82 | |
83 | } | |
84 | ||
e5cc530b | 85 | /** |
86 | * Given an object containing all the necessary data, | |
87 | * (defined by the form in mod_form.php) this function | |
88 | * will update an existing instance with new data. | |
89 | * | |
90 | * @global object | |
91 | * @param object $survey | |
92 | * @return bool | |
93 | */ | |
04eba58f | 94 | function survey_update_instance($survey) { |
c18269c7 | 95 | global $DB; |
04eba58f | 96 | |
c18269c7 | 97 | if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) { |
04eba58f | 98 | return 0; |
99 | } | |
100 | ||
e5dd8e3b PS |
101 | $survey->id = $survey->instance; |
102 | $survey->questions = $template->questions; | |
04eba58f | 103 | $survey->timemodified = time(); |
104 | ||
c18269c7 | 105 | return $DB->update_record("survey", $survey); |
04eba58f | 106 | } |
107 | ||
e5cc530b | 108 | /** |
109 | * Given an ID of an instance of this module, | |
110 | * this function will permanently delete the instance | |
e5dd8e3b | 111 | * and any data that depends on it. |
e5cc530b | 112 | * |
113 | * @global object | |
114 | * @param int $id | |
115 | * @return bool | |
116 | */ | |
04eba58f | 117 | function survey_delete_instance($id) { |
c18269c7 | 118 | global $DB; |
04eba58f | 119 | |
c18269c7 | 120 | if (! $survey = $DB->get_record("survey", array("id"=>$id))) { |
04eba58f | 121 | return false; |
122 | } | |
123 | ||
124 | $result = true; | |
125 | ||
c18269c7 | 126 | if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) { |
04eba58f | 127 | $result = false; |
128 | } | |
129 | ||
c18269c7 | 130 | if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) { |
04eba58f | 131 | $result = false; |
132 | } | |
133 | ||
c18269c7 | 134 | if (! $DB->delete_records("survey", array("id"=>$survey->id))) { |
04eba58f | 135 | $result = false; |
136 | } | |
137 | ||
138 | return $result; | |
139 | } | |
140 | ||
e5cc530b | 141 | /** |
142 | * @global object | |
143 | * @param object $course | |
144 | * @param object $user | |
145 | * @param object $mod | |
146 | * @param object $survey | |
147 | * @return $result | |
148 | */ | |
66ea15f3 | 149 | function survey_user_outline($course, $user, $mod, $survey) { |
deb3a60d | 150 | global $DB; |
66ea15f3 | 151 | |
deb3a60d | 152 | if ($answers = $DB->get_records("survey_answers", array('survey'=>$survey->id, 'userid'=>$user->id))) { |
66ea15f3 | 153 | $lastanswer = array_pop($answers); |
154 | ||
deb3a60d | 155 | $result = new object(); |
66ea15f3 | 156 | $result->info = get_string("done", "survey"); |
157 | $result->time = $lastanswer->time; | |
158 | return $result; | |
159 | } | |
160 | return NULL; | |
161 | } | |
162 | ||
e5cc530b | 163 | /** |
164 | * @global stdObject | |
165 | * @global object | |
166 | * @uses SURVEY_CIQ | |
167 | * @param object $course | |
168 | * @param object $user | |
169 | * @param object $mod | |
170 | * @param object $survey | |
171 | */ | |
66ea15f3 | 172 | function survey_user_complete($course, $user, $mod, $survey) { |
02f78a26 | 173 | global $CFG, $DB, $OUTPUT; |
66ea15f3 | 174 | |
175 | if (survey_already_done($survey->id, $user->id)) { | |
b6b3deb4 | 176 | if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents |
02f78a26 | 177 | $table = new html_table(); |
b6b3deb4 | 178 | $table->align = array("left", "left"); |
179 | ||
44e1b7d7 | 180 | $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions)); |
b6b3deb4 | 181 | $questionorder = explode(",", $survey->questions); |
e5dd8e3b | 182 | |
b6b3deb4 | 183 | foreach ($questionorder as $key=>$val) { |
184 | $question = $questions[$val]; | |
185 | $questiontext = get_string($question->shorttext, "survey"); | |
e5dd8e3b | 186 | |
b6b3deb4 | 187 | if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) { |
188 | $answertext = "$answer->answer1"; | |
189 | } else { | |
190 | $answertext = "No answer"; | |
191 | } | |
192 | $table->data[] = array("<b>$questiontext</b>", $answertext); | |
193 | } | |
16be8974 | 194 | echo html_writer::table($table); |
e5dd8e3b | 195 | |
b6b3deb4 | 196 | } else { |
e5dd8e3b | 197 | |
839f2456 | 198 | survey_print_graph("id=$mod->id&sid=$user->id&type=student.png"); |
b6b3deb4 | 199 | } |
e5dd8e3b | 200 | |
66ea15f3 | 201 | } else { |
202 | print_string("notdone", "survey"); | |
203 | } | |
204 | } | |
205 | ||
e5cc530b | 206 | /** |
207 | * @global stdClass | |
208 | * @global object | |
209 | * @param object $course | |
210 | * @param mixed $viewfullnames | |
211 | * @param int $timestamp | |
212 | * @return bool | |
213 | */ | |
dd97c328 | 214 | function survey_print_recent_activity($course, $viewfullnames, $timestart) { |
11e0676a | 215 | global $CFG, $DB, $OUTPUT; |
54634efc | 216 | |
dd97c328 | 217 | $modinfo = get_fast_modinfo($course); |
218 | $ids = array(); | |
219 | foreach ($modinfo->cms as $cm) { | |
220 | if ($cm->modname != 'survey') { | |
221 | continue; | |
222 | } | |
223 | if (!$cm->uservisible) { | |
224 | continue; | |
225 | } | |
226 | $ids[$cm->instance] = $cm->instance; | |
227 | } | |
54634efc | 228 | |
dd97c328 | 229 | if (!$ids) { |
1b5910c4 | 230 | return false; |
231 | } | |
232 | ||
dd97c328 | 233 | $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right? |
234 | ||
deb3a60d | 235 | if (!$rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time, |
236 | u.firstname, u.lastname, u.email, u.picture | |
237 | FROM {survey_answers} sa | |
238 | JOIN {user} u ON u.id = sa.userid | |
239 | WHERE sa.survey IN ($slist) AND sa.time > ? | |
240 | GROUP BY sa.userid, sa.survey, u.firstname, u.lastname, u.email, u.picture | |
241 | ORDER BY time ASC", array($timestart))) { | |
dd97c328 | 242 | return false; |
54634efc | 243 | } |
244 | ||
dd97c328 | 245 | $surveys = array(); |
246 | ||
deb3a60d | 247 | foreach ($rs as $survey) { |
dd97c328 | 248 | $cm = $modinfo->instances['survey'][$survey->survey]; |
249 | $survey->name = $cm->name; | |
250 | $survey->cmid = $cm->id; | |
251 | $surveys[] = $survey; | |
e5dd8e3b | 252 | } |
deb3a60d | 253 | $rs->close(); |
dd97c328 | 254 | |
255 | if (!$surveys) { | |
256 | return false; | |
257 | } | |
258 | ||
11e0676a | 259 | echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey').':'); |
dd97c328 | 260 | foreach ($surveys as $survey) { |
d5edb961 | 261 | $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid; |
dd97c328 | 262 | print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames); |
54634efc | 263 | } |
e5dd8e3b | 264 | |
dd97c328 | 265 | return true; |
54634efc | 266 | } |
04eba58f | 267 | |
e5cc530b | 268 | /** |
269 | * Returns the users with data in one survey | |
270 | * (users with records in survey_analysis and survey_answers, students) | |
271 | * | |
272 | * @global object | |
273 | * @param int $surveyid | |
274 | * @return array | |
275 | */ | |
05855091 | 276 | function survey_get_participants($surveyid) { |
deb3a60d | 277 | global $DB; |
05855091 | 278 | |
279 | //Get students from survey_analysis | |
deb3a60d | 280 | $st_analysis = $DB->get_records_sql("SELECT DISTINCT u.id, u.id |
281 | FROM {user} u, {survey_analysis} a | |
282 | WHERE a.survey = ? AND | |
283 | u.id = a.userid", array($surveyid)); | |
05855091 | 284 | //Get students from survey_answers |
deb3a60d | 285 | $st_answers = $DB->get_records_sql("SELECT DISTINCT u.id, u.id |
286 | FROM {user} u, {survey_answers} a | |
287 | WHERE a.survey = ? AND | |
288 | u.id = a.userid", array($surveyid)); | |
05855091 | 289 | |
290 | //Add st_answers to st_analysis | |
291 | if ($st_answers) { | |
292 | foreach ($st_answers as $st_answer) { | |
293 | $st_analysis[$st_answer->id] = $st_answer; | |
294 | } | |
295 | } | |
296 | //Return st_analysis array (it contains an array of unique users) | |
297 | return ($st_analysis); | |
298 | } | |
66ea15f3 | 299 | |
e323955d | 300 | // SQL FUNCTIONS //////////////////////////////////////////////////////// |
66ea15f3 | 301 | |
e5cc530b | 302 | /** |
303 | * @global object | |
304 | * @param sting $log | |
305 | * @return array | |
306 | */ | |
e323955d | 307 | function survey_log_info($log) { |
deb3a60d | 308 | global $DB; |
309 | return $DB->get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture | |
310 | FROM {survey} s, {user} u | |
311 | WHERE s.id = ? AND u.id = ?", array($log->info, $log->userid)); | |
e323955d | 312 | } |
f9903ed0 | 313 | |
e5cc530b | 314 | /** |
315 | * @global object | |
316 | * @param int $surveyid | |
317 | * @param int $groupid | |
318 | * @param int $groupingid | |
319 | * @return array | |
320 | */ | |
07aa2057 | 321 | function survey_get_responses($surveyid, $groupid, $groupingid) { |
deb3a60d | 322 | global $DB; |
323 | ||
e5dd8e3b | 324 | $params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid); |
a9ccbf60 | 325 | |
326 | if ($groupid) { | |
deb3a60d | 327 | $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid "; |
1d684195 | 328 | |
07aa2057 | 329 | } else if ($groupingid) { |
deb3a60d | 330 | $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid |
331 | JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid "; | |
a9ccbf60 | 332 | } else { |
07aa2057 | 333 | $groupsjoin = ""; |
a9ccbf60 | 334 | } |
335 | ||
deb3a60d | 336 | return $DB->get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, MAX(a.time) as time |
337 | FROM {survey_answers} a | |
338 | JOIN {user} u ON a.userid = u.id | |
339 | $groupsjoin | |
340 | WHERE a.survey = :surveyid | |
341 | GROUP BY u.id, u.firstname, u.lastname, u.picture | |
342 | ORDER BY time ASC", $params); | |
e323955d | 343 | } |
344 | ||
e5cc530b | 345 | /** |
346 | * @global object | |
347 | * @param int $survey | |
348 | * @param int $user | |
349 | * @return array | |
350 | */ | |
e323955d | 351 | function survey_get_analysis($survey, $user) { |
deb3a60d | 352 | global $DB; |
e323955d | 353 | |
e5dd8e3b | 354 | return $DB->get_record_sql("SELECT notes |
deb3a60d | 355 | FROM {survey_analysis} |
356 | WHERE survey=? AND userid=?", array($survey, $user)); | |
e323955d | 357 | } |
358 | ||
e5cc530b | 359 | /** |
360 | * @global object | |
361 | * @param int $survey | |
362 | * @param int $user | |
363 | * @param string $notes | |
364 | */ | |
e323955d | 365 | function survey_update_analysis($survey, $user, $notes) { |
deb3a60d | 366 | global $DB; |
e323955d | 367 | |
deb3a60d | 368 | return $DB->execute("UPDATE {survey_analysis} |
e5dd8e3b PS |
369 | SET notes=? |
370 | WHERE survey=? | |
deb3a60d | 371 | AND userid=?", array($notes, $survey, $user)); |
e323955d | 372 | } |
373 | ||
e5cc530b | 374 | /** |
375 | * @global object | |
376 | * @param int $surveyid | |
377 | * @param int $groupid | |
378 | * @param string $sort | |
379 | * @return array | |
380 | */ | |
a9ccbf60 | 381 | function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") { |
deb3a60d | 382 | global $DB; |
383 | ||
384 | $params = array('surveyid'=>$surveyid, 'questionid'=>$questionid, 'groupid'=>$groupid); | |
e323955d | 385 | |
a9ccbf60 | 386 | if ($groupid) { |
deb3a60d | 387 | $groupsql = "AND gm.groupid = :groupid AND u.id = gm.userid"; |
a9ccbf60 | 388 | } else { |
389 | $groupsql = ""; | |
390 | } | |
391 | ||
e5dd8e3b PS |
392 | return $DB->get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture |
393 | FROM {survey_answers} sa, {user} u, {groups_members} gm | |
394 | WHERE sa.survey = :surveyid | |
395 | AND sa.question = :questionid | |
deb3a60d | 396 | AND u.id = sa.userid $groupsql |
397 | ORDER BY $sort", $params); | |
7c8c335f | 398 | } |
399 | ||
e5cc530b | 400 | /** |
401 | * @global object | |
402 | * @param int $surveyid | |
403 | * @param int $questionid | |
404 | * @param int $userid | |
405 | * @return array | |
406 | */ | |
7c8c335f | 407 | function survey_get_user_answer($surveyid, $questionid, $userid) { |
deb3a60d | 408 | global $DB; |
7c8c335f | 409 | |
e5dd8e3b | 410 | return $DB->get_record_sql("SELECT sa.* |
deb3a60d | 411 | FROM {survey_answers} sa |
e5dd8e3b PS |
412 | WHERE sa.survey = ? |
413 | AND sa.question = ? | |
deb3a60d | 414 | AND sa.userid = ?", array($surveyid, $questionid, $userid)); |
e323955d | 415 | } |
416 | ||
417 | // MODULE FUNCTIONS //////////////////////////////////////////////////////// | |
e5cc530b | 418 | /** |
419 | * @global object | |
420 | * @param int $survey | |
421 | * @param int $user | |
422 | * @param string $notes | |
423 | * @return bool|int | |
424 | */ | |
ebc3bd2b | 425 | function survey_add_analysis($survey, $user, $notes) { |
deb3a60d | 426 | global $DB; |
ebc3bd2b | 427 | |
deb3a60d | 428 | $record = new object(); |
ebc3bd2b | 429 | $record->survey = $survey; |
430 | $record->userid = $user; | |
431 | $record->notes = $notes; | |
432 | ||
deb3a60d | 433 | return $DB->insert_record("survey_analysis", $record, false); |
ebc3bd2b | 434 | } |
e5cc530b | 435 | /** |
436 | * @global object | |
437 | * @param int $survey | |
438 | * @param int $user | |
439 | * @return bool | |
440 | */ | |
e323955d | 441 | function survey_already_done($survey, $user) { |
deb3a60d | 442 | global $DB; |
443 | ||
444 | return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user)); | |
551b0b98 | 445 | } |
e5cc530b | 446 | /** |
447 | * @param int $surveyid | |
448 | * @param int $groupid | |
449 | * @param int $groupingid | |
450 | * @return int | |
451 | */ | |
07aa2057 | 452 | function survey_count_responses($surveyid, $groupid, $groupingid) { |
453 | if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) { | |
551b0b98 | 454 | return count($responses); |
455 | } else { | |
456 | return 0; | |
457 | } | |
f9903ed0 | 458 | } |
459 | ||
e5cc530b | 460 | /** |
461 | * @param int $cmid | |
462 | * @param array $results | |
463 | * @param int $courseid | |
464 | */ | |
7c8c335f | 465 | function survey_print_all_responses($cmid, $results, $courseid) { |
02f78a26 | 466 | global $OUTPUT; |
467 | $table = new html_table(); | |
a30f997e | 468 | $table->head = array ("", get_string("name"), get_string("time")); |
469 | $table->align = array ("", "left", "left"); | |
470 | $table->size = array (35, "", "" ); | |
b416a1c3 | 471 | |
472 | foreach ($results as $a) { | |
812dbaf7 | 473 | $table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)), |
9bf16314 | 474 | html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)), |
a30f997e | 475 | userdate($a->time)); |
b416a1c3 | 476 | } |
7c8c335f | 477 | |
16be8974 | 478 | echo html_writer::table($table); |
b416a1c3 | 479 | } |
480 | ||
e5cc530b | 481 | /** |
482 | * @global object | |
483 | * @param int $templateid | |
484 | * @return string | |
485 | */ | |
551b0b98 | 486 | function survey_get_template_name($templateid) { |
fd0e6640 | 487 | global $DB; |
f9903ed0 | 488 | |
489 | if ($templateid) { | |
deb3a60d | 490 | if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) { |
e323955d | 491 | return $ss->name; |
f9903ed0 | 492 | } |
493 | } else { | |
494 | return ""; | |
495 | } | |
496 | } | |
497 | ||
0d22e7cc | 498 | |
e5cc530b | 499 | /** |
500 | * @param string $name | |
501 | * @param array $numwords | |
502 | * @return string | |
503 | */ | |
0e30c987 | 504 | function survey_shorten_name ($name, $numwords) { |
505 | $words = explode(" ", $name); | |
506 | for ($i=0; $i < $numwords; $i++) { | |
507 | $output .= $words[$i]." "; | |
508 | } | |
509 | return $output; | |
510 | } | |
f9903ed0 | 511 | |
e5cc530b | 512 | /** |
513 | * @todo Check this function | |
514 | * | |
515 | * @global object | |
516 | * @global object | |
517 | * @global int | |
518 | * @global void This is never defined | |
519 | * @global object This is defined twice? | |
520 | * @param object $question | |
521 | */ | |
0e30c987 | 522 | function survey_print_multi($question) { |
8d849394 | 523 | global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT; |
0e30c987 | 524 | |
98899fc0 | 525 | $stripreferthat = get_string("ipreferthat", "survey"); |
526 | $strifoundthat = get_string("ifoundthat", "survey"); | |
532f494d | 527 | $strdefault = get_string('default'); |
528 | $strresponses = get_string('responses', 'survey'); | |
529 | ||
8d849394 | 530 | echo $OUTPUT->heading($question->text, 3, 'questiontext'); |
90723839 | 531 | echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">"; |
0e30c987 | 532 | |
533 | $options = explode( ",", $question->options); | |
534 | $numoptions = count($options); | |
535 | ||
536 | $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false; | |
8a12a132 | 537 | if ($question->type == 2) { |
538 | $P = "P"; | |
539 | } else { | |
540 | $P = ""; | |
541 | } | |
0e30c987 | 542 | |
532f494d | 543 | echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>"; |
0e30c987 | 544 | while (list ($key, $val) = each ($options)) { |
532f494d | 545 | echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n"; |
546 | } | |
547 | echo "<th> </th></tr>\n"; | |
548 | ||
549 | if ($oneanswer) { | |
550 | echo "<tr><th scope=\"col\" colspan=\"6\">$question->intro</th></tr>\n"; | |
551 | } else { | |
e5dd8e3b | 552 | echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n"; |
0e30c987 | 553 | } |
0e30c987 | 554 | |
44e1b7d7 | 555 | $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi)); |
532f494d | 556 | |
0e30c987 | 557 | foreach ($subquestions as $q) { |
558 | $qnum++; | |
f1c1d2f1 | 559 | $rowclass = survey_question_rowclass($qnum); |
f762c448 | 560 | if ($q->text) { |
561 | $q->text = get_string($q->text, "survey"); | |
562 | } | |
563 | ||
532f494d | 564 | echo "<tr class=\"$rowclass rblock\">"; |
0e30c987 | 565 | if ($oneanswer) { |
3a2e0fe9 | 566 | |
532f494d | 567 | echo "<th scope=\"row\" class=\"optioncell\">"; |
568 | echo "<b class=\"qnumtopcell\">$qnum</b> "; | |
569 | echo $q->text ."</th>\n"; | |
570 | for ($i=1;$i<=$numoptions;$i++) { | |
571 | $hiddentext = get_accesshide($options[$i-1]); | |
572 | $id = "q$P" . $q->id . "_$i"; | |
573 | echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; | |
0e30c987 | 574 | } |
532f494d | 575 | $default = get_accesshide($strdefault, 'label', '', "for=\"q$P$q->id\""); |
576 | echo "<td class=\"whitecell\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</td>"; | |
0e30c987 | 577 | $checklist["q$P$q->id"] = $numoptions; |
e5dd8e3b PS |
578 | |
579 | } else { | |
f40aee6f | 580 | // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly. |
532f494d | 581 | echo "<th scope=\"row\" class=\"optioncell\">"; |
582 | echo "<b class=\"qnumtopcell\">$qnum</b> "; | |
5d1a5ef4 | 583 | $qnum++; |
532f494d | 584 | echo "<span class=\"preferthat smalltext\">$stripreferthat</span> "; |
585 | echo "<span class=\"option\">$q->text</span></th>\n"; | |
0e30c987 | 586 | for ($i=1;$i<=$numoptions;$i++) { |
532f494d | 587 | $hiddentext = get_accesshide($options[$i-1]); |
588 | $id = "qP" . $q->id . "_$i"; | |
589 | echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; | |
0e30c987 | 590 | } |
532f494d | 591 | $default = get_accesshide($strdefault, 'label', '', "for=\"qP$q->id\""); |
592 | echo "<td><input type=\"radio\" name=\"qP$q->id\" id=\"qP$q->id\" value=\"0\" checked=\"checked\" />$default</td>"; | |
d1290cec | 593 | echo "</tr>"; |
0e30c987 | 594 | |
532f494d | 595 | echo "<tr class=\"$rowclass rblock\">"; |
596 | echo "<th scope=\"row\" class=\"optioncell\">"; | |
597 | echo "<b class=\"qnumtopcell\">$qnum</b> "; | |
598 | echo "<span class=\"foundthat smalltext\">$strifoundthat</span> "; | |
599 | echo "<span class=\"option\">$q->text</span></th>\n"; | |
0e30c987 | 600 | for ($i=1;$i<=$numoptions;$i++) { |
532f494d | 601 | $hiddentext = get_accesshide($options[$i-1]); |
602 | $id = "q" . $q->id . "_$i"; | |
603 | echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; | |
0e30c987 | 604 | } |
532f494d | 605 | $default = get_accesshide($strdefault, 'label', '', "for=\"q$q->id\""); |
606 | echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" id=\"q$q->id\" value=\"0\" checked=\"checked\" />$default</td>"; | |
0e30c987 | 607 | $checklist["qP$q->id"] = $numoptions; |
e5dd8e3b | 608 | $checklist["q$q->id"] = $numoptions; |
0e30c987 | 609 | } |
d1290cec | 610 | echo "</tr>\n"; |
0e30c987 | 611 | } |
d1290cec | 612 | echo "</table>"; |
0e30c987 | 613 | } |
614 | ||
615 | ||
e5cc530b | 616 | /** |
617 | * @global object | |
618 | * @global int | |
619 | * @param object $question | |
620 | */ | |
0e30c987 | 621 | function survey_print_single($question) { |
8b1b0382 | 622 | global $DB, $qnum, $OUTPUT; |
0e30c987 | 623 | |
f1c1d2f1 | 624 | $rowclass = survey_question_rowclass(0); |
0e30c987 | 625 | |
626 | $qnum++; | |
627 | ||
caf261b0 | 628 | echo "<br />\n"; |
795b56b3 | 629 | echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n"; |
f1c1d2f1 | 630 | echo "<tr class=\"$rowclass\">"; |
532f494d | 631 | echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> "; |
632 | echo "<span class=\"questioncell\">$question->text</span></label></th>\n"; | |
633 | echo "<td class=\"questioncell smalltext\">\n"; | |
0e30c987 | 634 | |
635 | ||
636 | if ($question->type == 0) { // Plain text field | |
532f494d | 637 | echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>"; |
0e30c987 | 638 | |
639 | } else if ($question->type > 0) { // Choose one of a number | |
98899fc0 | 640 | $strchoose = get_string("choose"); |
532f494d | 641 | echo "<select name=\"q$question->id\" id=\"q$question->id\">"; |
e0b7b090 | 642 | echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>"; |
0e30c987 | 643 | $options = explode( ",", $question->options); |
644 | foreach ($options as $key => $val) { | |
645 | $key++; | |
d1290cec | 646 | echo "<option value=\"$key\">$val</option>"; |
0e30c987 | 647 | } |
d1290cec | 648 | echo "</select>"; |
0e30c987 | 649 | |
650 | } else if ($question->type < 0) { // Choose several of a number | |
651 | $options = explode( ",", $question->options); | |
8b1b0382 | 652 | echo $OUTPUT->notification("This question type not supported yet"); |
0e30c987 | 653 | } |
654 | ||
532f494d | 655 | echo "</td></tr></table>"; |
0e30c987 | 656 | |
657 | } | |
658 | ||
e5cc530b | 659 | /** |
660 | * | |
661 | * @param int $qnum | |
662 | * @return string | |
663 | */ | |
f1c1d2f1 | 664 | function survey_question_rowclass($qnum) { |
0e30c987 | 665 | |
666 | if ($qnum) { | |
f1c1d2f1 | 667 | return $qnum % 2 ? 'r0' : 'r1'; |
0e30c987 | 668 | } else { |
f1c1d2f1 | 669 | return 'r0'; |
0e30c987 | 670 | } |
671 | } | |
672 | ||
e5cc530b | 673 | /** |
674 | * @global object | |
675 | * @global int | |
676 | * @global int | |
677 | * @param string $url | |
678 | */ | |
607809b3 | 679 | function survey_print_graph($url) { |
680 | global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH; | |
681 | ||
682 | if (empty($CFG->gdversion)) { | |
683 | echo "(".get_string("gdneed").")"; | |
684 | ||
685 | } else { | |
795b56b3 | 686 | echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"". |
7150b8ae | 687 | " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />"; |
607809b3 | 688 | } |
689 | } | |
690 | ||
e5cc530b | 691 | /** |
692 | * @return array | |
693 | */ | |
f3221af9 | 694 | function survey_get_view_actions() { |
695 | return array('download','view all','view form','view graph','view report'); | |
696 | } | |
697 | ||
e5cc530b | 698 | /** |
699 | * @return array | |
700 | */ | |
f3221af9 | 701 | function survey_get_post_actions() { |
702 | return array('submit'); | |
703 | } | |
704 | ||
0b5a80a1 | 705 | |
706 | /** | |
707 | * Implementation of the function for printing the form elements that control | |
708 | * whether the course reset functionality affects the survey. | |
e5dd8e3b | 709 | * |
e5cc530b | 710 | * @param object $mform form passed by reference |
0b5a80a1 | 711 | */ |
712 | function survey_reset_course_form_definition(&$mform) { | |
713 | $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey')); | |
714 | $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey')); | |
715 | $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey')); | |
716 | $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked'); | |
717 | } | |
718 | ||
719 | /** | |
720 | * Course reset form defaults. | |
e5cc530b | 721 | * @return array |
0b5a80a1 | 722 | */ |
723 | function survey_reset_course_form_defaults($course) { | |
724 | return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1); | |
725 | } | |
726 | ||
727 | /** | |
728 | * Actual implementation of the rest coures functionality, delete all the | |
729 | * survey responses for course $data->courseid. | |
e5cc530b | 730 | * |
731 | * @global object | |
0b5a80a1 | 732 | * @param $data the data submitted from the reset course. |
733 | * @return array status array | |
734 | */ | |
735 | function survey_reset_userdata($data) { | |
deb3a60d | 736 | global $DB; |
0b5a80a1 | 737 | |
738 | $componentstr = get_string('modulenameplural', 'survey'); | |
739 | $status = array(); | |
740 | ||
741 | $surveyssql = "SELECT ch.id | |
deb3a60d | 742 | FROM {survey} ch |
743 | WHERE ch.course=?"; | |
744 | $params = array($data->courseid); | |
0b5a80a1 | 745 | |
746 | if (!empty($data->reset_survey_answers)) { | |
deb3a60d | 747 | $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params); |
748 | $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params); | |
0b5a80a1 | 749 | $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false); |
750 | } | |
751 | ||
752 | if (!empty($data->reset_survey_analysis)) { | |
deb3a60d | 753 | $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params); |
0b5a80a1 | 754 | $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false); |
755 | } | |
756 | ||
757 | // no date shifting | |
758 | return $status; | |
759 | } | |
760 | ||
f432bebf | 761 | /** |
762 | * Returns all other caps used in module | |
e5cc530b | 763 | * |
764 | * @return array | |
f432bebf | 765 | */ |
766 | function survey_get_extra_capabilities() { | |
767 | return array('moodle/site:accessallgroups'); | |
768 | } | |
769 | ||
42f103be | 770 | /** |
e5cc530b | 771 | * @uses FEATURE_GROUPS |
772 | * @uses FEATURE_GROUPINGS | |
773 | * @uses FEATURE_GROUPMEMBERSONLY | |
774 | * @uses FEATURE_MOD_INTRO | |
775 | * @uses FEATURE_COMPLETION_TRACKS_VIEWS | |
776 | * @uses FEATURE_GRADE_HAS_GRADE | |
777 | * @uses FEATURE_GRADE_OUTCOMES | |
42f103be | 778 | * @param string $feature FEATURE_xx constant for requested feature |
e5cc530b | 779 | * @return mixed True if module supports feature, false if not, null if doesn't know |
42f103be | 780 | */ |
781 | function survey_supports($feature) { | |
782 | switch($feature) { | |
783 | case FEATURE_GROUPS: return true; | |
784 | case FEATURE_GROUPINGS: return true; | |
785 | case FEATURE_GROUPMEMBERSONLY: return true; | |
dc5c2bd9 | 786 | case FEATURE_MOD_INTRO: return true; |
42f103be | 787 | case FEATURE_COMPLETION_TRACKS_VIEWS: return true; |
788 | case FEATURE_GRADE_HAS_GRADE: return true; | |
789 | case FEATURE_GRADE_OUTCOMES: return true; | |
790 | ||
791 | default: return null; | |
792 | } | |
17da2e6f | 793 | } |
a5cb6242 | 794 | |
795 | /** | |
796 | * This fucntion extends the global navigaiton for the site. | |
797 | * It is important to note that you should not rely on PAGE objects within this | |
798 | * body of code as there is no guarantee that during an AJAX request they are | |
799 | * available | |
800 | * | |
801 | * @param navigation_node $navigation The quiz node within the global navigation | |
802 | * @param stdClass $course The course object returned from the DB | |
803 | * @param stdClass $module The module object returned from the DB | |
804 | * @param stdClass $cm The course module isntance returned from the DB | |
805 | */ | |
806 | function survey_extend_navigation($navigation, $course, $module, $cm) { | |
807 | /** | |
808 | * This is currently just a stub so that it can be easily expanded upon. | |
809 | * When expanding just remove this comment and the line below and then add | |
810 | * you content. | |
811 | */ | |
812 | $navigation->nodetype = navigation_node::NODETYPE_LEAF; | |
813 | } | |
814 | ||
815 | /** | |
816 | * This function extends the settings navigation block for the site. | |
817 | * | |
818 | * It is safe to rely on PAGE here as we will only ever be within the module | |
819 | * context when this is called | |
820 | * | |
821 | * @param navigation_node $settings | |
0b29477b SH |
822 | * @param navigation_node $surveynode |
823 | */ | |
824 | function survey_extend_settings_navigation($settings, $surveynode) { | |
825 | global $PAGE; | |
a5cb6242 | 826 | |
827 | if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) { | |
3406acde | 828 | $responsesnode = $surveynode->add(get_string("responsereports", "survey")); |
a5cb6242 | 829 | |
a6855934 | 830 | $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary')); |
3406acde | 831 | $responsesnode->add(get_string("summary", "survey"), $url); |
a5cb6242 | 832 | |
a6855934 | 833 | $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales')); |
3406acde | 834 | $responsesnode->add(get_string("scales", "survey"), $url); |
a5cb6242 | 835 | |
a6855934 | 836 | $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions')); |
3406acde | 837 | $responsesnode->add(get_string("question", "survey"), $url); |
a5cb6242 | 838 | |
a6855934 | 839 | $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students')); |
3406acde | 840 | $responsesnode->add(get_string('participants'), $url); |
a5cb6242 | 841 | |
842 | if (has_capability('mod/survey:download', $PAGE->cm->context)) { | |
a6855934 | 843 | $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download')); |
0b29477b | 844 | $surveynode->add(get_string('downloadresults', 'survey'), $url); |
a5cb6242 | 845 | } |
846 | } | |
3406acde | 847 | } |