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,92 @@
<?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/>.
namespace block_recentlyaccesseditems;
use block_recentlyaccesseditems\external\recentlyaccesseditems_item_exporter;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_value;
use context_user;
use context_module;
/**
* External API class.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends external_api {
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function get_recent_items_parameters() {
return new external_function_parameters(
array(
'limit' => new external_value(PARAM_INT, 'result set limit', VALUE_DEFAULT, 0)
)
);
}
/**
* Get last accessed items by the logged user (activities or resources).
*
* @param int $limit Max num of items to return
* @return array List of items
* @since Moodle 3.6
*/
public static function get_recent_items(int $limit = 0) {
global $USER, $PAGE;
$userid = $USER->id;
$params = self::validate_parameters(self::get_recent_items_parameters(),
array(
'limit' => $limit,
)
);
$limit = $params['limit'];
self::validate_context(context_user::instance($userid));
$items = helper::get_recent_items($limit);
$renderer = $PAGE->get_renderer('core');
$recentitems = array_map(function($item) use ($renderer) {
$context = context_module::instance($item->cmid);
$exporter = new recentlyaccesseditems_item_exporter($item, ['context' => $context]);
return $exporter->export($renderer);
}, $items);
return $recentitems;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.6
*/
public static function get_recent_items_returns() {
return new external_multiple_structure(recentlyaccesseditems_item_exporter::get_read_structure(),
'The most recently accessed activities/resources by the logged user');
}
}
@@ -0,0 +1,132 @@
<?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/>.
/**
* Class for exporting the data needed to render a recent accessed item.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems\external;
defined('MOODLE_INTERNAL') || die();
use renderer_base;
use moodle_url;
/**
* Class for exporting the data needed to render a recent accessed item.
*
* @copyright 2018 Victor Deniz
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class recentlyaccesseditems_item_exporter extends \core\external\exporter {
/**
* Returns a list of objects that are related to this persistent.
*
*/
protected static function define_related() {
// We cache the context so it does not need to be retrieved from the course.
return array('context' => '\\context');
}
/**
* Get the additional values to inject while exporting.
*
* @param renderer_base $output The renderer
* @return array Additional properties with values
*/
protected function get_other_values(renderer_base $output) {
global $CFG;
require_once($CFG->libdir.'/modinfolib.php');
$iconurl = get_fast_modinfo($this->data->courseid)->cms[$this->data->cmid]->get_icon_url();
$iconclass = $iconurl->get_param('filtericon') ? '' : 'nofilter';
$isbranded = component_callback('mod_' . $this->data->modname, 'is_branded') !== null ? : false;
return array(
'viewurl' => (new moodle_url('/mod/'.$this->data->modname.'/view.php',
array('id' => $this->data->cmid)))->out(false),
'courseviewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->courseid)))->out(false),
'icon' => \html_writer::img(
$iconurl,
get_string('pluginname', $this->data->modname),
['title' => get_string('pluginname', $this->data->modname), 'class' => "icon $iconclass"]
),
'purpose' => plugin_supports('mod', $this->data->modname, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER),
'branded' => $isbranded,
);
}
/**
* Return the list of properties.
*
* @return array Properties.
*/
public static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
),
'courseid' => array(
'type' => PARAM_INT,
),
'cmid' => array(
'type' => PARAM_INT,
),
'userid' => array(
'type' => PARAM_INT,
),
'modname' => array(
'type' => PARAM_PLUGIN,
),
'name' => array(
'type' => PARAM_TEXT,
),
'coursename' => array(
'type' => PARAM_TEXT,
),
'timeaccess' => array(
'type' => PARAM_INT,
)
);
}
/**
* Return the list of additional properties.
*
* @return array Additional properties.
*/
public static function define_other_properties() {
return array(
'viewurl' => array(
'type' => PARAM_RAW,
),
'courseviewurl' => array(
'type' => PARAM_URL,
),
'icon' => array(
'type' => PARAM_RAW,
),
'purpose' => array(
'type' => PARAM_ALPHA,
),
'branded' => [
'type' => PARAM_BOOL,
'optional' => true,
],
);
}
}
@@ -0,0 +1,98 @@
<?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/>.
/**
* Recently accessed items helper.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems;
defined('MOODLE_INTERNAL') || die();
/**
* Recently accessed items helper.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Returns a list of the most recently items accessed by the logged user
*
* @param int $limit Restrict result set to this amount
* @return array List of recent items accessed by userid
*/
public static function get_recent_items(int $limit = 0) {
global $USER, $DB;
$userid = $USER->id;
$courses = array();
$recentitems = array();
if (!isloggedin() or \core\session\manager::is_loggedinas() or isguestuser()) {
// No access tracking.
return $recentitems;
}
$paramsql = array('userid' => $userid);
$sql = "SELECT rai.*
FROM {block_recentlyaccesseditems} rai
JOIN {course} c ON c.id = rai.courseid
WHERE userid = :userid
ORDER BY rai.timeaccess DESC, rai.id DESC";
$records = $DB->get_records_sql($sql, $paramsql);
$order = 0;
// Get array of items by course. Use $order index to keep sql sorted results.
foreach ($records as $record) {
$courses[$record->courseid][$order++] = $record;
}
// Group by courses to reduce get_fast_modinfo requests.
foreach ($courses as $key => $items) {
$modinfo = get_fast_modinfo($key);
if (!can_access_course($modinfo->get_course(), null, '', true)) {
continue;
}
foreach ($items as $key => $item) {
// Exclude not visible items.
if (!$modinfo->cms[$item->cmid]->uservisible) {
continue;
}
$item->modname = $modinfo->cms[$item->cmid]->modname;
$item->name = $modinfo->cms[$item->cmid]->name;
$item->coursename = get_course_display_name_for_list($modinfo->get_course());
$recentitems[$key] = $item;
}
}
ksort($recentitems);
// Apply limit.
if (!$limit) {
$limit = count($recentitems);
}
$recentitems = array_slice($recentitems, 0, $limit);
return $recentitems;
}
}
@@ -0,0 +1,105 @@
<?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/>.
/**
* Event observer.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems;
defined('MOODLE_INTERNAL') || die();
/**
* Events observer.
*
* Stores all actions about modules viewed in block_recentlyaccesseditems table.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class observer {
/**
* @var string Block table name.
*/
private static $table = 'block_recentlyaccesseditems';
/**
* Register items views in block_recentlyaccesseditems table.
*
* When the item is view for the first time, a new record is created. If the item was viewed before, the time is
* updated.
*
* @param \core\event\base $event
*/
public static function store(\core\event\base $event) {
global $DB;
if (!isloggedin() or \core\session\manager::is_loggedinas() or isguestuser()) {
// No access tracking.
return;
}
$conditions = [
'userid' => $event->userid
];
$records = $DB->get_records(self::$table, $conditions, "timeaccess DESC");
foreach ($records as $record) {
if (($record->userid == $event->userid) && ($record->cmid == $event->contextinstanceid)) {
$conditions = [
'userid' => $event->userid,
'cmid' => $event->contextinstanceid
];
$DB->set_field(self::$table, 'timeaccess', $event->timecreated, $conditions);
return;
}
}
if (count($records) >= 9) {
$conditions = [
'id' => end($records)->id,
];
$DB->delete_records(self::$table, $conditions);
}
$eventdata = new \stdClass();
$eventdata->cmid = $event->contextinstanceid;
$eventdata->timeaccess = $event->timecreated;
$eventdata->courseid = $event->courseid;
$eventdata->userid = $event->userid;
$DB->insert_record(self::$table, $eventdata);
}
/**
* Remove record when course module is deleted.
*
* @param \core\event\base $event
*/
public static function remove(\core\event\base $event) {
global $DB;
$DB->delete_records(self::$table, array('cmid' => $event->contextinstanceid));
}
}
@@ -0,0 +1,53 @@
<?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/>.
/**
* Class containing data for Recently accessed items block.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use renderer_base;
use templatable;
/**
* Class containing data for Recently accessed items block.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class main implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output
* @return array
*/
public function export_for_template(renderer_base $output) {
$noitemsimgurl = $output->image_url('items', 'block_recentlyaccesseditems')->out();
return [
'noitemsimgurl' => $noitemsimgurl
];
}
}
@@ -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/>.
/**
* Recently accessed items block renderer
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems\output;
defined('MOODLE_INTERNAL') || die;
use plugin_renderer_base;
/**
* Recently accessed items block renderer
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Return the main content for the Recently accessed items block.
*
* @param \renderer_base $main The main renderable
* @return string HTML string
*/
public function render_recentlyaccesseditems(renderer_base $main) {
return $this->render_from_template('block_recentlyaccesseditems/main', $main->export_for_template($this));
}
}
@@ -0,0 +1,193 @@
<?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 Recently accessed items block.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccesseditems\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\transform;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\userlist;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\approved_userlist;
use \core_privacy\local\request\writer;
/**
* Privacy Subsystem for block_recentlyaccesseditems.
*
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns information about the user data stored in this component.
*
* @param collection $collection A list of information about this component
* @return collection The collection object filled out with information about this component.
*/
public static function get_metadata(collection $collection): collection {
$recentitems = [
'userid' => 'privacy:metadata:userid',
'courseid' => 'privacy:metadata:courseid',
'cmid' => 'privacy:metadata:cmid',
'timeaccess' => 'privacy:metadata:timeaccess'
];
$collection->add_database_table('block_recentlyaccesseditems', $recentitems,
'privacy:metadata:block_recentlyaccesseditemstablesummary');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$params = ['userid' => $userid, 'contextuser' => CONTEXT_USER];
$sql = "SELECT c.id
FROM {context} c
JOIN {block_recentlyaccesseditems} b
ON b.userid = c.instanceid
WHERE c.instanceid = :userid
AND c.contextlevel = :contextuser";
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users within a specific context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if (!$context instanceof \context_user) {
return;
}
if ($DB->record_exists('block_recentlyaccesseditems', ['userid' => $context->instanceid])) {
$userlist->add_user($context->instanceid);
}
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
$context = $contextlist->current();
$user = \core_user::get_user($contextlist->get_user()->id);
static::export_recentitems($user->id, $context);
}
/**
* Export information about the most recently accessed items.
*
* @param int $userid The user ID.
* @param \context $context The user context.
*/
protected static function export_recentitems(int $userid, \context $context) {
global $DB;
$sql = "SELECT ra.id, c.fullname, ra.timeaccess, m.name, ra.cmid
FROM {block_recentlyaccesseditems} ra
JOIN {course} c ON c.id = ra.courseid
JOIN {course_modules} cm on cm.id = ra.cmid
JOIN {modules} m ON m.id = cm.module
WHERE ra.userid = :userid";
$params = ['userid' => $userid];
$records = $DB->get_records_sql($sql, $params);
if (!empty($records)) {
$recentitems = (object) array_map(function($record) use($context) {
return [
'course_name' => format_string($record->fullname, true, ['context' => $context]),
'module_name' => format_string($record->name),
'timeaccess' => transform::datetime($record->timeaccess)
];
}, $records);
writer::with_context($context)->export_data([get_string('privacy:recentlyaccesseditemspath',
'block_recentlyaccesseditems')], $recentitems);
}
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
// Only delete data for a user context.
if ($context->contextlevel == CONTEXT_USER) {
// Delete recent items access.
$DB->delete_records('block_recentlyaccesseditems', ['userid' => $context->instanceid]);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
foreach ($contextlist as $context) {
// Let's be super certain that we have the right information for this user here.
if ($context->contextlevel == CONTEXT_USER && $contextlist->get_user()->id == $context->instanceid) {
$DB->delete_records('block_recentlyaccesseditems', ['userid' => $context->instanceid]);
}
}
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context instanceof \context_user && in_array($context->instanceid, $userlist->get_userids())) {
$DB->delete_records('block_recentlyaccesseditems', ['userid' => $context->instanceid]);
}
}
}