[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/scorm/report/graphs/ -> graph.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   * Core Report class of graphs reporting plugin
  18   *
  19   * @package    scormreport_graphs
  20   * @copyright  2012 Ankit Kumar Agarwal
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  define('NO_DEBUG_DISPLAY', true);
  25  
  26  require_once(dirname(__FILE__) . '/../../../../config.php');
  27  require_once($CFG->libdir . '/graphlib.php');
  28  require_once($CFG->dirroot.'/mod/scorm/report/reportlib.php');
  29  require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  30  
  31  $scoid = required_param('scoid', PARAM_INT);// SCO ID.
  32  
  33  $sco = $DB->get_record('scorm_scoes', array('id' => $scoid), '*', MUST_EXIST);
  34  $scorm = $DB->get_record('scorm', array('id' => $sco->scorm), '*', MUST_EXIST);
  35  $cm = get_coursemodule_from_instance('scorm', $scorm->id, 0, false, MUST_EXIST);
  36  $course = $DB->get_record('course', array('id' => $scorm->course), '*', MUST_EXIST);
  37  
  38  
  39  // Capability Checks.
  40  require_login($course, false, $cm);
  41  $contextmodule = context_module::instance($cm->id);
  42  require_capability('mod/scorm:viewreport', $contextmodule);
  43  
  44  // Find out current groups mode.
  45  $currentgroup = groups_get_activity_group($cm, true);
  46  
  47  // Group Check.
  48  if (empty($currentgroup)) {
  49      // All users who can attempt scoes.
  50      $students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id' , '', '', '', '', '', false);
  51      $allowedlist = empty($students) ? array() : array_keys($students);
  52  } else {
  53      // All users who can attempt scoes and who are in the currently selected group.
  54      $groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', $currentgroup, '', false);
  55      $allowedlist = empty($groupstudents) ? array() : array_keys($groupstudents);
  56  }
  57  
  58  $params = array();
  59  $bands = 11;
  60  $bandwidth = 10;
  61  
  62  // Determine maximum % secured for each user.
  63  $usergrades = array();
  64  $graphdata = array();
  65  for ($i = 0; $i < $bands; $i++) {
  66      $graphdata[$i] = 0;
  67  }
  68  
  69  // Do this only if we have students to report.
  70  if (!empty($allowedlist)) {
  71      list($usql, $params) = $DB->get_in_or_equal($allowedlist);
  72      $params[] = $scoid;
  73      // Construct the SQL.
  74      $select = 'SELECT DISTINCT '.$DB->sql_concat('st.userid', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
  75      $select .= 'st.userid AS userid, st.scormid AS scormid, st.attempt AS attempt, st.scoid AS scoid ';
  76      $from = 'FROM {scorm_scoes_track} st ';
  77      $where = ' WHERE st.userid ' .$usql. ' and st.scoid = ?';
  78      $attempts = $DB->get_records_sql($select.$from.$where, $params);
  79  
  80      foreach ($attempts as $attempt) {
  81          if ($trackdata = scorm_get_tracks($scoid, $attempt->userid, $attempt->attempt)) {
  82              if (isset($trackdata->score_raw)) {
  83                  $score = $trackdata->score_raw;
  84                  if (empty($trackdata->score_min)) {
  85                      $minmark = 0;
  86                  } else {
  87                      $minmark = $trackdata->score_min;
  88                  }
  89                  if (empty($trackdata->score_max)) {
  90                      $maxmark = 100;
  91                  } else {
  92                      $maxmark = $trackdata->score_max;
  93                  }
  94                  $range = ($maxmark - $minmark);
  95                  if (empty($range)) {
  96                      continue;
  97                  }
  98                  $percent = round((($score * 100) / $range), 2);
  99                  if (empty($usergrades[$attempt->userid]) || !isset($usergrades[$attempt->userid])
 100                          || ($percent > $usergrades[$attempt->userid]) || ($usergrades[$attempt->userid] === '*')) {
 101                      $usergrades[$attempt->userid] = $percent;
 102                  }
 103                  unset($percent);
 104              } else {
 105                  // User has made an attempt but either SCO was not able to record the score or something else is broken in SCO.
 106                  if (!isset($usergrades[$attempt->userid])) {
 107                      $usergrades[$attempt->userid] = '*';
 108                  }
 109              }
 110          }
 111      }
 112  }
 113  
 114  $bandlabels[] = get_string('invaliddata', 'scormreport_graphs');
 115  for ($i = 1; $i <= $bands - 1; $i++) {
 116      $bandlabels[] = ($i - 1) * $bandwidth . ' - ' . $i * $bandwidth;
 117  }
 118  // Recording all users  who attempted the SCO,but resulting data was invalid.
 119  foreach ($usergrades as $userpercent) {
 120      if ($userpercent === '*') {
 121          $graphdata[0]++;
 122      } else {
 123          $gradeband = floor($userpercent / 10);
 124          if ($gradeband != ($bands - 1)) {
 125              $gradeband++;
 126          }
 127          $graphdata[$gradeband]++;
 128      }
 129  }
 130  
 131  $line = new graph(800, 600);
 132  $line->parameter['title'] = '';
 133  $line->parameter['y_label_left'] = get_string('participants', 'scormreport_graphs');
 134  $line->parameter['x_label'] = get_string('percent', 'scormreport_graphs');
 135  $line->parameter['y_label_angle'] = 90;
 136  $line->parameter['x_label_angle'] = 0;
 137  $line->parameter['x_axis_angle'] = 60;
 138  
 139  // Following two lines seem to silence notice warnings from graphlib.php.
 140  $line->y_tick_labels = null;
 141  $line->offset_relation = null;
 142  
 143  $line->parameter['bar_size'] = 1;
 144  // Don't forget to increase spacing so that graph doesn't become one big block of colour.
 145  $line->parameter['bar_spacing'] = 10;
 146  $line->x_data = $bandlabels;
 147  
 148  $line->y_format['allusers'] = array(
 149      'colour' => 'red',
 150      'bar' => 'fill',
 151      'shadow_offset' => 1,
 152      'legend' => get_string('allparticipants')
 153  );
 154  ksort($graphdata);
 155  $line->y_data['allusers'] = $graphdata;
 156  $line->y_order = array('allusers');
 157  
 158  $ymax = max($line->y_data['allusers']);
 159  $line->parameter['y_min_left'] = 0;  // Start at 0.
 160  $line->parameter['y_max_left'] = $ymax;
 161  $line->parameter['y_decimal_left'] = 0; // 2 decimal places for y axis.
 162  
 163  // Pick a sensible number of gridlines depending on max value on graph.
 164  $gridlines = $ymax;
 165  while ($gridlines >= 10) {
 166      if ($gridlines >= 50) {
 167          $gridlines /= 5;
 168      } else {
 169          $gridlines /= 2;
 170      }
 171  }
 172  $gridlines = max(2, ($gridlines + 1)); // We need a minimum of two lines.
 173  $line->parameter['y_axis_gridlines'] = $gridlines;
 174  
 175  $line->draw();


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