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 = ''.get_string('yes').' ('.get_string('change', 'admin').')'; + } + else { + $multiple = ''.get_string('no').' ('.get_string('change', 'admin').')'; + } + } + else { + $multiple = ''; + } - $table->data[] = array (''.$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 .= "

pixpath/t/edit.gif\" alt=\"\" />
"; } diff --git a/blocks/db/mysql.php b/blocks/db/mysql.php index cdf3efb2b43..1b573ee733e 100644 --- a/blocks/db/mysql.php +++ b/blocks/db/mysql.php @@ -24,7 +24,7 @@ global $CFG; $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 '', @@ -37,6 +37,89 @@ global $CFG; 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; diff --git a/blocks/db/mysql.sql b/blocks/db/mysql.sql index 36f2bde8c1f..fe9b44f1af5 100644 --- a/blocks/db/mysql.sql +++ b/blocks/db/mysql.sql @@ -3,13 +3,27 @@ # 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; # -------------------------------------------------------- diff --git a/blocks/db/postgres7.php b/blocks/db/postgres7.php index 174fcb194c1..ef29933a8f7 100644 --- a/blocks/db/postgres7.php +++ b/blocks/db/postgres7.php @@ -39,6 +39,85 @@ global $CFG; } + 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; diff --git a/blocks/db/postgres7.sql b/blocks/db/postgres7.sql index eccdc73f06c..c60694d3e43 100644 --- a/blocks/db/postgres7.sql +++ b/blocks/db/postgres7.sql @@ -9,6 +9,19 @@ CREATE TABLE prefix_blocks ( 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 '' +) ; + # -------------------------------------------------------- diff --git a/blocks/login/block_login.php b/blocks/login/block_login.php index 6e54061c670..7919ad38549 100644 --- a/blocks/login/block_login.php +++ b/blocks/login/block_login.php @@ -1,10 +1,9 @@ title = get_string('login'); $this->content_type = BLOCK_TYPE_TEXT; - $this->course = $course; $this->version = 2004081600; } diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 69cd7190ac4..2010bc4a7d4 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -6,12 +6,18 @@ define('BLOCK_TYPE_NUKE', 3); 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, @@ -118,14 +124,9 @@ class MoodleBlock { 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'); @@ -134,6 +135,7 @@ class MoodleBlock { $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'; @@ -148,7 +150,7 @@ class MoodleBlock { $movebuttons = '
'; - if($enabled) { + if($this->instance->visible) { $icon = '/t/hide.gif'; $title = $this->str->hide; } @@ -157,27 +159,37 @@ class MoodleBlock { $title = $this->str->show; } - $movebuttons .= '' . - ''; + $page = new stdClass; + $page->id = $this->instance->pageid; + $page->type = $this->instance->pagetype; + $script = page_source_script($page); + + $movebuttons .= '' . + '\"\"'; - $movebuttons .= '' . - ' '; + 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 .= '
'; @@ -225,12 +237,27 @@ class MoodleBlock { 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 @@ -248,6 +275,49 @@ class MoodleBlock { // 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 { diff --git a/blocks/news_items/block_news_items.php b/blocks/news_items/block_news_items.php index 1d4869ef420..b896f2d7501 100644 --- a/blocks/news_items/block_news_items.php +++ b/blocks/news_items/block_news_items.php @@ -1,10 +1,9 @@ title = get_string('latestnews'); $this->content_type = BLOCK_TYPE_TEXT; - $this->course = $course; $this->version = 2004052600; } @@ -15,23 +14,24 @@ class CourseBlock_news_items extends MoodleBlock { 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(); } diff --git a/blocks/online_users/block_online_users.php b/blocks/online_users/block_online_users.php index 1c3e31a5416..c9e11f4605c 100644 --- a/blocks/online_users/block_online_users.php +++ b/blocks/online_users/block_online_users.php @@ -1,10 +1,9 @@ title = get_string('blockname','block_online_users'); $this->content_type = BLOCK_TYPE_TEXT; - $this->course = $course; $this->version = 2004052700; } @@ -31,14 +30,16 @@ class CourseBlock_online_users extends MoodleBlock { 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; @@ -46,11 +47,10 @@ class CourseBlock_online_users extends MoodleBlock { $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 = ""; @@ -61,11 +61,11 @@ class CourseBlock_online_users extends MoodleBlock { $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"; } @@ -83,7 +83,7 @@ class CourseBlock_online_users extends MoodleBlock { } } - 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' @@ -130,7 +130,7 @@ class CourseBlock_online_users extends MoodleBlock { } $this->content->text .= $imgtag; } - $this->content->text .= ''.$user->fullname.''; + $this->content->text .= ''.$user->fullname.''; } /* $table->align = array("right","left"); diff --git a/blocks/participants/block_participants.php b/blocks/participants/block_participants.php index 0a85027eeac..02e181bc96a 100644 --- a/blocks/participants/block_participants.php +++ b/blocks/participants/block_participants.php @@ -1,10 +1,9 @@ title = get_string('people'); $this->content_type = BLOCK_TYPE_LIST; - $this->course = $course; $this->version = 2004052600; } @@ -14,12 +13,12 @@ class CourseBlock_participants extends MoodleBlock { 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 = ''; @@ -27,26 +26,28 @@ class CourseBlock_participants extends MoodleBlock { $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[]=''.get_string('participants').''; + $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[]=''.get_string('participants').''; $this->content->icons[]=''; } - if ($this->course->groupmode or !$this->course->groupmodeforce) { - if ($this->course->groupmode == VISIBLEGROUPS or isteacheredit($this->course->id)) { - $this->content->items[]=''.$strgroups.''; + if ($course->groupmode || !$course->groupmodeforce) { + if ($course->groupmode == VISIBLEGROUPS or isteacheredit($this->instance->pageid)) { + $this->content->items[]=''.$strgroups.''; $this->content->icons[]=''; - } 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[]=''.$strgroupmy.''; + } else if ($currentgroup = get_current_group($this->instance->pageid)) { + $this->content->items[]=''.$strgroupmy.''; $this->content->icons[]=''; } } if (!empty($USER->id)) { $fullname = fullname($USER, true); - $editmyprofile = ''.get_string('editmyprofile').''; + $editmyprofile = ''.get_string('editmyprofile').''; if ($USER->description) { $this->content->items[]= $editmyprofile; } else { diff --git a/blocks/recent_activity/block_recent_activity.php b/blocks/recent_activity/block_recent_activity.php index 03ee7a3767b..4a64ddbe624 100644 --- a/blocks/recent_activity/block_recent_activity.php +++ b/blocks/recent_activity/block_recent_activity.php @@ -1,10 +1,9 @@ title = get_string('recentactivity'); $this->content_type = BLOCK_TYPE_TEXT; - $this->course = $course; $this->version = 2004042900; } @@ -14,18 +13,20 @@ class CourseBlock_recent_activity extends MoodleBlock { 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(); diff --git a/blocks/search_forums/block_search_forums.php b/blocks/search_forums/block_search_forums.php index aee0be0b465..75c78fdb694 100644 --- a/blocks/search_forums/block_search_forums.php +++ b/blocks/search_forums/block_search_forums.php @@ -1,10 +1,9 @@ title = get_string('search', 'forum'); $this->content_type = BLOCK_TYPE_TEXT; - $this->course = $course; $this->version = 2004041000; } @@ -17,16 +16,18 @@ class CourseBlock_search_forums extends MoodleBlock { 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 = '
'.$form.'
'; return $this->content; diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index 5ada326d7a3..ec4dfc84496 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -2,7 +2,15 @@ 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'); @@ -11,12 +19,7 @@ class CourseBlock_section_links extends MoodleBlock { } 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() { @@ -32,41 +35,46 @@ class CourseBlock_section_links extends MoodleBlock { 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 = ''; - 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"'; @@ -77,8 +85,8 @@ class CourseBlock_section_links extends MoodleBlock { } } 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 .= "
$linktext"; } diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php index de6d283236a..72bd588cf7d 100644 --- a/blocks/site_main_menu/block_site_main_menu.php +++ b/blocks/site_main_menu/block_site_main_menu.php @@ -1,10 +1,9 @@ title = get_string('mainmenu'); $this->content_type = BLOCK_TYPE_LIST; - $this->course = $course; $this->version = 2004052700; } @@ -19,28 +18,30 @@ class CourseBlock_site_main_menu extends MoodleBlock { 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; + } - $sections = get_all_sections($this->course->id); + $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->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'); @@ -49,7 +50,7 @@ class CourseBlock_site_main_menu extends MoodleBlock { $stractivityclipboard = $USER->activitycopyname; } - $modinfo = unserialize($this->course->modinfo); + $modinfo = unserialize($course->modinfo); $editbuttons = ''; if ($ismoving) { @@ -67,7 +68,7 @@ class CourseBlock_site_main_menu extends MoodleBlock { if ($isediting && !$ismoving) { if ($groupbuttons) { if (! $mod->groupmodelink = $groupbuttonslink) { - $mod->groupmode = $this->course->groupmode; + $mod->groupmode = $course->groupmode; } } else { @@ -88,7 +89,7 @@ class CourseBlock_site_main_menu extends MoodleBlock { } $instancename = urldecode($modinfo[$modnumber]->name); if (!empty($CFG->filterall)) { - $instancename = filter_text(''.$instancename.'', $this->course->id); + $instancename = filter_text(''.$instancename.'', $this->instance->pageid); } $linkcss = $mod->visible ? '' : ' class="dimmed" '; if (!empty($modinfo[$modnumber]->extra)) { @@ -122,7 +123,7 @@ class CourseBlock_site_main_menu extends MoodleBlock { if ($isediting && $modnames) { $this->content->footer = '
'. - print_section_add_menus($this->course, 0, $modnames, true, true).'
'; + print_section_add_menus($course, 0, $modnames, true, true).''; } else { $this->content->footer = ''; } diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php index b0a182eaa87..e5c562a67c9 100644 --- a/blocks/social_activities/block_social_activities.php +++ b/blocks/social_activities/block_social_activities.php @@ -1,10 +1,9 @@ title = get_string('blockname','block_social_activities'); $this->content_type = BLOCK_TYPE_LIST; - $this->course = $course; $this->version = 2004041800; } @@ -18,26 +17,29 @@ class CourseBlock_social_activities extends MoodleBlock { 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'")); @@ -45,7 +47,7 @@ class CourseBlock_social_activities extends MoodleBlock { $stractivityclipboard = $USER->activitycopyname; } - $modinfo = unserialize($this->course->modinfo); + $modinfo = unserialize($course->modinfo); $editbuttons = ''; if ($ismoving) { @@ -63,7 +65,7 @@ class CourseBlock_social_activities extends MoodleBlock { if ($isediting && !$ismoving) { if ($groupbuttons) { if (! $mod->groupmodelink = $groupbuttonslink) { - $mod->groupmode = $this->course->groupmode; + $mod->groupmode = $course->groupmode; } } else { @@ -84,7 +86,7 @@ class CourseBlock_social_activities extends MoodleBlock { } $instancename = urldecode($modinfo[$modnumber]->name); if (!empty($CFG->filterall)) { - $instancename = filter_text(''.$instancename.'', $this->course->id); + $instancename = filter_text(''.$instancename.'', $this->instance->pageid); } $linkcss = $mod->visible ? '' : ' class="dimmed" '; if (!empty($modinfo[$modnumber]->extra)) { @@ -118,7 +120,7 @@ class CourseBlock_social_activities extends MoodleBlock { if ($isediting && $modnames) { $this->content->footer = '
'. - print_section_add_menus($this->course, 0, $modnames, true, true).'
'; + print_section_add_menus($course, 0, $modnames, true, true).''; } else { $this->content->footer = ''; } diff --git a/blocks/version.php b/blocks/version.php index 05834435d26..3e3151a6dde 100644 --- a/blocks/version.php +++ b/blocks/version.php @@ -5,4 +5,4 @@ // 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) diff --git a/course/edit.php b/course/edit.php index 400fb72313f..345a94d41be 100644 --- a/course/edit.php +++ b/course/edit.php @@ -54,8 +54,11 @@ 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)) { @@ -68,10 +71,14 @@ } 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; diff --git a/course/format/social/format.php b/course/format/social/format.php index cc93c13ad14..c10a254eb8c 100644 --- a/course/format/social/format.php +++ b/course/format/social/format.php @@ -11,8 +11,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); @@ -25,9 +25,9 @@ echo ''; echo ''; - if(block_have_active($leftblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) { echo ''; } @@ -53,14 +53,14 @@ } echo ''; - if(block_have_active($rightblocks) || $editing) { - echo ''; + // The right column + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { + echo ''; } echo ''; diff --git a/course/format/topics/format.php b/course/format/topics/format.php index 49e3ac5b81d..41316e7e533 100644 --- a/course/format/topics/format.php +++ b/course/format/topics/format.php @@ -14,8 +14,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); @@ -63,14 +63,14 @@ /// The left column ... - if(block_have_active($leftblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) { echo ''; } /// Start main column - echo ""; // The right column - if(block_have_active($rightblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { 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 ''; } /// Start main column - echo ""; // The right column - if(block_have_active($rightblocks) || $editing) { + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) { 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 @@ '; - print_course_blocks($site, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo ''; } @@ -260,15 +204,18 @@ } echo ''; - if(block_have_active($rightblocks) || $editing || isadmin()) { + + + // The right column + if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing || isadmin()) { 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 = '
'.$content.'
'; - print_side_block($strblocks, $content, NULL, NULL, NULL); +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 '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); + } + if(!$blockobject->instance_config_save($data)) { + error('Error saving block configuration'); + } + // And nothing more, continue with displaying the page + } + else { + $loggedinas = "

".user_login_string($course, $USER)."

"; + 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 ''; // This I wouldn't call a hack but it sure looks cheeky + echo '

'; + foreach($hiddendata as $name => $val) { + echo ''; + } + echo '

'; + $blockobject->instance_config_print(); + echo ''; + print_footer(); + die(); // Do not go on with the other course-related stuff + } + break; + case 'toggle': + if(empty($instance)) { + error('Invalid block instance for '.$blockaction); + } + $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': + 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': + 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 '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); + + 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; + } +} + +function blocks_get_by_page($page) { + $blocks = get_records_select('block_instance', 'pageid = '.$page->id.' AND pagetype = \''.$page->type.'\'', 'position, weight'); + + $arr = array(BLOCK_POS_LEFT => array(), BLOCK_POS_RIGHT => array()); + if(empty($blocks)) { + return $arr; + } + + foreach($blocks as $block) { + $arr[$block->position][$block->weight] = $block; + } + + return $arr; +} + +//This function prints the block to admin blocks as necessary +function blocks_print_adminblock($page, $missingblocks) { + global $USER; + + $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); + } + + $blockobject = block_instance($block->name); + if ($blockobject === false) { + continue; + } + $menu[$block->id] = $blockobject->get_title(); + } + + 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 = '
'.$content.'
'; + print_side_block($strblocks, $content, NULL, NULL, NULL); + } +} + +function blocks_repopulate_page($page) { + global $CFG; + + /// 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'; + } + } + // It's a normal course, so do it accodring to the course format + else { + $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'; + } + } + } + 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) { @@ -506,7 +690,7 @@ function upgrade_blocks_plugins($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; @@ -516,11 +700,11 @@ function upgrade_blocks_plugins($continueto) { $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'); } @@ -533,15 +717,15 @@ function upgrade_blocks_plugins($continueto) { $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"; @@ -555,7 +739,9 @@ function upgrade_blocks_plugins($continueto) { 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 @@ -563,10 +749,16 @@ function upgrade_blocks_plugins($continueto) { $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')) { @@ -589,7 +781,7 @@ function upgrade_blocks_plugins($continueto) { $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) { @@ -616,10 +808,10 @@ function upgrade_blocks_plugins($continueto) { 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 '
'; @@ -631,11 +823,13 @@ function upgrade_blocks_plugins($continueto) { } 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('Naming conflict: block '.$block->name.' has the same title with an existing block, '.$conflictblock.'!'); @@ -650,7 +844,7 @@ function upgrade_blocks_plugins($continueto) { @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 "
"; } else { @@ -670,24 +864,25 @@ function upgrade_blocks_plugins($continueto) { } } - //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.')
'; - } + $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'); } @@ -697,107 +892,10 @@ function upgrade_blocks_plugins($continueto) { } } -//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; @@ -807,7 +905,7 @@ function block_get_id_by_name ($blockname) { //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; @@ -831,89 +929,7 @@ function blocks_get_default_blocks ($courseid = NULL, $blocknames = '') { } } - //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 @@ -1000,7 +1016,7 @@ function blocks_get_block_names ($blockinfo) { $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; @@ -1018,7 +1034,7 @@ function blocks_get_block_names ($blockinfo) { $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; @@ -1056,21 +1072,11 @@ function blocks_get_block_ids ($blockinfo) { 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()
"; + print_object($blocks); } + ?> diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index d70b7d72ab7..eaf39b54488 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -39,7 +39,6 @@ CREATE TABLE `prefix_course` ( `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', diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 5094e477e9d..8d3479ab425 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -17,7 +17,6 @@ CREATE TABLE prefix_course ( 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', diff --git a/lib/weblib.php b/lib/weblib.php index a96283e8465..961f1d10ae2 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -317,6 +317,35 @@ function stripslashes_safe($string) { 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 @@ -3423,5 +3452,16 @@ function print_speller_code ($usehtmleditor=false) { function print_speller_button () { echo ''."\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: ?> diff --git a/version.php b/version.php index 21d4be45605..8f67e1e3293 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // 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
'; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo ''; - print_course_blocks($course, $rightblocks, BLOCK_RIGHT); - if ($editing && !empty($missingblocks)) { - block_print_blocks_admin($course, $missingblocks); - } - print_spacer(1, 120, true); - echo ''; + blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]); + if ($editing && !empty($missingblocks)) { + blocks_print_adminblock($page, $missingblocks); + } + echo '
'; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo '"; + echo ''; print_heading_block(get_string("topicoutline"), "100%", "outlineheadingblock"); print_spacer(8, 1, true); @@ -270,13 +270,12 @@ 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 ''; - print_course_blocks($course, $leftblocks, BLOCK_LEFT); + blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]); echo '"; + echo ''; print_heading_block(get_string("weeklyoutline"), "100%", "outlineheadingblock"); print_spacer(8, 1, true); @@ -256,14 +256,13 @@ 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 '
'; 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 '