Commit 78e07cbc authored by victor's avatar victor 🙇
Browse files

MDL-63063 recentlyaccesseditems: fully contained block

parent 4ea6f848
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
// 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/>.
/**
* Javascript to initialise the Recently accessed items block.
*
* @module block_recentlyaccesseditems/main
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(
[
'jquery',
'block_recentlyaccesseditems/repository',
'core/templates',
'core/notification'
],
function(
$,
Repository,
Templates,
Notification
) {
var NUM_ITEMS = 9;
var SELECTORS = {
CARDDECK_CONTAINER: '[data-region="recentlyaccesseditems-view"]',
CARDDECK: '[data-region="recentlyaccesseditems-view-content"]',
};
/**
* Get recent items from backend.
*
* @method getRecentItems
* @param {int} limit Only return this many results
* @return {array} Items user most recently has accessed
*/
var getRecentItems = function(limit) {
return Repository.getRecentItems(limit);
};
/**
* Render the block content.
*
* @method renderItems
* @param {object} root The root element for the items view.
* @param {array} items containing array of returned items.
* @return {promise} Resolved with HTML and JS strings
*/
var renderItems = function(root, items) {
if (items.length > 0) {
return Templates.render('block_recentlyaccesseditems/view-cards', {
items: items
});
} else {
var noitemsimgurl = root.attr('data-noitemsimgurl');
return Templates.render('block_recentlyaccesseditems/no-items', {
noitemsimgurl: noitemsimgurl
});
}
};
/**
* Get and show the recent items into the block.
*
* @param {object} root The root element for the items block.
*/
var init = function(root) {
root = $(root);
var itemsContainer = root.find(SELECTORS.CARDDECK_CONTAINER);
var itemsContent = root.find(SELECTORS.CARDDECK);
var itemsPromise = getRecentItems(NUM_ITEMS);
itemsPromise.then(function(items) {
var pageContentPromise = renderItems(itemsContainer, items);
pageContentPromise.then(function(html, js) {
return Templates.replaceNodeContents(itemsContent, html, js);
}).catch(Notification.exception);
return itemsPromise;
}).catch(Notification.exception);
};
return {
init: init
};
});
\ No newline at end of file
// 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/>.
/**
* A javascript module to handle user ajax actions.
*
* @module block_recentlyaccesseditems/repository
* @package block_recentlyaccesseditems
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/ajax'], function(Ajax) {
/**
* Get the list of items that the user has most recently accessed.
*
* @method getRecentItems
* @param {int} limit Only return this many results
* @return {promise} Resolved with an array of items
*/
var getRecentItems = function(limit) {
var args = {};
if (typeof limit !== 'undefined') {
args.limit = limit;
}
var request = {
methodname: 'block_recentlyaccesseditems_get_recent_items',
args: args
};
return Ajax.call([request])[0];
};
return {
getRecentItems: getRecentItems
};
});
\ No newline at end of file
<?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 definition for the 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
*/
defined('MOODLE_INTERNAL') || die();
/**
* Recently accessed items block class.
*
* @package block_recentlyaccesseditems
* @copyright Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_recentlyaccesseditems extends block_base {
/**
* Initialize class member variables
*/
public function init() {
$this->title = get_string('pluginname', 'block_recentlyaccesseditems');
}
/**
* Returns the contents.
*
* @return stdClass contents of block
*/
public function get_content() {
if (isset($this->content)) {
return $this->content;
}
$renderable = new block_recentlyaccesseditems\output\main();
$renderer = $this->page->get_renderer('block_recentlyaccesseditems');
$this->content = new stdClass();
$this->content->text = $renderer->render($renderable);
$this->content->footer = '';
return $this->content;
}
/**
* Locations where block can be displayed.
*
* @return array
*/
public function applicable_formats() {
return array('my' => true);
}
}
\ No newline at end of file
<?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/>.
/**
* External API.
*
* @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();
require_once("$CFG->libdir/externallib.php");
use block_recentlyaccesseditems\external\recentlyaccesseditems_item_exporter;
use external_api;
use external_function_parameters;
use external_value;
use external_multiple_structure;
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;
if ($userid != $USER->id) {
return array();
}
$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 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');
}
}
\ No newline at end of file
<?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 $OUTPUT;
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' => $OUTPUT->image_icon('icon', get_string('pluginname', $this->data->modname), $this->data->modname)
);
}
/**
* 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_TEXT,
),
'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_TEXT,
),
'courseviewurl' => array(
'type' => PARAM_URL,
),
'icon' => array(
'type' => PARAM_RAW,
)
);
}
}
\ No newline at end of file
<?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;
}
// Determine sort sql clause.
$sort = 'timeaccess DESC';
$paramsql = array('userid' => $userid);
$records = $DB->get_records('block_recentlyaccesseditems', $paramsql, $sort);
$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);
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;
}
}
\ No newline at end of file
<?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 DE