]> git.mjollnir.org Git - moodle.git/commitdiff
blocks editing ui: MDL-19398 showing and hiding blocks now works.
authortjhunt <tjhunt>
Wed, 15 Jul 2009 07:41:25 +0000 (07:41 +0000)
committertjhunt <tjhunt>
Wed, 15 Jul 2009 07:41:25 +0000 (07:41 +0000)
blocks/moodleblock.class.php
lang/en_utf8/moodle.php
lib/blocklib.php
lib/outputlib.php

index 2202d8f711d61367d98535546880bbe3874ae5e9..fd1c17a05d27eb28824e3435465b01143e20c039 100644 (file)
@@ -355,9 +355,12 @@ class block_base {
         if (!$this->hide_header()) {
             $bc->title = $this->title;
         }
-        $bc->content = $this->formatted_contents($output);
-        if (!empty($this->content->footer)) {
-            $bc->footer = $this->content->footer;
+
+        if ($this->instance->visible) {
+            $bc->content = $this->formatted_contents($output);
+            if (!empty($this->content->footer)) {
+                $bc->footer = $this->content->footer;
+            }
         }
 
         if ($this->page->user_is_editing()) {
@@ -368,7 +371,8 @@ class block_base {
             return null;
         }
 
-        if (empty($CFG->allowuserblockhiding)) {
+        if (empty($CFG->allowuserblockhiding) ||
+                (empty($bc->content) && empty($bc->footer))) {
             $bc->collapsible = block_contents::NOT_HIDEABLE;
         } else if (get_user_preferences('block' . $bc->blockinstanceid . 'hidden', false)) {
             $bc->collapsible = block_contents::HIDDEN;
index 0ddc7820ad30a5c1042b79f48baefa001d66d89c..319b2e7623920e4d7ad1c6cfc856fea65884a814 100644 (file)
@@ -820,6 +820,7 @@ $string['hideadvancedsettings'] = 'Hide advanced settings';
 $string['hidepicture'] = 'Hide picture';
 $string['hidesection'] = 'Hide section $a';
 $string['hidesettings'] = 'Hide settings';
+$string['hideshowblocks'] = 'Hide or show blocks';
 $string['hidetopicfromothers'] = 'Hide topic from others';
 $string['hideweekfromothers'] = 'Hide week from others';
 $string['hits'] = 'Hits';
index a6d9433d86b788007c7e1e927212247f8f047f59..19003a981ba60618201a793c1610b4a57e780a00 100644 (file)
@@ -354,9 +354,12 @@ class block_manager {
     /**
      * This method actually loads the blocks for our page from the database.
      *
-     * @param bool|null $includeinvisible
+     * @param boolean|null $includeinvisible
+     *      null (default) - load hidden blocks if $this->page->user_is_editing();
+     *      true - load hidden blocks.
+     *      false - don't load hidden blocks.
      */
-    public function load_blocks($includeinvisible = NULL) {
+    public function load_blocks($includeinvisible = null) {
         global $DB, $CFG;
         if (!is_null($this->birecordsbyregion)) {
             // Already done.
@@ -383,9 +386,9 @@ class block_manager {
             $includeinvisible = $this->page->user_is_editing();
         }
         if ($includeinvisible) {
-            $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)';
-        } else {
             $visiblecheck = '';
+        } else {
+            $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)';
         }
 
         $context = $this->page->context;
@@ -418,6 +421,8 @@ class block_manager {
                     bi.showinsubcontexts,
                     bi.pagetypepattern,
                     bi.subpagepattern,
+                    bi.defaultregion,
+                    bi.defaultweight,
                     COALESCE(bp.visible, 1) AS visible,
                     COALESCE(bp.region, bi.defaultregion) AS region,
                     COALESCE(bp.weight, bi.defaultweight) AS weight,
@@ -863,10 +868,10 @@ function block_edit_controls($block, $page) {
     if ($block->user_can_edit() && $page->user_can_edit_blocks()) {
         // Show/hide icon.
         if ($block->instance->visible) {
-            $controls[] = array('url' => $actionurl . '&amp;action=hide',
+            $controls[] = array('url' => $actionurl . '&amp;bui_hideid=' . $block->instance->id,
                     'icon' => 't/hide', 'caption' => get_string('hide'));
         } else {
-            $controls[] = array('url' => $actionurl . '&amp;action=show',
+            $controls[] = array('url' => $actionurl . '&amp;bui_showid=' . $block->instance->id,
                     'icon' => 't/show', 'caption' => get_string('show'));
         }
 
@@ -970,7 +975,29 @@ function block_process_url_delete($page) {
  * @return boolean true if anything was done. False if not.
  */
 function block_process_url_show_hide($page) {
-    // TODO MDL-19398
+    if ($blockid = optional_param('bui_hideid', null, PARAM_INTEGER)) {
+        $newvisibility = 0;
+    } else if ($blockid = optional_param('bui_showid', null, PARAM_INTEGER)) {
+        $newvisibility = 1;
+    } else {
+        return false;
+    }
+
+    confirm_sesskey();
+
+    $block = $page->blocks->find_instance($blockid);
+
+    if (!$block->user_can_edit() || !$page->user_can_edit_blocks()) {
+        throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('hideshowblocks'));
+    }
+
+    blocks_set_visibility($block->instance, $page, $newvisibility);
+
+    // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there.
+    $page->ensure_param_not_in_url('bui_hideid');
+    $page->ensure_param_not_in_url('bui_showid');
+
+    return true;
 }
 
 ///**
@@ -1099,6 +1126,36 @@ function blocks_delete_all_for_context($contextid) {
     $DB->delete_records('block_positions', array('contextid' => $contextid));
 }
 
+/**
+ * Set a block to be visible or hidden on a particular page.
+ *
+ * @param object $instance a row from the block_instances, preferably LEFT JOINed with the
+ *      block_positions table as return by block_manager.
+ * @param moodle_page $page the back to set the visibility with respect to.
+ * @param integer $newvisibility 1 for visible, 0 for hidden.
+ */
+function blocks_set_visibility($instance, $page, $newvisibility) {
+    global $DB;
+    if (!empty($instance->blockpositionid)) {
+        // Already have local information on this page.
+        $DB->set_field('block_positions', 'visible', $newvisibility, array('id' => $instance->blockpositionid));
+        return;
+    }
+
+    // Create a new block_positions record.
+    $bp = new stdClass;
+    $bp->blockinstanceid = $instance->id;
+    $bp->contextid = $page->context->id;
+    $bp->pagetype = $page->pagetype;
+    if ($page->subpage) {
+        $bp->subpage = $page->subpage;
+    }
+    $bp->visible = $newvisibility;
+    $bp->region = $instance->defaultregion;
+    $bp->weight = $instance->defaultweight;
+    $DB->insert_record('block_positions', $bp);
+}
+
 /**
  * @deprecated since 2.0
  * Delete all the blocks from a particular page.
index 5e6f16abcbc1a0c179e41d2166a87b12643a3960..2bc2a81097af0a559e64f6357e3ede064020e485 100644 (file)
@@ -1932,7 +1932,7 @@ class moodle_core_renderer extends moodle_renderer_base {
         echo '<table id="layout-table"><tr>';
         foreach ($lt as $column) {
             if ($column == 'left' && $this->page->blocks->region_has_content(BLOCK_POS_LEFT)) {
-                echo '<td id="left-column" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
+                echo '<td id="left-column" class="block-region" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
                 echo $this->container_start();
                 echo $this->blocks_for_region(BLOCK_POS_LEFT);
                 echo $this->container_end();
@@ -1947,7 +1947,7 @@ class moodle_core_renderer extends moodle_renderer_base {
                 echo '</td>';
 
             } else if ($column == 'right' && $this->page->blocks->region_has_content(BLOCK_POS_RIGHT)) {
-                echo '<td id="right-column" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
+                echo '<td id="right-column" class="block-region" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
                 echo $this->container_start();
                 echo $this->blocks_for_region(BLOCK_POS_RIGHT);
                 echo $this->container_end();