Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-25347 blog Checking association capability at course and module l…

…evels instead of system level
  • Loading branch information...
commit 15343304d205938a86557867718a7bbac1fa7e6d 1 parent e6aac11
@dmonllao dmonllao authored
Showing with 29 additions and 20 deletions.
  1. +20 −13 blog/edit_form.php
  2. +9 −7 blog/lib.php
View
33 blog/edit_form.php
@@ -71,24 +71,28 @@ function definition() {
$allmodnames = array();
if (!empty($CFG->useblogassociations)) {
- if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
+ if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
if (!empty($courseid)) {
$course = $DB->get_record('course', array('id' => $courseid));
- $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
- $context = get_context_instance(CONTEXT_COURSE, $courseid);
+ $context = context_course::instance($courseid);
$a = new stdClass();
$a->coursename = format_string($course->fullname, true, array('context' => $context));
$contextid = $context->id;
} else {
+ $context = context::instance_by_id($entry->courseassoc);
$sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
$a = new stdClass();
$a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
$contextid = $entry->courseassoc;
}
- $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)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
+ 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);
+ }
+
+ } else if ((!empty($entry->modassoc) || !empty($modid))) {
if (!empty($modid)) {
$mod = get_coursemodule_from_id(false, $modid);
$a = new stdClass();
@@ -104,9 +108,11 @@ function definition() {
$modid = $context->instanceid;
}
- $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);
+ 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);
+ }
}
}
@@ -132,13 +138,13 @@ function validation($data, $files) {
global $CFG, $DB, $USER;
$errors = array();
- $sitecontext = get_context_instance(CONTEXT_SYSTEM);
// validate course association
- if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
+ if (!empty($data['courseassoc'])) {
$coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING);
- if ($coursecontext and $coursecontext->contextlevel == CONTEXT_COURSE) {
+ $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));
}
@@ -152,7 +158,8 @@ function validation($data, $files) {
$modcontextid = $data['modassoc'];
$modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING);
- if ($modcontext and $modcontext->contextlevel == CONTEXT_MODULE) {
+ $canassociatemodule = has_capability('moodle/blog:associatecourse', $modcontext);
+ if ($modcontext->contextlevel == CONTEXT_MODULE && $canassociatemodule) {
// get context of the mod's course
$coursecontext = $modcontext->get_course_context(true);
View
16 blog/lib.php
@@ -510,8 +510,9 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
}
// Check that the user can associate with the course
- $sitecontext = get_context_instance(CONTEXT_SYSTEM);
- if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
+ $sitecontext = context_system::instance();
+ $coursecontext = context_course::instance($course->id);
+ if (!has_capability('moodle/blog:associatecourse', $coursecontext)) {
return $options;
}
// Generate the cache key
@@ -526,7 +527,6 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
return $courseoptions[$key];
}
- $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$canparticipate = (is_enrolled($coursecontext) or is_viewing($coursecontext));
if (has_capability('moodle/blog:view', $coursecontext)) {
@@ -587,8 +587,9 @@ function blog_get_options_for_module($module, $user=null) {
}
// Check the user can associate with the module
- $sitecontext = get_context_instance(CONTEXT_SYSTEM);
- if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
+ $modcontext = context_module::instance($module->id);
+ $sitecontext = context_system::instance();
+ if (!has_capability('moodle/blog:associatemodule', $modcontext)) {
return $options;
}
@@ -604,7 +605,6 @@ function blog_get_options_for_module($module, $user=null) {
return $moduleoptions[$module->id];
}
- $modcontext = get_context_instance(CONTEXT_MODULE, $module->id);
$canparticipate = (is_enrolled($modcontext) or is_viewing($modcontext));
if (has_capability('moodle/blog:view', $modcontext)) {
@@ -743,7 +743,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu
$PAGE->set_pagelayout('standard');
- if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
+ // modid always overrides courseid, so the $course object may be reset here
+ if (!empty($modid) && $CFG->useblogassociations) {
+
$headers['filters']['module'] = $modid;
// A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
$courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
Please sign in to comment.
Something went wrong with that request. Please try again.