MDL-10689:
[moodle-linuxchix.git] / blocks / moodleblock.class.php
blob49d369cd8ac36079f69d1e99cc4c8d5c1c69943f
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 // place holder for roles button
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>';
368 if ($this->user_can_edit()) {
369 $movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&amp;blockaction=toggle">' .
370 '<img src="'. $CFG->pixpath.$icon .'" alt="'.$title.'" /></a>';
373 if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
374 $movebuttons .= '<a class="icon edit" title="'. $this->str->configure .'" href="'.$script.'&amp;blockaction=config">' .
375 '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. $this->str->configure .'" /></a>';
378 if ($this->user_can_addto($page)) {
379 $movebuttons .= '<a class="icon delete" title="'. $this->str->delete .'" href="'.$script.'&amp;blockaction=delete">' .
380 '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. $this->str->delete .'" /></a>';
383 if ($options & BLOCK_MOVE_LEFT) {
384 $movebuttons .= '<a class="icon left" title="'. $this->str->moveleft .'" href="'.$script.'&amp;blockaction=moveleft">' .
385 '<img src="'. $CFG->pixpath .'/t/'.$leftarrow.'" alt="'. $this->str->moveleft .'" /></a>';
387 if ($options & BLOCK_MOVE_UP) {
388 $movebuttons .= '<a class="icon up" title="'. $this->str->moveup .'" href="'.$script.'&amp;blockaction=moveup">' .
389 '<img src="'. $CFG->pixpath .'/t/up.gif" alt="'. $this->str->moveup .'" /></a>';
391 if ($options & BLOCK_MOVE_DOWN) {
392 $movebuttons .= '<a class="icon down" title="'. $this->str->movedown .'" href="'.$script.'&amp;blockaction=movedown">' .
393 '<img src="'. $CFG->pixpath .'/t/down.gif" alt="'. $this->str->movedown .'" /></a>';
395 if ($options & BLOCK_MOVE_RIGHT) {
396 $movebuttons .= '<a class="icon right" title="'. $this->str->moveright .'" href="'.$script.'&amp;blockaction=moveright">' .
397 '<img src="'. $CFG->pixpath .'/t/'.$rightarrow.'" alt="'. $this->str->moveright .'" /></a>';
400 $movebuttons .= '</div>';
401 $this->edit_controls = $movebuttons;
405 * Tests if this block has been implemented correctly.
406 * Also, $errors isn't used right now
408 * @return boolean
411 function _self_test() {
412 // Tests if this block has been implemented correctly.
413 // Also, $errors isn't used right now
414 $errors = array();
416 $correct = true;
417 if ($this->get_title() === NULL) {
418 $errors[] = 'title_not_set';
419 $correct = false;
421 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT))) {
422 $errors[] = 'invalid_content_type';
423 $correct = false;
425 //following selftest was not working when roles&capabilities were used from block
426 /* if ($this->get_content() === NULL) {
427 $errors[] = 'content_not_set';
428 $correct = false;
430 if ($this->get_version() === NULL) {
431 $errors[] = 'version_not_set';
432 $correct = false;
435 $formats = $this->applicable_formats();
436 if (empty($formats) || array_sum($formats) === 0) {
437 $errors[] = 'no_formats';
438 $correct = false;
441 $width = $this->preferred_width();
442 if (!is_int($width) || $width <= 0) {
443 $errors[] = 'invalid_width';
444 $correct = false;
446 return $correct;
450 * Subclasses should override this and return true if the
451 * subclass block has a config_global.html file.
453 * @return boolean
455 function has_config() {
456 return false;
460 * Default behavior: print the config_global.html file
461 * You don't need to override this if you're satisfied with the above
463 * @uses $CFG
464 * @return boolean
466 function config_print() {
467 // Default behavior: print the config_global.html file
468 // You don't need to override this if you're satisfied with the above
469 if (!$this->has_config()) {
470 return false;
472 global $CFG;
473 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
474 include($CFG->dirroot.'/blocks/'. $this->name() .'/config_global.html');
475 print_simple_box_end();
476 return true;
480 * Default behavior: save all variables as $CFG properties
481 * You don't need to override this if you 're satisfied with the above
483 * @param array $data
484 * @return boolean
486 function config_save($data) {
487 foreach ($data as $name => $value) {
488 set_config($name, $value);
490 return true;
494 * Default case: the block can be used in all course types
495 * @return array
496 * @todo finish documenting this function
498 function applicable_formats() {
499 // Default case: the block can be used in courses and site index, but not in activities
500 return array('all' => true, 'mod' => false, 'tag' => false);
505 * Default case: the block wants to be 180 pixels wide
506 * @return int
508 function preferred_width() {
509 return 180;
513 * Default return is false - header will be shown
514 * @return boolean
516 function hide_header() {
517 return false;
521 * Default case: an id with the instance and a class with our name in it
522 * @return array
523 * @todo finish documenting this function
525 function html_attributes() {
526 return array('id' => 'inst'.$this->instance->id, 'class' => 'block_'. $this->name());
530 * Given an instance set the class var $instance to it and
531 * load class var $config
532 * @param block $instance
533 * @todo add additional documentation to further explain the format of instance and config
535 function _load_instance($instance) {
536 if (!empty($instance->configdata)) {
537 $this->config = unserialize(base64_decode($instance->configdata));
539 // [pj] This line below is supposed to be an optimization (we don't need configdata anymore)
540 // but what it does is break in PHP5 because the same instance object will be passed to
541 // this function twice in each page view, and the second time it won't have any configdata
542 // so it won't work correctly. Thus it's commented out.
543 // unset($instance->configdata);
544 $this->instance = $instance;
545 $this->specialization();
549 * This function is called on your subclass right after an instance is loaded
550 * Use this function to act on instance data just after it's loaded and before anything else is done
551 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
553 function specialization() {
554 // Just to make sure that this method exists.
558 * Is each block of this type going to have instance-specific configuration?
559 * Normally, this setting is controlled by {@link instance_allow_multiple}: if multiple
560 * instances are allowed, then each will surely need its own configuration. However, in some
561 * cases it may be necessary to provide instance configuration to blocks that do not want to
562 * allow multiple instances. In that case, make this function return true.
563 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple} returns false.
564 * @return boolean
565 * @todo finish documenting this function by explaining per-instance configuration further
567 function instance_allow_config() {
568 return false;
572 * Are you going to allow multiple instances of each block?
573 * If yes, then it is assumed that the block WILL USE per-instance configuration
574 * @return boolean
575 * @todo finish documenting this function by explaining per-instance configuration further
577 function instance_allow_multiple() {
578 // Are you going to allow multiple instances of each block?
579 // If yes, then it is assumed that the block WILL USE per-instance configuration
580 return false;
584 * Default behavior: print the config_instance.html file
585 * You don't need to override this if you're satisfied with the above
587 * @uses $CFG
588 * @return boolean
589 * @todo finish documenting this function
591 function instance_config_print() {
592 // Default behavior: print the config_instance.html file
593 // You don't need to override this if you're satisfied with the above
594 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
595 return false;
597 global $CFG;
599 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
600 print_simple_box_start('center', '', '', 5, 'blockconfiginstance');
601 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
602 print_simple_box_end();
603 } else {
604 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
607 return true;
611 * Serialize and store config data
612 * @return boolean
613 * @todo finish documenting this function
615 function instance_config_save($data,$pinned=false) {
616 $data = stripslashes_recursive($data);
617 $this->config = $data;
618 $table = 'block_instance';
619 if (!empty($pinned)) {
620 $table = 'block_pinned';
622 return set_field($table, 'configdata', base64_encode(serialize($data)), 'id', $this->instance->id);
626 * Replace the instance's configuration data with those currently in $this->config;
627 * @return boolean
628 * @todo finish documenting this function
630 function instance_config_commit($pinned=false) {
631 $table = 'block_instance';
632 if (!empty($pinned)) {
633 $table = 'block_pinned';
635 return set_field($table, 'configdata', base64_encode(serialize($this->config)), 'id', $this->instance->id);
639 * Do any additional initialization you may need at the time a new block instance is created
640 * @return boolean
641 * @todo finish documenting this function
643 function instance_create() {
644 return true;
648 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
649 * @return boolean
650 * @todo finish documenting this function
652 function instance_delete() {
653 return true;
657 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
658 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
659 * but if the framework does allow it, the block can still decide to refuse.
660 * @return boolean
661 * @todo finish documenting this function
663 function user_can_edit() {
664 return true;
668 * Allows the block class to have a say in the user's ability to create new instances of this block.
669 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
670 * but if the framework does allow it, the block can still decide to refuse.
671 * This function has access to the complete page object, the creation related to which is being determined.
672 * @return boolean
673 * @todo finish documenting this function
675 function user_can_addto(&$page) {
676 return true;
682 * Specialized class for displaying a block with a list of icons/text labels
684 * @author Jon Papaioannou
685 * @package blocks
688 class block_list extends block_base {
689 var $content_type = BLOCK_TYPE_LIST;
691 function is_empty() {
692 $this->get_content();
693 return (empty($this->content->items) && empty($this->content->footer));
696 function _print_block() {
697 global $COURSE;
699 // is_empty() includes a call to get_content()
700 if ($this->is_empty() && empty($COURSE->javascriptportal)) {
701 if (empty($this->edit_controls)) {
702 // No content, no edit controls, so just shut up
703 return;
704 } else {
705 // No content but editing, so show something at least
706 $this->_print_shadow();
708 } else {
709 if ($this->hide_header() && empty($this->edit_controls)) {
710 // Header wants to hide, no edit controls to show, so no header it is
711 print_side_block(NULL, '', $this->content->items, $this->content->icons,
712 $this->content->footer, $this->html_attributes());
713 } else {
714 // The full treatment, please. Include the title text.
715 print_side_block($this->_title_html(), '', $this->content->items, $this->content->icons,
716 $this->content->footer, $this->html_attributes(), $this->title);