diff --git a/blog/edit.php b/blog/edit.php index 6a182332238..2c367630988 100644 --- a/blog/edit.php +++ b/blog/edit.php @@ -45,7 +45,6 @@ $returnurl = new moodle_url('/blog/index.php'); if (!empty($courseid) && empty($modid)) { $returnurl->param('courseid', $courseid); - $PAGE->set_context(context_course::instance($courseid)); } // If a modid is given, guess courseid @@ -53,13 +52,12 @@ if (!empty($modid)) { $returnurl->param('modid', $modid); $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid)); $returnurl->param('courseid', $courseid); - $PAGE->set_context(context_module::instance($modid)); } -// If courseid is empty use the system context -if (empty($courseid)) { - $PAGE->set_context(context_system::instance()); -} +// Blogs are always in system context. +$sitecontext = context_system::instance(); +$PAGE->set_context($sitecontext); + $blogheaders = blog_get_headers(); @@ -77,7 +75,6 @@ if (isguestuser()) { print_error('noguestentry', 'blog'); } -$sitecontext = context_system::instance(); if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) { print_error('cannoteditentryorblog'); } diff --git a/blog/edit_form.php b/blog/edit_form.php index 42ab6e310da..2b28a737567 100644 --- a/blog/edit_form.php +++ b/blog/edit_form.php @@ -86,11 +86,9 @@ class blog_edit_form extends moodleform { $contextid = $entry->courseassoc; } - if (has_capability('moodle/blog:associatecourse', $context)) { - $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); - $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); - $mform->setDefault('courseassoc', $contextid); - } + $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); + $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); + $mform->setDefault('courseassoc', $contextid); } else if ((!empty($entry->modassoc) || !empty($modid))) { if (!empty($modid)) { @@ -108,11 +106,9 @@ class blog_edit_form extends moodleform { $modid = $context->instanceid; } - if (has_capability('moodle/blog:associatemodule', $context)) { - $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); - $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); - $mform->setDefault('modassoc', $context->id); - } + $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); + $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); + $mform->setDefault('modassoc', $context->id); } } @@ -141,14 +137,9 @@ class blog_edit_form extends moodleform { // validate course association if (!empty($data['courseassoc'])) { - $coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING); + $coursecontext = context::instance_by_id($data['courseassoc']); - $canassociatecourse = has_capability('moodle/blog:associatecourse', $coursecontext); - if ($coursecontext->contextlevel == CONTEXT_COURSE && $canassociatecourse) { - if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) { - $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); - } - } else { + if ($coursecontext->contextlevel != CONTEXT_COURSE) { $errors['courseassoc'] = get_string('error'); } } @@ -156,10 +147,9 @@ class blog_edit_form extends moodleform { // validate mod association if (!empty($data['modassoc'])) { $modcontextid = $data['modassoc']; - $modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING); + $modcontext = context::instance_by_id($modcontextid); - $canassociatemodule = has_capability('moodle/blog:associatemodule', $modcontext); - if ($canassociatemodule) { + if ($modcontext->contextlevel == CONTEXT_MODULE) { // get context of the mod's course $coursecontext = $modcontext->get_course_context(true); @@ -171,11 +161,6 @@ class blog_edit_form extends moodleform { } else { $data['courseassoc'] = $coursecontext->id; } - - // ensure the user has access to each mod's course - if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { - $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); - } } else { $errors['modassoc'] = get_string('error'); } diff --git a/blog/index.php b/blog/index.php index 94bc527cfcb..cef0eeba438 100644 --- a/blog/index.php +++ b/blog/index.php @@ -57,6 +57,8 @@ if (!empty($groupid) && empty($courseid)) { } $sitecontext = context_system::instance(); +// Blogs are always in system context. +$PAGE->set_context($sitecontext); // check basic permissions if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) { @@ -122,11 +124,9 @@ if (!empty($courseid)) { } $courseid = $course->id; - $coursecontext = context_course::instance($course->id); - require_login($course); - if (!has_capability('moodle/blog:view', $coursecontext)) { + if (!has_capability('moodle/blog:view', $sitecontext)) { print_error('cannotviewcourseblog', 'blog'); } } else { @@ -150,7 +150,7 @@ if (!empty($groupid)) { $courseid = $course->id; require_login($course); - if (!has_capability('moodle/blog:view', $coursecontext)) { + if (!has_capability('moodle/blog:view', $sitecontext)) { print_error(get_string('cannotviewcourseorgroupblog', 'blog')); } @@ -199,15 +199,6 @@ if (!empty($userid)) { $courseid = (empty($courseid)) ? SITEID : $courseid; -if (empty($entryid) && empty($modid) && empty($groupid)) { - $PAGE->set_context(context_user::instance($USER->id)); -} else if (!empty($modid)) { - $PAGE->set_context(context_module::instance($modid)); -} else if (!empty($courseid)) { - $PAGE->set_context(context_course::instance($courseid)); -} else { - $PAGE->set_context(context_system::instance()); -} $blogheaders = blog_get_headers(); @@ -217,7 +208,7 @@ if ($CFG->enablerssfeeds) { $thingid = null; list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']); if (empty($rsscontext)) { - $rsscontext = get_system_context(); + $rsscontext = context_system::instance(); } $rsstitle = $blogheaders['heading']; diff --git a/blog/lib.php b/blog/lib.php index 2ce7ca3e08e..799e0d2fc3d 100644 --- a/blog/lib.php +++ b/blog/lib.php @@ -294,57 +294,6 @@ function blog_delete_external_entries($externalblog) { array($externalblog->id)); } -/** - * Returns a URL based on the context of the current page. - * This URL points to blog/index.php and includes filter parameters appropriate for the current page. - * - * @param stdclass $context - * @return string - */ -function blog_get_context_url($context=null) { - global $CFG; - - $viewblogentriesurl = new moodle_url('/blog/index.php'); - - if (empty($context)) { - global $PAGE; - $context = $PAGE->context; - } - - // Change contextlevel to SYSTEM if viewing the site course - if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) { - $context = context_system::instance(); - } - - $filterparam = ''; - $strlevel = ''; - - switch ($context->contextlevel) { - case CONTEXT_SYSTEM: - case CONTEXT_BLOCK: - case CONTEXT_COURSECAT: - break; - case CONTEXT_COURSE: - $filterparam = 'courseid'; - $strlevel = get_string('course'); - break; - case CONTEXT_MODULE: - $filterparam = 'modid'; - $strlevel = print_context_name($context); - break; - case CONTEXT_USER: - $filterparam = 'userid'; - $strlevel = get_string('user'); - break; - } - - if (!empty($filterparam)) { - $viewblogentriesurl->param($filterparam, $context->instanceid); - } - - return $viewblogentriesurl; -} - /** * This function checks that blogs are enabled, and that the user can see blogs at all * @return bool @@ -512,10 +461,6 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) { // Check that the user can associate with the course $sitecontext = context_system::instance(); - $coursecontext = context_course::instance($course->id); - if (!has_capability('moodle/blog:associatecourse', $coursecontext)) { - return $options; - } // Generate the cache key $key = $course->id.':'; if (!empty($user)) { @@ -528,36 +473,35 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) { return $courseoptions[$key]; } - $canparticipate = (is_enrolled($coursecontext) or is_viewing($coursecontext)); - if (has_capability('moodle/blog:view', $coursecontext)) { + if (has_capability('moodle/blog:view', $sitecontext)) { // We can view! if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // View entries about this course $options['courseview'] = array( 'string' => get_string('viewcourseblogs', 'blog'), - 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id)) + 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id)) ); } // View MY entries about this course $options['courseviewmine'] = array( 'string' => get_string('viewmyentriesaboutcourse', 'blog'), - 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id)) + 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id)) ); if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { // View the provided users entries about this course $options['courseviewuser'] = array( 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)), - 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id)) + 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id)) ); } } - if (has_capability('moodle/blog:create', $sitecontext) and $canparticipate) { + if (has_capability('moodle/blog:create', $sitecontext)) { // We can blog about this course $options['courseadd'] = array( 'string' => get_string('blogaboutthiscourse', 'blog'), - 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id)) + 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id)) ); } @@ -587,12 +531,7 @@ function blog_get_options_for_module($module, $user=null) { return $options; } - // Check the user can associate with the module - $modcontext = context_module::instance($module->id); $sitecontext = context_system::instance(); - if (!has_capability('moodle/blog:associatemodule', $modcontext)) { - return $options; - } // Generate the cache key $key = $module->id.':'; @@ -606,9 +545,8 @@ function blog_get_options_for_module($module, $user=null) { return $moduleoptions[$key]; } - $canparticipate = (is_enrolled($modcontext) or is_viewing($modcontext)); - if (has_capability('moodle/blog:view', $modcontext)) { + if (has_capability('moodle/blog:view', $sitecontext)) { // Save correct module name for later usage. $modulename = get_string('modulename', $module->modname); @@ -639,7 +577,7 @@ function blog_get_options_for_module($module, $user=null) { } } - if (has_capability('moodle/blog:create', $sitecontext) and $canparticipate) { + if (has_capability('moodle/blog:create', $sitecontext)) { // The user can blog about this module $options['moduleadd'] = array( 'string' => get_string('blogaboutthismodule', 'blog', $modulename), @@ -699,23 +637,6 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $blogurl = new moodle_url('/blog/index.php'); - // If the title is not yet set, it's likely that the context isn't set either, so skip this part - $pagetitle = $PAGE->title; - if (!empty($pagetitle)) { - $contexturl = blog_get_context_url(); - - // Look at the context URL, it may have additional params that are not in the current URL - if (!$blogurl->compare($contexturl)) { - $blogurl = $contexturl; - if (empty($courseid)) { - $courseid = $blogurl->param('courseid'); - } - if (empty($modid)) { - $modid = $blogurl->param('modid'); - } - } - } - $headers['stradd'] = get_string('addnewentry', 'blog'); $headers['strview'] = null; diff --git a/blog/locallib.php b/blog/locallib.php index 67fa66d71a0..a5080526ac8 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -679,41 +679,26 @@ class blog_listing { if (empty($userid) || (!empty($userid) && $userid == $USER->id)) { - $canaddentries = true; $courseid = optional_param('courseid', null, PARAM_INT); - if ($modid = optional_param('modid', null, PARAM_INT)) { - if (!has_capability('moodle/blog:associatemodule', context_module::instance($modid))) { - $canaddentries = false; - } - } else if ($courseid) { - if (!has_capability('moodle/blog:associatecourse', context_course::instance($courseid))) { - $canaddentries = false; - } - } + $modid = optional_param('modid', null, PARAM_INT); - if ($canaddentries) { - $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php"); - $urlparams = array('action' => 'add', - 'userid' => $userid, - 'courseid' => $courseid, - 'groupid' => optional_param('groupid', null, PARAM_INT), - 'modid' => $modid, - 'tagid' => optional_param('tagid', null, PARAM_INT), - 'tag' => optional_param('tag', null, PARAM_INT), - 'search' => optional_param('search', null, PARAM_INT)); + $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php"); + $urlparams = array('action' => 'add', + 'userid' => $userid, + 'courseid' => $courseid, + 'groupid' => optional_param('groupid', null, PARAM_INT), + 'modid' => $modid, + 'tagid' => optional_param('tagid', null, PARAM_INT), + 'tag' => optional_param('tag', null, PARAM_INT), + 'search' => optional_param('search', null, PARAM_INT)); - foreach ($urlparams as $var => $val) { - if (empty($val)) { - unset($urlparams[$var]); - } - } - $addurl->params($urlparams); + $urlparams = array_filter($urlparams); + $addurl->params($urlparams); - $addlink = '