Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 * Log report renderer. 19 * 20 * @package report_log 21 * @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die; 26 use core\log\manager; 27 28 /** 29 * Report log renderable class. 30 * 31 * @package report_log 32 * @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class report_log_renderable implements renderable { 36 /** @var manager log manager */ 37 protected $logmanager; 38 39 /** @var string selected log reader pluginname */ 40 public $selectedlogreader = null; 41 42 /** @var int page number */ 43 public $page; 44 45 /** @var int perpage records to show */ 46 public $perpage; 47 48 /** @var stdClass course record */ 49 public $course; 50 51 /** @var moodle_url url of report page */ 52 public $url; 53 54 /** @var int selected date from which records should be displayed */ 55 public $date; 56 57 /** @var int selected user id for which logs are displayed */ 58 public $userid; 59 60 /** @var int selected moduleid */ 61 public $modid; 62 63 /** @var string selected action filter */ 64 public $action; 65 66 /** @var int educational level */ 67 public $edulevel; 68 69 /** @var bool show courses */ 70 public $showcourses; 71 72 /** @var bool show users */ 73 public $showusers; 74 75 /** @var bool show report */ 76 public $showreport; 77 78 /** @var bool show selector form */ 79 public $showselectorform; 80 81 /** @var string selected log format */ 82 public $logformat; 83 84 /** @var string order to sort */ 85 public $order; 86 87 /** @var string origin to filter event origin */ 88 public $origin; 89 90 /** @var int group id */ 91 public $groupid; 92 93 /** @var table_log table log which will be used for rendering logs */ 94 public $tablelog; 95 96 /** 97 * Constructor. 98 * 99 * @param string $logreader (optional)reader pluginname from which logs will be fetched. 100 * @param stdClass|int $course (optional) course record or id 101 * @param int $userid (optional) id of user to filter records for. 102 * @param int|string $modid (optional) module id or site_errors for filtering errors. 103 * @param string $action (optional) action name to filter. 104 * @param int $groupid (optional) groupid of user. 105 * @param int $edulevel (optional) educational level. 106 * @param bool $showcourses (optional) show courses. 107 * @param bool $showusers (optional) show users. 108 * @param bool $showreport (optional) show report. 109 * @param bool $showselectorform (optional) show selector form. 110 * @param moodle_url|string $url (optional) page url. 111 * @param int $date date (optional) timestamp of start of the day for which logs will be displayed. 112 * @param string $logformat log format. 113 * @param int $page (optional) page number. 114 * @param int $perpage (optional) number of records to show per page. 115 * @param string $order (optional) sortorder of fetched records 116 */ 117 public function __construct($logreader = "", $course = 0, $userid = 0, $modid = 0, $action = "", $groupid = 0, $edulevel = -1, 118 $showcourses = false, $showusers = false, $showreport = true, $showselectorform = true, $url = "", $date = 0, 119 $logformat='showashtml', $page = 0, $perpage = 100, $order = "timecreated ASC", $origin ='') { 120 121 global $PAGE; 122 123 // Use first reader as selected reader, if not passed. 124 if (empty($logreader)) { 125 $readers = $this->get_readers(); 126 if (!empty($readers)) { 127 reset($readers); 128 $logreader = key($readers); 129 } else { 130 $logreader = null; 131 } 132 } 133 // Use page url if empty. 134 if (empty($url)) { 135 $url = new moodle_url($PAGE->url); 136 } else { 137 $url = new moodle_url($url); 138 } 139 $this->selectedlogreader = $logreader; 140 $url->param('logreader', $logreader); 141 142 // Use site course id, if course is empty. 143 if (!empty($course) && is_int($course)) { 144 $course = get_course($course); 145 } 146 $this->course = $course; 147 148 $this->userid = $userid; 149 $this->date = $date; 150 $this->page = $page; 151 $this->perpage = $perpage; 152 $this->url = $url; 153 $this->order = $order; 154 $this->modid = $modid; 155 $this->action = $action; 156 $this->groupid = $groupid; 157 $this->edulevel = $edulevel; 158 $this->showcourses = $showcourses; 159 $this->showusers = $showusers; 160 $this->showreport = $showreport; 161 $this->showselectorform = $showselectorform; 162 $this->logformat = $logformat; 163 $this->origin = $origin; 164 } 165 166 /** 167 * Get a list of enabled sql_reader objects/name 168 * 169 * @param bool $nameonly if true only reader names will be returned. 170 * @return array core\log\sql_reader object or name. 171 */ 172 public function get_readers($nameonly = false) { 173 if (!isset($this->logmanager)) { 174 $this->logmanager = get_log_manager(); 175 } 176 177 $readers = $this->logmanager->get_readers('core\log\sql_reader'); 178 if ($nameonly) { 179 foreach ($readers as $pluginname => $reader) { 180 $readers[$pluginname] = $reader->get_name(); 181 } 182 } 183 return $readers; 184 } 185 186 /** 187 * Helper function to return list of activities to show in selection filter. 188 * 189 * @return array list of activities. 190 */ 191 public function get_activities_list() { 192 $activities = array(); 193 194 // For site just return site errors option. 195 $sitecontext = context_system::instance(); 196 if ($this->course->id == SITEID && has_capability('report/log:view', $sitecontext)) { 197 $activities["site_errors"] = get_string("siteerrors"); 198 return $activities; 199 } 200 201 $modinfo = get_fast_modinfo($this->course); 202 if (!empty($modinfo->cms)) { 203 $section = 0; 204 $thissection = array(); 205 foreach ($modinfo->cms as $cm) { 206 // Exclude activities that aren't visible or have no view link (e.g. label). Account for folders displayed inline. 207 if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) { 208 continue; 209 } 210 if ($cm->sectionnum > 0 and $section <> $cm->sectionnum) { 211 $activities[] = $thissection; 212 $thissection = array(); 213 } 214 $section = $cm->sectionnum; 215 $modname = strip_tags($cm->get_formatted_name()); 216 if (core_text::strlen($modname) > 55) { 217 $modname = core_text::substr($modname, 0, 50)."..."; 218 } 219 if (!$cm->visible) { 220 $modname = "(".$modname.")"; 221 } 222 $key = get_section_name($this->course, $cm->sectionnum); 223 if (!isset($thissection[$key])) { 224 $thissection[$key] = array(); 225 } 226 $thissection[$key][$cm->id] = $modname; 227 } 228 if (!empty($thissection)) { 229 $activities[] = $thissection; 230 } 231 } 232 return $activities; 233 } 234 235 /** 236 * Helper function to get selected group. 237 * 238 * @return int selected group. 239 */ 240 public function get_selected_group() { 241 global $SESSION, $USER; 242 243 // No groups for system. 244 if (empty($this->course)) { 245 return 0; 246 } 247 248 $context = context_course::instance($this->course->id); 249 250 $selectedgroup = 0; 251 // Setup for group handling. 252 $groupmode = groups_get_course_groupmode($this->course); 253 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { 254 $selectedgroup = -1; 255 } else if ($groupmode) { 256 $selectedgroup = $this->groupid; 257 } else { 258 $selectedgroup = 0; 259 } 260 261 if ($selectedgroup === -1) { 262 if (isset($SESSION->currentgroup[$this->course->id])) { 263 $selectedgroup = $SESSION->currentgroup[$this->course->id]; 264 } else { 265 $selectedgroup = groups_get_all_groups($this->course->id, $USER->id); 266 if (is_array($selectedgroup)) { 267 $groupids = array_keys($selectedgroup); 268 $selectedgroup = array_shift($groupids); 269 $SESSION->currentgroup[$this->course->id] = $selectedgroup; 270 } else { 271 $selectedgroup = 0; 272 } 273 } 274 } 275 return $selectedgroup; 276 } 277 278 /** 279 * Return list of actions for log reader. 280 * 281 * @todo MDL-44528 Get list from log_store. 282 * @return array list of action options. 283 */ 284 public function get_actions() { 285 $actions = array( 286 'c' => get_string('create'), 287 'r' => get_string('view'), 288 'u' => get_string('update'), 289 'd' => get_string('delete'), 290 'cud' => get_string('allchanges') 291 ); 292 return $actions; 293 } 294 295 /** 296 * Return selected user fullname. 297 * 298 * @return string user fullname. 299 */ 300 public function get_selected_user_fullname() { 301 $user = core_user::get_user($this->userid); 302 if (empty($this->course)) { 303 // We are in system context. 304 $context = context_system::instance(); 305 } else { 306 // We are in course context. 307 $context = context_course::instance($this->course->id); 308 } 309 return fullname($user, has_capability('moodle/site:viewfullnames', $context)); 310 } 311 312 /** 313 * Return list of courses to show in selector. 314 * 315 * @return array list of courses. 316 */ 317 public function get_course_list() { 318 global $DB, $SITE; 319 320 $courses = array(); 321 322 $sitecontext = context_system::instance(); 323 // First check to see if we can override showcourses and showusers. 324 $numcourses = $DB->count_records("course"); 325 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$this->showcourses) { 326 $this->showcourses = 1; 327 } 328 329 // Check if course filter should be shown. 330 if (has_capability('report/log:view', $sitecontext) && $this->showcourses) { 331 if ($courserecords = $DB->get_records("course", null, "fullname", "id,shortname,fullname,category")) { 332 foreach ($courserecords as $course) { 333 if ($course->id == SITEID) { 334 $courses[$course->id] = format_string($course->fullname) . ' (' . get_string('site') . ')'; 335 } else { 336 $courses[$course->id] = format_string(get_course_display_name_for_list($course)); 337 } 338 } 339 } 340 core_collator::asort($courses); 341 } 342 return $courses; 343 } 344 345 /** 346 * Return list of groups. 347 * 348 * @return array list of groups. 349 */ 350 public function get_group_list() { 351 352 // No groups for system. 353 if (empty($this->course)) { 354 return array(); 355 } 356 357 $context = context_course::instance($this->course->id); 358 $groups = array(); 359 $groupmode = groups_get_course_groupmode($this->course); 360 if (($groupmode == VISIBLEGROUPS) || 361 ($groupmode == SEPARATEGROUPS and has_capability('moodle/site:accessallgroups', $context))) { 362 // Get all groups. 363 if ($cgroups = groups_get_all_groups($this->course->id)) { 364 foreach ($cgroups as $cgroup) { 365 $groups[$cgroup->id] = $cgroup->name; 366 } 367 } 368 } 369 return $groups; 370 } 371 372 /** 373 * Return list of users. 374 * 375 * @return array list of users. 376 */ 377 public function get_user_list() { 378 global $CFG, $SITE; 379 380 $courseid = $SITE->id; 381 if (!empty($this->course)) { 382 $courseid = $this->course->id; 383 } 384 $context = context_course::instance($courseid); 385 $limitfrom = empty($this->showusers) ? 0 : ''; 386 $limitnum = empty($this->showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : ''; 387 $courseusers = get_enrolled_users($context, '', $this->groupid, 'u.id, ' . get_all_user_name_fields(true, 'u'), 388 null, $limitfrom, $limitnum); 389 390 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$this->showusers) { 391 $this->showusers = 1; 392 } 393 394 $users = array(); 395 if ($this->showusers) { 396 if ($courseusers) { 397 foreach ($courseusers as $courseuser) { 398 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context)); 399 } 400 } 401 $users[$CFG->siteguest] = get_string('guestuser'); 402 } 403 return $users; 404 } 405 406 /** 407 * Return list of date options. 408 * 409 * @return array date options. 410 */ 411 public function get_date_options() { 412 global $SITE; 413 414 $strftimedate = get_string("strftimedate"); 415 $strftimedaydate = get_string("strftimedaydate"); 416 417 // Get all the possible dates. 418 // Note that we are keeping track of real (GMT) time and user time. 419 // User time is only used in displays - all calcs and passing is GMT. 420 $timenow = time(); // GMT. 421 422 // What day is it now for the user, and when is midnight that day (in GMT). 423 $timemidnight = usergetmidnight($timenow); 424 425 // Put today up the top of the list. 426 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) ); 427 428 // If course is empty, get it from frontpage. 429 $course = $SITE; 430 if (!empty($this->course)) { 431 $course = $this->course; 432 } 433 if (!$course->startdate or ($course->startdate > $timenow)) { 434 $course->startdate = $course->timecreated; 435 } 436 437 $numdates = 1; 438 while ($timemidnight > $course->startdate and $numdates < 365) { 439 $timemidnight = $timemidnight - 86400; 440 $timenow = $timenow - 86400; 441 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); 442 $numdates++; 443 } 444 return $dates; 445 } 446 447 /** 448 * Return list of components to show in selector. 449 * 450 * @return array list of origins. 451 */ 452 public function get_origin_options() { 453 $ret = array(); 454 $ret[''] = get_string('allsources', 'report_log'); 455 $ret['cli'] = get_string('cli', 'report_log'); 456 $ret['restore'] = get_string('restore', 'report_log'); 457 $ret['web'] = get_string('web', 'report_log'); 458 $ret['ws'] = get_string('ws', 'report_log'); 459 $ret['---'] = get_string('other', 'report_log'); 460 return $ret; 461 } 462 463 /** 464 * Return list of edulevel. 465 * 466 * @todo MDL-44528 Get list from log_store. 467 * @return array list of edulevels. 468 */ 469 public function get_edulevel_options() { 470 $edulevels = array( 471 -1 => get_string("edulevel"), 472 1 => get_string('edulevelteacher'), 473 2 => get_string('edulevelparticipating'), 474 0 => get_string('edulevelother') 475 ); 476 return $edulevels; 477 } 478 479 /** 480 * Setup table log. 481 */ 482 public function setup_table() { 483 $readers = $this->get_readers(); 484 485 $filter = new \stdClass(); 486 if (!empty($this->course)) { 487 $filter->courseid = $this->course->id; 488 } else { 489 $filter->courseid = 0; 490 } 491 492 $filter->userid = $this->userid; 493 $filter->modid = $this->modid; 494 $filter->groupid = $this->get_selected_group(); 495 $filter->logreader = $readers[$this->selectedlogreader]; 496 $filter->edulevel = $this->edulevel; 497 $filter->action = $this->action; 498 $filter->date = $this->date; 499 $filter->orderby = $this->order; 500 $filter->origin = $this->origin; 501 // If showing site_errors. 502 if ('site_errors' === $this->modid) { 503 $filter->siteerrors = true; 504 $filter->modid = 0; 505 } 506 507 $this->tablelog = new report_log_table_log('report_log', $filter); 508 $this->tablelog->define_baseurl($this->url); 509 $this->tablelog->is_downloadable(true); 510 $this->tablelog->show_download_buttons_at(array(TABLE_P_BOTTOM)); 511 } 512 513 /** 514 * Download logs in specified format. 515 */ 516 public function download() { 517 $filename = 'logs_' . userdate(time(), get_string('backupnameformat', 'langconfig'), 99, false); 518 if ($this->course->id !== SITEID) { 519 $courseshortname = format_string($this->course->shortname, true, 520 array('context' => context_course::instance($this->course->id))); 521 $filename = clean_filename('logs_' . $courseshortname . '_' . userdate(time(), 522 get_string('backupnameformat', 'langconfig'), 99, false)); 523 } 524 $this->tablelog->is_downloading($this->logformat, $filename); 525 $this->tablelog->out($this->perpage, false); 526 } 527 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body