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 = groups_get_activity_groupmode($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 !groups_is_member($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
40 $users = groups_get_members($group);
41 } else if (!empty($CFG->enablegroupings
) && !empty($cm->groupingid
)) {
42 $users = groups_get_grouping_members($cm->groupingid
);
44 $users = get_course_users($course->id
);
48 $stractual = get_string("actual", "survey");
49 $stractualclass = get_string("actualclass", "survey");
50 $stractualstudent = get_string("actualstudent", "survey", $course->student
);
52 $strpreferred = get_string("preferred", "survey");
53 $strpreferredclass = get_string("preferredclass", "survey");
54 $strpreferredstudent = get_string("preferredstudent", "survey", $course->student
);
56 $virtualscales = false; //set default value for case clauses
62 $question = get_record("survey_questions", "id", $qid);
63 $question->text
= get_string($question->text
, "survey");
64 $question->options
= get_string($question->options
, "survey");
66 $options = explode(",",$question->options
);
68 while (list($key,) = each($options)) {
73 if ($aaa = get_records_select("survey_answers", "survey = '$cm->instance' AND question = '$qid'")) {
74 foreach ($aaa as $aa) {
75 if (!$group or isset($users[$aa->userid
])) {
76 if ($a1 = $aa->answer1
) {
79 if ($a2 = $aa->answer2
) {
87 $maxbuckets1 = max($buckets1);
88 $maxbuckets2 = max($buckets2);
89 $maxbuckets = ($maxbuckets1 > $maxbuckets2) ?
$maxbuckets1 : $maxbuckets2;
91 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
92 $graph->parameter
['title'] = "$question->text";
94 $graph->x_data
= $options;
96 $graph->y_data
['answers1'] = $buckets1;
97 $graph->y_format
['answers1'] = array('colour' => 'ltblue','bar' => 'fill','legend' =>$stractual,'bar_size' => 0.4);
98 $graph->y_data
['answers2'] = $buckets2;
99 $graph->y_format
['answers2'] = array('colour' =>'ltorange','bar' => 'fill','legend' =>$strpreferred,'bar_size' => 0.2);
101 $graph->parameter
['legend'] = 'outside-top';
102 $graph->parameter
['legend_border'] = 'black';
103 $graph->parameter
['legend_offset'] = 4;
105 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
106 $graph->y_order
= array('answers1', 'answers2');
107 } else if ($maxbuckets1 > 0.0) {
108 $graph->y_order
= array('answers1');
110 $graph->y_order
= array('answers2');
113 $graph->parameter
['y_axis_gridlines']= $maxbuckets +
1;
114 $graph->parameter
['y_resolution_left']= 1;
115 $graph->parameter
['y_decimal_left'] = 0;
116 $graph->parameter
['x_axis_angle'] = 20;
117 $graph->parameter
['shadow'] = 'none';
119 $graph->y_tick_labels
= null;
120 $graph->offset_relation
= null;
122 $graph->draw_stack();
128 case "multiquestion.png":
130 $question = get_record("survey_questions", "id", $qid);
131 $question->text
= get_string($question->text
, "survey");
132 $question->options
= get_string($question->options
, "survey");
134 $options = explode(",",$question->options
);
135 $questionorder = explode( ",", $question->multi
);
137 $qqq = get_records_list("survey_questions", "id", $question->multi
);
139 foreach ($questionorder as $i => $val) {
140 $names[$i] = get_string($qqq["$val"]->shorttext
, "survey");
150 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
153 foreach ($aaa as $a) {
154 if (!$group or isset($users[$a->userid
])) {
155 $index = $indexof[$a->question
];
157 $buckets1[$index] +
= $a->answer1
;
161 $buckets2[$index] +
= $a->answer2
;
168 foreach ($questionorder as $i => $val) {
170 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
173 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
178 foreach ($aaa as $a) {
179 if (!$group or isset($users[$a->userid
])) {
180 $index = $indexof[$a->question
];
182 $difference = (float) ($a->answer1
- $buckets1[$index]);
183 $stdev1[$index] +
= ($difference * $difference);
186 $difference = (float) ($a->answer2
- $buckets2[$index]);
187 $stdev2[$index] +
= ($difference * $difference);
193 foreach ($questionorder as $i => $val) {
195 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
198 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
200 $buckets1[$i] = $buckets1[$i] - 1;
201 $buckets2[$i] = $buckets2[$i] - 1;
206 $maxbuckets1 = max($buckets1);
207 $maxbuckets2 = max($buckets2);
210 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
211 $graph->parameter
['title'] = "$question->text";
213 $graph->x_data
= $names;
214 $graph->y_data
['answers1'] = $buckets1;
215 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
216 'shadow_offset' => 4, 'legend' => $stractual);
217 $graph->y_data
['answers2'] = $buckets2;
218 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
219 'shadow_offset' => 4, 'legend' => $strpreferred);
220 $graph->y_data
['stdev1'] = $stdev1;
221 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
222 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
223 $graph->y_data
['stdev2'] = $stdev2;
224 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
225 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
226 $graph->offset_relation
['stdev1'] = 'answers1';
227 $graph->offset_relation
['stdev2'] = 'answers2';
229 $graph->parameter
['bar_size'] = 0.15;
231 $graph->parameter
['legend'] = 'outside-top';
232 $graph->parameter
['legend_border'] = 'black';
233 $graph->parameter
['legend_offset'] = 4;
235 $graph->y_tick_labels
= $options;
237 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
238 $graph->y_order
= array('stdev1', 'answers1', 'stdev2', 'answers2');
239 } else if ($maxbuckets1 > 0.0) {
240 $graph->y_order
= array('stdev1', 'answers1');
242 $graph->y_order
= array('stdev2', 'answers2');
245 $graph->parameter
['y_max_left']= count($options) - 1;
246 $graph->parameter
['y_axis_gridlines']= count($options);
247 $graph->parameter
['y_resolution_left']= 1;
248 $graph->parameter
['y_decimal_left']= 1;
249 $graph->parameter
['x_axis_angle'] = 20;
259 $qqq = get_records_list("survey_questions", "id", $survey->questions
);
262 foreach ($qqq as $key => $qq) {
264 $qqq[$key]->text
= get_string($qq->text
, "survey");
265 $qqq[$key]->options
= get_string($qq->options
, "survey");
267 $virtualscales = true;
271 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
273 if ($virtualscales && $qq->type
< 0) {
275 } else if (!$virtualscales && $qq->type
> 0) {
280 $numquestions = count($question);
282 $options = explode(",",$question[0]->options
);
283 $numoptions = count($options);
285 for ($i=0; $i<$numquestions; $i++
) {
286 $names[$i] = $question[$i]->text
;
293 $subquestions = $question[$i]->multi
; // otherwise next line doesn't work
294 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($subquestions)))");
297 foreach ($aaa as $a) {
298 if (!$group or isset($users[$a->userid
])) {
300 $buckets1[$i] +
= $a->answer1
;
304 $buckets2[$i] +
= $a->answer2
;
312 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
315 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
318 // Calculate the standard devaiations
320 foreach ($aaa as $a) {
321 if (!$group or isset($users[$a->userid
])) {
323 $difference = (float) ($a->answer1
- $buckets1[$i]);
324 $stdev1[$i] +
= ($difference * $difference);
327 $difference = (float) ($a->answer2
- $buckets2[$i]);
328 $stdev2[$i] +
= ($difference * $difference);
335 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
338 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
341 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
342 $buckets2[$i] = $buckets2[$i] - 1;
346 $maxbuckets1 = max($buckets1);
347 $maxbuckets2 = max($buckets2);
350 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
351 $graph->parameter
['title'] = strip_tags(format_string($survey->name
,true));
353 $graph->x_data
= $names;
355 $graph->y_data
['answers1'] = $buckets1;
356 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
357 'shadow_offset' => 4, 'legend' => $stractual);
358 $graph->y_data
['answers2'] = $buckets2;
359 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
360 'shadow_offset' => 4, 'legend' => $strpreferred);
362 $graph->y_data
['stdev1'] = $stdev1;
363 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
364 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
365 $graph->y_data
['stdev2'] = $stdev2;
366 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
367 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
368 $graph->offset_relation
['stdev1'] = 'answers1';
369 $graph->offset_relation
['stdev2'] = 'answers2';
371 $graph->parameter
['legend'] = 'outside-top';
372 $graph->parameter
['legend_border'] = 'black';
373 $graph->parameter
['legend_offset'] = 4;
375 $graph->y_tick_labels
= $options;
377 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
378 $graph->y_order
= array('stdev1', 'answers1', 'stdev2', 'answers2');
379 } else if ($maxbuckets1 > 0.0) {
380 $graph->y_order
= array('stdev1', 'answers1');
382 $graph->y_order
= array('stdev2', 'answers2');
385 $graph->parameter
['y_max_left']= $numoptions - 1;
386 $graph->parameter
['y_axis_gridlines']= $numoptions;
387 $graph->parameter
['y_resolution_left']= 1;
388 $graph->parameter
['y_decimal_left']= 1;
389 $graph->parameter
['x_axis_angle'] = 0;
390 $graph->parameter
['x_inner_padding'] = 6;
400 $qqq = get_records_list("survey_questions", "id", $survey->questions
);
402 foreach ($qqq as $key => $qq) {
404 $qqq[$key]->text
= get_string($qq->text
, "survey");
405 $qqq[$key]->options
= get_string($qq->options
, "survey");
407 $virtualscales = true;
411 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
413 if ($virtualscales && $qq->type
< 0) {
415 } else if (!$virtualscales && $qq->type
> 0) {
420 $numquestions= count($question);
422 $options = explode(",",$question[0]->options
);
423 $numoptions = count($options);
425 for ($i=0; $i<$numquestions; $i++
) {
426 $names[$i] = $question[$i]->text
;
431 $studbuckets1[$i] = 0.0;
432 $studbuckets2[$i] = 0.0;
438 $subquestions = $question[$i]->multi
; // otherwise next line doesn't work
439 $aaa = get_records_select("survey_answers","((survey = $cm->instance) AND (question in ($subquestions)))");
442 foreach ($aaa as $a) {
443 if (!$group or isset($users[$a->userid
])) {
444 if ($a->userid
== $sid) {
446 $studbuckets1[$i] +
= $a->answer1
;
450 $studbuckets2[$i] +
= $a->answer2
;
455 $buckets1[$i] +
= $a->answer1
;
459 $buckets2[$i] +
= $a->answer2
;
467 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
470 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
472 if ($studcount1[$i]) {
473 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
475 if ($studcount2[$i]) {
476 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
479 // Calculate the standard devaiations
480 foreach ($aaa as $a) {
481 if (!$group or isset($users[$a->userid
])) {
483 $difference = (float) ($a->answer1
- $buckets1[$i]);
484 $stdev1[$i] +
= ($difference * $difference);
487 $difference = (float) ($a->answer2
- $buckets2[$i]);
488 $stdev2[$i] +
= ($difference * $difference);
494 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
497 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
500 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
501 $buckets2[$i] = $buckets2[$i] - 1;
502 $studbuckets1[$i] = $studbuckets1[$i] - 1;
503 $studbuckets2[$i] = $studbuckets2[$i] - 1;
507 $maxbuckets1 = max($buckets1);
508 $maxbuckets2 = max($buckets2);
511 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
512 $graph->parameter
['title'] = strip_tags(format_string($survey->name
,true));
514 $graph->x_data
= $names;
516 $graph->y_data
['answers1'] = $buckets1;
517 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
518 'shadow_offset' => 0.1, 'legend' => $stractualclass);
519 $graph->y_data
['answers2'] = $buckets2;
520 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
521 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
522 $graph->y_data
['studanswers1'] = $studbuckets1;
523 $graph->y_format
['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
524 'shadow_offset' => 4, 'legend' => $stractualstudent);
525 $graph->y_data
['studanswers2'] = $studbuckets2;
526 $graph->y_format
['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
527 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
528 $graph->y_data
['stdev1'] = $stdev1;
529 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
530 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
531 $graph->y_data
['stdev2'] = $stdev2;
532 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
533 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
534 $graph->offset_relation
['stdev1'] = 'answers1';
535 $graph->offset_relation
['stdev2'] = 'answers2';
537 $graph->y_tick_labels
= $options;
539 $graph->parameter
['bar_size'] = 0.15;
541 $graph->parameter
['legend'] = 'outside-top';
542 $graph->parameter
['legend_border'] = 'black';
543 $graph->parameter
['legend_offset'] = 4;
545 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
546 $graph->y_order
= array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
547 } else if ($maxbuckets1 > 0.0) {
548 $graph->y_order
= array('stdev1', 'answers1', 'studanswers1');
550 $graph->y_order
= array('stdev2', 'answers2', 'studanswers2');
553 $graph->parameter
['y_max_left']= $numoptions - 1;
554 $graph->parameter
['y_axis_gridlines']= $numoptions;
555 $graph->parameter
['y_resolution_left']= 1;
556 $graph->parameter
['y_decimal_left']= 1;
557 $graph->parameter
['x_axis_angle'] = 20;
564 case "studentmultiquestion.png":
566 $question = get_record("survey_questions", "id", $qid);
567 $question->text
= get_string($question->text
, "survey");
568 $question->options
= get_string($question->options
, "survey");
570 $options = explode(",",$question->options
);
571 $questionorder = explode( ",", $question->multi
);
573 $qqq = get_records_list("survey_questions", "id", $question->multi
);
575 foreach ($questionorder as $i => $val) {
576 $names[$i] = get_string($qqq[$val]->shorttext
, "survey");
582 $studbuckets1[$i] = 0.0;
583 $studbuckets2[$i] = 0.0;
590 $aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
593 foreach ($aaa as $a) {
594 if (!$group or isset($users[$a->userid
])) {
595 $index = $indexof[$a->question
];
596 if ($a->userid
== $sid) {
598 $studbuckets1[$index] +
= $a->answer1
;
599 $studcount1[$index]++
;
602 $studbuckets2[$index] +
= $a->answer2
;
603 $studcount2[$index]++
;
607 $buckets1[$index] +
= $a->answer1
;
611 $buckets2[$index] +
= $a->answer2
;
618 foreach ($questionorder as $i => $val) {
620 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
623 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
625 if ($studcount1[$i]) {
626 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
628 if ($studcount2[$i]) {
629 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
633 foreach ($aaa as $a) {
634 if (!$group or isset($users[$a->userid
])) {
635 $index = $indexof[$a->question
];
637 $difference = (float) ($a->answer1
- $buckets1[$index]);
638 $stdev1[$index] +
= ($difference * $difference);
641 $difference = (float) ($a->answer2
- $buckets2[$index]);
642 $stdev2[$index] +
= ($difference * $difference);
647 foreach ($questionorder as $i => $val) {
649 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
652 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
654 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
655 $buckets2[$i] = $buckets2[$i] - 1;
656 $studbuckets1[$i] = $studbuckets1[$i] - 1;
657 $studbuckets2[$i] = $studbuckets2[$i] - 1;
662 $maxbuckets1 = max($buckets1);
663 $maxbuckets2 = max($buckets2);
666 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
667 $graph->parameter
['title'] = "$question->text";
669 $graph->x_data
= $names;
670 $graph->y_data
['answers1'] = $buckets1;
671 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
672 'shadow_offset' => 0.1, 'legend' => $stractualclass);
673 $graph->y_data
['answers2'] = $buckets2;
674 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
675 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
676 $graph->y_data
['studanswers1'] = $studbuckets1;
677 $graph->y_format
['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
678 'shadow_offset' => 4, 'legend' => $stractualstudent);
679 $graph->y_data
['studanswers2'] = $studbuckets2;
680 $graph->y_format
['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
681 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
682 $graph->y_data
['stdev1'] = $stdev1;
683 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
684 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
685 $graph->y_data
['stdev2'] = $stdev2;
686 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
687 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
688 $graph->offset_relation
['stdev1'] = 'answers1';
689 $graph->offset_relation
['stdev2'] = 'answers2';
691 $graph->parameter
['bar_size'] = 0.15;
693 $graph->parameter
['legend'] = 'outside-top';
694 $graph->parameter
['legend_border'] = 'black';
695 $graph->parameter
['legend_offset'] = 4;
697 $graph->y_tick_labels
= $options;
699 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
700 $graph->y_order
= array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
701 } else if ($maxbuckets1 > 0.0) {
702 $graph->y_order
= array('stdev1', 'answers1', 'studanswers1');
704 $graph->y_order
= array('stdev2', 'answers2', 'studanswers2');
707 $graph->parameter
['y_max_left']= count($options)-1;
708 $graph->parameter
['y_axis_gridlines']= count($options);
709 $graph->parameter
['y_resolution_left']= 1;
710 $graph->parameter
['y_decimal_left']= 1;
711 $graph->parameter
['x_axis_angle'] = 20;