]> git.mjollnir.org Git - moodle.git/commitdiff
Changing the way that applicable formats for each block are defined:
authordefacer <defacer>
Tue, 8 Feb 2005 02:59:44 +0000 (02:59 +0000)
committerdefacer <defacer>
Tue, 8 Feb 2005 02:59:44 +0000 (02:59 +0000)
The format for each page is now the same as the id attribute of the
BODY tag, which in turn is a simple function of the script's relative path:

The format for e.g. a quiz view page is "mod-quiz-view". The format for the
site index is "site-index". Exception: the format for courses is not just
"course-view", but "course-view-weeks" etc.

Obviously the applicable_formats() override for each block should now take
this into account. The matching rules now are:

* You can specify the full format, e.g. "mod-quiz-view" => true
  will allow the block to be added in quizzes
* Prefixes match the full page format, e.g. "mod" matches ALL activities
* You can use "*" as a wildcard, e.g. "mod-*-view" matches just the view.php
  page of all activities
* These rules interoperate, thus "mod-*" is the same as "mod"
* "all" remains as a catch-all situation

blocks/moodleblock.class.php
blocks/section_links/block_section_links.php
blocks/social_activities/block_social_activities.php
lib/blocklib.php
lib/pagelib.php

index 76b799a040cffcdf3603074fdb98f29178ba0788..bbf22f4688b182ca02963777ff92cd1008e6e8d8 100644 (file)
@@ -354,7 +354,7 @@ class block_base {
 
         $formats = $this->applicable_formats();
         if (empty($formats) || array_sum($formats) === 0) {
-            $errors[] = 'no_course_formats';
+            $errors[] = 'no_formats';
             $correct = false;
         }
 
@@ -418,8 +418,8 @@ class block_base {
      * @todo finish documenting this function
      */
     function applicable_formats() {
-        // Default case: the block can be used in all course types and not in quizzes
-        return array('all' => true, 'quiz' => false);
+        // Default case: the block can be used in all course types but not in activities
+        return array('all' => true, 'mod' => false);
     }
     
 
index e1fda967ea07eeec9aa42abd93c18caa9b10e1e1..de6de78d4d6e58afae768b7ff07b23d58abc7c60 100644 (file)
@@ -23,7 +23,7 @@ class block_section_links extends block_base {
     }
 
     function applicable_formats() {
-        return (array('weeks' => true, 'topics' => true));
+        return (array('course-view-weeks' => true, 'course-view-topics' => true));
     }
 
     function get_content() {
index d56a6a2aab39a9b382cd0e8d97891432b4ff09d5..706a5a72bb872785e94d7962ceb77905398c4e27 100644 (file)
@@ -8,7 +8,7 @@ class block_social_activities extends block_base {
     }
 
     function applicable_formats() {
-        return array('social' => true);
+        return array('course-view-social' => true);
     }
 
     function get_content() {
index bad6c02fb42a22f3bc9dad1ad1e71b43437d5800..4bb84d5c901e47bb5e47cefd6d67bbc7040ee51a 100644 (file)
@@ -103,8 +103,20 @@ function blocks_get_missing(&$page, &$pageblocks) {
             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');
-                if(isset($formats[$pageformat]) ? $formats[$pageformat] : !empty($formats['all'])) {
-                    // Add it to the missing blocks
+                $accept  = NULL;
+                foreach($formats as $format => $allowed) {
+                    $thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$';
+                    if(ereg($thisformat, $pageformat)) {
+                        $accept = $allowed;
+                        break;
+                    }
+                }
+                if($accept === NULL) {
+                    // ...or in all pages...
+                    $accept = !empty($formats['all']);
+                }
+                if(!empty($accept)) {
+                    // ...add it to the missing blocks
                     $missingblocks[] = $block->id;
                 }
             }
@@ -128,12 +140,19 @@ function blocks_remove_inappropriate($page) {
         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);
+            $accept  = NULL;
+            foreach($formats as $format => $allowed) {
+                $thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$';
+                if(ereg($thisformat, $pageformat)) {
+                    $accept = $allowed;
+                    break;
+                }
+            }
+            if($accept === NULL) {
+                $accept = !empty($formats['all']);
+            }
+            if(empty($accept)) {
+               blocks_delete_instance($instance);
             }
         }
     }
index bbdff8165178ca76c072709044129d5d20e0125d..df1fb73fcd036a10d354b5f00a42df58776774f9 100644 (file)
@@ -249,9 +249,9 @@ class page_base {
         return $this->id;
     }
 
-    // "Sensible default" case here. Don't trigger any error.
+    // "Sensible default" case here. Take it from the body id.
     function get_format_name() {
-        return NULL;
+        return $this->body_id;
     }
 
     // Returns $this->body_class
@@ -388,7 +388,10 @@ class page_course extends page_base {
     // the format_name might be that activity's name etc.
     function get_format_name() {
         $this->init_full();
-        return $this->courserecord->format;
+        if($this->id == SITEID) {
+            return parent::get_format_name();
+        }
+        return $this->body_id.'-'.$this->courserecord->format;
     }
 
     // This should return a fully qualified path to the URL which is responsible for displaying us.
@@ -610,13 +613,6 @@ class page_quiz extends page_base {
         return PAGE_QUIZ_VIEW;
     }
 
-    // This is like the "category" of a page of this "type". For example, if the type is PAGE_COURSE_VIEW
-    // the format_name is the actual name of the course format. If the type were PAGE_ACTIVITY_VIEW, then
-    // the format_name might be that activity's name etc.
-    function get_format_name() {
-        return 'quiz';
-    }
-
     // This should return a fully qualified path to the URL which is responsible for displaying us.
     function url_get_path() {
         global $CFG;