]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15264 "Bar graph image missed when group with no attempts selected" added a query...
authorjamiesensei <jamiesensei>
Thu, 19 Jun 2008 14:03:24 +0000 (14:03 +0000)
committerjamiesensei <jamiesensei>
Thu, 19 Jun 2008 14:03:24 +0000 (14:03 +0000)
Merged from 1.9 branch

mod/quiz/report/overview/overviewgraph.php
mod/quiz/report/overview/report.php
mod/quiz/report/reportlib.php

index 10b48be1e76f4d97de03e8a6e5f77973762b2184..687272c5e36dc26764a9cfc56497d50a251418d5 100644 (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 = $DB->get_record('quiz', array('id' => $quizid));
 $course = $DB->get_record('course', array('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,41 @@ for ($i=0;$i < $quiz->grade;$i += $bandwidth){
 }
 $line->x_data          = $bandlabels;
 
-$userids = array_keys(get_users_by_capability($modcontext, 'mod/quiz:attempt','','','','','','',false));
-$line->y_data['allusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid, $userids);
-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){
+            $groupdata = quiz_report_grade_bands($bandwidth, $bands, $quizid, array_keys($useridingroup));
+            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
index 3046809b7d2d5a7e05a7db0663d5e491111b1775..045159aa738ab9e43ef563808c4d8b8ee915295a 100644 (file)
@@ -290,7 +290,8 @@ class quiz_report extends quiz_default_report {
         $table->out($pagesize, true);
 
         if (!$table->is_downloading()) {
-            if (count($table->totalrows)){
+            //should be quicker than a COUNT to test if there is at least one record :
+            if ($DB->get_records('quiz_grades', array('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>';
index f87a885d705751718384abb58036eff2f1cf18a0..170244ceac428f55d5be6d728c80403b34c8eaa1 100644 (file)
@@ -176,15 +176,22 @@ function quiz_report_qm_filter_subselect($quiz, $useridsql = 'u.id', $quizidsql
     return $qmsubselect;
 }
 
-function quiz_report_grade_bands($bandwidth, $bands, $quizid, $userids){
+function quiz_report_grade_bands($bandwidth, $bands, $quizid, $userids=array()){
     global $CFG, $DB;
-    list($usql, $params) = $DB->get_in_or_equal($userids);
+    if ($userids){
+        list($usql, $params) = $DB->get_in_or_equal($userids);
+    } else {
+        $usql ='';
+        $params = array();
+    }
     $sql = "SELECT
         FLOOR(qg.grade/$bandwidth) AS band,
         COUNT(1) AS num
     FROM
         {quiz_grades} qg,  {quiz} q
-    WHERE qg.quiz = q.id AND qg.userid $usql AND qg.quiz = ?
+    WHERE qg.quiz = q.id " .
+            ($usql?"AND qg.userid $usql ":'') .
+            "AND qg.quiz = ?
     GROUP BY band
     ORDER BY band";
     $params[] = $quizid;