From ae42ff6feee0963f99cbde95e83aff44ac98180f Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 15 Jul 2009 07:41:25 +0000 Subject: [PATCH] blocks editing ui: MDL-19398 showing and hiding blocks now works. --- blocks/moodleblock.class.php | 12 ++++-- lang/en_utf8/moodle.php | 1 + lib/blocklib.php | 71 ++++++++++++++++++++++++++++++++---- lib/outputlib.php | 4 +- 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 2202d8f711..fd1c17a05d 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -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; diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index 0ddc7820ad..319b2e7623 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -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'; diff --git a/lib/blocklib.php b/lib/blocklib.php index a6d9433d86..19003a981b 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -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 . '&action=hide', + $controls[] = array('url' => $actionurl . '&bui_hideid=' . $block->instance->id, 'icon' => 't/hide', 'caption' => get_string('hide')); } else { - $controls[] = array('url' => $actionurl . '&action=show', + $controls[] = array('url' => $actionurl . '&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. diff --git a/lib/outputlib.php b/lib/outputlib.php index 5e6f16abcb..2bc2a81097 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -1932,7 +1932,7 @@ class moodle_core_renderer extends moodle_renderer_base { echo ''; foreach ($lt as $column) { if ($column == 'left' && $this->page->blocks->region_has_content(BLOCK_POS_LEFT)) { - echo ''; } else if ($column == 'right' && $this->page->blocks->region_has_content(BLOCK_POS_RIGHT)) { - echo '
'; + echo ''; 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 ''; + echo ''; echo $this->container_start(); echo $this->blocks_for_region(BLOCK_POS_RIGHT); echo $this->container_end(); -- 2.39.5