MDL-11515:
[moodle-linuxchix.git] / blocks / moodleblock.class.php
blobe8598cec1adb792dbcc2bfbab501bf307bde7d03
1 <?php // $Id$
3 /**
4 * This file contains the parent class for moodle blocks, block_base.
6 * @author Jon Papaioannou
7 * @version $Id$
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package blocks
12 /// Constants
14 /**
15 * 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.
17 define('BLOCK_TYPE_LIST', 1);
19 /**
20 * 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.
22 define('BLOCK_TYPE_TEXT', 2);
24 /**
25 * Class for describing a moodle block, all Moodle blocks derive from this class
27 * @author Jon Papaioannou
28 * @package blocks
30 class block_base {
32 /**
33 * Internal var for storing/caching translated strings
34 * @var string $str
36 var $str;
38 /**
39 * The title of the block to be displayed in the block title area.
40 * @var string $title
42 var $title = NULL;
44 /**
45 * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT
46 * @var int $content_type
48 var $content_type = BLOCK_TYPE_TEXT;
50 /**
51 * An object to contain the information to be displayed in the block.
52 * @var stdObject $content
54 var $content = NULL;
56 /**
57 * A string generated by {@link _add_edit_controls()} to display block manipulation links when the user is in editing mode.
58 * @var string $edit_controls
60 var $edit_controls = NULL;
62 /**
63 * The current version that the block type defines.
64 * @var string $version
66 var $version = NULL;
68 /**
69 * The initialized instance of this block object.
70 * @var block $instance
72 var $instance = NULL;
74 /**
75 * An object containing the instance configuration information for the current instance of this block.
76 * @var stdObject $config
78 var $config = NULL;
80 /**
81 * How often the cronjob should run, 0 if not at all.
82 * @var int $cron
85 var $cron = NULL;
88 /// Class Functions
90 /**
91 * The class constructor
94 function block_base() {
95 $this->init();
98 /**
99 * Fake constructor to keep PHP5 happy
102 function __construct() {
103 $this->block_base();
106 /**
107 * Function that can be overridden to do extra setup after
108 * the database install. (Called once per block, not per instance!)
110 function after_install() {
114 * Function that can be overridden to do extra cleanup before
115 * the database tables are deleted. (Called once per block, not per instance!)
117 function before_delete() {
121 * Function that can be overridden to do extra setup after a block instance has been
122 * restored from backup. For example, it may need to alter any dates that the block
123 * stores, if the $restore->course_startdateoffset is set.
125 function after_restore($restore) {
129 * Returns the block name, as present in the class name,
130 * the database, the block directory, etc etc.
132 * @return string
134 function name() {
135 // Returns the block name, as present in the class name,
136 // the database, the block directory, etc etc.
137 static $myname;
138 if ($myname === NULL) {
139 $myname = strtolower(get_class($this));
140 $myname = substr($myname, strpos($myname, '_') + 1);
142 return $myname;
146 * Parent class version of this function simply returns NULL
147 * This should be implemented by the derived class to return
148 * the content object.
150 * @return stdObject
152 function get_content() {
153 // This should be implemented by the derived class.
154 return NULL;
158 * Returns the class $title var value.
160 * Intentionally doesn't check if a title is set.
161 * This is already done in {@link _self_test()}
163 * @return string $this->title
165 function get_title() {
166 // Intentionally doesn't check if a title is set. This is already done in _self_test()
167 return $this->title;
171 * Returns the class $content_type var value.
173 * Intentionally doesn't check if content_type is set.
174 * This is already done in {@link _self_test()}
176 * @return string $this->content_type
178 function get_content_type() {
179 // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
180 return $this->content_type;
184 * Returns the class $version var value.
186 * Intentionally doesn't check if a version is set.
187 * This is already done in {@link _self_test()}
189 * @return string $this->version
191 function get_version() {
192 // Intentionally doesn't check if a version is set. This is already done in _self_test()
193 return $this->version;
197 * Returns true or false, depending on whether this block has any content to display
198 * and whether the user has permission to view the block
200 * @return boolean
202 function is_empty() {
204 $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
206 if ( !has_capability('moodle/block:view', $context) ) {
207 return true;
210 $this->get_content();
211 return(empty($this->content->text) && empty($this->content->footer));
215 * First sets the current value of $this->content to NULL
216 * then calls the block's {@link get_content()} function
217 * to set its value back.
219 * @return stdObject
221 function refresh_content() {
222 // Nothing special here, depends on content()
223 $this->content = NULL;
224 return $this->get_content();
228 * Display the block!
230 function _print_block() {
231 global $COURSE;
233 // is_empty() includes a call to get_content()
234 if ($this->is_empty() && empty($COURSE->javascriptportal)) {
235 if (empty($this->edit_controls)) {
236 // No content, no edit controls, so just shut up
237 return;
238 } else {
239 // No content but editing, so show something at least
240 $this->_print_shadow();
242 } else {
243 if ($this->hide_header() && empty($this->edit_controls)) {
244 // Header wants to hide, no edit controls to show, so no header it is
245 print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
246 } else {
247 // The full treatment, please. Include the title text.
248 print_side_block($this->_title_html(), $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes(), $this->title);
254 * Block contents are missing. Simply display an empty block so that
255 * edit controls are accessbile to the user and they are aware that this
256 * block is in place, even if empty.
258 function _print_shadow() {
259 print_side_block($this->_title_html(), '&nbsp;', NULL, NULL, '', array('class' => 'hidden'), $this->title);
263 function _title_html() {
264 global $CFG;
266 //Accessibility: validation, can't have <div> inside <h2>, use <span>.
267 $title = '<div class="title">';
269 if (!empty($CFG->allowuserblockhiding)) {
270 //Accessibility: added static 'alt' text for the +- icon.
271 //TODO (nfreear): language string 'hide OR show block'
272 $title .= '<div class="hide-show">'.
273 '<a title="'.get_string('showhideblock','access').
274 '" href="#" onclick="elementToggleHide(this, true, function(el) {'.
275 'return findParentNode(el, \'DIV\', \'sideblock\'); '.
276 '}, \''.$CFG->pixpath.'\' ); return false;">'.
277 '<img src="'.$CFG->pixpath.'/spacer.gif" '.
278 'id = "togglehide_inst'.$this->instance->id.'" '.
279 'alt="'.get_string('showhideblock','access').'" class="hide-show-image" /></a></div>';
282 //Accesssibility: added H2 (was in, weblib.php: print_side_block)
283 $title .= '<h2>'.$this->title.'</h2>';
285 if ($this->edit_controls !== NULL) {
286 $title .= $this->edit_controls;
289 $title .= '</div>';
290 return $title;
294 * Sets class $edit_controls var with correct block manipulation links.
296 * @uses $CFG
297 * @uses $USER
298 * @param stdObject $options ?
299 * @todo complete documenting this function. Define $options.
301 function _add_edit_controls($options) {
302 global $CFG, $USER, $PAGE;
304 // this is the context relevant to this particular block instance
305 $blockcontext = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
307 // context for site or course, i.e. participant list etc
308 // check to see if user can edit site or course blocks.
309 // blocks can appear on other pages such as mod and blog pages...
311 switch ($this->instance->pagetype) {
312 case 'course-view':
313 if (!has_capability('moodle/site:manageblocks', $blockcontext)) {
314 return null;
316 break;
317 default:
319 break;
323 if (!isset($this->str)) {
324 $this->str->delete = get_string('delete');
325 $this->str->moveup = get_string('moveup');
326 $this->str->movedown = get_string('movedown');
327 $this->str->moveright = get_string('moveright');
328 $this->str->moveleft = get_string('moveleft');
329 $this->str->hide = get_string('hide');
330 $this->str->show = get_string('show');
331 $this->str->configure = get_string('configuration');
332 $this->str->assignroles = get_string('assignroles', 'role');
335 // RTL support - exchange right and left arrows
336 if (right_to_left()) {
337 $rightarrow = 'left.gif';
338 $leftarrow = 'right.gif';
339 } else {
340 $rightarrow = 'right.gif';
341 $leftarrow = 'left.gif';
344 $movebuttons = '<div class="commands">';
346 if ($this->instance->visible) {
347 $icon = '/t/hide.gif';
348 $title = $this->str->hide;
349 } else {
350 $icon = '/t/show.gif';
351 $title = $this->str->show;
354 if (empty($this->instance->pageid)) {
355 $this->instance->pageid = 0;
357 if (!empty($PAGE->type) and ($this->instance->pagetype == $PAGE->type) and $this->instance->pageid == $PAGE->id) {
358 $page = $PAGE;
359 } else {
360 $page = page_create_object($this->instance->pagetype, $this->instance->pageid);
362 $script = $page->url_get_full(array('instanceid' => $this->instance->id, 'sesskey' => $USER->sesskey));
364 if (empty($this->instance->pinned)) {
365 $movebuttons .= '<a class="icon roles" title="'. $this->str->assignroles .'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$blockcontext->id.'">' .
366 '<img src="'.$CFG->pixpath.'/i/roles.gif" alt="'.$this->str->assignroles.'" /></a>';
369 if ($this->user_can_edit()) {
370 $movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&amp;blockaction=toggle">' .
371 '<img src="'. $CFG->pixpath.$icon .'" alt="'.$title.'" /></a>';
374 if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
375 $movebuttons .= '<a class="icon edit" title="'. $this->str->configure .'" href="'.$script.'&amp;blockaction=config">' .
376 '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. $this->str->configure .'" /></a>';
379 if ($this->user_can_addto($page)) {
380 $movebuttons .= '<a class="icon delete" title="'. $this->str->delete .'" href="'.$script.'&amp;blockaction=delete">' .
381 '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. $this->str->delete .'" /></a>';
384 if ($options & BLOCK_MOVE_LEFT) {
385 $movebuttons .= '<a class="icon left" title="'. $this->str->moveleft .'" href="'.$script.'&amp;blockaction=moveleft">' .
386 '<img src="'. $CFG->pixpath .'/t/'.$leftarrow.'" alt="'. $this->str->moveleft .'" /></a>';
388 if ($options & BLOCK_MOVE_UP) {
389 $movebuttons .= '<a class="icon up" title="'. $this->str->moveup .'" href="'.$script.'&amp;blockaction=moveup">' .
390 '<img src="'. $CFG->pixpath .'/t/up.gif" alt="'. $this->str->moveup .'" /></a>';
392 if ($options & BLOCK_MOVE_DOWN) {
393 $movebuttons .= '<a class="icon down" title="'. $this->str->movedown .'" href="'.$script.'&amp;blockaction=movedown">' .
394 '<img src="'. $CFG->pixpath .'/t/down.gif" alt="'. $this->str->movedown .'" /></a>';
396 if ($options & BLOCK_MOVE_RIGHT) {
397 $movebuttons .= '<a class="icon right" title="'. $this->str->moveright .'" href="'.$script.'&amp;blockaction=moveright">' .
398 '<img src="'. $CFG->pixpath .'/t/'.$rightarrow.'" alt="'. $this->str->moveright .'" /></a>';
401 $movebuttons .= '</div>';
402 $this->edit_controls = $movebuttons;
406 * Tests if this block has been implemented correctly.
407 * Also, $errors isn't used right now
409 * @return boolean
412 function _self_test() {
413 // Tests if this block has been implemented correctly.
414 // Also, $errors isn't used right now
415 $errors = array();
417 $correct = true;
418 if ($this->get_title() === NULL) {
419 $errors[] = 'title_not_set';
420 $correct = false;
422 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT))) {
423 $errors[] = 'invalid_content_type';
424 $correct = false;
426 //following selftest was not working when roles&capabilities were used from block
427 /* if ($this->get_content() === NULL) {
428 $errors[] = 'content_not_set';
429 $correct = false;
431 if ($this->get_version() === NULL) {
432 $errors[] = 'version_not_set';
433 $correct = false;
436 $formats = $this->applicable_formats();
437 if (empty($formats) || array_sum($formats) === 0) {
438 $errors[] = 'no_formats';
439 $correct = false;
442 $width = $this->preferred_width();
443 if (!is_int($width) || $width <= 0) {
444 $errors[] = 'invalid_width';
445 $correct = false;
447 return $correct;
451 * Subclasses should override this and return true if the
452 * subclass block has a config_global.html file.
454 * @return boolean
456 function has_config() {
457 return false;
461 * Default behavior: print the config_global.html file
462 * You don't need to override this if you're satisfied with the above
464 * @uses $CFG
465 * @return boolean
467 function config_print() {
468 // Default behavior: print the config_global.html file
469 // You don't need to override this if you're satisfied with the above
470 if (!$this->has_config()) {
471 return false;
473 global $CFG;
474 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
475 include($CFG->dirroot.'/blocks/'. $this->name() .'/config_global.html');
476 print_simple_box_end();
477 return true;
481 * Default behavior: save all variables as $CFG properties
482 * You don't need to override this if you 're satisfied with the above
484 * @param array $data
485 * @return boolean
487 function config_save($data) {
488 foreach ($data as $name => $value) {
489 set_config($name, $value);
491 return true;
495 * Default case: the block can be used in all course types
496 * @return array
497 * @todo finish documenting this function
499 function applicable_formats() {
500 // Default case: the block can be used in courses and site index, but not in activities
501 return array('all' => true, 'mod' => false, 'tag' => false);
506 * Default case: the block wants to be 180 pixels wide
507 * @return int
509 function preferred_width() {
510 return 180;
514 * Default return is false - header will be shown
515 * @return boolean
517 function hide_header() {
518 return false;
522 * Default case: an id with the instance and a class with our name in it
523 * @return array
524 * @todo finish documenting this function
526 function html_attributes() {
527 return array('id' => 'inst'.$this->instance->id, 'class' => 'block_'. $this->name());
531 * Given an instance set the class var $instance to it and
532 * load class var $config
533 * @param block $instance
534 * @todo add additional documentation to further explain the format of instance and config
536 function _load_instance($instance) {
537 if (!empty($instance->configdata)) {
538 $this->config = unserialize(base64_decode($instance->configdata));
540 // [pj] This line below is supposed to be an optimization (we don't need configdata anymore)
541 // but what it does is break in PHP5 because the same instance object will be passed to
542 // this function twice in each page view, and the second time it won't have any configdata
543 // so it won't work correctly. Thus it's commented out.
544 // unset($instance->configdata);
545 $this->instance = $instance;
546 $this->specialization();
550 * This function is called on your subclass right after an instance is loaded
551 * Use this function to act on instance data just after it's loaded and before anything else is done
552 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
554 function specialization() {
555 // Just to make sure that this method exists.
559 * Is each block of this type going to have instance-specific configuration?
560 * Normally, this setting is controlled by {@link instance_allow_multiple}: if multiple
561 * instances are allowed, then each will surely need its own configuration. However, in some
562 * cases it may be necessary to provide instance configuration to blocks that do not want to
563 * allow multiple instances. In that case, make this function return true.
564 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple} returns false.
565 * @return boolean
566 * @todo finish documenting this function by explaining per-instance configuration further
568 function instance_allow_config() {
569 return false;
573 * Are you going to allow multiple instances of each block?
574 * If yes, then it is assumed that the block WILL USE per-instance configuration
575 * @return boolean
576 * @todo finish documenting this function by explaining per-instance configuration further
578 function instance_allow_multiple() {
579 // Are you going to allow multiple instances of each block?
580 // If yes, then it is assumed that the block WILL USE per-instance configuration
581 return false;
585 * Default behavior: print the config_instance.html file
586 * You don't need to override this if you're satisfied with the above
588 * @uses $CFG
589 * @return boolean
590 * @todo finish documenting this function
592 function instance_config_print() {
593 // Default behavior: print the config_instance.html file
594 // You don't need to override this if you're satisfied with the above
595 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
596 return false;
598 global $CFG;
600 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
601 print_simple_box_start('center', '', '', 5, 'blockconfiginstance');
602 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
603 print_simple_box_end();
604 } else {
605 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
608 return true;
612 * Serialize and store config data
613 * @return boolean
614 * @todo finish documenting this function
616 function instance_config_save($data,$pinned=false) {
617 $data = stripslashes_recursive($data);
618 $this->config = $data;
619 $table = 'block_instance';
620 if (!empty($pinned)) {
621 $table = 'block_pinned';
623 return set_field($table, 'configdata', base64_encode(serialize($data)), 'id', $this->instance->id);
627 * Replace the instance's configuration data with those currently in $this->config;
628 * @return boolean
629 * @todo finish documenting this function
631 function instance_config_commit($pinned=false) {
632 $table = 'block_instance';
633 if (!empty($pinned)) {
634 $table = 'block_pinned';
636 return set_field($table, 'configdata', base64_encode(serialize($this->config)), 'id', $this->instance->id);
640 * Do any additional initialization you may need at the time a new block instance is created
641 * @return boolean
642 * @todo finish documenting this function
644 function instance_create() {
645 return true;
649 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
650 * @return boolean
651 * @todo finish documenting this function
653 function instance_delete() {
654 return true;
658 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
659 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
660 * but if the framework does allow it, the block can still decide to refuse.
661 * @return boolean
662 * @todo finish documenting this function
664 function user_can_edit() {
665 return true;
669 * Allows the block class to have a say in the user's ability to create new instances of this block.
670 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
671 * but if the framework does allow it, the block can still decide to refuse.
672 * This function has access to the complete page object, the creation related to which is being determined.
673 * @return boolean
674 * @todo finish documenting this function
676 function user_can_addto(&$page) {
677 return true;
683 * Specialized class for displaying a block with a list of icons/text labels
685 * @author Jon Papaioannou
686 * @package blocks
689 class block_list extends block_base {
690 var $content_type = BLOCK_TYPE_LIST;
692 function is_empty() {
694 $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
696 if ( !has_capability('moodle/block:view', $context) ) {
697 return true;
700 $this->get_content();
701 return (empty($this->content->items) && empty($this->content->footer));
704 function _print_block() {
705 global $COURSE;
707 // is_empty() includes a call to get_content()
708 if ($this->is_empty() && empty($COURSE->javascriptportal)) {
709 if (empty($this->edit_controls)) {
710 // No content, no edit controls, so just shut up
711 return;
712 } else {
713 // No content but editing, so show something at least
714 $this->_print_shadow();
716 } else {
717 if ($this->hide_header() && empty($this->edit_controls)) {
718 // Header wants to hide, no edit controls to show, so no header it is
719 print_side_block(NULL, '', $this->content->items, $this->content->icons,
720 $this->content->footer, $this->html_attributes());
721 } else {
722 // The full treatment, please. Include the title text.
723 print_side_block($this->_title_html(), '', $this->content->items, $this->content->icons,
724 $this->content->footer, $this->html_attributes(), $this->title);