First Official realease of the "Blocks System".
[moodle.git] / blocks / moodleblock.class.php
CommitLineData
0f3fe4b6 1<?php
2
3define('BLOCK_TYPE_LIST', 1);
4define('BLOCK_TYPE_TEXT', 2);
5
6class MoodleBlock {
7 var $str;
8 var $title = NULL;
9 var $course = NULL;
10 var $content_type = NULL;
11 var $content = NULL;
12 var $edit_controls = NULL;
13 var $version = NULL;
14
15 function name() {
16 // Returns the block name, as present in the class name,
17 // the database, the block directory, etc etc.
18 static $myname;
19 if($myname === NULL) {
20 $myname = strtolower(get_class($this));
21 $myname = substr($myname, strpos($myname, '_') + 1);
22 }
23 return $myname;
24 }
25
26 function get_content() {
27 // This should be implemented by the derived class.
28 return NULL;
29 }
30 function get_title() {
31 // Intentionally doesn't check if a title is set, for _test_self()
32 return $this->title;
33 }
34 function get_content_type() {
35 // Intentionally doesn't check if a content_type is set, for _test_self()
36 return $this->content_type;
37 }
38 function get_version() {
39 // Intentionally doesn't check if a version is set, for _test_self()
40 return $this->version;
41 }
42 function get_header() {
43 // Intentionally doesn't check if a header is set, for _test_self()
44 return $this->header;
45 }
46 function refresh_content() {
47 // Nothing special here, depends on content()
48 $this->content = NULL;
49 return $this->get_content();
50 }
51 function print_block() {
52 // Wrap it up, in case we have buttons too
53 $title = '<div style="float: left;">'.$this->title.'</div>';
54 if($this->edit_controls !== NULL) {
55 $title .= $this->edit_controls;
56 }
57 $this->get_content();
58
59 switch($this->content_type) {
60 case BLOCK_TYPE_TEXT:
61 if ($this->edit_controls !== NULL || !$this->hide_header()) {
62 print_side_block($title, $this->content->text, NULL, NULL, $this->content->footer);
63 } else {
64 print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer);
65 }
66 break;
67 case BLOCK_TYPE_LIST:
68 if ($this->edit_controls !== NULL || !$this->hide_header()) {
69 print_side_block($title, '', $this->content->items, $this->content->icons, $this->content->footer);
70 } else {
71 print_side_block(NULL, '', $this->content->items, $this->content->icons, $this->content->footer);
72 }
73 break;
74 }
75 }
76 function print_shadow() {
77 $title = '<div style="float: left;">'.$this->title.'</div>';
78 if($this->edit_controls !== NULL) {
79 $title .= $this->edit_controls;
80 }
81 print_side_block($title, '&nbsp;', NULL, NULL, '');
82 }
83 function add_edit_controls($options, $blockid) {
84 global $CFG, $THEME;
85
86 // The block may be disabled
87 $blockid = intval($blockid);
88 $enabled = $blockid > 0;
89 $blockid = abs($blockid);
90
91 if (!isset($this->str)) {
92 $this->str->delete = get_string('delete');
93 $this->str->moveup = get_string('moveup');
94 $this->str->movedown = get_string('movedown');
95 $this->str->moveright = get_string('moveright');
96 $this->str->moveleft = get_string('moveleft');
97 $this->str->hide = get_string('hide');
98 $this->str->show = get_string('show');
99 }
100
101 $path = $CFG->wwwroot.'/course';
102
103 if (empty($THEME->custompix)) {
104 $pixpath = $path.'/../pix';
105 } else {
106 $pixpath = $path.'/../theme/'.$CFG->theme.'/pix';
107 }
108
109 $movebuttons = '<div style="float: right;">';
110
111 if($enabled) {
112 $icon = '/t/hide.gif';
113 $title = $this->str->hide;
114 }
115 else {
116 $icon = '/t/show.gif';
117 $title = $this->str->show;
118 }
119
120 $movebuttons .= '<a style="margin-right: 10px;" title="'.$title.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=toggle&amp;blockid='.$blockid.'">' .
121 '<img src="'.$pixpath.$icon.'" /></a>';
122
123 $movebuttons .= '<a style="margin-right: 10px;" title="'.$this->str->delete.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=delete&amp;blockid='.$blockid.'">' .
124 '<img src="'.$pixpath.'/t/delete.gif" /></a>';
125
126 if ($options & BLOCK_MOVE_LEFT) {
127 $movebuttons .= '<a style="margin-right: 3px;" title="'.$this->str->moveleft.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveside&amp;blockid='.$blockid.'">' .
128 '<img src="'.$pixpath.'/t/left.gif" /></a>';
129 }
130 if ($options & BLOCK_MOVE_UP) {
131 $movebuttons .= '<a style="margin-right: 3px;" title="'.$this->str->moveup.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveup&amp;blockid='.$blockid.'">' .
132 '<img src="'.$pixpath.'/t/up.gif" /></a>';
133 }
134 if ($options & BLOCK_MOVE_DOWN) {
135 $movebuttons .= '<a style="margin-right: 3px;" title="'.$this->str->movedown.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=movedown&amp;blockid='.$blockid.'">' .
136 '<img src="'.$pixpath.'/t/down.gif" /></a>';
137 }
138 if ($options & BLOCK_MOVE_RIGHT) {
139 $movebuttons .= '<a style="margin-right: 3px;" title="'.$this->str->moveright.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveside&amp;blockid='.$blockid.'">' .
140 '<img src="'.$pixpath.'/t/right.gif" /></a>';
141 }
142
143 $movebuttons .= '</div>';
144 $this->edit_controls = $movebuttons;
145 }
146
147 function _self_test() {
148 // Tests if this block has been implemented correctly.
149 // Also, $errors isn't used right now
150 $errors = array();
151
152 $correct = true;
153 if($this->get_title() === NULL) {
154 $errors[] = 'title_not_set';
155 $correct = false;
156 }
157 if(!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT))) {
158 $errors[] = 'invalid_content_type';
159 $correct = false;
160 }
161 if($this->get_content() === NULL) {
162 $errors[] = 'content_not_set';
163 $correct = false;
164 }
165 if($this->get_version() === NULL) {
166 $errors[] = 'version_not_set';
167 $correct = false;
168 }
169 $allformats = COURSE_FORMAT_WEEKS | COURSE_FORMAT_TOPICS | COURSE_FORMAT_SOCIAL;
170 if(!($this->applicable_formats() & $allformats)) {
171 $errors[] = 'no_course_formats';
172 $correct = false;
173 }
174 $width = $this->preferred_width();
175 if(!is_int($width) || $width <= 0) {
176 $errors[] = 'invalid_width';
177 $correct = false;
178 }
179 return $correct;
180 }
181
182 function has_config() {
183 return false;
184 }
185 function print_config() {
186 // This does nothing, it's here to prevent errors from
187 // derived classes if they implement has_config() but not print_config()
188 }
189 function handle_config() {
190 // This does nothing, it's here to prevent errors from
191 // derived classes if they implement has_config() but not handle_config()
192 }
193 function applicable_formats() {
194 // Default case: the block can be used in all course types
195 return COURSE_FORMAT_WEEKS | COURSE_FORMAT_TOPICS | COURSE_FORMAT_SOCIAL;
196 }
197 function preferred_width() {
198 // Default case: the block wants to be 180 pixels wide
199 return 180;
200 }
201 function hide_header() {
202 //Default, false--> the header is showed
203 return false;
204 }
205}
206
207?>