MDL-34910 blog Remove new entry link when the context doesn't allows to add new entries
authorDavid Monllao <davidm@moodle.com>
Wed, 15 Aug 2012 08:02:52 +0000 (16:02 +0800)
committerDavid Monllao <davidm@moodle.com>
Mon, 29 Oct 2012 07:04:45 +0000 (15:04 +0800)
blog/locallib.php

index 9a07fba..168faf1 100644 (file)
@@ -663,27 +663,42 @@ class blog_listing {
             $userid = optional_param('userid', null, PARAM_INT);
 
             if (empty($userid) || (!empty($userid) && $userid == $USER->id)) {
-                $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php");
-                $urlparams = array('action' => 'add',
-                                   'userid' => $userid,
-                                   'courseid' => optional_param('courseid', null, PARAM_INT),
-                                   'groupid' => optional_param('groupid', null, PARAM_INT),
-                                   'modid' => optional_param('modid', null, PARAM_INT),
-                                   '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]);
+
+                $canaddentries = true;
+                if ($modid = optional_param('modid', null, PARAM_INT)) {
+                    if (!has_capability('moodle/blog:associatemodule', context_module::instance($modid))) {
+                        $canaddentries = false;
+                    }
+                }
+                if ($courseid = optional_param('courseid', null, PARAM_INT)) {
+                    if (!has_capability('moodle/blog:associatecourse', context_course::instance($courseid))) {
+                        $canaddentries = false;
                     }
                 }
-                $addurl->params($urlparams);
 
-                $addlink = '<div class="addbloglink">';
-                $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
-                $addlink .= '</div>';
-                echo $addlink;
+                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));
+
+                    foreach ($urlparams as $var => $val) {
+                        if (empty($val)) {
+                            unset($urlparams[$var]);
+                        }
+                    }
+                    $addurl->params($urlparams);
+
+                    $addlink = '<div class="addbloglink">';
+                    $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
+                    $addlink .= '</div>';
+                    echo $addlink;
+                }
             }
         }