lib.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * My Moodle -- a user's personal dashboard
  18. *
  19. * This file contains common functions for the dashboard and profile pages.
  20. *
  21. * @package moodlecore
  22. * @subpackage my
  23. * @copyright 2010 Remote-Learner.net
  24. * @author Hubert Chathi <hubert@remote-learner.net>
  25. * @author Olav Jordan <olav.jordan@remote-learner.net>
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27. */
  28. define('MY_PAGE_PUBLIC', 0);
  29. define('MY_PAGE_PRIVATE', 1);
  30. require_once("$CFG->libdir/blocklib.php");
  31. /*
  32. * For a given user, this returns the $page information for their My Moodle page
  33. *
  34. */
  35. function my_get_page($userid, $private=MY_PAGE_PRIVATE) {
  36. global $DB, $CFG;
  37. if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them
  38. // Does the user have their own page defined? If so, return it.
  39. if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
  40. return $customised;
  41. }
  42. }
  43. // Otherwise return the system default page
  44. return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private));
  45. }
  46. /*
  47. * This copies a system default page to the current user
  48. *
  49. */
  50. function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
  51. global $DB;
  52. if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
  53. return $customised; // We're done!
  54. }
  55. // Get the system default page
  56. if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
  57. return false; // error
  58. }
  59. // Clone the basic system page record
  60. $page = clone($systempage);
  61. unset($page->id);
  62. $page->userid = $userid;
  63. $page->id = $DB->insert_record('my_pages', $page);
  64. // Clone ALL the associated blocks as well
  65. $systemcontext = context_system::instance();
  66. $usercontext = context_user::instance($userid);
  67. $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
  68. 'pagetypepattern' => $pagetype,
  69. 'subpagepattern' => $systempage->id));
  70. foreach ($blockinstances as $instance) {
  71. $originalid = $instance->id;
  72. unset($instance->id);
  73. $instance->parentcontextid = $usercontext->id;
  74. $instance->subpagepattern = $page->id;
  75. $instance->id = $DB->insert_record('block_instances', $instance);
  76. $blockcontext = context_block::instance($instance->id); // Just creates the context record
  77. $block = block_instance($instance->blockname, $instance);
  78. if (!$block->instance_copy($originalid)) {
  79. debugging("Unable to copy block-specific data for original block instance: $originalid
  80. to new block instance: $instance->id", DEBUG_DEVELOPER);
  81. }
  82. }
  83. // FIXME: block position overrides should be merged in with block instance
  84. //$blockpositions = $DB->get_records('block_positions', array('subpage' => $page->name));
  85. //foreach($blockpositions as $positions) {
  86. // $positions->subpage = $page->name;
  87. // $DB->insert_record('block_positions', $tc);
  88. //}
  89. return $page;
  90. }
  91. /*
  92. * For a given user, this deletes their My Moodle page and returns them to the system default.
  93. *
  94. * @param int $userid the id of the user whose page should be reset
  95. * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
  96. * @param string $pagetype either my-index or user-profile
  97. * @return mixed system page, or false on error
  98. */
  99. function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
  100. global $DB, $CFG;
  101. $page = my_get_page($userid, $private);
  102. if ($page->userid == $userid) {
  103. $context = context_user::instance($userid);
  104. if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
  105. 'pagetypepattern' => $pagetype))) {
  106. foreach ($blocks as $block) {
  107. if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
  108. blocks_delete_instance($block);
  109. }
  110. }
  111. }
  112. $DB->delete_records('my_pages', array('id' => $page->id));
  113. }
  114. // Get the system default page
  115. if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
  116. return false; // error
  117. }
  118. // Trigger dashboard has been reset event.
  119. $eventparams = array('context' => context_user::instance($userid));
  120. $event = \core\event\dashboard_reset::create($eventparams);
  121. $event->trigger();
  122. return $systempage;
  123. }
  124. /**
  125. * Resets the page customisations for all users.
  126. *
  127. * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
  128. * @param string $pagetype Either my-index or user-profile.
  129. * @return void
  130. */
  131. function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index') {
  132. global $DB;
  133. // This may take a while. Raise the execution time limit.
  134. core_php_time_limit::raise();
  135. // Find all the user pages and all block instances in them.
  136. $sql = "SELECT bi.id
  137. FROM {my_pages} p
  138. JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
  139. JOIN {block_instances} bi ON bi.parentcontextid = ctx.id AND
  140. bi.pagetypepattern = :pagetypepattern AND
  141. (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
  142. WHERE p.private = :private";
  143. $params = array('private' => $private,
  144. 'usercontextlevel' => CONTEXT_USER,
  145. 'pagetypepattern' => $pagetype);
  146. $blockids = $DB->get_fieldset_sql($sql, $params);
  147. // Wrap the SQL queries in a transaction.
  148. $transaction = $DB->start_delegated_transaction();
  149. // Delete the block instances.
  150. if (!empty($blockids)) {
  151. blocks_delete_instances($blockids);
  152. }
  153. // Finally delete the pages.
  154. $DB->delete_records_select('my_pages', 'userid IS NOT NULL AND private = :private', ['private' => $private]);
  155. // We should be good to go now.
  156. $transaction->allow_commit();
  157. // Trigger dashboard has been reset event.
  158. $eventparams = array('context' => context_system::instance());
  159. $event = \core\event\dashboards_reset::create($eventparams);
  160. $event->trigger();
  161. }
  162. class my_syspage_block_manager extends block_manager {
  163. // HACK WARNING!
  164. // TODO: figure out a better way to do this
  165. /**
  166. * Load blocks using the system context, rather than the user's context.
  167. *
  168. * This is needed because the My Moodle pages set the page context to the
  169. * user's context for access control, etc. But the blocks for the system
  170. * pages are stored in the system context.
  171. */
  172. public function load_blocks($includeinvisible = null) {
  173. $origcontext = $this->page->context;
  174. $this->page->context = context_system::instance();
  175. parent::load_blocks($includeinvisible);
  176. $this->page->context = $origcontext;
  177. }
  178. }