mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 12:40:01 +01:00
MDL-25347 blog Checking association capability at course and module levels instead of system level
This commit is contained in:
parent
87e9331d93
commit
1b30a9fac6
@ -71,24 +71,28 @@ class blog_edit_form extends moodleform {
|
||||
$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 = 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 @@ class blog_edit_form extends moodleform {
|
||||
$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 @@ class blog_edit_form extends moodleform {
|
||||
global $CFG, $DB, $USER;
|
||||
|
||||
$errors = array();
|
||||
$sitecontext = context_system::instance();
|
||||
|
||||
// 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 @@ class blog_edit_form extends moodleform {
|
||||
$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);
|
||||
|
||||
|
12
blog/lib.php
12
blog/lib.php
@ -511,7 +511,8 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
|
||||
|
||||
// Check that the user can associate with the course
|
||||
$sitecontext = context_system::instance();
|
||||
if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
|
||||
$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 = context_course::instance($course->id, IGNORE_MISSING);
|
||||
$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
|
||||
$modcontext = context_module::instance($module->id);
|
||||
$sitecontext = context_system::instance();
|
||||
if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
|
||||
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 = context_module::instance($module->id, IGNORE_MISSING);
|
||||
$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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user