MDL-77105 course_format: Add 'nofilter' class when necessary
[moodle.git] / my / lib.php
blob1ed7676ceed2c9cb94ea500a1e0b7ed01c335091
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 (empty($block) || !$block->instance_copy($originalid)) {
125 debugging("Unable to copy block-specific data for original block
126 instance: $originalid to new block instance: $instance->id for
127 block: $instance->blockname", DEBUG_DEVELOPER);
131 // Clone block position overrides.
132 if ($blockpositions = $DB->get_records('block_positions',
133 ['subpage' => $systempage->id, 'pagetype' => $pagetype, 'contextid' => $systemcontext->id])) {
134 foreach ($blockpositions as &$positions) {
135 $positions->subpage = $page->id;
136 $positions->contextid = $usercontext->id;
137 if (array_key_exists($positions->blockinstanceid, $newblockinstanceids)) {
138 // For block instances that were defined on the default dashboard and copied to the user dashboard
139 // use the new blockinstanceid.
140 $positions->blockinstanceid = $newblockinstanceids[$positions->blockinstanceid];
142 unset($positions->id);
144 $DB->insert_records('block_positions', $blockpositions);
147 return $page;
151 * For a given user, this deletes their My Moodle page and returns them to the system default.
153 * @param int $userid the id of the user whose page should be reset
154 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
155 * @param string $pagetype either my-index or user-profile
156 * @param string $pagename Differentiate between standard /my or /courses pages.
157 * @return mixed system page, or false on error
159 function my_reset_page(
160 int $userid,
161 int $private = MY_PAGE_PRIVATE,
162 string $pagetype='my-index',
163 string $pagename = MY_PAGE_DEFAULT
165 global $DB, $CFG;
167 $page = my_get_page($userid, $private, $pagename);
168 if ($page->userid == $userid) {
169 $context = context_user::instance($userid);
170 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
171 'pagetypepattern' => $pagetype))) {
172 foreach ($blocks as $block) {
173 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
174 blocks_delete_instance($block);
178 $DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]);
179 $DB->delete_records('my_pages', array('id' => $page->id, 'name' => $pagename));
182 // Get the system default page
183 if (!$systempage = $DB->get_record(
184 'my_pages',
185 array('userid' => null, 'name' => $pagename, 'private' => $private),
186 '*',
187 IGNORE_MULTIPLE
188 )) {
189 return false; // error
192 // Trigger dashboard has been reset event.
193 $eventparams = array(
194 'context' => context_user::instance($userid),
195 'other' => array(
196 'private' => $private,
197 'pagetype' => $pagetype,
200 $event = \core\event\dashboard_reset::create($eventparams);
201 $event->trigger();
202 return $systempage;
206 * Resets the page customisations for all users.
208 * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
209 * @param string $pagetype Either my-index or user-profile.
210 * @param progress_bar|null $progressbar A progress bar to update.
211 * @param string $pagename Differentiate between standard /my or /courses pages.
212 * @return void
214 function my_reset_page_for_all_users(
215 int $private = MY_PAGE_PRIVATE,
216 string $pagetype = 'my-index',
217 ?progress_bar $progressbar = null,
218 string $pagename = MY_PAGE_DEFAULT
220 global $DB;
222 // This may take a while. Raise the execution time limit.
223 core_php_time_limit::raise();
225 $users = $DB->get_fieldset_select(
226 'my_pages',
227 'DISTINCT(userid)',
228 'userid IS NOT NULL AND private = :private',
229 ['private' => $private]
231 $chunks = array_chunk($users, 20);
233 if (!empty($progressbar) && count($chunks) > 0) {
234 $count = count($chunks);
235 $message = get_string('inprogress');
236 $progressbar->update(0, $count, $message);
239 foreach ($chunks as $key => $userchunk) {
240 list($infragment, $inparams) = $DB->get_in_or_equal($userchunk, SQL_PARAMS_NAMED);
241 // Find all the user pages and all block instances in them.
242 $sql = "SELECT bi.id
243 FROM {my_pages} p
244 JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
245 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
246 AND bi.pagetypepattern = :pagetypepattern
247 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_cast_to_char('p.id') . ")
248 WHERE p.private = :private
249 AND p.name = :name
250 AND p.userid $infragment";
252 $params = array_merge([
253 'private' => $private,
254 'usercontextlevel' => CONTEXT_USER,
255 'pagetypepattern' => $pagetype,
256 'name' => $pagename
257 ], $inparams);
258 $blockids = $DB->get_fieldset_sql($sql, $params);
260 // Wrap the SQL queries in a transaction.
261 $transaction = $DB->start_delegated_transaction();
263 // Delete the block instances.
264 if (!empty($blockids)) {
265 blocks_delete_instances($blockids);
268 // Finally delete the pages.
269 $DB->delete_records_select(
270 'my_pages',
271 "userid $infragment AND private = :private",
272 array_merge(['private' => $private], $inparams)
275 // We should be good to go now.
276 $transaction->allow_commit();
278 if (!empty($progressbar)) {
279 $progressbar->update(((int) $key + 1), $count, $message);
283 // Trigger dashboard has been reset event.
284 $eventparams = array(
285 'context' => context_system::instance(),
286 'other' => array(
287 'private' => $private,
288 'pagetype' => $pagetype,
291 $event = \core\event\dashboards_reset::create($eventparams);
292 $event->trigger();
294 if (!empty($progressbar)) {
295 $progressbar->update(1, 1, get_string('completed'));
299 class my_syspage_block_manager extends block_manager {
300 // HACK WARNING!
301 // TODO: figure out a better way to do this
303 * Load blocks using the system context, rather than the user's context.
305 * This is needed because the My Moodle pages set the page context to the
306 * user's context for access control, etc. But the blocks for the system
307 * pages are stored in the system context.
309 public function load_blocks($includeinvisible = null) {
310 $origcontext = $this->page->context;
311 $this->page->context = context_system::instance();
312 parent::load_blocks($includeinvisible);
313 $this->page->context = $origcontext;