OKAY! THIS IS IT! MOODLE 1.6!
[moodle.git] / blocks / moodleblock.class.php
blob72149026f453efa1b5bea8bff5221ddb95195931
1 <?php // $Id$
3 /**
4 * This file contains the parent class for moodle blocks, block_base,
5 * as well as the block_nuke subclass.
7 * @author Jon Papaioannou
8 * @version $Id$
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package blocks
13 /// Constants
15 /**
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);
20 /**
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);
25 /**
26 * Block type of nuke. Compitibility with post nuke blocks. Basically treated as BLOCK_TYPE_TEXT.
28 define('BLOCK_TYPE_NUKE', 3);
30 /**
31 * Class for describing a moodle block, all Moodle blocks derive from this class
33 * @author Jon Papaioannou
34 * @package blocks
36 class block_base {
38 /**
39 * Internal var for storing/caching translated strings
40 * @var string $str
42 var $str;
44 /**
45 * The title of the block to be displayed in the block title area.
46 * @var string $title
48 var $title = NULL;
50 /**
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;
56 /**
57 * An object to contain the information to be displayed in the block.
58 * @var stdObject $content
60 var $content = NULL;
62 /**
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;
68 /**
69 * The current version that the block type defines.
70 * @var string $version
72 var $version = NULL;
74 /**
75 * The initialized instance of this block object.
76 * @var block $instance
78 var $instance = NULL;
80 /**
81 * An object containing the instance configuration information for the current instance of this block.
82 * @var stdObject $config
84 var $config = NULL;
86 /**
87 * How often the cronjob should run, 0 if not at all.
88 * @var int $cron
91 var $cron = NULL;
94 /// Class Functions
96 /**
97 * The class constructor
100 function block_base() {
101 $this->init();
105 * Fake constructor to keep PHP5 happy
108 function __construct() {
109 $this->block_base();
113 * Returns the block name, as present in the class name,
114 * the database, the block directory, etc etc.
116 * @return string
118 function name() {
119 // Returns the block name, as present in the class name,
120 // the database, the block directory, etc etc.
121 static $myname;
122 if ($myname === NULL) {
123 $myname = strtolower(get_class($this));
124 $myname = substr($myname, strpos($myname, '_') + 1);
126 return $myname;
130 * Parent class version of this function simply returns NULL
131 * This should be implemented by the derived class to return
132 * the content object.
134 * @return stdObject
136 function get_content() {
137 // This should be implemented by the derived class.
138 return NULL;
142 * Returns the class $title var value.
144 * Intentionally doesn't check if a title is set.
145 * This is already done in {@link _self_test()}
147 * @return string $this->title
149 function get_title() {
150 // Intentionally doesn't check if a title is set. This is already done in _self_test()
151 return $this->title;
155 * Returns the class $content_type var value.
157 * Intentionally doesn't check if content_type is set.
158 * This is already done in {@link _self_test()}
160 * @return string $this->content_type
162 function get_content_type() {
163 // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
164 return $this->content_type;
168 * Returns the class $version var value.
170 * Intentionally doesn't check if a version is set.
171 * This is already done in {@link _self_test()}
173 * @return string $this->version
175 function get_version() {
176 // Intentionally doesn't check if a version is set. This is already done in _self_test()
177 return $this->version;
181 * Returns true or false, depending on whether this block has any content to display
183 * @return boolean
185 function is_empty() {
186 $this->get_content();
187 return(empty($this->content->text) && empty($this->content->footer));
191 * First sets the current value of $this->content to NULL
192 * then calls the block's {@link get_content()} function
193 * to set its value back.
195 * @return stdObject
197 function refresh_content() {
198 // Nothing special here, depends on content()
199 $this->content = NULL;
200 return $this->get_content();
204 * Display the block!
206 function _print_block() {
207 // is_empty() includes a call to get_content()
208 if ($this->is_empty()) {
209 if (empty($this->edit_controls)) {
210 // No content, no edit controls, so just shut up
211 return;
212 } else {
213 // No content but editing, so show something at least
214 $this->_print_shadow();
216 } else {
217 if ($this->hide_header() && empty($this->edit_controls)) {
218 // Header wants to hide, no edit controls to show, so no header it is
219 print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
220 } else {
221 // The full treatment, please
222 print_side_block($this->_title_html(), $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
228 * Block contents are missing. Simply display an empty block so that
229 * edit controls are accessbile to the user and they are aware that this
230 * block is in place, even if empty.
232 function _print_shadow() {
233 print_side_block($this->_title_html(), '&nbsp;', NULL, NULL, '', array('class' => 'hidden'));
237 function _title_html() {
238 global $CFG;
240 //Accessibility: validation, can't have <div> inside <h2>, use <span>.
241 $title = '<div class="title">';
243 if (!empty($CFG->allowuserblockhiding)) {
244 //Accessibility: added static 'alt' text for the +- icon.
245 //TODO (nfreear): language string 'hide OR show block'
246 $title .= '<div class="hide-show"><a title="'.get_string('showhideblock','access').'" href="#" onclick="elementToggleHide(this, true, function(el) {return findParentNode(el, \'DIV\', \'sideblock\'); } ); return false;"><img src="'.$CFG->pixpath.'/spacer.gif" alt="'.get_string('showhideblock','access').'" class="hide-show-image" /></a></div>';
249 //Accesssibility: added H2 (was in, weblib.php: print_side_block)
250 $title .= '<h2>'.$this->title.'</h2>';
252 if ($this->edit_controls !== NULL) {
253 $title .= $this->edit_controls;
256 $title .= '</div>';
258 return $title;
262 * Sets class $edit_controls var with correct block manipulation links.
264 * @uses $CFG
265 * @uses $USER
266 * @param stdObject $options ?
267 * @todo complete documenting this function. Define $options.
269 function _add_edit_controls($options) {
270 global $CFG, $USER;
272 if (!isset($this->str)) {
273 $this->str->delete = get_string('delete');
274 $this->str->moveup = get_string('moveup');
275 $this->str->movedown = get_string('movedown');
276 $this->str->moveright = get_string('moveright');
277 $this->str->moveleft = get_string('moveleft');
278 $this->str->hide = get_string('hide');
279 $this->str->show = get_string('show');
280 $this->str->configure = get_string('configuration');
283 $movebuttons = '<div class="commands">';
285 if ($this->instance->visible) {
286 $icon = '/t/hide.gif';
287 $title = $this->str->hide;
288 } else {
289 $icon = '/t/show.gif';
290 $title = $this->str->show;
293 if (empty($this->instance->pageid)) {
294 $this->instance->pageid = 0;
296 $page = page_create_object($this->instance->pagetype, $this->instance->pageid);
297 $script = $page->url_get_full(array('instanceid' => $this->instance->id, 'sesskey' => $USER->sesskey));
299 $movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&amp;blockaction=toggle">' .
300 '<img src="'. $CFG->pixpath.$icon .'" alt="'.$title.'" /></a>';
302 if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
303 $movebuttons .= '<a class="icon edit" title="'. $this->str->configure .'" href="'.$script.'&amp;blockaction=config">' .
304 '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. $this->str->configure .'" /></a>';
307 $movebuttons .= '<a class="icon delete" title="'. $this->str->delete .'" href="'.$script.'&amp;blockaction=delete">' .
308 '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. $this->str->delete .'" /></a>';
310 if ($options & BLOCK_MOVE_LEFT) {
311 $movebuttons .= '<a class="icon left" title="'. $this->str->moveleft .'" href="'.$script.'&amp;blockaction=moveleft">' .
312 '<img src="'. $CFG->pixpath .'/t/left.gif" alt="'. $this->str->moveleft .'" /></a>';
314 if ($options & BLOCK_MOVE_UP) {
315 $movebuttons .= '<a class="icon up" title="'. $this->str->moveup .'" href="'.$script.'&amp;blockaction=moveup">' .
316 '<img src="'. $CFG->pixpath .'/t/up.gif" alt="'. $this->str->moveup .'" /></a>';
318 if ($options & BLOCK_MOVE_DOWN) {
319 $movebuttons .= '<a class="icon down" title="'. $this->str->movedown .'" href="'.$script.'&amp;blockaction=movedown">' .
320 '<img src="'. $CFG->pixpath .'/t/down.gif" alt="'. $this->str->movedown .'" /></a>';
322 if ($options & BLOCK_MOVE_RIGHT) {
323 $movebuttons .= '<a class="icon right" title="'. $this->str->moveright .'" href="'.$script.'&amp;blockaction=moveright">' .
324 '<img src="'. $CFG->pixpath .'/t/right.gif" alt="'. $this->str->moveright .'" /></a>';
327 $movebuttons .= '</div>';
328 $this->edit_controls = $movebuttons;
332 * Tests if this block has been implemented correctly.
333 * Also, $errors isn't used right now
335 * @return boolean
338 function _self_test() {
339 // Tests if this block has been implemented correctly.
340 // Also, $errors isn't used right now
341 $errors = array();
343 $correct = true;
344 if ($this->get_title() === NULL) {
345 $errors[] = 'title_not_set';
346 $correct = false;
348 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE))) {
349 $errors[] = 'invalid_content_type';
350 $correct = false;
352 if ($this->get_content() === NULL) {
353 $errors[] = 'content_not_set';
354 $correct = false;
356 if ($this->get_version() === NULL) {
357 $errors[] = 'version_not_set';
358 $correct = false;
361 $formats = $this->applicable_formats();
362 if (empty($formats) || array_sum($formats) === 0) {
363 $errors[] = 'no_formats';
364 $correct = false;
367 $width = $this->preferred_width();
368 if (!is_int($width) || $width <= 0) {
369 $errors[] = 'invalid_width';
370 $correct = false;
372 return $correct;
376 * Subclasses should override this and return true if the
377 * subclass block has a config_global.html file.
379 * @return boolean
381 function has_config() {
382 return false;
386 * Default behavior: print the config_global.html file
387 * You don't need to override this if you're satisfied with the above
389 * @uses $CFG
390 * @return boolean
392 function config_print() {
393 // Default behavior: print the config_global.html file
394 // You don't need to override this if you're satisfied with the above
395 if (!$this->has_config()) {
396 return false;
398 global $CFG;
399 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
400 include($CFG->dirroot.'/blocks/'. $this->name() .'/config_global.html');
401 print_simple_box_end();
402 return true;
406 * Default behavior: save all variables as $CFG properties
407 * You don't need to override this if you 're satisfied with the above
409 * @param array $data
410 * @return boolean
412 function config_save($data) {
413 foreach ($data as $name => $value) {
414 set_config($name, $value);
416 return true;
420 * Default case: the block can be used in all course types
421 * @return array
422 * @todo finish documenting this function
424 function applicable_formats() {
425 // Default case: the block can be used in courses and site index, but not in activities
426 return array('all' => true, 'mod' => false);
431 * Default case: the block wants to be 180 pixels wide
432 * @return int
434 function preferred_width() {
435 return 180;
439 * Default return is false - header will be shown
440 * @return boolean
442 function hide_header() {
443 return false;
447 * Default case: an id with the instance and a class with our name in it
448 * @return array
449 * @todo finish documenting this function
451 function html_attributes() {
452 return array('id' => 'inst'.$this->instance->id, 'class' => 'block_'. $this->name());
456 * Given an instance set the class var $instance to it and
457 * load class var $config
458 * @param block $instance
459 * @todo add additional documentation to further explain the format of instance and config
461 function _load_instance($instance) {
462 if (!empty($instance->configdata)) {
463 $this->config = unserialize(base64_decode($instance->configdata));
465 // [pj] This line below is supposed to be an optimization (we don't need configdata anymore)
466 // but what it does is break in PHP5 because the same instance object will be passed to
467 // this function twice in each page view, and the second time it won't have any configdata
468 // so it won't work correctly. Thus it's commented out.
469 // unset($instance->configdata);
470 $this->instance = $instance;
471 $this->specialization();
475 * This function is called on your subclass right after an instance is loaded
476 * Use this function to act on instance data just after it's loaded and before anything else is done
477 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
479 function specialization() {
480 // Just to make sure that this method exists.
484 * Is each block of this type going to have instance-specific configuration?
485 * Normally, this setting is controlled by {@link instance_allow_multiple}: if multiple
486 * instances are allowed, then each will surely need its own configuration. However, in some
487 * cases it may be necessary to provide instance configuration to blocks that do not want to
488 * allow multiple instances. In that case, make this function return true.
489 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple} returns false.
490 * @return boolean
491 * @todo finish documenting this function by explaining per-instance configuration further
493 function instance_allow_config() {
494 return false;
498 * Are you going to allow multiple instances of each block?
499 * If yes, then it is assumed that the block WILL USE per-instance configuration
500 * @return boolean
501 * @todo finish documenting this function by explaining per-instance configuration further
503 function instance_allow_multiple() {
504 // Are you going to allow multiple instances of each block?
505 // If yes, then it is assumed that the block WILL USE per-instance configuration
506 return false;
510 * Default behavior: print the config_instance.html file
511 * You don't need to override this if you're satisfied with the above
513 * @uses $CFG
514 * @return boolean
515 * @todo finish documenting this function
517 function instance_config_print() {
518 // Default behavior: print the config_instance.html file
519 // You don't need to override this if you're satisfied with the above
520 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
521 return false;
523 global $CFG;
525 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
526 print_simple_box_start('center', '', '', 5, 'blockconfiginstance');
527 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
528 print_simple_box_end();
529 } else {
530 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
533 return true;
537 * Serialize and store config data
538 * @return boolean
539 * @todo finish documenting this function
541 function instance_config_save($data,$pinned=false) {
542 $data = stripslashes_recursive($data);
543 $this->config = $data;
544 $table = 'block_instance';
545 if (!empty($pinned)) {
546 $table = 'block_pinned';
548 return set_field($table, 'configdata', base64_encode(serialize($data)), 'id', $this->instance->id);
552 * Replace the instance's configuration data with those currently in $this->config;
553 * @return boolean
554 * @todo finish documenting this function
556 function instance_config_commit($pinned=false) {
557 $table = 'block_instance';
558 if (!empty($pinned)) {
559 $table = 'block_pinned';
561 return set_field($table, 'configdata', base64_encode(serialize($this->config)), 'id', $this->instance->id);
565 * Do any additional initialization you may need at the time a new block instance is created
566 * @return boolean
567 * @todo finish documenting this function
569 function instance_create() {
570 return true;
574 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
575 * @return boolean
576 * @todo finish documenting this function
578 function instance_delete() {
579 return true;
583 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
584 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
585 * but if the framework does allow it, the block can still decide to refuse.
586 * @return boolean
587 * @todo finish documenting this function
589 function user_can_edit() {
590 return true;
594 * Allows the block class to have a say in the user's ability to create new instances of this block.
595 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
596 * but if the framework does allow it, the block can still decide to refuse.
597 * This function has access to the complete page object, the creation related to which is being determined.
598 * @return boolean
599 * @todo finish documenting this function
601 function user_can_addto(&$page) {
602 return true;
608 * Specialized class for displaying a block with a list of icons/text labels
610 * @author Jon Papaioannou
611 * @package blocks
614 class block_list extends block_base {
615 var $content_type = BLOCK_TYPE_LIST;
617 function is_empty() {
618 $this->get_content();
619 return (empty($this->content->items) && empty($this->content->footer));
622 function _print_block() {
623 // is_empty() includes a call to get_content()
624 if ($this->is_empty()) {
625 if (empty($this->edit_controls)) {
626 // No content, no edit controls, so just shut up
627 return;
628 } else {
629 // No content but editing, so show something at least
630 $this->_print_shadow();
632 } else {
633 if ($this->hide_header() && empty($this->edit_controls)) {
634 // Header wants to hide, no edit controls to show, so no header it is
635 print_side_block(NULL, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
636 } else {
637 // The full treatment, please
638 print_side_block($this->_title_html(), '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
646 * Class for supporting a phpnuke style block as a moodle block
648 * @author Jon Papaioannou
649 * @package blocks
651 class block_nuke extends block_base {
653 var $content_type = BLOCK_TYPE_NUKE;
655 function get_content() {
657 if ($this->content !== NULL) {
658 return $this->content;
661 global $CFG;
662 $this->content = &New stdClass;
664 // This whole thing begs to be written for PHP >= 4.3.0 using glob();
665 $dir = $CFG->dirroot .'/blocks/'. $this->name() .'/nuke/';
666 if ($dh = @opendir($dir)) {
667 while (($file = readdir($dh)) !== false) {
668 $regs = array();
669 if (ereg('^block\-(.*)\.php$', $file, $regs)) {
670 // Found it! Let's prepare the environment...
672 $oldvals = array();
673 if (isset($GLOBALS['admin'])) {
674 $oldvals['admin'] = $GLOBALS['admin'];
677 $GLOBALS['admin'] = isteacher($this->course->id);
678 @include($dir.$file);
680 foreach($oldvals as $key => $val) {
681 $GLOBALS[$key] = $val;
684 // We should have $content set now
685 if (!isset($content)) {
686 return NULL;
688 return $this->content->text = $content;
693 // If we reached here, we couldn't find the nuke block for some reason
694 return $this->content->text = get_string('blockmissingnuke');