My Moodle MDL-19124 New version of My Moodle, User profiles and Course profiles,...
[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
30define('MY_PAGE_PUBLIC', 0);
31define('MY_PAGE_PRIVATE', 1);
32
33require_once("$CFG->libdir/blocklib.php");
34
35/*
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
42 if (empty($CFG->forcedefaultmymoodle)) { // We 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 */
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;
74 if (!$page->id = $DB->insert_record('my_pages', $page)) {
75 return false;
76 }
77
78 // Clone ALL the associated blocks as well
79 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
80 $usercontext = get_context_instance(CONTEXT_USER, $userid);
81
82 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
83 'pagetypepattern' => $pagetype,
84 'subpagepattern' => $systempage->id));
85 foreach ($blockinstances as $instance) {
86 unset($instance->id);
87 $instance->parentcontextid = $usercontext->id;
88 $instance->subpagepattern = $page->id;
89 if ($instance->id = $DB->insert_record('block_instances', $instance)) {
90 $blockcontext = get_context_instance(CONTEXT_BLOCK, $instance->id); // Just creates the context record
91 }
92 }
93
94 // FIXME: block position overrides should be merged in with block instance
95 //$blockpositions = $DB->get_records('block_positions', array('subpage' => $page->name));
96 //foreach($blockpositions as $positions) {
97 // $positions->subpage = $page->name;
98 // $DB->insert_record('block_positions', $tc);
99 //}
100
101 return $page;
102}
103
104class my_syspage_block_manager extends block_manager {
105 // HACK WARNING!
106 // TODO: figure out a better way to do this
107 /**
108 * Load blocks using the system context, rather than the user's context.
109 *
110 * This is needed because the My Moodle pages set the page context to the
111 * user's context for access control, etc. But the blocks for the system
112 * pages are stored in the system context.
113 */
114 public function load_blocks($includeinvisible = null) {
115 $origcontext = $this->page->context;
116 $this->page->context = get_context_instance(CONTEXT_SYSTEM);
117 parent::load_blocks($includeinvisible);
118 $this->page->context = $origcontext;
119 }
120}