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
@@ -0,0 +1,74 @@
<?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_loglive report viewed event.
*
* @package report_loglive
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_loglive\event;
defined('MOODLE_INTERNAL') || die();
/**
* The report_loglive report viewed event class.
*
* @package report_loglive
* @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_loglive');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the live log report for the course with id '$this->courseid'.";
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/loglive/index.php', array('id' => $this->courseid));
}
}
@@ -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_loglive.
*
* @package report_loglive
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_loglive\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for report_loglive 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';
}
}
+233
View File
@@ -0,0 +1,233 @@
<?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/>.
/**
* Loglive report renderable class.
*
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Report loglive renderable class.
*
* @since Moodle 2.7
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_loglive_renderable implements renderable {
/** @var int number of seconds to show logs from, by default. */
const CUTOFF = 3600;
/** @var \core\log\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 string order to sort */
public $order;
/** @var int group id */
public $groupid;
/** @var report_loglive_table_log table log which will be used for rendering logs */
public $tablelog;
/** @var int refresh rate in seconds */
protected $refresh = 60;
/**
* Constructor.
*
* @param string $logreader (optional)reader pluginname from which logs will be fetched.
* @param stdClass|int $course (optional) course record or id
* @param moodle_url|string $url (optional) page url.
* @param int $date date (optional) from which records will be fetched.
* @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, $url = "", $date = 0, $page = 0, $perpage = 100,
$order = "timecreated DESC") {
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;
}
}
$this->selectedlogreader = $logreader;
// Use page url if empty.
if (empty($url)) {
$url = new moodle_url($PAGE->url);
} else {
$url = new moodle_url($url);
}
$this->url = $url;
// Use site course id, if course is empty.
if (!empty($course) && is_int($course)) {
$course = get_course($course);
}
$this->course = $course;
if ($date == 0 ) {
$date = time() - self::CUTOFF;
}
$this->date = $date;
$this->page = $page;
$this->perpage = $perpage;
$this->order = $order;
$this->set_refresh_rate();
}
/**
* 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;
}
/**
* Setup table log.
*/
protected function setup_table() {
$filter = $this->setup_filters();
$this->tablelog = new report_loglive_table_log('report_loglive', $filter);
$this->tablelog->define_baseurl($this->url);
}
/**
* Setup table log for ajax output.
*/
protected function setup_table_ajax() {
$filter = $this->setup_filters();
$this->tablelog = new report_loglive_table_log_ajax('report_loglive', $filter);
$this->tablelog->define_baseurl($this->url);
}
/**
* Setup filters
*
* @return stdClass filters
*/
protected function setup_filters() {
$readers = $this->get_readers();
// Set up filters.
$filter = new \stdClass();
if (!empty($this->course)) {
$filter->courseid = $this->course->id;
$context = context_course::instance($filter->courseid);
if (!has_capability('moodle/site:viewanonymousevents', $context)) {
$filter->anonymous = 0;
}
} else {
$filter->courseid = 0;
}
$filter->logreader = $readers[$this->selectedlogreader];
$filter->date = $this->date;
$filter->orderby = $this->order;
return $filter;
}
/**
* Set refresh rate of the live updates.
*/
protected function set_refresh_rate() {
if (defined('BEHAT_SITE_RUNNING')) {
// Hack for behat tests.
$this->refresh = 5;
} else {
if (defined('REPORT_LOGLIVE_REFRESH')) {
// Backward compatibility.
$this->refresh = REPORT_LOGLIVE_REFRESH;
} else {
// Default.
$this->refresh = 60;
}
}
}
/**
* Get refresh rate of the live updates.
*/
public function get_refresh_rate() {
return $this->refresh;
}
/**
* Setup table and return it.
*
* @param bool $ajax If set to true report_loglive_table_log_ajax is set instead of report_loglive_table_log.
*
* @return report_loglive_table_log|report_loglive_table_log_ajax table object
*/
public function get_table($ajax = false) {
if (empty($this->tablelog)) {
if ($ajax) {
$this->setup_table_ajax();
} else {
$this->setup_table();
}
}
return $this->tablelog;
}
}
+118
View File
@@ -0,0 +1,118 @@
<?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/>.
/**
* Loglive report renderer.
*
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@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.
*
* @since Moodle 2.7
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_loglive_renderer extends plugin_renderer_base {
/**
* This method should never be manually called, it should only be called by process.
* Please call the render method instead.
*
* @deprecated since 2.8, to be removed in 2.9
* @param report_loglive_renderable $reportloglive
* @return string
*/
public function render_report_loglive_renderable(report_loglive_renderable $reportloglive) {
debugging('Do not call this method. Please call $renderer->render($reportloglive) instead.', DEBUG_DEVELOPER);
return $this->render($reportloglive);
}
/**
* Return html to render the loglive page..
*
* @param report_loglive_renderable $reportloglive object of report_log.
*
* @return string html used to render the page;
*/
protected function render_report_loglive(report_loglive_renderable $reportloglive) {
if (empty($reportloglive->selectedlogreader)) {
return $this->output->notification(get_string('nologreaderenabled', 'report_loglive'), 'notifyproblem');
}
$table = $reportloglive->get_table();
return $this->render_table($table, $reportloglive->perpage);
}
/**
* Prints/return reader selector
*
* @param report_loglive_renderable $reportloglive log report.
*
* @return string Returns rendered widget
*/
public function reader_selector(report_loglive_renderable $reportloglive) {
$readers = $reportloglive->get_readers(true);
if (count($readers) <= 1) {
// One or no readers found, no need of this drop down.
return '';
}
$select = new single_select($reportloglive->url, 'logreader', $readers, $reportloglive->selectedlogreader, null);
$select->set_label(get_string('selectlogreader', 'report_loglive'));
return $this->output->render($select);
}
/**
* Prints a button to update/resume live updates.
*
* @param report_loglive_renderable $reportloglive log report.
*
* @return string Returns rendered widget
*/
public function toggle_liveupdate_button(report_loglive_renderable $reportloglive) {
// Add live log controls.
if ($reportloglive->page == 0 && $reportloglive->selectedlogreader) {
echo html_writer::tag('button' , get_string('pause', 'report_loglive'),
array('id' => 'livelogs-pause-button', 'class' => 'btn btn-secondary'));
$icon = new pix_icon('i/loading_small', 'loading', 'moodle', array('class' => 'spinner'));
return html_writer::tag('span', $this->output->render($icon), array('class' => 'spinner'));
}
return '';
}
/**
* Get the html for the table.
*
* @param report_loglive_table_log $table table object.
* @param int $perpage entries to display perpage.
*
* @return string table html
*/
protected function render_table(report_loglive_table_log $table, $perpage) {
$o = '';
ob_start();
$table->out($perpage, true);
$o = ob_get_contents();
ob_end_clean();
return $o;
}
}
+61
View File
@@ -0,0 +1,61 @@
<?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 live report ajax renderer.
*
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Log live report ajax renderer.
*
* @since Moodle 2.7
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_loglive_renderer_ajax extends plugin_renderer_base {
/**
* This method should never be manually called, it should only be called by process.
* Please call the render method instead.
*
* @deprecated since 2.8, to be removed in 2.9
* @param report_loglive_renderable $reportloglive
* @return string
*/
public function render_report_loglive_renderable(report_loglive_renderable $reportloglive) {
debugging('Do not call this method. Please call $renderer->render($reportloglive) instead.', DEBUG_DEVELOPER);
return $this->render($reportloglive);
}
/**
* Render logs for ajax.
*
* @param report_loglive_renderable $reportloglive object of report_loglive_renderable.
*
* @return string html to be displayed to user.
*/
protected function render_report_loglive(report_loglive_renderable $reportloglive) {
if (empty($reportloglive->selectedlogreader)) {
return null;
}
$table = $reportloglive->get_table(true);
return $table->out($reportloglive->perpage, false);
}
}
+441
View File
@@ -0,0 +1,441 @@
<?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_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/tablelib.php');
/**
* Table log class for displaying logs.
*
* @since Moodle 2.7
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_loglive_table_log extends table_sql {
/** @var array list of user fullnames shown in report */
protected $userfullnames = array();
/** @var array list of course short names shown in report */
protected $courseshortnames = array();
/** @var array list of context name shown in report */
protected $contextname = array();
/** @var stdClass filters parameters */
protected $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', 'reportloglive generaltable table-sm');
$this->set_attribute('aria-live', 'polite');
$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_loglive'),
get_string('eventcontext', 'report_loglive'),
get_string('eventcomponent', 'report_loglive'),
get_string('eventname'),
get_string('description'),
get_string('eventorigin', 'report_loglive'),
get_string('ip_address')
)
));
$this->collapsible(false);
$this->sortable(false);
$this->pageable(true);
$this->is_downloadable(false);
}
/**
* Generate the course column.
*
* @param stdClass $event event data.
* @return string HTML for the course column.
*/
public function col_course($event) {
if (empty($event->courseid) || empty($this->courseshortnames[$event->courseid])) {
return '-';
} else {
return $this->courseshortnames[$event->courseid];
}
}
/**
* Generate the time column.
*
* @param stdClass $event event data.
* @return string HTML for the time column
*/
public function col_time($event) {
$recenttimestr = get_string('strftimedatetimeaccurate', 'core_langconfig');
return userdate($event->timecreated, $recenttimestr);
}
/**
* Generate the username column.
*
* @param stdClass $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();
// Add username who did the action.
if (!empty($logextra['realuserid'])) {
$a = new stdClass();
$params = array('id' => $logextra['realuserid']);
if ($event->courseid) {
$params['course'] = $event->courseid;
}
$a->realusername = html_writer::link(new moodle_url("/user/view.php", $params),
$this->userfullnames[$logextra['realuserid']]);
$params['id'] = $event->userid;
$a->asusername = html_writer::link(new moodle_url("/user/view.php", $params),
$this->userfullnames[$event->userid]);
$username = get_string('eventloggedas', 'report_loglive', $a);
} else if (!empty($event->userid) && !empty($this->userfullnames[$event->userid])) {
$params = array('id' => $event->userid);
if ($event->courseid) {
$params['course'] = $event->courseid;
}
$username = html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->userid]);
} 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) && isset($this->userfullnames[$event->relateduserid])) {
$params = array('id' => $event->relateduserid);
if ($event->courseid) {
$params['course'] = $event->courseid;
}
return html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->relateduserid]);
} else {
return '-';
}
}
/**
* 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 ($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) {
$eventname = $event->get_name();
if ($url = $event->get_url()) {
$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) {
// Description.
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();
$url = new moodle_url("/iplookup/index.php?popup=1&ip={$logextra['ip']}&user=$event->userid");
return $this->action_link($url, $logextra['ip'], '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' => 550, 'width' => 700)));
return $OUTPUT->render($link);
}
/**
* 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) {
$joins = [];
$params = [];
if (!empty($this->filterparams->courseid)) {
$joins[] = "courseid = :courseid";
$params['courseid'] = $this->filterparams->courseid;
}
// Getting all members of a group.
[
'joins' => $groupjoins,
'params' => $groupparams,
'useridfilter' => $this->lateuseridfilter,
] = \core\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";
$params['date'] = $this->filterparams->date;
}
if (isset($this->filterparams->anonymous)) {
$joins[] = "anonymous = :anon";
$params['anon'] = $this->filterparams->anonymous;
}
$selector = implode(' AND ', $joins);
$total = $this->filterparams->logreader->get_events_select_count($selector, $params);
$this->pagesize($pagesize, $total);
$this->rawdata =
array_filter(
$this->filterparams->logreader->get_events_select(
$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->initialbars($total > $pagesize);
}
// Update list of users and courses list which will be displayed on log page.
$this->update_users_and_courses_used();
}
/**
* 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.
*/
public function update_users_and_courses_used() {
global $SITE, $DB;
$this->userfullnames = array();
$this->courseshortnames = array($SITE->id => $SITE->shortname);
$userids = array();
$courseids = array();
// For each event cache full username and course.
// Get list of userids and courseids which will be shown in log report.
foreach ($this->rawdata as $event) {
$logextra = $event->get_logextra();
if (!empty($event->userid) && !in_array($event->userid, $userids)) {
$userids[] = $event->userid;
}
if (!empty($logextra['realuserid']) && !in_array($logextra['realuserid'], $userids)) {
$userids[] = $logextra['realuserid'];
}
if (!empty($event->relateduserid) && !in_array($event->relateduserid, $userids)) {
$userids[] = $event->relateduserid;
}
if (!empty($event->courseid) && ($event->courseid != $SITE->id) && !in_array($event->courseid, $courseids)) {
$courseids[] = $event->courseid;
}
}
// Get user fullname and put that in return list.
if (!empty($userids)) {
list($usql, $uparams) = $DB->get_in_or_equal($userids);
$userfieldsapi = \core_user\fields::for_name();
$users = $DB->get_records_sql("SELECT id," .
$userfieldsapi->get_sql('', false, '', '', false)->selects . " FROM {user} WHERE id " . $usql,
$uparams);
foreach ($users as $userid => $user) {
$this->userfullnames[$userid] = fullname($user, has_capability('moodle/site:viewfullnames', $this->get_context()));
}
}
// Get course shortname and put that in return list.
if (!empty($courseids)) { // If all logs don't belog to site level then get course info.
list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$courseparams['contextlevel'] = CONTEXT_COURSE;
$sql = "SELECT c.id,c.shortname $ccselect FROM {course} c
$ccjoin
WHERE c.id " . $coursesql;
$courses = $DB->get_records_sql($sql, $courseparams);
foreach ($courses as $courseid => $course) {
$url = new moodle_url("/course/view.php", array('id' => $courseid));
context_helper::preload_from_record($course);
$context = context_course::instance($courseid, IGNORE_MISSING);
// Method format_string() takes care of missing contexts.
$this->courseshortnames[$courseid] = html_writer::link($url, format_string($course->shortname, true,
array('context' => $context)));
}
}
}
/**
* Returns the latest timestamp of the records in the table.
*
* @return int
*/
public function get_until(): int {
$until = $this->filterparams->date;
if (!empty($this->rawdata)) {
foreach ($this->rawdata as $row) {
$until = max($row->timecreated, $until);
}
}
return $until;
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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 generating data in ajax mode.
*
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Table log class for generating data in ajax mode.
*
* @since Moodle 2.7
* @package report_loglive
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_loglive_table_log_ajax extends report_loglive_table_log {
/**
* Convenience method to call a number of methods for you to display the
* table.
*
* @param int $pagesize pagesize
* @param bool $useinitialsbar Not used, present only for compatibility with parent.
* @param string $downloadhelpbutton Not used, present only for compatibility with parent.
*
* @return string json encoded data containing html of new rows.
*/
public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
$this->query_db($pagesize, false);
$html = '';
if ($this->rawdata && $this->columns) {
foreach ($this->rawdata as $row) {
$formatedrow = $this->format_row($row);
$formatedrow = $this->get_row_from_keyed($formatedrow);
$html .= $this->get_row_html($formatedrow, 'newrow');
}
}
$result = array('logs' => $html, 'until' => $this->get_until());
return json_encode($result);
}
/**
* Popup actions do not function when they are rendered in response to an AJAX request, encode within the link itself
*
* @param moodle_url $url
* @param string $text
* @param string $name
* @return string
*/
protected function action_link(moodle_url $url, $text, $name = 'popup') {
global $OUTPUT;
$link = new action_link($url, $text, null, [
'data-action' => 'action-popup',
'data-popup-action' => json_encode(
new popup_action('click', $url, $name, ['height' => 440, 'width' => 700])
),
]);
return $OUTPUT->render($link);
}
}