[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/quiz/report/overview/ -> overviewgraph.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file renders the quiz overview graph.
  19   *
  20   * @package   quiz_overview
  21   * @copyright 2008 Jamie Pratt
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  require_once(dirname(__FILE__) . '/../../../../config.php');
  27  require_once($CFG->libdir . '/graphlib.php');
  28  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  29  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  30  
  31  $quizid = required_param('id', PARAM_INT);
  32  $groupid = optional_param('groupid', 0, PARAM_INT);
  33  
  34  $quiz = $DB->get_record('quiz', array('id' => $quizid));
  35  $course = $DB->get_record('course', array('id' => $quiz->course));
  36  $cm = get_coursemodule_from_instance('quiz', $quizid);
  37  
  38  require_login($course, false, $cm);
  39  $modcontext = context_module::instance($cm->id);
  40  require_capability('mod/quiz:viewreports', $modcontext);
  41  
  42  if ($groupid && $groupmode = groups_get_activity_groupmode($cm)) {
  43      // Groups are being used.
  44      $groups = groups_get_activity_allowed_groups($cm);
  45      if (!array_key_exists($groupid, $groups)) {
  46          print_error('errorinvalidgroup', 'group', null, $groupid);
  47      }
  48      $group = $groups[$groupid];
  49      $groupusers = get_users_by_capability($modcontext,
  50              array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
  51              '', '', '', '', $group->id, '', false);
  52      if (!$groupusers) {
  53          print_error('nostudentsingroup');
  54      }
  55      $groupusers = array_keys($groupusers);
  56  } else {
  57      $groupusers = array();
  58  }
  59  
  60  $line = new graph(800, 600);
  61  $line->parameter['title'] = '';
  62  $line->parameter['y_label_left'] = get_string('participants');
  63  $line->parameter['x_label'] = get_string('grade');
  64  $line->parameter['y_label_angle'] = 90;
  65  $line->parameter['x_label_angle'] = 0;
  66  $line->parameter['x_axis_angle'] = 60;
  67  
  68  // The following two lines seem to silence notice warnings from graphlib.php.
  69  $line->y_tick_labels = null;
  70  $line->offset_relation = null;
  71  
  72  // We will make size > 1 to get an overlap effect when showing groups.
  73  $line->parameter['bar_size'] = 1;
  74  // Don't forget to increase spacing so that graph doesn't become one big block of colour.
  75  $line->parameter['bar_spacing'] = 10;
  76  
  77  // Pick a sensible number of bands depending on quiz maximum grade.
  78  $bands = $quiz->grade;
  79  while ($bands > 20 || $bands <= 10) {
  80      if ($bands > 50) {
  81          $bands /= 5;
  82      } else if ($bands > 20) {
  83          $bands /= 2;
  84      }
  85      if ($bands < 4) {
  86          $bands *= 5;
  87      } else if ($bands <= 10) {
  88          $bands *= 2;
  89      }
  90  }
  91  
  92  // See MDL-34589. Using doubles as array keys causes problems in PHP 5.4,
  93  // hence the explicit cast to int.
  94  $bands = (int) ceil($bands);
  95  $bandwidth = $quiz->grade / $bands;
  96  $bandlabels = array();
  97  for ($i = 1; $i <= $bands; $i++) {
  98      $bandlabels[] = quiz_format_grade($quiz, ($i - 1) * $bandwidth) . ' - ' .
  99              quiz_format_grade($quiz, $i * $bandwidth);
 100  }
 101  $line->x_data = $bandlabels;
 102  
 103  $line->y_format['allusers'] = array(
 104      'colour' => 'red',
 105      'bar' => 'fill',
 106      'shadow_offset' => 1,
 107      'legend' => get_string('allparticipants')
 108  );
 109  $line->y_data['allusers'] = quiz_report_grade_bands($bandwidth, $bands, $quizid, $groupusers);
 110  
 111  $line->y_order = array('allusers');
 112  
 113  $ymax = max($line->y_data['allusers']);
 114  $line->parameter['y_min_left'] = 0;
 115  $line->parameter['y_max_left'] = $ymax;
 116  $line->parameter['y_decimal_left'] = 0;
 117  
 118  // Pick a sensible number of gridlines depending on max value on graph.
 119  $gridlines = $ymax;
 120  while ($gridlines >= 10) {
 121      if ($gridlines >= 50) {
 122          $gridlines /= 5;
 123      } else {
 124          $gridlines /= 2;
 125      }
 126  }
 127  
 128  $line->parameter['y_axis_gridlines'] = $gridlines + 1;
 129  $line->draw();


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1