Esempio n. 1
0
 /**
  * Passport badges. Placeholder for now.
  *
  * @apiMethod POST
  * @apiUri    /courses/passport/badge
  * @apiParameter {
  * 		"name":        "action",
  * 		"description": "Badge action",
  * 		"type":        "string",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "badge_id",
  * 		"description": "Passport badge ID",
  * 		"type":        "integer",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "user_email",
  * 		"description": "Email address to which the badge was asserted",
  * 		"type":        "string",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @return    void
  */
 public function badgeTask()
 {
     // Require authentication and authorization
     $this->authorizeOrFail();
     $action = Request::getVar('action', '');
     $badge_id = Request::getVar('badge_id', '');
     $user_email = Request::getVar('user_email', '');
     if (empty($action)) {
         App::abort(400, 'Please provide action');
     }
     if ($action != 'accept' && $action != 'deny') {
         App::abort(400, 'Bad action. Must be either accept or deny');
     }
     if (empty($badge_id)) {
         App::abort(400, 'Please provide badge ID');
     }
     if (empty($user_email)) {
         App::abort(400, 'Please provide user email');
     }
     // Find user by email
     $user = User::oneByEmail($user_email);
     if (!$user->get('id')) {
         App::abort(404, 'User was not found');
     }
     $user_id = $user->get('id');
     // Get section from provider badge id
     $section_badge = \Components\Courses\Models\Section\Badge::loadByProviderBadgeId($badge_id);
     // Check if there is a match
     if (!($section_id = $section_badge->get('section_id'))) {
         App::abort(400, 'No matching badge found');
     }
     // Get member id via user id and section id
     $member = \Components\Courses\Models\Member::getInstance($user_id, 0, 0, $section_id);
     // Check if there is a match
     if (!$member->get('id')) {
         App::abort(400, 'Matching course member not found');
     }
     // Now actually load the badge
     $member_badge = \Components\Courses\Models\MemberBadge::loadByMemberId($member->get('id'));
     // Check if there is a match
     if (!$member_badge->get('id')) {
         App::abort(400, 'This member does not have a matching badge entry');
     }
     $now = Date::toSql();
     $member_badge->set('action', $action);
     $member_badge->set('action_on', $now);
     $member_badge->store();
     // Return message
     $this->send('Passport data saved.');
 }
Esempio n. 2
0
 /**
  * Check whether or not the student(s) have earned a badge
  *
  * @param      int $member_id
  * @return     bool
  **/
 public function hasEarnedBadge($member_id = null)
 {
     // Check whether or not they're eligable for a badge at this point
     // First, does this course even offers a badge
     if ($this->course->offering()->section()->badge()->isAvailable()) {
         $members = $this->isEligibleForRecognition($member_id);
         if ($members && count($members) > 0) {
             foreach ($members as $m) {
                 // Mark student as having earned badge
                 $badge = MemberBadge::loadByMemberId($m);
                 $sb = Section\Badge::loadBySectionId($this->course->offering()->section()->get('id'));
                 if (is_object($badge) && !$badge->hasEarned()) {
                     $badge->set('member_id', $m);
                     $badge->set('section_badge_id', $sb->get('id'));
                     $badge->set('earned', 1);
                     $badge->set('earned_on', \Date::toSql());
                     $badge->set('criteria_id', $sb->get('criteria_id'));
                     $badge->store();
                     // Get courses config
                     $cconfig = \Component::params('com_courses');
                     // Tell the badge provider that they've earned the badge
                     $request_type = $cconfig->get('badges_request_type', 'oauth');
                     $badgesHandler = new \Hubzero\Badges\Wallet(strtoupper($sb->get('provider_name')), $request_type);
                     $badgesProvider = $badgesHandler->getProvider();
                     $credentials = new \stdClass();
                     $credentials->consumer_key = $this->config()->get($sb->get('provider_name') . '_consumer_key');
                     $credentials->consumer_secret = $this->config()->get($sb->get('provider_name') . '_consumer_secret');
                     $credentials->clientId = $this->config()->get($sb->get('provider_name') . '_client_id');
                     $badgesProvider->setCredentials($credentials);
                     $dbo = \App::get('db');
                     $memberTbl = new Tables\Member($dbo);
                     $memberTbl->loadByMemberId($m);
                     $user_id = $memberTbl->get('user_id');
                     $data = new \stdClass();
                     $data->id = $sb->get('provider_badge_id');
                     $data->evidenceUrl = rtrim(\Request::root(), '/') . '/courses/badge/' . $sb->get('id') . '/validation/' . $badge->get('validation_token');
                     $users = array();
                     $users[] = \User::getInstance($user_id)->get('email');
                     // Publish assertion
                     $badgesProvider->grantBadge($data, $users);
                 }
             }
         }
     } else {
         return false;
     }
 }
Esempio n. 3
0
 /**
  * Get member badge
  *
  * @return  object \Components\Courses\Models\MemberBadge
  */
 public function badge()
 {
     if (!isset($this->_badge)) {
         $this->_badge = MemberBadge::loadByMemberId($this->get('id'));
     }
     return $this->_badge;
 }