first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The report_log report viewed event.
*
* @package report_log
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_log\event;
defined('MOODLE_INTERNAL') || die();
/**
* The report_log report viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - int groupid: Group to display.
* - int date: Date to display logs from.
* - int modid: Module id for which logs were displayed.
* - string modaction: Module action.
* - string logformat: Log format in which logs were displayed.
* }
*
* @package report_log
* @since Moodle 2.7
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_viewed extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreportviewed', 'report_log');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the log report for the course with id '$this->courseid'.";
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/log/index.php', array('id' => $this->courseid));
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['groupid'])) {
throw new \coding_exception('The \'groupid\' value must be set in other.');
}
if (!isset($this->other['date'])) {
throw new \coding_exception('The \'date\' value must be set in other.');
}
if (!isset($this->other['modid'])) {
throw new \coding_exception('The \'modid\' value must be set in other.');
}
if (!isset($this->other['modaction'])) {
throw new \coding_exception('The \'modaction\' value must be set in other.');
}
if (!isset($this->other['logformat'])) {
throw new \coding_exception('The \'logformat\' value must be set in other.');
}
if (!isset($this->relateduserid)) {
throw new \coding_exception('The \'relateduserid\' must be set.');
}
}
public static function get_other_mapping() {
$othermapped = array();
$othermapped['modid'] = array('db' => 'course_modules', 'restore' => 'course_module');
$othermapped['groupid'] = array('db' => 'groups', 'restore' => 'group');
return $othermapped;
}
}
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The report_log user report viewed event.
*
* @package report_log
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_log\event;
defined('MOODLE_INTERNAL') || die();
/**
* The report_log user report viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - string mode: display mode.
* }
*
* @package report_log
* @since Moodle 2.7
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_report_viewed extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventuserreportviewed', 'report_log');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the user log report for the user with id '$this->relateduserid'.";
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/log/user.php', array('course' => $this->courseid, 'id' => $this->relateduserid,
'mode' => $this->other['mode']));
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (empty($this->other['mode'])) {
throw new \coding_exception('The \'mode\' value must be set in other.');
}
if (empty($this->relateduserid)) {
throw new \coding_exception('The \'relateduserid\' must be set.');
}
}
public static function get_other_mapping() {
// Nothing to map.
return false;
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for report_log.
*
* @package report_log
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_log\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for report_log implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+551
View File
@@ -0,0 +1,551 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Log report renderer.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
use core\log\manager;
/**
* Report log renderable class.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_log_renderable implements renderable {
/** @var manager log manager */
protected $logmanager;
/** @var string selected log reader pluginname */
public $selectedlogreader = null;
/** @var int page number */
public $page;
/** @var int perpage records to show */
public $perpage;
/** @var stdClass course record */
public $course;
/** @var moodle_url url of report page */
public $url;
/** @var int selected date from which records should be displayed */
public $date;
/** @var int selected user id for which logs are displayed */
public $userid;
/** @var int selected moduleid */
public $modid;
/** @var string selected action filter */
public $action;
/** @var int educational level */
public $edulevel;
/** @var bool show courses */
public $showcourses;
/** @var bool show users */
public $showusers;
/** @var bool show report */
public $showreport;
/** @var bool show selector form */
public $showselectorform;
/** @var string selected log format */
public $logformat;
/** @var string order to sort */
public $order;
/** @var string origin to filter event origin */
public $origin;
/** @var int group id */
public $groupid;
/** @var table_log table log which will be used for rendering logs */
public $tablelog;
/**
* @var array group ids
* @deprecated since Moodle 4.4 - please do not use this public property
* @todo MDL-81155 remove this property as it is not used anymore.
*/
public $grouplist;
/**
* Constructor.
*
* @param string $logreader (optional)reader pluginname from which logs will be fetched.
* @param stdClass|int $course (optional) course record or id
* @param int $userid (optional) id of user to filter records for.
* @param int|string $modid (optional) module id or site_errors for filtering errors.
* @param string $action (optional) action name to filter.
* @param int $groupid (optional) groupid of user.
* @param int $edulevel (optional) educational level.
* @param bool $showcourses (optional) show courses.
* @param bool $showusers (optional) show users.
* @param bool $showreport (optional) show report.
* @param bool $showselectorform (optional) show selector form.
* @param moodle_url|string $url (optional) page url.
* @param int $date date (optional) timestamp of start of the day for which logs will be displayed.
* @param string $logformat log format.
* @param int $page (optional) page number.
* @param int $perpage (optional) number of records to show per page.
* @param string $order (optional) sortorder of fetched records
*/
public function __construct($logreader = "", $course = 0, $userid = 0, $modid = 0, $action = "", $groupid = 0, $edulevel = -1,
$showcourses = false, $showusers = false, $showreport = true, $showselectorform = true, $url = "", $date = 0,
$logformat='showashtml', $page = 0, $perpage = 100, $order = "timecreated ASC", $origin ='') {
global $PAGE;
// Use first reader as selected reader, if not passed.
if (empty($logreader)) {
$readers = $this->get_readers();
if (!empty($readers)) {
reset($readers);
$logreader = key($readers);
} else {
$logreader = null;
}
}
// Use page url if empty.
if (empty($url)) {
$url = new moodle_url($PAGE->url);
} else {
$url = new moodle_url($url);
}
$this->selectedlogreader = $logreader;
$url->param('logreader', $logreader);
// Use site course id, if course is empty.
if (!empty($course) && is_int($course)) {
$course = get_course($course);
}
$this->course = $course;
$this->userid = $userid;
$this->date = $date;
$this->page = $page;
$this->perpage = $perpage;
$this->url = $url;
$this->order = $order;
$this->modid = $modid;
$this->action = $action;
$this->groupid = $groupid;
$this->edulevel = $edulevel;
$this->showcourses = $showcourses;
$this->showusers = $showusers;
$this->showreport = $showreport;
$this->showselectorform = $showselectorform;
$this->logformat = $logformat;
$this->origin = $origin;
}
/**
* Get a list of enabled sql_reader objects/name
*
* @param bool $nameonly if true only reader names will be returned.
* @return array core\log\sql_reader object or name.
*/
public function get_readers($nameonly = false) {
if (!isset($this->logmanager)) {
$this->logmanager = get_log_manager();
}
$readers = $this->logmanager->get_readers('core\log\sql_reader');
if ($nameonly) {
foreach ($readers as $pluginname => $reader) {
$readers[$pluginname] = $reader->get_name();
}
}
return $readers;
}
/**
* Helper function to return list of activities to show in selection filter.
*
* @return array list of activities.
*/
public function get_activities_list() {
$activities = array();
// For site just return site errors option.
$sitecontext = context_system::instance();
if ($this->course->id == SITEID && has_capability('report/log:view', $sitecontext)) {
$activities["site_errors"] = get_string("siteerrors");
return $activities;
}
$modinfo = get_fast_modinfo($this->course);
if (!empty($modinfo->cms)) {
$section = 0;
$thissection = array();
foreach ($modinfo->cms as $cm) {
// Exclude activities that aren't visible or have no view link (e.g. label). Account for folders displayed inline.
if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) {
continue;
}
if ($cm->sectionnum > 0 and $section <> $cm->sectionnum) {
$activities[] = $thissection;
$thissection = array();
}
$section = $cm->sectionnum;
$modname = strip_tags($cm->get_formatted_name());
if (core_text::strlen($modname) > 55) {
$modname = core_text::substr($modname, 0, 50)."...";
}
if (!$cm->visible) {
$modname = "(".$modname.")";
}
$key = get_section_name($this->course, $cm->sectionnum);
if (!isset($thissection[$key])) {
$thissection[$key] = array();
}
$thissection[$key][$cm->id] = $modname;
}
if (!empty($thissection)) {
$activities[] = $thissection;
}
}
return $activities;
}
/**
* Helper function to get selected group.
*
* @return int selected group.
*/
public function get_selected_group() {
global $SESSION, $USER;
// No groups for system.
if (empty($this->course)) {
return 0;
}
$context = context_course::instance($this->course->id);
$selectedgroup = 0;
// Setup for group handling.
$groupmode = groups_get_course_groupmode($this->course);
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
if (isset($SESSION->currentgroup[$this->course->id])) {
$selectedgroup = $SESSION->currentgroup[$this->course->id];
} else if ($this->groupid > 0) {
$SESSION->currentgroup[$this->course->id] = $this->groupid;
$selectedgroup = $this->groupid;
}
} else if ($groupmode) {
$selectedgroup = $this->groupid;
}
return $selectedgroup;
}
/**
* Return list of actions for log reader.
*
* @todo MDL-44528 Get list from log_store.
* @return array list of action options.
*/
public function get_actions() {
$actions = array(
'c' => get_string('create'),
'r' => get_string('view'),
'u' => get_string('update'),
'd' => get_string('delete'),
'cud' => get_string('allchanges')
);
return $actions;
}
/**
* Return selected user fullname.
*
* @return string user fullname.
*/
public function get_selected_user_fullname() {
$user = core_user::get_user($this->userid);
if (empty($this->course)) {
// We are in system context.
$context = context_system::instance();
} else {
// We are in course context.
$context = context_course::instance($this->course->id);
}
return fullname($user, has_capability('moodle/site:viewfullnames', $context));
}
/**
* Return list of courses to show in selector.
*
* @return array list of courses.
*/
public function get_course_list() {
global $DB, $SITE;
$courses = array();
$sitecontext = context_system::instance();
// First check to see if we can override showcourses and showusers.
$numcourses = $DB->count_records("course");
if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$this->showcourses) {
$this->showcourses = 1;
}
// Check if course filter should be shown.
if (has_capability('report/log:view', $sitecontext) && $this->showcourses) {
if ($courserecords = $DB->get_records("course", null, "fullname", "id,shortname,fullname,category")) {
foreach ($courserecords as $course) {
if ($course->id == SITEID) {
$courses[$course->id] = format_string($course->fullname) . ' (' . get_string('site') . ')';
} else {
$courses[$course->id] = format_string(get_course_display_name_for_list($course));
}
}
}
core_collator::asort($courses);
}
return $courses;
}
/**
* Return list of groups that are used in this course. This is done when groups are used in the course
* and the user is allowed to see all groups or groups are visible anyway. If groups are used but the
* mode is separate groups and the user is not allowed to see all groups, the list contains the groups
* only, where the user is member.
* If the course uses no groups, the list is empty.
*
* @return array list of groups.
*/
public function get_group_list() {
global $USER;
// No groups for system.
if (empty($this->course)) {
return [];
}
$context = context_course::instance($this->course->id);
$groupmode = groups_get_course_groupmode($this->course);
$grouplist = [];
$userid = $groupmode == SEPARATEGROUPS ? $USER->id : 0;
if (has_capability('moodle/site:accessallgroups', $context)) {
$userid = 0;
}
$cgroups = groups_get_all_groups($this->course->id, $userid);
if (!empty($cgroups)) {
$grouplist = array_column($cgroups, 'name', 'id');
}
$this->grouplist = $grouplist; // Keep compatibility with MDL-41465.
return $grouplist;
}
/**
* Return list of users.
*
* @return array list of users.
*/
public function get_user_list() {
global $CFG, $SITE;
$courseid = $SITE->id;
if (!empty($this->course)) {
$courseid = $this->course->id;
}
$context = context_course::instance($courseid);
$limitfrom = empty($this->showusers) ? 0 : '';
$limitnum = empty($this->showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : '';
$userfieldsapi = \core_user\fields::for_name();
// Get the groups of that course that the user can see.
$groups = $this->get_group_list();
$groupids = array_keys($groups);
// Now doublecheck the value of groupids and deal with special case like USERWITHOUTGROUP.
$groupmode = groups_get_course_groupmode($this->course);
if (
has_capability('moodle/site:accessallgroups', $context)
|| $groupmode != SEPARATEGROUPS
|| empty($groupids)
) {
$groupids[] = USERSWITHOUTGROUP;
}
// First case, the user has selected a group and user is in this group.
if ($this->groupid > 0) {
if (!isset($groups[$this->groupid])) {
// The user is not in this group, so we will ignore the group selection.
$groupids = 0;
} else {
$groupids = [$this->groupid];
}
}
$courseusers = get_enrolled_users($context, '', $groupids, 'u.id, ' .
$userfieldsapi->get_sql('u', false, '', '', false)->selects,
null, $limitfrom, $limitnum);
if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$this->showusers) {
$this->showusers = 1;
}
$users = array();
if ($this->showusers) {
if ($courseusers) {
foreach ($courseusers as $courseuser) {
$users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
}
}
$users[$CFG->siteguest] = get_string('guestuser');
}
return $users;
}
/**
* Return list of date options.
*
* @return array date options.
*/
public function get_date_options() {
global $SITE;
$strftimedate = get_string("strftimedate");
$strftimedaydate = get_string("strftimedaydate");
// Get all the possible dates.
// Note that we are keeping track of real (GMT) time and user time.
// User time is only used in displays - all calcs and passing is GMT.
$timenow = time(); // GMT.
// What day is it now for the user, and when is midnight that day (in GMT).
$timemidnight = usergetmidnight($timenow);
// Put today up the top of the list.
$dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
// If course is empty, get it from frontpage.
$course = $SITE;
if (!empty($this->course)) {
$course = $this->course;
}
if (!$course->startdate or ($course->startdate > $timenow)) {
$course->startdate = $course->timecreated;
}
$numdates = 1;
while ($timemidnight > $course->startdate and $numdates < 365) {
$timemidnight = $timemidnight - 86400;
$timenow = $timenow - 86400;
$dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
$numdates++;
}
return $dates;
}
/**
* Return list of components to show in selector.
*
* @return array list of origins.
*/
public function get_origin_options() {
$ret = array();
$ret[''] = get_string('allsources', 'report_log');
$ret['cli'] = get_string('cli', 'report_log');
$ret['restore'] = get_string('restore', 'report_log');
$ret['web'] = get_string('web', 'report_log');
$ret['ws'] = get_string('ws', 'report_log');
$ret['---'] = get_string('other', 'report_log');
return $ret;
}
/**
* Return list of edulevel.
*
* @todo MDL-44528 Get list from log_store.
* @return array list of edulevels.
*/
public function get_edulevel_options() {
$edulevels = array(
-1 => get_string("edulevel"),
1 => get_string('edulevelteacher'),
2 => get_string('edulevelparticipating'),
0 => get_string('edulevelother')
);
return $edulevels;
}
/**
* Setup table log.
*/
public function setup_table() {
$readers = $this->get_readers();
$filter = new \stdClass();
if (!empty($this->course)) {
$filter->courseid = $this->course->id;
} else {
$filter->courseid = 0;
}
$filter->userid = $this->userid;
$filter->modid = $this->modid;
$filter->groupid = $this->get_selected_group();
$filter->logreader = $readers[$this->selectedlogreader];
$filter->edulevel = $this->edulevel;
$filter->action = $this->action;
$filter->date = $this->date;
$filter->orderby = $this->order;
$filter->origin = $this->origin;
// If showing site_errors.
if ('site_errors' === $this->modid) {
$filter->siteerrors = true;
$filter->modid = 0;
}
$this->tablelog = new report_log_table_log('report_log', $filter);
$this->tablelog->define_baseurl($this->url);
$this->tablelog->is_downloadable(true);
$this->tablelog->show_download_buttons_at(array(TABLE_P_BOTTOM));
}
/**
* Download logs in specified format.
*/
public function download() {
$filename = 'logs_' . userdate(time(), get_string('backupnameformat', 'langconfig'), 99, false);
if ($this->course->id !== SITEID) {
$courseshortname = format_string($this->course->shortname, true,
array('context' => context_course::instance($this->course->id)));
$filename = clean_filename('logs_' . $courseshortname . '_' . userdate(time(),
get_string('backupnameformat', 'langconfig'), 99, false));
}
$this->tablelog->is_downloading($this->logformat, $filename);
$this->tablelog->out($this->perpage, false);
}
}
+207
View File
@@ -0,0 +1,207 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Log report renderer.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Report log renderer's for printing reports.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_log_renderer extends plugin_renderer_base {
/**
* This method should never be manually called, it should only be called by process.
*
* @deprecated since 2.8, to be removed in 2.9
* @param report_log_renderable $reportlog
* @return string
*/
public function render_report_log_renderable(report_log_renderable $reportlog) {
debugging('Do not call this method. Please call $renderer->render($reportlog) instead.', DEBUG_DEVELOPER);
return $this->render($reportlog);
}
/**
* Render log report page.
*
* @param report_log_renderable $reportlog object of report_log.
*/
protected function render_report_log(report_log_renderable $reportlog) {
if (empty($reportlog->selectedlogreader)) {
echo $this->output->notification(get_string('nologreaderenabled', 'report_log'), 'notifyproblem');
return;
}
if ($reportlog->showselectorform) {
$this->report_selector_form($reportlog);
}
if ($reportlog->showreport) {
$reportlog->tablelog->out($reportlog->perpage, true);
}
}
/**
* Prints/return reader selector
*
* @param report_log_renderable $reportlog log report.
*/
public function reader_selector(report_log_renderable $reportlog) {
$readers = $reportlog->get_readers(true);
if (empty($readers)) {
$readers = array(get_string('nologreaderenabled', 'report_log'));
}
$url = fullclone ($reportlog->url);
$url->remove_params(array('logreader'));
$select = new single_select($url, 'logreader', $readers, $reportlog->selectedlogreader, null);
$select->set_label(get_string('selectlogreader', 'report_log'));
echo $this->output->render($select);
}
/**
* This function is used to generate and display selector form
*
* @param report_log_renderable $reportlog log report.
*/
public function report_selector_form(report_log_renderable $reportlog) {
echo html_writer::start_tag('form', array('class' => 'logselecform', 'action' => $reportlog->url, 'method' => 'get'));
echo html_writer::start_div();
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'chooselog', 'value' => '1'));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'showusers', 'value' => $reportlog->showusers));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'showcourses',
'value' => $reportlog->showcourses));
$selectedcourseid = empty($reportlog->course) ? 0 : $reportlog->course->id;
// Add course selector.
$sitecontext = context_system::instance();
$courses = $reportlog->get_course_list();
if (!empty($courses) && $reportlog->showcourses) {
echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide'));
echo html_writer::select($courses, "id", $selectedcourseid, null, ['class' => 'mr-2 mb-2']);
} else {
$courses = array();
$courses[$selectedcourseid] = get_course_display_name_for_list($reportlog->course) . (($selectedcourseid == SITEID) ?
' (' . get_string('site') . ') ' : '');
echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide'));
echo html_writer::select($courses, "id", $selectedcourseid, false, ['class' => 'mr-2 mb-2']);
// Check if user is admin and this came because of limitation on number of courses to show in dropdown.
if (has_capability('report/log:view', $sitecontext)) {
$a = new stdClass();
$a->url = new moodle_url('/report/log/index.php', array('chooselog' => 0,
'group' => $reportlog->get_selected_group(), 'user' => $reportlog->userid,
'id' => $selectedcourseid, 'date' => $reportlog->date, 'modid' => $reportlog->modid,
'showcourses' => 1, 'showusers' => $reportlog->showusers));
$a->url = $a->url->out(false);
print_string('logtoomanycourses', 'moodle', $a);
}
}
// Add group selector.
$groups = $reportlog->get_group_list();
if (!empty($groups)) {
echo html_writer::label(get_string('selectagroup'), 'menugroup', false, array('class' => 'accesshide'));
echo html_writer::select($groups, "group", $reportlog->groupid, get_string("allgroups"),
['class' => 'mr-2 mb-2']);
}
// Add user selector.
$users = $reportlog->get_user_list();
if ($reportlog->showusers) {
echo html_writer::label(get_string('selctauser'), 'menuuser', false, array('class' => 'accesshide'));
echo html_writer::select($users, "user", $reportlog->userid, get_string("allparticipants"),
['class' => 'mr-2 mb-2']);
} else {
$users = array();
if (!empty($reportlog->userid)) {
$users[$reportlog->userid] = $reportlog->get_selected_user_fullname();
} else {
$users[0] = get_string('allparticipants');
}
echo html_writer::label(get_string('selctauser'), 'menuuser', false, array('class' => 'accesshide'));
echo html_writer::select($users, "user", $reportlog->userid, false, ['class' => 'mr-2 mb-2']);
$a = new stdClass();
$a->url = new moodle_url('/report/log/index.php', array('chooselog' => 0,
'group' => $reportlog->get_selected_group(), 'user' => $reportlog->userid,
'id' => $selectedcourseid, 'date' => $reportlog->date, 'modid' => $reportlog->modid,
'showusers' => 1, 'showcourses' => $reportlog->showcourses));
$a->url = $a->url->out(false);
echo html_writer::start_span('mx-1');
print_string('logtoomanyusers', 'moodle', $a);
echo html_writer::end_span();
}
// Add date selector.
$dates = $reportlog->get_date_options();
echo html_writer::label(get_string('date'), 'menudate', false, array('class' => 'accesshide'));
echo html_writer::select($dates, "date", $reportlog->date, get_string("alldays"),
['class' => 'mr-2 mb-2']);
// Add activity selector.
$activities = $reportlog->get_activities_list();
echo html_writer::label(get_string('activities'), 'menumodid', false, array('class' => 'accesshide'));
echo html_writer::select($activities, "modid", $reportlog->modid, get_string("allactivities"),
['class' => 'mr-2 mb-2']);
// Add actions selector.
echo html_writer::label(get_string('actions'), 'menumodaction', false, array('class' => 'accesshide'));
echo html_writer::select($reportlog->get_actions(), 'modaction', $reportlog->action,
get_string("allactions"), ['class' => 'mr-2 mb-2']);
// Add origin.
$origin = $reportlog->get_origin_options();
echo html_writer::label(get_string('origin', 'report_log'), 'menuorigin', false, array('class' => 'accesshide'));
echo html_writer::select($origin, 'origin', $reportlog->origin, false, ['class' => 'mr-2 mb-2']);
// Add edulevel.
$edulevel = $reportlog->get_edulevel_options();
echo html_writer::label(get_string('edulevel'), 'menuedulevel', false, array('class' => 'accesshide'));
echo html_writer::select($edulevel, 'edulevel', $reportlog->edulevel, false,
['class' => 'mr-2 mb-2']) .$this->help_icon('edulevel');
// Add reader option.
// If there is some reader available then only show submit button.
$readers = $reportlog->get_readers(true);
if (!empty($readers)) {
if (count($readers) == 1) {
$attributes = array('type' => 'hidden', 'name' => 'logreader', 'value' => key($readers));
echo html_writer::empty_tag('input', $attributes);
} else {
echo html_writer::label(get_string('selectlogreader', 'report_log'), 'menureader', false,
array('class' => 'accesshide'));
echo html_writer::select($readers, 'logreader', $reportlog->selectedlogreader, false,
['class' => 'mr-2 mb-2']);
}
echo html_writer::start_div('mt-1');
echo html_writer::empty_tag('input', array('type' => 'submit',
'value' => get_string('gettheselogs'), 'class' => 'btn btn-primary'));
echo html_writer::end_div();
}
echo html_writer::end_div();
echo html_writer::end_tag('form');
}
}
+546
View File
@@ -0,0 +1,546 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Table log for displaying logs.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\dataformat;
use core\report_helper;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/tablelib.php');
/**
* Table log class for displaying logs.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh.taneja@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_log_table_log extends table_sql {
/** @var array list of user fullnames shown in report */
private $userfullnames = array();
/** @var array list of context name shown in report */
private $contextname = array();
/** @var stdClass filters parameters */
private $filterparams;
/** @var int[] A list of users to filter by */
private ?array $lateuseridfilter = null;
/**
* Sets up the table_log parameters.
*
* @param string $uniqueid unique id of form.
* @param stdClass $filterparams (optional) filter params.
* - int courseid: id of course
* - int userid: user id
* - int|string modid: Module id or "site_errors" to view site errors
* - int groupid: Group id
* - \core\log\sql_reader logreader: reader from which data will be fetched.
* - int edulevel: educational level.
* - string action: view action
* - int date: Date from which logs to be viewed.
*/
public function __construct($uniqueid, $filterparams = null) {
parent::__construct($uniqueid);
$this->set_attribute('class', 'reportlog generaltable generalbox table-sm');
$this->filterparams = $filterparams;
// Add course column if logs are displayed for site.
$cols = array();
$headers = array();
if (empty($filterparams->courseid)) {
$cols = array('course');
$headers = array(get_string('course'));
}
$this->define_columns(array_merge($cols, array('time', 'fullnameuser', 'relatedfullnameuser', 'context', 'component',
'eventname', 'description', 'origin', 'ip')));
$this->define_headers(array_merge($headers, array(
get_string('time'),
get_string('fullnameuser'),
get_string('eventrelatedfullnameuser', 'report_log'),
get_string('eventcontext', 'report_log'),
get_string('eventcomponent', 'report_log'),
get_string('eventname'),
get_string('description'),
get_string('eventorigin', 'report_log'),
get_string('ip_address')
)
));
$this->collapsible(false);
$this->sortable(false);
$this->pageable(true);
}
/**
* Generate the course column.
*
* @deprecated since Moodle 2.9 MDL-48595 - please do not use this function any more.
*/
public function col_course($event) {
throw new coding_exception('col_course() can not be used any more, there is no such column.');
}
/**
* Gets the user full name.
*
* This function is useful because, in the unlikely case that the user is
* not already loaded in $this->userfullnames it will fetch it from db.
*
* @since Moodle 2.9
* @param int $userid
* @return string|false
*/
protected function get_user_fullname($userid) {
if (empty($userid)) {
return false;
}
// Check if we already have this users' fullname.
$userfullname = $this->userfullnames[$userid] ?? null;
if (!empty($userfullname)) {
return $userfullname;
}
// We already looked for the user and it does not exist.
if ($userfullname === false) {
return false;
}
// If we reach that point new users logs have been generated since the last users db query.
$userfieldsapi = \core_user\fields::for_name();
$fields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
if ($user = \core_user::get_user($userid, $fields)) {
$this->userfullnames[$userid] = fullname($user, has_capability('moodle/site:viewfullnames', $this->get_context()));
} else {
$this->userfullnames[$userid] = false;
}
return $this->userfullnames[$userid];
}
/**
* Generate the time column.
*
* @param stdClass $event event data.
* @return string HTML for the time column
*/
public function col_time($event) {
if (empty($this->download)) {
$dateformat = get_string('strftimedatetimeaccurate', 'core_langconfig');
} else {
$dateformat = get_string('strftimedatetimeshortaccurate', 'core_langconfig');
}
return userdate($event->timecreated, $dateformat);
}
/**
* Generate the username column.
*
* @param \core\event\base $event event data.
* @return string HTML for the username column
*/
public function col_fullnameuser($event) {
// Get extra event data for origin and realuserid.
$logextra = $event->get_logextra();
$eventusername = $event->userid ? $this->get_user_fullname($event->userid) : false;
// Add username who did the action.
if (!empty($logextra['realuserid'])) {
$a = new stdClass();
if (!$a->realusername = $this->get_user_fullname($logextra['realuserid'])) {
$a->realusername = '-';
}
if (!$a->asusername = $eventusername) {
$a->asusername = '-';
}
if (empty($this->download)) {
$params = array('id' => $logextra['realuserid']);
if ($event->courseid) {
$params['course'] = $event->courseid;
}
$a->realusername = html_writer::link(new moodle_url('/user/view.php', $params), $a->realusername);
$params['id'] = $event->userid;
$a->asusername = html_writer::link(new moodle_url('/user/view.php', $params), $a->asusername);
}
$username = get_string('eventloggedas', 'report_log', $a);
} else if ($eventusername) {
if (empty($this->download)) {
$params = ['id' => $event->userid];
if ($event->courseid) {
$params['course'] = $event->courseid;
}
$username = html_writer::link(new moodle_url('/user/view.php', $params), $eventusername);
} else {
$username = $eventusername;
}
} else {
$username = '-';
}
return $username;
}
/**
* Generate the related username column.
*
* @param stdClass $event event data.
* @return string HTML for the related username column
*/
public function col_relatedfullnameuser($event) {
// Add affected user.
if (!empty($event->relateduserid) && $username = $this->get_user_fullname($event->relateduserid)) {
if (empty($this->download)) {
$params = array('id' => $event->relateduserid);
if ($event->courseid) {
$params['course'] = $event->courseid;
}
$username = html_writer::link(new moodle_url('/user/view.php', $params), $username);
}
} else {
$username = '-';
}
return $username;
}
/**
* Generate the context column.
*
* @param stdClass $event event data.
* @return string HTML for the context column
*/
public function col_context($event) {
// Add context name.
if ($event->contextid) {
// If context name was fetched before then return, else get one.
if (isset($this->contextname[$event->contextid])) {
return $this->contextname[$event->contextid];
} else {
$context = context::instance_by_id($event->contextid, IGNORE_MISSING);
if ($context) {
$contextname = $context->get_context_name(true);
if (empty($this->download) && $url = $context->get_url()) {
$contextname = html_writer::link($url, $contextname);
}
} else {
$contextname = get_string('other');
}
}
} else {
$contextname = get_string('other');
}
$this->contextname[$event->contextid] = $contextname;
return $contextname;
}
/**
* Generate the component column.
*
* @param stdClass $event event data.
* @return string HTML for the component column
*/
public function col_component($event) {
// Component.
$componentname = $event->component;
if (($event->component === 'core') || ($event->component === 'legacy')) {
return get_string('coresystem');
} else if (get_string_manager()->string_exists('pluginname', $event->component)) {
return get_string('pluginname', $event->component);
} else {
return $componentname;
}
}
/**
* Generate the event name column.
*
* @param stdClass $event event data.
* @return string HTML for the event name column
*/
public function col_eventname($event) {
// Event name.
$eventname = $event->get_name();
// Only encode as an action link if we're not downloading.
if (($url = $event->get_url()) && empty($this->download)) {
$eventname = $this->action_link($url, $eventname, 'action');
}
return $eventname;
}
/**
* Generate the description column.
*
* @param stdClass $event event data.
* @return string HTML for the description column
*/
public function col_description($event) {
if (empty($this->download) || dataformat::get_format_instance($this->download)->supports_html()) {
return format_text($event->get_description(), FORMAT_PLAIN);
} else {
return $event->get_description();
}
}
/**
* Generate the origin column.
*
* @param stdClass $event event data.
* @return string HTML for the origin column
*/
public function col_origin($event) {
// Get extra event data for origin and realuserid.
$logextra = $event->get_logextra();
// Add event origin, normally IP/cron.
return $logextra['origin'];
}
/**
* Generate the ip column.
*
* @param stdClass $event event data.
* @return string HTML for the ip column
*/
public function col_ip($event) {
// Get extra event data for origin and realuserid.
$logextra = $event->get_logextra();
$ip = $logextra['ip'];
if (empty($this->download)) {
$url = new moodle_url("/iplookup/index.php?popup=1&ip={$ip}&user={$event->userid}");
$ip = $this->action_link($url, $ip, 'ip');
}
return $ip;
}
/**
* Method to create a link with popup action.
*
* @param moodle_url $url The url to open.
* @param string $text Anchor text for the link.
* @param string $name Name of the popup window.
*
* @return string html to use.
*/
protected function action_link(moodle_url $url, $text, $name = 'popup') {
global $OUTPUT;
$link = new action_link($url, $text, new popup_action('click', $url, $name, array('height' => 440, 'width' => 700)));
return $OUTPUT->render($link);
}
/**
* Helper function to get legacy crud action.
*
* @param string $crud crud action
* @return string legacy action.
*/
public function get_legacy_crud_action($crud) {
$legacyactionmap = array('c' => 'add', 'r' => 'view', 'u' => 'update', 'd' => 'delete');
if (array_key_exists($crud, $legacyactionmap)) {
return $legacyactionmap[$crud];
} else {
// From old legacy log.
return '-view';
}
}
/**
* Helper function which is used by build logs to get action sql and param.
*
* @return array sql and param for action.
*/
public function get_action_sql() {
global $DB;
// In new logs we have a field to pick, and in legacy try get this from action.
if (!empty($this->filterparams->action)) {
list($sql, $params) = $DB->get_in_or_equal(str_split($this->filterparams->action),
SQL_PARAMS_NAMED, 'crud');
$sql = "crud " . $sql;
} else {
// Add condition for all possible values of crud (to use db index).
list($sql, $params) = $DB->get_in_or_equal(array('c', 'r', 'u', 'd'),
SQL_PARAMS_NAMED, 'crud');
$sql = "crud ".$sql;
}
return array($sql, $params);
}
/**
* Helper function which is used by build logs to get course module sql and param.
*
* @return array sql and param for action.
*/
public function get_cm_sql() {
$joins = array();
$params = array();
$joins[] = "contextinstanceid = :contextinstanceid";
$joins[] = "contextlevel = :contextmodule";
$params['contextinstanceid'] = $this->filterparams->modid;
$params['contextmodule'] = CONTEXT_MODULE;
$sql = implode(' AND ', $joins);
return array($sql, $params);
}
/**
* Query the reader. Store results in the object for use by build_table.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
global $DB, $USER;
$joins = array();
$params = array();
// If we filter by userid and module id we also need to filter by crud and edulevel to ensure DB index is engaged.
$useextendeddbindex = !empty($this->filterparams->userid) && !empty($this->filterparams->modid);
if (!empty($this->filterparams->courseid) && $this->filterparams->courseid != SITEID) {
$joins[] = "courseid = :courseid";
$params['courseid'] = $this->filterparams->courseid;
}
if (!empty($this->filterparams->siteerrors)) {
$joins[] = "( action='error' OR action='infected' OR action='failed' )";
}
if (!empty($this->filterparams->modid)) {
list($actionsql, $actionparams) = $this->get_cm_sql();
$joins[] = $actionsql;
$params = array_merge($params, $actionparams);
}
if (!empty($this->filterparams->action) || $useextendeddbindex) {
list($actionsql, $actionparams) = $this->get_action_sql();
$joins[] = $actionsql;
$params = array_merge($params, $actionparams);
}
// Getting all members of a group.
[
'joins' => $groupjoins,
'params' => $groupparams,
'useridfilter' => $this->lateuseridfilter,
] = report_helper::get_group_filter($this->filterparams);
$joins = array_merge($joins, $groupjoins);
$params = array_merge($params, $groupparams);
if (!empty($this->filterparams->date)) {
$joins[] = "timecreated > :date AND timecreated < :enddate";
$params['date'] = $this->filterparams->date;
$params['enddate'] = $this->filterparams->date + DAYSECS; // Show logs only for the selected date.
}
if (isset($this->filterparams->edulevel) && ($this->filterparams->edulevel >= 0)) {
$joins[] = "edulevel = :edulevel";
$params['edulevel'] = $this->filterparams->edulevel;
} else if ($useextendeddbindex) {
list($edulevelsql, $edulevelparams) = $DB->get_in_or_equal(array(\core\event\base::LEVEL_OTHER,
\core\event\base::LEVEL_PARTICIPATING, \core\event\base::LEVEL_TEACHING), SQL_PARAMS_NAMED, 'edulevel');
$joins[] = "edulevel ".$edulevelsql;
$params = array_merge($params, $edulevelparams);
}
// Origin.
if (isset($this->filterparams->origin) && ($this->filterparams->origin != '')) {
if ($this->filterparams->origin !== '---') {
// Filter by a single origin.
$joins[] = "origin = :origin";
$params['origin'] = $this->filterparams->origin;
} else {
// Filter by everything else.
list($originsql, $originparams) = $DB->get_in_or_equal(array('cli', 'restore', 'ws', 'web'),
SQL_PARAMS_NAMED, 'origin', false);
$joins[] = "origin " . $originsql;
$params = array_merge($params, $originparams);
}
}
// Filter out anonymous actions, this is N/A for legacy log because it never stores them.
if ($this->filterparams->modid) {
$context = context_module::instance($this->filterparams->modid);
} else {
$context = context_course::instance($this->filterparams->courseid);
}
if (!has_capability('moodle/site:viewanonymousevents', $context)) {
$joins[] = "anonymous = 0";
}
$selector = implode(' AND ', $joins);
if (!$this->is_downloading()) {
$total = $this->filterparams->logreader->get_events_select_count($selector, $params);
$this->pagesize($pagesize, $total);
} else {
$this->pageable(false);
}
// Get the events. Same query than before; even if it is not likely, logs from new users
// may be added since last query so we will need to work around later to prevent problems.
// In almost most of the cases this will be better than having two opened recordsets.
$this->rawdata = new \CallbackFilterIterator(
$this->filterparams->logreader->get_events_select_iterator(
$selector,
$params,
$this->filterparams->orderby,
$this->get_page_start(),
$this->get_page_size(),
),
function ($event) {
if ($this->lateuseridfilter === null) {
return true;
}
return isset($this->lateuseridfilter[$event->userid]);
},
);
// Set initial bars.
if ($useinitialsbar && !$this->is_downloading()) {
$this->initialbars($total > $pagesize);
}
}
/**
* Helper function to create list of course shortname and user fullname shown in log report.
*
* This will update $this->userfullnames and $this->courseshortnames array with userfullname and courseshortname (with link),
* which will be used to render logs in table.
*
* @deprecated since Moodle 2.9 MDL-48595 - please do not use this function any more.
*/
public function update_users_and_courses_used() {
throw new coding_exception('update_users_and_courses_used() can not be used any more.');
}
}