Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…evels instead of system level
  • Loading branch information...
commit 1b30a9fac63bd000b908d5c786c420a9f672c283 1 parent 87e9331
David Monllaó dmonllao authored

Showing 2 changed files with 26 additions and 17 deletions. Show diff stats Hide diff stats

  1. +19 12 blog/edit_form.php
  2. +7 5 blog/lib.php
31 blog/edit_form.php
@@ -71,24 +71,28 @@ function definition() {
71 71 $allmodnames = array();
72 72
73 73 if (!empty($CFG->useblogassociations)) {
74   - if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
  74 + if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
75 75 if (!empty($courseid)) {
76 76 $course = $DB->get_record('course', array('id' => $courseid));
77   - $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
78 77 $context = context_course::instance($courseid);
79 78 $a = new stdClass();
80 79 $a->coursename = format_string($course->fullname, true, array('context' => $context));
81 80 $contextid = $context->id;
82 81 } else {
  82 + $context = context::instance_by_id($entry->courseassoc);
83 83 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
84 84 $a = new stdClass();
85 85 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
86 86 $contextid = $entry->courseassoc;
87 87 }
88 88
89   - $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
90   - $mform->setDefault('courseassoc', $contextid);
91   - } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
  89 + if (has_capability('moodle/blog:associatecourse', $context)) {
  90 + $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  91 + $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
  92 + $mform->setDefault('courseassoc', $contextid);
  93 + }
  94 +
  95 + } else if ((!empty($entry->modassoc) || !empty($modid))) {
92 96 if (!empty($modid)) {
93 97 $mod = get_coursemodule_from_id(false, $modid);
94 98 $a = new stdClass();
@@ -104,9 +108,11 @@ function definition() {
104 108 $modid = $context->instanceid;
105 109 }
106 110
107   - $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
108   - $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
109   - $mform->setDefault('modassoc', $context->id);
  111 + if (has_capability('moodle/blog:associatemodule', $context)) {
  112 + $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  113 + $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
  114 + $mform->setDefault('modassoc', $context->id);
  115 + }
110 116 }
111 117 }
112 118
@@ -132,13 +138,13 @@ function validation($data, $files) {
132 138 global $CFG, $DB, $USER;
133 139
134 140 $errors = array();
135   - $sitecontext = context_system::instance();
136 141
137 142 // validate course association
138   - if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
  143 + if (!empty($data['courseassoc'])) {
139 144 $coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING);
140 145
141   - if ($coursecontext and $coursecontext->contextlevel == CONTEXT_COURSE) {
  146 + $canassociatecourse = has_capability('moodle/blog:associatecourse', $coursecontext);
  147 + if ($coursecontext->contextlevel == CONTEXT_COURSE && $canassociatecourse) {
142 148 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
143 149 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
144 150 }
@@ -152,7 +158,8 @@ function validation($data, $files) {
152 158 $modcontextid = $data['modassoc'];
153 159 $modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING);
154 160
155   - if ($modcontext and $modcontext->contextlevel == CONTEXT_MODULE) {
  161 + $canassociatemodule = has_capability('moodle/blog:associatecourse', $modcontext);
  162 + if ($modcontext->contextlevel == CONTEXT_MODULE && $canassociatemodule) {
156 163 // get context of the mod's course
157 164 $coursecontext = $modcontext->get_course_context(true);
158 165
12 blog/lib.php
@@ -511,7 +511,8 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
511 511
512 512 // Check that the user can associate with the course
513 513 $sitecontext = context_system::instance();
514   - if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
  514 + $coursecontext = context_course::instance($course->id);
  515 + if (!has_capability('moodle/blog:associatecourse', $coursecontext)) {
515 516 return $options;
516 517 }
517 518 // Generate the cache key
@@ -526,7 +527,6 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
526 527 return $courseoptions[$key];
527 528 }
528 529
529   - $coursecontext = context_course::instance($course->id, IGNORE_MISSING);
530 530 $canparticipate = (is_enrolled($coursecontext) or is_viewing($coursecontext));
531 531
532 532 if (has_capability('moodle/blog:view', $coursecontext)) {
@@ -587,8 +587,9 @@ function blog_get_options_for_module($module, $user=null) {
587 587 }
588 588
589 589 // Check the user can associate with the module
  590 + $modcontext = context_module::instance($module->id);
590 591 $sitecontext = context_system::instance();
591   - if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
  592 + if (!has_capability('moodle/blog:associatemodule', $modcontext)) {
592 593 return $options;
593 594 }
594 595
@@ -604,7 +605,6 @@ function blog_get_options_for_module($module, $user=null) {
604 605 return $moduleoptions[$module->id];
605 606 }
606 607
607   - $modcontext = context_module::instance($module->id, IGNORE_MISSING);
608 608 $canparticipate = (is_enrolled($modcontext) or is_viewing($modcontext));
609 609
610 610 if (has_capability('moodle/blog:view', $modcontext)) {
@@ -743,7 +743,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu
743 743
744 744 $PAGE->set_pagelayout('standard');
745 745
746   - if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
  746 + // modid always overrides courseid, so the $course object may be reset here
  747 + if (!empty($modid) && $CFG->useblogassociations) {
  748 +
747 749 $headers['filters']['module'] = $modid;
748 750 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
749 751 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));

0 comments on commit 1b30a9f

Please sign in to comment.
Something went wrong with that request. Please try again.