diff --git a/admin/block.php b/admin/block.php
index d6eb8da1a61..755a3c141e6 100644
--- a/admin/block.php
+++ b/admin/block.php
@@ -20,12 +20,12 @@
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');
}
diff --git a/admin/blocks.php b/admin/blocks.php
index 1ab298d7174..28da4898494 100644
--- a/admin/blocks.php
+++ b/admin/blocks.php
@@ -5,11 +5,11 @@
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();
@@ -32,8 +32,9 @@
$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",
"$stradministration -> ".
@@ -44,27 +45,35 @@
/// 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']) {
@@ -76,11 +85,16 @@
} 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
@@ -105,12 +119,12 @@
/// 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;
}
@@ -136,11 +150,11 @@
//$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) {
@@ -155,7 +169,7 @@
$settings = ''.$strsettings.'';
}
- $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) {
@@ -166,8 +180,27 @@
'
';
$class = ' class="dimmed_text"'; // Leading space required!
}
+ if ($blockobject->instance_allow_multiple()) {
+ if($blocks[$blockid]->multiple) {
+ $multiple = '
'.$blockobject->get_title().'
', $count, $blockobject->get_version(), $visible, $delete, $settings); + $table->data[] = array( + ''.$blockobject->get_title().'
', + $count, + $blockobject->get_version(), + $visible, + $multiple, + $delete, + $settings + ); } echo '';
print_table($table);
diff --git a/admin/site.php b/admin/site.php
index 0c7a6a65ae6..ab18e5e9e82 100644
--- a/admin/site.php
+++ b/admin/site.php
@@ -16,7 +16,7 @@
if (!empty($USER->id)) { // Additional identity check
if (!confirm_sesskey()) {
- error(get_string('confirmsesskeybad', 'error'));
+ //error(get_string('confirmsesskeybad', 'error'));
}
}
@@ -38,9 +38,14 @@
// [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);
diff --git a/blocks/activity_modules/block_activity_modules.php b/blocks/activity_modules/block_activity_modules.php
index 55a59d18ced..c8a1b757aee 100644
--- a/blocks/activity_modules/block_activity_modules.php
+++ b/blocks/activity_modules/block_activity_modules.php
@@ -1,10 +1,9 @@
title = get_string('activities');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004041000;
}
@@ -18,7 +17,7 @@ class CourseBlock_activity_modules extends MoodleBlock {
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
@@ -26,7 +25,7 @@ class CourseBlock_activity_modules extends MoodleBlock {
if ($modnamesused) {
foreach ($modnamesused as $modname => $modfullname) {
if ($modname != 'label') {
- $this->content->items[] = ''.$modnamesplural[$modname].'';
+ $this->content->items[] = ''.$modnamesplural[$modname].'';
$this->content->icons[] = '
';
}
}
diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php
index 4e3a615e314..a7a6405c303 100644
--- a/blocks/admin/block_admin.php
+++ b/blocks/admin/block_admin.php
@@ -1,10 +1,9 @@
title = get_string('administration');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004081200;
}
@@ -14,14 +13,14 @@ class CourseBlock_admin extends MoodleBlock {
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();
@@ -77,81 +76,83 @@ class CourseBlock_admin extends MoodleBlock {
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[]='
';
- if (isediting($this->course->id)) {
- $this->content->items[]=''.get_string('turneditingoff').'';
+ if (isediting($this->instance->pageid)) {
+ $this->content->items[]=''.get_string('turneditingoff').'';
} else {
- $this->content->items[]=''.get_string('turneditingon').'';
+ $this->content->items[]=''.get_string('turneditingon').'';
}
- $this->content->items[]=''.get_string('settings').'...';
+ $this->content->items[]=''.get_string('settings').'...';
$this->content->icons[]='
';
- 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[]=''.$this->course->teachers.'...';
+ $this->content->items[]=''.$course->teachers.'...';
$this->content->icons[]='
';
}
- if (!$this->course->students) {
- $this->course->students = get_string('defaultcoursestudents');
+ if (!$course->students) {
+ $course->students = get_string('defaultcoursestudents');
}
- $this->content->items[]=''.$this->course->students.'...';
+ $this->content->items[]=''.$course->students.'...';
$this->content->icons[]='
';
- $this->content->items[]=''.get_string('backup').'...';
+ $this->content->items[]=''.get_string('backup').'...';
$this->content->icons[]='
';
- $this->content->items[]=''.get_string('restore').'...';
+ $this->content->items[]=''.get_string('restore').'...';
$this->content->icons[]='
';
- $this->content->items[]=''.get_string('scales').'...';
+ $this->content->items[]=''.get_string('scales').'...';
$this->content->icons[]='
';
}
- $this->content->items[]=''.get_string('grades').'...';
+ $this->content->items[]=''.get_string('grades').'...';
$this->content->icons[]='
';
- $this->content->items[]=''.get_string('logs').'...';
+ $this->content->items[]=''.get_string('logs').'...';
$this->content->icons[]='
';
if ($isteacheredit) {
- $this->content->items[]=''.get_string('files').'...';
+ $this->content->items[]=''.get_string('files').'...';
$this->content->icons[]='
';
}
- $this->content->items[]=''.get_string('help').'...';
+ $this->content->items[]=''.get_string('help').'...';
$this->content->icons[]='
';
- if ($teacherforum = forum_get_course_forum($this->course->id, 'teacher')) {
+ if ($teacherforum = forum_get_course_forum($this->instance->pageid, 'teacher')) {
$this->content->items[]=''.get_string('nameteacher', 'forum').'';
$this->content->icons[]='
';
}
} else if (!isguest()) { // Students menu
- if ($this->course->showgrades) {
- $this->content->items[]=''.get_string('grades').'...';
+ if ($course->showgrades) {
+ $this->content->items[]=''.get_string('grades').'...';
$this->content->icons[]='
';
}
- if ($this->course->showreports) {
- $this->content->items[]=''.get_string('activityreport').'...';
+ if ($course->showreports) {
+ $this->content->items[]=''.get_string('activityreport').'...';
$this->content->icons[]='
';
}
if (is_internal_auth()) {
- $this->content->items[]=''.get_string('changepassword').'...';
+ $this->content->items[]=''.get_string('changepassword').'...';
$this->content->icons[]='
';
} else if ($CFG->changepassword) {
$this->content->items[]=''.get_string('changepassword').'...';
$this->content->icons[]='
';
}
if ($CFG->allowunenroll) {
- $this->content->items[]=''.get_string('unenrolme', '', $this->course->shortname).'...';
+ $this->content->items[]=''.get_string('unenrolme', '', $course->shortname).'...';
$this->content->icons[]='
';
}
}
diff --git a/blocks/calendar_month/block_calendar_month.php b/blocks/calendar_month/block_calendar_month.php
index 130409f2098..defb8bdc8d2 100644
--- a/blocks/calendar_month/block_calendar_month.php
+++ b/blocks/calendar_month/block_calendar_month.php
@@ -1,10 +1,9 @@
title = get_string('calendar', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004081200;
}
@@ -19,20 +18,20 @@ class CourseBlock_calendar_month extends MoodleBlock {
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;
diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php
index 6b77a8c8167..15dff0111e4 100644
--- a/blocks/calendar_upcoming/block_calendar_upcoming.php
+++ b/blocks/calendar_upcoming/block_calendar_upcoming.php
@@ -1,10 +1,9 @@
title = get_string('upcomingevents', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
- $this->course = $course;
$this->version = 2004052600;
}
@@ -19,10 +18,10 @@ class CourseBlock_calendar_upcoming extends MoodleBlock {
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();
@@ -30,15 +29,15 @@ class CourseBlock_calendar_upcoming extends MoodleBlock {
} else {
- $courseshown = $this->course->id;
+ $courseshown = $this->instance->pageid;
$this->content->footer = '
'.
+ '/calendar/view.php?view=upcoming&course='.$courseshown.'">'.
get_string('gotocalendar', 'calendar').'...';
$this->content->footer .= '
'.
+ '/calendar/event.php?action=new&course='.$courseshown.'">'.
get_string('newevent', 'calendar').'...';
- 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;
@@ -59,9 +58,9 @@ class CourseBlock_calendar_upcoming extends MoodleBlock {
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)) {
diff --git a/blocks/course_list/block_course_list.php b/blocks/course_list/block_course_list.php
index f93c8e58e8d..cda9e3e8d7e 100644
--- a/blocks/course_list/block_course_list.php
+++ b/blocks/course_list/block_course_list.php
@@ -1,10 +1,9 @@
title = get_string('courses');
$this->content_type = BLOCK_TYPE_LIST;
- $this->course = $course;
$this->version = 2004081200;
}
@@ -20,13 +19,6 @@ class CourseBlock_course_list extends MoodleBlock {
return true;
}
- function handle_config($config) {
- foreach ($config as $name => $value) {
- set_config($name, $value);
- }
- return true;
- }
-
function get_content() {
global $THEME, $CFG, $USER;
@@ -34,7 +26,7 @@ class CourseBlock_course_list extends MoodleBlock {
return $this->content;
}
- $this->content = New object;
+ $this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
diff --git a/blocks/course_summary/block_course_summary.php b/blocks/course_summary/block_course_summary.php
index 143fa62a7bf..9639735e38e 100644
--- a/blocks/course_summary/block_course_summary.php
+++ b/blocks/course_summary/block_course_summary.php
@@ -1,17 +1,18 @@
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;
@@ -19,18 +20,20 @@ class CourseBlock_course_summary extends MoodleBlock {
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 .= "
';
+ if($options & BLOCK_CONFIGURE) {
+ $movebuttons .= '' .
+ '
';
+ }
+
+ $movebuttons .= '' .
+ '
';
if ($options & BLOCK_MOVE_LEFT) {
- $movebuttons .= '' .
- '
';
+ $movebuttons .= '' .
+ '
';
}
if ($options & BLOCK_MOVE_UP) {
- $movebuttons .= '' .
- '
';
+ $movebuttons .= '' .
+ '
';
}
if ($options & BLOCK_MOVE_DOWN) {
- $movebuttons .= '' .
- '
';
+ $movebuttons .= '' .
+ '
';
}
if ($options & BLOCK_MOVE_RIGHT) {
- $movebuttons .= '' .
- '
';
+ $movebuttons .= '' .
+ '
';
}
$movebuttons .= '| '; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo ' | '; } @@ -53,14 +53,14 @@ } echo ''; - if(block_have_active($rightblocks) || $editing) { - echo ''; - print_course_blocks($course, $rightblocks, BLOCK_RIGHT); - if ($editing && !empty($missingblocks)) { - block_print_blocks_admin($course, $missingblocks); - } - print_spacer(1, 120, true); - echo ' | '; + // The right column + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { + echo ''; + blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]); + if ($editing && !empty($missingblocks)) { + blocks_print_adminblock($page, $missingblocks); + } + echo ' | '; } echo ''; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo ' | '; } /// Start main column - echo ""; + echo ' | '; print_heading_block(get_string("topicoutline"), "100%", "outlineheadingblock"); print_spacer(8, 1, true); @@ -270,13 +270,12 @@ echo " | "; // The right column - if(block_have_active($rightblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { echo ''; - 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 ' | '; } diff --git a/course/format/weeks/format.php b/course/format/weeks/format.php index be5b94a6ce4..ef339d29b91 100644 --- a/course/format/weeks/format.php +++ b/course/format/weeks/format.php @@ -10,8 +10,8 @@ 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); @@ -54,14 +54,14 @@ /// The left column ... - if(block_have_active($leftblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) { echo ''; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo ' | '; } /// Start main column - echo ""; + echo ' | '; print_heading_block(get_string("weeklyoutline"), "100%", "outlineheadingblock"); print_spacer(8, 1, true); @@ -256,14 +256,13 @@ echo " | "; // The right column - if(block_have_active($rightblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { echo ''; - 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 ' | '; + echo ''; } echo "\n"; diff --git a/course/view.php b/course/view.php index 4b9bb405261..2f30c82d289 100644 --- a/course/view.php +++ b/course/view.php @@ -9,6 +9,10 @@ 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"); } @@ -33,31 +37,12 @@ $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; } @@ -75,47 +60,29 @@ $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)); + } + 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); } - // 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; - } - } - } - } + $missingblocks = blocks_get_missing($page, $pageblocks); if (!empty($section)) { if (!empty($move) and confirm_sesskey()) { @@ -126,11 +93,6 @@ } } 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"; @@ -168,26 +130,6 @@ } } - // 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); diff --git a/index.php b/index.php index a8ce03945ea..5977b97db5d 100644 --- a/index.php +++ b/index.php @@ -18,6 +18,10 @@ 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"); } @@ -57,92 +61,32 @@ $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. + $page = new stdClass; + $page->id = SITEID; + $page->type = MOODLE_PAGE_COURSE; - $blocks = $site->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); - } + $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); @@ -155,9 +99,9 @@
| ';
if (isadmin()) {
echo ' '.update_course_icon($site->id).' ';
echo ''; } - 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 ' | ';
}
diff --git a/lang/en/admin.php b/lang/en/admin.php
index 81418df9caa..e53937efbfb 100755
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -2,6 +2,9 @@
// 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. More information';
diff --git a/lang/en/block_course_summary.php b/lang/en/block_course_summary.php
index 3557c542d41..5980a98fffd 100644
--- a/lang/en/block_course_summary.php
+++ b/lang/en/block_course_summary.php
@@ -1,5 +1,6 @@
diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 6f33852ecc3..bceb049e78a 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -116,6 +116,9 @@ $string['backuptakealook'] = 'Please take a look to your backup logs in:
$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';
diff --git a/lib/blocklib.php b/lib/blocklib.php
index 4884b29f693..9ee8e81db21 100644
--- a/lib/blocklib.php
+++ b/lib/blocklib.php
@@ -2,61 +2,96 @@
//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();
- }
- 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);
+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;
}
+ return NULL;
+}
- $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]);
+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;
+ }
}
}
}
- 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]);
+ return $missingblocks;
+}
+
+function blocks_remove_inappropriate($page) {
+ $pageblocks = blocks_get_by_page($page);
+
+ if(empty($pageblocks)) {
+ return;
+ }
+
+ 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;
+ }
+
+ 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
@@ -111,12 +146,16 @@ function block_method_result($blockname, $method) {
}
//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
@@ -132,309 +171,454 @@ function block_load_class($blockname) {
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;
+function blocks_print_group($page, $instances) {
+
+ if(empty($instances)) {
+ return;
+ }
- $isediting = isediting($course->id);
- $ismoving = ismoving($course->id);
- $isteacheredit = isteacheredit($course->id);
+ switch($page->type) {
+ case MOODLE_PAGE_COURSE:
+ $isediting = isediting($page->id);
+ $ismoving = ismoving($page->id);
+ $isteacheredit = isteacheredit($page->id);
+ break;
+ }
- 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');
+ // 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');
+ }
+
+ $maxweight = max(array_keys($instances));
+
+ foreach($instances as $instance) {
+ $block = blocks_get_record($instance->blockid);
+ if(!$block->visible) {
+ // Disabled by the admin
+ continue;
}
- $blockdata = get_records('blocks', 'visible', 1);
- if($blockdata !== false) {
+ $obj = block_instance($block->name, $instance);
- $lastblock = end($blocksarray);
- $firstblock = reset($blocksarray);
+ 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);
+ }
- foreach($blocksarray as $blockid) {
- if(!isset($blockdata[abs($blockid)])) {
- // This block is hidden. Don't show it.
- continue;
- }
-
- $blockname = $blockdata[abs($blockid)]->name;
- $block = block_instance($blockname, $course);
- if($block === false) {
- // Something went wrong
- continue;
- }
-
- // There are various sanity checks commented out below
- // because the block detection code should have already done them long ago.
-
- /*
- if(!is_subclass_of($block, 'MoodleBlock')) {
- // Error: you have to derive from MoodleBlock
- continue;
- }
-
- 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($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.
-
-function block_action(&$course, &$leftblocks, &$rightblocks, $blockaction, $blockid) {
-
- $blockid = abs(intval($blockid)); // Just to make sure
-
- switch($blockaction) {
- case 'toggle':
- $block = block_find($blockid, $leftblocks, $rightblocks);
- if($block !== false) {
- if($block->side == BLOCK_LEFT) {
- $leftblocks[$block->position] = -$leftblocks[$block->position];
- }
- else {
- $rightblocks[$block->position] = -$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;
- 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;
- }
- 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];
- }
- }
- 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;
- }
- }
- }
- 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;
- }
- }
- }
- 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;
- }
- }
- break;
+ if($invalidate || empty($cache)) {
+ $cache = get_records('block');
}
- $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
- set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
+ if($blockid === NULL) {
+ return $cache;
+ }
+ return (isset($cache[$blockid])? $cache[$blockid] : false);
}
-// 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 block_find($blockid, $leftblocks, $rightblocks) {
-
- if(($blockid = abs($blockid)) == 0) {
- return false;
- }
-
- $finding->side = BLOCK_LEFT;
- $finding->enabled = true;
- $finding->position = NULL;
-
- // First, search for the "enabled" block, since that's what we
- // will be doing most of the time.
-
- $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;
- }
-
- // "enabled" block not found. Now search for the disabled block.
- $finding->enabled = false;
- $blockid = -$blockid;
-
- $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;
- }
-
- // Nothing found :(
-
- return false;
-}
-
-//This function prints the block to admin blocks as necessary
-function block_print_blocks_admin(&$course, $missingblocks) {
-
- global $USER;
-
- 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;
- }
- $menu[$block->id] = $blockobject->get_title();
- }
-
- if(!$course->category) {
- $target = 'index.php';
- }
- else {
- $target = 'view.php';
- }
- $content = popup_form($target.'?id='.$course->id.'&blockaction=add&sesskey='.$USER->sesskey.'&blockid=',
- $menu, 'add_block', '', "$stradd...", '', '', true);
- $content = '