4 * This file contains the parent class for moodle blocks, block_base,
5 * as well as the block_nuke subclass.
7 * @author Jon Papaioannou
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
16 * Block type of list. Contents of block should be set as an associative array in the content object as items ($this->content->items). Optionally include footer text in $this->content->footer.
18 define('BLOCK_TYPE_LIST', 1);
21 * Block type of text. Contents of block should be set to standard html text in the content object as items ($this->content->text). Optionally include footer text in $this->content->footer.
23 define('BLOCK_TYPE_TEXT', 2);
26 * Block type of nuke. Compitibility with post nuke blocks. Basically treated as BLOCK_TYPE_TEXT.
28 define('BLOCK_TYPE_NUKE', 3);
31 * Class for describing a moodle block, all Moodle blocks derive from this class
33 * @author Jon Papaioannou
39 * Internal var for storing/caching translated strings
45 * The title of the block to be displayed in the block title area.
51 * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE
52 * @var int $content_type
54 var $content_type = BLOCK_TYPE_TEXT
;
57 * An object to contain the information to be displayed in the block.
58 * @var stdObject $content
63 * A string generated by {@link _add_edit_controls()} to display block manipulation links when the user is in editing mode.
64 * @var string $edit_controls
66 var $edit_controls = NULL;
69 * The current version that the block type defines.
70 * @var string $version
75 * The initialized instance of this block object.
76 * @var block $instance
81 * An object containing the instance configuration information for the current instance of this block.
82 * @var stdObject $config
90 * The class constructor
93 function block_base() {
98 * Fake constructor to keep PHP5 happy
101 function __construct() {
106 * Returns the block name, as present in the class name,
107 * the database, the block directory, etc etc.
112 // Returns the block name, as present in the class name,
113 // the database, the block directory, etc etc.
115 if ($myname === NULL) {
116 $myname = strtolower(get_class($this));
117 $myname = substr($myname, strpos($myname, '_') +
1);
123 * Parent class version of this function simply returns NULL
124 * This should be implemented by the derived class to return
125 * the content object.
129 function get_content() {
130 // This should be implemented by the derived class.
135 * Returns the class $title var value.
137 * Intentionally doesn't check if a title is set.
138 * This is already done in {@link _self_test()}
140 * @return string $this->title
142 function get_title() {
143 // Intentionally doesn't check if a title is set. This is already done in _self_test()
148 * Returns the class $content_type var value.
150 * Intentionally doesn't check if content_type is set.
151 * This is already done in {@link _self_test()}
153 * @return string $this->content_type
155 function get_content_type() {
156 // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
157 return $this->content_type
;
161 * Returns the class $version var value.
163 * Intentionally doesn't check if a version is set.
164 * This is already done in {@link _self_test()}
166 * @return string $this->version
168 function get_version() {
169 // Intentionally doesn't check if a version is set. This is already done in _self_test()
170 return $this->version
;
174 * Returns true or false, depending on whether this block has any content to display
178 function is_empty() {
179 $this->get_content();
180 return(empty($this->content
->text
) && empty($this->content
->footer
));
184 * First sets the current value of $this->content to NULL
185 * then calls the block's {@link get_content()} function
186 * to set its value back.
190 function refresh_content() {
191 // Nothing special here, depends on content()
192 $this->content
= NULL;
193 return $this->get_content();
199 function _print_block() {
200 // is_empty() includes a call to get_content()
201 if ($this->is_empty()) {
202 if (empty($this->edit_controls
)) {
203 // No content, no edit controls, so just shut up
206 // No content but editing, so show something at least
207 $this->_print_shadow();
210 if ($this->hide_header() && empty($this->edit_controls
)) {
211 // Header wants to hide, no edit controls to show, so no header it is
212 print_side_block(NULL, $this->content
->text
, NULL, NULL, $this->content
->footer
, $this->html_attributes());
214 // The full treatment, please
215 print_side_block($this->_title_html(), $this->content
->text
, NULL, NULL, $this->content
->footer
, $this->html_attributes());
221 * Block contents are missing. Simply display an empty block so that
222 * edit controls are accessbile to the user and they are aware that this
223 * block is in place, even if empty.
225 function _print_shadow() {
226 print_side_block($this->_title_html(), ' ', NULL, NULL, '', array('class' => 'hidden'));
230 function _title_html() {
233 $title = '<div class="title">';
235 if (!empty($CFG->allowuserblockhiding
)) {
236 $title .= '<div class="hide-show"><a href="#" onclick="elementToggleHide(this, true, function(el) {return findParentNode(el, \'DIV\', \'sideblock\'); } ); return false;"><img src="'.$CFG->pixpath
.'/spacer.gif" alt="" class="hide-show-image" /></a></div>';
239 $title .= $this->title
;
241 if ($this->edit_controls
!== NULL) {
242 $title .= $this->edit_controls
;
251 * Sets class $edit_controls var with correct block manipulation links.
255 * @param stdObject $options ?
256 * @todo complete documenting this function. Define $options.
258 function _add_edit_controls($options) {
261 if (!isset($this->str
)) {
262 $this->str
->delete
= get_string('delete');
263 $this->str
->moveup
= get_string('moveup');
264 $this->str
->movedown
= get_string('movedown');
265 $this->str
->moveright
= get_string('moveright');
266 $this->str
->moveleft
= get_string('moveleft');
267 $this->str
->hide
= get_string('hide');
268 $this->str
->show
= get_string('show');
269 $this->str
->configure
= get_string('configuration');
272 $movebuttons = '<div class="commands">';
274 if ($this->instance
->visible
) {
275 $icon = '/t/hide.gif';
276 $title = $this->str
->hide
;
278 $icon = '/t/show.gif';
279 $title = $this->str
->show
;
282 $page = page_create_object($this->instance
->pagetype
, $this->instance
->pageid
);
283 $script = $page->url_get_full(array('instanceid' => $this->instance
->id
, 'sesskey' => $USER->sesskey
));
285 $movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&blockaction=toggle">' .
286 '<img src="'. $CFG->pixpath
.$icon .'" alt="'.$title.'" /></a>';
288 if ($options & BLOCK_CONFIGURE
) {
289 $movebuttons .= '<a class="icon edit" title="'. $this->str
->configure
.'" href="'.$script.'&blockaction=config">' .
290 '<img src="'. $CFG->pixpath
.'/t/edit.gif" alt="'. $this->str
->configure
.'" /></a>';
293 $movebuttons .= '<a class="icon delete" title="'. $this->str
->delete
.'" href="'.$script.'&blockaction=delete">' .
294 '<img src="'. $CFG->pixpath
.'/t/delete.gif" alt="'. $this->str
->delete
.'" /></a>';
296 if ($options & BLOCK_MOVE_LEFT
) {
297 $movebuttons .= '<a class="icon left" title="'. $this->str
->moveleft
.'" href="'.$script.'&blockaction=moveleft">' .
298 '<img src="'. $CFG->pixpath
.'/t/left.gif" alt="'. $this->str
->moveleft
.'" /></a>';
300 if ($options & BLOCK_MOVE_UP
) {
301 $movebuttons .= '<a class="icon up" title="'. $this->str
->moveup
.'" href="'.$script.'&blockaction=moveup">' .
302 '<img src="'. $CFG->pixpath
.'/t/up.gif" alt="'. $this->str
->moveup
.'" /></a>';
304 if ($options & BLOCK_MOVE_DOWN
) {
305 $movebuttons .= '<a class="icon down" title="'. $this->str
->movedown
.'" href="'.$script.'&blockaction=movedown">' .
306 '<img src="'. $CFG->pixpath
.'/t/down.gif" alt="'. $this->str
->movedown
.'" /></a>';
308 if ($options & BLOCK_MOVE_RIGHT
) {
309 $movebuttons .= '<a class="icon right" title="'. $this->str
->moveright
.'" href="'.$script.'&blockaction=moveright">' .
310 '<img src="'. $CFG->pixpath
.'/t/right.gif" alt="'. $this->str
->moveright
.'" /></a>';
313 $movebuttons .= '</div>';
314 $this->edit_controls
= $movebuttons;
318 * Tests if this block has been implemented correctly.
319 * Also, $errors isn't used right now
324 function _self_test() {
325 // Tests if this block has been implemented correctly.
326 // Also, $errors isn't used right now
330 if ($this->get_title() === NULL) {
331 $errors[] = 'title_not_set';
334 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST
, BLOCK_TYPE_TEXT
, BLOCK_TYPE_NUKE
))) {
335 $errors[] = 'invalid_content_type';
338 if ($this->get_content() === NULL) {
339 $errors[] = 'content_not_set';
342 if ($this->get_version() === NULL) {
343 $errors[] = 'version_not_set';
347 $formats = $this->applicable_formats();
348 if (empty($formats) ||
array_sum($formats) === 0) {
349 $errors[] = 'no_formats';
353 $width = $this->preferred_width();
354 if (!is_int($width) ||
$width <= 0) {
355 $errors[] = 'invalid_width';
362 * Subclasses should override this and return true if the
363 * subclass block has a config_global.html file.
367 function has_config() {
372 * Default behavior: print the config_global.html file
373 * You don't need to override this if you're satisfied with the above
378 function config_print() {
379 // Default behavior: print the config_global.html file
380 // You don't need to override this if you're satisfied with the above
381 if (!$this->has_config()) {
385 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
386 include($CFG->dirroot
.'/blocks/'. $this->name() .'/config_global.html');
387 print_simple_box_end();
392 * Default behavior: save all variables as $CFG properties
393 * You don't need to override this if you 're satisfied with the above
398 function config_save($data) {
399 // Default behavior: save all variables as $CFG properties
400 // You don't need to override this if you 're satisfied with the above
401 foreach ($data as $name => $value) {
402 set_config($name, $value);
408 * Default case: the block can be used in all course types
410 * @todo finish documenting this function
412 function applicable_formats() {
413 // Default case: the block can be used in courses and site index, but not in activities
414 return array('all' => true, 'mod' => false);
419 * Default case: the block wants to be 180 pixels wide
422 function preferred_width() {
423 // Default case: the block wants to be 180 pixels wide
428 * Default return is false - header will be shown
431 function hide_header() {
432 //Default, false--> the header is shown
437 * Default case: just an id for the block, with our name in it
439 * @todo finish documenting this function
441 function html_attributes() {
442 // Default case: an id with the instance and a class with our name in it
443 return array('id' => 'inst'.$this->instance
->id
, 'class' => 'block_'. $this->name());
447 * Given an instance set the class var $instance to it and
448 * load class var $config
449 * @param block $instance
450 * @todo add additional documentation to further explain the format of instance and config
452 function _load_instance($instance) {
453 if (!empty($instance->configdata
)) {
454 $this->config
= unserialize(base64_decode($instance->configdata
));
456 // [pj] This line below is supposed to be an optimization (we don't need configdata anymore)
457 // but what it does is break in PHP5 because the same instance object will be passed to
458 // this function twice in each page view, and the second time it won't have any configdata
459 // so it won't work correctly. Thus it's commented out.
460 // unset($instance->configdata);
461 $this->instance
= $instance;
462 $this->specialization();
466 * This function is called on your subclass right after an instance is loaded
467 * Use this function to act on instance data just after it's loaded and before anything else is done
468 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
470 function specialization() {
471 // Just to make sure that this method exists.
475 * Is each block of this type going to have instance-specific configuration?
476 * Normally, this setting is controlled by {@link instance_allow_multiple}: if multiple
477 * instances are allowed, then each will surely need its own configuration. However, in some
478 * cases it may be necessary to provide instance configuration to blocks that do not want to
479 * allow multiple instances. In that case, make this function return true.
480 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple} returns false.
482 * @todo finish documenting this function by explaining per-instance configuration further
484 function instance_allow_config() {
489 * Are you going to allow multiple instances of each block?
490 * If yes, then it is assumed that the block WILL USE per-instance configuration
492 * @todo finish documenting this function by explaining per-instance configuration further
494 function instance_allow_multiple() {
495 // Are you going to allow multiple instances of each block?
496 // If yes, then it is assumed that the block WILL USE per-instance configuration
501 * Default behavior: print the config_instance.html file
502 * You don't need to override this if you're satisfied with the above
506 * @todo finish documenting this function
508 function instance_config_print() {
509 // Default behavior: print the config_instance.html file
510 // You don't need to override this if you're satisfied with the above
511 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
516 if (is_file($CFG->dirroot
.'/blocks/'. $this->name() .'/config_instance.html')) {
517 print_simple_box_start('center', '', '', 5, 'blockconfiginstance');
518 include($CFG->dirroot
.'/blocks/'. $this->name() .'/config_instance.html');
519 print_simple_box_end();
521 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
528 * Serialize and store config data
530 * @todo finish documenting this function
532 function instance_config_save($data) {
533 $data = stripslashes_recursive($data);
534 $this->config
= $data;
535 return set_field('block_instance', 'configdata', base64_encode(serialize($data)), 'id', $this->instance
->id
);
539 * Replace the instance's configuration data with those currently in $this->config;
541 * @todo finish documenting this function
543 function instance_config_commit() {
544 return set_field('block_instance', 'configdata', base64_encode(serialize($this->config
)), 'id', $this->instance
->id
);
548 * Do any additional initialization you may need at the time a new block instance is created
550 * @todo finish documenting this function
552 function instance_create() {
557 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
559 * @todo finish documenting this function
561 function instance_delete() {
568 * Specialized class for displaying a block with a list of icons/text labels
570 * @author Jon Papaioannou
574 class block_list
extends block_base
{
575 var $content_type = BLOCK_TYPE_LIST
;
577 function is_empty() {
578 $this->get_content();
579 return (empty($this->content
->items
) && empty($this->content
->footer
));
582 function _print_block() {
583 // is_empty() includes a call to get_content()
584 if ($this->is_empty()) {
585 if (empty($this->edit_controls
)) {
586 // No content, no edit controls, so just shut up
589 // No content but editing, so show something at least
590 $this->_print_shadow();
593 if ($this->hide_header() && empty($this->edit_controls
)) {
594 // Header wants to hide, no edit controls to show, so no header it is
595 print_side_block(NULL, '', $this->content
->items
, $this->content
->icons
, $this->content
->footer
, $this->html_attributes());
597 // The full treatment, please
598 print_side_block($this->_title_html(), '', $this->content
->items
, $this->content
->icons
, $this->content
->footer
, $this->html_attributes());
606 * Class for supporting a phpnuke style block as a moodle block
608 * @author Jon Papaioannou
611 class block_nuke
extends block_base
{
613 var $content_type = BLOCK_TYPE_NUKE
;
615 function get_content() {
617 if ($this->content
!== NULL) {
618 return $this->content
;
622 $this->content
= &New stdClass
;
624 // This whole thing begs to be written for PHP >= 4.3.0 using glob();
625 $dir = $CFG->dirroot
.'/blocks/'. $this->name() .'/nuke/';
626 if ($dh = @opendir
($dir)) {
627 while (($file = readdir($dh)) !== false) {
629 if (ereg('^block\-(.*)\.php$', $file, $regs)) {
630 // Found it! Let's prepare the environment...
633 if (isset($GLOBALS['admin'])) {
634 $oldvals['admin'] = $GLOBALS['admin'];
637 $GLOBALS['admin'] = isteacher($this->course
->id
);
638 @include
($dir.$file);
640 foreach($oldvals as $key => $val) {
641 $GLOBALS[$key] = $val;
644 // We should have $content set now
645 if (!isset($content)) {
648 return $this->content
->text
= $content;
653 // If we reached here, we couldn't find the nuke block for some reason
654 return $this->content
->text
= get_string('blockmissingnuke');