MDL-15264 "Bar graph image missed when group with no attempts selected" added a query to check for sure that there are some grades there to display in the graph. Also now displaying grades for all groups a user has permission to view - but only if that is up to 4 groups and only if there are less than 500 grades total.

This commit is contained in:
jamiesensei
2008-06-19 12:31:28 +00:00
parent f5018bc548
commit e05eb943d0
3 changed files with 62 additions and 26 deletions
+53 -22
View File
@@ -2,19 +2,32 @@
include '../../../../config.php';
include $CFG->dirroot."/lib/graphlib.php";
include $CFG->dirroot."/mod/quiz/report/reportlib.php";
function graph_get_new_colour(){
static $colourindex = 0;
$colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black', 'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange', 'olive', 'gray', 'ltltred', 'ltorange', 'lime', 'ltblue', 'ltltblue');
$colour = $colours[$colourindex];
$colourindex++;
if ($colourindex > (count($colours)-1)){
$colourindex =0;
}
return $colour;
}
define('QUIZ_REPORT_MAX_PARTICIPANTS_TO_SHOW_ALL_GROUPS', 500);
$quizid = required_param('id', PARAM_INT);
$quiz = get_record('quiz', 'id', $quizid);
$course = get_record('course', 'id', $quiz->course);
require_login($course);
$cm = get_coursemodule_from_instance('quiz', $quizid);
$currentgroup = groups_get_activity_group($cm);
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
$groups = groups_get_activity_allowed_groups($cm);
} else {
$groups = false;
}
$modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/quiz:viewreports', $modcontext);
$line = new graph(640,480);
$line = new graph(800,600);
$line->parameter['title'] = '';
$line->parameter['y_label_left'] = $course->students;
$line->parameter['x_label'] = get_string('grade');
@@ -26,8 +39,8 @@ $line->parameter['x_axis_angle'] = 60;
$line->y_tick_labels = null;
$line->offset_relation = null;
$line->parameter['bar_size'] = 1.5; // make size > 1 to get overlap effect
$line->parameter['bar_spacing'] = 30; // don't forget to increase spacing so that graph doesn't become one big block of colour
$line->parameter['bar_size'] = 1; // will make size > 1 to get overlap effect when showing groups
$line->parameter['bar_spacing'] = 10; // don't forget to increase spacing so that graph doesn't become one big block of colour
//pick a sensible number of bands depending on quiz maximum grade.
$bands = $quiz->grade;
@@ -58,25 +71,42 @@ for ($i=0;$i < $quiz->grade;$i += $bandwidth){
}
$line->x_data = $bandlabels;
$useridlist = join(',',array_keys(get_users_by_capability($modcontext, 'mod/quiz:attempt','','','','','','',false)));
$line->y_data['allusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridlist);
if ($currentgroup){
//only turn on legends if there is more than one set of bars
$line->parameter['legend'] = 'outside-top';
$line->parameter['legend_border'] = 'black';
$line->parameter['legend_offset'] = 4;
$useridingrouplist = join(',',array_keys(get_users_by_capability($modcontext, 'mod/quiz:attempt','','','','',$currentgroup,'',false)));
$line->y_data['groupusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridingrouplist);
$line->y_format['groupusers'] =
array('colour' => 'green', 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => groups_get_group_name($currentgroup));
$line->y_order = array('allusers', 'groupusers');
} else {
$line->y_order = array('allusers');
$line->y_format['allusers'] =
array('colour' => graph_get_new_colour(), 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => get_string('allparticipants'));
$line->y_data['allusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid);
if (array_sum($line->y_data['allusers'])>QUIZ_REPORT_MAX_PARTICIPANTS_TO_SHOW_ALL_GROUPS ||
count($groups)>4){
if ($groups){
if ($currentgroup = groups_get_activity_group($cm)){
$groups = array($currentgroup=>'');
} else {
$groups = false;//all participants mode
}
}
}
$line->y_order = array('allusers');
if ($groups){
foreach (array_keys($groups) as $group){
$useridingroup = get_users_by_capability($modcontext, 'mod/quiz:attempt','','','','',$group,'',false);
if ($useridingroup){
$useridingrouplist = join(',',array_keys($useridingroup));
$groupdata = quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridingrouplist);
if ($groupdata){
$line->parameter['bar_size'] = 1.2;
$line->y_data['groupusers'.$group] = $groupdata;
//only turn on legends if there is more than one set of bars
$line->parameter['legend'] = 'outside-top';
$line->parameter['legend_border'] = 'black';
$line->parameter['legend_offset'] = 4;
$line->y_format['groupusers'.$group] =
array('colour' => graph_get_new_colour(), 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => groups_get_group_name($group));
$line->y_order[] ='groupusers'.$group;
}
}
}
}
$line->y_format['allusers'] =
array('colour' => 'red', 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => get_string('allparticipants'));
$line->parameter['y_min_left'] = 0; // start at 0
@@ -96,4 +126,5 @@ while ($gridlines >= 10){
$line->parameter['y_axis_gridlines'] = $gridlines+1;
$line->draw();
?>
+2 -1
View File
@@ -643,7 +643,8 @@ class quiz_report extends quiz_default_report {
// Print display options
$mform->set_data($displayoptions +compact('detailedmarks', 'pagesize'));
$mform->display();
if ($attempts){
//should be quicker than a COUNT to test if there is at least one record :
if (get_records('quiz_grades', 'quiz', $quiz->id, '', '*', 0, 1)){
$imageurl = $CFG->wwwroot.'/mod/quiz/report/overview/overviewgraph.php?id='.$quiz->id;
print_heading(get_string('overviewreportgraph', 'quiz_overview'));
echo '<div class="mdl-align"><img src="'.$imageurl.'" alt="'.get_string('overviewreportgraph', 'quiz_overview').'" /></div>';
+7 -3
View File
@@ -163,7 +163,7 @@ function quiz_report_qm_filter_subselect($quiz, $useridsql = 'u.id'){
return $qmsubselect;
}
function quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridlist){
function quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridlist=''){
global $CFG;
$sql = "SELECT
FLOOR(qg.grade/$bandwidth) AS band,
@@ -171,10 +171,13 @@ function quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridlist){
FROM
{$CFG->prefix}quiz_grades qg,
{$CFG->prefix}quiz q
WHERE qg.quiz = q.id AND qg.quiz = $quizid AND qg.userid IN ($useridlist)
WHERE qg.quiz = q.id AND qg.quiz = $quizid
".($useridlist?"AND qg.userid IN ($useridlist) ":'')."
GROUP BY band
ORDER BY band";
$data = get_records_sql_menu($sql);
if (!$data = get_records_sql_menu($sql)){
$data= array();
}
//need to create array elements with values 0 at indexes where there is no element
$data = $data + array_fill(0, $bands+1, 0);
ksort($data);
@@ -184,6 +187,7 @@ function quiz_report_grade_bands($bandwidth, $bands, $quizid, $useridlist){
$data[$bands-1] += $data[$bands];
unset($data[$bands]);
return $data;
}
function quiz_report_highlighting_grading_method($quiz, $qmsubselect, $qmfilter){
if ($quiz->attempts == 1) {//only one attempt allowed on this quiz