[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/report/log/ -> locallib.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 contains functions used by the log reports
  19   *
  20   * This files lists the functions that are used during the log report generation.
  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  defined('MOODLE_INTERNAL') || die;
  28  
  29  if (!defined('REPORT_LOG_MAX_DISPLAY')) {
  30      define('REPORT_LOG_MAX_DISPLAY', 150); // days
  31  }
  32  
  33  require_once(dirname(__FILE__).'/lib.php');
  34  
  35  /**
  36   * This function is used to generate and display the log activity graph
  37   *
  38   * @global stdClass $CFG
  39   * @param  stdClass $course course instance
  40   * @param  int    $userid id of the user whose logs are needed
  41   * @param  string $type type of logs graph needed (usercourse.png/userday.png)
  42   * @param  int    $date timestamp in GMT (seconds since epoch)
  43   * @param  string $logreader Log reader.
  44   * @return void
  45   */
  46  function report_log_print_graph($course, $userid, $type, $date=0, $logreader='') {
  47      $logmanager = get_log_manager();
  48      $readers = $logmanager->get_readers();
  49  
  50      if (empty($logreader)) {
  51          $reader = reset($readers);
  52      } else {
  53          $reader = $readers[$logreader];
  54      }
  55      // If reader is not a sql_internal_reader and not legacy store then don't show graph.
  56      if (!($reader instanceof \core\log\sql_internal_reader) && !($reader instanceof logstore_legacy\log\store)) {
  57          return array();
  58      }
  59  
  60      $url = new moodle_url('/report/log/graph.php', array('id' => $course->id, 'user' => $userid, 'type' => $type,
  61          'date' => $date, 'logreader' => $logreader));
  62      echo html_writer::empty_tag('img', array('src' => $url, 'alt' => ''));
  63  }
  64  
  65  /**
  66   * Select all log records for a given course and user
  67   *
  68   * @param int $userid The id of the user as found in the 'user' table.
  69   * @param int $courseid The id of the course as found in the 'course' table.
  70   * @param string $coursestart unix timestamp representing course start date and time.
  71   * @param string $logreader log reader to use.
  72   * @return array
  73   */
  74  function report_log_usercourse($userid, $courseid, $coursestart, $logreader = '') {
  75      global $DB;
  76  
  77      $logmanager = get_log_manager();
  78      $readers = $logmanager->get_readers();
  79      if (empty($logreader)) {
  80          $reader = reset($readers);
  81      } else {
  82          $reader = $readers[$logreader];
  83      }
  84  
  85      // If reader is not a sql_internal_reader and not legacy store then return.
  86      if (!($reader instanceof \core\log\sql_internal_reader) && !($reader instanceof logstore_legacy\log\store)) {
  87          return array();
  88      }
  89  
  90      $coursestart = (int)$coursestart; // Note: unfortunately pg complains if you use name parameter or column alias in GROUP BY.
  91      if ($reader instanceof logstore_legacy\log\store) {
  92          $logtable = 'log';
  93          $timefield = 'time';
  94          $coursefield = 'course';
  95          // Anonymous actions are never logged in legacy log.
  96          $nonanonymous = '';
  97      } else {
  98          $logtable = $reader->get_internal_log_table_name();
  99          $timefield = 'timecreated';
 100          $coursefield = 'courseid';
 101          $nonanonymous = 'AND anonymous = 0';
 102      }
 103  
 104      $params = array();
 105      $courseselect = '';
 106      if ($courseid) {
 107          $courseselect = "AND $coursefield = :courseid";
 108          $params['courseid'] = $courseid;
 109      }
 110      $params['userid'] = $userid;
 111      return $DB->get_records_sql("SELECT FLOOR(($timefield - $coursestart)/" . DAYSECS . ") AS day, COUNT(*) AS num
 112                                     FROM {" . $logtable . "}
 113                                    WHERE userid = :userid
 114                                          AND $timefield > $coursestart $courseselect $nonanonymous
 115                                 GROUP BY FLOOR(($timefield - $coursestart)/" . DAYSECS .")", $params);
 116  }
 117  
 118  /**
 119   * Select all log records for a given course, user, and day
 120   *
 121   * @param int $userid The id of the user as found in the 'user' table.
 122   * @param int $courseid The id of the course as found in the 'course' table.
 123   * @param string $daystart unix timestamp of the start of the day for which the logs needs to be retrived
 124   * @param string $logreader log reader to use.
 125   * @return array
 126   */
 127  function report_log_userday($userid, $courseid, $daystart, $logreader = '') {
 128      global $DB;
 129      $logmanager = get_log_manager();
 130      $readers = $logmanager->get_readers();
 131      if (empty($logreader)) {
 132          $reader = reset($readers);
 133      } else {
 134          $reader = $readers[$logreader];
 135      }
 136  
 137      // If reader is not a sql_internal_reader and not legacy store then return.
 138      if (!($reader instanceof \core\log\sql_internal_reader) && !($reader instanceof logstore_legacy\log\store)) {
 139          return array();
 140      }
 141  
 142      $daystart = (int)$daystart; // Note: unfortunately pg complains if you use name parameter or column alias in GROUP BY.
 143  
 144      if ($reader instanceof logstore_legacy\log\store) {
 145          $logtable = 'log';
 146          $timefield = 'time';
 147          $coursefield = 'course';
 148          // Anonymous actions are never logged in legacy log.
 149          $nonanonymous = '';
 150      } else {
 151          $logtable = $reader->get_internal_log_table_name();
 152          $timefield = 'timecreated';
 153          $coursefield = 'courseid';
 154          $nonanonymous = 'AND anonymous = 0';
 155      }
 156      $params = array('userid' => $userid);
 157  
 158      $courseselect = '';
 159      if ($courseid) {
 160          $courseselect = "AND $coursefield = :courseid";
 161          $params['courseid'] = $courseid;
 162      }
 163      return $DB->get_records_sql("SELECT FLOOR(($timefield - $daystart)/" . HOURSECS . ") AS hour, COUNT(*) AS num
 164                                     FROM {" . $logtable . "}
 165                                    WHERE userid = :userid
 166                                          AND $timefield > $daystart $courseselect $nonanonymous
 167                                 GROUP BY FLOOR(($timefield - $daystart)/" . HOURSECS . ") ", $params);
 168  }
 169  
 170  /**
 171   * This function is used to generate and display Mnet selector form
 172   *
 173   * @global stdClass $USER
 174   * @global stdClass $CFG
 175   * @global stdClass $SITE
 176   * @global moodle_database $DB
 177   * @global core_renderer $OUTPUT
 178   * @global stdClass $SESSION
 179   * @uses CONTEXT_SYSTEM
 180   * @uses COURSE_MAX_COURSES_PER_DROPDOWN
 181   * @uses CONTEXT_COURSE
 182   * @uses SEPARATEGROUPS
 183   * @param  int      $hostid host id
 184   * @param  stdClass $course course instance
 185   * @param  int      $selecteduser id of the selected user
 186   * @param  string   $selecteddate Date selected
 187   * @param  string   $modname course_module->id
 188   * @param  string   $modid number or 'site_errors'
 189   * @param  string   $modaction an action as recorded in the logs
 190   * @param  int      $selectedgroup Group to display
 191   * @param  int      $showcourses whether to show courses if we're over our limit.
 192   * @param  int      $showusers whether to show users if we're over our limit.
 193   * @param  string   $logformat Format of the logs (downloadascsv, showashtml, downloadasods, downloadasexcel)
 194   * @return void
 195   */
 196  function report_log_print_mnet_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today',
 197                                   $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
 198  
 199      global $USER, $CFG, $SITE, $DB, $OUTPUT, $SESSION;
 200      require_once $CFG->dirroot.'/mnet/peer.php';
 201  
 202      $mnet_peer = new mnet_peer();
 203      $mnet_peer->set_id($hostid);
 204  
 205      $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}";
 206      $courses = $DB->get_records_sql($sql);
 207      $remotecoursecount = count($courses);
 208  
 209      // first check to see if we can override showcourses and showusers
 210      $numcourses = $remotecoursecount + $DB->count_records('course');
 211      if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
 212          $showcourses = 1;
 213      }
 214  
 215      $sitecontext = context_system::instance();
 216  
 217      // Context for remote data is always SITE
 218      // Groups for remote data are always OFF
 219      if ($hostid == $CFG->mnet_localhost_id) {
 220          $context = context_course::instance($course->id);
 221  
 222          /// Setup for group handling.
 223          if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
 224              $selectedgroup = -1;
 225              $showgroups = false;
 226          } else if ($course->groupmode) {
 227              $showgroups = true;
 228          } else {
 229              $selectedgroup = 0;
 230              $showgroups = false;
 231          }
 232  
 233          if ($selectedgroup === -1) {
 234              if (isset($SESSION->currentgroup[$course->id])) {
 235                  $selectedgroup =  $SESSION->currentgroup[$course->id];
 236              } else {
 237                  $selectedgroup = groups_get_all_groups($course->id, $USER->id);
 238                  if (is_array($selectedgroup)) {
 239                      $selectedgroup = array_shift(array_keys($selectedgroup));
 240                      $SESSION->currentgroup[$course->id] = $selectedgroup;
 241                  } else {
 242                      $selectedgroup = 0;
 243                  }
 244              }
 245          }
 246  
 247      } else {
 248          $context = $sitecontext;
 249      }
 250  
 251      // Get all the possible users
 252      $users = array();
 253  
 254      // Define limitfrom and limitnum for queries below
 255      // If $showusers is enabled... don't apply limitfrom and limitnum
 256      $limitfrom = empty($showusers) ? 0 : '';
 257      $limitnum  = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : '';
 258  
 259      // If looking at a different host, we're interested in all our site users
 260      if ($hostid == $CFG->mnet_localhost_id && $course->id != SITEID) {
 261          $courseusers = get_enrolled_users($context, '', $selectedgroup, 'u.id, ' . get_all_user_name_fields(true, 'u'),
 262                  null, $limitfrom, $limitnum);
 263      } else {
 264          // this may be a lot of users :-(
 265          $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, ' . get_all_user_name_fields(true),
 266                  $limitfrom, $limitnum);
 267      }
 268  
 269      if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
 270          $showusers = 1;
 271      }
 272  
 273      if ($showusers) {
 274          if ($courseusers) {
 275              foreach ($courseusers as $courseuser) {
 276                  $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
 277              }
 278          }
 279          $users[$CFG->siteguest] = get_string('guestuser');
 280      }
 281  
 282      // Get all the hosts that have log records
 283      $sql = "select distinct
 284                  h.id,
 285                  h.name
 286              from
 287                  {mnet_host} h,
 288                  {mnet_log} l
 289              where
 290                  h.id = l.hostid
 291              order by
 292                  h.name";
 293  
 294      if ($hosts = $DB->get_records_sql($sql)) {
 295          foreach($hosts as $host) {
 296              $hostarray[$host->id] = $host->name;
 297          }
 298      }
 299  
 300      $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname;
 301      asort($hostarray);
 302  
 303      $dropdown = array();
 304  
 305      foreach($hostarray as $hostid => $name) {
 306          $courses = array();
 307          $sites = array();
 308          if ($CFG->mnet_localhost_id == $hostid) {
 309              if (has_capability('report/log:view', $sitecontext) && $showcourses) {
 310                  if ($ccc = $DB->get_records("course", null, "fullname","id,shortname,fullname,category")) {
 311                      foreach ($ccc as $cc) {
 312                          if ($cc->id == SITEID) {
 313                              $sites["$hostid/$cc->id"]   = format_string($cc->fullname).' ('.get_string('site').')';
 314                          } else {
 315                              $courses["$hostid/$cc->id"] = format_string(get_course_display_name_for_list($cc));
 316                          }
 317                      }
 318                  }
 319              }
 320          } else {
 321              if (has_capability('report/log:view', $sitecontext) && $showcourses) {
 322                  $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?";
 323                  if ($ccc = $DB->get_records_sql($sql, array($hostid))) {
 324                      foreach ($ccc as $cc) {
 325                          if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id
 326                              $sites["$hostid/$cc->course"]   = $cc->coursename.' ('.get_string('site').')';
 327                          } else {
 328                              $courses["$hostid/$cc->course"] = $cc->coursename;
 329                          }
 330                      }
 331                  }
 332              }
 333          }
 334  
 335          asort($courses);
 336          $dropdown[] = array($name=>($sites + $courses));
 337      }
 338  
 339  
 340      $activities = array();
 341      $selectedactivity = "";
 342  
 343      $modinfo = get_fast_modinfo($course);
 344      if (!empty($modinfo->cms)) {
 345          $section = 0;
 346          $thissection = array();
 347          foreach ($modinfo->cms as $cm) {
 348              if (!$cm->uservisible || !$cm->has_view()) {
 349                  continue;
 350              }
 351              if ($cm->sectionnum > 0 and $section <> $cm->sectionnum) {
 352                  $activities[] = $thissection;
 353                  $thissection = array();
 354              }
 355              $section = $cm->sectionnum;
 356              $modname = strip_tags($cm->get_formatted_name());
 357              if (core_text::strlen($modname) > 55) {
 358                  $modname = core_text::substr($modname, 0, 50)."...";
 359              }
 360              if (!$cm->visible) {
 361                  $modname = "(".$modname.")";
 362              }
 363              $key = get_section_name($course, $cm->sectionnum);
 364              if (!isset($thissection[$key])) {
 365                  $thissection[$key] = array();
 366              }
 367              $thissection[$key][$cm->id] = $modname;
 368  
 369              if ($cm->id == $modid) {
 370                  $selectedactivity = "$cm->id";
 371              }
 372          }
 373          if (!empty($thissection)) {
 374              $activities[] = $thissection;
 375          }
 376      }
 377  
 378      if (has_capability('report/log:view', $sitecontext) && !$course->category) {
 379          $activities["site_errors"] = get_string("siteerrors");
 380          if ($modid === "site_errors") {
 381              $selectedactivity = "site_errors";
 382          }
 383      }
 384  
 385      $strftimedate = get_string("strftimedate");
 386      $strftimedaydate = get_string("strftimedaydate");
 387  
 388      asort($users);
 389  
 390      // Prepare the list of action options.
 391      $actions = array(
 392          'view' => get_string('view'),
 393          'add' => get_string('add'),
 394          'update' => get_string('update'),
 395          'delete' => get_string('delete'),
 396          '-view' => get_string('allchanges')
 397      );
 398  
 399      // Get all the possible dates
 400      // Note that we are keeping track of real (GMT) time and user time
 401      // User time is only used in displays - all calcs and passing is GMT
 402  
 403      $timenow = time(); // GMT
 404  
 405      // What day is it now for the user, and when is midnight that day (in GMT).
 406      $timemidnight = $today = usergetmidnight($timenow);
 407  
 408      // Put today up the top of the list
 409      $dates = array(
 410          "0" => get_string('alldays'),
 411          "$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate)
 412      );
 413  
 414      if (!$course->startdate or ($course->startdate > $timenow)) {
 415          $course->startdate = $course->timecreated;
 416      }
 417  
 418      $numdates = 1;
 419      while ($timemidnight > $course->startdate and $numdates < 365) {
 420          $timemidnight = $timemidnight - 86400;
 421          $timenow = $timenow - 86400;
 422          $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
 423          $numdates++;
 424      }
 425  
 426      if ($selecteddate === "today") {
 427          $selecteddate = $today;
 428      }
 429  
 430      echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/report/log/index.php\" method=\"get\">\n";
 431      echo "<div>\n";//invisible fieldset here breaks wrapping
 432      echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
 433      echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
 434      echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
 435      if (has_capability('report/log:view', $sitecontext) && $showcourses) {
 436          $cid = empty($course->id)? '1' : $course->id;
 437          echo html_writer::label(get_string('selectacoursesite'), 'menuhost_course', false, array('class' => 'accesshide'));
 438          echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid);
 439      } else {
 440          $courses = array();
 441          $courses[$course->id] = get_course_display_name_for_list($course) . ((empty($course->category)) ? ' ('.get_string('site').') ' : '');
 442          echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide'));
 443          echo html_writer::select($courses,"id",$course->id, false);
 444          if (has_capability('report/log:view', $sitecontext)) {
 445              $a = new stdClass();
 446              $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
 447                  ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
 448              print_string('logtoomanycourses','moodle',$a);
 449          }
 450      }
 451  
 452      if ($showgroups) {
 453          if ($cgroups = groups_get_all_groups($course->id)) {
 454              foreach ($cgroups as $cgroup) {
 455                  $groups[$cgroup->id] = $cgroup->name;
 456              }
 457          }
 458          else {
 459              $groups = array();
 460          }
 461          echo html_writer::label(get_string('selectagroup'), 'menugroup', false, array('class' => 'accesshide'));
 462          echo html_writer::select($groups, "group", $selectedgroup, get_string("allgroups"));
 463      }
 464  
 465      if ($showusers) {
 466          echo html_writer::label(get_string('participantslist'), 'menuuser', false, array('class' => 'accesshide'));
 467          echo html_writer::select($users, "user", $selecteduser, get_string("allparticipants"));
 468      }
 469      else {
 470          $users = array();
 471          if (!empty($selecteduser)) {
 472              $user = $DB->get_record('user', array('id'=>$selecteduser));
 473              $users[$selecteduser] = fullname($user);
 474          }
 475          else {
 476              $users[0] = get_string('allparticipants');
 477          }
 478          echo html_writer::label(get_string('participantslist'), 'menuuser', false, array('class' => 'accesshide'));
 479          echo html_writer::select($users, "user", $selecteduser, false);
 480          $a = new stdClass();
 481          $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
 482              ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
 483          print_string('logtoomanyusers','moodle',$a);
 484      }
 485  
 486      echo html_writer::label(get_string('date'), 'menudate', false, array('class' => 'accesshide'));
 487      echo html_writer::select($dates, "date", $selecteddate, false);
 488      echo html_writer::label(get_string('showreports'), 'menumodid', false, array('class' => 'accesshide'));
 489      echo html_writer::select($activities, "modid", $selectedactivity, get_string("allactivities"));
 490      echo html_writer::label(get_string('actions'), 'menumodaction', false, array('class' => 'accesshide'));
 491      echo html_writer::select($actions, 'modaction', $modaction, get_string("allactions"));
 492  
 493      $logformats = array('showashtml' => get_string('displayonpage'),
 494                          'downloadascsv' => get_string('downloadtext'),
 495                          'downloadasods' => get_string('downloadods'),
 496                          'downloadasexcel' => get_string('downloadexcel'));
 497      echo html_writer::label(get_string('logsformat', 'report_log'), 'menulogformat', false, array('class' => 'accesshide'));
 498      echo html_writer::select($logformats, 'logformat', $logformat, false);
 499      echo '<input type="submit" value="'.get_string('gettheselogs').'" />';
 500      echo '</div>';
 501      echo '</form>';
 502  }


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