require_variable($_REQUEST['block']);
$blockid = intval($_REQUEST['block']);
-
- if(($blockrecord = get_record('blocks', 'id', $blockid)) === false) {
+
+ if(($blockrecord = blocks_get_record($blockid)) === false) {
error('This block does not exist');
}
- $block = block_instance($blockrecord->name, NULL);
+ $block = block_instance($blockrecord->name);
if($block === false) {
error('Problem in instantiating block object');
}
require_once('../config.php');
require_once($CFG->libdir.'/blocklib.php');
- optional_variable($_GET['hide']);
- optional_variable($_GET['show']);
- optional_variable($_GET['delete']);
optional_variable($_GET['confirm'], 0);
- $delete = $_GET['delete']; // Dependency remover
+ $hide = optional_param('hide', 0, PARAM_INT);
+ $show = optional_param('show', 0, PARAM_INT);
+ $delete = optional_param('delete', 0, PARAM_INT);
+ $multiple = optional_param('multiple', 0, PARAM_INT);
require_login();
$strhide = get_string('hide');
$strshow = get_string('show');
$strsettings = get_string('settings');
- $strcourses = get_string('courses');
+ $strcourses = get_string('blockinstances', 'admin');
$strname = get_string('name');
+ $strmultiple = get_string('blockmultiple', 'admin');
print_header("$site->shortname: $strmanageblocks", "$site->fullname",
"<a href=\"index.php\">$stradministration</a> -> ".
/// If data submitted, then process and store.
- if (!empty($_GET['hide']) and confirm_sesskey()) {
- if (!$block = get_record('blocks', 'id', $_GET['hide'])) {
+ if (!empty($hide) && confirm_sesskey()) {
+ if (!$block = get_record('block', 'id', $hide)) {
error("Block doesn't exist!");
}
- set_field('blocks', 'visible', '0', 'id', $block->id); // Hide block
+ set_field('block', 'visible', '0', 'id', $block->id); // Hide block
}
- if (!empty($_GET['show']) and confirm_sesskey() ) {
- if (!$block = get_record('blocks', 'id', $_GET['show'])) {
+ if (!empty($show) && confirm_sesskey() ) {
+ if (!$block = get_record('block', 'id', $show)) {
error("Block doesn't exist!");
}
- set_field('blocks', 'visible', '1', 'id', $block->id); // Show block
+ set_field('block', 'visible', '1', 'id', $block->id); // Show block
}
- if (!empty($delete) and confirm_sesskey()) {
+ if (!empty($multiple) && confirm_sesskey()) {
+ if (!$block = blocks_get_record($multiple)) {
+ error("Block doesn't exist!");
+ }
+ $block->multiple = !$block->multiple;
+ update_record('block', $block);
+ }
- if (!$block = get_record('blocks', 'id', $delete)) {
+ if (!empty($delete) && confirm_sesskey()) {
+
+ if (!$block = blocks_get_record($delete)) {
error("Block doesn't exist!");
}
- $blockobject = block_instance($block->name, $site);
+ $blockobject = block_instance($block->name);
$strblockname = $blockobject->get_title();
if (!$_GET['confirm']) {
} else {
// Delete block
- if (!delete_records('blocks', 'id', $block->id)) {
+ if (!delete_records('block', 'id', $block->id)) {
notify("Error occurred while deleting the $strblockname record from blocks table");
}
- blocks_update_every_block_by_id($block->id, 'delete'); // Delete blocks in all courses by id
+ $instances = get_records('block_instance', 'blockid', $block->id);
+ if(!empty($instances)) {
+ foreach($instances as $instance) {
+ blocks_delete_instance($instance);
+ }
+ }
// Then the tables themselves
/// Get and sort the existing blocks
- if (false === ($blocks = get_records('blocks'))) {
+ if (false === ($blocks = get_records('block'))) {
error('No blocks found!'); // Should never happen
}
foreach ($blocks as $block) {
- if(($blockobject = block_instance($block->name, NULL)) === false) {
+ if(($blockobject = block_instance($block->name)) === false) {
// Failed to load
continue;
}
//$modpixpath = '../theme/'.$CFG->theme.'/pix/mod';
}
- $table->head = array ($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings);
- $table->align = array ('LEFT', 'RIGHT', 'LEFT', 'CENTER', 'CENTER', 'CENTER');
- $table->wrap = array ("NOWRAP", "", "", "", "","");
- $table->size = array ("100%", "10", "10", "10", "10","12");
- $table->width = "100";
+ $table->head = array ($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings);
+ $table->align = array ('LEFT', 'RIGHT', 'LEFT', 'CENTER', 'CENTER', 'CENTER', 'CENTER');
+ $table->wrap = array ('NOWRAP', '', '', '', '', '', '');
+ $table->size = array ('100%', '10', '10', '10', '10','12');
+ $table->width = '100';
foreach ($blockbyname as $blockname => $blockid) {
$settings = '<a href="block.php?block='.$blockid.'&sesskey='.$USER->sesskey.'">'.$strsettings.'</a>';
}
- $count = blocks_get_courses_using_block_by_id($blockid);
+ $count = count_records('block_instance', 'blockid', $blockid);
$class = ''; // Nothing fancy, by default
if ($blocks[$blockid]->visible) {
'<img src="'.$pixpath.'/i/show.gif" style="height: 16px; width: 16px;" alt=\"\" /></a>';
$class = ' class="dimmed_text"'; // Leading space required!
}
+ if ($blockobject->instance_allow_multiple()) {
+ if($blocks[$blockid]->multiple) {
+ $multiple = '<nobr>'.get_string('yes').' (<a href="blocks.php?multiple='.$blockid.'&sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</nobr>';
+ }
+ else {
+ $multiple = '<nobr>'.get_string('no').' (<a href="blocks.php?multiple='.$blockid.'&sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</nobr>';
+ }
+ }
+ else {
+ $multiple = '';
+ }
- $table->data[] = array ('<p'.$class.'>'.$blockobject->get_title().'</p>', $count, $blockobject->get_version(), $visible, $delete, $settings);
+ $table->data[] = array(
+ '<p'.$class.'>'.$blockobject->get_title().'</p>',
+ $count,
+ $blockobject->get_version(),
+ $visible,
+ $multiple,
+ $delete,
+ $settings
+ );
}
echo '<p>';
print_table($table);
if (!empty($USER->id)) { // Additional identity check
if (!confirm_sesskey()) {
- error(get_string('confirmsesskeybad', 'error'));
+ //error(get_string('confirmsesskeybad', 'error'));
}
}
// [pj] We are about to create the site, so let's add some blocks...
// calendar_month is included as a Moodle feature advertisement ;-)
require_once($CFG->dirroot.'/lib/blocklib.php');
- $form->blockinfo = blocks_get_default_blocks(NULL, blocks_get_config_default('site'));
if ($newid = insert_record("course", $form)) {
+ // Site created, add blocks for it
+ $page = new stdClass;
+ $page->type = MOODLE_PAGE_COURSE;
+ $page->id = $newid;
+ blocks_repopulate_page($page); // Return value not checked because you can always edit later
+
$cat->name = get_string("miscellaneous");
if (insert_record("course_categories", $cat)) {
redirect("$CFG->wwwroot/$CFG->admin/index.php", get_string("changessaved"), 1);
<?PHP //$Id$
class CourseBlock_activity_modules extends MoodleBlock {
- function CourseBlock_activity_modules($course) {
+ function init() {
$this->title = get_string('activities');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004041000;
}
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
if ($modnamesused) {
foreach ($modnamesused as $modname => $modfullname) {
if ($modname != 'label') {
- $this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->course->id.'">'.$modnamesplural[$modname].'</a>';
+ $this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->instance->pageid.'">'.$modnamesplural[$modname].'</a>';
$this->content->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" height="16" width="16" alt="" />';
}
}
<?php //$Id$
class CourseBlock_admin extends MoodleBlock {
- function CourseBlock_admin($course) {
+ function init() {
$this->title = get_string('administration');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004081200;
}
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
- if (empty($this->course)) {
+ if (empty($this->instance)) {
$this->content = '';
- } else if ($this->course->id == SITEID) {
+ } else if ($this->instance->pageid == SITEID) {
$this->load_content_for_site();
} else {
$this->load_content_for_course();
return $this->content;
}
- if (isteacher($this->course->id)) {
+ $course = get_record('course', 'id', $this->instance->pageid);
- $isteacheredit = isteacheredit($this->course->id);
+ if (isteacher($this->instance->pageid)) {
+
+ $isteacheredit = isteacheredit($this->instance->pageid);
if ($isteacheredit) {
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" />';
- if (isediting($this->course->id)) {
- $this->content->items[]='<a href="view.php?id='.$this->course->id.'&edit=off">'.get_string('turneditingoff').'</a>';
+ if (isediting($this->instance->pageid)) {
+ $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&edit=off">'.get_string('turneditingoff').'</a>';
} else {
- $this->content->items[]='<a href="view.php?id='.$this->course->id.'&edit=on">'.get_string('turneditingon').'</a>';
+ $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&edit=on">'.get_string('turneditingon').'</a>';
}
- $this->content->items[]='<a href="edit.php?id='.$this->course->id.'">'.get_string('settings').'...</a>';
+ $this->content->items[]='<a href="edit.php?id='.$this->instance->pageid.'">'.get_string('settings').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/settings.gif" height="16" width="16" alt="" />';
- if (iscreator() or !empty($CFG->teacherassignteachers)) {
- if (!$this->course->teachers) {
- $this->course->teachers = get_string('defaultcourseteachers');
+ if (iscreator() || !empty($CFG->teacherassignteachers)) {
+ if (!$course->teachers) {
+ $course->teachers = get_string('defaultcourseteachers');
}
- $this->content->items[]='<a href="teacher.php?id='.$this->course->id.'">'.$this->course->teachers.'...</a>';
+ $this->content->items[]='<a href="teacher.php?id='.$this->instance->pageid.'">'.$course->teachers.'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
}
- if (!$this->course->students) {
- $this->course->students = get_string('defaultcoursestudents');
+ if (!$course->students) {
+ $course->students = get_string('defaultcoursestudents');
}
- $this->content->items[]='<a href="student.php?id='.$this->course->id.'">'.$this->course->students.'...</a>';
+ $this->content->items[]='<a href="student.php?id='.$this->instance->pageid.'">'.$course->students.'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
- $this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->course->id.'">'.get_string('backup').'...</a>';
+ $this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->instance->pageid.'">'.get_string('backup').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" height="16" width="16" alt="" />';
- $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'&wdir=/backupdata">'.get_string('restore').'...</a>';
+ $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'&wdir=/backupdata">'.get_string('restore').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" height="16" width="16" alt="" />';
- $this->content->items[]='<a href="scales.php?id='.$this->course->id.'">'.get_string('scales').'...</a>';
+ $this->content->items[]='<a href="scales.php?id='.$this->instance->pageid.'">'.get_string('scales').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/scales.gif" height="16" width="16" alt="" />';
}
- $this->content->items[]='<a href="grades.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
+ $this->content->items[]='<a href="grades.php?id='.$this->instance->pageid.'">'.get_string('grades').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="" />';
- $this->content->items[]='<a href="log.php?id='.$this->course->id.'">'.get_string('logs').'...</a>';
+ $this->content->items[]='<a href="log.php?id='.$this->instance->pageid.'">'.get_string('logs').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/log.gif" height="16" width="16" alt="" />';
if ($isteacheredit) {
- $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'">'.get_string('files').'...</a>';
+ $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'">'.get_string('files').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" height="16" width="16" alt="" />';
}
- $this->content->items[]='<a href="'.$CFG->wwwroot.'/doc/view.php?id='.$this->course->id.'&file=teacher.html">'.get_string('help').'...</a>';
+ $this->content->items[]='<a href="'.$CFG->wwwroot.'/doc/view.php?id='.$this->instance->pageid.'&file=teacher.html">'.get_string('help').'...</a>';
$this->content->icons[]='<img src="'.$CFG->modpixpath.'/resource/icon.gif" height="16" width="16" alt="" />';
- if ($teacherforum = forum_get_course_forum($this->course->id, 'teacher')) {
+ if ($teacherforum = forum_get_course_forum($this->instance->pageid, 'teacher')) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$teacherforum->id.'">'.get_string('nameteacher', 'forum').'</a>';
$this->content->icons[]='<img src="'.$CFG->modpixpath.'/forum/icon.gif" height="16" width="16" alt="" />';
}
} else if (!isguest()) { // Students menu
- if ($this->course->showgrades) {
- $this->content->items[]='<a href="grade.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
+ if ($course->showgrades) {
+ $this->content->items[]='<a href="grade.php?id='.$this->instance->pageid.'">'.get_string('grades').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="" />';
}
- if ($this->course->showreports) {
- $this->content->items[]='<a href="user.php?id='.$this->course->id.'&user='.$USER->id.'">'.get_string('activityreport').'...</a>';
+ if ($course->showreports) {
+ $this->content->items[]='<a href="user.php?id='.$this->instance->pageid.'&user='.$USER->id.'">'.get_string('activityreport').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/report.gif" height="16" width="16" alt="" />';
}
if (is_internal_auth()) {
- $this->content->items[]='<a href="'.$CFG->wwwroot.'/login/change_password.php?id='.$this->course->id.'">'.get_string('changepassword').'...</a>';
+ $this->content->items[]='<a href="'.$CFG->wwwroot.'/login/change_password.php?id='.$this->instance->pageid.'">'.get_string('changepassword').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
} else if ($CFG->changepassword) {
$this->content->items[]='<a href="'.$CFG->changepassword.'">'.get_string('changepassword').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
}
if ($CFG->allowunenroll) {
- $this->content->items[]='<a href="unenrol.php?id='.$this->course->id.'">'.get_string('unenrolme', '', $this->course->shortname).'...</a>';
+ $this->content->items[]='<a href="unenrol.php?id='.$this->instance->pageid.'">'.get_string('unenrolme', '', $course->shortname).'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
}
}
<?PHP //$Id$
class CourseBlock_calendar_month extends MoodleBlock {
- function CourseBlock_calendar_month($course) {
+ function init() {
$this->title = get_string('calendar', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004081200;
}
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
- if (empty($this->course)) { // Overrides: use no course at all
+ if (empty($this->instance)) { // Overrides: use no course at all
$courseshown = false;
$filtercourse = array();
} else {
- $courseshown = $this->course->id;
+ $courseshown = $this->instance->pageid;
- if($this->course->id == SITEID) {
+ if($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = NULL;
<?PHP //$Id$
class CourseBlock_calendar_upcoming extends MoodleBlock {
- function CourseBlock_calendar_upcoming ($course) {
+ function init() {
$this->title = get_string('upcomingevents', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004052600;
}
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->text = '';
- if (empty($this->course)) { // Overrides: use no course at all
+ if (empty($this->instance)) { // Overrides: use no course at all
$courseshown = false;
$filtercourse = array();
} else {
- $courseshown = $this->course->id;
+ $courseshown = $this->instance->pageid;
$this->content->footer = '<br /><a href="'.$CFG->wwwroot.
- '/calendar/view.php?view=upcoming&course='.$this->course->id.'">'.
+ '/calendar/view.php?view=upcoming&course='.$courseshown.'">'.
get_string('gotocalendar', 'calendar').'</a>...';
$this->content->footer .= '<br /><a href="'.$CFG->wwwroot.
- '/calendar/event.php?action=new&course='.$this->course->id.'">'.
+ '/calendar/event.php?action=new&course='.$courseshown.'">'.
get_string('newevent', 'calendar').'</a>...';
- if ($this->course->id == SITEID) {
+ if ($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = NULL;
get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS),
get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS));
- if (!empty($this->course)) {
+ if (!empty($this->instance)) {
$this->content->text = calendar_get_sideblock_upcoming($events,
- 'view.php?view=day&course='.$this->course->id.'&');
+ 'view.php?view=day&course='.$courseshown.'&');
}
if (empty($this->content->text)) {
<?PHP //$Id$
class CourseBlock_course_list extends MoodleBlock {
- function CourseBlock_course_list ($course) {
+ function init() {
$this->title = get_string('courses');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004081200;
}
return true;
}
- function handle_config($config) {
- foreach ($config as $name => $value) {
- set_config($name, $value);
- }
- return true;
- }
-
function get_content() {
global $THEME, $CFG, $USER;
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
<?PHP //$Id$
class CourseBlock_course_summary extends MoodleBlock {
- function CourseBlock_course_summary ($course) {
- if(!empty($course) && $course->id == SITEID) { // Site level
- $this->title = get_string('frontpagedescription');
- } else {
- $this->title = get_string('blockname','block_course_summary');
- }
+ function init() {
+ $this->title = get_string('pagedescription', 'block_course_summary');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004052600;
}
+ function specialization() {
+ if($this->instance->pagetype == MOODLE_PAGE_COURSE && $this->instance->pageid != SITEID) {
+ $this->title = get_string('coursesummary', 'block_course_summary');
+ }
+ }
+
function get_content() {
global $CFG, $THEME;
return $this->content;
}
- if (empty($this->course)) {
+ if (empty($this->instance)) {
return '';
}
+ $course = get_record('course', 'id', $this->instance->pageid);
+
$this->content = New stdClass;
$options->noclean = true; // Don't clean Javascripts etc
- $this->content->text = format_text($this->course->summary, FORMAT_HTML, $options);
- if(isediting($this->course->id)) {
- if($this->course->id == SITEID) {
+ $this->content->text = format_text($course->summary, FORMAT_HTML, $options);
+ if(isediting($this->instance->pageid)) {
+ if($this->instance->pageid == SITEID) {
$editpage = $CFG->wwwroot.'/admin/site.php';
} else {
- $editpage = $CFG->wwwroot.'/course/edit.php?id='.$this->course->id;
+ $editpage = $CFG->wwwroot.'/course/edit.php?id='.$this->instance->pageid;
}
$this->content->text .= "<div align=\"right\"><a href=\"$editpage\"><img src=\"$CFG->pixpath/t/edit.gif\" alt=\"\" /></a></div>";
}
$result = true;
- if ($oldversion < 2004041000 and $result) {
+ if ($oldversion < 2004041000 && $result) {
$result = execute_sql("CREATE TABLE `{$CFG->prefix}blocks` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(40) NOT NULL default '',
COMMENT = 'To register and update all the available blocks'");
}
+ if ($oldversion < 2004101900 && $result) {
+ $result = execute_sql("CREATE TABLE `{$CFG->prefix}block` (
+ `id` int(10) unsigned NOT NULL auto_increment,
+ `name` varchar(40) NOT NULL default '',
+ `version` int(10) NOT NULL default '0',
+ `cron` int(10) unsigned NOT NULL default '0',
+ `lastcron` int(10) unsigned NOT NULL default '0',
+ `visible` tinyint(1) NOT NULL default '1',
+ `multiple` tinyint(1) NOT NULL default '0',
+ PRIMARY KEY (`id`)
+ )
+ COMMENT = 'To register and update all the available blocks'");
+
+ if(!$result) {
+ return false;
+ }
+
+ $records = get_records('blocks');
+ if(!empty($records)) {
+ foreach($records as $block) {
+ $block->multiple = 0;
+ insert_record('block', $block);
+ }
+ }
+
+ execute_sql("DROP TABLE `{$CFG->prefix}blocks`");
+
+ $result = execute_sql("CREATE TABLE `{$CFG->prefix}block_instance` (
+ `id` int(10) not null auto_increment,
+ `blockid` int(10) not null default '0',
+ `pageid` int(10) not null default '0',
+ `pagetype` enum('course') not null,
+ `position` enum('l', 'r') not null,
+ `weight` tinyint(3) not null default '0',
+ `visible` tinyint(1) not null default '0',
+ `configdata` text not null default '',
+
+ PRIMARY KEY(`id`),
+ INDEX pageid(`pageid`)
+ )");
+
+ if(!$result) {
+ return false;
+ }
+
+ $records = get_records('course');
+ if(!empty($records)) {
+ foreach($records as $thiscourse) {
+ // The @ suppresses a notice emitted if there is no : in the string
+ @list($left, $right) = split(':', $thiscourse->blockinfo);
+ if(!empty($left)) {
+ $arr = explode(',', $left);
+ foreach($arr as $weight => $blk) {
+ $instance = new stdClass;
+ $instance->blockid = abs($blk);
+ $instance->pageid = $thiscourse->id;
+ $instance->pagetype = MOODLE_PAGE_COURSE;
+ $instance->position = BLOCK_POS_LEFT;
+ $instance->weight = $weight;
+ $instance->visible = ($blk > 0) ? 1 : 0;
+ $instance->configdata = '';
+ insert_record('block_instance', $instance);
+ }
+ }
+ if(!empty($right)) {
+ $arr = explode(',', $right);
+ foreach($arr as $weight => $blk) {
+ $instance = new stdClass;
+ $instance->blockid = abs($blk);
+ $instance->pageid = $thiscourse->id;
+ $instance->pagetype = MOODLE_PAGE_COURSE;
+ $instance->position = BLOCK_POS_RIGHT;
+ $instance->weight = $weight;
+ $instance->visible = ($blk > 0) ? 1 : 0;
+ $instance->configdata = '';
+ insert_record('block_instance', $instance);
+ }
+ }
+ }
+ }
+
+ execute_sql("ALTER TABLE `{$CFG->prefix}course` DROP COLUMN blockinfo");
+ }
//Finally, return result
return $result;
# Table structure for table `blocks`
#
-CREATE TABLE `prefix_blocks` (
+CREATE TABLE `prefix_block` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(40) NOT NULL default '',
`version` int(10) NOT NULL default '0',
`cron` int(10) unsigned NOT NULL default '0',
`lastcron` int(10) unsigned NOT NULL default '0',
`visible` tinyint(1) NOT NULL default '1',
+ `multiple` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
+
+CREATE TABLE `prefix_block_instance` (
+ `id` int(10) not null auto_increment,
+ `blockid` int(10) not null default '0',
+ `pageid` int(10) not null default '0',
+ `pagetype` enum('course') not null,
+ `position` enum('l', 'r') not null,
+ `weight` tinyint(3) not null default '0',
+ `visible` tinyint(1) not null default '0',
+ `configdata` text not null default '',
+ PRIMARY KEY(`id`),
+ INDEX pageid(`pageid`)
+) TYPE=MyISAM;
# --------------------------------------------------------
}
+ if ($oldversion < 2004101900 && $result) {
+ $result = execute_sql("CREATE TABLE {$CFG->prefix}block (
+ id SERIAL8 PRIMARY KEY,
+ name varchar(40) NOT NULL default '',
+ version INT8 NOT NULL default '0',
+ cron INT8 unsigned NOT NULL default '0',
+ lastcron INT8 unsigned NOT NULL default '0',
+ visible int NOT NULL default '1',
+ multiple int NOT NULL default '0'
+ )
+ ");
+
+ if(!$result) {
+ return false;
+ }
+
+ $records = get_records('blocks');
+ if(!empty($records)) {
+ foreach($records as $block) {
+ $block->multiple = 0;
+ insert_record('block', $block);
+ }
+ }
+
+ execute_sql("DROP TABLE {$CFG->prefix}blocks");
+
+ $result = execute_sql("CREATE TABLE {$CFG->prefix}block_instance (
+ id SERIAL8 PRIMARY KEY,
+ blockid INT8 not null default '0',
+ pageid INT8 not null default '0',
+ pagetype enum('course') not null,
+ position enum('l', 'r') not null,
+ weight int not null default '0',
+ visible int not null default '0',
+ configdata text not null default ''
+ )");
+
+ if(!$result) {
+ return false;
+ }
+
+ $records = get_records('course');
+ if(!empty($records)) {
+ foreach($records as $thiscourse) {
+ // The @ suppresses a notice emitted if there is no : in the string
+ @list($left, $right) = split(':', $thiscourse->blockinfo);
+ if(!empty($left)) {
+ $arr = explode(',', $left);
+ foreach($arr as $weight => $blk) {
+ $instance = new stdClass;
+ $instance->blockid = abs($blk);
+ $instance->pageid = $thiscourse->id;
+ $instance->pagetype = MOODLE_PAGE_COURSE;
+ $instance->position = BLOCK_POS_LEFT;
+ $instance->weight = $weight;
+ $instance->visible = ($blk > 0) ? 1 : 0;
+ $instance->configdata = '';
+ insert_record('block_instance', $instance);
+ }
+ }
+ if(!empty($right)) {
+ $arr = explode(',', $right);
+ foreach($arr as $weight => $blk) {
+ $instance = new stdClass;
+ $instance->blockid = abs($blk);
+ $instance->pageid = $thiscourse->id;
+ $instance->pagetype = MOODLE_PAGE_COURSE;
+ $instance->position = BLOCK_POS_RIGHT;
+ $instance->weight = $weight;
+ $instance->visible = ($blk > 0) ? 1 : 0;
+ $instance->configdata = '';
+ insert_record('block_instance', $instance);
+ }
+ }
+ }
+ }
+
+ execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN blockinfo");
+ }
//Finally, return result
return $result;
version INT8 NOT NULL default '0',
cron INT8 NOT NULL default '0',
lastcron INT8 NOT NULL default '0',
- visible int NOT NULL default '1'
+ visible int NOT NULL default '1',
+ multiple int NOT NULL default '1'
) ;
+
+CREATE TABLE prefix_block_instance (
+ id SERIAL8 PRIMARY KEY,
+ blockid INT8 not null default '0',
+ pageid INT8 not null default '0',
+ pagetype enum('course') not null,
+ position enum('l', 'r') not null,
+ weight int not null default '0',
+ visible int not null default '0',
+ configdata text not null default ''
+) ;
+
# --------------------------------------------------------
<?PHP //$Id$
class CourseBlock_login extends MoodleBlock {
- function CourseBlock_login ($course) {
+ function init() {
$this->title = get_string('login');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004081600;
}
class MoodleBlock {
var $str;
- var $title = NULL;
- var $course = NULL;
- var $content_type = NULL;
- var $content = NULL;
+ var $title = NULL;
+ var $course = NULL;
+ var $content_type = NULL;
+ var $content = NULL;
var $edit_controls = NULL;
- var $version = NULL;
+ var $version = NULL;
+ var $instance = NULL;
+ var $config = NULL;
+
+ function MoodleBlock() {
+ $this->init();
+ }
function name() {
// Returns the block name, as present in the class name,
print_side_block($title, ' ', NULL, NULL, '');
}
- function add_edit_controls($options, $blockid) {
+ function add_edit_controls($options) {
global $CFG, $THEME, $USER;
- // The block may be disabled
- $blockid = intval($blockid);
- $enabled = $blockid > 0;
- $blockid = abs($blockid);
-
if (!isset($this->str)) {
$this->str->delete = get_string('delete');
$this->str->moveup = get_string('moveup');
$this->str->moveleft = get_string('moveleft');
$this->str->hide = get_string('hide');
$this->str->show = get_string('show');
+ $this->str->configure = get_string('configuration');
}
$path = $CFG->wwwroot.'/course';
$movebuttons = '<div style="float: right;">';
- if($enabled) {
+ if($this->instance->visible) {
$icon = '/t/hide.gif';
$title = $this->str->hide;
}
$title = $this->str->show;
}
- $movebuttons .= '<a style="margin-right: 6px; margin-left: 3px;" title="'.$title.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=toggle&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.$icon.'" alt="" /></a>';
+ $page = new stdClass;
+ $page->id = $this->instance->pageid;
+ $page->type = $this->instance->pagetype;
+ $script = page_source_script($page);
+
+ $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) {
+ $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>';
+ }
- $movebuttons .= '<a title="'.$this->str->delete.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=delete&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.'/t/delete.gif" alt="" /></a> ';
+ $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->delete.'" href="'.$script.'&blockaction=delete&instanceid='.$this->instance->id.$sesskeystr.'">' .
+ '<img src="'.$pixpath.'/t/delete.gif" alt=\"\" /></a> ';
if ($options & BLOCK_MOVE_LEFT) {
- $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveleft.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=moveside&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.'/t/left.gif" alt="" /></a>';
+ $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveleft.'" href="'.$script.'&blockaction=moveleft&instanceid='.$this->instance->id.$sesskeystr.'">' .
+ '<img src="'.$pixpath.'/t/left.gif" alt=\"\" /></a>';
}
if ($options & BLOCK_MOVE_UP) {
- $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveup.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=moveup&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.'/t/up.gif" alt="" /></a>';
+ $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveup.'" href="'.$script.'&blockaction=moveup&instanceid='.$this->instance->id.$sesskeystr.'">' .
+ '<img src="'.$pixpath.'/t/up.gif" alt=\"\" /></a>';
}
if ($options & BLOCK_MOVE_DOWN) {
- $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->movedown.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=movedown&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.'/t/down.gif" alt="" /></a>';
+ $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->movedown.'" href="'.$script.'&blockaction=movedown&instanceid='.$this->instance->id.$sesskeystr.'">' .
+ '<img src="'.$pixpath.'/t/down.gif" alt=\"\" /></a>';
}
if ($options & BLOCK_MOVE_RIGHT) {
- $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveright.'" href="'.$path.'/view.php?id='.$this->course->id.'&blockaction=moveside&blockid='.$blockid.$sesskeystr.'">' .
- '<img src="'.$pixpath.'/t/right.gif" alt="" /></a>';
+ $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveright.'" href="'.$script.'&blockaction=moveright&instanceid='.$this->instance->id.$sesskeystr.'">' .
+ '<img src="'.$pixpath.'/t/right.gif" alt=\"\" /></a>';
}
$movebuttons .= '</div>';
return false;
}
function print_config() {
- // This does nothing, it's here to prevent errors from
- // derived classes if they implement has_config() but not 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()) {
+ return false;
+ }
+ global $CFG, $USER, $THEME;
+ print_simple_box_start('center', '', $THEME->cellheading);
+ include($CFG->dirroot.'/blocks/'.$this->name().'/config_global.html');
+ print_simple_box_end();
+ return true;
}
- function handle_config() {
- // This does nothing, it's here to prevent errors from
- // derived classes if they implement has_config() but not handle_config()
+ 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()) {
+ return false;
+ }
+ foreach ($config as $name => $value) {
+ set_config($name, $value);
+ }
+ return true;
}
function applicable_formats() {
// Default case: the block can be used in all course types
// Default case: just an id for the block, with our name in it
return array('id' => 'block_'.$this->name());
}
+ function load_instance($instance) {
+ if(!empty($instance->configdata)) {
+ $this->config = unserialize(base64_decode($instance->configdata));
+ }
+ unset($instance->configdata);
+ $this->instance = $instance;
+ $this->specialization();
+ }
+ function specialization() {
+ // Just to make sure that this method exists.
+ return;
+ }
+
+ 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;
+ }
+ 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()) {
+ return false;
+ }
+ global $CFG, $USER, $THEME;
+
+ 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();
+ }
+ else {
+ notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
+ }
+
+ return true;
+ }
+ function instance_config_save($data) {
+ $data = stripslashes_recursive($data);
+ $this->config = $data;
+ return set_field('block_instance', 'configdata', base64_encode(serialize($data)), 'id', $this->instance->id);
+ }
+
}
class MoodleBlock_Nuke extends MoodleBlock {
<?PHP //$Id$
class CourseBlock_news_items extends MoodleBlock {
- function CourseBlock_news_items ($course) {
+ function init() {
$this->title = get_string('latestnews');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004052600;
}
return $this->content;
}
- if (empty($this->course)) {
- $this->content = '';
+ $this->content = new stdClass;
+ $this->content->text = '';
+ $this->content->footer = '';
+
+ if (empty($this->instance)) {
return $this->content;
}
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
- $this->content = New object;
- $this->content->text = '';
- $this->content->footer = '';
+ $course = get_record('course', 'id', $this->instance->pageid);
- if ($this->course->newsitems) {
- $news = forum_get_course_forum($this->course->id, 'news');
+ if ($course->newsitems) {
+ $news = forum_get_course_forum($this->instance->pageid, 'news');
// Slightly hacky way to do it but...
ob_start();
- forum_print_latest_discussions($news->id, $this->course->newsitems, "minimal", "", get_current_group($this->course->id));
+ forum_print_latest_discussions($news->id, $course->newsitems, "minimal", "", get_current_group($this->instance->pageid));
$this->content->text = ob_get_contents();
ob_end_clean();
}
<?PHP //$Id$
class CourseBlock_online_users extends MoodleBlock {
- function CourseBlock_online_users ($course) {
+ function init() {
$this->title = get_string('blockname','block_online_users');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004052700;
}
return $this->content;
}
- $this->content = New stdClass;
+ $this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
- if (empty($this->course)) {
+ if (empty($this->instance)) {
return $this->content;
}
+ $course = get_record('course', 'id', $this->instance->pageid);
+
$timetoshowusers = 300; //Seconds default
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$timefrom = time()-$timetoshowusers;
//Calculate if we are in separate groups
- $isseparategroups = ($this->course->groupmode == SEPARATEGROUPS and $this->course->groupmodeforce and
- !isteacheredit($this->course->id));
+ $isseparategroups = ($course->groupmode == SEPARATEGROUPS && $course->groupmodeforce && !isteacheredit($this->instance->pageid));
//Get the user current group
- $currentgroup = $isseparategroups ? get_current_group($this->course->id) : NULL;
+ $currentgroup = $isseparategroups ? get_current_group($this->instance->pageid) : NULL;
$groupmembers = "";
$groupselect = "";
$groupselect .= " AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
}
- if (empty($this->course->category)) { // Site-level
+ if ($this->instance->pageid == SITEID) { // Site-level
$courseselect = '';
$timeselect = "AND (s.timeaccess > $timefrom OR u.lastaccess > $timefrom)";
} else {
- $courseselect = "AND s.course = '".$this->course->id."'";
+ $courseselect = "AND s.course = '".$this->instance->pageid."'";
$timeselect = "AND s.timeaccess > $timefrom";
}
}
}
- if (!$this->course->category and $CFG->allusersaresitestudents) {
+ if ($this->instance->pageid == SITEID && $CFG->allusersaresitestudents) {
if ($siteusers = get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.picture, u.lastaccess
FROM {$CFG->prefix}user u
WHERE u.lastaccess > $timefrom AND u.username <> 'guest'
}
$this->content->text .= $imgtag;
}
- $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.$this->course->id.'" title="'.$timeago.'">'.$user->fullname.'</a></div>';
+ $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.$this->instance->pageid.'" title="'.$timeago.'">'.$user->fullname.'</a></div>';
}
/*
$table->align = array("right","left");
<?PHP //$Id$
class CourseBlock_participants extends MoodleBlock {
- function CourseBlock_participants ($course) {
+ function init() {
$this->title = get_string('people');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004052600;
}
if ($this->content !== NULL) {
return $this->content;
}
- if (empty($this->course)) {
+ if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
$strgroups = get_string('groups');
$strgroupmy = get_string('groupmy');
- if ($this->course->category or $CFG->showsiteparticipantslist > 1 or ($CFG->showsiteparticipantslist == 1 and isteacher()) or isteacher(SITEID)) {
- $this->content->items[]='<a title="'.get_string('listofallpeople').'" href="'.$CFG->wwwroot.'/user/index.php?id='.$this->course->id.'">'.get_string('participants').'</a>';
+ $course = get_record('course', 'id', $this->instance->pageid);
+
+ if ($this->instance->pageid != SITEID || $CFG->showsiteparticipantslist > 1 || ($CFG->showsiteparticipantslist == 1 && isteacher()) || isteacher(SITEID)) {
+ $this->content->items[]='<a title="'.get_string('listofallpeople').'" href="'.$CFG->wwwroot.'/user/index.php?id='.$this->instance->pageid.'">'.get_string('participants').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
}
- if ($this->course->groupmode or !$this->course->groupmodeforce) {
- if ($this->course->groupmode == VISIBLEGROUPS or isteacheredit($this->course->id)) {
- $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/course/groups.php?id='.$this->course->id.'">'.$strgroups.'</a>';
+ if ($course->groupmode || !$course->groupmodeforce) {
+ if ($course->groupmode == VISIBLEGROUPS or isteacheredit($this->instance->pageid)) {
+ $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/course/groups.php?id='.$this->instance->pageid.'">'.$strgroups.'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" height="16" width="16" alt="" />';
- } else if ($this->course->groupmode == SEPARATEGROUPS and $this->course->groupmodeforce) {
+ } else if ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce) {
// Show nothing
- } else if ($currentgroup = get_current_group($this->course->id)) {
- $this->content->items[]='<a title="'.$strgroupmy.'" href="'.$CFG->wwwroot.'/course/group.php?id='.$this->course->id.'">'.$strgroupmy.'</a>';
+ } else if ($currentgroup = get_current_group($this->instance->pageid)) {
+ $this->content->items[]='<a title="'.$strgroupmy.'" href="'.$CFG->wwwroot.'/course/group.php?id='.$this->instance->pageid.'">'.$strgroupmy.'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" height="16" width="16" alt="" />';
}
}
if (!empty($USER->id)) {
$fullname = fullname($USER, true);
- $editmyprofile = '<a title="'.$fullname.'" href="'.$CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&course='.$this->course->id.'">'.get_string('editmyprofile').'</a>';
+ $editmyprofile = '<a title="'.$fullname.'" href="'.$CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&course='.$this->instance->pageid.'">'.get_string('editmyprofile').'</a>';
if ($USER->description) {
$this->content->items[]= $editmyprofile;
} else {
<?PHP //$Id$
class CourseBlock_recent_activity extends MoodleBlock {
- function CourseBlock_recent_activity ($course) {
+ function init() {
$this->title = get_string('recentactivity');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004042900;
}
return $this->content;
}
- if (empty($this->course)) {
+ if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
+ $course = get_record('course', 'id', $this->instance->pageid);
+
// Slightly hacky way to do it but...
ob_start();
- print_recent_activity($this->course);
+ print_recent_activity($course);
$this->content->text = ob_get_contents();
ob_end_clean();
<?PHP //$Id$
class CourseBlock_search_forums extends MoodleBlock {
- function CourseBlock_search_forums ($course) {
+ function init() {
$this->title = get_string('search', 'forum');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004041000;
}
return $this->content;
}
- if (empty($this->course)) {
+ if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
- $this->content = New object;
+ $course = get_record('course', 'id', $this->instance->pageid);
+
+ $this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
- $form = forum_print_search_form($this->course, '', true);
+ $form = forum_print_search_form($course, '', true);
$this->content->text = '<div align="center">'.$form.'</div>';
return $this->content;
class CourseBlock_section_links extends MoodleBlock {
- function CourseBlock_section_links ($course) {
+ function init() {
+ $this->title = get_string('blockname', 'block_section_links');
+ $this->content_type = BLOCK_TYPE_TEXT;
+ $this->version = 2004052800;
+ }
+
+ function instance_config($instance) {
+ parent::instance_config($instance);
+ $course = get_record('course', 'id', $this->instance->pageid);
if (isset($course->format)) {
if ($course->format == 'topics') {
$this->title = get_string('topics', 'block_section_links');
} else {
$this->title = get_string('blockname', 'block_section_links');
}
- } else {
- $this->title = get_string('blockname', 'block_section_links');
}
- $this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
- $this->version = 2004052800;
}
function applicable_formats() {
return $this->content;
}
- if (empty($this->course)) {
- $this->content = '';
+ $this->content = New stdClass;
+ $this->content->footer = '';
+ $this->content->text = '';
+
+ if (empty($this->instance)) {
return $this->content;
}
- if ($this->course->format == 'weeks') {
- $highlight = ceil((time()-$this->course->startdate)/604800);
+ $course = get_record('course', 'id', $this->instance->pageid);
+
+ if ($course->format == 'weeks') {
+ $highlight = ceil((time()-$course->startdate)/604800);
$linktext = get_string('jumptocurrentweek', 'block_section_links');
$sectionname = 'week';
}
- else if ($this->course->format == 'topics') {
- $highlight = $this->course->marker;
+ else if ($course->format == 'topics') {
+ $highlight = $course->marker;
$linktext = get_string('jumptocurrenttopic', 'block_section_links');
$sectionname = 'topic';
}
$inc = 1;
- if ($this->course->numsections > 22) {
+ if ($course->numsections > 22) {
$inc = 2;
}
- if ($this->course->numsections > 40) {
+ if ($course->numsections > 40) {
$inc = 5;
}
- $courseid = $this->course->id;
+
if (!empty($USER->id)) {
- $display = get_field('course_display', 'display', 'course', $courseid, 'userid', $USER->id);
+ $display = get_field('course_display', 'display', 'course', $this->instance->pageid, 'userid', $USER->id);
}
if (!empty($display)) {
- $link = "$CFG->wwwroot/course/view.php?id=$courseid&$sectionname=";
+ $link = "$CFG->wwwroot/course/view.php?id=$this->instance->pageid&$sectionname=";
} else {
$link = '#';
}
$text = '<font size="-1">';
- for ($i = $inc; $i <= $this->course->numsections; $i += $inc) {
- $isvisible = get_field('course_sections', 'visible', 'course', $this->course->id, 'section', $i);
- if (!$isvisible and !isteacher($this->course->id)) {
+ for ($i = $inc; $i <= $course->numsections; $i += $inc) {
+ $isvisible = get_field('course_sections', 'visible', 'course', $this->instance->pageid, 'section', $i);
+ if (!$isvisible and !isteacher($this->instance->pageid)) {
continue;
}
$style = ($isvisible) ? '' : ' class="dimmed"';
}
}
if ($highlight) {
- $isvisible = get_field('course_sections', 'visible', 'course', $this->course->id, 'section', $highlight);
- if ($isvisible or isteacher($this->course->id)) {
+ $isvisible = get_field('course_sections', 'visible', 'course', $this->instance->pageid, 'section', $highlight);
+ if ($isvisible or isteacher($this->instance->pageid)) {
$style = ($isvisible) ? '' : ' class="dimmed"';
$text .= "<br /><a href=\"$link$highlight\"$style>$linktext</a>";
}
<?php //$Id$
class CourseBlock_site_main_menu extends MoodleBlock {
- function CourseBlock_site_main_menu ($course) {
+ function init() {
$this->title = get_string('mainmenu');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004052700;
}
return $this->content;
}
- if (empty($this->course)) {
- return '';
- }
-
$this->content = New stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
- $isteacher = isteacher($this->course->id);
- $isediting = isediting($this->course->id);
- $ismoving = ismoving($this->course->id);
+ if (empty($this->instance)) {
+ return $this->content;
+ }
+
+ $course = get_record('course', 'id', $this->instance->pageid);
+
+ $isteacher = isteacher($this->instance->pageid);
+ $isediting = isediting($this->instance->pageid);
+ $ismoving = ismoving($this->instance->pageid);
- $sections = get_all_sections($this->course->id);
+ $sections = get_all_sections($this->instance->pageid);
$section = $sections[0];
if($section->sequence || $isediting) {
- get_all_mods($this->course->id, $mods, $modnames, $modnamesplural, $modnamesused);
+ get_all_mods($this->instance->pageid, $mods, $modnames, $modnamesplural, $modnamesused);
}
- $groupbuttons = $this->course->groupmode;
- $groupbuttonslink = (!$this->course->groupmodeforce);
+ $groupbuttons = $course->groupmode;
+ $groupbuttonslink = (!$course->groupmodeforce);
if ($ismoving) {
$strmovehere = get_string('movehere');
$stractivityclipboard = $USER->activitycopyname;
}
- $modinfo = unserialize($this->course->modinfo);
+ $modinfo = unserialize($course->modinfo);
$editbuttons = '';
if ($ismoving) {
if ($isediting && !$ismoving) {
if ($groupbuttons) {
if (! $mod->groupmodelink = $groupbuttonslink) {
- $mod->groupmode = $this->course->groupmode;
+ $mod->groupmode = $course->groupmode;
}
} else {
}
$instancename = urldecode($modinfo[$modnumber]->name);
if (!empty($CFG->filterall)) {
- $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->course->id);
+ $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->instance->pageid);
}
$linkcss = $mod->visible ? '' : ' class="dimmed" ';
if (!empty($modinfo[$modnumber]->extra)) {
if ($isediting && $modnames) {
$this->content->footer = '<div style="text-align: right;">'.
- print_section_add_menus($this->course, 0, $modnames, true, true).'</div>';
+ print_section_add_menus($course, 0, $modnames, true, true).'</div>';
} else {
$this->content->footer = '';
}
<?php //$Id$
class CourseBlock_social_activities extends MoodleBlock {
- function CourseBlock_social_activities ($course) {
+ function init(){
$this->title = get_string('blockname','block_social_activities');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004041800;
}
if ($this->content !== NULL) {
return $this->content;
}
- if (empty($this->course)) {
- return '';
- }
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
+ if (empty($this->instance)) {
+ return $this->content;
+ }
+
+ $course = get_record('course', 'id', $this->instance->pageid);
+
// To make our day, we start with an ugly hack
global $sections, $mods, $modnames;
$section = $sections[0];
// That wasn't so bad, was it?
- $groupbuttons = $this->course->groupmode;
- $groupbuttonslink = (!$this->course->groupmodeforce);
- $isteacher = isteacher($this->course->id);
- $isediting = isediting($this->course->id);
- $ismoving = ismoving($this->course->id);
+ $groupbuttons = $course->groupmode;
+ $groupbuttonslink = (!$course->groupmodeforce);
+ $isteacher = isteacher($this->instance->pageid);
+ $isediting = isediting($this->instance->pageid);
+ $ismoving = ismoving($this->instance->pageid);
if ($ismoving) {
$strmovehere = get_string('movehere');
$strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
$stractivityclipboard = $USER->activitycopyname;
}
- $modinfo = unserialize($this->course->modinfo);
+ $modinfo = unserialize($course->modinfo);
$editbuttons = '';
if ($ismoving) {
if ($isediting && !$ismoving) {
if ($groupbuttons) {
if (! $mod->groupmodelink = $groupbuttonslink) {
- $mod->groupmode = $this->course->groupmode;
+ $mod->groupmode = $course->groupmode;
}
} else {
}
$instancename = urldecode($modinfo[$modnumber]->name);
if (!empty($CFG->filterall)) {
- $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->course->id);
+ $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->instance->pageid);
}
$linkcss = $mod->visible ? '' : ' class="dimmed" ';
if (!empty($modinfo[$modnumber]->extra)) {
if ($isediting && $modnames) {
$this->content->footer = '<div style="text-align: right;">'.
- print_section_add_menus($this->course, 0, $modnames, true, true).'</div>';
+ print_section_add_menus($course, 0, $modnames, true, true).'</div>';
} else {
$this->content->footer = '';
}
// database (blocks_version) to determine whether upgrades should
// be performed (see db/backup_*.php)
-$blocks_version = 2004052700; // The current version is a date (YYYYMMDDXX)
+$blocks_version = 2004101900; // The current version is a date (YYYYMMDDXX)
if (!empty($course)) {
// Test for and remove blocks which aren't appropriate anymore
- $form->blockinfo = $course->blockinfo;
- block_remove_inappropriate_from_course($form);
+ $page = new stdClass;
+ $page->id = $course->id;
+ $page->type = MOODLE_PAGE_COURSE;
+
+ blocks_remove_inappropriate($page);
// Update with the new data
if (update_record("course", $form)) {
} else {
$form->timecreated = time();
- //Create blockinfo default content
- $form->blockinfo = blocks_get_default_blocks(null, blocks_get_config_default($form->format));
-
if ($newcourseid = insert_record("course", $form)) { // Set up new course
+
+ // Setup the blocks
+ $page = new stdClass;
+ $page->type = MOODLE_PAGE_COURSE;
+ $page->id = $newcourseid;
+ blocks_repopulate_page($page); // Return value not checked because you can always edit later
+
$section = NULL;
$section->course = $newcourseid; // Create a default section.
$section->section = 0;
define('BLOCK_R_MIN_WIDTH', 100);
define('BLOCK_R_MAX_WIDTH', 210);
- optional_variable($preferred_width_left, 0);
- optional_variable($preferred_width_right, 0);
+ optional_variable($preferred_width_left, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]));
+ optional_variable($preferred_width_right, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]));
$preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
$preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
$preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
echo '<table width="100%" border="0" cellspacing="5" cellpadding="5">';
echo '<tr>';
- if(block_have_active($leftblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
- print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
echo '</td>';
}
}
echo '</td>';
- if(block_have_active($rightblocks) || $editing) {
- echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
- print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
- if ($editing && !empty($missingblocks)) {
- block_print_blocks_admin($course, $missingblocks);
- }
- print_spacer(1, 120, true);
- echo '</td>';
+ // The right column
+ if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
+ echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
+ blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
+ if ($editing && !empty($missingblocks)) {
+ blocks_print_adminblock($page, $missingblocks);
+ }
+ echo '</td>';
}
echo '</tr>';
define('BLOCK_R_MIN_WIDTH', 100);
define('BLOCK_R_MAX_WIDTH', 210);
- optional_variable($preferred_width_left, 0);
- optional_variable($preferred_width_right, 0);
+ optional_variable($preferred_width_left, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]));
+ optional_variable($preferred_width_right, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]));
$preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
$preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
$preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
/// The left column ...
- if(block_have_active($leftblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
- print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
echo '</td>';
}
/// Start main column
- echo "<td width=\"*\">";
+ echo '<td valign="top">';
print_heading_block(get_string("topicoutline"), "100%", "outlineheadingblock");
print_spacer(8, 1, true);
echo "</td>";
// The right column
- if(block_have_active($rightblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
- print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
if ($editing && !empty($missingblocks)) {
- block_print_blocks_admin($course, $missingblocks);
+ blocks_print_adminblock($page, $missingblocks);
}
- print_spacer(1, 120, true);
echo '</td>';
}
define('BLOCK_R_MIN_WIDTH', 100);
define('BLOCK_R_MAX_WIDTH', 210);
- optional_variable($preferred_width_left, 0);
- optional_variable($preferred_width_right, 0);
+ optional_variable($preferred_width_left, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]));
+ optional_variable($preferred_width_right, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]));
$preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
$preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
$preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
/// The left column ...
- if(block_have_active($leftblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
- print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
echo '</td>';
}
/// Start main column
- echo "</td><td width=\"*\">";
+ echo '<td valign="top">';
print_heading_block(get_string("weeklyoutline"), "100%", "outlineheadingblock");
print_spacer(8, 1, true);
echo "</td>";
// The right column
- if(block_have_active($rightblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
- print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
if ($editing && !empty($missingblocks)) {
- block_print_blocks_admin($course, $missingblocks);
+ blocks_print_adminblock($page, $missingblocks);
}
- print_spacer(1, 120, true);
- echo '</td>';
+ echo '</td>';
}
echo "</tr>\n";
optional_variable($id);
optional_variable($name);
+ optional_param('blockaction');
+ optional_param('instanceid', 0, PARAM_INT);
+ optional_param('blockid', 0, PARAM_INT);
+
if (!$id and !$name) {
error("Must specify course id or short name");
}
$course->format = 'weeks'; // Default format is weeks
}
- // Doing this now so we can pass the results to block_action()
- // and dodge the overhead of doing the same work twice.
-
- $blocks = $course->blockinfo;
- $delimpos = strpos($blocks, ':');
-
- if($delimpos === false) {
- // No ':' found, we have all left blocks
- $leftblocks = explode(',', $blocks);
- $rightblocks = array();
- }
- else if($delimpos === 0) {
- // ':' at start of string, we have all right blocks
- $blocks = substr($blocks, 1);
- $leftblocks = array();
- $rightblocks = explode(',', $blocks);
- }
- else {
- // Both left and right blocks
- $leftpart = substr($blocks, 0, $delimpos);
- $rightpart = substr($blocks, $delimpos + 1);
- $leftblocks = explode(',', $leftpart);
- $rightblocks = explode(',', $rightpart);
- }
+ $page = new stdClass;
+ $page->id = $course->id;
+ $page->type = MOODLE_PAGE_COURSE;
+ $pageblocks = blocks_get_by_page($page);
+
if (!isset($USER->editing)) {
$USER->editing = false;
}
$editing = $USER->editing;
- if (isset($hide) and confirm_sesskey()) {
+ if (isset($hide) && confirm_sesskey()) {
set_section_visible($course->id, $hide, '0');
}
- if (isset($show) and confirm_sesskey()) {
+ if (isset($show) && confirm_sesskey()) {
set_section_visible($course->id, $show, '1');
}
- if (isset($_GET['blockaction']) and confirm_sesskey()) {
- if (isset($_GET['blockid'])) {
- block_action($course, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
+ if (!empty($blockaction) && confirm_sesskey()) {
+ if (!empty($blockid)) {
+ blocks_execute_action($page, $pageblocks, strtolower($blockaction), intval($blockid));
+
}
- }
-
- // This has to happen after block_action() has possibly updated the two arrays
- $allblocks = array_merge($leftblocks, $rightblocks);
-
- $missingblocks = array();
- $recblocks = get_records('blocks','visible','1');
-
- // Note down which blocks are going to get displayed
- blocks_used($allblocks, $recblocks);
-
- if($editing && $recblocks) {
- foreach($recblocks as $recblock) {
- // If it's not hidden or displayed right now...
- if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
- // And if it's applicable for display in this format...
- $formats = block_method_result($recblock->name, 'applicable_formats');
-
- if( isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all'])) {
- // Translation: if the course format is explicitly accepted/rejected, use
- // that setting. Otherwise, fallback to the 'all' format. The empty() test
- // uses the trick that empty() fails if 'all' is either !isset() or false.
-
- // Add it to the missing blocks
- $missingblocks[] = $recblock->id;
- }
- }
+ else if (!empty($instanceid)) {
+ $instance = blocks_find_instance($instanceid, $pageblocks);
+ blocks_execute_action($page, $pageblocks, strtolower($blockaction), $instance);
}
+ // This re-query could be eliminated by judicious programming in blocks_execute_action(),
+ // but I 'm not sure if it's worth the complexity increase...
+ $pageblocks = blocks_get_by_page($page);
}
+ $missingblocks = blocks_get_missing($page, $pageblocks);
+
if (!empty($section)) {
if (!empty($move) and confirm_sesskey()) {
if (!move_section($course, $section, $move)) {
}
} else {
$USER->editing = false;
-
- // Note down which blocks are going to get displayed
- $allblocks = array_merge($leftblocks, $rightblocks);
- $recblocks = get_records('blocks','visible','1');
- blocks_used($allblocks, $recblocks);
}
$SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
}
}
- // If the block width cache is not set, set it
- if(!isset($SESSION->blockcache->width->{$course->id}) || $editing) {
-
- // This query might be optimized away if we 're in editing mode
- if(!isset($recblocks)) {
- $recblocks = get_records('blocks','visible','1');
- }
- $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
- $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
-
- // This may be kind of organizational overkill, granted...
- // But is there any real need to simplify the structure?
- $SESSION->blockcache->width->{$course->id}->left = $preferred_width_left;
- $SESSION->blockcache->width->{$course->id}->right = $preferred_width_right;
- }
- else {
- $preferred_width_left = $SESSION->blockcache->width->{$course->id}->left;
- $preferred_width_right = $SESSION->blockcache->width->{$course->id}->right;
- }
-
require("$CFG->dirroot/course/format/$course->format/format.php"); // Include the actual course format
print_footer(NULL, $course);
require_once($CFG->dirroot.'/mod/resource/lib.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
+ optional_param('blockaction');
+ optional_param('instanceid', 0, PARAM_INT);
+ optional_param('blockid', 0, PARAM_INT);
+
if (! $site = get_site()) {
redirect("$CFG->wwwroot/$CFG->admin/index.php");
}
$editing = isediting($site->id);
- // Doing this now so we can pass the results to block_action()
- // and dodge the overhead of doing the same work twice.
-
- $blocks = $site->blockinfo;
- $delimpos = strpos($blocks, ':');
+ $page = new stdClass;
+ $page->id = SITEID;
+ $page->type = MOODLE_PAGE_COURSE;
- if($delimpos === false) {
- // No ':' found, we have all left blocks
- $leftblocks = explode(',', $blocks);
- $rightblocks = array();
- }
- else if($delimpos === 0) {
- // ':' at start of string, we have all right blocks
- $blocks = substr($blocks, 1);
- $leftblocks = array();
- $rightblocks = explode(',', $blocks);
- }
- else {
- // Both left and right blocks
- $leftpart = substr($blocks, 0, $delimpos);
- $rightpart = substr($blocks, $delimpos + 1);
- $leftblocks = explode(',', $leftpart);
- $rightblocks = explode(',', $rightpart);
- }
+ $pageblocks = blocks_get_by_page($page);
if($editing) {
- if (isset($_GET['blockaction'])) {
- if (isset($_GET['blockid'])) {
- block_action($site, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
+ if (!empty($blockaction) && confirm_sesskey()) {
+ if (!empty($blockid)) {
+ blocks_execute_action($page, $pageblocks, strtolower($blockaction), intval($blockid));
+
}
- }
-
- // This has to happen after block_action() has possibly updated the two arrays
- $allblocks = array_merge($leftblocks, $rightblocks);
-
- $missingblocks = array();
- $recblocks = get_records('blocks','visible','1');
-
- // Note down which blocks are going to get displayed
- blocks_used($allblocks, $recblocks);
-
- if($editing && $recblocks) {
- foreach($recblocks as $recblock) {
- // If it's not hidden or displayed right now...
- if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
- // And if it's applicable for display in this format...
- $formats = block_method_result($recblock->name, 'applicable_formats');
-
- if( isset($formats['site']) ? $formats['site'] : !empty($formats['all'])) {
- // Translation: if the 'site' format is explicitly accepted/rejected, use
- // that setting. Otherwise, fallback to the 'all' format. The empty() test
- // uses the trick that empty() fails if 'all' is either !isset() or false.
-
- // Add it to the missing blocks
- $missingblocks[] = $recblock->id;
- }
- }
+ else if (!empty($instanceid)) {
+ $instance = blocks_find_instance($instanceid, $pageblocks);
+ blocks_execute_action($page, $pageblocks, strtolower($blockaction), $instance);
}
+ // This re-query could be eliminated by judicious programming in blocks_execute_action(),
+ // but I 'm not sure if it's worth the complexity increase...
+ $pageblocks = blocks_get_by_page($page);
}
- }
- else {
- // Note down which blocks are going to get displayed
- $allblocks = array_merge($leftblocks, $rightblocks);
- $recblocks = get_records('blocks','visible','1');
- blocks_used($allblocks, $recblocks);
- }
-
- // If the block width cache is not set, set it
- if(!isset($SESSION) or !isset($SESSION->blockcache) or
- !isset($SESSION->blockcache->width->{$site->id}) or $editing) {
- // This query might be optimized away if we 're in editing mode
- if(!isset($recblocks)) {
- $recblocks = get_records('blocks','visible','1');
- }
- $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
- $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
- // This may be kind of organizational overkill, granted...
- // But is there any real need to simplify the structure?
- $SESSION->blockcache->width->{$site->id}->left = $preferred_width_left;
- $SESSION->blockcache->width->{$site->id}->right = $preferred_width_right;
- } else {
- $preferred_width_left = $SESSION->blockcache->width->{$site->id}->left;
- $preferred_width_right = $SESSION->blockcache->width->{$site->id}->right;
+ $missingblocks = blocks_get_missing($page, $pageblocks);
}
+ optional_variable($preferred_width_left, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]));
+ optional_variable($preferred_width_right, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]));
$preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
$preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
$preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
<tr>
<?PHP
- if(block_have_active($leftblocks) || $editing) {
+ if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
- print_course_blocks($site, $leftblocks, BLOCK_LEFT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
echo '</td>';
}
}
echo '</td>';
- if(block_have_active($rightblocks) || $editing || isadmin()) {
+
+
+ // The right column
+ if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing || isadmin()) {
echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
if (isadmin()) {
echo '<div align="center">'.update_course_icon($site->id).'</div>';
echo '<br />';
}
- print_course_blocks($site, $rightblocks, BLOCK_RIGHT);
+ blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
if ($editing && !empty($missingblocks)) {
- block_print_blocks_admin($site, $missingblocks);
+ blocks_print_adminblock($page, $missingblocks);
}
echo '</td>';
}
// admin.php - created with Moodle 1.2 development (2003111400)
+$string['blockinstances'] = 'Instances';
+$string['blockmultiple'] = 'Multiple';
+$string['change'] = 'change';
$string['cachetext'] = 'Text cache lifetime';
$string['filteruploadedfiles'] = 'Filter uploaded files';
$string['upgradelogs'] = 'For full functionality, your old logs need to be upgraded. <a href=\"$a\">More information</a>';
<?php // $Id$
-$string['blockname'] = 'Course Summary';
-$string['siteinfo'] = 'Site Information';
+$string['coursesummary'] = 'Course Summary';
+$string['pagedescription'] = 'Course/Site Description';
+
?>
$string['backupuserfileshelp'] = 'Choose whether user files (eg profile images) should be included in automated backups';
$string['backupusershelp'] = 'Select whether you want to include all the users in the server or only the needed users for each course';
$string['backupversion'] = 'Backup Version';
+$string['blockconfiga'] = 'Configuring a $a block';
+$string['blockconfigin'] = 'Course: Configuring a block in $a';
+$string['blockconfigbad'] = 'This block has not been implemented correctly and thus cannot provide a configuration interface.';
$string['blockdeleteconfirm'] = 'You are about to completely delete the block \'$a\'. This will completely delete everything in the database associated with this block. Are you SURE you want to continue?';
$string['blockdeletefiles'] = 'All data associated with the block \'$a->block\' has been deleted from the database. To complete the deletion (and prevent the block re-installing itself), you should now delete this directory from your server: $a->directory';
$string['blocks'] = 'Blocks';
//This library includes all the necessary stuff to use blocks in course pages
-define('BLOCK_LEFT', 11);
-define('BLOCK_RIGHT', 12);
define('BLOCK_MOVE_LEFT', 0x01);
define('BLOCK_MOVE_RIGHT', 0x02);
define('BLOCK_MOVE_UP', 0x04);
define('BLOCK_MOVE_DOWN', 0x08);
+define('BLOCK_CONFIGURE', 0x10);
-function block_remove_inappropriate_from_course(&$course) {
- $blocks = $course->blockinfo;
+define('MOODLE_PAGE_COURSE', 'course');
+define('BLOCK_POS_LEFT', 'l');
+define('BLOCK_POS_RIGHT', 'r');
- $delimpos = strpos($blocks, ':');
-
- if($delimpos === false) {
- // No ':' found, we have all left blocks
- $leftblocks = explode(',', $blocks);
- $rightblocks = array();
+function page_get_format($page) {
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ if($page->id == SITEID) {
+ return 'site';
+ }
+ else {
+ $course = get_record('course', 'id', $page->id);
+ return $course->format;
+ }
+ break;
}
- else if($delimpos === 0) {
- // ':' at start of string, we have all right blocks
- $blocks = substr($blocks, 1);
- $leftblocks = array();
- $rightblocks = explode(',', $blocks);
+ return NULL;
+}
+
+function blocks_get_missing($page, $pageblocks) {
+ $missingblocks = array();
+ $allblocks = blocks_get_record();
+
+ if(!empty($allblocks)) {
+ foreach($allblocks as $block) {
+ if($block->visible && (!blocks_find_block($block->id, $pageblocks) || $block->multiple)) {
+ // And if it's applicable for display in this format...
+ $formats = block_method_result($block->name, 'applicable_formats');
+ $pageformat = page_get_format($page);
+ if(isset($formats[$pageformat]) ? $formats[$pageformat] : !empty($formats['all'])) {
+ // Add it to the missing blocks
+ $missingblocks[] = $block->id;
+ }
+ }
+ }
}
- else {
- // Both left and right blocks
- $leftpart = substr($blocks, 0, $delimpos);
- $rightpart = substr($blocks, $delimpos + 1);
- $leftblocks = explode(',', $leftpart);
- $rightblocks = explode(',', $rightpart);
+ return $missingblocks;
+}
+
+function blocks_remove_inappropriate($page) {
+ $pageblocks = blocks_get_by_page($page);
+
+ if(empty($pageblocks)) {
+ return;
}
- $allblocks = get_records('blocks');
- if(!empty($leftblocks)) {
- foreach($leftblocks as $key => $id) {
- $positiveid = abs($id);
- $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats');
- if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) {
- unset($leftblocks[$key]);
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ $course = get_record('course', 'id', $page->id);
+ if($page->id == SITEID) {
+ $pageformat = 'site';
}
- }
+ else {
+ $pageformat = $course->format;
+ }
+ break;
+ default:
+ return;
+ break;
}
- if(!empty($rightblocks)) {
- foreach($rightblocks as $key => $id) {
- $positiveid = abs($id);
- $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats');
- if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) {
- unset($rightblocks[$key]);
+
+ foreach($pageblocks as $position) {
+ foreach($position as $instance) {
+ $block = blocks_get_record($instance->blockid);
+ $formats = block_method_result($block->name, 'applicable_formats');
+ if(! (isset($formats[$pageformat]) ? $formats[$pageformat] : !empty($formats['all']))) {
+ // Translation: if the course format is explicitly accepted/rejected, use
+ // that setting. Otherwise, fallback to the 'all' format. The empty() test
+ // uses the trick that empty() fails if 'all' is either !isset() or false.
+
+ blocks_delete_instance($instance);
}
}
}
+}
- $course->blockinfo = implode(',', $leftblocks);
- if(!empty($rightblocks)) {
- $course->blockinfo .= ':' . implode(',', $rightblocks);
- }
+function blocks_delete_instance($instance) {
+ global $CFG;
+
+ delete_records('block_instance', 'id', $instance->id);
+ // And now, decrement the weight of all blocks after this one
+ execute_sql('UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+ '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+ '\' AND weight > '.$instance->weight, false);
}
// Returns the case-sensitive name of the class' constructor function. This includes both
}
//This function creates a new object of the specified block class
-function block_instance($blockname, $argument) {
+function block_instance($blockname, $instance = NULL) {
if(!block_load_class($blockname)) {
return false;
}
$classname = 'CourseBlock_'.$blockname;
- return New $classname($argument);
+ $retval = New $classname;
+ if($instance !== NULL) {
+ $retval->load_instance($instance);
+ }
+ return $retval;
}
//This function loads the necessary class files for a block
return class_exists($classname);
}
-//This function determines if there is some active block in an array of blocks
-function block_have_active($array) {
- foreach($array as $blockid) {
- if($blockid > 0) {
- return true;
+function blocks_have_content($instances) {
+ foreach($instances as $instance) {
+ if(!$instance->visible) {
+ continue;
+ }
+ $record = blocks_get_record($instance->blockid);
+ $obj = block_instance($record->name, $instance);
+ $content = $obj->get_content();
+ $type = $obj->get_content_type();
+ switch($type) {
+ case BLOCK_TYPE_LIST:
+ if(!empty($content->items) || !empty($content->footer)) {
+ return true;
+ }
+ break;
+ case BLOCK_TYPE_TEXT:
+ case BLOCK_TYPE_NUKE:
+ if(!empty($content->text) || !empty($content->footer)) {
+ return true;
+ }
+ break;
}
}
+
return false;
}
//This function print the one side of blocks in course main page
-function print_course_blocks(&$course, $blocksarray, $side) {
- global $CFG;
-
- $isediting = isediting($course->id);
- $ismoving = ismoving($course->id);
- $isteacheredit = isteacheredit($course->id);
-
- if(!empty($blocksarray)) {
- // Include the base class
- @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
- if(!class_exists('moodleblock')) {
- error('Class MoodleBlock is not defined or file not found for /course/blocks/moodleblock.class.php');
- }
-
- $blockdata = get_records('blocks', 'visible', 1);
- if($blockdata !== false) {
+function blocks_print_group($page, $instances) {
+
+ if(empty($instances)) {
+ return;
+ }
- $lastblock = end($blocksarray);
- $firstblock = reset($blocksarray);
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ $isediting = isediting($page->id);
+ $ismoving = ismoving($page->id);
+ $isteacheredit = isteacheredit($page->id);
+ break;
+ }
- foreach($blocksarray as $blockid) {
- if(!isset($blockdata[abs($blockid)])) {
- // This block is hidden. Don't show it.
- continue;
- }
+ // Include the base class
+ @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
+ if(!class_exists('moodleblock')) {
+ error('Class MoodleBlock is not defined or file not found for /course/blocks/moodleblock.class.php');
+ }
- $blockname = $blockdata[abs($blockid)]->name;
- $block = block_instance($blockname, $course);
- if($block === false) {
- // Something went wrong
- continue;
- }
+ $maxweight = max(array_keys($instances));
- // There are various sanity checks commented out below
- // because the block detection code should have already done them long ago.
+ foreach($instances as $instance) {
+ $block = blocks_get_record($instance->blockid);
+ if(!$block->visible) {
+ // Disabled by the admin
+ continue;
+ }
- /*
- if(!is_subclass_of($block, 'MoodleBlock')) {
- // Error: you have to derive from MoodleBlock
- continue;
- }
+ $obj = block_instance($block->name, $instance);
- if($content === NULL || $title === NULL) {
- // Error: This shouldn't have happened
- continue;
- }
- */
- if ($isediting && !$ismoving && $isteacheredit) {
- $options = 0;
- $options |= BLOCK_MOVE_UP * ($blockid != $firstblock);
- $options |= BLOCK_MOVE_DOWN * ($blockid != $lastblock);
- $options |= ($side == BLOCK_LEFT) ? BLOCK_MOVE_RIGHT : BLOCK_MOVE_LEFT;
- $block->add_edit_controls($options, $blockid);
- }
+ if ($isediting && !$ismoving && $isteacheredit) {
+ $options = 0;
+ $options |= BLOCK_MOVE_UP * ($instance->weight != 0);
+ $options |= BLOCK_MOVE_DOWN * ($instance->weight != $maxweight);
+ $options |= BLOCK_MOVE_RIGHT * ($instance->position != BLOCK_POS_RIGHT);
+ $options |= BLOCK_MOVE_LEFT * ($instance->position != BLOCK_POS_LEFT);
+ $options |= BLOCK_CONFIGURE * ($block->multiple);
+ $obj->add_edit_controls($options);
+ }
- if($blockid < 0) {
- // We won't print this block...
- if($isediting) {
- // Unless we 're in editing mode, in which case we 'll print a 'shadow'
- $block->print_shadow();
- }
- continue;
- }
- // So simple...
- $block->print_block();
+ if(!$instance->visible) {
+ if($isediting) {
+ $obj->print_shadow();
}
}
+ else {
+ $obj->print_block();
+ }
}
}
//This iterates over an array of blocks and calculates the preferred width
-function blocks_preferred_width($blockarray, $blockinfos) {
+function blocks_preferred_width($instances) {
$width = 0;
- if(!is_array($blockarray) || empty($blockarray)) {
+ if(empty($instances) || !is_array($instances)) {
return 0;
}
- foreach($blockarray as $blockid) {
- if($blockid < 0) {
- // Invisible block
+ foreach($instances as $instance) {
+ if(!$instance->visible) {
continue;
}
- if(isset($blockinfos[$blockid])) {
- $blockname = $blockinfos[$blockid]->name;
- $pref = block_method_result($blockname, 'preferred_width');
- if($pref === NULL) {
- continue;
- }
- if($pref > $width) {
- $width = $pref;
- }
+ $block = blocks_get_record($instance->blockid);
+ $pref = block_method_result($block->name, 'preferred_width');
+ if($pref === NULL) {
+ continue;
+ }
+ if($pref > $width) {
+ $width = $pref;
}
}
return $width;
}
+function blocks_get_record($blockid = NULL, $invalidate = false) {
+ static $cache = NULL;
-// $course passed by reference for speed
-// $leftblocks, $rightblocks passed by reference because block_action() needs to
-// update the arrays so that the change can be shown immediately.
+ if($invalidate || empty($cache)) {
+ $cache = get_records('block');
+ }
-function block_action(&$course, &$leftblocks, &$rightblocks, $blockaction, $blockid) {
+ if($blockid === NULL) {
+ return $cache;
+ }
- $blockid = abs(intval($blockid)); // Just to make sure
+ return (isset($cache[$blockid])? $cache[$blockid] : false);
+}
+
+function blocks_find_block($blockid, $blocksarray) {
+ foreach($blocksarray as $blockgroup) {
+ foreach($blockgroup as $instance) {
+ if($instance->blockid == $blockid) {
+ return $instance;
+ }
+ }
+ }
+ return false;
+}
+
+function blocks_find_instance($instanceid, $blocksarray) {
+ foreach($blocksarray as $subarray) {
+ foreach($subarray as $instance) {
+ if($instance->id == $instanceid) {
+ return $instance;
+ }
+ }
+ }
+ return false;
+}
+
+function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid) {
+ global $CFG;
+
+ if(is_int($instanceorid)) {
+ $blockid = $instanceorid;
+ }
+ else if(is_object($instanceorid)) {
+ $instance = $instanceorid;
+ }
switch($blockaction) {
- case 'toggle':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- $leftblocks[$block->position] = -$leftblocks[$block->position];
+ case 'config':
+ // Series of ugly hacks following...
+ global $course, $USER; // First hack; we need $course to print out the headers
+ $block = blocks_get_record($instance->blockid);
+ $blockobject = block_instance($block->name, $instance);
+ if ($blockobject === false) {
+ continue;
+ }
+ optional_param('submitted', 0, PARAM_INT);
+
+ // Define the data we 're going to silently include in the instance config form here,
+ // so we can strip them from the submitted data BEFORE serializing it.
+ $hiddendata = array(
+ 'sesskey' => $USER->sesskey,
+ 'id' => $course->id,
+ 'instanceid' => $instance->id,
+ 'blockaction' => 'config'
+ );
+ // The 'id' thing is a crude hack in all its glory...
+ // Redirecting the form submission back to ourself with qualified_me() was a good idea since otherwise
+ // we 'd need to have an "extra" script that would have to infer where to redirect us back just from
+ // the data in $instance (pagetype and pageid). But, "ourself" is most likely course/view.php and it needs
+ // a course id. Hence the hack.
+
+ if($data = data_submitted()) {
+ $remove = array_keys($hiddendata);
+ foreach($remove as $item) {
+ unset($data->$item);
}
- else {
- $rightblocks[$block->position] = -$rightblocks[$block->position];
+ if(!$blockobject->instance_config_save($data)) {
+ error('Error saving block configuration');
}
+ // And nothing more, continue with displaying the page
}
- break;
- case 'delete':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- unset($leftblocks[$block->position]);
- }
- else {
- unset($rightblocks[$block->position]);
+ else {
+ $loggedinas = "<p class=\"logininfo\">".user_login_string($course, $USER)."</p>";
+ print_header(get_string('blockconfigin', 'moodle', $course->fullname), $course->fullname, $course->shortname,
+ "", "", true, update_course_icon($course->id), $loggedinas);
+ print_heading(get_string('blockconfiga', 'moodle', $block->name));
+ echo '<form method="post" action="'.strip_querystring(qualified_me()).'">'; // This I wouldn't call a hack but it sure looks cheeky
+ echo '<p>';
+ foreach($hiddendata as $name => $val) {
+ echo '<input type="hidden" name="'.$name.'" value="'.$val.'" />';
}
+ echo '</p>';
+ $blockobject->instance_config_print();
+ echo '</form>';
+ print_footer();
+ die(); // Do not go on with the other course-related stuff
}
break;
- case 'add':
- // Toggle to enabled, or add it if it doesn't exist at all
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block === false) {
- // It doesn't exist at all, so add it
- $rightblocks[] = $blockid;
+ case 'toggle':
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
}
- else if($block->enabled == false) {
- // Enable it
- if($block->side == BLOCK_LEFT) {
- $leftblocks[$block->position] = -$leftblocks[$block->position];
- }
- else {
- $rightblocks[$block->position] = -$rightblocks[$block->position];
- }
+ $instance->visible = ($instance->visible) ? 0 : 1;
+ update_record('block_instance', $instance);
+ break;
+ case 'delete':
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
}
+ blocks_delete_instance($instance);
break;
case 'moveup':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- if(isset($leftblocks[$block->position - 1])) {
- // We can move it upwards
- $oldblock = $leftblocks[$block->position - 1];
- $leftblocks[$block->position - 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
- $leftblocks[$block->position] = $oldblock;
- }
- }
- else {
- if(isset($rightblocks[$block->position - 1])) {
- // We can move it upwards
- $oldblock = $rightblocks[$block->position - 1];
- $rightblocks[$block->position - 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
- $rightblocks[$block->position] = $oldblock;
- }
- }
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
+ }
+ $other = $pageblocks[$instance->position][$instance->weight - 1];
+ if(!empty($other)) {
+ --$instance->weight;
+ ++$other->weight;
+ update_record('block_instance', $instance);
+ update_record('block_instance', $other);
}
break;
case 'movedown':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- if(isset($leftblocks[$block->position + 1])) {
- // We can move it downwards
- $oldblock = $leftblocks[$block->position + 1];
- $leftblocks[$block->position + 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
- $leftblocks[$block->position] = $oldblock;
- }
- }
- else {
- if(isset($rightblocks[$block->position + 1])) {
- // We can move it downwards
- $oldblock = $rightblocks[$block->position + 1];
- $rightblocks[$block->position + 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
- $rightblocks[$block->position] = $oldblock;
- }
- }
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
+ }
+ $other = $pageblocks[$instance->position][$instance->weight + 1];
+ if(!empty($other)) {
+ ++$instance->weight;
+ --$other->weight;
+ update_record('block_instance', $instance);
+ update_record('block_instance', $other);
}
+
break;
- case 'moveside':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- unset($leftblocks[$block->position]);
- $rightblocks[] = $block->enabled ? $blockid : -$blockid;
- }
- else {
- unset($rightblocks[$block->position]);
- $leftblocks[] = $block->enabled ? $blockid : -$blockid;
- }
+ case 'moveleft':
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
+ }
+ $sql = '';
+ switch($instance->position) {
+ case BLOCK_POS_RIGHT:
+ // To preserve the continuity of block weights
+ $sql = 'UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+ '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+ '\' AND weight > '.$instance->weight;
+
+ $instance->position = BLOCK_POS_LEFT;
+ $maxweight = max(array_keys($pageblocks[$instance->position]));
+ $instance->weight = $maxweight + 1;
+ break;
+ }
+ if($sql) {
+ update_record('block_instance', $instance);
+ execute_sql($sql, false);
}
break;
- }
+ case 'moveright':
+ if(empty($instance)) {
+ error('Invalid block instance for '.$blockaction);
+ }
+ $sql = '';
+ switch($instance->position) {
+ case BLOCK_POS_LEFT:
+ // To preserve the continuity of block weights
+ $sql = 'UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+ '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+ '\' AND weight > '.$instance->weight;
+
+ $instance->position = BLOCK_POS_RIGHT;
+ $maxweight = max(array_keys($pageblocks[$instance->position]));
+ $instance->weight = $maxweight + 1;
+ break;
+ }
+ if($sql) {
+ update_record('block_instance', $instance);
+ execute_sql($sql, false);
+ }
+ break;
+ case 'add':
+ // Add a new instance of this block, if allowed
+ $block = blocks_get_record($blockid);
- $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
- set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
+ if(!$block->visible) {
+ // Only allow adding if the block is enabled
+ return false;
+ }
+ $weight = get_record_sql('SELECT 1, max(weight) + 1 AS nextfree FROM '.$CFG->prefix.'block_instance WHERE pageid = '.$page->id.' AND pagetype = \''.$page->type.'\' AND position = \''.BLOCK_POS_RIGHT.'\'');
+
+ $newinstance = new stdClass;
+ $newinstance->blockid = $blockid;
+ $newinstance->pageid = $page->id;
+ $newinstance->pagetype = $page->type;
+ $newinstance->position = BLOCK_POS_RIGHT;
+ $newinstance->weight = $weight->nextfree;
+ $newinstance->visible = 1;
+ $newinstance->configdata = '';
+ insert_record('block_instance', $newinstance);
+ break;
+ }
}
-// Searches for the block with ID $blockid in one or more of the two
-// blocks arrays. If not found, returns boolean false. Otherwise,
-// returns an object $finding where:
-// $finding->side = BLOCK_LEFT or BLOCK_RIGHT
-// $finding->enabled = true or false
-// $finding->position = index of corresponding array where found
+function blocks_get_by_page($page) {
+ $blocks = get_records_select('block_instance', 'pageid = '.$page->id.' AND pagetype = \''.$page->type.'\'', 'position, weight');
-function block_find($blockid, $leftblocks, $rightblocks) {
+ $arr = array(BLOCK_POS_LEFT => array(), BLOCK_POS_RIGHT => array());
+ if(empty($blocks)) {
+ return $arr;
+ }
- if(($blockid = abs($blockid)) == 0) {
- return false;
+ foreach($blocks as $block) {
+ $arr[$block->position][$block->weight] = $block;
}
- $finding->side = BLOCK_LEFT;
- $finding->enabled = true;
- $finding->position = NULL;
+ return $arr;
+}
- // First, search for the "enabled" block, since that's what we
- // will be doing most of the time.
+//This function prints the block to admin blocks as necessary
+function blocks_print_adminblock($page, $missingblocks) {
+ global $USER;
- $key = array_search($blockid, $leftblocks);
- if($key !== false && $key !== NULL) {
- $finding->position = $key;
- return $finding;
- }
- $key = array_search($blockid, $rightblocks);
- if($key !== false && $key !== NULL) {
- $finding->position = $key;
- $finding->side = BLOCK_RIGHT;
- return $finding;
- }
+ $strblocks = get_string('blocks');
+ $stradd = get_string('add');
+ if (!empty($missingblocks)) {
+ foreach ($missingblocks as $blockid) {
+ $block = blocks_get_record($blockid);
+
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ $course = get_record('course', 'id', $page->id);
+ break;
+ default: die('unknown pagetype: '.$page->type);
+ }
- // "enabled" block not found. Now search for the disabled block.
- $finding->enabled = false;
- $blockid = -$blockid;
+ $blockobject = block_instance($block->name);
+ if ($blockobject === false) {
+ continue;
+ }
+ $menu[$block->id] = $blockobject->get_title();
+ }
- $key = array_search($blockid, $leftblocks);
- if($key !== false && $key !== NULL) {
- $finding->position = $key;
- return $finding;
- }
- $key = array_search($blockid, $rightblocks);
- if($key !== false && $key !== NULL) {
- $finding->position = $key;
- $finding->side = BLOCK_RIGHT;
- return $finding;
+ if($page->id == SITEID) {
+ $target = 'index.php';
+ }
+ else {
+ $target = 'view.php';
+ }
+ $content = popup_form($target.'?id='.$course->id.'&sesskey='.$USER->sesskey.'&blockaction=add&blockid=',
+ $menu, 'add_block', '', "$stradd...", '', '', true);
+ $content = '<div align="center">'.$content.'</div>';
+ print_side_block($strblocks, $content, NULL, NULL, NULL);
}
-
- // Nothing found :(
-
- return false;
}
-//This function prints the block to admin blocks as necessary
-function block_print_blocks_admin(&$course, $missingblocks) {
-
- global $USER;
+function blocks_repopulate_page($page) {
+ global $CFG;
- if (isediting($course->id)) {
- $strblocks = get_string('blocks');
- $stradd = get_string('add');
- if (!empty($missingblocks)) {
- $blockdata = get_records_list('blocks', 'id', implode(',', $missingblocks));
- if ($blockdata !== false) {
- foreach ($blockdata as $block) {
- $blockobject = block_instance($block->name, $course);
- if ($blockobject === false) {
- continue;
+ /// If the site override has been defined, it is the only valid one.
+ if (!empty($CFG->defaultblocks_override)) {
+ $blocknames = $CFG->defaultblocks_override;
+ }
+ /// Else, try to find out what page this is
+ else {
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ // Is it the site?
+ if($page->id == SITEID) {
+ if (!empty($CFG->defaultblocks_site)) {
+ $blocknames = $CFG->defaultblocks_site;
+ }
+ /// Failsafe - in case nothing was defined.
+ else {
+ $blocknames = 'site_main_menu,admin,course_list:course_summary,calendar_month';
}
- $menu[$block->id] = $blockobject->get_title();
- }
-
- if(!$course->category) {
- $target = 'index.php';
}
+ // It's a normal course, so do it accodring to the course format
else {
- $target = 'view.php';
+ $course = get_record('course', 'id', $page->id);
+ if (!empty($CFG->{'defaultblocks_'.$course->format})) {
+ $blocknames = $CFG->{'defaultblocks_'.$course->format};
+ }
+ else {
+ $format_config = $CFG->dirroot.'/course/format/'.$course->format.'/config.php';
+ if (@is_file($format_config) && is_readable($format_config)) {
+ require($format_config);
+ }
+ if (!empty($format['defaultblocks'])) {
+ $blocknames = $format['defaultblocks'];
+ }
+ else if (!empty($CFG->defaultblocks)){
+ $blocknames = $CFG->defaultblocks;
+ }
+ /// Failsafe - in case nothing was defined.
+ else {
+ $blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
+ }
+ }
}
- $content = popup_form($target.'?id='.$course->id.'&blockaction=add&sesskey='.$USER->sesskey.'&blockid=',
- $menu, 'add_block', '', "$stradd...", '', '', true);
- $content = '<div align="center">'.$content.'</div>';
- print_side_block($strblocks, $content, NULL, NULL, NULL);
- }
+ break;
+ default:
+ error('Invalid page type: '.$page->type);
+ break;
+ }
+ }
+
+ $allblocks = blocks_get_record();
+
+ if(empty($allblocks)) {
+ error('Could not retrieve blocks from the database');
+ }
+
+ // We have the blocks, make up two arrays
+ $left = '';
+ $right = '';
+ @list($left, $right) = explode(':', $blocknames);
+ $instances = array(BLOCK_POS_LEFT => explode(',', $left), BLOCK_POS_RIGHT => explode(',', $right));
+
+ // Arrays are fine, now we have to correlate block names to ids
+ $idforname = array();
+ foreach($allblocks as $block) {
+ $idforname[$block->name] = $block->id;
+ }
+
+ // Ready to start creating block instances, but first drop any existing ones
+ delete_records('block_instance', 'pageid', $page->id, 'pagetype', $page->type);
+
+ foreach($instances as $position => $blocknames) {
+ $weight = 0;
+ foreach($blocknames as $blockname) {
+ $newinstance = new stdClass;
+ $newinstance->blockid = $idforname[$blockname];
+ $newinstance->pageid = $page->id;
+ $newinstance->pagetype = $page->type;
+ $newinstance->position = $position;
+ $newinstance->weight = $weight;
+ $newinstance->visible = 1;
+ $newinstance->configdata = '';
+ insert_record('block_instance', $newinstance);
+ $weight++;
}
}
+
+ return true;
}
function upgrade_blocks_db($continueto) {
$notices = array();
//Count the number of blocks in db
- $blockcount = count_records("blocks");
+ $blockcount = count_records('block');
//If there isn't records. This is the first install, so I remember it
if ($blockcount == 0) {
$first_install = true;
$site = get_site();
- if (!$blocks = get_list_of_plugins("blocks", "db") ) {
+ if (!$blocks = get_list_of_plugins('blocks', 'db') ) {
error("No blocks installed!");
}
- include_once($CFG->dirroot."/blocks/moodleblock.class.php");
+ include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
if(!class_exists('moodleblock')) {
error('Class MoodleBlock is not defined or file not found for /blocks/moodleblock.class.php');
}
$fullblock = "$CFG->dirroot/blocks/$blockname";
- if ( is_readable($fullblock."/block_".$blockname.".php")) {
- include_once($fullblock."/block_".$blockname.".php");
+ if ( is_readable($fullblock.'/block_'.$blockname.'.php')) {
+ include_once($fullblock.'/block_'.$blockname.'.php');
} else {
$notices[] = "Block $blockname: ".$fullblock."/block_".$blockname.".php was not readable";
continue;
}
- if ( is_dir("$fullblock/db/")) {
- if ( is_readable("$fullblock/db/$CFG->dbtype.php")) {
+ if ( @is_dir("$fullblock/db/")) {
+ if ( @is_readable("$fullblock/db/$CFG->dbtype.php")) {
include_once("$fullblock/db/$CFG->dbtype.php"); # defines upgrading function
} else {
$notices[] ="Block $blockname: $fullblock/db/$CFG->dbtype.php was not readable";
continue;
}
- // Let's see if it supports some basic methods
+ // Here is the place to see if the block implements a constructor (old style),
+ // an init() function (new style) or nothing at all (error time).
+
$constructor = get_class_constructor($classname);
if(empty($constructor)) {
// No constructor
$invalidblocks[] = $blockname;
continue;
}
+ $methods = get_class_methods($classname);
+ if(!in_array('init', $methods)) {
+ // This is an old-style block
+ $notices[] = "Block $blockname is an old style block and needs to be updated by a programmer.";
+ $invalidblocks[] = $blockname;
+ continue;
+ }
- unset($block);
-
- $blockobj = New $classname($site);
+ $block = new stdClass; // This may be used to update the db below
+ $blockobj = new $classname; // This is what we 'll be testing
// Inherits from MoodleBlock?
if(!is_subclass_of($blockobj, 'moodleblock')) {
$block->name = $blockname; // The name MUST match the directory
$blocktitle = $blockobj->get_title();
- if ($currblock = get_record("blocks", "name", $block->name)) {
+ if ($currblock = get_record('block', 'name', $block->name)) {
if ($currblock->version == $block->version) {
// do nothing
} else if ($currblock->version < $block->version) {
notify("Upgrading block $block->name from $currblock->version to $block->version FAILED!");
}
else {
- // OK so far, now update the blocks record
+ // OK so far, now update the block record
$block->id = $currblock->id;
- if (! update_record('blocks', $block)) {
- error("Could not update block $block->name record in blocks table!");
+ if (! update_record('block', $block)) {
+ error("Could not update block $block->name record in block table!");
}
notify(get_string('blocksuccess', '', $blocktitle), 'green');
echo '<hr />';
} else { // block not installed yet, so install it
+ // If it allows multiples, start with it enabled
+ $block->multiple = $blockobj->instance_allow_multiple();
+
// [pj] Normally this would be inline in the if, but we need to
// check for NULL (necessary for 4.0.5 <= PHP < 4.2.0)
$conflictblock = array_search($blocktitle, $blocktitles);
if($conflictblock !== false && $conflictblock !== NULL) {
-
// Duplicate block titles are not allowed, they confuse people
// AND PHP's associative arrays ;)
error('<strong>Naming conflict</strong>: block <strong>'.$block->name.'</strong> has the same title with an existing block, <strong>'.$conflictblock.'</strong>!');
@set_time_limit(0); // To allow slow databases to complete the long SQL
if (!is_dir("$fullblock/db/") || modify_database("$fullblock/db/$CFG->dbtype.sql")) {
$db->debug = false;
- if ($block->id = insert_record('blocks', $block)) {
+ if ($block->id = insert_record('block', $block)) {
notify(get_string('blocksuccess', '', $blocktitle), 'green');
echo "<hr />";
} else {
}
}
- //Finally, if we are in the first_install, update every course blockinfo field with
- //default values.
+ // Finally, if we are in the first_install of BLOCKS (this means that we are
+ // upgrading from Moodle < 1.3), put blocks in all existing courses.
if ($first_install) {
//Iterate over each course
- if ($courses = get_records("course")) {
+ if ($courses = get_records('course')) {
foreach ($courses as $course) {
- //Depending of the format, insert some values
- $blockinfo = blocks_get_default_blocks ($course->id,
- blocks_get_config_default($course->format));
- if ($CFG->debug) {
- echo 'Updating blockinfo for course: '.$course->shortname.'('.$blockinfo.')<br />';
- }
+ $page = new stdClass;
+ $page->type = MOODLE_PAGE_COURSE;
+ $page->id = $course->id;
+ blocks_repopulate_page($page);
}
}
}
if (!empty($CFG->siteblocksadded)) { /// This is a once-off hack to make a proper upgrade
- blocks_get_default_blocks(SITEID, blocks_get_config_default()); // Add blockinfo to the site course
+ $page = new stdClass;
+ $page->type = MOODLE_PAGE_COURSE;
+ $page->id = SITEID;
+ blocks_repopulate_page($page);
delete_records('config', 'name', 'siteblocksadded');
}
}
}
-//This function returns the number of courses currently using the block
-function blocks_get_courses_using_block_by_id($blockid) {
-
- $num = 0;
-
- if ($courses = get_records("course")) {
- foreach($courses as $course) {
- $blocks = str_replace(":",",",$course->blockinfo);
- $blocksarr = explode(",",$blocks);
- if (block_find($blockid,$blocksarr,array())) {
- $num++;
- }
- }
- }
-
- return $num;
-}
-
-//This function hides a block in all courses using it
-function blocks_update_every_block_by_id($blockid,$action) {
-
- if ($courses = get_records("course")) {
- foreach($courses as $course) {
- //Calculate left and right blocks
- $blocks = $course->blockinfo;
- $delimpos = strpos($blocks, ':');
-
- if($delimpos === false) {
- // No ':' found, we have all left blocks
- $leftblocks = explode(',', $blocks);
- $rightblocks = array();
- } else if($delimpos === 0) {
- // ':' at start of string, we have all right blocks
- $blocks = substr($blocks, 1);
- $leftblocks = array();
- $rightblocks = explode(',', $blocks);
- }
- else {
- // Both left and right blocks
- $leftpart = substr($blocks, 0, $delimpos);
- $rightpart = substr($blocks, $delimpos + 1);
- $leftblocks = explode(',', $leftpart);
- $rightblocks = explode(',', $rightpart);
- }
-
- switch($action) {
- case 'show':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- $leftblocks[$block->position] = abs($leftblocks[$block->position]);
- }
- else {
- $rightblocks[$block->position] = abs($rightblocks[$block->position]);
- }
- }
- break;
- case 'hide':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- $leftblocks[$block->position] = -abs($leftblocks[$block->position]);
- }
- else {
- $rightblocks[$block->position] = -abs($rightblocks[$block->position]);
- }
- }
- break;
- case 'delete':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- unset($leftblocks[$block->position]);
- }
- else {
- unset($rightblocks[$block->position]);
- }
- }
- break;
- }
- $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
- set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
- }
- }
-}
-
-// [pj] I didn't like the block_get_X_by_Y() functions because
-// we should be able to do without them with clever coding,
-// so I set out to see if they could be removed somehow.
-// Only block_get_default_blocks() depends on them, and that
-// one is used nowhere at the moment. So I 'm commenting
-// them out until a use IS found.
-// [el] Uncommented to be used in the installation process, when
-// inserting new courses and when restoring courses. Perhaps
-// they can be modified, but previously related processes
-// will use them since now.
-
//This function returns the id of the block, searching it by name
function block_get_id_by_name ($blockname) {
- if ($block = get_record("blocks","name",$blockname)) {
+ if ($block = get_record('block','name',$blockname)) {
return $block->id;
} else {
return 0;
//This function returns the name of the block, searching it by id
function block_get_name_by_id ($blockid) {
- if ($block = get_record("blocks","id",$blockid)) {
+ if ($block = get_record('block','id',$blockid)) {
return $block->name;
} else {
return NULL;
}
}
- //Calculate left and right blocks
- $blocksn = $blocknames;
- $delimpos = strpos($blocksn, ':');
-
- if($delimpos === false) {
- // No ':' found, we have all left blocks
- $leftblocksn = explode(',', $blocksn);
- $rightblocksn = array();
- } else if($delimpos === 0) {
- // ':' at start of string, we have all right blocks
- $blocksn = substr($blocksn, 1);
- $leftblocksn = array();
- $rightblocksn = explode(',', $blocksn);
- }
- else {
- // Both left and right blocks
- $leftpartn = substr($blocksn, 0, $delimpos);
- $rightpartn = substr($blocksn, $delimpos + 1);
- $leftblocksn = explode(',', $leftpartn);
- $rightblocksn = explode(',', $rightpartn);
- }
-
- //Now I have blocks separated
-
- $leftblocks = array();
- $rightblocks = array();
-
- if ($leftblocksn) {
- foreach($leftblocksn as $leftblockn) {
- //Convert blockname to id
- $leftblock = block_get_id_by_name(str_replace("-","",$leftblockn));
- if ($leftblock) {
- //Check it's visible
- if($block = get_record("blocks","id",$leftblock,"visible","1")) {
- //Check if the module was hidden at course level
- if (substr($leftblockn,0,1) == "-") {
- $leftblocks[] = -$leftblock;
- } else {
- $leftblocks[] = $leftblock;
- }
- }
- }
- }
- }
-
- if ($rightblocksn) {
- foreach($rightblocksn as $rightblockn) {
- //Convert blockname to id
- $rightblock = block_get_id_by_name(str_replace("-","",$rightblockn));
- if ($rightblock) {
- //Check it's visible
- if($block = get_record("blocks","id",$rightblock,"visible","1")) {
- //Check if the module was hidden at course level
- if (substr($rightblockn,0,1) == "-") {
- $rightblocks[] = -$rightblock;
- } else {
- $rightblocks[] = $rightblock;
- }
- }
- }
- }
- }
-
- //Calculate the blockinfo field
- if ($leftblocks || $rightblocks) {
- $blockinfo = '';
- if ($leftblocks) {
- $blockinfo .= implode(",", $leftblocks);
- }
- if ($rightblocks) {
- $blockinfo .= ':'.implode(",",$rightblocks);
- }
- } else {
- $blockinfo = '';
- }
-
- //If a course has been specified, update it
- if ($courseid) {
- set_field('course', "blockinfo", $blockinfo, "id", $courseid);
- }
-
- //Returns the blockinfo
- return $blockinfo;
+ // Make up and store the blockinfo field
}
// This function returns the appropriate block default configuration string
$leftblock = block_get_name_by_id(abs($leftblockn));
if ($leftblock) {
//Check it's visible
- if($block = get_record("blocks","name",$leftblock,"visible","1")) {
+ if($block = get_record('block','name',$leftblock,'visible','1')) {
//Check if it's hidden oe no in the course
if($leftblockn<0) {
$leftblocks[] = '-'.$leftblock;
$rightblock = block_get_name_by_id(abs($rightblockn));
if ($rightblock) {
//Check it's visible
- if($block = get_record("blocks","name",$rightblock,"visible","1")) {
+ if($block = get_record('block', 'name', $rightblock, 'visible', '1')) {
//Check if it's hidden oe no in the course
if($rightblockn<0) {
$rightblocks[] = '-'.$rightblock;
return blocks_get_default_blocks(NULL,$blockinfo);
}
-// This is used to register the blocks that are displayed in the course page.
-// Set in course/view.php or /index.php, and read from any other place.
-function blocks_used($blocks = NULL, $records = NULL) {
- static $used = NULL;
-
- if(!empty($blocks) && !empty($records)) {
- $used = array();
- foreach($blocks as $val) {
- if($val > 0 && isset($records[$val])) {
- $used[] = $records[$val]->name;
- }
- }
- }
-
- return $used;
+function blocks_print_blocks($page, $position) {
+ $blocks = get_records('block_instance', 'pageid', $page->id, 'pagetype', $page->type, 'visible', '1');
+ echo "blocks_print_blocks()<br />";
+ print_object($blocks);
}
+
?>
`format` varchar(10) NOT NULL default 'topics',
`showgrades` smallint(2) unsigned NOT NULL default '1',
`modinfo` longtext NOT NULL,
- `blockinfo` varchar(255) NOT NULL default '',
`newsitems` smallint(5) unsigned NOT NULL default '1',
`teacher` varchar(100) NOT NULL default 'Teacher',
`teachers` varchar(100) NOT NULL default 'Teachers',
format varchar(10) NOT NULL default 'topics',
showgrades integer NOT NULL default '1',
modinfo text NOT NULL default '',
- blockinfo varchar(255) NOT NULL default '',
newsitems integer NOT NULL default '1',
teacher varchar(100) NOT NULL default 'Teacher',
teachers varchar(100) NOT NULL default 'Teachers',
return $string;
}
+/**
+ * Recursive implementation of stripslashes()
+ *
+ * This function will allow you to strip the slashes from a variable.
+ * If the variable is an array or object, slashes will be stripped
+ * from the items (or properties) it contains, even if they are arrays
+ * or objects themselves.
+ *
+ * @param mixed the variable to remove slashes from
+ * @return mixed
+ */
+function stripslashes_recursive($var) {
+ if(is_object($var)) {
+ $properties = get_object_vars($var);
+ foreach($properties as $property => $value) {
+ $var->$property = stripslashes_recursive($value);
+ }
+ }
+ else if(is_array($var)) {
+ foreach($var as $property => $value) {
+ $var[$property] = stripslashes_recursive($value);
+ }
+ }
+ else if(is_string($var)) {
+ $var = stripslashes($var);
+ }
+ return $var;
+}
+
/**
* Given some normal text this function will break up any
* long words to a given size by inserting the given character
function print_speller_button () {
echo '<input type="button" value="Check spelling" onclick="openSpellChecker();" />'."\n";
}
+
+function page_source_script($page) {
+ global $CFG;
+
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ return $CFG->wwwroot.'/course/view.php?id='.$page->id;
+ break;
+ }
+}
+
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
?>
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)
- $version = 2004100800; // YYYYMMDD = date of first major branch release 1.4
+ $version = 2004101900; // YYYYMMDD = date of first major branch release 1.4
// XY = increments within a single day
$release = '1.5 UNSTABLE DEVELOPMENT'; // Human-friendly version name