Skip to content

Commit

Permalink
Merge branch 'MDL-25347_23' of git://github.com/dmonllao/moodle into …
Browse files Browse the repository at this point in the history
…MOODLE_23_STABLE
  • Loading branch information
stronk7 committed Aug 15, 2012
2 parents 36e6961 + 1534330 commit 833e135
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
33 changes: 20 additions & 13 deletions blog/edit_form.php
Expand Up @@ -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();
Expand All @@ -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);
}
}
}

Expand All @@ -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));
}
Expand All @@ -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);

Expand Down
16 changes: 9 additions & 7 deletions blog/lib.php
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 833e135

Please sign in to comment.