Moodle release 3.3.8
[moodle.git] / my / lib.php
blobb9b523439b97cf7bfce59092f1ce1fb7238b1efa
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.
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/>.
17 /**
18 * My Moodle -- a user's personal dashboard
20 * This file contains common functions for the dashboard and profile pages.
22 * @package moodlecore
23 * @subpackage my
24 * @copyright 2010 Remote-Learner.net
25 * @author Hubert Chathi <hubert@remote-learner.net>
26 * @author Olav Jordan <olav.jordan@remote-learner.net>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 define('MY_PAGE_PUBLIC', 0);
31 define('MY_PAGE_PRIVATE', 1);
33 require_once("$CFG->libdir/blocklib.php");
36 * For a given user, this returns the $page information for their My Moodle page
39 function my_get_page($userid, $private=MY_PAGE_PRIVATE) {
40 global $DB, $CFG;
42 if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them
43 // Does the user have their own page defined? If so, return it.
44 if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
45 return $customised;
49 // Otherwise return the system default page
50 return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private));
55 * This copies a system default page to the current user
58 function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
59 global $DB;
61 if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
62 return $customised; // We're done!
65 // Get the system default page
66 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
67 return false; // error
70 // Clone the basic system page record
71 $page = clone($systempage);
72 unset($page->id);
73 $page->userid = $userid;
74 $page->id = $DB->insert_record('my_pages', $page);
76 // Clone ALL the associated blocks as well
77 $systemcontext = context_system::instance();
78 $usercontext = context_user::instance($userid);
80 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
81 'pagetypepattern' => $pagetype,
82 'subpagepattern' => $systempage->id));
83 $newblockinstanceids = [];
84 foreach ($blockinstances as $instance) {
85 $originalid = $instance->id;
86 unset($instance->id);
87 $instance->parentcontextid = $usercontext->id;
88 $instance->subpagepattern = $page->id;
89 $instance->id = $DB->insert_record('block_instances', $instance);
90 $newblockinstanceids[$originalid] = $instance->id;
91 $blockcontext = context_block::instance($instance->id); // Just creates the context record
92 $block = block_instance($instance->blockname, $instance);
93 if (!$block->instance_copy($originalid)) {
94 debugging("Unable to copy block-specific data for original block instance: $originalid
95 to new block instance: $instance->id", DEBUG_DEVELOPER);
99 // Clone block position overrides.
100 if ($blockpositions = $DB->get_records('block_positions',
101 ['subpage' => $systempage->id, 'pagetype' => $pagetype, 'contextid' => $systemcontext->id])) {
102 foreach ($blockpositions as &$positions) {
103 $positions->subpage = $page->id;
104 $positions->contextid = $usercontext->id;
105 if (array_key_exists($positions->blockinstanceid, $newblockinstanceids)) {
106 // For block instances that were defined on the default dashboard and copied to the user dashboard
107 // use the new blockinstanceid.
108 $positions->blockinstanceid = $newblockinstanceids[$positions->blockinstanceid];
110 unset($positions->id);
112 $DB->insert_records('block_positions', $blockpositions);
115 return $page;
119 * For a given user, this deletes their My Moodle page and returns them to the system default.
121 * @param int $userid the id of the user whose page should be reset
122 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
123 * @param string $pagetype either my-index or user-profile
124 * @return mixed system page, or false on error
126 function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
127 global $DB, $CFG;
129 $page = my_get_page($userid, $private);
130 if ($page->userid == $userid) {
131 $context = context_user::instance($userid);
132 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
133 'pagetypepattern' => $pagetype))) {
134 foreach ($blocks as $block) {
135 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
136 blocks_delete_instance($block);
140 $DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]);
141 $DB->delete_records('my_pages', array('id' => $page->id));
144 // Get the system default page
145 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
146 return false; // error
149 // Trigger dashboard has been reset event.
150 $eventparams = array(
151 'context' => context_user::instance($userid),
152 'other' => array(
153 'private' => $private,
154 'pagetype' => $pagetype,
157 $event = \core\event\dashboard_reset::create($eventparams);
158 $event->trigger();
159 return $systempage;
163 * Resets the page customisations for all users.
165 * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
166 * @param string $pagetype Either my-index or user-profile.
167 * @return void
169 function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index') {
170 global $DB;
172 // This may take a while. Raise the execution time limit.
173 core_php_time_limit::raise();
175 // Find all the user pages and all block instances in them.
176 $sql = "SELECT bi.id
177 FROM {my_pages} p
178 JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
179 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id AND
180 bi.pagetypepattern = :pagetypepattern AND
181 (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
182 WHERE p.private = :private";
183 $params = array('private' => $private,
184 'usercontextlevel' => CONTEXT_USER,
185 'pagetypepattern' => $pagetype);
186 $blockids = $DB->get_fieldset_sql($sql, $params);
188 // Wrap the SQL queries in a transaction.
189 $transaction = $DB->start_delegated_transaction();
191 // Delete the block instances.
192 if (!empty($blockids)) {
193 blocks_delete_instances($blockids);
196 // Finally delete the pages.
197 $DB->delete_records_select('my_pages', 'userid IS NOT NULL AND private = :private', ['private' => $private]);
199 // We should be good to go now.
200 $transaction->allow_commit();
202 // Trigger dashboard has been reset event.
203 $eventparams = array(
204 'context' => context_system::instance(),
205 'other' => array(
206 'private' => $private,
207 'pagetype' => $pagetype,
210 $event = \core\event\dashboards_reset::create($eventparams);
211 $event->trigger();
214 class my_syspage_block_manager extends block_manager {
215 // HACK WARNING!
216 // TODO: figure out a better way to do this
218 * Load blocks using the system context, rather than the user's context.
220 * This is needed because the My Moodle pages set the page context to the
221 * user's context for access control, etc. But the blocks for the system
222 * pages are stored in the system context.
224 public function load_blocks($includeinvisible = null) {
225 $origcontext = $this->page->context;
226 $this->page->context = context_system::instance();
227 parent::load_blocks($includeinvisible);
228 $this->page->context = $origcontext;