|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00030 define('MY_PAGE_PUBLIC', 0); 00031 define('MY_PAGE_PRIVATE', 1); 00032 00033 require_once("$CFG->libdir/blocklib.php"); 00034 00035 /* 00036 * For a given user, this returns the $page information for their My Moodle page 00037 * 00038 */ 00039 function my_get_page($userid, $private=MY_PAGE_PRIVATE) { 00040 global $DB, $CFG; 00041 00042 if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them 00043 // Does the user have their own page defined? If so, return it. 00044 if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) { 00045 return $customised; 00046 } 00047 } 00048 00049 // Otherwise return the system default page 00050 return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private)); 00051 } 00052 00053 00054 /* 00055 * This copies a system default page to the current user 00056 * 00057 */ 00058 function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') { 00059 global $DB; 00060 00061 if ($customised = $DB->record_exists('my_pages', array('userid' => $userid, 'private' => $private))) { 00062 return $customised; // We're done! 00063 } 00064 00065 // Get the system default page 00066 if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) { 00067 return false; // error 00068 } 00069 00070 // Clone the basic system page record 00071 $page = clone($systempage); 00072 unset($page->id); 00073 $page->userid = $userid; 00074 $page->id = $DB->insert_record('my_pages', $page); 00075 00076 // Clone ALL the associated blocks as well 00077 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00078 $usercontext = get_context_instance(CONTEXT_USER, $userid); 00079 00080 $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id, 00081 'pagetypepattern' => $pagetype, 00082 'subpagepattern' => $systempage->id)); 00083 foreach ($blockinstances as $instance) { 00084 unset($instance->id); 00085 $instance->parentcontextid = $usercontext->id; 00086 $instance->subpagepattern = $page->id; 00087 $instance->id = $DB->insert_record('block_instances', $instance); 00088 $blockcontext = get_context_instance(CONTEXT_BLOCK, $instance->id); // Just creates the context record 00089 } 00090 00091 // FIXME: block position overrides should be merged in with block instance 00092 //$blockpositions = $DB->get_records('block_positions', array('subpage' => $page->name)); 00093 //foreach($blockpositions as $positions) { 00094 // $positions->subpage = $page->name; 00095 // $DB->insert_record('block_positions', $tc); 00096 //} 00097 00098 return $page; 00099 } 00100 00101 class my_syspage_block_manager extends block_manager { 00102 // HACK WARNING! 00103 // TODO: figure out a better way to do this 00111 public function load_blocks($includeinvisible = null) { 00112 $origcontext = $this->page->context; 00113 $this->page->context = get_context_instance(CONTEXT_SYSTEM); 00114 parent::load_blocks($includeinvisible); 00115 $this->page->context = $origcontext; 00116 } 00117 }