MDL-19976 Changing redirect($url->out()) to redirect($url). NEVER, NEVER use redirect...
[moodle.git] / mod / quiz / report / overview / overviewgraph.php
CommitLineData
8b87ab00 1<?php // $Id$
2include '../../../../config.php';
3include $CFG->dirroot."/lib/graphlib.php";
4include $CFG->dirroot."/mod/quiz/report/reportlib.php";
8b87ab00 5$quizid = required_param('id', PARAM_INT);
f29e6691 6$groupid = optional_param('groupid', 0, PARAM_INT);
8b87ab00 7
9cf4a18b 8$quiz = $DB->get_record('quiz', array('id' => $quizid));
9$course = $DB->get_record('course', array('id' => $quiz->course));
8b87ab00 10$cm = get_coursemodule_from_instance('quiz', $quizid);
084f4c15 11require_login($course, true, $cm);
f29e6691 12$modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
13if ($groupid && $groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
8b2f8253 14 $groups = groups_get_activity_allowed_groups($cm);
f29e6691 15 if (array_key_exists($groupid, $groups)){
16 $group = $groups[$groupid];
96c7d771 17 if (!$groupusers = get_users_by_capability($modcontext, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),'','','','',$group->id,'',false)){
f29e6691 18 print_error('nostudentsingroup');
19 } else {
20 $groupusers = array_keys($groupusers);
21 }
22 } else {
23 print_error('errorinvalidgroup', 'group', null, $groupid);
24 }
8b2f8253 25} else {
26 $groups = false;
f29e6691 27 $group = false;
28 $groupusers = array();
8b2f8253 29}
8b87ab00 30require_capability('mod/quiz:viewreports', $modcontext);
31
8b2f8253 32$line = new graph(800,600);
8b87ab00 33$line->parameter['title'] = '';
9101efd3 34$line->parameter['y_label_left'] = get_string('participants');
8b87ab00 35$line->parameter['x_label'] = get_string('grade');
36$line->parameter['y_label_angle'] = 90;
37$line->parameter['x_label_angle'] = 0;
38$line->parameter['x_axis_angle'] = 60;
39
40//following two lines seem to silence notice warnings from graphlib.php
41$line->y_tick_labels = null;
42$line->offset_relation = null;
43
8b2f8253 44$line->parameter['bar_size'] = 1; // will make size > 1 to get overlap effect when showing groups
45$line->parameter['bar_spacing'] = 10; // don't forget to increase spacing so that graph doesn't become one big block of colour
8b87ab00 46
47//pick a sensible number of bands depending on quiz maximum grade.
48$bands = $quiz->grade;
f29e6691 49while ($bands > 20 || $bands <= 10){
50 if ($bands > 50){
8b87ab00 51 $bands = $bands /5;
f29e6691 52 } else if ($bands > 20) {
8b87ab00 53 $bands = $bands /2;
54 }
55 if ($bands < 4){
56 $bands = $bands * 5;
f29e6691 57 } else if ($bands <= 10){
8b87ab00 58 $bands = $bands * 2;
59 }
60}
61
62$bandwidth = $quiz->grade / $bands;
b392a56c 63$bands = ceil($bands);
8b87ab00 64$bandlabels = array();
65for ($i=0;$i < $quiz->grade;$i += $bandwidth){
f88fb62c 66 $label = quiz_format_grade($quiz, $i).' - ';
b392a56c 67 if ($quiz->grade > $i+$bandwidth){
f88fb62c 68 $label .= quiz_format_grade($quiz, $i+$bandwidth);
b392a56c 69 } else {
f88fb62c 70 $label .= quiz_format_grade($quiz, $quiz->grade);
b392a56c 71 }
72 $bandlabels[] = $label;
9cf4a18b 73}
8b87ab00 74$line->x_data = $bandlabels;
75
8b2f8253 76$line->y_format['allusers'] =
f29e6691 77 array('colour' => 'red', 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => get_string('allparticipants'));
78$line->y_data['allusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid, $groupusers);
8b87ab00 79
f29e6691 80$line->y_order = array('allusers');
8b87ab00 81
82$line->parameter['y_min_left'] = 0; // start at 0
9cf4a18b 83$line->parameter['y_max_left'] = max($line->y_data['allusers']);
8b87ab00 84$line->parameter['y_decimal_left'] = 0; // 2 decimal places for y axis.
85
86
87//pick a sensible number of gridlines depending on max value on graph.
88$gridlines = max($line->y_data['allusers']);
89while ($gridlines >= 10){
90 if ($gridlines >= 50){
91 $gridlines = $gridlines /5;
92 } else {
93 $gridlines = $gridlines /2;
94 }
95}
96
9cf4a18b 97$line->parameter['y_axis_gridlines'] = $gridlines+1;
8b87ab00 98$line->draw();
99?>