3 require_once("../../config.php");
4 require_once("$CFG->libdir/graphlib.php");
5 require_once("lib.php");
7 $id = required_param('id', PARAM_INT
); // Course Module ID
8 $type = required_param('type', PARAM_FILE
); // Graph Type
9 $group = optional_param('group', 0, PARAM_INT
); // Group ID
10 $sid = optional_param('sid', false, PARAM_INT
); // Student ID
11 $qid = optional_param('qid', 0, PARAM_INT
); // Group ID
13 if (! $cm = get_coursemodule_from_id('survey', $id)) {
14 error("Course Module ID was incorrect");
17 if (! $course = get_record("course", "id", $cm->course
)) {
18 error("Course is misconfigured");
21 require_login($course->id
, false, $cm);
23 $groupmode = groupmode($course, $cm); // Groups are being used
24 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
26 if (!has_capability('mod/survey:readresponses', $context)) {
27 if ($type != "student.png" or $sid != $USER->id
) {
28 error("Sorry, you aren't allowed to see this.");
29 } else if ($groupmode and !ismember($group)) {
30 error("Sorry, you aren't allowed to see this.");
34 if (! $survey = get_record("survey", "id", $cm->instance
)) {
35 error("Survey ID was incorrect");
38 /// Check to see if groups are being used in this survey
39 if ($groupmode and $group) {
40 $users = get_group_users($group);
42 $users = get_course_users($course->id
);
46 $stractual = get_string("actual", "survey");
47 $stractualclass = get_string("actualclass", "survey");
48 $stractualstudent = get_string("actualstudent", "survey", $course->student
);
50 $strpreferred = get_string("preferred", "survey");
51 $strpreferredclass = get_string("preferredclass", "survey");
52 $strpreferredstudent = get_string("preferredstudent", "survey", $course->student
);
54 $virtualscales = false; //set default value for case clauses
60 $question = get_record("survey_questions", "id", $qid);
61 $question->text
= get_string($question->text
, "survey");
62 $question->options
= get_string($question->options
, "survey");
64 $options = explode(",",$question->options
);
66 while (list($key,) = each($options)) {
71 if ($aaa = get_records_select("survey_answers", "survey = '$cm->instance' AND question = '$qid'")) {
72 foreach ($aaa as $aa) {
73 if (!$group or isset($users[$aa->userid
])) {
74 if ($a1 = $aa->answer1
) {
77 if ($a2 = $aa->answer2
) {
85 $maxbuckets1 = max($buckets1);
86 $maxbuckets2 = max($buckets2);
87 $maxbuckets = ($maxbuckets1 > $maxbuckets2) ?
$maxbuckets1 : $maxbuckets2;
89 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
90 $graph->parameter
['title'] = "$question->text";
92 $graph->x_data
= $options;
94 $graph->y_data
['answers1'] = $buckets1;
95 $graph->y_format
['answers1'] = array('colour' => 'ltblue','bar' => 'fill','legend' =>$stractual,'bar_size' => 0.4);
96 $graph->y_data
['answers2'] = $buckets2;
97 $graph->y_format
['answers2'] = array('colour' =>'ltorange','bar' => 'fill','legend' =>$strpreferred,'bar_size' => 0.2);
99 $graph->parameter
['legend'] = 'outside-top';
100 $graph->parameter
['legend_border'] = 'black';
101 $graph->parameter
['legend_offset'] = 4;
103 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
104 $graph->y_order
= array('answers1', 'answers2');
105 } else if ($maxbuckets1 > 0.0) {
106 $graph->y_order
= array('answers1');
108 $graph->y_order
= array('answers2');
111 $graph->parameter
['y_axis_gridlines']= $maxbuckets +
1;
112 $graph->parameter
['y_resolution_left']= 1;
113 $graph->parameter
['y_decimal_left'] = 0;
114 $graph->parameter
['x_axis_angle'] = 20;
115 $graph->parameter
['shadow'] = 'none';
117 $graph->y_tick_labels
= null;
118 $graph->offset_relation
= null;
120 $graph->draw_stack();
126 case "multiquestion.png":
128 $question = get_record("survey_questions", "id", $qid);
129 $question->text
= get_string($question->text
, "survey");
130 $question->options
= get_string($question->options
, "survey");
132 $options = explode(",",$question->options
);
133 $questionorder = explode( ",", $question->multi
);
135 $qqq = get_records_list("survey_questions", "id", $question->multi
);
137 foreach ($questionorder as $i => $val) {
138 $names[$i] = get_string($qqq["$val"]->shorttext
, "survey");
148 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
151 foreach ($aaa as $a) {
152 if (!$group or isset($users[$a->userid
])) {
153 $index = $indexof[$a->question
];
155 $buckets1[$index] +
= $a->answer1
;
159 $buckets2[$index] +
= $a->answer2
;
166 foreach ($questionorder as $i => $val) {
168 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
171 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
176 foreach ($aaa as $a) {
177 if (!$group or isset($users[$a->userid
])) {
178 $index = $indexof[$a->question
];
180 $difference = (float) ($a->answer1
- $buckets1[$index]);
181 $stdev1[$index] +
= ($difference * $difference);
184 $difference = (float) ($a->answer2
- $buckets2[$index]);
185 $stdev2[$index] +
= ($difference * $difference);
191 foreach ($questionorder as $i => $val) {
193 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
196 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
198 $buckets1[$i] = $buckets1[$i] - 1;
199 $buckets2[$i] = $buckets2[$i] - 1;
204 $maxbuckets1 = max($buckets1);
205 $maxbuckets2 = max($buckets2);
208 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
209 $graph->parameter
['title'] = "$question->text";
211 $graph->x_data
= $names;
212 $graph->y_data
['answers1'] = $buckets1;
213 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
214 'shadow_offset' => 4, 'legend' => $stractual);
215 $graph->y_data
['answers2'] = $buckets2;
216 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
217 'shadow_offset' => 4, 'legend' => $strpreferred);
218 $graph->y_data
['stdev1'] = $stdev1;
219 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
220 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
221 $graph->y_data
['stdev2'] = $stdev2;
222 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
223 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
224 $graph->offset_relation
['stdev1'] = 'answers1';
225 $graph->offset_relation
['stdev2'] = 'answers2';
227 $graph->parameter
['bar_size'] = 0.15;
229 $graph->parameter
['legend'] = 'outside-top';
230 $graph->parameter
['legend_border'] = 'black';
231 $graph->parameter
['legend_offset'] = 4;
233 $graph->y_tick_labels
= $options;
235 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
236 $graph->y_order
= array('stdev1', 'answers1', 'stdev2', 'answers2');
237 } else if ($maxbuckets1 > 0.0) {
238 $graph->y_order
= array('stdev1', 'answers1');
240 $graph->y_order
= array('stdev2', 'answers2');
243 $graph->parameter
['y_max_left']= count($options) - 1;
244 $graph->parameter
['y_axis_gridlines']= count($options);
245 $graph->parameter
['y_resolution_left']= 1;
246 $graph->parameter
['y_decimal_left']= 1;
247 $graph->parameter
['x_axis_angle'] = 20;
257 $qqq = get_records_list("survey_questions", "id", $survey->questions
);
260 foreach ($qqq as $key => $qq) {
262 $qqq[$key]->text
= get_string($qq->text
, "survey");
263 $qqq[$key]->options
= get_string($qq->options
, "survey");
265 $virtualscales = true;
269 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
271 if ($virtualscales && $qq->type
< 0) {
273 } else if (!$virtualscales && $qq->type
> 0) {
278 $numquestions = count($question);
280 $options = explode(",",$question[0]->options
);
281 $numoptions = count($options);
283 for ($i=0; $i<$numquestions; $i++
) {
284 $names[$i] = $question[$i]->text
;
291 $subquestions = $question[$i]->multi
; // otherwise next line doesn't work
292 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($subquestions)))");
295 foreach ($aaa as $a) {
296 if (!$group or isset($users[$a->userid
])) {
298 $buckets1[$i] +
= $a->answer1
;
302 $buckets2[$i] +
= $a->answer2
;
310 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
313 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
316 // Calculate the standard devaiations
318 foreach ($aaa as $a) {
319 if (!$group or isset($users[$a->userid
])) {
321 $difference = (float) ($a->answer1
- $buckets1[$i]);
322 $stdev1[$i] +
= ($difference * $difference);
325 $difference = (float) ($a->answer2
- $buckets2[$i]);
326 $stdev2[$i] +
= ($difference * $difference);
333 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
336 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
339 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
340 $buckets2[$i] = $buckets2[$i] - 1;
344 $maxbuckets1 = max($buckets1);
345 $maxbuckets2 = max($buckets2);
348 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
349 $graph->parameter
['title'] = strip_tags(format_string($survey->name
,true));
351 $graph->x_data
= $names;
353 $graph->y_data
['answers1'] = $buckets1;
354 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
355 'shadow_offset' => 4, 'legend' => $stractual);
356 $graph->y_data
['answers2'] = $buckets2;
357 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
358 'shadow_offset' => 4, 'legend' => $strpreferred);
360 $graph->y_data
['stdev1'] = $stdev1;
361 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
362 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
363 $graph->y_data
['stdev2'] = $stdev2;
364 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
365 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
366 $graph->offset_relation
['stdev1'] = 'answers1';
367 $graph->offset_relation
['stdev2'] = 'answers2';
369 $graph->parameter
['legend'] = 'outside-top';
370 $graph->parameter
['legend_border'] = 'black';
371 $graph->parameter
['legend_offset'] = 4;
373 $graph->y_tick_labels
= $options;
375 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
376 $graph->y_order
= array('stdev1', 'answers1', 'stdev2', 'answers2');
377 } else if ($maxbuckets1 > 0.0) {
378 $graph->y_order
= array('stdev1', 'answers1');
380 $graph->y_order
= array('stdev2', 'answers2');
383 $graph->parameter
['y_max_left']= $numoptions - 1;
384 $graph->parameter
['y_axis_gridlines']= $numoptions;
385 $graph->parameter
['y_resolution_left']= 1;
386 $graph->parameter
['y_decimal_left']= 1;
387 $graph->parameter
['x_axis_angle'] = 0;
388 $graph->parameter
['x_inner_padding'] = 6;
398 $qqq = get_records_list("survey_questions", "id", $survey->questions
);
400 foreach ($qqq as $key => $qq) {
402 $qqq[$key]->text
= get_string($qq->text
, "survey");
403 $qqq[$key]->options
= get_string($qq->options
, "survey");
405 $virtualscales = true;
409 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
411 if ($virtualscales && $qq->type
< 0) {
413 } else if (!$virtualscales && $qq->type
> 0) {
418 $numquestions= count($question);
420 $options = explode(",",$question[0]->options
);
421 $numoptions = count($options);
423 for ($i=0; $i<$numquestions; $i++
) {
424 $names[$i] = $question[$i]->text
;
429 $studbuckets1[$i] = 0.0;
430 $studbuckets2[$i] = 0.0;
436 $subquestions = $question[$i]->multi
; // otherwise next line doesn't work
437 $aaa = get_records_select("survey_answers","((survey = $cm->instance) AND (question in ($subquestions)))");
440 foreach ($aaa as $a) {
441 if (!$group or isset($users[$a->userid
])) {
442 if ($a->userid
== $sid) {
444 $studbuckets1[$i] +
= $a->answer1
;
448 $studbuckets2[$i] +
= $a->answer2
;
453 $buckets1[$i] +
= $a->answer1
;
457 $buckets2[$i] +
= $a->answer2
;
465 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
468 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
470 if ($studcount1[$i]) {
471 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
473 if ($studcount2[$i]) {
474 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
477 // Calculate the standard devaiations
478 foreach ($aaa as $a) {
479 if (!$group or isset($users[$a->userid
])) {
481 $difference = (float) ($a->answer1
- $buckets1[$i]);
482 $stdev1[$i] +
= ($difference * $difference);
485 $difference = (float) ($a->answer2
- $buckets2[$i]);
486 $stdev2[$i] +
= ($difference * $difference);
492 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
495 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
498 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
499 $buckets2[$i] = $buckets2[$i] - 1;
500 $studbuckets1[$i] = $studbuckets1[$i] - 1;
501 $studbuckets2[$i] = $studbuckets2[$i] - 1;
505 $maxbuckets1 = max($buckets1);
506 $maxbuckets2 = max($buckets2);
509 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
510 $graph->parameter
['title'] = strip_tags(format_string($survey->name
,true));
512 $graph->x_data
= $names;
514 $graph->y_data
['answers1'] = $buckets1;
515 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
516 'shadow_offset' => 0.1, 'legend' => $stractualclass);
517 $graph->y_data
['answers2'] = $buckets2;
518 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
519 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
520 $graph->y_data
['studanswers1'] = $studbuckets1;
521 $graph->y_format
['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
522 'shadow_offset' => 4, 'legend' => $stractualstudent);
523 $graph->y_data
['studanswers2'] = $studbuckets2;
524 $graph->y_format
['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
525 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
526 $graph->y_data
['stdev1'] = $stdev1;
527 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
528 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
529 $graph->y_data
['stdev2'] = $stdev2;
530 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
531 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
532 $graph->offset_relation
['stdev1'] = 'answers1';
533 $graph->offset_relation
['stdev2'] = 'answers2';
535 $graph->y_tick_labels
= $options;
537 $graph->parameter
['bar_size'] = 0.15;
539 $graph->parameter
['legend'] = 'outside-top';
540 $graph->parameter
['legend_border'] = 'black';
541 $graph->parameter
['legend_offset'] = 4;
543 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
544 $graph->y_order
= array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
545 } else if ($maxbuckets1 > 0.0) {
546 $graph->y_order
= array('stdev1', 'answers1', 'studanswers1');
548 $graph->y_order
= array('stdev2', 'answers2', 'studanswers2');
551 $graph->parameter
['y_max_left']= $numoptions - 1;
552 $graph->parameter
['y_axis_gridlines']= $numoptions;
553 $graph->parameter
['y_resolution_left']= 1;
554 $graph->parameter
['y_decimal_left']= 1;
555 $graph->parameter
['x_axis_angle'] = 20;
562 case "studentmultiquestion.png":
564 $question = get_record("survey_questions", "id", $qid);
565 $question->text
= get_string($question->text
, "survey");
566 $question->options
= get_string($question->options
, "survey");
568 $options = explode(",",$question->options
);
569 $questionorder = explode( ",", $question->multi
);
571 $qqq = get_records_list("survey_questions", "id", $question->multi
);
573 foreach ($questionorder as $i => $val) {
574 $names[$i] = get_string($qqq[$val]->shorttext
, "survey");
580 $studbuckets1[$i] = 0.0;
581 $studbuckets2[$i] = 0.0;
588 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
591 foreach ($aaa as $a) {
592 if (!$group or isset($users[$a->userid
])) {
593 $index = $indexof[$a->question
];
594 if ($a->userid
== $sid) {
596 $studbuckets1[$index] +
= $a->answer1
;
597 $studcount1[$index]++
;
600 $studbuckets2[$index] +
= $a->answer2
;
601 $studcount2[$index]++
;
605 $buckets1[$index] +
= $a->answer1
;
609 $buckets2[$index] +
= $a->answer2
;
616 foreach ($questionorder as $i => $val) {
618 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
621 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
623 if ($studcount1[$i]) {
624 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
626 if ($studcount2[$i]) {
627 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
631 foreach ($aaa as $a) {
632 if (!$group or isset($users[$a->userid
])) {
633 $index = $indexof[$a->question
];
635 $difference = (float) ($a->answer1
- $buckets1[$index]);
636 $stdev1[$index] +
= ($difference * $difference);
639 $difference = (float) ($a->answer2
- $buckets2[$index]);
640 $stdev2[$index] +
= ($difference * $difference);
645 foreach ($questionorder as $i => $val) {
647 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
650 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
652 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
653 $buckets2[$i] = $buckets2[$i] - 1;
654 $studbuckets1[$i] = $studbuckets1[$i] - 1;
655 $studbuckets2[$i] = $studbuckets2[$i] - 1;
660 $maxbuckets1 = max($buckets1);
661 $maxbuckets2 = max($buckets2);
664 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
665 $graph->parameter
['title'] = "$question->text";
667 $graph->x_data
= $names;
668 $graph->y_data
['answers1'] = $buckets1;
669 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
670 'shadow_offset' => 0.1, 'legend' => $stractualclass);
671 $graph->y_data
['answers2'] = $buckets2;
672 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
673 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
674 $graph->y_data
['studanswers1'] = $studbuckets1;
675 $graph->y_format
['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
676 'shadow_offset' => 4, 'legend' => $stractualstudent);
677 $graph->y_data
['studanswers2'] = $studbuckets2;
678 $graph->y_format
['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
679 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
680 $graph->y_data
['stdev1'] = $stdev1;
681 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
682 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
683 $graph->y_data
['stdev2'] = $stdev2;
684 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
685 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
686 $graph->offset_relation
['stdev1'] = 'answers1';
687 $graph->offset_relation
['stdev2'] = 'answers2';
689 $graph->parameter
['bar_size'] = 0.15;
691 $graph->parameter
['legend'] = 'outside-top';
692 $graph->parameter
['legend_border'] = 'black';
693 $graph->parameter
['legend_offset'] = 4;
695 $graph->y_tick_labels
= $options;
697 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
698 $graph->y_order
= array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
699 } else if ($maxbuckets1 > 0.0) {
700 $graph->y_order
= array('stdev1', 'answers1', 'studanswers1');
702 $graph->y_order
= array('stdev2', 'answers2', 'studanswers2');
705 $graph->parameter
['y_max_left']= count($options)-1;
706 $graph->parameter
['y_axis_gridlines']= count($options);
707 $graph->parameter
['y_resolution_left']= 1;
708 $graph->parameter
['y_decimal_left']= 1;
709 $graph->parameter
['x_axis_angle'] = 20;