From 71919749e37b482b39143812374785bca4d522e9 Mon Sep 17 00:00:00 2001
From: Yuliya Bozhko <yuliya.bozhko@totaralms.com>
Date: Tue, 10 Sep 2013 14:21:36 +1200
Subject: [PATCH] T-11188 badges: Fixes around badge capabilities and
 navigation

Change-Id: I597a0cded3878f1d991441c5fa870ec3242f3ad2
Reviewed-on: https://review.totaralms.com/3714
Reviewed-by: David Curry <david.curry@totaralms.com>
Tested-by: Jenkins Automation <jenkins@totaralms.com>
Reviewed-by: Maria Torres <maria.torres@totaralms.com>
---
 admin/settings/badges.php | 12 +++++++++++-
 badges/action.php         |  6 +++++-
 badges/criteria.php       |  3 ++-
 badges/edit.php           |  6 +++++-
 badges/index.php          |  8 +++++++-
 badges/newbadge.php       |  6 +++++-
 lib/badgeslib.php         | 45 ++++++++++++++++++---------------------------
 7 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/admin/settings/badges.php b/admin/settings/badges.php
index e7083d2..4b76d44 100644
--- a/admin/settings/badges.php
+++ b/admin/settings/badges.php
@@ -32,6 +32,7 @@ if ($hassiteconfig || has_any_capability(array(
             'moodle/badges:createbadge',
             'moodle/badges:manageglobalsettings',
             'moodle/badges:awardbadge',
+            'moodle/badges:configurecriteria',
             'moodle/badges:configuremessages',
             'moodle/badges:configuredetails',
             'moodle/badges:deletebadge'), $systemcontext)) {
@@ -70,7 +71,16 @@ if ($hassiteconfig || has_any_capability(array(
         new admin_externalpage('managebadges',
             new lang_string('managebadges', 'badges'),
             new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE)),
-            array('moodle/badges:viewawarded'), empty($CFG->enablebadges)
+            array(
+                'moodle/badges:viewawarded',
+                'moodle/badges:createbadge',
+                'moodle/badges:awardbadge',
+                'moodle/badges:configurecriteria',
+                'moodle/badges:configuremessages',
+                'moodle/badges:configuredetails',
+                'moodle/badges:deletebadge'
+            ),
+            empty($CFG->enablebadges)
         )
     );
 
diff --git a/badges/action.php b/badges/action.php
index ffd1927..a002be3 100644
--- a/badges/action.php
+++ b/badges/action.php
@@ -95,7 +95,11 @@ if ($copy) {
     require_capability('moodle/badges:createbadge', $context);
 
     $cloneid = $badge->make_clone();
-    redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'details')));
+    // If a user can edit badge details, they will be redirected to the edit page.
+    if (has_capability('moodle/badges:configuredetails', $context)) {
+        redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'details')));
+    }
+    redirect(new moodle_url('/badges/overview.php', array('id' => $cloneid)));
 }
 
 if ($activate) {
diff --git a/badges/criteria.php b/badges/criteria.php
index bff988c..979977b 100644
--- a/badges/criteria.php
+++ b/badges/criteria.php
@@ -40,6 +40,8 @@ $badge = new badge($badgeid);
 $context = $badge->get_context();
 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
 
+require_capability('moodle/badges:configurecriteria', $context);
+
 if ($badge->type == BADGE_TYPE_COURSE) {
     if (empty($CFG->badges_allowcoursebadges)) {
         print_error('coursebadgesdisabled', 'badges');
@@ -67,7 +69,6 @@ $emsg = optional_param('emsg', '', PARAM_TEXT);
 
 if ((($update == BADGE_CRITERIA_AGGREGATION_ALL) || ($update == BADGE_CRITERIA_AGGREGATION_ANY))) {
     require_sesskey();
-    require_capability('moodle/badges:configurecriteria', $context);
     $obj = new stdClass();
     $obj->id = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->id;
     $obj->method = $update;
diff --git a/badges/edit.php b/badges/edit.php
index 7c791d8..dad1769 100644
--- a/badges/edit.php
+++ b/badges/edit.php
@@ -41,7 +41,11 @@ $badge = new badge($badgeid);
 $context = $badge->get_context();
 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
 
-require_capability('moodle/badges:configuredetails', $context);
+if ($action == 'message') {
+    require_capability('moodle/badges:configuremessages', $context);
+} else {
+    require_capability('moodle/badges:configuredetails', $context);
+}
 
 if ($badge->type == BADGE_TYPE_COURSE) {
     if (empty($CFG->badges_allowcoursebadges)) {
diff --git a/badges/index.php b/badges/index.php
index 84294b8..a2fe6e7 100644
--- a/badges/index.php
+++ b/badges/index.php
@@ -91,7 +91,13 @@ if ($type == BADGE_TYPE_SITE) {
     );
 }
 
-if (!has_capability('moodle/badges:awardbadge', $PAGE->context)) {
+if (!has_any_capability(array('moodle/badges:viewawarded',
+                                          'moodle/badges:createbadge',
+                                          'moodle/badges:awardbadge',
+                                          'moodle/badges:configurecriteria',
+                                          'moodle/badges:configuremessages',
+                                          'moodle/badges:configuredetails',
+                                          'moodle/badges:deletebadge'), $PAGE->context)) {
     redirect($CFG->wwwroot);
 }
 
diff --git a/badges/newbadge.php b/badges/newbadge.php
index 91c316c..756ef91 100644
--- a/badges/newbadge.php
+++ b/badges/newbadge.php
@@ -98,7 +98,11 @@ if ($form->is_cancelled()) {
 
     $newbadge = new badge($newid);
     badges_process_badge_image($newbadge, $form->save_temp_file('image'));
-    redirect(new moodle_url('/badges/criteria.php', array('id' => $newid)));
+    // If a user can configure badge criteria, they will be redirected to the criteria page.
+    if (has_capability('moodle/badges:configurecriteria', $PAGE->context)) {
+        redirect(new moodle_url('/badges/criteria.php', array('id' => $newid)));
+    }
+    redirect(new moodle_url('/badges/overview.php', array('id' => $newid)));
 }
 
 echo $OUTPUT->header();
diff --git a/lib/badgeslib.php b/lib/badgeslib.php
index 42f0a86..4d85578 100644
--- a/lib/badgeslib.php
+++ b/lib/badgeslib.php
@@ -947,38 +947,29 @@ function badges_add_course_navigation(navigation_node $coursenode, stdClass $cou
 
     $coursecontext = context_course::instance($course->id);
     $isfrontpage = (!$coursecontext || $course->id == $SITE->id);
+    $canmanage = has_any_capability(array('moodle/badges:viewawarded',
+                                          'moodle/badges:createbadge',
+                                          'moodle/badges:awardbadge',
+                                          'moodle/badges:configurecriteria',
+                                          'moodle/badges:configuremessages',
+                                          'moodle/badges:configuredetails',
+                                          'moodle/badges:deletebadge'), $coursecontext);
 
-    if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) && !$isfrontpage) {
-        if (has_capability('moodle/badges:configuredetails', $coursecontext)) {
-            $coursenode->add(get_string('coursebadges', 'badges'), null,
-                    navigation_node::TYPE_CONTAINER, null, 'coursebadges',
-                    new pix_icon('i/badge', get_string('coursebadges', 'badges')));
+    if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) && !$isfrontpage && $canmanage) {
+        $coursenode->add(get_string('coursebadges', 'badges'), null,
+                navigation_node::TYPE_CONTAINER, null, 'coursebadges',
+                new pix_icon('i/badge', get_string('coursebadges', 'badges')));
 
-            if (has_capability('moodle/badges:viewawarded', $coursecontext)) {
-                $url = new moodle_url('/badges/index.php',
-                        array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
+        $url = new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
 
-                $coursenode->get('coursebadges')->add(get_string('managebadges', 'badges'), $url,
-                    navigation_node::TYPE_SETTING, null, 'coursebadges');
-            }
-
-            if (has_capability('moodle/badges:createbadge', $coursecontext)) {
-                $url = new moodle_url('/badges/newbadge.php',
-                        array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
-
-                $coursenode->get('coursebadges')->add(get_string('newbadge', 'badges'), $url,
-                        navigation_node::TYPE_SETTING, null, 'newbadge');
-            }
-        } else if (has_capability('moodle/badges:awardbadge', $coursecontext)) {
-            $coursenode->add(get_string('coursebadges', 'badges'), null,
-                    navigation_node::TYPE_CONTAINER, null, 'coursebadges',
-                    new pix_icon('i/badge', get_string('coursebadges', 'badges')));
+        $coursenode->get('coursebadges')->add(get_string('managebadges', 'badges'), $url,
+            navigation_node::TYPE_SETTING, null, 'coursebadges');
 
-            $url = new moodle_url('/badges/index.php',
-                    array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
+        if (has_capability('moodle/badges:createbadge', $coursecontext)) {
+            $url = new moodle_url('/badges/newbadge.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
 
-            $coursenode->get('coursebadges')->add(get_string('managebadges', 'badges'), $url,
-                    navigation_node::TYPE_SETTING, null, 'coursebadges');
+            $coursenode->get('coursebadges')->add(get_string('newbadge', 'badges'), $url,
+                    navigation_node::TYPE_SETTING, null, 'newbadge');
         }
     }
 }
-- 
1.8.1.2

