<?php // $Id$
+/**
+ * Parent class for moodle blocks
+ *
+ * @author Jon Papaioannou
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package blocks
+ */
+
+/// Constants
+
+/**
+ * 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.
+ */
define('BLOCK_TYPE_LIST', 1);
+
+/**
+ * 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.
+ */
define('BLOCK_TYPE_TEXT', 2);
+
+/**
+ * Block type of nuke. Compitibility with post nuke blocks. Basically treated as BLOCK_TYPE_TEXT.
+ */
define('BLOCK_TYPE_NUKE', 3);
+/**
+ * Class for describing a moodle block
+ *
+ * @author Jon Papaioannou
+ * @package blocks
+ */
class MoodleBlock {
+
+ /**
+ * Internal var for storing/caching translated strings
+ * @var string $str
+ */
var $str;
+
+ /**
+ * The title of the block to be displayed in the block header area.
+ * @var string $title
+ */
var $title = NULL;
+
+ /**
+ * A course object that this block is associated with. Take care - it may be NULL.
+ * @var COURSE $course
+ */
var $course = NULL;
+
+ /**
+ * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE
+ * @var int $content_type
+ */
var $content_type = NULL;
+
+ /**
+ * An object to contain the information to be displayed in the block.
+ * @var stdObject $content
+ */
var $content = NULL;
+
+ /**
+ * A string generated by {@link add_edit_controls()} to display block manipulation links when the user is in editing mode.
+ * @var string $edit_controls
+ */
var $edit_controls = NULL;
+
+ /**
+ * The current version that the block type defines.
+ * @var string $version
+ */
var $version = NULL;
+
+ /**
+ * The initialized instance of this block object.
+ * @var block $instance
+ */
var $instance = NULL;
+
+ /**
+ * An object containing the instance configuration information for the current instance of this block.
+ * @var stdObject $config
+ */
var $config = NULL;
+
+/// Functions
+
+ /**
+ * The class constructor
+ *
+ */
function MoodleBlock() {
$this->init();
}
+ /**
+ * Returns the block name, as present in the class name,
+ * the database, the block directory, etc etc.
+ *
+ * @return string
+ */
function name() {
// Returns the block name, as present in the class name,
// the database, the block directory, etc etc.
static $myname;
- if($myname === NULL) {
+ if ($myname === NULL) {
$myname = strtolower(get_class($this));
$myname = substr($myname, strpos($myname, '_') + 1);
}
return $myname;
}
+ /**
+ * Parent class version of this function simply returns NULL
+ * This should be implemented by the derived class to return
+ * the content object.
+ *
+ * @return stdObject
+ */
function get_content() {
// This should be implemented by the derived class.
return NULL;
}
+
+ /**
+ * Returns the class $title var value.
+ *
+ * Intentionally doesn't check if a title is set.
+ * This is already done in {@link _self_test()}
+ *
+ * @return string $this->title
+ */
function get_title() {
// Intentionally doesn't check if a title is set. This is already done in _self_test()
return $this->title;
}
+
+ /**
+ * Returns the class $content_type var value.
+ *
+ * Intentionally doesn't check if content_type is set.
+ * This is already done in {@link _self_test()}
+ *
+ * @return string $this->content_type
+ */
function get_content_type() {
// Intentionally doesn't check if a content_type is set. This is already done in _self_test()
return $this->content_type;
}
+
+ /**
+ * Returns the class $version var value.
+ *
+ * Intentionally doesn't check if a version is set.
+ * This is already done in {@link _self_test()}
+ *
+ * @return string $this->version
+ */
function get_version() {
// Intentionally doesn't check if a version is set. This is already done in _self_test()
return $this->version;
}
+
+ /**
+ * Returns the class $header var value.
+ *
+ * Intentionally doesn't check if a header is set.
+ * This is already done in {@link _self_test()}
+ *
+ * @return string $this->header
+ */
function get_header() {
// Intentionally doesn't check if a header is set. This is already done in _self_test()
return $this->header;
}
+
+ /**
+ * First sets the current value of $this->content to NULL
+ * then calls the block's {@link get_content()} function
+ * to set its value back.
+ *
+ * @return stdObject
+ */
function refresh_content() {
// Nothing special here, depends on content()
$this->content = NULL;
return $this->get_content();
}
+
+ /**
+ * Display the block!
+ */
function print_block() {
// Wrap the title in a floating DIV, in case we have edit controls to display
// These controls will always be wrapped on a right-floating DIV
$title = '<div style="float: left;">'.$this->title.'</div>';
- if($this->edit_controls !== NULL) {
+ if ($this->edit_controls !== NULL) {
$title .= $this->edit_controls;
}
$this->get_content();
- if(!isset($this->content->footer)) {
+ if (!isset($this->content->footer)) {
$this->content->footer = '';
}
switch($this->content_type) {
case BLOCK_TYPE_NUKE:
case BLOCK_TYPE_TEXT:
- if(empty($this->content->text) && empty($this->content->footer)) {
- if(empty($this->edit_controls)) {
+ if (empty($this->content->text) && empty($this->content->footer)) {
+ if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
- }
- else {
+ } else {
// No content but editing, so show something at least
$this->print_shadow();
}
- }
- else {
- if($this->hide_header() && empty($this->edit_controls)) {
+ } else {
+ if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
- }
- else {
+ } else {
// The full treatment, please
print_side_block($title, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
}
}
break;
case BLOCK_TYPE_LIST:
- if(empty($this->content->items) && empty($this->content->footer)) {
- if(empty($this->edit_controls)) {
+ if (empty($this->content->items) && empty($this->content->footer)) {
+ if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
- }
- else {
+ } else {
// No content but editing, so show something at least
$this->print_shadow();
}
- }
- else {
- if($this->hide_header() && empty($this->edit_controls)) {
+ } else {
+ if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
- }
- else {
+ } else {
// The full treatment, please
print_side_block($title, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
}
break;
}
}
+
+ /**
+ * Block contents are missing. Simply display an empty block so that
+ * edit controls are accessbile to the user and they are aware that this
+ * block is in place, even if empty.
+ */
function print_shadow() {
$title = '<div style="float: left;">'.$this->title.'</div>';
- if($this->edit_controls !== NULL) {
+ if ($this->edit_controls !== NULL) {
$title .= $this->edit_controls;
}
print_side_block($title, ' ', NULL, NULL, '');
}
+ /**
+ * Sets class $edit_controls var with correct block manipulation links.
+ *
+ * @uses $CFG
+ * @uses $THEME
+ * @uses $USER
+ * @param stdObject $options ?
+ * @todo complete documenting this function. Define $options.
+ */
function add_edit_controls($options) {
global $CFG, $THEME, $USER;
$path = $CFG->wwwroot.'/course';
if (empty($THEME->custompix)) {
- $pixpath = $path.'/../pix';
+ $pixpath = $path .'/../pix';
} else {
- $pixpath = $path.'/../theme/'.$CFG->theme.'/pix';
+ $pixpath = $path .'/../theme/'. $CFG->theme .'/pix';
}
- $sesskeystr = '&sesskey='.$USER->sesskey;
+ $sesskeystr = '&sesskey='. $USER->sesskey;
$movebuttons = '<div style="float: right;">';
- if($this->instance->visible) {
+ if ($this->instance->visible) {
$icon = '/t/hide.gif';
$title = $this->str->hide;
- }
- else {
+ } else {
$icon = '/t/show.gif';
$title = $this->str->show;
}
$movebuttons .= '<a style="margin-right: 6px; margin-left: 2px;" title="'. $title .'" href="'.$script.'&blockaction=toggle&instanceid='. $this->instance->id . $sesskeystr .'">' .
'<img src="'. $pixpath.$icon .'" alt=\"\" /></a>';
- if($options & BLOCK_CONFIGURE) {
+ if ($options & BLOCK_CONFIGURE) {
$movebuttons .= '<a style="margin-right: 6px; margin-left: 2px;" title="'. $this->str->configure .'" href="'. $script .'&blockaction=config&instanceid='. $this->instance->id.$sesskeystr .'">' .
'<img src="'. $pixpath .'/t/edit.gif" alt=\"\" /></a>';
}
$this->edit_controls = $movebuttons;
}
+ /**
+ * Tests if this block has been implemented correctly.
+ * Also, $errors isn't used right now
+ *
+ * @return boolean
+ */
+
function _self_test() {
// Tests if this block has been implemented correctly.
// Also, $errors isn't used right now
$errors = array();
$correct = true;
- if($this->get_title() === NULL) {
+ if ($this->get_title() === NULL) {
$errors[] = 'title_not_set';
$correct = false;
}
- if(!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE))) {
+ if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE))) {
$errors[] = 'invalid_content_type';
$correct = false;
}
- if($this->get_content() === NULL) {
+ if ($this->get_content() === NULL) {
$errors[] = 'content_not_set';
$correct = false;
}
- if($this->get_version() === NULL) {
+ if ($this->get_version() === NULL) {
$errors[] = 'version_not_set';
$correct = false;
}
$formats = $this->applicable_formats();
- if(empty($formats) || array_sum($formats) === 0) {
+ if (empty($formats) || array_sum($formats) === 0) {
$errors[] = 'no_course_formats';
$correct = false;
}
$width = $this->preferred_width();
- if(!is_int($width) || $width <= 0) {
+ if (!is_int($width) || $width <= 0) {
$errors[] = 'invalid_width';
$correct = false;
}
return $correct;
}
+ /**
+ * Subclasses should override this and return true if the
+ * subclass block has a config_global.html file.
+ *
+ * @return boolean
+ */
function has_config() {
return false;
}
+
+ /**
+ * Default behavior: print the config_global.html file
+ * You don't need to override this if you're satisfied with the above
+ *
+ * @uses $CFG
+ * @uses $USER
+ * @uses $THEME
+ * @return boolean
+ */
function print_config() {
// Default behavior: print the config_global.html file
- // You don't need to override this if you 're satisfied with the above
- if(!$this->has_config()) {
+ // You don't need to override this if you're satisfied with the above
+ if (!$this->has_config()) {
return false;
}
global $CFG, $USER, $THEME;
return true;
}
+ /**
+ * Default behavior: save all variables as $CFG properties
+ * You don't need to override this if you 're satisfied with the above
+ *
+ * @param array $config
+ * @return boolean
+ */
function handle_config($config) {
// Default behavior: save all variables as $CFG properties
// You don't need to override this if you 're satisfied with the above
- if(!$this->has_config()) {
+ if (!$this->has_config()) {
return false;
}
foreach ($config as $name => $value) {
return true;
}
+ /**
+ * Default case: the block can be used in all course types
+ * @return array
+ * @todo finish documenting this function
+ */
function applicable_formats() {
// Default case: the block can be used in all course types
return array('all' => true);
}
+
+ /**
+ * Default case: the block wants to be 180 pixels wide
+ * @return int
+ */
function preferred_width() {
// Default case: the block wants to be 180 pixels wide
return 180;
}
+ /**
+ * Default return is false - header will be shown
+ * @return boolean
+ */
function hide_header() {
//Default, false--> the header is shown
return false;
}
-
+
+ /**
+ * Default case: just an id for the block, with our name in it
+ * @return array
+ * @todo finish documenting this function
+ */
function html_attributes() {
// Default case: just an id for the block, with our name in it
return array('id' => 'block_'. $this->name());
}
+ /**
+ * Given an instance set the class var $instance to it and
+ * load class var $config
+ * @param block $instance
+ * @todo add additional documentation to further explain the format of instance and config
+ */
function load_instance($instance) {
- if(!empty($instance->configdata)) {
+ if (!empty($instance->configdata)) {
$this->config = unserialize(base64_decode($instance->configdata));
}
unset($instance->configdata);
return;
}
+ /**
+ *Are you going to allow multiple instances of each block?
+ * If yes, then it is assumed that the block WILL USE per-instance configuration
+ * @return boolean
+ * @todo finish documenting this function by explaining per-instance configuration further
+ */
function instance_allow_multiple() {
// Are you going to allow multiple instances of each block?
// If yes, then it is assumed that the block WILL USE per-instance configuration
return false;
}
+ /**
+ * Default behavior: print the config_instance.html file
+ * You don't need to override this if you're satisfied with the above
+ *
+ * @uses $CFG
+ * @uses $THEME
+ * @uses $USER
+ * @return boolean
+ * @todo finish documenting this function
+ */
function instance_config_print() {
// Default behavior: print the config_instance.html file
// You don't need to override this if you're satisfied with the above
- if(!$this->instance_allow_multiple()) {
+ if (!$this->instance_allow_multiple()) {
return false;
}
global $CFG, $USER, $THEME;
- if(is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
+ if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
print_simple_box_start('center', '', $THEME->cellheading);
include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
print_simple_box_end();
return true;
}
-
+
+ /**
+ * Serialize and store config data
+ * @return boolean
+ * @todo finish documenting this function
+ */
function instance_config_save($data) {
$data = stripslashes_recursive($data);
$this->config = $data;
}
+/**
+ * Class for supporting a postnuke style block as a moodle block
+ *
+ * @author Jon Papaioannou
+ * @package blocks
+ */
class MoodleBlock_Nuke extends MoodleBlock {
function get_content() {
- if($this->content !== NULL) {
+ if ($this->content !== NULL) {
return $this->content;
}
// This whole thing begs to be written for PHP >= 4.3.0 using glob();
$dir = $CFG->dirroot .'/blocks/'. $this->name() .'/nuke/';
- if($dh = @opendir($dir)) {
+ if ($dh = @opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$regs = array();
- if(ereg('^block\-(.*)\.php$', $file, $regs)) {
+ if (ereg('^block\-(.*)\.php$', $file, $regs)) {
// Found it! Let's prepare the environment...
$oldvals = array();
- if(isset($GLOBALS['admin'])) {
+ if (isset($GLOBALS['admin'])) {
$oldvals['admin'] = $GLOBALS['admin'];
}
}
// We should have $content set now
- if(!isset($content)) {
+ if (!isset($content)) {
return NULL;
}
return $this->content->text = $content;