]> git.mjollnir.org Git - moodle.git/commitdiff
Da monster-commit of blocks version 2!
authordefacer <defacer>
Tue, 19 Oct 2004 21:04:28 +0000 (21:04 +0000)
committerdefacer <defacer>
Tue, 19 Oct 2004 21:04:28 +0000 (21:04 +0000)
Code based on the work of Daryl Hawes for the blog module. Thanks, Daryl!

Please test the hell out of it as it's sure to have issues that need to be
ironed out.

38 files changed:
admin/block.php
admin/blocks.php
admin/site.php
blocks/activity_modules/block_activity_modules.php
blocks/admin/block_admin.php
blocks/calendar_month/block_calendar_month.php
blocks/calendar_upcoming/block_calendar_upcoming.php
blocks/course_list/block_course_list.php
blocks/course_summary/block_course_summary.php
blocks/db/mysql.php
blocks/db/mysql.sql
blocks/db/postgres7.php
blocks/db/postgres7.sql
blocks/login/block_login.php
blocks/moodleblock.class.php
blocks/news_items/block_news_items.php
blocks/online_users/block_online_users.php
blocks/participants/block_participants.php
blocks/recent_activity/block_recent_activity.php
blocks/search_forums/block_search_forums.php
blocks/section_links/block_section_links.php
blocks/site_main_menu/block_site_main_menu.php
blocks/social_activities/block_social_activities.php
blocks/version.php
course/edit.php
course/format/social/format.php
course/format/topics/format.php
course/format/weeks/format.php
course/view.php
index.php
lang/en/admin.php
lang/en/block_course_summary.php
lang/en/moodle.php
lib/blocklib.php
lib/db/mysql.sql
lib/db/postgres7.sql
lib/weblib.php
version.php

index d6eb8da1a61873961e31fb51f154dbcba379b553..755a3c141e60a08e8f8d856753083be3706a56de 100644 (file)
 
     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');
     }
index 1ab298d71749909b4a720a0709e6783b5e999990..28da48984948b31ed5e9919815e84d563e58e274 100644 (file)
@@ -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",
                  "<a href=\"index.php\">$stradministration</a> -> ".
 
 /// If data submitted, then process and store.
 
