Merge branch 'MDL-40471_master' of https://github.com/totara/openbadges
authorSam Hemelryk <sam@moodle.com>
Tue, 10 Sep 2013 20:42:37 +0000 (08:42 +1200)
committerSam Hemelryk <sam@moodle.com>
Tue, 10 Sep 2013 20:42:37 +0000 (08:42 +1200)
1  2 
lib/badgeslib.php

diff --combined lib/badgeslib.php
@@@ -895,42 -895,67 +895,33 @@@ function badges_add_course_navigation(n
  
      $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');
          }
      }
  }
  
 -/**
 - * Triggered when 'user_updated' event happens.
 - *
 - * @param   object $eventdata Holds all information about a user.
 - * @return  boolean
 - */
 -function badges_award_handle_profile_criteria_review(stdClass $eventdata) {
 -    global $DB, $CFG;
 -
 -    if (!empty($CFG->enablebadges)) {
 -        $userid = $eventdata->id;
 -
 -        if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
 -            foreach ($rs as $r) {
 -                $badge = new badge($r->badgeid);
 -                if (!$badge->is_active() || $badge->is_issued($userid)) {
 -                    continue;
 -                }
 -
 -                if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
 -                    $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
 -
 -                    if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 -                        $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 -                        $badge->issue($userid);
 -                    }
 -                }
 -            }
 -        }
 -    }
 -
 -    return true;
 -}
 -
  /**
   * Triggered when badge is manually awarded.
   *
@@@ -1052,25 -1077,13 +1043,25 @@@ function badges_bake($hash, $badgeid, $
  /**
   * Returns external backpack settings and badges from this backpack.
   *
 + * This function first checks if badges for the user are cached and
 + * tries to retrieve them from the cache. Otherwise, badges are obtained
 + * through curl request to the backpack.
 + *
   * @param int $userid Backpack user ID.
 + * @param boolean $refresh Refresh badges collection in cache.
   * @return null|object Returns null is there is no backpack or object with backpack settings.
   */
 -function get_backpack_settings($userid) {
 +function get_backpack_settings($userid, $refresh = false) {
      global $DB;
      require_once(dirname(dirname(__FILE__)) . '/badges/lib/backpacklib.php');
  
 +    // Try to get badges from cache first.
 +    $badgescache = cache::make('core', 'externalbadges');
 +    $out = $badgescache->get($userid);
 +    if ($out !== false && !$refresh) {
 +        return $out;
 +    }
 +    // Get badges through curl request to the backpack.
      $record = $DB->get_record('badge_backpack', array('userid' => $userid));
      if ($record) {
          $backpack = new OpenBadgesBackpackHandler($record);
              $out->totalcollections = 0;
          }
  
 +        $badgescache->set($userid, $out);
          return $out;
      }