1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Event observer. 19 * 20 * @package block_recentlyaccesseditems 21 * @copyright 2018 Victor Deniz 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace block_recentlyaccesseditems; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Events observer. 31 * 32 * Stores all actions about modules viewed in block_recentlyaccesseditems table. 33 * 34 * @package block_recentlyaccesseditems 35 * @copyright 2018 Victor Deniz <victor@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class observer { 39 40 /** 41 * @var string Block table name. 42 */ 43 private static $table = 'block_recentlyaccesseditems'; 44 45 /** 46 * Register items views in block_recentlyaccesseditems table. 47 * 48 * When the item is view for the first time, a new record is created. If the item was viewed before, the time is 49 * updated. 50 * 51 * @param \core\event\base $event 52 */ 53 public static function store(\core\event\base $event) { 54 global $DB; 55 56 if (!isloggedin() or \core\session\manager::is_loggedinas() or isguestuser()) { 57 // No access tracking. 58 return; 59 } 60 61 $conditions = [ 62 'userid' => $event->userid 63 ]; 64 65 $records = $DB->get_records(self::$table, $conditions, "timeaccess DESC"); 66 67 foreach ($records as $record) { 68 if (($record->userid == $event->userid) && ($record->cmid == $event->contextinstanceid)) { 69 $conditions = [ 70 'userid' => $event->userid, 71 'cmid' => $event->contextinstanceid 72 ]; 73 $DB->set_field(self::$table, 'timeaccess', $event->timecreated, $conditions); 74 return; 75 } 76 } 77 78 if (count($records) >= 9) { 79 $conditions = [ 80 'id' => end($records)->id, 81 ]; 82 $DB->delete_records(self::$table, $conditions); 83 } 84 85 $eventdata = new \stdClass(); 86 87 $eventdata->cmid = $event->contextinstanceid; 88 $eventdata->timeaccess = $event->timecreated; 89 $eventdata->courseid = $event->courseid; 90 $eventdata->userid = $event->userid; 91 92 $DB->insert_record(self::$table, $eventdata); 93 } 94 95 /** 96 * Remove record when course module is deleted. 97 * 98 * @param \core\event\base $event 99 */ 100 public static function remove(\core\event\base $event) { 101 global $DB; 102 103 $DB->delete_records(self::$table, array('cmid' => $event->contextinstanceid)); 104 } 105 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body