MDL-39986 badges: Show badge navigation to users with awardbadge capability
authorYuliya Bozhko <yuliya.bozhko@totaralms.com>
Wed, 12 Jun 2013 04:07:49 +0000 (16:07 +1200)
committerDan Poltawski <dan@moodle.com>
Wed, 3 Jul 2013 05:13:55 +0000 (13:13 +0800)
Also fixes an arror when a user with this capability tries to award a badge,
but their role is not among the criteria set up for a badge

badges/award.php
badges/index.php
badges/renderer.php
lib/badgeslib.php

index 3d3aba7..834af0c 100644 (file)
@@ -89,22 +89,31 @@ if (count($acceptedroles) > 1) {
         $roleids = array_map(create_function('$o', 'return $o->roleid;'), $roles);
         $selection = array_intersect($acceptedroles, $roleids);
     }
-    list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
-    $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
-    foreach ($options as $p) {
-        $select[$p->id] = role_get_name($p);
-    }
-    if (!$role) {
-        $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
+
+    if (!empty($selection)) {
+        list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
+        $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
+        foreach ($options as $p) {
+            $select[$p->id] = role_get_name($p);
+        }
+        if (!$role) {
+            $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
+            echo $OUTPUT->header();
+            echo $OUTPUT->box(get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select));
+            echo $OUTPUT->footer();
+            die();
+        } else {
+            $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
+            $issuerrole = new stdClass();
+            $issuerrole->roleid = $role;
+            $roleselect = get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role);
+        }
+    } else {
         echo $OUTPUT->header();
-        echo $OUTPUT->box(get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select));
+        $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
+        echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
         echo $OUTPUT->footer();
         die();
-    } else {
-        $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
-        $issuerrole = new stdClass();
-        $issuerrole->roleid = $role;
-        $roleselect = get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role);
     }
 } else {
     // User has to be an admin or the one with the required role.
@@ -112,8 +121,8 @@ if (count($acceptedroles) > 1) {
     $usersids = array_keys($users);
     if (!$isadmin && !in_array($USER->id, $usersids)) {
         echo $OUTPUT->header();
-        $rlink = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), get_string('recipients', 'badges'));
-        echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $rlink));
+        $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
+        echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
         echo $OUTPUT->footer();
         die();
     } else {
index 8a30471..6b00bd2 100644 (file)
@@ -191,8 +191,11 @@ if ($totalcount) {
     echo $output->render($badges);
 } else {
     echo $output->notification(get_string('nobadges', 'badges'));
-    echo $OUTPUT->single_button(new moodle_url('newbadge.php', array('type' => $type, 'id' => $courseid)),
+
+    if (has_capability('moodle/badges:createbadge', $PAGE->context)) {
+        echo $OUTPUT->single_button(new moodle_url('newbadge.php', array('type' => $type, 'id' => $courseid)),
             get_string('newbadge', 'badges'));
+    }
 }
 
 echo $OUTPUT->footer();
index 8c0381e..171bdb3 100644 (file)
@@ -549,9 +549,12 @@ class core_badges_renderer extends plugin_renderer_base {
         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
 
         // New badge button.
-        $n['type'] = $this->page->url->get_param('type');
-        $n['id'] = $this->page->url->get_param('id');
-        $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
+        $htmlnew = '';
+        if (has_capability('moodle/badges:createbadge', $this->page->context)) {
+            $n['type'] = $this->page->url->get_param('type');
+            $n['id'] = $this->page->url->get_param('id');
+            $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
+        }
 
         $htmlpagingbar = $this->render($paging);
         $table = new html_table();
index 81a2f49..daf2d44 100644 (file)
@@ -903,7 +903,7 @@ function badges_add_course_navigation(navigation_node $coursenode, stdClass $cou
                     new pix_icon('i/badge', get_string('coursebadges', 'badges')));
 
             if (has_capability('moodle/badges:viewawarded', $coursecontext)) {
-                $url = new moodle_url($CFG->wwwroot . '/badges/index.php',
+                $url = new moodle_url('/badges/index.php',
                         array('type' => BADGE_TYPE_COURSE, 'id' => $course->id));
 
                 $coursenode->get('coursebadges')->add(get_string('managebadges', 'badges'), $url,
@@ -911,12 +911,22 @@ function badges_add_course_navigation(navigation_node $coursenode, stdClass $cou
             }
 
             if (has_capability('moodle/badges:createbadge', $coursecontext)) {
-                $url = new moodle_url($CFG->wwwroot . '/badges/newbadge.php',
+                $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')));
+
+            $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');
         }
     }
 }