[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/report/stats/ -> 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  /**
  18   * This file is part of the User section Moodle
  19   *
  20   * @package    report
  21   * @subpackage stats
  22   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../config.php');
  27  require_once($CFG->dirroot.'/report/stats/locallib.php');
  28  require_once($CFG->dirroot.'/lib/graphlib.php');
  29  
  30  $courseid = required_param('course', PARAM_INT);
  31  $report   = required_param('report', PARAM_INT);
  32  $time     = required_param('time', PARAM_INT);
  33  $mode     = required_param('mode', PARAM_INT);
  34  $userid   = optional_param('userid', 0, PARAM_INT);
  35  $roleid   = optional_param('roleid',0,PARAM_INT);
  36  
  37  $url = new moodle_url('/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode, 'userid'=>$userid, 'roleid'=>$roleid));
  38  $PAGE->set_url($url);
  39  
  40  $course = $DB->get_record("course", array("id"=>$courseid), '*', MUST_EXIST);
  41  $coursecontext   = context_course::instance($course->id);
  42  $PAGE->set_context($coursecontext);
  43  
  44  if (!empty($userid)) {
  45      $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
  46      $personalcontext = context_user::instance($user->id);
  47  
  48      if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
  49              and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
  50          //TODO: do not require parents to be enrolled in courses - this is a hack!
  51          require_login();
  52          $PAGE->set_course($course);
  53      } else {
  54          require_login($course);
  55      }
  56  
  57      if (!report_stats_can_access_user_report($user, $course, true)) {
  58          require_capability('report/stats:view', $coursecontext);
  59      }
  60  } else if ($mode === STATS_MODE_DETAILED) {
  61      print_error('invaliduser');
  62  } else {
  63      require_capability('report/stats:view', $coursecontext);
  64  }
  65  
  66  stats_check_uptodate($course->id);
  67  
  68  $param = stats_get_parameters($time,$report,$course->id,$mode);
  69  
  70  if (!empty($userid)) {
  71      $param->table = 'user_'.$param->table;
  72  }
  73  
  74  // TODO: cleanup this ugly mess!
  75  $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
  76  .' FROM {stats_'.$param->table.'} WHERE '
  77  .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
  78   .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
  79   .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
  80   . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
  81   .' timeend >= '.$param->timeafter
  82  .' '.$param->extras
  83  .' ORDER BY timeend DESC';
  84  
  85  $stats = $DB->get_records_sql($sql, $param->params);
  86  
  87  $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
  88  
  89  $stats = array_reverse($stats);
  90  
  91  $graph = new graph(750,400);
  92  
  93  $graph->parameter['legend'] = 'outside-right';
  94  $graph->parameter['legend_size'] = 10;
  95  $graph->parameter['x_axis_angle'] = 90;
  96  $graph->parameter['title'] = false; // moodle will do a nicer job.
  97  $graph->y_tick_labels = null;
  98  
  99  if (empty($param->crosstab)) {
 100      foreach ($stats as $stat) {
 101          $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
 102          $graph->y_data['line1'][] = $stat->line1;
 103          if (isset($stat->line2)) {
 104              $graph->y_data['line2'][] = $stat->line2;
 105          }
 106          if (isset($stat->line3)) {
 107              $graph->y_data['line3'][] = $stat->line3;
 108          }
 109      }
 110      $graph->y_order = array('line1');
 111      $graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
 112      if (!empty($param->line2)) {
 113          $graph->y_order[] = 'line2';
 114          $graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
 115      }
 116      if (!empty($param->line3)) {
 117          $graph->y_order[] = 'line3';
 118          $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
 119      }
 120      $graph->y_tick_labels = false;
 121  
 122  } else {
 123      $data = array();
 124      $times = array();
 125      $roles = array();
 126      $missedlines = array();
 127      $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true);
 128      foreach ($stats as $stat) {
 129          $data[$stat->roleid][$stat->timeend] = $stat->line1;
 130          if (!empty($stat->zerofixed)) {
 131              $missedlines[] = $stat->timeend;
 132          }
 133          if ($stat->roleid != 0) {
 134              if (!array_key_exists($stat->roleid,$roles)) {
 135                  $roles[$stat->roleid] = $rolenames[$stat->roleid];
 136              }
 137          } else {
 138              if (!array_key_exists($stat->roleid,$roles)) {
 139                  $roles[$stat->roleid] = get_string('all');
 140              }
 141          }
 142          if (!array_key_exists($stat->timeend,$times)) {
 143              $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
 144          }
 145      }
 146      foreach (array_keys($times) as $t) {
 147          foreach ($data as $roleid => $stuff) {
 148              if (!array_key_exists($t, $stuff)) {
 149                  $data[$roleid][$t] = 0;
 150              }
 151          }
 152      }
 153  
 154      $roleid = 0;
 155      krsort($roles); // the same sorting as in table below graph
 156  
 157      $colors = array('green', 'blue', 'red', 'purple', 'yellow', 'olive', 'navy', 'maroon', 'gray', 'ltred', 'ltltred', 'ltgreen', 'ltltgreen', 'orange', 'ltorange', 'ltltorange', 'lime', 'ltblue', 'ltltblue', 'fuchsia', 'aqua', 'grayF0', 'grayEE', 'grayDD', 'grayCC', 'gray33', 'gray66', 'gray99');
 158      $colorindex = 0;
 159  
 160      foreach ($roles as $roleid=>$rname) {
 161          ksort($data[$roleid]);
 162          $graph->y_order[] = $roleid+1;
 163          if ($roleid) {
 164              $color = $colors[$colorindex++];
 165              $colorindex = $colorindex % count($colors);
 166          } else {
 167              $color = 'black';
 168          }
 169          $graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
 170      }
 171      foreach (array_keys($data[$roleid]) as $time) {
 172          $graph->x_data[] = $times[$time];
 173      }
 174      foreach ($data as $roleid => $t) {
 175          foreach ($t as $time => $data) {
 176              $graph->y_data[$roleid+1][] = $data;
 177          }
 178      }
 179  }
 180  
 181  $graph->draw_stack();
 182  


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