MDL-74288 enrol_lti: fix cross-origin postMessage call in registration
[moodle.git] / my / lib.php
blob9002df0b1c649eb17def82c5a6cb89fd4e96d6b3
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);
32 define('MY_PAGE_DEFAULT', '__default');
33 define('MY_PAGE_COURSES', '__courses');
35 require_once("$CFG->libdir/blocklib.php");
37 /**
38 * For a given user, this returns the $page information for their My Moodle page
40 * @param int|null $userid the id of the user whose page should be retrieved
41 * @param int|null $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
42 * @param string|null $pagename Differentiate between standard /my or /courses pages.
44 function my_get_page(?int $userid, int $private = MY_PAGE_PRIVATE, string $pagename = MY_PAGE_DEFAULT) {
45 global $DB, $CFG;
47 if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them
48 // Does the user have their own page defined? If so, return it.
49 if ($customised = $DB->get_record(
50 'my_pages',
51 array('userid' => $userid, 'private' => $private, 'name' => $pagename),
52 '*',
53 IGNORE_MULTIPLE
54 )) {
55 return $customised;
59 // Otherwise return the system default page
60 return $DB->get_record('my_pages', array('userid' => null, 'name' => $pagename, 'private' => $private), '*', IGNORE_MULTIPLE);
64 /**
65 * This copies a system default page to the current user
67 * @param int $userid the id of the user whose page should be reset
68 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
69 * @param string $pagetype either my-index or user-profile
70 * @param string $pagename Differentiate between standard /my or /courses pages.
72 function my_copy_page(
73 int $userid,
74 int $private = MY_PAGE_PRIVATE,
75 string $pagetype = 'my-index',
76 string $pagename = MY_PAGE_DEFAULT
77 ) {
78 global $DB;
80 if ($customised = $DB->get_record(
81 'my_pages',
82 array('userid' => $userid, 'name' => $pagename, 'private' => $private),
83 '*',
84 IGNORE_MULTIPLE
85 )) {
86 return $customised; // We're done!
89 // Get the system default page
90 if (!$systempage = $DB->get_record(
91 'my_pages',
92 array('userid' => null, 'name' => $pagename, 'private' => $private),
93 '*',
94 IGNORE_MULTIPLE
95 )) {
96 return false; // error
99 // Clone the basic system page record
100 $page = clone($systempage);
101 unset($page->id);
102 $page->userid = $userid;
103 $page->id = $DB->insert_record('my_pages', $page);
105 // Clone ALL the associated blocks as well
106 $systemcontext = context_system::instance();
107 $usercontext = context_user::instance($userid);
109 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
110 'pagetypepattern' => $pagetype,
111 'subpagepattern' => $systempage->id));
112 $newblockinstanceids = [];
113 foreach ($blockinstances as $instance) {
114 $originalid = $instance->id;
115 unset($instance->id);
116 $instance->parentcontextid = $usercontext->id;
117 $instance->subpagepattern = $page->id;
118 $instance->timecreated = time();
119 $instance->timemodified = $instance->timecreated;
120 $instance->id = $DB->insert_record('block_instances', $instance);
121 $newblockinstanceids[$originalid] = $instance->id;
122 $blockcontext = context_block::instance($instance->id); // Just creates the context record
123 $block = block_instance($instance->blockname, $instance);
124 if (!$block->instance_copy($originalid)) {
125 debugging("Unable to copy block-specific data for original block instance: $originalid
126 to new block instance: $instance->id", DEBUG_DEVELOPER);
130 // Clone block position overrides.
131 if ($blockpositions = $DB->get_records('block_positions',
132 ['subpage' => $systempage->id, 'pagetype' => $pagetype, 'contextid' => $systemcontext->id])) {
133 foreach ($blockpositions as &$positions) {
134 $positions->subpage = $page->id;
135 $positions->contextid = $usercontext->id;
136 if (array_key_exists($positions->blockinstanceid, $newblockinstanceids)) {
137 // For block instances that were defined on the default dashboard and copied to the user dashboard
138 // use the new blockinstanceid.
139 $positions->blockinstanceid = $newblockinstanceids[$positions->blockinstanceid];
141 unset($positions->id);
143 $DB->insert_records('block_positions', $blockpositions);
146 return $page;
150 * For a given user, this deletes their My Moodle page and returns them to the system default.
152 * @param int $userid the id of the user whose page should be reset
153 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
154 * @param string $pagetype either my-index or user-profile
155 * @param string $pagename Differentiate between standard /my or /courses pages.
156 * @return mixed system page, or false on error
158 function my_reset_page(
159 int $userid,
160 int $private = MY_PAGE_PRIVATE,
161 string $pagetype='my-index',
162 string $pagename = MY_PAGE_DEFAULT
164 global $DB, $CFG;
166 $page = my_get_page($userid, $private, $pagename);
167 if ($page->userid == $userid) {
168 $context = context_user::instance($userid);
169 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
170 'pagetypepattern' => $pagetype))) {
171 foreach ($blocks as $block) {
172 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
173 blocks_delete_instance($block);
177 $DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]);
178 $DB->delete_records('my_pages', array('id' => $page->id, 'name' => $pagename));
181 // Get the system default page
182 if (!$systempage = $DB->get_record(
183 'my_pages',
184 array('userid' => null, 'name' => $pagename, 'private' => $private),
185 '*',
186 IGNORE_MULTIPLE
187 )) {
188 return false; // error
191 // Trigger dashboard has been reset event.
192 $eventparams = array(
193 'context' => context_user::instance($userid),
194 'other' => array(
195 'private' => $private,
196 'pagetype' => $pagetype,
199 $event = \core\event\dashboard_reset::create($eventparams);
200 $event->trigger();
201 return $systempage;
205 * Resets the page customisations for all users.
207 * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
208 * @param string $pagetype Either my-index or user-profile.
209 * @param progress_bar|null $progressbar A progress bar to update.
210 * @param string $pagename Differentiate between standard /my or /courses pages.
211 * @return void
213 function my_reset_page_for_all_users(
214 int $private = MY_PAGE_PRIVATE,
215 string $pagetype = 'my-index',
216 ?progress_bar $progressbar = null,
217 string $pagename = MY_PAGE_DEFAULT
219 global $DB;
221 // This may take a while. Raise the execution time limit.
222 core_php_time_limit::raise();
224 $users = $DB->get_fieldset_select(
225 'my_pages',
226 'DISTINCT(userid)',
227 'userid IS NOT NULL AND private = :private',
228 ['private' => $private]
230 $chunks = array_chunk($users, 20);
232 if (!empty($progressbar) && count($chunks) > 0) {
233 $count = count($chunks);
234 $message = get_string('inprogress');
235 $progressbar->update(0, $count, $message);
238 foreach ($chunks as $key => $userchunk) {
239 list($infragment, $inparams) = $DB->get_in_or_equal($userchunk, SQL_PARAMS_NAMED);
240 // Find all the user pages and all block instances in them.
241 $sql = "SELECT bi.id
242 FROM {my_pages} p
243 JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
244 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
245 AND bi.pagetypepattern = :pagetypepattern
246 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
247 WHERE p.private = :private
248 AND p.name = :name
249 AND p.userid $infragment";
251 $params = array_merge([
252 'private' => $private,
253 'usercontextlevel' => CONTEXT_USER,
254 'pagetypepattern' => $pagetype,
255 'name' => $pagename
256 ], $inparams);
257 $blockids = $DB->get_fieldset_sql($sql, $params);
259 // Wrap the SQL queries in a transaction.
260 $transaction = $DB->start_delegated_transaction();
262 // Delete the block instances.
263 if (!empty($blockids)) {
264 blocks_delete_instances($blockids);
267 // Finally delete the pages.
268 $DB->delete_records_select(
269 'my_pages',
270 "userid $infragment AND private = :private",
271 array_merge(['private' => $private], $inparams)
274 // We should be good to go now.
275 $transaction->allow_commit();
277 if (!empty($progressbar)) {
278 $progressbar->update(((int) $key + 1), $count, $message);
282 // Trigger dashboard has been reset event.
283 $eventparams = array(
284 'context' => context_system::instance(),
285 'other' => array(
286 'private' => $private,
287 'pagetype' => $pagetype,
290 $event = \core\event\dashboards_reset::create($eventparams);
291 $event->trigger();
293 if (!empty($progressbar)) {
294 $progressbar->update(1, 1, get_string('completed'));
298 class my_syspage_block_manager extends block_manager {
299 // HACK WARNING!
300 // TODO: figure out a better way to do this
302 * Load blocks using the system context, rather than the user's context.
304 * This is needed because the My Moodle pages set the page context to the
305 * user's context for access control, etc. But the blocks for the system
306 * pages are stored in the system context.
308 public function load_blocks($includeinvisible = null) {
309 $origcontext = $this->page->context;
310 $this->page->context = context_system::instance();
311 parent::load_blocks($includeinvisible);
312 $this->page->context = $origcontext;