[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/report/log/ -> 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   * Produces a graph of log accesses for a user
  19   *
  20   * Generates an image representing the log data in a graphical manner for a user.
  21   *
  22   * @package    report_log
  23   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  require("../../config.php");
  28  require_once("$CFG->libdir/graphlib.php");
  29  require_once($CFG->dirroot.'/report/log/locallib.php');
  30  
  31  $id   = required_param('id', PARAM_INT);       // Course ID.
  32  $type = required_param('type', PARAM_FILE);    // Graph Type.
  33  $user = required_param('user', PARAM_INT);     // Student ID.
  34  $date = optional_param('date', 0, PARAM_INT);  // A time of a day (in GMT).
  35  $logreader = optional_param('logreader', '', PARAM_COMPONENT);
  36  
  37  $url = new moodle_url('/report/log/graph.php', array('id' => $id, 'type' => $type, 'user' => $user, 'date' => $date,
  38      'logreader' => $logreader));
  39  $PAGE->set_url($url);
  40  
  41  if ($type !== "usercourse.png" and $type !== "userday.png") {
  42      $type = 'userday.png';
  43  }
  44  
  45  $course = $DB->get_record("course", array("id" => $id), '*', MUST_EXIST);
  46  $user = $DB->get_record("user", array("id" => $user, 'deleted' => 0), '*', MUST_EXIST);
  47  
  48  $coursecontext   = context_course::instance($course->id);
  49  $personalcontext = context_user::instance($user->id);
  50  
  51  if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
  52          and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
  53      //TODO: do not require parents to be enrolled in courses - this is a hack!
  54      require_login();
  55      $PAGE->set_course($course);
  56  } else {
  57      require_login($course);
  58  }
  59  
  60  list($all, $today) = report_log_can_access_user_report($user, $course);
  61  
  62  if ($type === "userday.png") {
  63      if (!$today) {
  64          require_capability('report/log:viewtoday', $coursecontext);
  65      }
  66  } else {
  67      if (!$all) {
  68          require_capability('report/log:view', $coursecontext);
  69      }
  70  }
  71  
  72  $logs = array();
  73  
  74  $timenow = time();
  75  
  76  if ($type === "usercourse.png") {
  77  
  78      $site = get_site();
  79  
  80      if ($course->id == $site->id) {
  81          $courseselect = 0;
  82      } else {
  83          $courseselect = $course->id;
  84      }
  85  
  86      $maxseconds = REPORT_LOG_MAX_DISPLAY * 3600 * 24;  // Seconds.
  87      if ($timenow - $course->startdate > $maxseconds) {
  88          $course->startdate = $timenow - $maxseconds;
  89      }
  90  
  91      if (!empty($CFG->loglifetime)) {
  92          $maxseconds = $CFG->loglifetime * 3600 * 24;  // Seconds.
  93          if ($timenow - $course->startdate > $maxseconds) {
  94              $course->startdate = $timenow - $maxseconds;
  95          }
  96      }
  97  
  98      $timestart = $coursestart = usergetmidnight($course->startdate);
  99  
 100      if ((($timenow - $timestart) / 86400.0) > 40) {
 101          $reducedays = 7;
 102      } else {
 103          $reducedays = 0;
 104      }
 105  
 106      $days = array();
 107      $i = 0;
 108      while ($timestart < $timenow) {
 109          $timefinish = $timestart + 86400;
 110          if ($reducedays) {
 111              if ($i % $reducedays) {
 112                  $days[$i] = "";
 113              } else {
 114                  $days[$i] = userdate($timestart, "%a %d %b");
 115              }
 116          } else {
 117              $days[$i] = userdate($timestart, "%a %d %b");
 118          }
 119          $logs[$i] = 0;
 120          $i++;
 121          $timestart = $timefinish;
 122      }
 123  
 124      $rawlogs = report_log_usercourse($user->id, $courseselect, $coursestart, $logreader);
 125  
 126      if (empty($rawlogs)) {
 127          return;
 128      }
 129  
 130      foreach ($rawlogs as $rawlog) {
 131          $logs[$rawlog->day] = $rawlog->num;
 132      }
 133  
 134      $graph = new graph(750, 400);
 135  
 136      $a = new stdClass();
 137      $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
 138      $a->username = fullname($user, true);
 139      $graph->parameter['title'] = get_string("hitsoncourse", "", $a);
 140  
 141      $graph->x_data           = $days;
 142  
 143      $graph->y_data['logs']   = $logs;
 144      $graph->y_order = array('logs');
 145  
 146      if (!empty($CFG->preferlinegraphs)) {
 147          $graph->y_format['logs'] = array('colour' => 'blue', 'line' => 'line');
 148      } else {
 149          $graph->y_format['logs'] = array('colour' => 'blue', 'bar' => 'fill', 'bar_size' => 0.6);
 150          $graph->parameter['bar_spacing'] = 0;
 151      }
 152  
 153  
 154      $graph->parameter['y_label_left'] = get_string("hits");
 155      $graph->parameter['label_size'] = "12";
 156      $graph->parameter['x_axis_angle'] = 90;
 157      $graph->parameter['x_label_angle'] = 0;
 158      $graph->parameter['tick_length'] = 0;
 159      $graph->parameter['shadow'] = 'none';
 160  
 161      error_reporting(5); // Ignore most warnings such as font problems etc.
 162      $graph->draw_stack();
 163  
 164  } else {
 165  
 166      $site = get_site();
 167  
 168      if ($course->id == $site->id) {
 169          $courseselect = 0;
 170      } else {
 171          $courseselect = $course->id;
 172      }
 173  
 174      if ($date) {
 175          $daystart = usergetmidnight($date);
 176      } else {
 177          $daystart = usergetmidnight(time());
 178      }
 179      $dayfinish = $daystart + 86400;
 180  
 181      $hours = array();
 182      for ($i = 0; $i <= 23; $i++) {
 183          $logs[$i] = 0;
 184          $hour = $daystart + $i * 3600;
 185          $hours[$i] = $i;
 186      }
 187  
 188      $rawlogs = report_log_userday($user->id, $courseselect, $daystart, $logreader);
 189  
 190      if (empty($rawlogs)) {
 191          return;
 192      }
 193  
 194      foreach ($rawlogs as $rawlog) {
 195          $logs[$rawlog->hour] = $rawlog->num;
 196      }
 197  
 198      $graph = new graph(750, 400);
 199  
 200      $a = new stdClass();
 201      $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
 202      $a->username = fullname($user, true);
 203  
 204      $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);
 205      $graph->x_data = $hours;
 206      $graph->y_data['logs'] = $logs;
 207      $graph->y_order = array('logs');
 208  
 209      if (!empty($CFG->preferlinegraphs)) {
 210          $graph->y_format['logs'] = array('colour' => 'blue', 'line' => 'line');
 211      } else {
 212          $graph->y_format['logs'] = array('colour' => 'blue', 'bar' => 'fill', 'bar_size' => 0.9);
 213      }
 214  
 215      $graph->parameter['y_label_left'] = get_string("hits");
 216      $graph->parameter['label_size'] = "12";
 217      $graph->parameter['x_axis_angle'] = 0;
 218      $graph->parameter['x_label_angle'] = 0;
 219      $graph->parameter['shadow'] = 'none';
 220  
 221      error_reporting(5); // Ignore most warnings such as font problems etc.
 222      $graph->draw_stack();
 223  }
 224  


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