weekly release 4.0dev
[moodle.git] / my / lib.php
CommitLineData
03d9401e
MD
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 * My Moodle -- a user's personal dashboard
19 *
20 * This file contains common functions for the dashboard and profile pages.
21 *
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
28 */
29
a9637e7d 30define('MY_PAGE_PUBLIC', 0);
03d9401e
MD
31define('MY_PAGE_PRIVATE', 1);
32
33require_once("$CFG->libdir/blocklib.php");
34
a9637e7d 35/*
03d9401e
MD
36 * For a given user, this returns the $page information for their My Moodle page
37 *
38 */
39function my_get_page($userid, $private=MY_PAGE_PRIVATE) {
40 global $DB, $CFG;
41
b8fa1826 42 if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them
03d9401e
MD
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;
46 }
47 }
48
49 // Otherwise return the system default page
50 return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private));
51}
52
53
54/*
55 * This copies a system default page to the current user
56 *
57 */
58function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
59 global $DB;
60
56af0fbd 61 if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
03d9401e
MD
62 return $customised; // We're done!
63 }
64
65 // Get the system default page
66 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
67 return false; // error
68 }
69
70 // Clone the basic system page record
71 $page = clone($systempage);
72 unset($page->id);
73 $page->userid = $userid;
a9637e7d
PS
74 $page->id = $DB->insert_record('my_pages', $page);
75
03d9401e 76 // Clone ALL the associated blocks as well
bf0f06b1
AA
77 $systemcontext = context_system::instance();
78 $usercontext = context_user::instance($userid);
03d9401e
MD
79
80 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
81 'pagetypepattern' => $pagetype,
82 'subpagepattern' => $systempage->id));
34c9d28e 83 $newblockinstanceids = [];
03d9401e 84 foreach ($blockinstances as $instance) {
ad5c3230 85 $originalid = $instance->id;
03d9401e
MD
86 unset($instance->id);
87 $instance->parentcontextid = $usercontext->id;
88 $instance->subpagepattern = $page->id;
557554f9 89 $instance->timecreated = time();
90 $instance->timemodified = $instance->timecreated;
a9637e7d 91 $instance->id = $DB->insert_record('block_instances', $instance);
34c9d28e 92 $newblockinstanceids[$originalid] = $instance->id;
bf0f06b1 93 $blockcontext = context_block::instance($instance->id); // Just creates the context record
ad5c3230
NL
94 $block = block_instance($instance->blockname, $instance);
95 if (!$block->instance_copy($originalid)) {
96 debugging("Unable to copy block-specific data for original block instance: $originalid
97 to new block instance: $instance->id", DEBUG_DEVELOPER);
98 }
03d9401e
MD
99 }
100
34c9d28e
MG
101 // Clone block position overrides.
102 if ($blockpositions = $DB->get_records('block_positions',
103 ['subpage' => $systempage->id, 'pagetype' => $pagetype, 'contextid' => $systemcontext->id])) {
104 foreach ($blockpositions as &$positions) {
105 $positions->subpage = $page->id;
106 $positions->contextid = $usercontext->id;
107 if (array_key_exists($positions->blockinstanceid, $newblockinstanceids)) {
108 // For block instances that were defined on the default dashboard and copied to the user dashboard
109 // use the new blockinstanceid.
110 $positions->blockinstanceid = $newblockinstanceids[$positions->blockinstanceid];
111 }
112 unset($positions->id);
113 }
114 $DB->insert_records('block_positions', $blockpositions);
115 }
03d9401e
MD
116
117 return $page;
118}
119
41dcfbf1
MA
120/*
121 * For a given user, this deletes their My Moodle page and returns them to the system default.
122 *
123 * @param int $userid the id of the user whose page should be reset
124 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
125 * @param string $pagetype either my-index or user-profile
126 * @return mixed system page, or false on error
127 */
128function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
129 global $DB, $CFG;
130
131 $page = my_get_page($userid, $private);
132 if ($page->userid == $userid) {
133 $context = context_user::instance($userid);
134 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
135 'pagetypepattern' => $pagetype))) {
136 foreach ($blocks as $block) {
137 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
138 blocks_delete_instance($block);
139 }
140 }
141 }
34c9d28e 142 $DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]);
41dcfbf1
MA
143 $DB->delete_records('my_pages', array('id' => $page->id));
144 }
145
146 // Get the system default page
147 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
148 return false; // error
149 }
94c7e9f1
SB
150
151 // Trigger dashboard has been reset event.
14d6c1cf
LC
152 $eventparams = array(
153 'context' => context_user::instance($userid),
154 'other' => array(
155 'private' => $private,
156 'pagetype' => $pagetype,
157 ),
158 );
94c7e9f1
SB
159 $event = \core\event\dashboard_reset::create($eventparams);
160 $event->trigger();
41dcfbf1
MA
161 return $systempage;
162}
163
ad347f68
FM
164/**
165 * Resets the page customisations for all users.
166 *
167 * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
168 * @param string $pagetype Either my-index or user-profile.
4e72d591 169 * @param progress_bar $progressbar A progress bar to update.
ad347f68
FM
170 * @return void
171 */
4e72d591 172function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index', $progressbar = null) {
ad347f68
FM
173 global $DB;
174
46b888db
JP
175 // This may take a while. Raise the execution time limit.
176 core_php_time_limit::raise();
177
4e72d591
PB
178 $users = $DB->get_fieldset_select(
179 'my_pages',
180 'DISTINCT(userid)',
181 'userid IS NOT NULL AND private = :private',
182 ['private' => $private]
183 );
184 $chunks = array_chunk($users, 20);
185
186 if (!empty($progressbar) && count($chunks) > 0) {
187 $count = count($chunks);
188 $message = get_string('inprogress');
189 $progressbar->update(0, $count, $message);
ad347f68
FM
190 }
191
4e72d591
PB
192 foreach ($chunks as $key => $userchunk) {
193 list($infragment, $inparams) = $DB->get_in_or_equal($userchunk, SQL_PARAMS_NAMED);
194 // Find all the user pages and all block instances in them.
195 $sql = "SELECT bi.id
196 FROM {my_pages} p
197 JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
198 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
199 AND bi.pagetypepattern = :pagetypepattern
200 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
201 WHERE p.private = :private
202 AND p.userid $infragment";
203
204 $params = array_merge([
205 'private' => $private,
206 'usercontextlevel' => CONTEXT_USER,
207 'pagetypepattern' => $pagetype
208 ], $inparams);
209 $blockids = $DB->get_fieldset_sql($sql, $params);
210
211 // Wrap the SQL queries in a transaction.
212 $transaction = $DB->start_delegated_transaction();
213
214 // Delete the block instances.
215 if (!empty($blockids)) {
216 blocks_delete_instances($blockids);
217 }
ad347f68 218
4e72d591
PB
219 // Finally delete the pages.
220 $DB->delete_records_select(
221 'my_pages',
222 "userid $infragment AND private = :private",
223 array_merge(['private' => $private], $inparams)
224 );
225
226 // We should be good to go now.
227 $transaction->allow_commit();
228
229 if (!empty($progressbar)) {
230 $progressbar->update(((int) $key + 1), $count, $message);
231 }
232 }
94c7e9f1
SB
233
234 // Trigger dashboard has been reset event.
14d6c1cf
LC
235 $eventparams = array(
236 'context' => context_system::instance(),
237 'other' => array(
238 'private' => $private,
239 'pagetype' => $pagetype,
240 ),
241 );
94c7e9f1
SB
242 $event = \core\event\dashboards_reset::create($eventparams);
243 $event->trigger();
4e72d591
PB
244
245 if (!empty($progressbar)) {
246 $progressbar->update(1, 1, get_string('completed'));
247 }
ad347f68
FM
248}
249
03d9401e
MD
250class my_syspage_block_manager extends block_manager {
251 // HACK WARNING!
252 // TODO: figure out a better way to do this
253 /**
254 * Load blocks using the system context, rather than the user's context.
255 *
256 * This is needed because the My Moodle pages set the page context to the
257 * user's context for access control, etc. But the blocks for the system
258 * pages are stored in the system context.
259 */
260 public function load_blocks($includeinvisible = null) {
261 $origcontext = $this->page->context;
bf0f06b1 262 $this->page->context = context_system::instance();
03d9401e
MD
263 parent::load_blocks($includeinvisible);
264 $this->page->context = $origcontext;
265 }
266}