From: tjhunt Date: Mon, 13 Jul 2009 08:37:34 +0000 (+0000) Subject: block contexts: MDL-19098 every block should have a context X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e92c286c20345f513f72bc7b12e3f86fc079d4d2;p=moodle.git block contexts: MDL-19098 every block should have a context even non-course and sticky blocks. The parent context is block_instances.parentcontextid. The block context should be used for checking permissions directly related to the block, like moodle/block:view or moodle/site:manageblocks. However, if the block is displaying information about the current page, for example the participants block showing who 'here', then it may be better to use the context of the page where the bloack is appearing - in other words $this->page->context - to check permissions about the user's ability to see participants here. Or, if the block is displaying something stronly related to courses, for example, a course meny block, the block should probably use the context for $this->page->course to check permissions. --- diff --git a/admin/replace.php b/admin/replace.php index 5f72748671..ff145dc272 100644 --- a/admin/replace.php +++ b/admin/replace.php @@ -46,7 +46,7 @@ $instances = $DB->get_recordset('block_instances', array('blockname' => 'html')) foreach ($instances as $instance) { $blockobject = block_instance('html', $instance); $blockobject->config->text = str_replace($search, $replace, $blockobject->config->text); - $blockobject->instance_config_commit($blockobject->pinned); + $blockobject->instance_config_commit(); } $instances->close(); diff --git a/admin/roles/assign.php b/admin/roles/assign.php index afcb7004fe..54625a2619 100755 --- a/admin/roles/assign.php +++ b/admin/roles/assign.php @@ -43,13 +43,15 @@ $extendperiod = optional_param('extendperiod', 0, PARAM_INT); $extendbase = optional_param('extendbase', 3, PARAM_INT); - $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $contextid; + $urlparams = array('contextid' => $contextid); if (!empty($userid)) { - $baseurl .= '&userid='.$userid; + $urlparams['userid'] = $userid; } if ($courseid && $courseid != SITEID) { - $baseurl .= '&courseid='.$courseid; + $urlparams['courseid'] = $courseid; } + $PAGE->set_url($CFG->admin . '/roles/assign.php', $urlparams); + $baseurl = $PAGE->url->out(); if (! $context = get_context_instance_by_id($contextid)) { print_error('wrongcontextid', 'error'); @@ -149,11 +151,8 @@ /// Create the user selector objects. $options = array('context' => $context, 'roleid' => $roleid); - if ($context->contextlevel > CONTEXT_COURSE && !is_inside_frontpage($context)) { - $potentialuserselector = new potential_assignees_below_course('addselect', $options); - } else { - $potentialuserselector = new potential_assignees_course_and_above('addselect', $options); - } + + $potentialuserselector = roles_get_potential_user_selector($context, 'addselect', $options); if ($context->contextlevel == CONTEXT_SYSTEM && is_admin_role($roleid)) { $currentuserselector = new existing_role_holders_site_admin('removeselect', $options); } else { @@ -377,7 +376,7 @@ } else if (empty($assignableroles)) { /// Print a message that there are no roles that can me assigned here. - print_heading(get_string('notabletoassignroleshere', 'role'), 'center', 3); + print_heading(get_string('notabletoassignroleshere', 'role'), '', 3); } else { /// Show UI for choosing a role to assign. @@ -388,7 +387,7 @@ } // Print instruction - print_heading(get_string('chooseroletoassign', 'role'), 'center', 3); + print_heading(get_string('chooseroletoassign', 'role'), '', 3); // sync metacourse enrolments if needed if ($inmeta) { @@ -452,6 +451,6 @@ if ($context->contextlevel == CONTEXT_SYSTEM || $isfrontpage) { admin_externalpage_print_footer(); } else { - print_footer($course); + print_footer(); } ?> diff --git a/admin/roles/check.php b/admin/roles/check.php index abd55d8447..4153ae8db5 100755 --- a/admin/roles/check.php +++ b/admin/roles/check.php @@ -37,6 +37,15 @@ $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs + $urlparams = array('contextid' => $contextid); + if (!empty($userid)) { + $urlparams['userid'] = $userid; + } + if ($courseid && $courseid != SITEID) { + $urlparams['courseid'] = $courseid; + } + $PAGE->set_url($CFG->admin . '/roles/check.php', $urlparams); + if (! $context = get_context_instance_by_id($contextid)) { print_error('wrongcontextid', 'error'); } @@ -75,10 +84,10 @@ /// Teachers within a course just get to see the same list of people they can /// assign roles to. Admins (people with moodle/role:manage) can run this report for any user. $options = array('context' => $context, 'roleid' => 0); - if ($context->contextlevel > CONTEXT_COURSE && !is_inside_frontpage($context) && !has_capability('moodle/role:manage', $context)) { - $userselector = new potential_assignees_below_course('reportuser', $options); - } else { + if (has_capability('moodle/role:manage', $context)) { $userselector = new potential_assignees_course_and_above('reportuser', $options); + } else { + $userselector = roles_get_potential_user_selector($context, 'reportuser', $options); } $userselector->set_multiselect(false); $userselector->set_rows(10); @@ -177,5 +186,5 @@ get_string('backto', '', $contextname) . ''; } - print_footer($course); + print_footer(); ?> diff --git a/admin/roles/lib.php b/admin/roles/lib.php index a67421f0d4..792be3633e 100644 --- a/admin/roles/lib.php +++ b/admin/roles/lib.php @@ -961,7 +961,7 @@ abstract class role_assign_user_selector_base extends user_selector_base { /** * User selector subclass for the list of potential users on the assign roles page, * when we are assigning in a context below the course level. (CONTEXT_MODULE and - * CONTEXT_BLOCK). + * some CONTEXT_BLOCK). * * In this case we replicate part of get_users_by_capability() get the users * with moodle/course:view (or moodle/site:doanything). We can't use @@ -1468,4 +1468,32 @@ class role_allow_switch_page extends role_allow_role_page { } } +/** + * Get the potential assignees selector for a given context. + * + * If this context is a course context, or inside a course context (module or + * some blocks) then return a potential_assignees_below_course object. Otherwise + * return a potential_assignees_course_and_above. + * + * @param $context a context. + * @param $name passed to user selector constructor. + * @param $options to user selector constructor. + * @return user_selector_base an appropriate user selector. + */ +function roles_get_potential_user_selector($context, $name, $options) { + $blockinsidecourse = false; + if ($context->contextlevel == CONTEXT_BLOCK) { + $parentcontext = get_context_instance_by_id(get_parent_contextid($context)); + $blockinsidecourse = in_array($parentcontext->contextlevel, array(CONTEXT_MODULE, CONTEXT_COURSE)); + } + + if (($context->contextlevel == CONTEXT_MODULE || $blockinsidecourse) && + !is_inside_frontpage($context)) { + $potentialuserselector = new potential_assignees_below_course('addselect', $options); + } else { + $potentialuserselector = new potential_assignees_course_and_above('addselect', $options); + } + return $potentialuserselector; +} + ?> \ No newline at end of file diff --git a/admin/roles/override.php b/admin/roles/override.php index d85cb30556..85ce08860c 100755 --- a/admin/roles/override.php +++ b/admin/roles/override.php @@ -39,13 +39,15 @@ $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs /// Get the base URL for this and related pages into a convenient variable. - $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/override.php?contextid=' . $contextid; + $urlparams = array('contextid' => $contextid); if (!empty($userid)) { - $baseurl .= '&userid=' . $userid; + $urlparams['userid'] = $userid; } if ($courseid && $courseid != SITEID) { - $baseurl .= '&courseid=' . $courseid; + $urlparams['courseid'] = $courseid; } + $PAGE->set_url($CFG->admin . '/roles/override.php', $urlparams); + $baseurl = $PAGE->url->out(); /// Validate the contextid parameter. if (!$context = $DB->get_record('context', array('id'=>$contextid))) { @@ -258,6 +260,5 @@ } } - print_footer($course); - + print_footer(); ?> \ No newline at end of file diff --git a/admin/roles/tabs.php b/admin/roles/tabs.php index 1d37a0dfa4..0d106bb60b 100755 --- a/admin/roles/tabs.php +++ b/admin/roles/tabs.php @@ -121,7 +121,7 @@ if ($currenttab != 'update') { if ($blockinstance = $DB->get_record('block_instances', array('id' => $context->instanceid))) { $blockname = print_context_name($context); - $parentcontext = get_context_instance_by_id($blockinstance->contextid); + $parentcontext = get_context_instance_by_id($blockinstance->parentcontextid); $navlinks[] = array('name' => $blockname, 'link' => null, 'type' => 'misc'); $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc'); switch ($parentcontext->contextlevel) { diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php index 9b0cddfc93..bfe5f8c5d6 100644 --- a/blocks/admin/block_admin.php +++ b/blocks/admin/block_admin.php @@ -18,10 +18,9 @@ class block_admin extends block_list { $this->content->icons = array(); $this->content->footer = ''; - $context = $this->page->context; $course = $this->page->course; - if (!has_capability('moodle/course:view', $context)) { // Just return + if (!has_capability('moodle/course:view', $this->page->context)) { // Just return return $this->content; } @@ -32,7 +31,7 @@ class block_admin extends block_list { } /// Course editing on/off - if ($course->id !== SITEID and has_capability('moodle/course:update', $context)) { + if ($course->id !== SITEID and has_capability('moodle/course:update', $this->page->context)) { $this->content->icons[]=''; if ($this->page->user_is_editing()) { $this->content->items[]=''.get_string('turneditingoff').''; @@ -46,11 +45,11 @@ class block_admin extends block_list { /// Assign roles to the course if ($course->id != SITEID) { - if (has_capability('moodle/role:assign', $context)) { - $this->content->items[]=''.get_string('assignroles', 'role').''; + if (has_capability('moodle/role:assign', $this->page->context)) { + $this->content->items[]=''.get_string('assignroles', 'role').''; $this->content->icons[]=''; - } else if (get_overridable_roles($context, ROLENAME_ORIGINAL)) { - $this->content->items[]=''.get_string('overridepermissions', 'role').''; + } else if (get_overridable_roles($this->page->context, ROLENAME_ORIGINAL)) { + $this->content->items[]=''.get_string('overridepermissions', 'role').''; $this->content->icons[]=''; } } @@ -59,13 +58,13 @@ class block_admin extends block_list { /// find all accessible reports if ($course->id !== SITEID) { $reportavailable = false; - if (has_capability('moodle/grade:viewall', $context)) { + if (has_capability('moodle/grade:viewall', $this->page->context)) { $reportavailable = true; } else if (!empty($course->showgrades)) { if ($reports = get_plugin_list('gradereport')) { // Get all installed reports arsort($reports); // user is last, we want to test it first foreach ($reports as $plugin => $plugindir) { - if (has_capability('gradereport/'.$plugin.':view', $context)) { + if (has_capability('gradereport/'.$plugin.':view', $this->page->context)) { //stop when the first visible plugin is found $reportavailable = true; break; @@ -82,7 +81,7 @@ class block_admin extends block_list { /// Course outcomes (to help give it more prominence because it's important) if (!empty($CFG->enableoutcomes)) { - if ($course->id!==SITEID and has_capability('moodle/course:update', $context)) { + if ($course->id!==SITEID and has_capability('moodle/course:update', $this->page->context)) { $this->content->items[]=''.get_string('outcomes', 'grades').''; $this->content->icons[]=''; } @@ -90,11 +89,11 @@ class block_admin extends block_list { /// Manage metacourses if ($course->metacourse) { - if (has_capability('moodle/course:managemetacourse', $context)) { + if (has_capability('moodle/course:managemetacourse', $this->page->context)) { $strchildcourses = get_string('childcourses'); $this->content->items[]=''.$strchildcourses.''; $this->content->icons[]=''; - } else if (has_capability('moodle/role:assign', $context)) { + } else if (has_capability('moodle/role:assign', $this->page->context)) { $strchildcourses = get_string('childcourses'); $this->content->items[]=''.$strchildcourses.''; $this->content->icons[]=''; @@ -103,38 +102,38 @@ class block_admin extends block_list { /// Manage groups in this course - if (($course->id!==SITEID) && ($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $context)) { + if (($course->id!==SITEID) && ($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $this->page->context)) { $strgroups = get_string('groups'); $this->content->items[]=''.$strgroups.''; $this->content->icons[]=''; } /// Backup this course - if ($course->id!==SITEID and has_capability('moodle/site:backup', $context)) { + if ($course->id!==SITEID and has_capability('moodle/site:backup', $this->page->context)) { $this->content->items[]=''.get_string('backup').''; $this->content->icons[]=''; } /// Restore to this course - if ($course->id !== SITEID and has_capability('moodle/site:restore', $context)) { + if ($course->id !== SITEID and has_capability('moodle/site:restore', $this->page->context)) { $this->content->items[]=''.get_string('restore').''; $this->content->icons[]=''; } /// Import data from other courses - if ($course->id !== SITEID and has_capability('moodle/site:import', $context)) { + if ($course->id !== SITEID and has_capability('moodle/site:import', $this->page->context)) { $this->content->items[]=''.get_string('import').''; $this->content->icons[]=''; } /// Reset this course - if ($course->id!==SITEID and has_capability('moodle/course:reset', $context)) { + if ($course->id!==SITEID and has_capability('moodle/course:reset', $this->page->context)) { $this->content->items[]=''.get_string('reset').''; $this->content->icons[]=''; } /// View course reports - if ($course->id !== SITEID and has_capability('moodle/site:viewreports', $context)) { // basic capability for listing of reports + if ($course->id !== SITEID and has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports $this->content->items[]=''.get_string('reports').''; $this->content->icons[]=''; } @@ -151,12 +150,12 @@ class block_admin extends block_list { 'moodle/question:movemine', 'moodle/question:moveall'); foreach ($questioncaps as $questioncap){ - if (has_capability($questioncap, $context)){ + if (has_capability($questioncap, $this->page->context)){ $questionlink = 'edit.php'; break; } } - if (!$questionlink && has_capability('moodle/question:managecategory', $context)) { + if (!$questionlink && has_capability('moodle/question:managecategory', $this->page->context)) { $questionlink = 'category.php'; } if ($questionlink) { @@ -168,14 +167,14 @@ class block_admin extends block_list { /// Repository Instances require_once($CFG->dirroot.'/repository/lib.php'); - $editabletypes = repository::get_editable_types($context); - if ($course->id !== SITEID && has_capability('moodle/course:update', $context) && !empty($editabletypes)) { - $this->content->items[]=''.get_string('repositories').''; + $editabletypes = repository::get_editable_types($this->page->context); + if ($course->id !== SITEID && has_capability('moodle/course:update', $this->page->context) && !empty($editabletypes)) { + $this->content->items[]=''.get_string('repositories').''; $this->content->icons[]=''; } /// Manage files - if ($course->id !== SITEID and has_capability('moodle/course:managefiles', $context)) { + if ($course->id !== SITEID and has_capability('moodle/course:managefiles', $this->page->context)) { $this->content->items[]=''.get_string('files').''; $this->content->icons[]=''; } @@ -184,7 +183,7 @@ class block_admin extends block_list { if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize') && ($course->id!==SITEID)) { require_once($CFG->dirroot.'/enrol/authorize/const.php'); $paymenturl = ''.get_string('payments').' '; - if (has_capability('enrol/authorize:managepayments', $context)) { + if (has_capability('enrol/authorize:managepayments', $this->page->context)) { if ($cnt = $DB->count_records('enrol_authorize', array('status'=>AN_STATUS_AUTH, 'courseid'=>$course->id))) { $paymenturl .= ''.get_string('paymentpending', 'moodle', $cnt).''; } @@ -195,10 +194,10 @@ class block_admin extends block_list { /// Unenrol link if (empty($course->metacourse) && ($course->id!==SITEID)) { - if (has_capability('moodle/legacy:guest', $context, NULL, false)) { // Are a guest now + if (has_capability('moodle/legacy:guest', $this->page->context, NULL, false)) { // Are a guest now $this->content->items[]=''.get_string('enrolme', '', format_string($course->shortname)).''; $this->content->icons[]=''; - } else if (has_capability('moodle/role:unassignself', $context, NULL, false) and get_user_roles($context, $USER->id, false)) { // Have some role + } else if (has_capability('moodle/role:unassignself', $this->page->context, NULL, false) and get_user_roles($this->page->context, $USER->id, false)) { // Have some role $this->content->items[]=''.get_string('unenrolme', '', format_string($course->shortname)).''; $this->content->icons[]=''; } diff --git a/blocks/html/block_html.php b/blocks/html/block_html.php index e55f5785db..16be06b64d 100755 --- a/blocks/html/block_html.php +++ b/blocks/html/block_html.php @@ -25,7 +25,7 @@ class block_html extends block_base { } if ($this->content_is_trusted()) { - // fancy html allowed only on course page and in pinned blocks for security reasons + // fancy html allowed only on course, category and system blocks. $filteropt = new stdClass; $filteropt->noclean = true; } else { @@ -88,7 +88,7 @@ class block_html extends block_base { $blockobject = block_instance('html', $instance); $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text); $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore); - $blockobject->instance_config_commit($blockobject->pinned); + $blockobject->instance_config_commit(); } } } diff --git a/blocks/mentees/block_mentees.php b/blocks/mentees/block_mentees.php index cf645fe4e1..1c7dbc745d 100755 --- a/blocks/mentees/block_mentees.php +++ b/blocks/mentees/block_mentees.php @@ -38,10 +38,9 @@ class block_mentees extends block_base { foreach ($usercontexts as $usercontext) { $this->content->text .= '
  • '.fullname($usercontext).'
  • '; } - $this->content->text .= ''; + $this->content->text .= ''; } - - + $this->content->footer = ''; return $this->content; diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 9f1bf0159d..8a4626f458 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -75,6 +75,12 @@ class block_base { */ public $page = NULL; + /** + * This blocks's context. + * @var stdClass + */ + public $context = NULL; + /** * An object containing the instance configuration information for the current instance of this block. * @var stdObject $config @@ -88,11 +94,6 @@ class block_base { var $cron = NULL; - /** - * Indicates blocked is pinned - can not be moved, always present, does not have own context - */ - var $pinned = false; - /// Class Functions /** @@ -300,16 +301,7 @@ class block_base { * @return boolean */ function is_empty() { - - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } - - if ( !has_capability('moodle/block:view', $context) ) { + if ( !has_capability('moodle/block:view', $this->context) ) { return true; } @@ -419,15 +411,7 @@ class block_base { * @since Moodle 2.0. */ protected function get_edit_controls($output) { - global $CFG, $DB; - - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } + global $CFG; $returnurlparam = '&returnurl=' . urlencode($this->page->url->out_returnurl()); $actionurl = $CFG->wwwroot.'/blocks/action.php?block=' . $this->instance->id . @@ -436,8 +420,8 @@ class block_base { $controls = array(); // Assign roles icon. - if ($context->contextlevel == CONTEXT_BLOCK && has_capability('moodle/role:assign', $context)) { - $controls[] = array('url' => $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id, + if (has_capability('moodle/role:assign', $this->context)) { + $controls[] = array('url' => $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$this->context->id, 'icon' => $output->old_icon_url('i/roles'), 'caption' => get_string('assignroles', 'role')); } @@ -604,12 +588,12 @@ class block_base { if (!empty($instance->configdata)) { $this->config = unserialize(base64_decode($instance->configdata)); } - // [pj] This line below is supposed to be an optimization (we don't need configdata anymore) - // but what it does is break in PHP5 because the same instance object will be passed to - // this function twice in each page view, and the second time it won't have any configdata - // so it won't work correctly. Thus it's commented out. - // unset($instance->configdata); $this->instance = $instance; + if (isset($instance->context)) { + $this->context = $instance->context; + } else { + $this->context = get_context_instance(CONTEXT_BLOCK, $instance->id); + } $this->page = $page; $this->specialization(); } @@ -795,16 +779,7 @@ class block_list extends block_base { var $content_type = BLOCK_TYPE_LIST; function is_empty() { - - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } - - if ( !has_capability('moodle/block:view', $context) ) { + if ( !has_capability('moodle/block:view', $this->context) ) { return true; } diff --git a/blocks/online_users/block_online_users.php b/blocks/online_users/block_online_users.php index d51cdc6cbb..c850cc2cb4 100644 --- a/blocks/online_users/block_online_users.php +++ b/blocks/online_users/block_online_users.php @@ -34,21 +34,10 @@ class block_online_users extends block_base { } $timefrom = 100 * floor((time()-$timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache - // Get context so we can check capabilities. - $context = $this->page->context; - - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } - //Calculate if we are in separate groups $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS && $this->page->course->groupmodeforce - && !has_capability('moodle/site:accessallgroups', $context)); + && !has_capability('moodle/site:accessallgroups', $this->page->context)); //Get the user current group $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL; @@ -81,8 +70,8 @@ class block_online_users extends block_base { GROUP BY u.id"; } else { // Course-level - if (!has_capability('moodle/role:viewhiddenassigns', $context)) { - $pcontext = get_related_contexts_string($context); + if (!has_capability('moodle/role:viewhiddenassigns', $this->page->context)) { + $pcontext = get_related_contexts_string($this->page->context); $rafrom = ", {role_assignments} ra"; $rawhere = " AND ra.userid = u.id AND ra.contextid $pcontext AND ra.hidden = 0"; } @@ -111,7 +100,7 @@ class block_online_users extends block_base { $minutes = floor($timetoshowusers/60); // Verify if we can see the list of users, if not just print number of users - if (!has_capability('block/online_users:viewlist', $context)) { + if (!has_capability('block/online_users:viewlist', $this->page->context)) { if (!$usercount = $DB->count_records_sql($csql, $params)) { $usercount = get_string("none"); } @@ -142,7 +131,7 @@ class block_online_users extends block_base { //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string). //Accessibility: Converted
    to
    '; // Closes off page form - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } print_box_start(); rss_display_feeds($id, $USER->id, '', $context); - rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context); + rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $this->context); print_box_end(); } ?> diff --git a/blocks/rss_client/config_instance_tabs.php b/blocks/rss_client/config_instance_tabs.php index a577497a15..a7ac1d45d8 100644 --- a/blocks/rss_client/config_instance_tabs.php +++ b/blocks/rss_client/config_instance_tabs.php @@ -5,15 +5,7 @@ global $USER; $tabs = $row = array(); - // TODO - temporary hack to get the block context only if it already exists. - global $DB; - if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) { - $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id); - } else { - $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context - } - -if (has_capability('moodle/site:manageblocks', $context)) { +if (has_capability('moodle/site:manageblocks', $this->context)) { $script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id, 'section' => 'rss')); $row[] = new tabobject('configblock', $script, get_string('configblock', 'block_rss_client')); diff --git a/lib/accesslib.php b/lib/accesslib.php index 325bd485b5..3124f5024a 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -483,8 +483,9 @@ function has_capability($capability, $context, $userid=NULL, $doanything=true) { if (empty($USER->id)) { // Session not set up yet. $userid = 0; + } else { + $userid = $USER->id; } - $userid = $USER->id; } if (is_null($context->path) or $context->depth == 0) { @@ -2221,11 +2222,10 @@ function create_context($contextlevel, $instanceid) { break; case CONTEXT_BLOCK: - // Only non-pinned & course-page based $sql = "SELECT ctx.path, ctx.depth FROM {context} ctx JOIN {block_instances} bi ON (bi.parentcontextid=ctx.id) - WHERE bi.id=? AND ctx.contextlevel=?"; + WHERE bi.id = ?"; $params = array($instanceid, CONTEXT_COURSE); if ($p = $DB->get_record_sql($sql, $params)) { $basepath = $p->path; @@ -2410,7 +2410,8 @@ function create_contexts($contextlevel=null, $buildpaths=true) { } - if (empty($contextlevel) or $contextlevel == CONTEXT_MODULE) { + if (empty($contextlevel) or $contextlevel == CONTEXT_MODULE + or $contextlevel == CONTEXT_BLOCK) { $sql = "INSERT INTO {context} (contextlevel, instanceid) SELECT ".CONTEXT_MODULE.", cm.id FROM {course}_modules cm @@ -2420,17 +2421,8 @@ function create_contexts($contextlevel=null, $buildpaths=true) { $DB->execute($sql); } - if (empty($contextlevel) or $contextlevel == CONTEXT_BLOCK) { - $sql = "INSERT INTO {context} (contextlevel, instanceid) - SELECT ".CONTEXT_BLOCK.", bi.id - FROM {block_instances} bi - WHERE NOT EXISTS (SELECT 'x' - FROM {context} cx - WHERE bi.id = cx.instanceid AND cx.contextlevel=".CONTEXT_BLOCK.")"; - $DB->execute($sql); - } - - if (empty($contextlevel) or $contextlevel == CONTEXT_USER) { + if (empty($contextlevel) or $contextlevel == CONTEXT_USER + or $contextlevel == CONTEXT_BLOCK) { $sql = "INSERT INTO {context} (contextlevel, instanceid) SELECT ".CONTEXT_USER.", u.id FROM {user} u @@ -2442,6 +2434,16 @@ function create_contexts($contextlevel=null, $buildpaths=true) { } + if (empty($contextlevel) or $contextlevel == CONTEXT_BLOCK) { + $sql = "INSERT INTO {context} (contextlevel, instanceid) + SELECT ".CONTEXT_BLOCK.", bi.id + FROM {block_instances} bi + WHERE NOT EXISTS (SELECT 'x' + FROM {context} cx + WHERE bi.id = cx.instanceid AND cx.contextlevel=".CONTEXT_BLOCK.")"; + $DB->execute($sql); + } + if ($buildpaths) { build_context_path(false); } @@ -2513,10 +2515,10 @@ function cleanup_contexts() { } /** - * Preloads all contexts relating to a course: course, modules, and blocks. + * Preloads all contexts relating to a course: course, modules. Block contexts + * are no longer loaded here. The contexts for all the blocks on the current + * page are now efficiently loaded by {@link block_manager::load_blocks()}. * - * @global object - * @global object * @param int $courseid Course ID * @return void */ @@ -2535,15 +2537,6 @@ function preload_course_contexts($courseid) { JOIN {context} x ON x.instanceid=cm.id WHERE cm.course=? AND x.contextlevel=".CONTEXT_MODULE." - UNION ALL - - SELECT x.instanceid, x.id, x.contextlevel, x.path, x.depth - FROM {context} px - JOIN {block_instances} bi ON bi.parentcontextid = px.id - JOIN {context} x ON x.instanceid=bi.id - WHERE px.instanceid = ? AND px.contextlevel = ".CONTEXT_COURSE." - AND x.contextlevel=".CONTEXT_BLOCK." - UNION ALL SELECT x.instanceid, x.id, x.contextlevel, x.path, x.depth @@ -2564,8 +2557,6 @@ function preload_course_contexts($courseid) { * * @todo Remove code branch from previous fix MDL-9016 which is no longer needed * - * @global object - * @global object * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0 @@ -3482,7 +3473,7 @@ function print_context_name($context, $withprefix = true, $short = false) { $name = ''; switch ($context->contextlevel) { - case CONTEXT_SYSTEM: // by now it's a definite an inherit + case CONTEXT_SYSTEM: $name = get_string('coresystem'); break; @@ -3495,7 +3486,7 @@ function print_context_name($context, $withprefix = true, $short = false) { } break; - case CONTEXT_COURSECAT: // Coursecat -> coursecat or site + case CONTEXT_COURSECAT: if ($category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) { if ($withprefix){ $name = get_string('category').': '; @@ -3504,7 +3495,7 @@ function print_context_name($context, $withprefix = true, $short = false) { } break; - case CONTEXT_COURSE: // 1 to 1 to course cat + case CONTEXT_COURSE: if ($context->instanceid == SITEID) { $name = get_string('frontpage', 'admin'); } else { @@ -3521,7 +3512,7 @@ function print_context_name($context, $withprefix = true, $short = false) { } break; - case CONTEXT_MODULE: // 1 to 1 to course + case CONTEXT_MODULE: if ($cm = $DB->get_record_sql('SELECT cm.*, md.name AS modname FROM {course_modules} cm ' . 'JOIN {modules} md ON md.id = cm.module WHERE cm.id = ?', array($context->instanceid))) { if ($mod = $DB->get_record($cm->modname, array('id' => $cm->instance))) { @@ -3533,7 +3524,7 @@ function print_context_name($context, $withprefix = true, $short = false) { } break; - case CONTEXT_BLOCK: // not necessarily 1 to 1 to course + case CONTEXT_BLOCK: if ($blockinstance = $DB->get_record('block_instances', array('id'=>$context->instanceid))) { global $CFG; require_once("$CFG->dirroot/blocks/moodleblock.class.php"); @@ -3893,14 +3884,12 @@ function get_sorted_contexts($select, $params = array()) { * displayed in the course page. * * For course category contexts it will return categories and courses. It will - * NOT recurse into courses - if you want to do that, call it on the returned - * courses. + * NOT recurse into courses, nor return blocks on the category pages. If you + * want to do that, call it on the returned courses. * * If called on a course context it _will_ populate the cache with the appropriate * contexts ;-) * - * @global object - * @global object * @param object $context. * @return array Array of child records */ @@ -3920,14 +3909,23 @@ function get_child_contexts($context) { break; case CONTEXT_MODULE: - // No children. - return array(); - break; + // Find + // - blocks under this context path. + $sql = " SELECT ctx.* + FROM {context} ctx + WHERE ctx.path LIKE ? + AND ctx.contextlevel = ".CONTEXT_BLOCK; + $params = array("{$context->path}/%", $context->instanceid); + $records = $DB->get_recordset_sql($sql, $params); + foreach ($records as $rec) { + cache_context($rec); + } + return $records; + break; case CONTEXT_COURSE: // Find - // - module instances - easy - // - blocks assigned to the course-view page explicitly - easy + // - modules and blocks under this context path. $sql = " SELECT ctx.* FROM {context} ctx WHERE ctx.path LIKE ? @@ -3957,9 +3955,20 @@ function get_child_contexts($context) { break; case CONTEXT_USER: - // No children. - return array(); - break; + // Find + // - blocks under this context path. + $sql = " SELECT ctx.* + FROM {context} ctx + WHERE ctx.path LIKE ? + AND ctx.contextlevel = ".CONTEXT_BLOCK; + $params = array("{$context->path}/%", $context->instanceid); + $records = $DB->get_recordset_sql($sql, $params); + foreach ($records as $rec) { + cache_context($rec); + } + return $records; + break; + break; case CONTEXT_SYSTEM: // Just get all the contexts except for CONTEXT_SYSTEM level @@ -3973,7 +3982,7 @@ function get_child_contexts($context) { default: print_error('unknowcontext', '', '', $context->contextlevel); - return false; + return false; } } @@ -5828,10 +5837,16 @@ function role_fix_names($roleoptions, $context, $rolenamedisplay=ROLENAME_ALIAS) // If necessary, get the localised names. if ($rolenamedisplay != ROLENAME_ORIGINAL && !empty($context->id)) { // Make sure we have a course context. - if ($context->contextlevel == CONTEXT_MODULE || $context->contextlevel == CONTEXT_BLOCK) { // find the parent course context + if ($context->contextlevel == CONTEXT_MODULE) { if ($parentcontextid = array_shift(get_parent_contexts($context))) { $context = get_context_instance_by_id($parentcontextid); } + } else if ($context->contextlevel == CONTEXT_BLOCK) { + do { + if ($parentcontextid = array_shift(get_parent_contexts($context))) { + $context = get_context_instance_by_id($parentcontextid); + } + } while ($parentcontextid && $context->contextlevel != CONTEXT_COURSE); } // The get the relevant renames, and use them. @@ -5925,8 +5940,6 @@ function rebuild_contexts(array $fixcontexts) { /** * Populate context.path and context.depth where missing. * - * @global object - * @global object * @param bool $force force a complete rebuild of the path and depth fields, defaults to false */ function build_context_path($force=false) { diff --git a/lib/blocklib.php b/lib/blocklib.php index f6cb661198..f667c86b23 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -407,6 +407,7 @@ class block_manager { 'contextid1' => $context->id, 'contextid2' => $context->id, 'pagetype' => $this->page->pagetype, + 'contextblock' => CONTEXT_BLOCK, ); $sql = "SELECT bi.id, @@ -419,7 +420,11 @@ class block_manager { COALESCE(bp.visible, 1) AS visible, COALESCE(bp.region, bi.defaultregion) AS region, COALESCE(bp.weight, bi.defaultweight) AS weight, - bi.configdata + bi.configdata, + ctx.id AS ctxid, + ctx.path AS ctxpath, + ctx.depth AS ctxdepth, + ctx.contextlevel AS ctxlevel FROM {block_instances} bi JOIN {block} b ON bi.blockname = b.name @@ -427,6 +432,8 @@ class block_manager { AND bp.contextid = :contextid1 AND bp.pagetype = :pagetype AND bp.subpage = :subpage1 + JOIN {context} ctx ON ctx.contextlevel = :contextblock + AND ctx.instanceid = bi.id WHERE $contexttest @@ -444,6 +451,7 @@ class block_manager { $this->birecordsbyregion = $this->prepare_per_region_arrays(); $unknown = array(); foreach ($blockinstances as $bi) { + $bi = make_context_subobj($bi); if ($this->is_known_region($bi->region)) { $this->birecordsbyregion[$bi->region][] = $bi; } else { @@ -491,12 +499,11 @@ class block_manager { $blockinstance->configdata = ''; $blockinstance->id = $DB->insert_record('block_instances', $blockinstance); - if ($this->page->context->contextlevel == CONTEXT_COURSE) { - get_context_instance(CONTEXT_BLOCK, $blockinstance->id); - } + // Ensure the block context is created. + get_context_instance(CONTEXT_BLOCK, $blockinstance->id); // If the new instance was created, allow it to do additional setup - if($block = block_instance($blockname, $blockinstance)) { + if ($block = block_instance($blockname, $blockinstance)) { $block->instance_create(); } } diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 27d603694d..8371ff076f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2302,8 +2302,29 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); upgrade_main_savepoint($result, 2009071000); } + if ($result && $oldversion < 2009071300) { + + /// Create contexts for every block. In the past, only non-sticky course block had contexts. + /// This is a copy of the code in create_contexts. + $sql = "INSERT INTO {context} (contextlevel, instanceid) + SELECT " . CONTEXT_BLOCK . ", bi.id + FROM {block_instances} bi + WHERE NOT EXISTS (SELECT 'x' + FROM {context} ctx + WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")"; + $DB->execute($sql); + + /// TODO MDL-19776 We should not really use API funcitons in upgrade. + /// If MDL-19776 is done, we can remove this whole upgrade block. + build_context_path(); + + /// Main savepoint reached + upgrade_main_savepoint($result, 2009071300); + } + return $result; } -//TODO: before 2.0 release -// 1/ remove the automatic enabling of completion lib if debug enabled \ No newline at end of file +//TODO: Before 2.0 release +// 1/ remove the automatic enabling of completion lib if debug enabled ( in 2008121701 block) +// 2/ move 2009061300 block to the top of the file so that we may log upgrade queries diff --git a/version.php b/version.php index 97234875aa..e2fd037dae 100644 --- a/version.php +++ b/version.php @@ -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 = 2009071000; // YYYYMMDD = date of the last version bump + $version = 2009071300; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20090713)'; // Human-friendly version name