MDL-49650 Templates: Move templates out of invalid sub directory.
[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
61 if ($customised = $DB->record_exists('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;
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));
83 foreach ($blockinstances as $instance) {
84 unset($instance->id);
85 $instance->parentcontextid = $usercontext->id;
86 $instance->subpagepattern = $page->id;
a9637e7d 87 $instance->id = $DB->insert_record('block_instances', $instance);
bf0f06b1 88 $blockcontext = context_block::instance($instance->id); // Just creates the context record
03d9401e
MD
89 }
90
91 // FIXME: block position overrides should be merged in with block instance
92 //$blockpositions = $DB->get_records('block_positions', array('subpage' => $page->name));
93 //foreach($blockpositions as $positions) {
94 // $positions->subpage = $page->name;
95 // $DB->insert_record('block_positions', $tc);
96 //}
97
98 return $page;
99}
100
41dcfbf1
MA
101/*
102 * For a given user, this deletes their My Moodle page and returns them to the system default.
103 *
104 * @param int $userid the id of the user whose page should be reset
105 * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
106 * @param string $pagetype either my-index or user-profile
107 * @return mixed system page, or false on error
108 */
109function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
110 global $DB, $CFG;
111
112 $page = my_get_page($userid, $private);
113 if ($page->userid == $userid) {
114 $context = context_user::instance($userid);
115 if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
116 'pagetypepattern' => $pagetype))) {
117 foreach ($blocks as $block) {
118 if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
119 blocks_delete_instance($block);
120 }
121 }
122 }
123 $DB->delete_records('my_pages', array('id' => $page->id));
124 }
125
126 // Get the system default page
127 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
128 return false; // error
129 }
130 return $systempage;
131}
132
03d9401e
MD
133class my_syspage_block_manager extends block_manager {
134 // HACK WARNING!
135 // TODO: figure out a better way to do this
136 /**
137 * Load blocks using the system context, rather than the user's context.
138 *
139 * This is needed because the My Moodle pages set the page context to the
140 * user's context for access control, etc. But the blocks for the system
141 * pages are stored in the system context.
142 */
143 public function load_blocks($includeinvisible = null) {
144 $origcontext = $this->page->context;
bf0f06b1 145 $this->page->context = context_system::instance();
03d9401e
MD
146 parent::load_blocks($includeinvisible);
147 $this->page->context = $origcontext;
148 }
149}