Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 30 define('MY_PAGE_PUBLIC', 0); 31 define('MY_PAGE_PRIVATE', 1); 32 33 require_once("$CFG->libdir/blocklib.php"); 34 35 /* 36 * For a given user, this returns the $page information for their My Moodle page 37 * 38 */ 39 function my_get_page($userid, $private=MY_PAGE_PRIVATE) { 40 global $DB, $CFG; 41 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; 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 */ 58 function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') { 59 global $DB; 60 61 if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) { 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; 74 $page->id = $DB->insert_record('my_pages', $page); 75 76 // Clone ALL the associated blocks as well 77 $systemcontext = context_system::instance(); 78 $usercontext = context_user::instance($userid); 79 80 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id, 81 'pagetypepattern' => $pagetype, 82 'subpagepattern' => $systempage->id)); 83 $roles = get_all_roles(); 84 $newblockinstanceids = []; 85 foreach ($blockinstances as $instance) { 86 $originalid = $instance->id; 87 $originalcontext = context_block::instance($originalid); 88 unset($instance->id); 89 $instance->parentcontextid = $usercontext->id; 90 $instance->subpagepattern = $page->id; 91 $instance->timecreated = time(); 92 $instance->timemodified = $instance->timecreated; 93 $instance->id = $DB->insert_record('block_instances', $instance); 94 $newblockinstanceids[$originalid] = $instance->id; 95 $blockcontext = context_block::instance($instance->id); // Just creates the context record 96 $block = block_instance($instance->blockname, $instance); 97 if (!$block->instance_copy($originalid)) { 98 debugging("Unable to copy block-specific data for original block instance: $originalid 99 to new block instance: $instance->id", DEBUG_DEVELOPER); 100 } 101 // Check if there are any overrides on this block instance. 102 // We check against all roles, not just roles assigned to the user. 103 // This is so any overrides that are applied to the system default page 104 // will be applied to the user's page as well, even if their role assignment changes in the future. 105 foreach ($roles as $role) { 106 $rolecapabilities = get_capabilities_from_role_on_context($role, $originalcontext); 107 // If there are overrides, then apply them to the new block instance. 108 foreach ($rolecapabilities as $rolecapability) { 109 role_change_permission( 110 $rolecapability->roleid, 111 $blockcontext, 112 $rolecapability->capability, 113 $rolecapability->permission 114 ); 115 } 116 } 117 } 118 119 // Clone block position overrides. 120 if ($blockpositions = $DB->get_records('block_positions', 121 ['subpage' => $systempage->id, 'pagetype' => $pagetype, 'contextid' => $systemcontext->id])) { 122 foreach ($blockpositions as &$positions) { 123 $positions->subpage = $page->id; 124 $positions->contextid = $usercontext->id; 125 if (array_key_exists($positions->blockinstanceid, $newblockinstanceids)) { 126 // For block instances that were defined on the default dashboard and copied to the user dashboard 127 // use the new blockinstanceid. 128 $positions->blockinstanceid = $newblockinstanceids[$positions->blockinstanceid]; 129 } 130 unset($positions->id); 131 } 132 $DB->insert_records('block_positions', $blockpositions); 133 } 134 135 return $page; 136 } 137 138 /* 139 * For a given user, this deletes their My Moodle page and returns them to the system default. 140 * 141 * @param int $userid the id of the user whose page should be reset 142 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC 143 * @param string $pagetype either my-index or user-profile 144 * @return mixed system page, or false on error 145 */ 146 function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') { 147 global $DB, $CFG; 148 149 $page = my_get_page($userid, $private); 150 if ($page->userid == $userid) { 151 $context = context_user::instance($userid); 152 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id, 153 'pagetypepattern' => $pagetype))) { 154 foreach ($blocks as $block) { 155 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) { 156 blocks_delete_instance($block); 157 } 158 } 159 } 160 $DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]); 161 $DB->delete_records('my_pages', array('id' => $page->id)); 162 } 163 164 // Get the system default page 165 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) { 166 return false; // error 167 } 168 169 // Trigger dashboard has been reset event. 170 $eventparams = array( 171 'context' => context_user::instance($userid), 172 'other' => array( 173 'private' => $private, 174 'pagetype' => $pagetype, 175 ), 176 ); 177 $event = \core\event\dashboard_reset::create($eventparams); 178 $event->trigger(); 179 return $systempage; 180 } 181 182 /** 183 * Resets the page customisations for all users. 184 * 185 * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC. 186 * @param string $pagetype Either my-index or user-profile. 187 * @param progress_bar $progressbar A progress bar to update. 188 * @return void 189 */ 190 function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index', $progressbar = null) { 191 global $DB; 192 193 // This may take a while. Raise the execution time limit. 194 core_php_time_limit::raise(); 195 196 $users = $DB->get_fieldset_select( 197 'my_pages', 198 'DISTINCT(userid)', 199 'userid IS NOT NULL AND private = :private', 200 ['private' => $private] 201 ); 202 $chunks = array_chunk($users, 20); 203 204 if (!empty($progressbar) && count($chunks) > 0) { 205 $count = count($chunks); 206 $message = get_string('inprogress'); 207 $progressbar->update(0, $count, $message); 208 } 209 210 foreach ($chunks as $key => $userchunk) { 211 list($infragment, $inparams) = $DB->get_in_or_equal($userchunk, SQL_PARAMS_NAMED); 212 // Find all the user pages and all block instances in them. 213 $sql = "SELECT bi.id 214 FROM {my_pages} p 215 JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel 216 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id 217 AND bi.pagetypepattern = :pagetypepattern 218 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ") 219 WHERE p.private = :private 220 AND p.userid $infragment"; 221 222 $params = array_merge([ 223 'private' => $private, 224 'usercontextlevel' => CONTEXT_USER, 225 'pagetypepattern' => $pagetype 226 ], $inparams); 227 $blockids = $DB->get_fieldset_sql($sql, $params); 228 229 // Wrap the SQL queries in a transaction. 230 $transaction = $DB->start_delegated_transaction(); 231 232 // Delete the block instances. 233 if (!empty($blockids)) { 234 blocks_delete_instances($blockids); 235 } 236 237 // Finally delete the pages. 238 $DB->delete_records_select( 239 'my_pages', 240 "userid $infragment AND private = :private", 241 array_merge(['private' => $private], $inparams) 242 ); 243 244 // We should be good to go now. 245 $transaction->allow_commit(); 246 247 if (!empty($progressbar)) { 248 $progressbar->update(((int) $key + 1), $count, $message); 249 } 250 } 251 252 // Trigger dashboard has been reset event. 253 $eventparams = array( 254 'context' => context_system::instance(), 255 'other' => array( 256 'private' => $private, 257 'pagetype' => $pagetype, 258 ), 259 ); 260 $event = \core\event\dashboards_reset::create($eventparams); 261 $event->trigger(); 262 263 if (!empty($progressbar)) { 264 $progressbar->update(1, 1, get_string('completed')); 265 } 266 } 267 268 class my_syspage_block_manager extends block_manager { 269 // HACK WARNING! 270 // TODO: figure out a better way to do this 271 /** 272 * Load blocks using the system context, rather than the user's context. 273 * 274 * This is needed because the My Moodle pages set the page context to the 275 * user's context for access control, etc. But the blocks for the system 276 * pages are stored in the system context. 277 */ 278 public function load_blocks($includeinvisible = null) { 279 $origcontext = $this->page->context; 280 $this->page->context = context_system::instance(); 281 parent::load_blocks($includeinvisible); 282 $this->page->context = $origcontext; 283 } 284 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body