MDL-8537 Added $CFG as the object to check for enrol method when the course is being...
[moodle-pu.git] / blocks / moodleblock.class.php
bloba5d1169715536d8868517009d401b532c901507f
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
199 * @return boolean
201 function is_empty() {
202 $this->get_content();
203 return(empty($this->content->text) && empty($this->content->footer));
207 * First sets the current value of $this->content to NULL
208 * then calls the block's {@link get_content()} function
209 * to set its value back.
211 * @return stdObject
213 function refresh_content() {
214 // Nothing special here, depends on content()
215 $this->content = NULL;
216 return $this->get_content();
220 * Display the block!
222 function _print_block() {
223 global $COURSE;
225 // is_empty() includes a call to get_content()
226 if ($this->is_empty() && empty($COURSE->javascriptportal)) {
227 if (empty($this->edit_controls)) {
228 // No content, no edit controls, so just shut up
229 return;
230 } else {
231 // No content but editing, so show something at least
232 $this->_print_shadow();
234 } else {
235 if ($this->hide_header() && empty($this->edit_controls)) {
236 // Header wants to hide, no edit controls to show, so no header it is
237 print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
238 } else {
239 // The full treatment, please. Include the title text.
240 print_side_block($this->_title_html(), $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes(), $this->title);
246 * Block contents are missing. Simply display an empty block so that
247 * edit controls are accessbile to the user and they are aware that this
248 * block is in place, even if empty.
250 function _print_shadow() {
251 print_side_block($this->_title_html(), '&nbsp;', NULL, NULL, '', array('class' => 'hidden'), $this->title);
255 function _title_html() {
256 global $CFG;
258 //Accessibility: validation, can't have <div> inside <h2>, use <span>.
259 $title = '<div class="title">';
261 if (!empty($CFG->allowuserblockhiding)) {
262 //Accessibility: added static 'alt' text for the +- icon.
263 //TODO (nfreear): language string 'hide OR show block'
264 $title .= '<div class="hide-show">'.
265 '<a title="'.get_string('showhideblock','access').
266 '" href="#" onclick="elementToggleHide(this, true, function(el) {'.
267 'return findParentNode(el, \'DIV\', \'sideblock\'); '.
268 '}, \''.$CFG->pixpath.'\' ); return false;">'.
269 '<img src="'.$CFG->pixpath.'/spacer.gif" '.
270 'id = "togglehide_inst'.$this->instance->id.'" '.
271 'alt="'.get_string('showhideblock','access').'" class="hide-show-image" /></a></div>';
274 //Accesssibility: added H2 (was in, weblib.php: print_side_block)
275 $title .= '<h2>'.$this->title.'</h2>';
277 if ($this->edit_controls !== NULL) {
278 $title .= $this->edit_controls;
281 $title .= '</div>';
282 return $title;
286 * Sets class $edit_controls var with correct block manipulation links.
288 * @uses $CFG
289 * @uses $USER
290 * @param stdObject $options ?
291 * @todo complete documenting this function. Define $options.
293 function _add_edit_controls($options) {
294 global $CFG, $USER, $PAGE;
296 // this is the context relevant to this particular block instance
297 $blockcontext = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
299 // context for site or course, i.e. participant list etc
300 // check to see if user can edit site or course blocks.
301 // blocks can appear on other pages such as mod and blog pages...
303 switch ($this->instance->pagetype) {
304 case 'course-view':
305 if (!has_capability('moodle/site:manageblocks', $blockcontext)) {
306 return null;
308 break;
309 default:
311 break;
315 if (!isset($this->str)) {
316 $this->str->delete = get_string('delete');
317 $this->str->moveup = get_string('moveup');
318 $this->str->movedown = get_string('movedown');
319 $this->str->moveright = get_string('moveright');
320 $this->str->moveleft = get_string('moveleft');
321 $this->str->hide = get_string('hide');
322 $this->str->show = get_string('show');
323 $this->str->configure = get_string('configuration');
324 $this->str->assignroles = get_string('assignroles', 'role');
327 $movebuttons = '<div class="commands">';
329 if ($this->instance->visible) {
330 $icon = '/t/hide.gif';
331 $title = $this->str->hide;
332 } else {
333 $icon = '/t/show.gif';
334 $title = $this->str->show;
337 if (empty($this->instance->pageid)) {
338 $this->instance->pageid = 0;
340 if (!empty($PAGE->type) and ($this->instance->pagetype == $PAGE->type) and $this->instance->pageid == $PAGE->id) {
341 $page = $PAGE;
342 } else {
343 $page = page_create_object($this->instance->pagetype, $this->instance->pageid);
345 $script = $page->url_get_full(array('instanceid' => $this->instance->id, 'sesskey' => $USER->sesskey));
347 // place holder for roles button
348 $movebuttons .= '<a class="icon roles" title="'. $this->str->assignroles .'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$blockcontext->id.'">' .
349 '<img src="'.$CFG->pixpath.'/i/roles.gif" alt="'.$this->str->assignroles.'" /></a>';
351 $movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&amp;blockaction=toggle">' .
352 '<img src="'. $CFG->pixpath.$icon .'" alt="'.$title.'" /></a>';
354 if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
355 $movebuttons .= '<a class="icon edit" title="'. $this->str->configure .'" href="'.$script.'&amp;blockaction=config">' .
356 '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. $this->str->configure .'" /></a>';
359 $movebuttons .= '<a class="icon delete" title="'. $this->str->delete .'" href="'.$script.'&amp;blockaction=delete">' .
360 '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. $this->str->delete .'" /></a>';
362 if ($options & BLOCK_MOVE_LEFT) {
363 $movebuttons .= '<a class="icon left" title="'. $this->str->moveleft .'" href="'.$script.'&amp;blockaction=moveleft">' .
364 '<img src="'. $CFG->pixpath .'/t/left.gif" alt="'. $this->str->moveleft .'" /></a>';
366 if ($options & BLOCK_MOVE_UP) {
367 $movebuttons .= '<a class="icon up" title="'. $this->str->moveup .'" href="'.$script.'&amp;blockaction=moveup">' .
368 '<img src="'. $CFG->pixpath .'/t/up.gif" alt="'. $this->str->moveup .'" /></a>';
370 if ($options & BLOCK_MOVE_DOWN) {
371 $movebuttons .= '<a class="icon down" title="'. $this->str->movedown .'" href="'.$script.'&amp;blockaction=movedown">' .
372 '<img src="'. $CFG->pixpath .'/t/down.gif" alt="'. $this->str->movedown .'" /></a>';
374 if ($options & BLOCK_MOVE_RIGHT) {
375 $movebuttons .= '<a class="icon right" title="'. $this->str->moveright .'" href="'.$script.'&amp;blockaction=moveright">' .
376 '<img src="'. $CFG->pixpath .'/t/right.gif" alt="'. $this->str->moveright .'" /></a>';
379 $movebuttons .= '</div>';
380 $this->edit_controls = $movebuttons;
384 * Tests if this block has been implemented correctly.
385 * Also, $errors isn't used right now
387 * @return boolean
390 function _self_test() {
391 // Tests if this block has been implemented correctly.
392 // Also, $errors isn't used right now
393 $errors = array();
395 $correct = true;
396 if ($this->get_title() === NULL) {
397 $errors[] = 'title_not_set';
398 $correct = false;
400 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT))) {
401 $errors[] = 'invalid_content_type';
402 $correct = false;
404 //following selftest was not working when roles&capabilities were used from block
405 /* if ($this->get_content() === NULL) {
406 $errors[] = 'content_not_set';
407 $correct = false;
409 if ($this->get_version() === NULL) {
410 $errors[] = 'version_not_set';
411 $correct = false;
414 $formats = $this->applicable_formats();
415 if (empty($formats) || array_sum($formats) === 0) {
416 $errors[] = 'no_formats';
417 $correct = false;
420 $width = $this->preferred_width();
421 if (!is_int($width) || $width <= 0) {
422 $errors[] = 'invalid_width';
423 $correct = false;
425 return $correct;
429 * Subclasses should override this and return true if the
430 * subclass block has a config_global.html file.
432 * @return boolean
434 function has_config() {
435 return false;
439 * Default behavior: print the config_global.html file
440 * You don't need to override this if you're satisfied with the above
442 * @uses $CFG
443 * @return boolean
445 function config_print() {
446 // Default behavior: print the config_global.html file
447 // You don't need to override this if you're satisfied with the above
448 if (!$this->has_config()) {
449 return false;
451 global $CFG;
452 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
453 include($CFG->dirroot.'/blocks/'. $this->name() .'/config_global.html');
454 print_simple_box_end();
455 return true;
459 * Default behavior: save all variables as $CFG properties
460 * You don't need to override this if you 're satisfied with the above
462 * @param array $data
463 * @return boolean
465 function config_save($data) {
466 foreach ($data as $name => $value) {
467 set_config($name, $value);
469 return true;
473 * Default case: the block can be used in all course types
474 * @return array
475 * @todo finish documenting this function
477 function applicable_formats() {
478 // Default case: the block can be used in courses and site index, but not in activities
479 return array('all' => true, 'mod' => false);
484 * Default case: the block wants to be 180 pixels wide
485 * @return int
487 function preferred_width() {
488 return 180;
492 * Default return is false - header will be shown
493 * @return boolean
495 function hide_header() {
496 return false;
500 * Default case: an id with the instance and a class with our name in it
501 * @return array
502 * @todo finish documenting this function
504 function html_attributes() {
505 return array('id' => 'inst'.$this->instance->id, 'class' => 'block_'. $this->name());
509 * Given an instance set the class var $instance to it and
510 * load class var $config
511 * @param block $instance
512 * @todo add additional documentation to further explain the format of instance and config
514 function _load_instance($instance) {
515 if (!empty($instance->configdata)) {
516 $this->config = unserialize(base64_decode($instance->configdata));
518 // [pj] This line below is supposed to be an optimization (we don't need configdata anymore)
519 // but what it does is break in PHP5 because the same instance object will be passed to
520 // this function twice in each page view, and the second time it won't have any configdata
521 // so it won't work correctly. Thus it's commented out.
522 // unset($instance->configdata);
523 $this->instance = $instance;
524 $this->specialization();
528 * This function is called on your subclass right after an instance is loaded
529 * Use this function to act on instance data just after it's loaded and before anything else is done
530 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
532 function specialization() {
533 // Just to make sure that this method exists.
537 * Is each block of this type going to have instance-specific configuration?
538 * Normally, this setting is controlled by {@link instance_allow_multiple}: if multiple
539 * instances are allowed, then each will surely need its own configuration. However, in some
540 * cases it may be necessary to provide instance configuration to blocks that do not want to
541 * allow multiple instances. In that case, make this function return true.
542 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple} returns false.
543 * @return boolean
544 * @todo finish documenting this function by explaining per-instance configuration further
546 function instance_allow_config() {
547 return false;
551 * Are you going to allow multiple instances of each block?
552 * If yes, then it is assumed that the block WILL USE per-instance configuration
553 * @return boolean
554 * @todo finish documenting this function by explaining per-instance configuration further
556 function instance_allow_multiple() {
557 // Are you going to allow multiple instances of each block?
558 // If yes, then it is assumed that the block WILL USE per-instance configuration
559 return false;
563 * Default behavior: print the config_instance.html file
564 * You don't need to override this if you're satisfied with the above
566 * @uses $CFG
567 * @return boolean
568 * @todo finish documenting this function
570 function instance_config_print() {
571 // Default behavior: print the config_instance.html file
572 // You don't need to override this if you're satisfied with the above
573 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
574 return false;
576 global $CFG;
578 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
579 print_simple_box_start('center', '', '', 5, 'blockconfiginstance');
580 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
581 print_simple_box_end();
582 } else {
583 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
586 return true;
590 * Serialize and store config data
591 * @return boolean
592 * @todo finish documenting this function
594 function instance_config_save($data,$pinned=false) {
595 $data = stripslashes_recursive($data);
596 $this->config = $data;
597 $table = 'block_instance';
598 if (!empty($pinned)) {
599 $table = 'block_pinned';
601 return set_field($table, 'configdata', base64_encode(serialize($data)), 'id', $this->instance->id);
605 * Replace the instance's configuration data with those currently in $this->config;
606 * @return boolean
607 * @todo finish documenting this function
609 function instance_config_commit($pinned=false) {
610 $table = 'block_instance';
611 if (!empty($pinned)) {
612 $table = 'block_pinned';
614 return set_field($table, 'configdata', base64_encode(serialize($this->config)), 'id', $this->instance->id);
618 * Do any additional initialization you may need at the time a new block instance is created
619 * @return boolean
620 * @todo finish documenting this function
622 function instance_create() {
623 return true;
627 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
628 * @return boolean
629 * @todo finish documenting this function
631 function instance_delete() {
632 return true;
636 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
637 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
638 * but if the framework does allow it, the block can still decide to refuse.
639 * @return boolean
640 * @todo finish documenting this function
642 function user_can_edit() {
643 return true;
647 * Allows the block class to have a say in the user's ability to create new instances of this block.
648 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
649 * but if the framework does allow it, the block can still decide to refuse.
650 * This function has access to the complete page object, the creation related to which is being determined.
651 * @return boolean
652 * @todo finish documenting this function
654 function user_can_addto(&$page) {
655 return true;
661 * Specialized class for displaying a block with a list of icons/text labels
663 * @author Jon Papaioannou
664 * @package blocks
667 class block_list extends block_base {
668 var $content_type = BLOCK_TYPE_LIST;
670 function is_empty() {
671 $this->get_content();
672 return (empty($this->content->items) && empty($this->content->footer));
675 function _print_block() {
676 global $COURSE;
678 // is_empty() includes a call to get_content()
679 if ($this->is_empty() && empty($COURSE->javascriptportal)) {
680 if (empty($this->edit_controls)) {
681 // No content, no edit controls, so just shut up
682 return;
683 } else {
684 // No content but editing, so show something at least
685 $this->_print_shadow();
687 } else {
688 if ($this->hide_header() && empty($this->edit_controls)) {
689 // Header wants to hide, no edit controls to show, so no header it is
690 print_side_block(NULL, '', $this->content->items, $this->content->icons,
691 $this->content->footer, $this->html_attributes());
692 } else {
693 // The full treatment, please. Include the title text.
694 print_side_block($this->_title_html(), '', $this->content->items, $this->content->icons,
695 $this->content->footer, $this->html_attributes(), $this->title);