Use course setting for "student"
[moodle.git] / mod / survey / graph.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3 require("../../config.php");
4 require("$CFG->libdir/graphlib.php");
5 require("lib.php");
6
7 require_variable($id); // Course Module ID
c015c2bc 8 require_variable($type); // Graph Type
9 optional_variable($sid); // Student ID
f9903ed0 10
11 if (! $cm = get_record("course_modules", "id", $id)) {
12 error("Course Module ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
17 }
18
19 require_login($course->id);
20
c015c2bc 21 if (!isteacher($course->id) && !isadmin()) {
22 if (! ($type == "student.png" && $sid == $USER->id) ) {
23 error("Sorry, you aren't allowed to see this.");
24 }
f9903ed0 25 }
26
27 if (! $survey = get_record("survey", "id", $cm->instance)) {
28 error("Survey ID was incorrect");
29 }
30
f762c448 31 $stractual = get_string("actual", "survey");
32 $stractualclass = get_string("actualclass", "survey");
f39d5f45 33 $stractualstudent = get_string("actualstudent", "survey", $course->student);
f762c448 34
35 $strpreferred = get_string("preferred", "survey");
36 $strpreferredclass = get_string("preferredclass", "survey");
f39d5f45 37 $strpreferredstudent = get_string("preferredstudent", "survey", $course->student);
f762c448 38
f9903ed0 39 switch ($type) {
40
41 case "question.png":
42
43 $question = get_record("survey_questions", "id", $qid);
f762c448 44 $question->text = get_string($question->text, "survey");
45 $question->options = get_string($question->options, "survey");
f9903ed0 46
47 $options = explode(",",$question->options);
48
49 while (list($key,) = each($options)) {
50 $buckets1[$key] = 0;
51 $buckets2[$key] = 0;
52 }
53
54 $aa = $db->Execute("SELECT * FROM survey_answers WHERE survey = $cm->instance AND question = $qid");
55
56 while (!$aa->EOF) {
57 if ($a1 = $aa->fields["answer1"]) {
58 $buckets1[$a1 - 1]++;
59 }
60 if ($a2 = $aa->fields["answer2"]) {
61 $buckets2[$a2 - 1]++;
62 }
63 $aa->MoveNext();
64 }
a2b4ecfb 65
f9903ed0 66
67 $maxbuckets1 = max($buckets1);
68 $maxbuckets2 = max($buckets2);
69 $maxbuckets = ($maxbuckets1 > $maxbuckets2) ? $maxbuckets1 : $maxbuckets2;
70
551b0b98 71 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
f9903ed0 72 $graph->parameter['title'] = "$question->text";
73
74 $graph->x_data = $options;
75
76 $graph->y_data['answers1'] = $buckets1;
f762c448 77 $graph->y_format['answers1'] = array('colour' => 'ltblue','bar' => 'fill','legend' =>$stractual,'bar_size' => 0.4);
f9903ed0 78 $graph->y_data['answers2'] = $buckets2;
f762c448 79 $graph->y_format['answers2'] = array('colour' =>'ltorange','bar' => 'fill','legend' =>$strpreferred,'bar_size' => 0.2);
f9903ed0 80
81 $graph->parameter['legend'] = 'outside-top';
82 $graph->parameter['legend_border'] = 'black';
83 $graph->parameter['legend_offset'] = 4;
84
85 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
86 $graph->y_order = array('answers1', 'answers2');
87 } else if ($maxbuckets1 > 0.0) {
88 $graph->y_order = array('answers1');
89 } else {
90 $graph->y_order = array('answers2');
91 }
92
93 $graph->parameter['y_axis_gridlines']= $maxbuckets + 1;
94 $graph->parameter['y_resolution_left']= 1;
95 $graph->parameter['y_decimal_left'] = 0;
96 $graph->parameter['x_axis_angle'] = 0;
97 $graph->parameter['shadow'] = 'none';
98
99 $graph->draw_stack();
100
101 break;
102
103
104
105 case "multiquestion.png":
106
107 $question = get_record("survey_questions", "id", $qid);
f762c448 108 $question->text = get_string($question->text, "survey");
109 $question->options = get_string($question->options, "survey");
f9903ed0 110
111 $options = explode(",",$question->options);
112 $questionorder = explode( ",", $question->multi);
113
114 $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi)");
115
116 foreach ($questionorder as $i => $val) {
f762c448 117 $names[$i] = get_string($qqq["$val"]->shorttext, "survey");
f9903ed0 118 $buckets1[$i] = 0;
119 $buckets2[$i] = 0;
120 $count1[$i] = 0;
121 $count2[$i] = 0;
122 $indexof[$val] = $i;
123 }
124
125 $aaa = get_records_sql("SELECT * FROM survey_answers WHERE ((survey = $cm->instance) AND (question in ($question->multi)))");
126
127 foreach ($aaa as $a) {
128 $index = $indexof[$a->question];
129 if ($a->answer1) {
130 $buckets1[$index] += $a->answer1;
131 $count1[$index]++;
132 }
133 if ($a->answer2) {
134 $buckets2[$index] += $a->answer2;
135 $count2[$index]++;
136 }
137 }
138
139 foreach ($questionorder as $i => $val) {
140 if ($count1[$i]) {
141 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
142 }
143 if ($count2[$i]) {
144 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
145 }
146 }
147
148 foreach ($aaa as $a) {
149 $index = $indexof[$a->question];
150 if ($a->answer1) {
151 $difference = (float) ($a->answer1 - $buckets1[$index]);
152 $stdev1[$index] += ($difference * $difference);
153 }
154 if ($a->answer2) {
155 $difference = (float) ($a->answer2 - $buckets2[$index]);
156 $stdev2[$index] += ($difference * $difference);
157 }
158 }
159
160 foreach ($questionorder as $i => $val) {
161 if ($count1[$i]) {
162 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
163 }
164 if ($count2[$i]) {
165 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
166 }
a2b4ecfb 167 $buckets1[$i] = $buckets1[$i] - 1;
168 $buckets2[$i] = $buckets2[$i] - 1;
f9903ed0 169 }
170
171
172
173 $maxbuckets1 = max($buckets1);
174 $maxbuckets2 = max($buckets2);
175
176
551b0b98 177 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
f9903ed0 178 $graph->parameter['title'] = "$question->text";
179
180 $graph->x_data = $names;
181 $graph->y_data['answers1'] = $buckets1;
182 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
f762c448 183 'shadow_offset' => 4, 'legend' => $stractual);
f9903ed0 184 $graph->y_data['answers2'] = $buckets2;
fb7c09cd 185 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
f762c448 186 'shadow_offset' => 4, 'legend' => $strpreferred);
f9903ed0 187 $graph->y_data['stdev1'] = $stdev1;
188 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
961669ac 189 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
f9903ed0 190 $graph->y_data['stdev2'] = $stdev2;
fb7c09cd 191 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
961669ac 192 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
f9903ed0 193 $graph->offset_relation['stdev1'] = 'answers1';
194 $graph->offset_relation['stdev2'] = 'answers2';
195
196 $graph->parameter['bar_size'] = 0.15;
197
198 $graph->parameter['legend'] = 'outside-top';
199 $graph->parameter['legend_border'] = 'black';
200 $graph->parameter['legend_offset'] = 4;
201
a2b4ecfb 202 $graph->y_tick_labels = $options;
b321d421 203
f9903ed0 204 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
205 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
206 } else if ($maxbuckets1 > 0.0) {
207 $graph->y_order = array('stdev1', 'answers1');
208 } else {
209 $graph->y_order = array('stdev2', 'answers2');
210 }
211
a2b4ecfb 212 $graph->parameter['y_max_left']= count($options) - 1;
213 $graph->parameter['y_axis_gridlines']= count($options);
f9903ed0 214 $graph->parameter['y_resolution_left']= 1;
215 $graph->parameter['y_decimal_left']= 1;
216 $graph->parameter['x_axis_angle'] = 0;
217
218 $graph->draw();
219
220 break;
221
222
223
224 case "overall.png":
225
226 $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($survey->questions) AND multi <> ''");
227
f762c448 228 foreach ($qqq as $key => $qq) {
229 $qqq[$key]->text = get_string($qq->text, "survey");
230 $qqq[$key]->options = get_string($qq->options, "survey");
f9903ed0 231 if ($qq->type < 0) {
232 $virtualscales = true;
233 }
234 }
235 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
236 if ($virtualscales && $qq->type < 0) {
237 $question[] = $qq;
238 } else if (!$virtualscales && $qq->type > 0) {
239 $question[] = $qq;
240 }
241 }
242 $numquestions = count($question);
243
244 $options = explode(",",$question[0]->options);
245 $numoptions = count($options);
246
247 for ($i=0; $i<$numquestions; $i++) {
248 $names[$i] = $question[$i]->text;
249 $buckets1[$i] = 0.0;
250 $buckets2[$i] = 0.0;
251 $stdev1[$i] = 0.0;
252 $stdev2[$i] = 0.0;
253 $count1[$i] = 0;
254 $count2[$i] = 0;
255 $subquestions = $question[$i]->multi; // otherwise next line doesn't work
256 $aaa = get_records_sql("SELECT * FROM survey_answers WHERE ((survey = $cm->instance) AND (question in ($subquestions)))");
257
258 foreach ($aaa as $a) {
259 if ($a->answer1) {
260 $buckets1[$i] += $a->answer1;
261 $count1[$i]++;
262 }
263 if ($a->answer2) {
264 $buckets2[$i] += $a->answer2;
265 $count2[$i]++;
266 }
267 }
268
269 if ($count1[$i]) {
270 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
271 }
272 if ($count2[$i]) {
273 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
274 }
275
276 // Calculate the standard devaiations
277 foreach ($aaa as $a) {
278 if ($a->answer1) {
279 $difference = (float) ($a->answer1 - $buckets1[$i]);
280 $stdev1[$i] += ($difference * $difference);
281 }
282 if ($a->answer2) {
283 $difference = (float) ($a->answer2 - $buckets2[$i]);
284 $stdev2[$i] += ($difference * $difference);
285 }
286 }
287
288 if ($count1[$i]) {
289 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
290 }
291 if ($count2[$i]) {
292 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
293 }
294
a2b4ecfb 295 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
296 $buckets2[$i] = $buckets2[$i] - 1;
f9903ed0 297
298 }
299
300 $maxbuckets1 = max($buckets1);
301 $maxbuckets2 = max($buckets2);
302
303
551b0b98 304 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
f9903ed0 305 $graph->parameter['title'] = "$survey->name";
306
307 $graph->x_data = $names;
308
309 $graph->y_data['answers1'] = $buckets1;
310 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
f762c448 311 'shadow_offset' => 4, 'legend' => $stractual);
f9903ed0 312 $graph->y_data['answers2'] = $buckets2;
fb7c09cd 313 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
f762c448 314 'shadow_offset' => 4, 'legend' => $strpreferred);
f9903ed0 315
316 $graph->y_data['stdev1'] = $stdev1;
317 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
961669ac 318 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
f9903ed0 319 $graph->y_data['stdev2'] = $stdev2;
fb7c09cd 320 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
961669ac 321 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
f9903ed0 322 $graph->offset_relation['stdev1'] = 'answers1';
323 $graph->offset_relation['stdev2'] = 'answers2';
324
f9903ed0 325 $graph->parameter['legend'] = 'outside-top';
326 $graph->parameter['legend_border'] = 'black';
327 $graph->parameter['legend_offset'] = 4;
328
a2b4ecfb 329 $graph->y_tick_labels = $options;
b321d421 330
f9903ed0 331 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
332 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
333 } else if ($maxbuckets1 > 0.0) {
334 $graph->y_order = array('stdev1', 'answers1');
335 } else {
336 $graph->y_order = array('stdev2', 'answers2');
337 }
338
a2b4ecfb 339 $graph->parameter['y_max_left']= $numoptions - 1;
340 $graph->parameter['y_axis_gridlines']= $numoptions;
f9903ed0 341 $graph->parameter['y_resolution_left']= 1;
342 $graph->parameter['y_decimal_left']= 1;
343 $graph->parameter['x_axis_angle'] = 0;
344
345 $graph->draw();
346
347 break;
348
349
350
351 case "student.png":
352
353 $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($survey->questions) AND multi <> ''");
354
f762c448 355 foreach ($qqq as $key => $qq) {
356 $qqq[$key]->text = get_string($qq->text, "survey");
357 $qqq[$key]->options = get_string($qq->options, "survey");
f9903ed0 358 if ($qq->type < 0) {
359 $virtualscales = true;
360 }
361 }
362 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual
363 if ($virtualscales && $qq->type < 0) {
364 $question[] = $qq;
365 } else if (!$virtualscales && $qq->type > 0) {
366 $question[] = $qq;
367 }
368 }
369 $numquestions= count($question);
370
371 $options = explode(",",$question[0]->options);
372 $numoptions = count($options);
373
374 for ($i=0; $i<$numquestions; $i++) {
375 $names[$i] = $question[$i]->text;
376 $buckets1[$i] = 0.0;
377 $buckets2[$i] = 0.0;
378 $count1[$i] = 0;
379 $count2[$i] = 0;
380 $studbuckets1[$i] = 0.0;
381 $studbuckets2[$i] = 0.0;
382 $studcount1[$i] = 0;
383 $studcount2[$i] = 0;
384 $subquestions = $question[$i]->multi; // otherwise next line doesn't work
385 $aaa = get_records_sql("SELECT * FROM survey_answers WHERE ((survey = $cm->instance) AND (question in ($subquestions)))");
386
387 foreach ($aaa as $a) {
388 if ($a->user == $sid) {
389 if ($a->answer1) {
390 $studbuckets1[$i] += $a->answer1;
391 $studcount1[$i]++;
392 }
393 if ($a->answer2) {
394 $studbuckets2[$i] += $a->answer2;
395 $studcount2[$i]++;
396 }
397 }
398 if ($a->answer1) {
399 $buckets1[$i] += $a->answer1;
400 $count1[$i]++;
401 }
402 if ($a->answer2) {
403 $buckets2[$i] += $a->answer2;
404 $count2[$i]++;
405 }
406 }
407
408 if ($count1[$i]) {
409 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
410 }
411 if ($count2[$i]) {
412 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
413 }
414 if ($studcount1[$i]) {
415 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
416 }
417 if ($studcount2[$i]) {
418 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
419 }
420
421 // Calculate the standard devaiations
422 foreach ($aaa as $a) {
423 if ($a->answer1) {
424 $difference = (float) ($a->answer1 - $buckets1[$i]);
425 $stdev1[$i] += ($difference * $difference);
426 }
427 if ($a->answer2) {
428 $difference = (float) ($a->answer2 - $buckets2[$i]);
429 $stdev2[$i] += ($difference * $difference);
430 }
431 }
432
433 if ($count1[$i]) {
434 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
435 }
436 if ($count2[$i]) {
437 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
438 }
439
a2b4ecfb 440 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
441 $buckets2[$i] = $buckets2[$i] - 1;
442 $studbuckets1[$i] = $studbuckets1[$i] - 1;
443 $studbuckets2[$i] = $studbuckets2[$i] - 1;
444
f9903ed0 445 }
446
447 $maxbuckets1 = max($buckets1);
448 $maxbuckets2 = max($buckets2);
449
450
551b0b98 451 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
f9903ed0 452 $graph->parameter['title'] = "$survey->name";
453
454 $graph->x_data = $names;
455
456 $graph->y_data['answers1'] = $buckets1;
457 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
f762c448 458 'shadow_offset' => 0.1, 'legend' => $stractualclass);
f9903ed0 459 $graph->y_data['answers2'] = $buckets2;
fb7c09cd 460 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
f762c448 461 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
f9903ed0 462 $graph->y_data['studanswers1'] = $studbuckets1;
463 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
f762c448 464 'shadow_offset' => 4, 'legend' => $stractualstudent);
f9903ed0 465 $graph->y_data['studanswers2'] = $studbuckets2;
fb7c09cd 466 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
f762c448 467 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
f9903ed0 468 $graph->y_data['stdev1'] = $stdev1;
469 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
961669ac 470 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
f9903ed0 471 $graph->y_data['stdev2'] = $stdev2;
fb7c09cd 472 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
961669ac 473 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
f9903ed0 474 $graph->offset_relation['stdev1'] = 'answers1';
475 $graph->offset_relation['stdev2'] = 'answers2';
476
a2b4ecfb 477 $graph->y_tick_labels = $options;
b321d421 478
f9903ed0 479 $graph->parameter['bar_size'] = 0.15;
480
481 $graph->parameter['legend'] = 'outside-top';
482 $graph->parameter['legend_border'] = 'black';
483 $graph->parameter['legend_offset'] = 4;
484
485 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
fc633303 486 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
f9903ed0 487 } else if ($maxbuckets1 > 0.0) {
488 $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
489 } else {
490 $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
491 }
492
a2b4ecfb 493 $graph->parameter['y_max_left']= $numoptions - 1;
494 $graph->parameter['y_axis_gridlines']= $numoptions;
f9903ed0 495 $graph->parameter['y_resolution_left']= 1;
496 $graph->parameter['y_decimal_left']= 1;
497 $graph->parameter['x_axis_angle'] = 0;
498
499 $graph->draw();
500 break;
501
502
503
504 case "studentmultiquestion.png":
505
506 $question = get_record("survey_questions", "id", $qid);
f762c448 507 $question->text = get_string($question->text, "survey");
508 $question->options = get_string($question->options, "survey");
f9903ed0 509
510 $options = explode(",",$question->options);
511 $questionorder = explode( ",", $question->multi);
512
513 $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi)");
514
515 foreach ($questionorder as $i => $val) {
f762c448 516 $names[$i] = get_string($qqq[$val]->shorttext, "survey");
f9903ed0 517 $buckets1[$i] = 0;
518 $buckets2[$i] = 0;
519 $count1[$i] = 0;
520 $count2[$i] = 0;
521 $indexof[$val] = $i;
522 $studbuckets1[$i] = 0.0;
523 $studbuckets2[$i] = 0.0;
524 $studcount1[$i] = 0;
525 $studcount2[$i] = 0;
526 }
527
528 $aaa = get_records_sql("SELECT * FROM survey_answers WHERE ((survey = $cm->instance) AND (question in ($question->multi)))");
529
530 foreach ($aaa as $a) {
531 $index = $indexof[$a->question];
532 if ($a->user == $sid) {
533 if ($a->answer1) {
534 $studbuckets1[$index] += $a->answer1;
535 $studcount1[$index]++;
536 }
537 if ($a->answer2) {
538 $studbuckets2[$index] += $a->answer2;
539 $studcount2[$index]++;
540 }
541 }
542 if ($a->answer1) {
543 $buckets1[$index] += $a->answer1;
544 $count1[$index]++;
545 }
546 if ($a->answer2) {
547 $buckets2[$index] += $a->answer2;
548 $count2[$index]++;
549 }
550 }
551
552 foreach ($questionorder as $i => $val) {
553 if ($count1[$i]) {
554 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i];
555 }
556 if ($count2[$i]) {
557 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i];
558 }
559 if ($studcount1[$i]) {
560 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i];
561 }
562 if ($studcount2[$i]) {
563 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i];
564 }
565 }
566
567 foreach ($aaa as $a) {
568 $index = $indexof[$a->question];
569 if ($a->answer1) {
570 $difference = (float) ($a->answer1 - $buckets1[$index]);
571 $stdev1[$index] += ($difference * $difference);
572 }
573 if ($a->answer2) {
574 $difference = (float) ($a->answer2 - $buckets2[$index]);
575 $stdev2[$index] += ($difference * $difference);
576 }
577 }
578
579 foreach ($questionorder as $i => $val) {
580 if ($count1[$i]) {
581 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i]));
582 }
583 if ($count2[$i]) {
584 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i]));
585 }
a2b4ecfb 586 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data.
587 $buckets2[$i] = $buckets2[$i] - 1;
588 $studbuckets1[$i] = $studbuckets1[$i] - 1;
589 $studbuckets2[$i] = $studbuckets2[$i] - 1;
f9903ed0 590 }
591
592
593
594 $maxbuckets1 = max($buckets1);
595 $maxbuckets2 = max($buckets2);
596
597
551b0b98 598 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT);
f9903ed0 599 $graph->parameter['title'] = "$question->text";
600
601 $graph->x_data = $names;
602 $graph->y_data['answers1'] = $buckets1;
603 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
f762c448 604 'shadow_offset' => 0.1, 'legend' => $stractualclass);
f9903ed0 605 $graph->y_data['answers2'] = $buckets2;
fb7c09cd 606 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
f762c448 607 'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
f9903ed0 608 $graph->y_data['studanswers1'] = $studbuckets1;
609 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
f762c448 610 'shadow_offset' => 4, 'legend' => $stractualstudent);
f9903ed0 611 $graph->y_data['studanswers2'] = $studbuckets2;
fb7c09cd 612 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
f762c448 613 'shadow_offset' => 4, 'legend' => $strpreferredstudent);
f9903ed0 614 $graph->y_data['stdev1'] = $stdev1;
615 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
961669ac 616 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
f9903ed0 617 $graph->y_data['stdev2'] = $stdev2;
fb7c09cd 618 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
961669ac 619 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
f9903ed0 620 $graph->offset_relation['stdev1'] = 'answers1';
621 $graph->offset_relation['stdev2'] = 'answers2';
622
623 $graph->parameter['bar_size'] = 0.15;
624
625 $graph->parameter['legend'] = 'outside-top';
626 $graph->parameter['legend_border'] = 'black';
627 $graph->parameter['legend_offset'] = 4;
628
a2b4ecfb 629 $graph->y_tick_labels = $options;
b321d421 630
f9903ed0 631 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
fc633303 632 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
f9903ed0 633 } else if ($maxbuckets1 > 0.0) {
634 $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
635 } else {
636 $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
637 }
638
a2b4ecfb 639 $graph->parameter['y_max_left']= count($options)-1;
640 $graph->parameter['y_axis_gridlines']= count($options);
f9903ed0 641 $graph->parameter['y_resolution_left']= 1;
642 $graph->parameter['y_decimal_left']= 1;
643 $graph->parameter['x_axis_angle'] = 0;
644
645 $graph->draw();
646
647 break;
648
649 default:
650 break;
651 }
652
653 exit;
654
f9903ed0 655
656?>