MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / survey / graph.php
1 <?php
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     $url = new moodle_url('/mod/survey/graph.php', array('id'=>$id, 'type'=>$type));
14     if ($group !== 0) {
15         $url->param('group', $group);
16     }
17     if ($sid !== false) {
18         $url->param('sid', $sid);
19     }
20     if ($qid !== 0) {
21         $url->param('qid', $qid);
22     }
23     $PAGE->set_url($url);
25     if (! $cm = get_coursemodule_from_id('survey', $id)) {
26         print_error('invalidcoursemodule');
27     }
29     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
30         print_error('coursemisconf');
31     }
33     if ($sid) {
34         if (!$user = $DB->get_record("user", array("id"=>$sid))) {
35             print_error('invaliduserid');
36         }
37     }
39     require_login($course, false, $cm);
41     $groupmode = groups_get_activity_groupmode($cm);   // Groups are being used
42     $context = context_module::instance($cm->id);
44     if (!has_capability('mod/survey:readresponses', $context)) {
45         if ($type != "student.png" or $sid != $USER->id ) {
46             print_error('nopermissiontoshow');
47         } else if ($groupmode and !groups_is_member($group)) {
48             print_error('nopermissiontoshow');
49         }
50     }
52     if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
53         print_error('invalidsurveyid', 'survey');
54     }
56 /// Check to see if groups are being used in this survey
57     if ($group) {
58         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false);
59     } else if (!empty($cm->groupingid)) {
60         $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
61         $groups = array_keys($groups);
62         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
63     } else {
64         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
65         $group = false;
66     }
68     $stractual = get_string("actual", "survey");
69     $stractualclass = get_string("actualclass", "survey");
71     $strpreferred = get_string("preferred", "survey");
72     $strpreferredclass = get_string("preferredclass", "survey");
74     if ($sid || isset($user)) {
75         $stractualstudent = get_string("actualstudent", "survey", fullname($user));
76         $strpreferredstudent = get_string("preferredstudent", "survey", fullname($user));
77     }
79     $virtualscales = false; //set default value for case clauses
81     switch ($type) {
83      case "question.png":
85        $question = $DB->get_record("survey_questions", array("id"=>$qid));
86        $question->text = get_string($question->text, "survey");
87        $question->options = get_string($question->options, "survey");
89        $options = explode(",",$question->options);
91        while (list($key,) = each($options)) {
92            $buckets1[$key] = 0;
93            $buckets2[$key] = 0;
94        }
96        if ($aaa = $DB->get_records('survey_answers', array('survey'=>$cm->instance, 'question'=>$qid))) {
97            foreach ($aaa as $aa) {
98                if (!$group or isset($users[$aa->userid])) {
99                    if ($a1 = $aa->answer1) {
100                        $buckets1[$a1 - 1]++;
101                    }
102                    if ($a2 = $aa->answer2) {
103                        $buckets2[$a2 - 1]++;
104                    }
105                }
106            }
107        }
110        $maxbuckets1 = max($buckets1);
111        $maxbuckets2 = max($buckets2);
112        $maxbuckets = ($maxbuckets1 > $maxbuckets2) ? $maxbuckets1 : $maxbuckets2;
114        $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
115        $graph->parameter['title'] = "$question->text";
117        $graph->x_data               = $options;
119        $graph->y_data['answers1']   = $buckets1;
120        $graph->y_format['answers1'] = array('colour' => 'ltblue','bar' => 'fill','legend' =>$stractual,'bar_size' => 0.4);
121        $graph->y_data['answers2']   = $buckets2;
122        $graph->y_format['answers2'] = array('colour' =>'ltorange','bar' => 'fill','legend' =>$strpreferred,'bar_size' => 0.2);
124        $graph->parameter['legend']        = 'outside-top';
125        $graph->parameter['legend_border'] = 'black';
126        $graph->parameter['legend_offset'] = 4;
128        if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
129            $graph->y_order = array('answers1', 'answers2');
130        } else if ($maxbuckets1 > 0.0) {
131            $graph->y_order = array('answers1');
132        } else {
133            $graph->y_order = array('answers2');
134        }
136        $graph->parameter['y_axis_gridlines']= $maxbuckets + 1;
137        $graph->parameter['y_resolution_left']= 1;
138        $graph->parameter['y_decimal_left']  = 0;
139        $graph->parameter['x_axis_angle']    = 20;
140        $graph->parameter['shadow']          = 'none';
142        $graph->y_tick_labels = null;
143        $graph->offset_relation = null;
145        $graph->draw_stack();
147        break;
151      case "multiquestion.png":
153        $question  = $DB->get_record("survey_questions", array("id"=>$qid));
154        $question->text = get_string($question->text, "survey");
155        $question->options = get_string($question->options, "survey");
157        $options = explode(",",$question->options);
158        $questionorder = explode( ",", $question->multi);
160        $qqq = $DB->get_records_list("survey_questions", "id", explode(',',$question->multi));
162        foreach ($questionorder as $i => $val) {
163            $names[$i] = get_string($qqq["$val"]->shorttext, "survey");
164            $buckets1[$i] = 0;
165            $buckets2[$i] = 0;
166            $count1[$i] = 0;
167            $count2[$i] = 0;
168            $indexof[$val] = $i;
169            $stdev1[$i] = 0;
170            $stdev2[$i] = 0;
171        }
173        $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance));
175        if ($aaa) {
176            foreach ($aaa as $a) {
177                if (!$group or isset($users[$a->userid])) {
178                    $index = $indexof[$a->question];
179                    if ($a->answer1) {
180                        $buckets1[$index] += $a->answer1;
181                        $count1[$index]++;
182                    }
183                    if ($a->answer2) {
184                        $buckets2[$index] += $a->answer2;
185                        $count2[$index]++;
186                    }
187                }
188            }
189        }
191        foreach ($questionorder as $i => $val) {
192            if ($count1[$i]) {
193                $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
194            }
195            if ($count2[$i]) {
196                $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
197            }
198        }
200        if ($aaa) {
201            foreach ($aaa as $a) {
202                if (!$group or isset($users[$a->userid])) {
203                    $index = $indexof[$a->question];
204                    if ($a->answer1) {
205                        $difference = (float) ($a->answer1 - $buckets1[$index]);
206                        $stdev1[$index] += ($difference * $difference);
207                    }
208                    if ($a->answer2) {
209                        $difference = (float) ($a->answer2 - $buckets2[$index]);
210                        $stdev2[$index] += ($difference * $difference);
211                    }
212                }
213            }
214        }
216        foreach ($questionorder as $i => $val) {
217            if ($count1[$i]) {
218                $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
219            }
220            if ($count2[$i]) {
221                $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
222            }
223            $buckets1[$i] = $buckets1[$i] - 1;
224            $buckets2[$i] = $buckets2[$i] - 1;
225        }
229        $maxbuckets1 = max($buckets1);
230        $maxbuckets2 = max($buckets2);
233        $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
234        $graph->parameter['title'] = "$question->text";
236        $graph->x_data               = $names;
237        $graph->y_data['answers1']   = $buckets1;
238        $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line',  'point' => 'square',
239                                             'shadow_offset' => 4, 'legend' => $stractual);
240        $graph->y_data['answers2']   = $buckets2;
241        $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
242                                                 'shadow_offset' => 4, 'legend' => $strpreferred);
243        $graph->y_data['stdev1']   = $stdev1;
244        $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
245                                             'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
246        $graph->y_data['stdev2']   = $stdev2;
247        $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
248                                             'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
249        $graph->offset_relation['stdev1'] = 'answers1';
250        $graph->offset_relation['stdev2'] = 'answers2';
252        $graph->parameter['bar_size']    = 0.15;
254        $graph->parameter['legend']        = 'outside-top';
255        $graph->parameter['legend_border'] = 'black';
256        $graph->parameter['legend_offset'] = 4;
258        $graph->y_tick_labels = $options;
260        if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
261               $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
262        } else if ($maxbuckets1 > 0.0) {
263            $graph->y_order = array('stdev1', 'answers1');
264        } else {
265            $graph->y_order = array('stdev2', 'answers2');
266        }
268        $graph->parameter['y_max_left']= count($options) - 1;
269        $graph->parameter['y_axis_gridlines']= count($options);
270        $graph->parameter['y_resolution_left']= 1;
271        $graph->parameter['y_decimal_left']= 1;
272        $graph->parameter['x_axis_angle']  = 20;
274        $graph->draw();
276        break;
280      case "overall.png":
282        $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
285        foreach ($qqq as $key => $qq) {
286            if ($qq->multi) {
287                $qqq[$key]->text = get_string($qq->text, "survey");
288                $qqq[$key]->options = get_string($qq->options, "survey");
289                if ($qq->type < 0) {
290                    $virtualscales = true;
291                }
292            }
293        }
294        foreach ($qqq as $qq) {         // if any virtual, then use JUST virtual, else use JUST nonvirtual
295            if ($qq->multi) {
296                if ($virtualscales && $qq->type < 0) {
297                    $question[] = $qq;
298                } else if (!$virtualscales && $qq->type > 0) {
299                    $question[] = $qq;
300                }
301            }
302        }
303        $numquestions = count($question);
305        $options = explode(",",$question[0]->options);
306        $numoptions = count($options);
308        for ($i=0; $i<$numquestions; $i++) {
309            $names[$i] = $question[$i]->text;
310            $buckets1[$i] = 0.0;
311            $buckets2[$i] = 0.0;
312            $stdev1[$i] = 0.0;
313            $stdev2[$i] = 0.0;
314            $count1[$i] = 0;
315            $count2[$i] = 0;
316            $subquestions = $question[$i]->multi;   // otherwise next line doesn't work
317            $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))", array($cm->instance));
319            if ($aaa) {
320                foreach ($aaa as $a) {
321                    if (!$group or isset($users[$a->userid])) {
322                        if ($a->answer1) {
323                            $buckets1[$i] += $a->answer1;
324                            $count1[$i]++;
325                        }
326                        if ($a->answer2) {
327                            $buckets2[$i] += $a->answer2;
328                            $count2[$i]++;
329                        }
330                    }
331                }
332            }
334            if ($count1[$i]) {
335                $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
336            }
337            if ($count2[$i]) {
338                $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
339            }
341            // Calculate the standard devaiations
342            if ($aaa) {
343                foreach ($aaa as $a) {
344                    if (!$group or isset($users[$a->userid])) {
345                        if ($a->answer1) {
346                            $difference = (float) ($a->answer1 - $buckets1[$i]);
347                            $stdev1[$i] += ($difference * $difference);
348                        }
349                        if ($a->answer2) {
350                            $difference = (float) ($a->answer2 - $buckets2[$i]);
351                            $stdev2[$i] += ($difference * $difference);
352                        }
353                    }
354                }
355            }
357            if ($count1[$i]) {
358                $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
359            }
360            if ($count2[$i]) {
361                $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
362            }
364            $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
365            $buckets2[$i] = $buckets2[$i] - 1;
367        }
369        $maxbuckets1 = max($buckets1);
370        $maxbuckets2 = max($buckets2);
373        $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
374        $graph->parameter['title'] = strip_tags(format_string($survey->name,true));
376        $graph->x_data               = $names;
378        $graph->y_data['answers1']   = $buckets1;
379        $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line',  'point' => 'square',
380                                             'shadow_offset' => 4, 'legend' => $stractual);
381        $graph->y_data['answers2']   = $buckets2;
382        $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
383                                                 'shadow_offset' => 4, 'legend' => $strpreferred);
385        $graph->y_data['stdev1']   = $stdev1;
386        $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
387                                             'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
388        $graph->y_data['stdev2']   = $stdev2;
389        $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
390                                             'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
391        $graph->offset_relation['stdev1'] = 'answers1';
392        $graph->offset_relation['stdev2'] = 'answers2';
394        $graph->parameter['legend']        = 'outside-top';
395        $graph->parameter['legend_border'] = 'black';
396        $graph->parameter['legend_offset'] = 4;
398        $graph->y_tick_labels = $options;
400        if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
401               $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
402        } else if ($maxbuckets1 > 0.0) {
403            $graph->y_order = array('stdev1', 'answers1');
404        } else {
405            $graph->y_order = array('stdev2', 'answers2');
406        }
408        $graph->parameter['y_max_left']= $numoptions - 1;
409        $graph->parameter['y_axis_gridlines']= $numoptions;
410        $graph->parameter['y_resolution_left']= 1;
411        $graph->parameter['y_decimal_left']= 1;
412        $graph->parameter['x_axis_angle']  = 0;
413        $graph->parameter['x_inner_padding']  = 6;
415        $graph->draw();
417        break;
421      case "student.png":
423        $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
425        foreach ($qqq as $key => $qq) {
426            if ($qq->multi) {
427                $qqq[$key]->text = get_string($qq->text, "survey");
428                $qqq[$key]->options = get_string($qq->options, "survey");
429                if ($qq->type < 0) {
430                    $virtualscales = true;
431                }
432            }
433        }
434        foreach ($qqq as $qq) {         // if any virtual, then use JUST virtual, else use JUST nonvirtual
435            if ($qq->multi) {
436                if ($virtualscales && $qq->type < 0) {
437                    $question[] = $qq;
438                } else if (!$virtualscales && $qq->type > 0) {
439                    $question[] = $qq;
440                }
441            }
442        }
443        $numquestions= count($question);
445        $options = explode(",",$question[0]->options);
446        $numoptions = count($options);
448        for ($i=0; $i<$numquestions; $i++) {
449            $names[$i] = $question[$i]->text;
450            $buckets1[$i] = 0.0;
451            $buckets2[$i] = 0.0;
452            $count1[$i] = 0;
453            $count2[$i] = 0;
454            $studbuckets1[$i] = 0.0;
455            $studbuckets2[$i] = 0.0;
456            $studcount1[$i] = 0;
457            $studcount2[$i] = 0;
458            $stdev1[$i] = 0.0;
459            $stdev2[$i] = 0.0;
461            $subquestions = $question[$i]->multi;   // otherwise next line doesn't work
462            $aaa = $DB->get_records_select("survey_answers","((survey = ?) AND (question in ($subquestions)))", array($cm->instance));
464            if ($aaa) {
465                foreach ($aaa as $a) {
466                    if (!$group or isset($users[$a->userid])) {
467                        if ($a->userid == $sid) {
468                            if ($a->answer1) {
469                                $studbuckets1[$i] += $a->answer1;
470                                $studcount1[$i]++;
471                            }
472                            if ($a->answer2) {
473                                $studbuckets2[$i] += $a->answer2;
474                                $studcount2[$i]++;
475                            }
476                        }
477                        if ($a->answer1) {
478                            $buckets1[$i] += $a->answer1;
479                            $count1[$i]++;
480                        }
481                        if ($a->answer2) {
482                            $buckets2[$i] += $a->answer2;
483                            $count2[$i]++;
484                        }
485                    }
486                }
487            }
489            if ($count1[$i]) {
490                $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
491            }
492            if ($count2[$i]) {
493                $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
494            }
495            if ($studcount1[$i]) {
496                $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
497            }
498            if ($studcount2[$i]) {
499                $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
500            }
502            // Calculate the standard devaiations
503            foreach ($aaa as $a) {
504                if (!$group or isset($users[$a->userid])) {
505                    if ($a->answer1) {
506                        $difference = (float) ($a->answer1 - $buckets1[$i]);
507                        $stdev1[$i] += ($difference * $difference);
508                    }
509                    if ($a->answer2) {
510                        $difference = (float) ($a->answer2 - $buckets2[$i]);
511                        $stdev2[$i] += ($difference * $difference);
512                    }
513                }
514            }
516            if ($count1[$i]) {
517                $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
518            }
519            if ($count2[$i]) {
520                $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
521            }
523            $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
524            $buckets2[$i] = $buckets2[$i] - 1;
525            $studbuckets1[$i] = $studbuckets1[$i] - 1;
526            $studbuckets2[$i] = $studbuckets2[$i] - 1;
528        }
530        $maxbuckets1 = max($buckets1);
531        $maxbuckets2 = max($buckets2);
534        $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
535        $graph->parameter['title'] = strip_tags(format_string($survey->name,true));
537        $graph->x_data               = $names;
539        $graph->y_data['answers1']   = $buckets1;
540        $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line',  'point' => 'square',
541                                             'shadow_offset' => 0.1, 'legend' => $stractualclass);
542        $graph->y_data['answers2']   = $buckets2;
543        $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
544                                                 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
545        $graph->y_data['studanswers1']   = $studbuckets1;
546        $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line',  'point' => 'square',
547                                             'shadow_offset' => 4, 'legend' => $stractualstudent);
548        $graph->y_data['studanswers2']   = $studbuckets2;
549        $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
550                                                 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
551        $graph->y_data['stdev1']   = $stdev1;
552        $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
553                                             'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
554        $graph->y_data['stdev2']   = $stdev2;
555        $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
556                                             'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
557        $graph->offset_relation['stdev1'] = 'answers1';
558        $graph->offset_relation['stdev2'] = 'answers2';
560        $graph->y_tick_labels = $options;
562        $graph->parameter['bar_size']    = 0.15;
564        $graph->parameter['legend']        = 'outside-top';
565        $graph->parameter['legend_border'] = 'black';
566        $graph->parameter['legend_offset'] = 4;
568        if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
569               $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
570        } else if ($maxbuckets1 > 0.0) {
571            $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
572        } else {
573            $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
574        }
576        $graph->parameter['y_max_left']= $numoptions - 1;
577        $graph->parameter['y_axis_gridlines']= $numoptions;
578        $graph->parameter['y_resolution_left']= 1;
579        $graph->parameter['y_decimal_left']= 1;
580        $graph->parameter['x_axis_angle']  = 20;
582        $graph->draw();
583        break;
587      case "studentmultiquestion.png":
589        $question  = $DB->get_record("survey_questions", array("id"=>$qid));
590        $question->text = get_string($question->text, "survey");
591        $question->options = get_string($question->options, "survey");
593        $options = explode(",",$question->options);
594        $questionorder = explode( ",", $question->multi);
596        $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
598        foreach ($questionorder as $i => $val) {
599            $names[$i] = get_string($qqq[$val]->shorttext, "survey");
600            $buckets1[$i] = 0;
601            $buckets2[$i] = 0;
602            $count1[$i] = 0;
603            $count2[$i] = 0;
604            $indexof[$val] = $i;
605            $studbuckets1[$i] = 0.0;
606            $studbuckets2[$i] = 0.0;
607            $studcount1[$i] = 0;
608            $studcount2[$i] = 0;
609            $stdev1[$i] = 0.0;
610            $stdev2[$i] = 0.0;
611        }
613        $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance));
615        if ($aaa) {
616            foreach ($aaa as $a) {
617                if (!$group or isset($users[$a->userid])) {
618                    $index = $indexof[$a->question];
619                        if ($a->userid == $sid) {
620                            if ($a->answer1) {
621                                $studbuckets1[$index] += $a->answer1;
622                                $studcount1[$index]++;
623                            }
624                            if ($a->answer2) {
625                                $studbuckets2[$index] += $a->answer2;
626                                $studcount2[$index]++;
627                            }
628                        }
629                    if ($a->answer1) {
630                        $buckets1[$index] += $a->answer1;
631                        $count1[$index]++;
632                    }
633                    if ($a->answer2) {
634                        $buckets2[$index] += $a->answer2;
635                        $count2[$index]++;
636                    }
637                }
638            }
639        }
641        foreach ($questionorder as $i => $val) {
642            if ($count1[$i]) {
643                $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
644            }
645            if ($count2[$i]) {
646                $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
647            }
648            if ($studcount1[$i]) {
649                $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
650            }
651            if ($studcount2[$i]) {
652                $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
653            }
654        }
656        foreach ($aaa as $a) {
657            if (!$group or isset($users[$a->userid])) {
658                $index = $indexof[$a->question];
659                if ($a->answer1) {
660                    $difference = (float) ($a->answer1 - $buckets1[$index]);
661                    $stdev1[$index] += ($difference * $difference);
662                }
663                if ($a->answer2) {
664                    $difference = (float) ($a->answer2 - $buckets2[$index]);
665                    $stdev2[$index] += ($difference * $difference);
666                }
667            }
668        }
670        foreach ($questionorder as $i => $val) {
671            if ($count1[$i]) {
672                $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
673            }
674            if ($count2[$i]) {
675                $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
676            }
677            $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
678            $buckets2[$i] = $buckets2[$i] - 1;
679            $studbuckets1[$i] = $studbuckets1[$i] - 1;
680            $studbuckets2[$i] = $studbuckets2[$i] - 1;
681        }
685        $maxbuckets1 = max($buckets1);
686        $maxbuckets2 = max($buckets2);
689        $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
690        $graph->parameter['title'] = "$question->text";
692        $graph->x_data               = $names;
693        $graph->y_data['answers1']   = $buckets1;
694        $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line',  'point' => 'square',
695                                             'shadow_offset' => 0.1, 'legend' => $stractualclass);
696        $graph->y_data['answers2']   = $buckets2;
697        $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
698                                                 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
699        $graph->y_data['studanswers1']   = $studbuckets1;
700        $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line',  'point' => 'square',
701                                             'shadow_offset' => 4, 'legend' => $stractualstudent);
702        $graph->y_data['studanswers2']   = $studbuckets2;
703        $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
704                                                 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
705        $graph->y_data['stdev1']   = $stdev1;
706        $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
707                                             'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
708        $graph->y_data['stdev2']   = $stdev2;
709        $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
710                                             'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
711        $graph->offset_relation['stdev1'] = 'answers1';
712        $graph->offset_relation['stdev2'] = 'answers2';
714        $graph->parameter['bar_size']    = 0.15;
716        $graph->parameter['legend']        = 'outside-top';
717        $graph->parameter['legend_border'] = 'black';
718        $graph->parameter['legend_offset'] = 4;
720        $graph->y_tick_labels = $options;
722        if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
723            $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
724        } else if ($maxbuckets1 > 0.0) {
725            $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
726        } else {
727            $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
728        }
730        $graph->parameter['y_max_left']= count($options)-1;
731        $graph->parameter['y_axis_gridlines']= count($options);
732        $graph->parameter['y_resolution_left']= 1;
733        $graph->parameter['y_decimal_left']= 1;
734        $graph->parameter['x_axis_angle']  = 20;
736        $graph->draw();
738        break;
740      default:
741        break;
742    }
744    exit;