-    if (!empty($_GET['hide']) and confirm_sesskey()) {
-        if (!$block = get_record('blocks', 'id', $_GET['hide'])) {
+    if (!empty($hide) && confirm_sesskey()) {
+        if (!$block = get_record('block', 'id', $hide)) {
             error("Block doesn't exist!");
         }
-        set_field('blocks', 'visible', '0', 'id', $block->id);      // Hide block
+        set_field('block', 'visible', '0', 'id', $block->id);      // Hide block
     }
 
-    if (!empty($_GET['show']) and confirm_sesskey() ) {
-        if (!$block = get_record('blocks', 'id', $_GET['show'])) {
+    if (!empty($show) && confirm_sesskey() ) {
+        if (!$block = get_record('block', 'id', $show)) {
             error("Block doesn't exist!");
         }
-        set_field('blocks', 'visible', '1', 'id', $block->id);      // Show block
+        set_field('block', 'visible', '1', 'id', $block->id);      // Show block
     }
 
-    if (!empty($delete) and confirm_sesskey()) {
+    if (!empty($multiple) && confirm_sesskey()) {
+        if (!$block = blocks_get_record($multiple)) {
+            error("Block doesn't exist!");
+        }
+        $block->multiple = !$block->multiple;
+        update_record('block', $block);
+    }
 
-        if (!$block = get_record('blocks', 'id', $delete)) {
+    if (!empty($delete) && confirm_sesskey()) {
+
+        if (!$block = blocks_get_record($delete)) {
             error("Block doesn't exist!");
         }
 
-        $blockobject = block_instance($block->name, $site);
+        $blockobject = block_instance($block->name);
         $strblockname = $blockobject->get_title();
 
         if (!$_GET['confirm']) {
 
         } else {
             // Delete block
-            if (!delete_records('blocks', 'id', $block->id)) {
+            if (!delete_records('block', 'id', $block->id)) {
                 notify("Error occurred while deleting the $strblockname record from blocks table");
             }
 
-            blocks_update_every_block_by_id($block->id, 'delete');                        // Delete blocks in all courses by id
+            $instances = get_records('block_instance', 'blockid', $block->id);
+            if(!empty($instances)) {
+                foreach($instances as $instance) {
+                    blocks_delete_instance($instance);
+                }
+            }
 
             // Then the tables themselves
 
 
 /// Get and sort the existing blocks
 
-    if (false === ($blocks = get_records('blocks'))) {
+    if (false === ($blocks = get_records('block'))) {
         error('No blocks found!');  // Should never happen
     }
 
     foreach ($blocks as $block) {
-        if(($blockobject = block_instance($block->name, NULL)) === false) {
+        if(($blockobject = block_instance($block->name)) === false) {
             // Failed to load
             continue;
         }
         //$modpixpath = '../theme/'.$CFG->theme.'/pix/mod';
     }
 
-    $table->head  = array ($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings);
-    $table->align = array ('LEFT', 'RIGHT', 'LEFT', 'CENTER', 'CENTER', 'CENTER');
-    $table->wrap = array ("NOWRAP", "", "", "", "","");
-    $table->size = array ("100%", "10", "10", "10", "10","12");
-    $table->width = "100";
+    $table->head  = array ($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings);
+    $table->align = array ('LEFT', 'RIGHT', 'LEFT', 'CENTER', 'CENTER', 'CENTER', 'CENTER');
+    $table->wrap = array ('NOWRAP', '', '', '', '', '', '');
+    $table->size = array ('100%', '10', '10', '10', '10','12');
+    $table->width = '100';
 
     foreach ($blockbyname as $blockname => $blockid) {
 
             $settings = '<a href="block.php?block='.$blockid.'&sesskey='.$USER->sesskey.'">'.$strsettings.'</a>';
         }
 
-        $count = blocks_get_courses_using_block_by_id($blockid);
+        $count = count_records('block_instance', 'blockid', $blockid);
         $class = ''; // Nothing fancy, by default
 
         if ($blocks[$blockid]->visible) {
                        '<img src="'.$pixpath.'/i/show.gif" style="height: 16px; width: 16px;" alt=\"\" /></a>';
             $class = ' class="dimmed_text"'; // Leading space required!
         }
+        if ($blockobject->instance_allow_multiple()) {
+            if($blocks[$blockid]->multiple) {
+                $multiple = '<nobr>'.get_string('yes').' (<a href="blocks.php?multiple='.$blockid.'&sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</nobr>';
+            }
+            else {
+                $multiple = '<nobr>'.get_string('no').' (<a href="blocks.php?multiple='.$blockid.'&sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</nobr>';
+            }
+        }
+        else {
+            $multiple = '';
+        }
 
-        $table->data[] = array ('<p'.$class.'>'.$blockobject->get_title().'</p>', $count, $blockobject->get_version(), $visible, $delete, $settings);
+        $table->data[] = array(
+            '<p'.$class.'>'.$blockobject->get_title().'</p>',
+            $count,
+            $blockobject->get_version(),
+            $visible,
+            $multiple,
+            $delete,
+            $settings
+        );
     }
     echo '<p>';
     print_table($table);
index 0c7a6a65ae6ede9ba724e8e3dba5f0574900c52a..ab18e5e9e825826b39554f66a3182c003573b8b2 100644 (file)
@@ -16,7 +16,7 @@
 
         if (!empty($USER->id)) {             // Additional identity check
             if (!confirm_sesskey()) {
-                error(get_string('confirmsesskeybad', 'error'));
+                //error(get_string('confirmsesskeybad', 'error'));
             }
         }
 
                 // [pj] We are about to create the site, so let's add some blocks...
                 // calendar_month is included as a Moodle feature advertisement ;-)
                 require_once($CFG->dirroot.'/lib/blocklib.php');
-                $form->blockinfo = blocks_get_default_blocks(NULL, blocks_get_config_default('site'));
 
                 if ($newid = insert_record("course", $form)) {
+                    // Site created, add blocks for it
+                    $page = new stdClass;
+                    $page->type = MOODLE_PAGE_COURSE;
+                    $page->id   = $newid;
+                    blocks_repopulate_page($page); // Return value not checked because you can always edit later
+
                     $cat->name = get_string("miscellaneous");
                     if (insert_record("course_categories", $cat)) {
                         redirect("$CFG->wwwroot/$CFG->admin/index.php", get_string("changessaved"), 1);
index 55a59d18ceda06cce450866ee6e711440e1788d1..c8a1b757aeefd8035564b0dd940d05a1c6317d3f 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_activity_modules extends MoodleBlock {
-    function CourseBlock_activity_modules($course) {
+    function init() {
         $this->title = get_string('activities');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004041000;
     }
 
@@ -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[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->course->id.'">'.$modnamesplural[$modname].'</a>';
+                    $this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->instance->pageid.'">'.$modnamesplural[$modname].'</a>';
                     $this->content->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" height="16" width="16" alt="" />';
                 }
             }
index 4e3a615e3145fcaf06316b5f3b9ded508be2bc81..a7a6405c303ff2fe4902d36ae429bf48bc8b6b3f 100644 (file)
@@ -1,10 +1,9 @@
 <?php //$Id$
 
 class CourseBlock_admin extends MoodleBlock {
-    function CourseBlock_admin($course) {
+    function init() {
         $this->title = get_string('administration');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004081200;
     }
 
@@ -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[]='<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" />';
-                if (isediting($this->course->id)) {
-                    $this->content->items[]='<a href="view.php?id='.$this->course->id.'&amp;edit=off">'.get_string('turneditingoff').'</a>';
+                if (isediting($this->instance->pageid)) {
+                    $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=off">'.get_string('turneditingoff').'</a>';
                 } else {
-                    $this->content->items[]='<a href="view.php?id='.$this->course->id.'&amp;edit=on">'.get_string('turneditingon').'</a>';
+                    $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=on">'.get_string('turneditingon').'</a>';
                 }
-                $this->content->items[]='<a href="edit.php?id='.$this->course->id.'">'.get_string('settings').'...</a>';
+                $this->content->items[]='<a href="edit.php?id='.$this->instance->pageid.'">'.get_string('settings').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/settings.gif" height="16" width="16" alt="" />';
 
-                if (iscreator() or !empty($CFG->teacherassignteachers)) {
-                    if (!$this->course->teachers) {
-                        $this->course->teachers = get_string('defaultcourseteachers');
+                if (iscreator() || !empty($CFG->teacherassignteachers)) {
+                    if (!$course->teachers) {
+                        $course->teachers = get_string('defaultcourseteachers');
                     }
-                    $this->content->items[]='<a href="teacher.php?id='.$this->course->id.'">'.$this->course->teachers.'...</a>';
+                    $this->content->items[]='<a href="teacher.php?id='.$this->instance->pageid.'">'.$course->teachers.'...</a>';
                     $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
                 }
 
-                if (!$this->course->students) {
-                    $this->course->students = get_string('defaultcoursestudents');
+                if (!$course->students) {
+                    $course->students = get_string('defaultcoursestudents');
                 }
-                $this->content->items[]='<a href="student.php?id='.$this->course->id.'">'.$this->course->students.'...</a>';
+                $this->content->items[]='<a href="student.php?id='.$this->instance->pageid.'">'.$course->students.'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
 
-                $this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->course->id.'">'.get_string('backup').'...</a>';
+                $this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->instance->pageid.'">'.get_string('backup').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" height="16" width="16" alt="" />';
 
-                $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'&amp;wdir=/backupdata">'.get_string('restore').'...</a>';
+                $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'&amp;wdir=/backupdata">'.get_string('restore').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" height="16" width="16" alt="" />';
-                $this->content->items[]='<a href="scales.php?id='.$this->course->id.'">'.get_string('scales').'...</a>';
+                $this->content->items[]='<a href="scales.php?id='.$this->instance->pageid.'">'.get_string('scales').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/scales.gif" height="16" width="16" alt="" />';
             }
 
-            $this->content->items[]='<a href="grades.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
+            $this->content->items[]='<a href="grades.php?id='.$this->instance->pageid.'">'.get_string('grades').'...</a>';
             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="" />';
 
-            $this->content->items[]='<a href="log.php?id='.$this->course->id.'">'.get_string('logs').'...</a>';
+            $this->content->items[]='<a href="log.php?id='.$this->instance->pageid.'">'.get_string('logs').'...</a>';
             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/log.gif" height="16" width="16" alt="" />';
 
             if ($isteacheredit) {
-                $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'">'.get_string('files').'...</a>';
+                $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'">'.get_string('files').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" height="16" width="16" alt="" />';
             }
 
-            $this->content->items[]='<a href="'.$CFG->wwwroot.'/doc/view.php?id='.$this->course->id.'&amp;file=teacher.html">'.get_string('help').'...</a>';
+            $this->content->items[]='<a href="'.$CFG->wwwroot.'/doc/view.php?id='.$this->instance->pageid.'&amp;file=teacher.html">'.get_string('help').'...</a>';
             $this->content->icons[]='<img src="'.$CFG->modpixpath.'/resource/icon.gif" height="16" width="16" alt="" />';
 
-            if ($teacherforum = forum_get_course_forum($this->course->id, 'teacher')) {
+            if ($teacherforum = forum_get_course_forum($this->instance->pageid, 'teacher')) {
                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$teacherforum->id.'">'.get_string('nameteacher', 'forum').'</a>';
                 $this->content->icons[]='<img src="'.$CFG->modpixpath.'/forum/icon.gif" height="16" width="16" alt="" />';
             }
 
         } else if (!isguest()) {  // Students menu
 
-            if ($this->course->showgrades) {
-                $this->content->items[]='<a href="grade.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
+            if ($course->showgrades) {
+                $this->content->items[]='<a href="grade.php?id='.$this->instance->pageid.'">'.get_string('grades').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="" />';
             }
-            if ($this->course->showreports) {
-                $this->content->items[]='<a href="user.php?id='.$this->course->id.'&amp;user='.$USER->id.'">'.get_string('activityreport').'...</a>';
+            if ($course->showreports) {
+                $this->content->items[]='<a href="user.php?id='.$this->instance->pageid.'&amp;user='.$USER->id.'">'.get_string('activityreport').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/report.gif" height="16" width="16" alt="" />';
             }
             if (is_internal_auth()) {
-                $this->content->items[]='<a href="'.$CFG->wwwroot.'/login/change_password.php?id='.$this->course->id.'">'.get_string('changepassword').'...</a>';
+                $this->content->items[]='<a href="'.$CFG->wwwroot.'/login/change_password.php?id='.$this->instance->pageid.'">'.get_string('changepassword').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
             } else if ($CFG->changepassword) {
                 $this->content->items[]='<a href="'.$CFG->changepassword.'">'.get_string('changepassword').'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
             }
             if ($CFG->allowunenroll) {
-                $this->content->items[]='<a href="unenrol.php?id='.$this->course->id.'">'.get_string('unenrolme', '', $this->course->shortname).'...</a>';
+                $this->content->items[]='<a href="unenrol.php?id='.$this->instance->pageid.'">'.get_string('unenrolme', '', $course->shortname).'...</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="" />';
             }
         }
index 130409f20987332d57740da508a6420f8b8245a4..defb8bdc8d2726f2903ed2d22869e173c15ee924 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_calendar_month extends MoodleBlock {
-    function CourseBlock_calendar_month($course) {
+    function init() {
         $this->title = get_string('calendar', 'calendar');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004081200;
     }
 
@@ -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;
index 6b77a8c8167e2ec6dbfcaa0f55798052d5d933ee..15dff0111e45898480e40b0cc3adf543f22f5ff8 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_calendar_upcoming extends MoodleBlock {
-    function CourseBlock_calendar_upcoming ($course) {
+    function init() {
         $this->title = get_string('upcomingevents', 'calendar');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004052600;
     }
 
@@ -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 = '<br /><a href="'.$CFG->wwwroot.
-                                     '/calendar/view.php?view=upcoming&amp;course='.$this->course->id.'">'.
+                                     '/calendar/view.php?view=upcoming&amp;course='.$courseshown.'">'.
                                       get_string('gotocalendar', 'calendar').'</a>...';
             $this->content->footer .= '<br /><a href="'.$CFG->wwwroot.
-                                      '/calendar/event.php?action=new&amp;course='.$this->course->id.'">'.
+                                      '/calendar/event.php?action=new&amp;course='.$courseshown.'">'.
                                        get_string('newevent', 'calendar').'</a>...';
             
-            if ($this->course->id == SITEID) {
+            if ($courseshown == SITEID) {
                 // Being displayed at site level. This will cause the filter to fall back to auto-detecting
                 // the list of courses it will be grabbing events from.
                 $filtercourse = NULL;
@@ -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&amp;course='.$this->course->id.'&amp;');
+                                   'view.php?view=day&amp;course='.$courseshown.'&amp;');
         }
 
         if (empty($this->content->text)) {
index f93c8e58e8dde31b18938fb8c36366d385f3abb8..cda9e3e8d7e76bd8e19b171d92e8abfab98d94da 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_course_list extends MoodleBlock {
-    function CourseBlock_course_list ($course) {
+    function init() {
         $this->title = get_string('courses');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004081200;
     }
     
@@ -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 = '';
index 143fa62a7bfa504b571e02e594f3c3608b89e7ca..9639735e38ef833702bbc10503a9dd4ed8471ba7 100644 (file)
@@ -1,17 +1,18 @@
 <?PHP //$Id$
 
 class CourseBlock_course_summary extends MoodleBlock {
-    function CourseBlock_course_summary ($course) {
-        if(!empty($course) && $course->id == SITEID) {   // Site level
-            $this->title = get_string('frontpagedescription');
-        } else {
-            $this->title = get_string('blockname','block_course_summary');
-        }
+    function init() {
+        $this->title = get_string('pagedescription', 'block_course_summary');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004052600;
     }
 
+    function specialization() {
+        if($this->instance->pagetype == MOODLE_PAGE_COURSE && $this->instance->pageid != SITEID) {
+            $this->title = get_string('coursesummary', 'block_course_summary');
+        }
+    }
+
     function get_content() {
         global $CFG, $THEME;
 
@@ -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 .= "<div align=\"right\"><a href=\"$editpage\"><img src=\"$CFG->pixpath/t/edit.gif\" alt=\"\" /></a></div>";
         }
index cdf3efb2b430579346f68a2fccbe3cd607dcee6b..1b573ee733eee0f19ed33f11bc66b96596d02db7 100644 (file)
@@ -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;
index 36f2bde8c1f22d5d7e8bd08c0d28a6ba2815cad8..fe9b44f1af5bd6890e645d94e02eb923003b665f 100644 (file)
@@ -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;
 # --------------------------------------------------------
index 174fcb194c1481e41ec9eed2bad803df9ca68920..ef29933a8f70abc49ab18c5dca69e8b4bdbe930a 100644 (file)
@@ -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;
index eccdc73f06cd0492ee6d72e7bd54993811cc10a5..c60694d3e4366926a31ac923854129ae88e1b6ac 100644 (file)
@@ -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 ''
+) ;
+      
 # --------------------------------------------------------
index 6e54061c6702deb2c720e5d0a52903d946c951dc..7919ad38549fc2bee39faa0badf03be57512cb7e 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_login extends MoodleBlock {
-    function CourseBlock_login ($course) {
+    function init() {
         $this->title = get_string('login');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004081600;
     }
 
index 69cd7190ac4a72e5eba4f64f57c49ad7a6ec89a7..2010bc4a7d4466eb70ce6806a9078b795c01c78b 100644 (file)
@@ -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, '&nbsp;', 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 = '<div style="float: right;">';
 
-        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 .= '<a style="margin-right: 6px; margin-left: 3px;" title="'.$title.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=toggle&amp;blockid='.$blockid.$sesskeystr.'">' .
-                        '<img src="'.$pixpath.$icon.'" alt="" /></a>';
+        $page = new stdClass;
+        $page->id   = $this->instance->pageid;
+        $page->type = $this->instance->pagetype;
+        $script = page_source_script($page);
+     
+        $movebuttons .= '<a style="margin-right: 6px; margin-left: 2px;" title="'.$title.'" href="'.$script.'&amp;blockaction=toggle&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                        '<img src="'.$pixpath.$icon.'" alt=\"\" /></a>';
+
+        if($options & BLOCK_CONFIGURE) {
+            $movebuttons .= '<a style="margin-right: 6px; margin-left: 2px;" title="'.$this->str->configure.'" href="'.$script.'&amp;blockaction=config&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                            '<img src="'.$pixpath.'/t/edit.gif" alt=\"\" /></a>';
+        }
 
-        $movebuttons .= '<a title="'.$this->str->delete.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=delete&amp;blockid='.$blockid.$sesskeystr.'">' .
-                        '<img src="'.$pixpath.'/t/delete.gif" alt="" /></a> ';
+        $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->delete.'" href="'.$script.'&amp;blockaction=delete&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                        '<img src="'.$pixpath.'/t/delete.gif" alt=\"\" /></a> ';
 
         if ($options & BLOCK_MOVE_LEFT) {
-            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveleft.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveside&amp;blockid='.$blockid.$sesskeystr.'">' .
-                            '<img src="'.$pixpath.'/t/left.gif" alt="" /></a>';
+            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveleft.'" href="'.$script.'&amp;blockaction=moveleft&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                            '<img src="'.$pixpath.'/t/left.gif" alt=\"\" /></a>';
         }
         if ($options & BLOCK_MOVE_UP) {
-            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveup.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveup&amp;blockid='.$blockid.$sesskeystr.'">' .
-                            '<img src="'.$pixpath.'/t/up.gif" alt="" /></a>';
+            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveup.'" href="'.$script.'&amp;blockaction=moveup&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                            '<img src="'.$pixpath.'/t/up.gif" alt=\"\" /></a>';
         }
         if ($options & BLOCK_MOVE_DOWN) {
-            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->movedown.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=movedown&amp;blockid='.$blockid.$sesskeystr.'">' .
-                            '<img src="'.$pixpath.'/t/down.gif" alt="" /></a>';
+            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->movedown.'" href="'.$script.'&amp;blockaction=movedown&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                            '<img src="'.$pixpath.'/t/down.gif" alt=\"\" /></a>';
         }
         if ($options & BLOCK_MOVE_RIGHT) {
-            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveright.'" href="'.$path.'/view.php?id='.$this->course->id.'&amp;blockaction=moveside&amp;blockid='.$blockid.$sesskeystr.'">' .
-                            '<img src="'.$pixpath.'/t/right.gif" alt="" /></a>';
+            $movebuttons .= '<a style="margin-right: 2px; margin-left: 2px;" title="'.$this->str->moveright.'" href="'.$script.'&amp;blockaction=moveright&amp;instanceid='.$this->instance->id.$sesskeystr.'">' .
+                            '<img src="'.$pixpath.'/t/right.gif" alt=\"\" /></a>';
         }
 
         $movebuttons .= '</div>';
@@ -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 {
index 1d4869ef420114aa7b781b7ef6a0f93bcca77726..b896f2d750186459dff2a1b7ce9818d787ccd91d 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_news_items extends MoodleBlock {
-    function CourseBlock_news_items ($course) {
+    function init() {
         $this->title = get_string('latestnews');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004052600;
     }
 
@@ -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();
         }
index 1c3e31a541609d3f28ec58c12dee38bee8dfb8c5..c9e11f4605cfac3d21565446e2d22e3604b99d19 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_online_users extends MoodleBlock {
-    function CourseBlock_online_users ($course) {
+    function init() {
         $this->title = get_string('blockname','block_online_users');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004052700;
     }
 
@@ -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 .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->course->id.'" title="'.$timeago.'">'.$user->fullname.'</a></div>';
+                $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->instance->pageid.'" title="'.$timeago.'">'.$user->fullname.'</a></div>';
             }
 /*
                 $table->align = array("right","left");
index 0a85027eeac167f394dc6df83bf0b4e8ff372f4d..02e181bc96addc633eb34d3f4cb9f8d530c7d269 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_participants extends MoodleBlock {
-    function CourseBlock_participants ($course) {
+    function init() {
         $this->title = get_string('people');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004052600;
     }
 
@@ -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[]='<a title="'.get_string('listofallpeople').'" href="'.$CFG->wwwroot.'/user/index.php?id='.$this->course->id.'">'.get_string('participants').'</a>';
+        $course = get_record('course', 'id', $this->instance->pageid);
+
+        if ($this->instance->pageid != SITEID || $CFG->showsiteparticipantslist > 1 || ($CFG->showsiteparticipantslist == 1 && isteacher()) || isteacher(SITEID)) {
+            $this->content->items[]='<a title="'.get_string('listofallpeople').'" href="'.$CFG->wwwroot.'/user/index.php?id='.$this->instance->pageid.'">'.get_string('participants').'</a>';
             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="" />';
         }
 
-        if ($this->course->groupmode or !$this->course->groupmodeforce) {
-            if ($this->course->groupmode == VISIBLEGROUPS or isteacheredit($this->course->id)) {
-                $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/course/groups.php?id='.$this->course->id.'">'.$strgroups.'</a>';
+        if ($course->groupmode || !$course->groupmodeforce) {
+            if ($course->groupmode == VISIBLEGROUPS or isteacheredit($this->instance->pageid)) {
+                $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/course/groups.php?id='.$this->instance->pageid.'">'.$strgroups.'</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" height="16" width="16" alt="" />';
-            } else if ($this->course->groupmode == SEPARATEGROUPS and $this->course->groupmodeforce) {
+            } else if ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce) {
                 // Show nothing
-            } else if ($currentgroup = get_current_group($this->course->id)) {
-                $this->content->items[]='<a title="'.$strgroupmy.'" href="'.$CFG->wwwroot.'/course/group.php?id='.$this->course->id.'">'.$strgroupmy.'</a>';
+            } else if ($currentgroup = get_current_group($this->instance->pageid)) {
+                $this->content->items[]='<a title="'.$strgroupmy.'" href="'.$CFG->wwwroot.'/course/group.php?id='.$this->instance->pageid.'">'.$strgroupmy.'</a>';
                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" height="16" width="16" alt="" />';
             }
         }
 
         if (!empty($USER->id)) {
             $fullname = fullname($USER, true);
-            $editmyprofile = '<a title="'.$fullname.'" href="'.$CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.$this->course->id.'">'.get_string('editmyprofile').'</a>';
+            $editmyprofile = '<a title="'.$fullname.'" href="'.$CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.$this->instance->pageid.'">'.get_string('editmyprofile').'</a>';
             if ($USER->description) {
                 $this->content->items[]= $editmyprofile;
             } else {
index 03ee7a3767bc6720ea68f1a19bc15bb528125756..4a64ddbe6248c915268df0b53dc85a788639f5f0 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_recent_activity extends MoodleBlock {
-    function CourseBlock_recent_activity ($course) {
+    function init() {
         $this->title = get_string('recentactivity');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004042900;
     }
 
@@ -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();
 
index aee0be0b4658e85f649a897e8257583a876da86d..75c78fdb694cc8aec4ce1eedbb3fd8d6b8b03c42 100644 (file)
@@ -1,10 +1,9 @@
 <?PHP //$Id$
 
 class CourseBlock_search_forums extends MoodleBlock {
-    function CourseBlock_search_forums ($course) {
+    function init() {
         $this->title = get_string('search', 'forum');
         $this->content_type = BLOCK_TYPE_TEXT;
-        $this->course = $course;
         $this->version = 2004041000;
     }
 
@@ -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 = '<div align="center">'.$form.'</div>';
 
         return $this->content;
index 5ada326d7a3739297488c46210e3a81b16a60c5a..ec4dfc844966cbefaf400f9058204179a1031172 100644 (file)
@@ -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&amp;$sectionname=";
+            $link = "$CFG->wwwroot/course/view.php?id=$this->instance->pageid&amp;$sectionname=";
         } else {
             $link = '#';
         }
         $text = '<font size="-1">';
-        for ($i = $inc; $i <= $this->course->numsections; $i += $inc) {
-            $isvisible = get_field('course_sections', 'visible', 'course', $this->course->id, 'section', $i);
-            if (!$isvisible and !isteacher($this->course->id)) {
+        for ($i = $inc; $i <= $course->numsections; $i += $inc) {
+            $isvisible = get_field('course_sections', 'visible', 'course', $this->instance->pageid, 'section', $i);
+            if (!$isvisible and !isteacher($this->instance->pageid)) {
                 continue;
             }
             $style = ($isvisible) ? '' : ' class="dimmed"';
@@ -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 .= "<br /><a href=\"$link$highlight\"$style>$linktext</a>";
             }
index de6d283236a9ccb316d035716ded4b0831c7c6b0..72bd588cf7da7f99604134bb230fea0f718458b0 100644 (file)
@@ -1,10 +1,9 @@
 <?php   //$Id$
 
 class CourseBlock_site_main_menu extends MoodleBlock {
-    function CourseBlock_site_main_menu ($course) {
+    function init() {
         $this->title = get_string('mainmenu');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004052700;
     }
 
@@ -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;
+        }
+
+        $course = get_record('course', 'id', $this->instance->pageid);
+
+        $isteacher = isteacher($this->instance->pageid);
+        $isediting = isediting($this->instance->pageid);
+        $ismoving  = ismoving($this->instance->pageid);
 
-        $sections = get_all_sections($this->course->id);
+        $sections = get_all_sections($this->instance->pageid);
         $section = $sections[0];
 
         if($section->sequence || $isediting) {
-            get_all_mods($this->course->id, $mods, $modnames, $modnamesplural, $modnamesused);
+            get_all_mods($this->instance->pageid, $mods, $modnames, $modnamesplural, $modnamesused);
         }
 
-        $groupbuttons = $this->course->groupmode;
-        $groupbuttonslink = (!$this->course->groupmodeforce);
+        $groupbuttons = $course->groupmode;
+        $groupbuttonslink = (!$course->groupmodeforce);
 
         if ($ismoving) {
             $strmovehere = get_string('movehere');
@@ -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('<nolink>'.$instancename.'</nolink>', $this->course->id);
+                        $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->instance->pageid);
                     }
                     $linkcss = $mod->visible ? '' : ' class="dimmed" ';
                     if (!empty($modinfo[$modnumber]->extra)) {
@@ -122,7 +123,7 @@ class CourseBlock_site_main_menu extends MoodleBlock {
 
         if ($isediting && $modnames) {
             $this->content->footer = '<div style="text-align: right;">'.
-                print_section_add_menus($this->course, 0, $modnames, true, true).'</div>';
+                print_section_add_menus($course, 0, $modnames, true, true).'</div>';
         } else {
             $this->content->footer = '';
         }
index b0a182eaa875e4b48efe0879d2cb9681346fc088..e5c562a67c9d231d7f566b2a968f825d25924d56 100644 (file)
@@ -1,10 +1,9 @@
 <?php //$Id$
 
 class CourseBlock_social_activities extends MoodleBlock {
-    function CourseBlock_social_activities ($course) {
+    function init(){
         $this->title = get_string('blockname','block_social_activities');
         $this->content_type = BLOCK_TYPE_LIST;
-        $this->course = $course;
         $this->version = 2004041800;
     }
 
@@ -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('<nolink>'.$instancename.'</nolink>', $this->course->id);
+                        $instancename = filter_text('<nolink>'.$instancename.'</nolink>', $this->instance->pageid);
                     }
                     $linkcss = $mod->visible ? '' : ' class="dimmed" ';
                     if (!empty($modinfo[$modnumber]->extra)) {
@@ -118,7 +120,7 @@ class CourseBlock_social_activities extends MoodleBlock {
 
         if ($isediting && $modnames) {
             $this->content->footer = '<div style="text-align: right;">'.
-                print_section_add_menus($this->course, 0, $modnames, true, true).'</div>';
+                print_section_add_menus($course, 0, $modnames, true, true).'</div>';
         } else {
             $this->content->footer = '';
         }
index 05834435d26f6ec147b43c4cf14647dd517904e3..3e3151a6dde8835b78cdcfdf5a7b7d2b068fea22 100644 (file)
@@ -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)
index 400fb72313fa8bcc5b4e36d2199ab0d7d77689ec..345a94d41bec759624c53b82325f18958ef3b012 100644 (file)
 
             if (!empty($course)) {
                 // Test for and remove blocks which aren't appropriate anymore
-                $form->blockinfo = $course->blockinfo;
-                block_remove_inappropriate_from_course($form);
+                $page = new stdClass;
+                $page->id   = $course->id;
+                $page->type = MOODLE_PAGE_COURSE;
+
+                blocks_remove_inappropriate($page);
 
                 // Update with the new data
                 if (update_record("course", $form)) {
             } else {
                 $form->timecreated = time();
 
-                //Create blockinfo default content
-                $form->blockinfo = blocks_get_default_blocks(null, blocks_get_config_default($form->format));
-
                 if ($newcourseid = insert_record("course", $form)) {  // Set up new course
+                    
+                    // Setup the blocks
+                    $page = new stdClass;
+                    $page->type = MOODLE_PAGE_COURSE;
+                    $page->id   = $newcourseid;
+                    blocks_repopulate_page($page); // Return value not checked because you can always edit later
+
                     $section = NULL;
                     $section->course = $newcourseid;   // Create a default section.
                     $section->section = 0;
index cc93c13ad143fc203b45e56d8c7a8a724367eb71..c10a254eb8cdd7c5750bb6d3b814a101ac01c071 100644 (file)
@@ -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 '<table width="100%" border="0" cellspacing="5" cellpadding="5">';
     echo '<tr>';
 
-    if(block_have_active($leftblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
-        print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
         echo '</td>';
     }
 
     }
     echo '</td>';
 
-    if(block_have_active($rightblocks) || $editing) {
-      echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
-      print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
-      if ($editing && !empty($missingblocks)) {
-          block_print_blocks_admin($course, $missingblocks);
-      }
-      print_spacer(1, 120, true);
-      echo '</td>';
+    // The right column
+    if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
+        echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
+        blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
+        if ($editing && !empty($missingblocks)) {
+            blocks_print_adminblock($page, $missingblocks);
+        }
+        echo '</td>';
     }
 
     echo '</tr>';
index 49e3ac5b81de144556e37e28ca6195334278f489..41316e7e53347f74dcf7ae31b33f066ed2ce4777 100644 (file)
@@ -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);
 
 /// The left column ...
 
-    if(block_have_active($leftblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
-        print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
         echo '</td>';
     }
 
 /// Start main column
-    echo "<td width=\"*\">";
+    echo '<td valign="top">';
 
     print_heading_block(get_string("topicoutline"), "100%", "outlineheadingblock");
     print_spacer(8, 1, true);
     echo "</td>";
 
     // The right column
-    if(block_have_active($rightblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
-        print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
         if ($editing && !empty($missingblocks)) {
-            block_print_blocks_admin($course, $missingblocks);
+            blocks_print_adminblock($page, $missingblocks);
         }
-        print_spacer(1, 120, true);
         echo '</td>';
     }
 
index be5b94a6ce4d8c1964b6e9e7d3056a9b672af77d..ef339d29b915902f8d656025fe78cbbfdcf14d34 100644 (file)
@@ -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);
 
 /// The left column ...
 
-    if(block_have_active($leftblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
-        print_course_blocks($course, $leftblocks, BLOCK_LEFT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
         echo '</td>';
     }
 
 /// Start main column
-    echo "</td><td width=\"*\">";
+    echo '<td valign="top">';
     print_heading_block(get_string("weeklyoutline"), "100%", "outlineheadingblock");
     print_spacer(8, 1, true);
 
     echo "</td>";
 
     // The right column
-    if(block_have_active($rightblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
-        print_course_blocks($course, $rightblocks, BLOCK_RIGHT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
         if ($editing && !empty($missingblocks)) {
-            block_print_blocks_admin($course, $missingblocks);
+            blocks_print_adminblock($page, $missingblocks);
         }
-        print_spacer(1, 120, true);
-       echo '</td>';
+        echo '</td>';
     }
 
     echo "</tr>\n";
index 4b9bb405261f0c39479d1c47b0512d95f900eb05..2f30c82d289f6bd94990088a9cdcadc2335290df 100644 (file)
@@ -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");
     }
         $course->format = 'weeks';  // Default format is weeks
     }
 
-    // Doing this now so we can pass the results to block_action()
-    // and dodge the overhead of doing the same work twice.
-
-    $blocks = $course->blockinfo;
-    $delimpos = strpos($blocks, ':');
-
-    if($delimpos === false) {
-        // No ':' found, we have all left blocks
-        $leftblocks = explode(',', $blocks);
-        $rightblocks = array();
-    }
-    else if($delimpos === 0) {
-        // ':' at start of string, we have all right blocks
-        $blocks = substr($blocks, 1);
-        $leftblocks = array();
-        $rightblocks = explode(',', $blocks);
-    }
-    else {
-        // Both left and right blocks
-        $leftpart = substr($blocks, 0, $delimpos);
-        $rightpart = substr($blocks, $delimpos + 1);
-        $leftblocks = explode(',', $leftpart);
-        $rightblocks = explode(',', $rightpart);
-    }
+    $page = new stdClass;
+    $page->id   = $course->id;
+    $page->type = MOODLE_PAGE_COURSE;
 
+    $pageblocks = blocks_get_by_page($page);
+   
     if (!isset($USER->editing)) {
         $USER->editing = false;
     }
 
         $editing = $USER->editing;
 
-        if (isset($hide) and confirm_sesskey()) {
+        if (isset($hide) && confirm_sesskey()) {
             set_section_visible($course->id, $hide, '0');
         }
 
-        if (isset($show) and confirm_sesskey()) {
+        if (isset($show) && confirm_sesskey()) {
             set_section_visible($course->id, $show, '1');
         }
 
-        if (isset($_GET['blockaction']) and confirm_sesskey()) {
-            if (isset($_GET['blockid'])) {
-                block_action($course, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
+        if (!empty($blockaction) && confirm_sesskey()) {
+            if (!empty($blockid)) {
+                blocks_execute_action($page, $pageblocks, strtolower($blockaction), intval($blockid));
+                
             }
-        }
-
-        // This has to happen after block_action() has possibly updated the two arrays
-        $allblocks = array_merge($leftblocks, $rightblocks);
-
-        $missingblocks = array();
-        $recblocks = get_records('blocks','visible','1');
-
-        // Note down which blocks are going to get displayed
-        blocks_used($allblocks, $recblocks);
-
-        if($editing && $recblocks) {
-            foreach($recblocks as $recblock) {
-                // If it's not hidden or displayed right now...
-                if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
-                    // And if it's applicable for display in this format...
-                    $formats = block_method_result($recblock->name, 'applicable_formats');
-
-                    if( isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all'])) {
-                        // Translation: if the course format is explicitly accepted/rejected, use
-                        // that setting. Otherwise, fallback to the 'all' format. The empty() test
-                        // uses the trick that empty() fails if 'all' is either !isset() or false.
-
-                        // Add it to the missing blocks
-                        $missingblocks[] = $recblock->id;
-                    }
-                }
+            else if (!empty($instanceid)) {
+                $instance = blocks_find_instance($instanceid, $pageblocks);
+                blocks_execute_action($page, $pageblocks, strtolower($blockaction), $instance);
             }
+            // This re-query could be eliminated by judicious programming in blocks_execute_action(),
+            // but I 'm not sure if it's worth the complexity increase...
+            $pageblocks = blocks_get_by_page($page);
         }
 
+        $missingblocks = blocks_get_missing($page, $pageblocks);
+
         if (!empty($section)) {
             if (!empty($move) and confirm_sesskey()) {
                 if (!move_section($course, $section, $move)) {
         }
     } else {
         $USER->editing = false;
-
-        // Note down which blocks are going to get displayed
-        $allblocks = array_merge($leftblocks, $rightblocks);
-        $recblocks = get_records('blocks','visible','1');
-        blocks_used($allblocks, $recblocks);
     }
 
     $SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
         }
     }
 
-    // If the block width cache is not set, set it
-    if(!isset($SESSION->blockcache->width->{$course->id}) || $editing) {
-
-        // This query might be optimized away if we 're in editing mode
-        if(!isset($recblocks)) {
-            $recblocks = get_records('blocks','visible','1');
-        }
-        $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
-        $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
-
-        // This may be kind of organizational overkill, granted...
-        // But is there any real need to simplify the structure?
-        $SESSION->blockcache->width->{$course->id}->left = $preferred_width_left;
-        $SESSION->blockcache->width->{$course->id}->right = $preferred_width_right;
-    }
-    else {
-        $preferred_width_left = $SESSION->blockcache->width->{$course->id}->left;
-        $preferred_width_right = $SESSION->blockcache->width->{$course->id}->right;
-    }
-
     require("$CFG->dirroot/course/format/$course->format/format.php");  // Include the actual course format
 
     print_footer(NULL, $course);
index a8ce03945eaafb34c06bf4a47207a932b131a052..5977b97db5d45b61a802ba2c2e0161023b56b1df 100644 (file)
--- a/index.php
+++ b/index.php
     require_once($CFG->dirroot.'/mod/resource/lib.php');
     require_once($CFG->dirroot.'/mod/forum/lib.php');
 
+    optional_param('blockaction');
+    optional_param('instanceid', 0, PARAM_INT);
+    optional_param('blockid',    0, PARAM_INT);
+
     if (! $site = get_site()) {
         redirect("$CFG->wwwroot/$CFG->admin/index.php");
     }
 
     $editing = isediting($site->id);
 
-    // Doing this now so we can pass the results to block_action()
-    // and dodge the overhead of doing the same work twice.
-
-    $blocks = $site->blockinfo;
-    $delimpos = strpos($blocks, ':');
+    $page = new stdClass;
+    $page->id   = SITEID;
+    $page->type = MOODLE_PAGE_COURSE;
 
-    if($delimpos === false) {
-        // No ':' found, we have all left blocks
-        $leftblocks = explode(',', $blocks);
-        $rightblocks = array();
-    }
-    else if($delimpos === 0) {
-        // ':' at start of string, we have all right blocks
-        $blocks = substr($blocks, 1);
-        $leftblocks = array();
-        $rightblocks = explode(',', $blocks);
-    }
-    else {
-        // Both left and right blocks
-        $leftpart = substr($blocks, 0, $delimpos);
-        $rightpart = substr($blocks, $delimpos + 1);
-        $leftblocks = explode(',', $leftpart);
-        $rightblocks = explode(',', $rightpart);
-    }
+    $pageblocks = blocks_get_by_page($page);
 
     if($editing) {
-        if (isset($_GET['blockaction'])) {
-            if (isset($_GET['blockid'])) {
-                block_action($site, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
+        if (!empty($blockaction) && confirm_sesskey()) {
+            if (!empty($blockid)) {
+                blocks_execute_action($page, $pageblocks, strtolower($blockaction), intval($blockid));
+                
             }
-        }
-
-        // This has to happen after block_action() has possibly updated the two arrays
-        $allblocks = array_merge($leftblocks, $rightblocks);
-
-        $missingblocks = array();
-        $recblocks = get_records('blocks','visible','1');
-
-        // Note down which blocks are going to get displayed
-        blocks_used($allblocks, $recblocks);
-
-        if($editing && $recblocks) {
-            foreach($recblocks as $recblock) {
-                // If it's not hidden or displayed right now...
-                if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
-                    // And if it's applicable for display in this format...
-                    $formats = block_method_result($recblock->name, 'applicable_formats');
-
-                    if( isset($formats['site']) ? $formats['site'] : !empty($formats['all'])) {
-                        // Translation: if the 'site' format is explicitly accepted/rejected, use
-                        // that setting. Otherwise, fallback to the 'all' format. The empty() test
-                        // uses the trick that empty() fails if 'all' is either !isset() or false.
-
-                        // Add it to the missing blocks
-                        $missingblocks[] = $recblock->id;
-                    }
-                }
+            else if (!empty($instanceid)) {
+                $instance = blocks_find_instance($instanceid, $pageblocks);
+                blocks_execute_action($page, $pageblocks, strtolower($blockaction), $instance);
             }
+            // This re-query could be eliminated by judicious programming in blocks_execute_action(),
+            // but I 'm not sure if it's worth the complexity increase...
+            $pageblocks = blocks_get_by_page($page);
         }
-    }
-    else {
-        // Note down which blocks are going to get displayed
-        $allblocks = array_merge($leftblocks, $rightblocks);
-        $recblocks = get_records('blocks','visible','1');
-        blocks_used($allblocks, $recblocks);
-    }
-
-    // If the block width cache is not set, set it
-    if(!isset($SESSION) or !isset($SESSION->blockcache) or 
-        !isset($SESSION->blockcache->width->{$site->id}) or $editing) {
-        // This query might be optimized away if we 're in editing mode
-        if(!isset($recblocks)) {
-            $recblocks = get_records('blocks','visible','1');
-        }
-        $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
-        $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
 
-        // This may be kind of organizational overkill, granted...
-        // But is there any real need to simplify the structure?
-        $SESSION->blockcache->width->{$site->id}->left = $preferred_width_left;
-        $SESSION->blockcache->width->{$site->id}->right = $preferred_width_right;
-    } else {
-        $preferred_width_left = $SESSION->blockcache->width->{$site->id}->left;
-        $preferred_width_right = $SESSION->blockcache->width->{$site->id}->right;
+        $missingblocks = blocks_get_missing($page, $pageblocks);
     }
 
+    optional_variable($preferred_width_left,  blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]));
+    optional_variable($preferred_width_right, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]));
     $preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
     $preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
     $preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
   <tr>
   <?PHP
 
-    if(block_have_active($leftblocks) || $editing) {
+    if(blocks_have_content($pageblocks[BLOCK_POS_LEFT]) || $editing) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
-        print_course_blocks($site, $leftblocks, BLOCK_LEFT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_LEFT]);
         echo '</td>';
     }
 
     }
 
     echo '</td>';
-    if(block_have_active($rightblocks) || $editing || isadmin()) {
+
+
+    // The right column
+    if(blocks_have_content($pageblocks[BLOCK_POS_RIGHT]) || $editing || isadmin()) {
         echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
         if (isadmin()) {
             echo '<div align="center">'.update_course_icon($site->id).'</div>';
             echo '<br />';
         }
-        print_course_blocks($site, $rightblocks, BLOCK_RIGHT);
+        blocks_print_group($page, $pageblocks[BLOCK_POS_RIGHT]);
         if ($editing && !empty($missingblocks)) {
-            block_print_blocks_admin($site, $missingblocks);
+            blocks_print_adminblock($page, $missingblocks);
         }
         echo '</td>';
     }
index 81418df9caa866947effa51df1e168403538c9e9..e53937efbfb2031d80e4963d36d8f60acfe6c874 100755 (executable)
@@ -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.  <a href=\"$a\">More information</a>';
index 3557c542d41d2a18547b7bb7102355690d17e586..5980a98fffdc4caa57885477480af93b8eb0d1a1 100644 (file)
@@ -1,5 +1,6 @@
 <?php // $Id$ 
 
-$string['blockname'] = 'Course Summary';
-$string['siteinfo'] = 'Site Information';
+$string['coursesummary'] = 'Course Summary';
+$string['pagedescription'] = 'Course/Site Description';
+
 ?>
index 6f33852ecc3f343baea25b34c6254b8f93056b61..bceb049e78ac5b3f8d97f443a5bfd7a8c6946527 100644 (file)
@@ -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';
index 4884b29f6934accc643e25881372702bc878fee7..9ee8e81db2171e09bd18a49355d1ae67c83b23df 100644 (file)
@@ -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();
+function page_get_format($page) {
+    switch($page->type) {
+        case MOODLE_PAGE_COURSE:
+            if($page->id == SITEID) {
+                return 'site';
+            }
+            else {
+                $course = get_record('course', 'id', $page->id);
+                return $course->format;
+            }
+        break;
     }
-    else if($delimpos === 0) {
-        // ':' at start of string, we have all right blocks
-        $blocks = substr($blocks, 1);
-        $leftblocks = array();
-        $rightblocks = explode(',', $blocks);
+    return NULL;
+}
+
+function blocks_get_missing($page, $pageblocks) {
+    $missingblocks = array();
+    $allblocks = blocks_get_record();
+
+    if(!empty($allblocks)) {
+        foreach($allblocks as $block) {
+            if($block->visible && (!blocks_find_block($block->id, $pageblocks) || $block->multiple)) {
+                // And if it's applicable for display in this format...
+                $formats = block_method_result($block->name, 'applicable_formats');
+                $pageformat = page_get_format($page);
+                if(isset($formats[$pageformat]) ? $formats[$pageformat] : !empty($formats['all'])) {
+                    // Add it to the missing blocks
+                    $missingblocks[] = $block->id;
+                }
+            }
+        }
     }
-    else {
-        // Both left and right blocks
-        $leftpart = substr($blocks, 0, $delimpos);
-        $rightpart = substr($blocks, $delimpos + 1);
-        $leftblocks = explode(',', $leftpart);
-        $rightblocks = explode(',', $rightpart);
+    return $missingblocks;
+}
+
+function blocks_remove_inappropriate($page) {
+    $pageblocks = blocks_get_by_page($page);
+
+    if(empty($pageblocks)) {
+        return;
     }
 
-    $allblocks = get_records('blocks');
-    if(!empty($leftblocks)) {
-        foreach($leftblocks as $key => $id) {
-            $positiveid = abs($id);
-            $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats');
-            if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) {
-                unset($leftblocks[$key]);
+    switch($page->type) {
+        case MOODLE_PAGE_COURSE:
+            $course = get_record('course', 'id', $page->id);
+            if($page->id == SITEID) {
+                $pageformat = 'site';
             }
-        }
+            else {
+                $pageformat = $course->format;
+            }
+        break;
+        default:
+            return;
+        break;
     }
-    if(!empty($rightblocks)) {
-        foreach($rightblocks as $key => $id) {
-            $positiveid = abs($id);
-            $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats');
-            if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) {
-                unset($rightblocks[$key]);
+
+    foreach($pageblocks as $position) {
+        foreach($position as $instance) {
+            $block = blocks_get_record($instance->blockid);
+            $formats = block_method_result($block->name, 'applicable_formats');
+            if(! (isset($formats[$pageformat]) ? $formats[$pageformat] : !empty($formats['all']))) {
+                // Translation: if the course format is explicitly accepted/rejected, use
+                // that setting. Otherwise, fallback to the 'all' format. The empty() test
+                // uses the trick that empty() fails if 'all' is either !isset() or false.
+
+                blocks_delete_instance($instance);
             }
         }
     }
+}
 
-    $course->blockinfo = implode(',', $leftblocks);
-    if(!empty($rightblocks)) {
-        $course->blockinfo .= ':' . implode(',', $rightblocks);
-    }
+function blocks_delete_instance($instance) {
+    global $CFG;
+
+    delete_records('block_instance', 'id', $instance->id);
+    // And now, decrement the weight of all blocks after this one
+    execute_sql('UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+                '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+                '\' AND weight > '.$instance->weight, false);
 }
 
 // Returns the case-sensitive name of the class' constructor function. This includes both
@@ -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;
-
-    $isediting = isediting($course->id);
-    $ismoving = ismoving($course->id);
-    $isteacheredit = isteacheredit($course->id);
-
-    if(!empty($blocksarray)) {
-        // Include the base class
-        @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
-        if(!class_exists('moodleblock')) {
-            error('Class MoodleBlock is not defined or file not found for /course/blocks/moodleblock.class.php');
-        }
-
-        $blockdata = get_records('blocks', 'visible', 1);
-        if($blockdata !== false) {
+function blocks_print_group($page, $instances) {
+    
+    if(empty($instances)) {
+        return;
+    }
 
-            $lastblock = end($blocksarray);
-            $firstblock = reset($blocksarray);
+    switch($page->type) {
+        case MOODLE_PAGE_COURSE:
+            $isediting     = isediting($page->id);
+            $ismoving      = ismoving($page->id);
+            $isteacheredit = isteacheredit($page->id);
+        break;
+    }
 
-            foreach($blocksarray as $blockid) {
-                if(!isset($blockdata[abs($blockid)])) {
-                    // This block is hidden. Don't show it.
-                    continue;
-                }
+    // Include the base class
+    @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
+    if(!class_exists('moodleblock')) {
+        error('Class MoodleBlock is not defined or file not found for /course/blocks/moodleblock.class.php');
+    }
 
-                $blockname = $blockdata[abs($blockid)]->name;
-                $block = block_instance($blockname, $course);
-                if($block === false) {
-                    // Something went wrong
-                    continue;
-                }
+    $maxweight = max(array_keys($instances));
 
-                // There are various sanity checks commented out below
-                // because the block detection code should have already done them long ago.
+    foreach($instances as $instance) {
+        $block = blocks_get_record($instance->blockid);
+        if(!$block->visible) {
+            // Disabled by the admin
+            continue;
+        }
 
-                /*
-                if(!is_subclass_of($block, 'MoodleBlock')) {
-                    // Error: you have to derive from MoodleBlock
-                    continue;
-                }
+        $obj = block_instance($block->name, $instance);
 
-                if($content === NULL || $title === NULL) {
-                    // Error: This shouldn't have happened
-                    continue;
-                }
-                */
-                if ($isediting && !$ismoving && $isteacheredit) {
-                    $options = 0;
-                    $options |= BLOCK_MOVE_UP * ($blockid != $firstblock);
-                    $options |= BLOCK_MOVE_DOWN * ($blockid != $lastblock);
-                    $options |= ($side == BLOCK_LEFT) ? BLOCK_MOVE_RIGHT : BLOCK_MOVE_LEFT;
-                    $block->add_edit_controls($options, $blockid);
-                }
+        if ($isediting && !$ismoving && $isteacheredit) {
+            $options = 0;
+            $options |= BLOCK_MOVE_UP * ($instance->weight != 0);
+            $options |= BLOCK_MOVE_DOWN * ($instance->weight != $maxweight);
+            $options |= BLOCK_MOVE_RIGHT * ($instance->position != BLOCK_POS_RIGHT);
+            $options |= BLOCK_MOVE_LEFT * ($instance->position != BLOCK_POS_LEFT);
+            $options |= BLOCK_CONFIGURE * ($block->multiple);
+            $obj->add_edit_controls($options);
+        }
 
-                if($blockid < 0) {
-                    // We won't print this block...
-                    if($isediting) {
-                        // Unless we 're in editing mode, in which case we 'll print a 'shadow'
-                        $block->print_shadow();
-                    }
-                    continue;
-                }
-                // So simple...
-                $block->print_block();
+        if(!$instance->visible) {
+            if($isediting) {
+                $obj->print_shadow();
             }
         }
+        else {
+            $obj->print_block();
+        }
     }
 }
 
 //This iterates over an array of blocks and calculates the preferred width
-function blocks_preferred_width($blockarray, $blockinfos) {
+function blocks_preferred_width($instances) {
     $width = 0;
 
-    if(!is_array($blockarray) || empty($blockarray)) {
+    if(empty($instances) || !is_array($instances)) {
         return 0;
     }
-    foreach($blockarray as $blockid) {
-        if($blockid < 0) {
-            // Invisible block
+    foreach($instances as $instance) {
+        if(!$instance->visible) {
             continue;
         }
-        if(isset($blockinfos[$blockid])) {
-            $blockname = $blockinfos[$blockid]->name;
-            $pref = block_method_result($blockname, 'preferred_width');
-            if($pref === NULL) {
-                continue;
-            }
-            if($pref > $width) {
-                $width = $pref;
-            }
+        $block = blocks_get_record($instance->blockid);
+        $pref = block_method_result($block->name, 'preferred_width');
+        if($pref === NULL) {
+            continue;
+        }
+        if($pref > $width) {
+            $width = $pref;
         }
     }
     return $width;
 }
 
+function blocks_get_record($blockid = NULL, $invalidate = false) {
+    static $cache = NULL;
 
-// $course passed by reference for speed
-// $leftblocks, $rightblocks passed by reference because block_action() needs to
-// update the arrays so that the change can be shown immediately.
+    if($invalidate || empty($cache)) {
+        $cache = get_records('block');
+    }
 
-function block_action(&$course, &$leftblocks, &$rightblocks, $blockaction, $blockid) {
+    if($blockid === NULL) {
+        return $cache;
+    }
 
-    $blockid = abs(intval($blockid)); // Just to make sure
+    return (isset($cache[$blockid])? $cache[$blockid] : false);
+}
+
+function blocks_find_block($blockid, $blocksarray) {
+    foreach($blocksarray as $blockgroup) {
+        foreach($blockgroup as $instance) {
+            if($instance->blockid == $blockid) {
+                return $instance;
+            }
+        }
+    }
+    return false;
+}
+
+function blocks_find_instance($instanceid, $blocksarray) {
+    foreach($blocksarray as $subarray) {
+        foreach($subarray as $instance) {
+            if($instance->id == $instanceid) {
+                return $instance;
+            }
+        }
+    }
+    return false;
+}
+
+function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid) {
+    global $CFG;
+
+    if(is_int($instanceorid)) {
+        $blockid = $instanceorid;
+    }
+    else if(is_object($instanceorid)) {
+        $instance = $instanceorid;
+    }
 
     switch($blockaction) {
-        case 'toggle':
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block !== false) {
-                if($block->side == BLOCK_LEFT) {
-                    $leftblocks[$block->position] = -$leftblocks[$block->position];
+        case 'config':
+            // Series of ugly hacks following...
+            global $course, $USER; // First hack; we need $course to print out the headers
+            $block = blocks_get_record($instance->blockid);
+            $blockobject = block_instance($block->name, $instance);
+            if ($blockobject === false) {
+                continue;
+            }
+            optional_param('submitted', 0, PARAM_INT);
+
+            // Define the data we 're going to silently include in the instance config form here,
+            // so we can strip them from the submitted data BEFORE serializing it.
+            $hiddendata = array(
+                'sesskey' => $USER->sesskey,
+                'id' => $course->id,
+                'instanceid' => $instance->id,
+                'blockaction' => 'config'
+            );
+            // The 'id' thing is a crude hack in all its glory...
+            // Redirecting the form submission back to ourself with qualified_me() was a good idea since otherwise
+            // we 'd need to have an "extra" script that would have to infer where to redirect us back just from
+            // the data in $instance (pagetype and pageid). But, "ourself" is most likely course/view.php and it needs
+            // a course id. Hence the hack.
+
+            if($data = data_submitted()) {
+                $remove = array_keys($hiddendata);
+                foreach($remove as $item) {
+                    unset($data->$item);
                 }
-                else {
-                    $rightblocks[$block->position] = -$rightblocks[$block->position];
+                if(!$blockobject->instance_config_save($data)) {
+                    error('Error saving block configuration');
                 }
+                // And nothing more, continue with displaying the page
             }
-        break;
-        case 'delete':
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block !== false) {
-                if($block->side == BLOCK_LEFT) {
-                    unset($leftblocks[$block->position]);
-                }
-                else {
-                    unset($rightblocks[$block->position]);
+            else {
+                $loggedinas = "<p class=\"logininfo\">".user_login_string($course, $USER)."</p>";
+                print_header(get_string('blockconfigin', 'moodle', $course->fullname), $course->fullname, $course->shortname,
+                     "", "", true, update_course_icon($course->id), $loggedinas);
+                print_heading(get_string('blockconfiga', 'moodle', $block->name));
+                echo '<form method="post" action="'.strip_querystring(qualified_me()).'">'; // This I wouldn't call a hack but it sure looks cheeky
+                echo '<p>';
+                foreach($hiddendata as $name => $val) {
+                    echo '<input type="hidden" name="'.$name.'" value="'.$val.'" />';
                 }
+                echo '</p>';
+                $blockobject->instance_config_print();
+                echo '</form>';
+                print_footer();
+                die(); // Do not go on with the other course-related stuff
             }
         break;
-        case 'add':
-            // Toggle to enabled, or add it if it doesn't exist at all
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block === false) {
-                // It doesn't exist at all, so add it
-                $rightblocks[] = $blockid;
+        case 'toggle':
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
             }
-            else if($block->enabled == false) {
-                // Enable it
-                if($block->side == BLOCK_LEFT) {
-                    $leftblocks[$block->position] = -$leftblocks[$block->position];
-                }
-                else {
-                    $rightblocks[$block->position] = -$rightblocks[$block->position];
-                }
+            $instance->visible = ($instance->visible) ? 0 : 1;
+            update_record('block_instance', $instance);
+        break;
+        case 'delete':
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
             }
+            blocks_delete_instance($instance);
         break;
         case 'moveup':
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block !== false) {
-                if($block->side == BLOCK_LEFT) {
-                    if(isset($leftblocks[$block->position - 1])) {
-                        // We can move it upwards
-                        $oldblock = $leftblocks[$block->position - 1];
-                        $leftblocks[$block->position - 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
-                        $leftblocks[$block->position] = $oldblock;
-                    }
-                }
-                else {
-                    if(isset($rightblocks[$block->position - 1])) {
-                        // We can move it upwards
-                        $oldblock = $rightblocks[$block->position - 1];
-                        $rightblocks[$block->position - 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
-                        $rightblocks[$block->position] = $oldblock;
-                    }
-                }
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
+            }
+            $other = $pageblocks[$instance->position][$instance->weight - 1];
+            if(!empty($other)) {
+                --$instance->weight;
+                ++$other->weight;
+                update_record('block_instance', $instance);
+                update_record('block_instance', $other);
             }
         break;
         case 'movedown':
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block !== false) {
-                if($block->side == BLOCK_LEFT) {
-                    if(isset($leftblocks[$block->position + 1])) {
-                        // We can move it downwards
-                        $oldblock = $leftblocks[$block->position + 1];
-                        $leftblocks[$block->position + 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
-                        $leftblocks[$block->position] = $oldblock;
-                    }
-                }
-                else {
-                    if(isset($rightblocks[$block->position + 1])) {
-                        // We can move it downwards
-                        $oldblock = $rightblocks[$block->position + 1];
-                        $rightblocks[$block->position + 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
-                        $rightblocks[$block->position] = $oldblock;
-                    }
-                }
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
+            }
+            $other = $pageblocks[$instance->position][$instance->weight + 1];
+            if(!empty($other)) {
+                ++$instance->weight;
+                --$other->weight;
+                update_record('block_instance', $instance);
+                update_record('block_instance', $other);
             }
+
         break;
-        case 'moveside':
-            $block = block_find($blockid, $leftblocks, $rightblocks);
-            if($block !== false) {
-                if($block->side == BLOCK_LEFT) {
-                    unset($leftblocks[$block->position]);
-                    $rightblocks[] = $block->enabled ? $blockid : -$blockid;
-                }
-                else {
-                    unset($rightblocks[$block->position]);
-                    $leftblocks[] = $block->enabled ? $blockid : -$blockid;
-                }
+        case 'moveleft':
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
+            }
+            $sql = '';
+            switch($instance->position) {
+                case BLOCK_POS_RIGHT:
+                    // To preserve the continuity of block weights
+                    $sql = 'UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+                           '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+                           '\' AND weight > '.$instance->weight;
+
+                    $instance->position = BLOCK_POS_LEFT;
+                    $maxweight = max(array_keys($pageblocks[$instance->position]));
+                    $instance->weight = $maxweight + 1;
+                break;
+            }
+            if($sql) {
+                update_record('block_instance', $instance);
+                execute_sql($sql, false);
             }
         break;
-    }
+        case 'moveright':
+            if(empty($instance))  {
+                error('Invalid block instance for '.$blockaction);
+            }
+            $sql = '';
+            switch($instance->position) {
+                case BLOCK_POS_LEFT:
+                    // To preserve the continuity of block weights
+                    $sql = 'UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
+                           '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
+                           '\' AND weight > '.$instance->weight;
+
+                    $instance->position = BLOCK_POS_RIGHT;
+                    $maxweight = max(array_keys($pageblocks[$instance->position]));
+                    $instance->weight = $maxweight + 1;
+                break;
+            }
+            if($sql) {
+                update_record('block_instance', $instance);
+                execute_sql($sql, false);
+            }
+        break;
+        case 'add':
+            // Add a new instance of this block, if allowed
+            $block = blocks_get_record($blockid);
 
-    $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
-    set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
+            if(!$block->visible) {
+                // Only allow adding if the block is enabled
+                return false;
+            }
 
+            $weight = get_record_sql('SELECT 1, max(weight) + 1 AS nextfree FROM '.$CFG->prefix.'block_instance WHERE pageid = '.$page->id.' AND pagetype = \''.$page->type.'\' AND position = \''.BLOCK_POS_RIGHT.'\''); 
+
+            $newinstance = new stdClass;
+            $newinstance->blockid    = $blockid;
+            $newinstance->pageid     = $page->id;
+            $newinstance->pagetype   = $page->type;
+            $newinstance->position   = BLOCK_POS_RIGHT;
+            $newinstance->weight     = $weight->nextfree;
+            $newinstance->visible    = 1;
+            $newinstance->configdata = '';
+            insert_record('block_instance', $newinstance);
+        break;
+    }
 }
 
-// Searches for the block with ID $blockid in one or more of the two
-// blocks arrays. If not found, returns boolean false. Otherwise,
-// returns an object $finding where:
-//      $finding->side = BLOCK_LEFT or BLOCK_RIGHT
-//      $finding->enabled = true or false
-//      $finding->position = index of corresponding array where found
+function blocks_get_by_page($page) {
+    $blocks = get_records_select('block_instance', 'pageid = '.$page->id.' AND pagetype = \''.$page->type.'\'', 'position, weight');
 
-function block_find($blockid, $leftblocks, $rightblocks) {
+    $arr = array(BLOCK_POS_LEFT => array(), BLOCK_POS_RIGHT => array());
+    if(empty($blocks)) {
+        return $arr;
+    }
 
-    if(($blockid = abs($blockid)) == 0) {
-        return false;
+    foreach($blocks as $block) {
+        $arr[$block->position][$block->weight] = $block;
     }
 
-    $finding->side = BLOCK_LEFT;
-    $finding->enabled = true;
-    $finding->position = NULL;
+    return $arr;    
+}
 
-    // First, search for the "enabled" block, since that's what we
-    // will be doing most of the time.
+//This function prints the block to admin blocks as necessary
+function blocks_print_adminblock($page, $missingblocks) {
+    global $USER;
 
-    $key = array_search($blockid, $leftblocks);
-    if($key !== false && $key !== NULL) {
-        $finding->position = $key;
-        return $finding;
-    }
-    $key = array_search($blockid, $rightblocks);
-    if($key !== false && $key !== NULL) {
-        $finding->position = $key;
-        $finding->side = BLOCK_RIGHT;
-        return $finding;
-    }
+    $strblocks = get_string('blocks');
+    $stradd    = get_string('add');
+    if (!empty($missingblocks)) {
+        foreach ($missingblocks as $blockid) {
+            $block = blocks_get_record($blockid);
+
+            switch($page->type) {
+                case MOODLE_PAGE_COURSE:
+                    $course = get_record('course', 'id', $page->id);
+                break;
+                default: die('unknown pagetype: '.$page->type);
+            }
 
-    // "enabled" block not found. Now search for the disabled block.
-    $finding->enabled = false;
-    $blockid = -$blockid;
+            $blockobject = block_instance($block->name);
+            if ($blockobject === false) {
+                continue;
+            }
+            $menu[$block->id] = $blockobject->get_title();
+        }
 
-    $key = array_search($blockid, $leftblocks);
-    if($key !== false && $key !== NULL) {
-        $finding->position = $key;
-        return $finding;
-    }
-    $key = array_search($blockid, $rightblocks);
-    if($key !== false && $key !== NULL) {
-        $finding->position = $key;
-        $finding->side = BLOCK_RIGHT;
-        return $finding;
+        if($page->id == SITEID) {
+            $target = 'index.php';
+        }
+        else {
+            $target = 'view.php';
+        }
+        $content = popup_form($target.'?id='.$course->id.'&amp;sesskey='.$USER->sesskey.'&amp;blockaction=add&amp;blockid=',
+                              $menu, 'add_block', '', "$stradd...", '', '', true);
+        $content = '<div align="center">'.$content.'</div>';
+        print_side_block($strblocks, $content, NULL, NULL, NULL);
     }
-
-    // Nothing found :(
-
-    return false;
 }
 
-//This function prints the block to admin blocks as necessary
-function block_print_blocks_admin(&$course, $missingblocks) {
-
-    global $USER;
+function blocks_repopulate_page($page) {
+    global $CFG;
 
-    if (isediting($course->id)) {
-        $strblocks = get_string('blocks');
-        $stradd    = get_string('add');
-        if (!empty($missingblocks)) {
-            $blockdata = get_records_list('blocks', 'id', implode(',', $missingblocks));
-            if ($blockdata !== false) {
-                foreach ($blockdata as $block) {
-                    $blockobject = block_instance($block->name, $course);
-                    if ($blockobject === false) {
-                        continue;
+    /// If the site override has been defined, it is the only valid one.
+    if (!empty($CFG->defaultblocks_override)) {
+        $blocknames = $CFG->defaultblocks_override;
+    }
+    /// Else, try to find out what page this is 
+    else {
+        switch($page->type) {
+            case MOODLE_PAGE_COURSE:
+                // Is it the site?
+                if($page->id == SITEID) {
+                    if (!empty($CFG->defaultblocks_site)) {
+                        $blocknames = $CFG->defaultblocks_site;
+                    }
+                    /// Failsafe - in case nothing was defined.
+                    else {
+                        $blocknames = 'site_main_menu,admin,course_list:course_summary,calendar_month';
                     }
-                    $menu[$block->id] = $blockobject->get_title();
-                }
-
-                if(!$course->category) {
-                    $target = 'index.php';
                 }
+                // It's a normal course, so do it accodring to the course format
                 else {
-                    $target = 'view.php';
+                    $course = get_record('course', 'id', $page->id);
+                    if (!empty($CFG->{'defaultblocks_'.$course->format})) {
+                        $blocknames = $CFG->{'defaultblocks_'.$course->format};
+                    }
+                    else {
+                        $format_config = $CFG->dirroot.'/course/format/'.$course->format.'/config.php';
+                        if (@is_file($format_config) && is_readable($format_config)) {
+                            require($format_config);
+                        }
+                        if (!empty($format['defaultblocks'])) {
+                            $blocknames = $format['defaultblocks'];
+                        }
+                        else if (!empty($CFG->defaultblocks)){
+                            $blocknames = $CFG->defaultblocks;
+                        }
+                        /// Failsafe - in case nothing was defined.
+                        else {
+                            $blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
+                        }
+                    }
                 }
-                $content = popup_form($target.'?id='.$course->id.'&amp;blockaction=add&amp;sesskey='.$USER->sesskey.'&amp;blockid=',
-                                      $menu, 'add_block', '', "$stradd...", '', '', true);
-                $content = '<div align="center">'.$content.'</div>';
-                print_side_block($strblocks, $content, NULL, NULL, NULL);
-            }
+            break;
+            default:
+                error('Invalid page type: '.$page->type);
+            break;
+        }
+    }
+
+    $allblocks = blocks_get_record();
+
+    if(empty($allblocks)) {
+        error('Could not retrieve blocks from the database');
+    }
+
+    // We have the blocks, make up two arrays
+    $left  = '';
+    $right = '';
+    @list($left, $right) = explode(':', $blocknames);
+    $instances = array(BLOCK_POS_LEFT => explode(',', $left), BLOCK_POS_RIGHT => explode(',', $right));
+
+    // Arrays are fine, now we have to correlate block names to ids
+    $idforname = array();
+    foreach($allblocks as $block) {
+        $idforname[$block->name] = $block->id;
+    }
+
+    // Ready to start creating block instances, but first drop any existing ones
+    delete_records('block_instance', 'pageid', $page->id, 'pagetype', $page->type);
+
+    foreach($instances as $position => $blocknames) {
+        $weight = 0;
+        foreach($blocknames as $blockname) {
+            $newinstance = new stdClass;
+            $newinstance->blockid    = $idforname[$blockname];
+            $newinstance->pageid     = $page->id;
+            $newinstance->pagetype   = $page->type;
+            $newinstance->position   = $position;
+            $newinstance->weight     = $weight;
+            $newinstance->visible    = 1;
+            $newinstance->configdata = '';
+            insert_record('block_instance', $newinstance);
+            $weight++;
         }
     }
+
+    return true;
 }
 
 function upgrade_blocks_db($continueto) {
@@ -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 '<hr />';
@@ -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('<strong>Naming conflict</strong>: block <strong>'.$block->name.'</strong> has the same title with an existing block, <strong>'.$conflictblock.'</strong>!');
@@ -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 "<hr />";
                 } 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.')<br />';
-                }
+                $page = new stdClass;
+                $page->type = MOODLE_PAGE_COURSE;
+                $page->id   = $course->id;
+                blocks_repopulate_page($page);
             }
         }
     }
 
     if (!empty($CFG->siteblocksadded)) {     /// This is a once-off hack to make a proper upgrade
-        blocks_get_default_blocks(SITEID, blocks_get_config_default());  // Add blockinfo to the site course
+        $page = new stdClass;
+        $page->type = MOODLE_PAGE_COURSE;
+        $page->id   = SITEID;
+        blocks_repopulate_page($page);
         delete_records('config', 'name', 'siteblocksadded');
     }
 
@@ -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()<br />";
+    print_object($blocks);
 }
 
+
 ?>
index d70b7d72ab7b90de17c1ce5a54a0aa9dfd2b40f1..eaf39b544883c829d7ea3dd5dc62f4c564f96165 100644 (file)
@@ -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',
index 5094e477e9d055ff56067a613beeed0487d93b36..8d3479ab425e4b6e0b4c414133cedce2e4873f6a 100644 (file)
@@ -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',
index a96283e84652fc25e790bb1096159c823d7ab876..961f1d10ae2d5a536b94d62586c58c197cb9ea76 100644 (file)
@@ -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 '<input type="button" value="Check spelling" onclick="openSpellChecker();" />'."\n";
 }
+
+function page_source_script($page) {
+    global $CFG;
+
+    switch($page->type) {
+        case MOODLE_PAGE_COURSE:
+            return $CFG->wwwroot.'/course/view.php?id='.$page->id;
+        break;
+    }
+}
+
 // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
 ?>
index 21d4be4560532b0e6bb9733a3d7e5f842b27d8e7..8f67e1e32938bc43fbbebedfa7e0660009e23d1b 100644 (file)
@@ -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