diff --git a/blocks/edit.php b/blocks/edit.php deleted file mode 100644 index 8a584c2b967..00000000000 --- a/blocks/edit.php +++ /dev/null @@ -1,148 +0,0 @@ -. - -/** - * This script is used to edit the settings for a block instance. - * - * It works with the {@link block_edit_form} class, or rather the particular - * subclass defined by this block, to do the editing. - * - * @package moodlecore - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once(dirname(__FILE__) . '/../config.php'); -require_once($CFG->dirroot . '/blocks/edit_form.php'); - -$blockid = required_param('id', PARAM_INTEGER); -$pagecontextid = required_param('pagecontextid', PARAM_INTEGER); -$pagetype = required_param('pagetype', PARAM_ALPHANUMEXT); -$subpage = optional_param('subpage', null, PARAM_ALPHANUMEXT); -$returnurl = required_param('returnurl', PARAM_LOCALURL); - -$urlparams = array( - 'id' => $blockid, - 'pagecontextid' => $pagecontextid, - 'pagetype' => $pagetype, - 'returnurl' => $returnurl, -); -if ($subpage) { - $urlparams['subpage'] = $subpage; -} -$PAGE->set_url('blocks/edit.php', $urlparams); - -require_login(); - -$blockpage = new moodle_page(); -$blockpage->set_context(get_context_instance_by_id($pagecontextid)); -$blockpage->set_pagetype($pagetype); -$blockpage->set_subpage($subpage); -$url = new moodle_url($returnurl); -$blockpage->set_url($url->out(true), $url->params()); - -$block = block_load_for_page($blockid, $blockpage); - -if (!$block->user_can_edit()) { - throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('editblock')); -} - -$PAGE->set_context($block->context); -$PAGE->set_generaltype('form'); - -$formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php'; -if (is_readable($formfile)) { - require_once($formfile); - $classname = 'block_' . $block->name() . '_edit_form'; -} else { - $classname = 'block_edit_form'; -} -$mform = new $classname($block, $blockpage); - -$mform->set_data($block->instance); - -if ($mform->is_cancelled()) { - redirect($CFG->wwwroot . '/' . $returnurl); - -} else if ($data = $mform->get_data()) { - $bi = new stdClass; - $bi->id = $block->instance->id; - $bi->showinsubcontexts = $data->showinsubcontexts; - $bi->pagetypepattern = $data->pagetypepattern; - if (empty($data->subpagepattern) || $data->subpagepattern == '%@NULL@%') { - $bi->subpagepattern = null; - } else { - $bi->subpagepattern = $data->subpagepattern; - } - $bi->defaultregion = $data->defaultregion; - $bi->defaultweight = $data->defaultweight; - $DB->update_record('block_instances', $bi); - - $config = new stdClass; - foreach ($data as $configfield => $value) { - if (strpos($configfield, 'config_') !== 0) { - continue; - } - $field = substr($configfield, 7); - $config->$field = $value; - } - $block->instance_config_save($config); - - $bp = new stdClass; - $bp->visible = $data->visible; - $bp->region = $data->region; - $bp->weight = $data->weight; - $needbprecord = !$data->visible || $data->region != $data->defaultregion || - $data->weight != $data->defaultweight; - - if ($block->instance->blockpositionid && !$needbprecord) { - $DB->delete_records('block_positions', array('id' => $block->instance->blockpositionid)); - - } else if ($block->instance->blockpositionid && $needbprecord) { - $bp->id = $block->instance->blockpositionid; - $DB->update_record('block_positions', $bp); - - } else if ($needbprecord) { - $bp->blockinstanceid = $block->instance->id; - $bp->contextid = $blockpage->contextid; - $bp->pagetype = $blockpage->pagetype; - if ($blockpage->subpage) { - $bp->subpage = $blockpage->subpage; - } else { - $bp->subpage = null; - } - $DB->insert_record('block_positions', $bp); - } - - redirect($CFG->wwwroot . '/' . $returnurl); - -} else { - $strheading = get_string('editinga', $block->name()); - if (strpos($strheading, '[[') === 0) { - $strheading = get_string('blockconfiga', 'moodle', $block->get_title()); - } - -$PAGE->set_title($strheading); - $PAGE->set_heading($strheading); - - echo $OUTPUT->header(); - echo $OUTPUT->heading($strheading, 2); - - $mform->display(); - - echo $OUTPUT->footer(); -} \ No newline at end of file diff --git a/blocks/edit_form.php b/blocks/edit_form.php index 1de15d03d06..d8c8fcc3f65 100644 --- a/blocks/edit_form.php +++ b/blocks/edit_form.php @@ -47,11 +47,11 @@ class block_edit_form extends moodleform { */ public $page; - function __construct($block, $page) { + function __construct($actionurl, $block, $page) { global $CFG; $this->block = $block; $this->page = $page; - parent::moodleform(block_edit_url($block, $page)); + parent::moodleform($actionurl); } function definition() { @@ -85,18 +85,18 @@ class block_edit_form extends moodleform { $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid); $mform->addElement('static', 'contextname', get_string('thisblockbelongsto', 'block'), print_context_name($parentcontext)); - $mform->addElement('selectyesno', 'showinsubcontexts', get_string('appearsinsubcontexts', 'block')); + $mform->addElement('selectyesno', 'bui_showinsubcontexts', get_string('appearsinsubcontexts', 'block')); $pagetypeoptions = matching_page_type_patterns($this->page->pagetype); $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions); - $mform->addElement('select', 'pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions); + $mform->addElement('select', 'bui_pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions); if ($this->page->subpage) { $subpageoptions = array( '%@NULL@%' => get_string('anypagematchingtheabove', 'block'), $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage), ); - $mform->addElement('select', 'subpagepattern', get_string('subpages', 'block'), $subpageoptions); + $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions); } $defaultregionoptions = $regionoptions; @@ -104,22 +104,22 @@ class block_edit_form extends moodleform { if (!array_key_exists($defaultregion, $defaultregionoptions)) { $defaultregionoptions[$defaultregion] = $defaultregion; } - $mform->addElement('select', 'defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions); + $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions); - $mform->addElement('select', 'defaultweight', get_string('defaultweight', 'block'), $weightoptions); + $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions); // Where this block is positioned on this page. $mform->addElement('header', 'whereheader', get_string('onthispage', 'block')); - $mform->addElement('selectyesno', 'visible', get_string('visible', 'block')); + $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block')); $blockregion = $this->block->instance->region; if (!array_key_exists($blockregion, $regionoptions)) { $regionoptions[$blockregion] = $blockregion; } - $mform->addElement('select', 'region', get_string('region', 'block'), $regionoptions); + $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions); - $mform->addElement('select', 'weight', get_string('weight', 'block'), $weightoptions); + $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions); $this->add_action_buttons(); } diff --git a/lib/blocklib.php b/lib/blocklib.php index 82520e6b7c8..a2d221e2f7d 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1,7 +1,7 @@ . /** * Block Class and Functions * - * This file defines the {@link block_manager} class, + * This file defines the {@link block_manager} class, * * @package moodlecore * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com @@ -273,7 +273,7 @@ class block_manager { * output the blocks anyway, so we are not doing wasted effort.) * * @param string $region a block region that exists on this page. - * @param object $output a moodle_core_renderer. normally the global $OUTPUT. + * @param object $output a moodle_core_renderer. normally the global $OUTPUT. * @return boolean Whether there is anything in this region. */ public function region_has_content($region, $output) { @@ -570,7 +570,7 @@ class block_manager { } /** - * Find a given block by its instance id + * Find a given block by its instance id * * @param integer $instanceid * @return object @@ -701,7 +701,7 @@ class block_manager { /** * Ensure block instances exist for a given region - * + * * @param string $region Check for bi's with the instance with this name */ protected function ensure_instances_exist($region) { @@ -734,6 +734,224 @@ class block_manager { $this->visibleblockcontent[$region] = $contents; } } + +/// Process actions from the URL =============================================== + + /** + * Process any block actions that were specified in the URL. + * + * This can only be done given a valid $page object. + * + * @param moodle_page $page the page to add blocks to. + * @return boolean true if anything was done. False if not. + */ + public function process_url_actions() { + return $this->process_url_add() || $this->process_url_delete() || + $this->process_url_show_hide() || $this->process_url_edit(); + } + + /** + * Handle adding a block. + * @return boolean true if anything was done. False if not. + */ + public function process_url_add() { + $blocktype = optional_param('bui_addblock', null, PARAM_SAFEDIR); + if (!$blocktype) { + return false; + } + + confirm_sesskey(); + + if (!$page->user_is_editing() && !$this->page->user_can_edit_blocks()) { + throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('addblock')); + } + + if (!array_key_exists($blocktype, $this->get_addable_blocks())) { + throw new moodle_exception('cannotaddthisblocktype', '', $this->page->url->out(), $blocktype); + } + + $this->add_block_at_end_of_default_region($blocktype); + + // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there. + $this->page->ensure_param_not_in_url('bui_addblock'); + + return true; + } + + /** + * Handle deleting a block. + * @return boolean true if anything was done. False if not. + */ + public function process_url_delete() { + $blockid = optional_param('bui_deleteid', null, PARAM_INTEGER); + if (!$blockid) { + return false; + } + + confirm_sesskey(); + + $block = $this->page->blocks->find_instance($blockid); + + if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks() || !$block->user_can_addto($page)) { + throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock')); + } + + blocks_delete_instance($block->instance); + + // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there. + $this->page->ensure_param_not_in_url('bui_deleteid'); + + return true; + } + + /** + * Handle showing or hiding a block. + * @return boolean true if anything was done. False if not. + */ + public function process_url_show_hide() { + 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 = $this->page->blocks->find_instance($blockid); + + if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks()) { + throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('hideshowblocks')); + } + + blocks_set_visibility($block->instance, $this->page, $newvisibility); + + // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there. + $this->page->ensure_param_not_in_url('bui_hideid'); + $this->page->ensure_param_not_in_url('bui_showid'); + + return true; + } + + /** + * Handle showing/processing the submission from the block editing form. + * @return boolean true if the form was submitted and the new config saved. Does not + * return if the editing form was displayed. False otherwise. + */ + public function process_url_edit() { + global $CFG, $DB, $PAGE; + + $blockid = optional_param('bui_editid', null, PARAM_INTEGER); + if (!$blockid) { + return false; + } + + confirm_sesskey(); + require_once($CFG->dirroot . '/blocks/edit_form.php'); + + $block = $this->find_instance($blockid); + + if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks()) { + throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock')); + } + + $editpage = new moodle_page(); + $editpage->set_generaltype('form'); + $editpage->set_course($this->page->course); + $editpage->set_context($this->page->context); + $editurlbase = str_replace($CFG->wwwroot . '/', '', $this->page->url->out(true)); + $editurlparams = $this->page->url->params(); + $editurlparams['bui_editid'] = $blockid; + $editpage->set_url($editurlbase, $editurlparams); + $editpage->_block_actions_done = true; + // At this point we are either going to redirect, or display the form, so + // overwrite global $PAGE ready for this. (Formslib refers to it.) + $PAGE = $editpage; + + $formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php'; + if (is_readable($formfile)) { + require_once($formfile); + $classname = 'block_' . $block->name() . '_edit_form'; + } else { + $classname = 'block_edit_form'; + } + + $mform = new $classname($editpage->url, $block, $this->page); + $mform->set_data($block->instance); + + if ($mform->is_cancelled()) { + redirect($this->page->url); + + } else if ($data = $mform->get_data()) { + $bi = new stdClass; + $bi->id = $block->instance->id; + $bi->showinsubcontexts = $data->bui_showinsubcontexts; + $bi->pagetypepattern = $data->bui_pagetypepattern; + if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') { + $bi->subpagepattern = null; + } else { + $bi->subpagepattern = $data->bui_subpagepattern; + } + $bi->defaultregion = $data->bui_defaultregion; + $bi->defaultweight = $data->bui_defaultweight; + $DB->update_record('block_instances', $bi); + + $config = clone($block->config); + foreach ($data as $configfield => $value) { + if (strpos($configfield, 'config_') !== 0) { + continue; + } + $field = substr($configfield, 7); + $config->$field = $value; + } + $block->instance_config_save($config); + + $bp = new stdClass; + $bp->visible = $data->bui_visible; + $bp->region = $data->bui_region; + $bp->weight = $data->bui_weight; + $needbprecord = !$data->bui_visible || $data->bui_region != $data->bui_defaultregion || + $data->bui_weight != $data->bui_defaultweight; + + if ($block->instance->blockpositionid && !$needbprecord) { + $DB->delete_records('block_positions', array('id' => $block->instance->blockpositionid)); + + } else if ($block->instance->blockpositionid && $needbprecord) { + $bp->id = $block->instance->blockpositionid; + $DB->update_record('block_positions', $bp); + + } else if ($needbprecord) { + $bp->blockinstanceid = $block->instance->id; + $bp->contextid = $this->page->contextid; + $bp->pagetype = $this->page->pagetype; + if ($this->page->subpage) { + $bp->subpage = $this->page->subpage; + } else { + $bp->subpage = null; + } + $DB->insert_record('block_positions', $bp); + } + + redirect($this->page->url); + + } else { + $strheading = get_string('editinga', $block->name()); + if (strpos($strheading, '[[') === 0) { + $strheading = get_string('blockconfiga', 'moodle', $block->get_title()); + } + + $editpage->set_title($strheading); + $editpage->set_heading($strheading); + + $output = $editpage->theme->get_renderer('core', $editpage); + echo $output->header(); + echo $output->heading($strheading, 2); + $mform->display(); + echo $output->footer(); + exit; + } + } } /// Helper functions for working with block classes ============================ @@ -959,7 +1177,7 @@ function block_edit_controls($block, $page) { } // Edit config icon - always show - needed for positioning UI. - $controls[] = array('url' => block_edit_url($block, $page)->out(), + $controls[] = array('url' => $actionurl . '&bui_editid=' . $block->instance->id, 'icon' => 't/edit', 'caption' => get_string('configuration')); // Delete icon. @@ -976,135 +1194,6 @@ function block_edit_controls($block, $page) { return $controls; } -/** - * Get the URL for editing a particular block instance. - * @param block_base $block a block object. - * @return moodle_url the url. - */ -function block_edit_url($block, $page) { - global $CFG; - $urlparams = array( - 'id' => $block->instance->id, - 'pagecontextid' => $page->context->id, - 'pagetype' => $page->pagetype, - 'returnurl' => $page->url->out_returnurl(), - ); - if ($page->subpage) { - $urlparams['subpage'] = $page->subpage; - } - return new moodle_url($CFG->wwwroot . '/blocks/edit.php', $urlparams); -} - -/** - * Process any block actions that were specified in the URL. - * - * This can only be done given a valid $page object. - * - * @param moodle_page $page the page to add blocks to. - * @return boolean true if anything was done. False if not. - */ -function block_process_url_actions($page) { - return block_process_url_add($page) || - block_process_url_delete($page) || - block_process_url_show_hide($page); -} - -/** - * Handle adding a block. - * @param moodle_page $page the page to add blocks to. - * @return boolean true if anything was done. False if not. - */ -function block_process_url_add($page) { - $blocktype = optional_param('bui_addblock', null, PARAM_SAFEDIR); - if (!$blocktype) { - return false; - } - - confirm_sesskey(); - - if (!$page->user_is_editing() && !$page->user_can_edit_blocks()) { - throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('addblock')); - } - - if (!array_key_exists($blocktype, $page->blocks->get_addable_blocks())) { - throw new moodle_exception('cannotaddthisblocktype', '', $page->url->out(), $blocktype); - } - - $page->blocks->add_block_at_end_of_default_region($blocktype); - - // 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_addblock'); - - return true; -} - -/** - * Handle deleting a block. - * @param moodle_page $page the page to add blocks to. - * @return boolean true if anything was done. False if not. - */ -function block_process_url_delete($page) { - $blockid = optional_param('bui_deleteid', null, PARAM_INTEGER); - if (!$blockid) { - return false; - } - - confirm_sesskey(); - - $block = $page->blocks->find_instance($blockid); - - if (!$block->user_can_edit() || !$page->user_can_edit_blocks() || !$block->user_can_addto($page)) { - throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('deleteablock')); - } - - blocks_delete_instance($block->instance); - - // 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_deleteid'); - - return true; -} - -/** - * Handle showing or hiding a block. - * @param moodle_page $page the page to add blocks to. - * @return boolean true if anything was done. False if not. - */ -function block_process_url_show_hide($page) { - 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; -} - -///** -// * Handle deleting a block. -// * @param moodle_page $page the page to add blocks to. -// * @return boolean true if anything was done. False if not. -// */ -//function block_process_url_delete($page) { -// -//} - // Functions that have been deprecated by block_manager ======================= /** @@ -1341,384 +1430,6 @@ function blocks_find_block($blockid, $blocksarray) { return false; } -/** - * TODO Document this function, description - * - * @param object $page The page object - * @param object $blockmanager The block manager object - * @param string $blockaction One of [config, add, delete] - * @param int|object $instanceorid The instance id or a block_instance object - * @param bool $pinned - * @param bool $redirect To redirect or not to that is the question but you should stick with true - */ -function blocks_execute_action($page, &$blockmanager, $blockaction, $instanceorid, $pinned=false, $redirect=true) { - global $CFG, $USER, $DB; - - if (!in_array($blockaction, array('config', 'add', 'delete'))) { - throw new moodle_exception('Sorry, blocks editing is currently broken. Will be fixed. See MDL-19010.'); - } - - if (is_int($instanceorid)) { - $blockid = $instanceorid; - } else if (is_object($instanceorid)) { - $instance = $instanceorid; - } - - switch($blockaction) { - case 'config': - // First of all check to see if the block wants to be edited - if(!$instance->user_can_edit()) { - break; - } - - // Now get the title and AFTER that load up the instance - $blocktitle = $instance->get_title(); - - // 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' => sesskey(), - 'instanceid' => $instance->instance->id, - 'blockaction' => 'config' - ); - - // To this data, add anything the page itself needs to display - $hiddendata = $page->url->params($hiddendata); - - if ($data = data_submitted()) { - $remove = array_keys($hiddendata); - foreach($remove as $item) { - unset($data->$item); - } - $instance->instance_config_save($data); - redirect($page->url->out()); - - } else { - // We need to show the config screen, so we highjack the display logic and then die - $strheading = get_string('blockconfiga', 'moodle', $blocktitle); - $nav = build_navigation($strheading, $page->cm); - print_header($strheading, $strheading, $nav); - - echo '