Merge branch 'MDL-78543' of https://github.com/skodak/moodle
[moodle.git] / mod / survey / graph.php
blobdf7378af7836983903ad97f021e756c0a9bfc8a1
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for producing the graph for survey reports
21 * @package mod_survey
22 * @copyright 2021 Sujith Haridasan <sujith@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once("$CFG->libdir/graphlib.php");
28 require_once("lib.php");
30 $id = required_param('id', PARAM_INT); // Course Module ID.
31 $type = required_param('type', PARAM_FILE); // Graph Type.
32 $group = optional_param('group', 0, PARAM_INT); // Group ID.
33 $sid = optional_param('sid', false, PARAM_INT); // Student ID.
34 $qid = optional_param('qid', 0, PARAM_INT); // Group ID.
36 $url = new moodle_url('/mod/survey/graph.php', array('id' => $id, 'type' => $type));
37 if ($group !== 0) {
38 $url->param('group', $group);
40 if ($sid !== false) {
41 $url->param('sid', $sid);
43 if ($qid !== 0) {
44 $url->param('qid', $qid);
46 $PAGE->set_url($url);
48 if (!$cm = get_coursemodule_from_id('survey', $id)) {
49 throw new \moodle_exception('invalidcoursemodule');
52 if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
53 throw new \moodle_exception('coursemisconf');
56 if ($sid) {
57 if (!$user = $DB->get_record("user", array("id" => $sid))) {
58 throw new \moodle_exception('invaliduserid');
62 require_login($course, false, $cm);
64 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used.
65 $context = context_module::instance($cm->id);
67 if (!has_capability('mod/survey:readresponses', $context)) {
68 if ($type != "student.png" or $sid != $USER->id) {
69 throw new \moodle_exception('nopermissiontoshow');
70 } else if ($groupmode and !groups_is_member($group)) {
71 throw new \moodle_exception('nopermissiontoshow');
75 if (!$survey = $DB->get_record("survey", array("id" => $cm->instance))) {
76 throw new \moodle_exception('invalidsurveyid', 'survey');
79 // Check to see if groups are being used in this survey.
80 if ($group) {
81 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false);
82 } else if (!empty($cm->groupingid)) {
83 $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
84 $groups = array_keys($groups);
85 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
86 } else {
87 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
88 $group = false;
91 $stractual = get_string("actual", "survey");
92 $stractualclass = get_string("actualclass", "survey");
94 $strpreferred = get_string("preferred", "survey");
95 $strpreferredclass = get_string("preferredclass", "survey");
97 if ($sid || isset($user)) {
98 $stractualstudent = get_string("actualstudent", "survey", fullname($user));
99 $strpreferredstudent = get_string("preferredstudent", "survey", fullname($user));
102 $virtualscales = false; // Set default value for case clauses.
104 switch ($type) {
106 case "question.png":
108 $question = $DB->get_record("survey_questions", array("id" => $qid));
109 $question->text = wordwrap(get_string($question->text, "survey"), SURVEY_QLENGTH_WRAP);
110 $question->options = get_string($question->options, "survey");
112 $options = explode(",", $question->options);
114 foreach ($options as $key => $unused) {
115 $buckets1[$key] = 0;
116 $buckets2[$key] = 0;
119 if ($aaa = $DB->get_records('survey_answers', array('survey' => $cm->instance, 'question' => $qid))) {
120 foreach ($aaa as $aa) {
121 if (!$group or isset($users[$aa->userid])) {
122 if ($a1 = $aa->answer1) {
123 $buckets1[$a1 - 1]++;
125 if ($a2 = $aa->answer2) {
126 $buckets2[$a2 - 1]++;
132 $maxbuckets1 = max($buckets1);
133 $maxbuckets2 = max($buckets2);
134 $maxbuckets = ($maxbuckets1 > $maxbuckets2) ? $maxbuckets1 : $maxbuckets2;
136 $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
137 $graph->parameter['title'] = "$question->text";
139 $graph->x_data = $options;
141 $graph->y_data['answers1'] = $buckets1;
142 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'bar' => 'fill', 'legend' => $stractual, 'bar_size' => 0.4);
143 $graph->y_data['answers2'] = $buckets2;
144 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'bar' => 'fill', 'legend' => $strpreferred, 'bar_size' => 0.2);
146 $graph->parameter['legend'] = 'outside-top';
147 $graph->parameter['legend_border'] = 'black';
148 $graph->parameter['legend_offset'] = 4;
150 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
151 $graph->y_order = array('answers1', 'answers2');
152 } else if ($maxbuckets1 > 0.0) {
153 $graph->y_order = array('answers1');
154 } else {
155 $graph->y_order = array('answers2');
158 $graph->parameter['y_axis_gridlines'] = $maxbuckets + 1;
159 $graph->parameter['y_resolution_left'] = 1;
160 $graph->parameter['y_decimal_left'] = 0;
161 $graph->parameter['x_axis_angle'] = 20;
162 $graph->parameter['shadow'] = 'none';
164 $graph->y_tick_labels = null;
165 $graph->offset_relation = null;
167 $graph->draw_stack();
169 break;
171 case "multiquestion.png":
173 $question = $DB->get_record("survey_questions", array("id" => $qid));
174 $question->text = get_string($question->text, "survey");
175 $question->options = get_string($question->options, "survey");
177 $options = explode(",", $question->options);
178 $questionorder = explode(",", $question->multi);
180 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
182 foreach ($questionorder as $i => $val) {
183 $names[$i] = get_string($qqq["$val"]->shorttext, "survey");
184 $buckets1[$i] = 0;
185 $buckets2[$i] = 0;
186 $count1[$i] = 0;
187 $count2[$i] = 0;
188 $indexof[$val] = $i;
189 $stdev1[$i] = 0;
190 $stdev2[$i] = 0;
193 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))",
194 array($cm->instance));
196 if ($aaa) {
197 foreach ($aaa as $a) {
198 if (!$group or isset($users[$a->userid])) {
199 $index = $indexof[$a->question];
200 if ($a->answer1) {
201 $buckets1[$index] += $a->answer1;
202 $count1[$index]++;
204 if ($a->answer2) {
205 $buckets2[$index] += $a->answer2;
206 $count2[$index]++;
212 foreach ($questionorder as $i => $val) {
213 if ($count1[$i]) {
214 $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
216 if ($count2[$i]) {
217 $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
221 if ($aaa) {
222 foreach ($aaa as $a) {
223 if (!$group or isset($users[$a->userid])) {
224 $index = $indexof[$a->question];
225 if ($a->answer1) {
226 $difference = (float) ($a->answer1 - $buckets1[$index]);
227 $stdev1[$index] += ($difference * $difference);
229 if ($a->answer2) {
230 $difference = (float) ($a->answer2 - $buckets2[$index]);
231 $stdev2[$index] += ($difference * $difference);
237 foreach ($questionorder as $i => $val) {
238 if ($count1[$i]) {
239 $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
241 if ($count2[$i]) {
242 $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
244 $buckets1[$i] = $buckets1[$i] - 1;
245 $buckets2[$i] = $buckets2[$i] - 1;
248 $maxbuckets1 = max($buckets1);
249 $maxbuckets2 = max($buckets2);
251 $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
252 $graph->parameter['title'] = "$question->text";
254 $graph->x_data = $names;
255 $graph->y_data['answers1'] = $buckets1;
256 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
257 'shadow_offset' => 4, 'legend' => $stractual);
258 $graph->y_data['answers2'] = $buckets2;
259 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
260 'shadow_offset' => 4, 'legend' => $strpreferred);
261 $graph->y_data['stdev1'] = $stdev1;
262 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
263 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
264 $graph->y_data['stdev2'] = $stdev2;
265 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
266 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
267 $graph->offset_relation['stdev1'] = 'answers1';
268 $graph->offset_relation['stdev2'] = 'answers2';
270 $graph->parameter['bar_size'] = 0.15;
272 $graph->parameter['legend'] = 'outside-top';
273 $graph->parameter['legend_border'] = 'black';
274 $graph->parameter['legend_offset'] = 4;
276 $graph->y_tick_labels = $options;
278 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
279 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
280 } else if ($maxbuckets1 > 0.0) {
281 $graph->y_order = array('stdev1', 'answers1');
282 } else {
283 $graph->y_order = array('stdev2', 'answers2');
286 $graph->parameter['y_max_left'] = count($options) - 1;
287 $graph->parameter['y_axis_gridlines'] = count($options);
288 $graph->parameter['y_resolution_left'] = 1;
289 $graph->parameter['y_decimal_left'] = 1;
290 $graph->parameter['x_axis_angle'] = 20;
292 $graph->draw();
294 break;
296 case "overall.png":
298 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
300 foreach ($qqq as $key => $qq) {
301 if ($qq->multi) {
302 $qqq[$key]->text = get_string($qq->text, "survey");
303 $qqq[$key]->options = get_string($qq->options, "survey");
304 if ($qq->type < 0) {
305 $virtualscales = true;
309 foreach ($qqq as $qq) { // If any virtual, then use JUST virtual, else use JUST nonvirtual.
310 if ($qq->multi) {
311 if ($virtualscales && $qq->type < 0) {
312 $question[] = $qq;
313 } else if (!$virtualscales && $qq->type > 0) {
314 $question[] = $qq;
318 $numquestions = count($question);
320 $options = explode(",", $question[0]->options);
321 $numoptions = count($options);
323 for ($i = 0; $i < $numquestions; $i++) {
324 $names[$i] = $question[$i]->text;
325 $buckets1[$i] = 0.0;
326 $buckets2[$i] = 0.0;
327 $stdev1[$i] = 0.0;
328 $stdev2[$i] = 0.0;
329 $count1[$i] = 0;
330 $count2[$i] = 0;
331 $subquestions = $question[$i]->multi; // Otherwise next line doesn't work.
332 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))",
333 array($cm->instance));
335 if ($aaa) {
336 foreach ($aaa as $a) {
337 if (!$group or isset($users[$a->userid])) {
338 if ($a->answer1) {
339 $buckets1[$i] += $a->answer1;
340 $count1[$i]++;
342 if ($a->answer2) {
343 $buckets2[$i] += $a->answer2;
344 $count2[$i]++;
350 if ($count1[$i]) {
351 $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
353 if ($count2[$i]) {
354 $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
357 // Calculate the standard devaiations.
358 if ($aaa) {
359 foreach ($aaa as $a) {
360 if (!$group or isset($users[$a->userid])) {
361 if ($a->answer1) {
362 $difference = (float) ($a->answer1 - $buckets1[$i]);
363 $stdev1[$i] += ($difference * $difference);
365 if ($a->answer2) {
366 $difference = (float) ($a->answer2 - $buckets2[$i]);
367 $stdev2[$i] += ($difference * $difference);
373 if ($count1[$i]) {
374 $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
376 if ($count2[$i]) {
377 $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
380 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
381 $buckets2[$i] = $buckets2[$i] - 1;
385 $maxbuckets1 = max($buckets1);
386 $maxbuckets2 = max($buckets2);
388 $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
389 $graph->parameter['title'] = strip_tags(format_string($survey->name, true));
391 $graph->x_data = $names;
393 $graph->y_data['answers1'] = $buckets1;
394 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
395 'shadow_offset' => 4, 'legend' => $stractual);
396 $graph->y_data['answers2'] = $buckets2;
397 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
398 'shadow_offset' => 4, 'legend' => $strpreferred);
400 $graph->y_data['stdev1'] = $stdev1;
401 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
402 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
403 $graph->y_data['stdev2'] = $stdev2;
404 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
405 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
406 $graph->offset_relation['stdev1'] = 'answers1';
407 $graph->offset_relation['stdev2'] = 'answers2';
409 $graph->parameter['legend'] = 'outside-top';
410 $graph->parameter['legend_border'] = 'black';
411 $graph->parameter['legend_offset'] = 4;
413 $graph->y_tick_labels = $options;
415 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
416 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
417 } else if ($maxbuckets1 > 0.0) {
418 $graph->y_order = array('stdev1', 'answers1');
419 } else {
420 $graph->y_order = array('stdev2', 'answers2');
423 $graph->parameter['y_max_left'] = $numoptions - 1;
424 $graph->parameter['y_axis_gridlines'] = $numoptions;
425 $graph->parameter['y_resolution_left'] = 1;
426 $graph->parameter['y_decimal_left'] = 1;
427 $graph->parameter['x_axis_angle'] = 0;
428 $graph->parameter['x_inner_padding'] = 6;
430 $graph->draw();
432 break;
434 case "student.png":
436 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
438 foreach ($qqq as $key => $qq) {
439 if ($qq->multi) {
440 $qqq[$key]->text = get_string($qq->text, "survey");
441 $qqq[$key]->options = get_string($qq->options, "survey");
442 if ($qq->type < 0) {
443 $virtualscales = true;
447 foreach ($qqq as $qq) { // If any virtual, then use JUST virtual, else use JUST nonvirtual.
448 if ($qq->multi) {
449 if ($virtualscales && $qq->type < 0) {
450 $question[] = $qq;
451 } else if (!$virtualscales && $qq->type > 0) {
452 $question[] = $qq;
456 $numquestions = count($question);
458 $options = explode(",", $question[0]->options);
459 $numoptions = count($options);
461 for ($i = 0; $i < $numquestions; $i++) {
462 $names[$i] = $question[$i]->text;
463 $buckets1[$i] = 0.0;
464 $buckets2[$i] = 0.0;
465 $count1[$i] = 0;
466 $count2[$i] = 0;
467 $studbuckets1[$i] = 0.0;
468 $studbuckets2[$i] = 0.0;
469 $studcount1[$i] = 0;
470 $studcount2[$i] = 0;
471 $stdev1[$i] = 0.0;
472 $stdev2[$i] = 0.0;
474 $subquestions = $question[$i]->multi; // Otherwise next line doesn't work.
475 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))",
476 array($cm->instance));
478 if ($aaa) {
479 foreach ($aaa as $a) {
480 if (!$group or isset($users[$a->userid])) {
481 if ($a->userid == $sid) {
482 if ($a->answer1) {
483 $studbuckets1[$i] += $a->answer1;
484 $studcount1[$i]++;
486 if ($a->answer2) {
487 $studbuckets2[$i] += $a->answer2;
488 $studcount2[$i]++;
491 if ($a->answer1) {
492 $buckets1[$i] += $a->answer1;
493 $count1[$i]++;
495 if ($a->answer2) {
496 $buckets2[$i] += $a->answer2;
497 $count2[$i]++;
503 if ($count1[$i]) {
504 $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
506 if ($count2[$i]) {
507 $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
509 if ($studcount1[$i]) {
510 $studbuckets1[$i] = (float) $studbuckets1[$i] / (float) $studcount1[$i];
512 if ($studcount2[$i]) {
513 $studbuckets2[$i] = (float) $studbuckets2[$i] / (float) $studcount2[$i];
516 // Calculate the standard devaiations.
517 foreach ($aaa as $a) {
518 if (!$group or isset($users[$a->userid])) {
519 if ($a->answer1) {
520 $difference = (float) ($a->answer1 - $buckets1[$i]);
521 $stdev1[$i] += ($difference * $difference);
523 if ($a->answer2) {
524 $difference = (float) ($a->answer2 - $buckets2[$i]);
525 $stdev2[$i] += ($difference * $difference);
530 if ($count1[$i]) {
531 $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
533 if ($count2[$i]) {
534 $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
537 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
538 $buckets2[$i] = $buckets2[$i] - 1;
539 $studbuckets1[$i] = $studbuckets1[$i] - 1;
540 $studbuckets2[$i] = $studbuckets2[$i] - 1;
544 $maxbuckets1 = max($buckets1);
545 $maxbuckets2 = max($buckets2);
547 $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
548 $graph->parameter['title'] = strip_tags(format_string($survey->name, true));
550 $graph->x_data = $names;
552 $graph->y_data['answers1'] = $buckets1;
553 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
554 'shadow_offset' => 0.1, 'legend' => $stractualclass);
555 $graph->y_data['answers2'] = $buckets2;
556 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
557 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
558 $graph->y_data['studanswers1'] = $studbuckets1;
559 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
560 'shadow_offset' => 4, 'legend' => $stractualstudent);
561 $graph->y_data['studanswers2'] = $studbuckets2;
562 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
563 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
564 $graph->y_data['stdev1'] = $stdev1;
565 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
566 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
567 $graph->y_data['stdev2'] = $stdev2;
568 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
569 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
570 $graph->offset_relation['stdev1'] = 'answers1';
571 $graph->offset_relation['stdev2'] = 'answers2';
573 $graph->y_tick_labels = $options;
575 $graph->parameter['bar_size'] = 0.15;
577 $graph->parameter['legend'] = 'outside-top';
578 $graph->parameter['legend_border'] = 'black';
579 $graph->parameter['legend_offset'] = 4;
581 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
582 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
583 } else if ($maxbuckets1 > 0.0) {
584 $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
585 } else {
586 $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
589 $graph->parameter['y_max_left'] = $numoptions - 1;
590 $graph->parameter['y_axis_gridlines'] = $numoptions;
591 $graph->parameter['y_resolution_left'] = 1;
592 $graph->parameter['y_decimal_left'] = 1;
593 $graph->parameter['x_axis_angle'] = 20;
595 $graph->draw();
596 break;
598 case "studentmultiquestion.png":
600 $question = $DB->get_record("survey_questions", array("id" => $qid));
601 $question->text = get_string($question->text, "survey");
602 $question->options = get_string($question->options, "survey");
604 $options = explode(",", $question->options);
605 $questionorder = explode(",", $question->multi);
607 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
609 foreach ($questionorder as $i => $val) {
610 $names[$i] = get_string($qqq[$val]->shorttext, "survey");
611 $buckets1[$i] = 0;
612 $buckets2[$i] = 0;
613 $count1[$i] = 0;
614 $count2[$i] = 0;
615 $indexof[$val] = $i;
616 $studbuckets1[$i] = 0.0;
617 $studbuckets2[$i] = 0.0;
618 $studcount1[$i] = 0;
619 $studcount2[$i] = 0;
620 $stdev1[$i] = 0.0;
621 $stdev2[$i] = 0.0;
624 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))",
625 array($cm->instance));
627 if ($aaa) {
628 foreach ($aaa as $a) {
629 if (!$group or isset($users[$a->userid])) {
630 $index = $indexof[$a->question];
631 if ($a->userid == $sid) {
632 if ($a->answer1) {
633 $studbuckets1[$index] += $a->answer1;
634 $studcount1[$index]++;
636 if ($a->answer2) {
637 $studbuckets2[$index] += $a->answer2;
638 $studcount2[$index]++;
641 if ($a->answer1) {
642 $buckets1[$index] += $a->answer1;
643 $count1[$index]++;
645 if ($a->answer2) {
646 $buckets2[$index] += $a->answer2;
647 $count2[$index]++;
653 foreach ($questionorder as $i => $val) {
654 if ($count1[$i]) {
655 $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
657 if ($count2[$i]) {
658 $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
660 if ($studcount1[$i]) {
661 $studbuckets1[$i] = (float) $studbuckets1[$i] / (float) $studcount1[$i];
663 if ($studcount2[$i]) {
664 $studbuckets2[$i] = (float) $studbuckets2[$i] / (float) $studcount2[$i];
668 foreach ($aaa as $a) {
669 if (!$group or isset($users[$a->userid])) {
670 $index = $indexof[$a->question];
671 if ($a->answer1) {
672 $difference = (float) ($a->answer1 - $buckets1[$index]);
673 $stdev1[$index] += ($difference * $difference);
675 if ($a->answer2) {
676 $difference = (float) ($a->answer2 - $buckets2[$index]);
677 $stdev2[$index] += ($difference * $difference);
682 foreach ($questionorder as $i => $val) {
683 if ($count1[$i]) {
684 $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
686 if ($count2[$i]) {
687 $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
689 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
690 $buckets2[$i] = $buckets2[$i] - 1;
691 $studbuckets1[$i] = $studbuckets1[$i] - 1;
692 $studbuckets2[$i] = $studbuckets2[$i] - 1;
695 $maxbuckets1 = max($buckets1);
696 $maxbuckets2 = max($buckets2);
698 $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
699 $graph->parameter['title'] = "$question->text";
701 $graph->x_data = $names;
702 $graph->y_data['answers1'] = $buckets1;
703 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
704 'shadow_offset' => 0.1, 'legend' => $stractualclass);
705 $graph->y_data['answers2'] = $buckets2;
706 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
707 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
708 $graph->y_data['studanswers1'] = $studbuckets1;
709 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
710 'shadow_offset' => 4, 'legend' => $stractualstudent);
711 $graph->y_data['studanswers2'] = $studbuckets2;
712 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
713 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
714 $graph->y_data['stdev1'] = $stdev1;
715 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
716 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
717 $graph->y_data['stdev2'] = $stdev2;
718 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
719 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
720 $graph->offset_relation['stdev1'] = 'answers1';
721 $graph->offset_relation['stdev2'] = 'answers2';
723 $graph->parameter['bar_size'] = 0.15;
725 $graph->parameter['legend'] = 'outside-top';
726 $graph->parameter['legend_border'] = 'black';
727 $graph->parameter['legend_offset'] = 4;
729 $graph->y_tick_labels = $options;
731 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
732 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
733 } else if ($maxbuckets1 > 0.0) {
734 $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
735 } else {
736 $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
739 $graph->parameter['y_max_left'] = count($options) - 1;
740 $graph->parameter['y_axis_gridlines'] = count($options);
741 $graph->parameter['y_resolution_left'] = 1;
742 $graph->parameter['y_decimal_left'] = 1;
743 $graph->parameter['x_axis_angle'] = 20;
745 $graph->draw();
747 break;
749 default:
750 break;
753 exit;