MDL-14406
[moodle-linuxchix.git] / blog / blogpage.php
blob7e6366d99dd15171863b358df02c68e2190d842a
1 <?php // $Id$
3 /**
4 * Definition of blog page type.
5 */
6 define('PAGE_BLOG_VIEW', 'blog-view');
8 // Blog class derived from moodle's page class
9 class page_blog extends page_base {
11 var $editing = false;
12 var $courserecord = NULL;
13 var $courseid = NULL;
14 var $filtertype = NULL;
15 var $filterselect = NULL;
16 var $tagid = NULL;
18 // Mandatory; should return our identifier.
19 function get_type() {
20 global $CFG;
21 require_once($CFG->dirroot .'/blog/lib.php');
22 return PAGE_BLOG_VIEW;
25 // we have no format type, use 'blog'
26 //I think it's a bug, but if this is left the default NULL value then pages can
27 //fail to load completely
28 function get_format_name() {
29 global $CFG;
30 require_once($CFG->dirroot .'/blog/lib.php');
31 return PAGE_BLOG_VIEW;
34 // Do any validation of the officially recognized bits of the data and forward to parent.
35 // Do NOT load up "expensive" resouces (e.g. SQL data) here!
36 function init_quick($data) {
37 parent::init_quick($data);
38 if (empty($data->pageid)) {
39 //if no pageid then the user is viewing a collection of blog entries
40 $this->id = 0; //set blog id to 0
44 // Here you should load up all heavy-duty data for your page. Basically everything that
45 // does not NEED to be loaded for the class to make basic decisions should NOT be loaded
46 // in init_quick() and instead deferred here. Of course this function had better recognize
47 // $this->full_init_done to prevent wasteful multiple-time data retrieval.
48 function init_full() {
49 if ($this->full_init_done) {
50 return;
52 // I need to determine how best to utilize this function. Most init
53 // is already done before we get here in blogFilter and blogInfo
55 if ($this->courseid == 0 || $this->courseid == 1 || !is_numeric($this->courseid) ) {
56 $this->courseid = '';
57 $courserecord = NULL;
58 } else {
59 if (! ($courserecord = get_record('course', 'id', $this->courseid)) ) {
60 error( 'You are tring to view an invalid course. Id: ('. $this->courseid .')' );
63 $this->full_init_done = true;
66 // For this test page, only admins are going to be allowed editing (for simplicity).
67 function user_allowed_editing() {
68 if (isloggedin() && !isguest()) {
69 return true;
71 return false;
74 // Also, admins are considered to have "always on" editing (I wanted to avoid duplicating
75 // the code that turns editing on/off here; you can roll your own or copy course/view.php).
76 function user_is_editing() {
77 global $SESSION;
79 if (isloggedin() && !isguest()) {
80 $this->editing = !empty($SESSION->blog_editing_enabled);
81 return $this->editing;
83 return false;
86 //over-ride parent method's print_header because blog already passes more than just the title along
87 function print_header($pageTitle='', $pageHeading='', $pageNavigation='', $pageFocus='', $pageMeta='') {
88 global $USER;
90 $this->init_full();
91 $extraheader = '';
92 if (!empty($USER) && !empty($USER->id)) {
93 $extraheader = $this->get_extra_header_string();
95 print_header($pageTitle, $pageHeading, $pageNavigation, $pageFocus, $pageMeta, true, $extraheader );
98 // This should point to the script that displays us
99 function url_get_path() {
100 global $CFG;
102 return $CFG->wwwroot .'/blog/index.php';
105 function url_get_parameters() {
107 $array = array();
108 if (!$this->full_init_done) {
109 $array['userid'] = $this->id;
110 return $array;
113 if (!empty($this->courseid)) {
114 $array['courseid'] = $this->courseid;
116 if (!empty($this->filtertype)) {
117 $array['filtertype'] = $this->filtertype;
119 if (!empty($this->filterselect)) {
120 $array['filterselect'] = $this->filterselect;
122 if (!empty($this->tagid)) {
123 $array['tagid'] = $this->tagid;
125 return $array;
129 // Having defined all identifiers we need, here we declare which block positions we are
130 // going to support.
131 function blocks_get_positions() {
132 return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT);
135 // When a new block is created in this page, which position should it go to?
136 function blocks_default_position() {
137 return BLOCK_POS_RIGHT;
140 // When we are creating a new page, use the data at your disposal to provide a textual representation of the
141 // blocks that are going to get added to this new page. Delimit block names with commas (,) and use double
142 // colons (:) to delimit between block positions in the page. See blocks_get_positions() for additional info.
143 function blocks_get_default() {
144 global $CFG;
146 $this->init_full();
148 // It's a normal blog page
149 if (!empty($CFG->{'defaultblocks_'. $this->get_type()})) {
150 $blocknames = $CFG->{'defaultblocks_'. $this->get_type()};
151 } else {
152 /// Failsafe - in case nothing was defined.
153 $blocknames = 'admin,calendar_month,online_users,blog_menu';
156 return $blocknames;
159 // And finally, a little block move logic. Given a block's previous position and where
160 // we want to move it to, return its new position. Pretty self-documenting.
161 function blocks_move_position(&$instance, $move) {
162 if ($instance->position == BLOCK_POS_LEFT && $move == BLOCK_MOVE_RIGHT) {
163 return BLOCK_POS_RIGHT;
164 } else if ($instance->position == BLOCK_POS_RIGHT && $move == BLOCK_MOVE_LEFT) {
165 return BLOCK_POS_LEFT;
167 return $instance->position;
170 /////////// Blog page specific functions
171 function get_extra_header_string() {
172 global $SESSION, $CFG, $USER;
174 $editformstring = '';
175 if ($this->user_allowed_editing()) {
176 if (!empty($SESSION->blog_editing_enabled)) {
177 $editingString = get_string('turneditingoff');
178 } else {
179 $editingString = get_string('turneditingon');
182 $params = $this->url_get_parameters();
183 $params['edit'] = empty($SESSION->blog_editing_enabled) ? 1 : 0;
184 $paramstring = '';
185 foreach ($params as $key=>$val) {
186 $paramstring .= '<input type="hidden" name="'.$key.'" value="'.s($val).'" />';
189 $editformstring = '<form '.$CFG->frametarget.' method="get" action="'.$this->url_get_path().'"><div>'
190 .$paramstring.'<input type="submit" value="'.$editingString.'" /></div></form>';
193 return $editformstring;