Exemple #1
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;
     }
 }
 /**
  * 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.');
 }
Exemple #3
0
 /**
  * Sync claimed/denied passport badges
  *
  * @param   object   $job  \Components\Cron\Models\Job
  * @return  boolean
  */
 public function syncPassportBadgeStatus(\Components\Cron\Models\Job $job)
 {
     $params = Component::params('com_courses');
     $badgesHandler = new Hubzero\Badges\Wallet('passport', $params->get('badges_request_type'));
     $badgesProvider = $badgesHandler->getProvider();
     $creds = new \stdClass();
     $creds->consumer_key = $params->get('passport_consumer_key');
     $creds->consumer_secret = $params->get('passport_consumer_secret');
     $badgesProvider->setCredentials($creds);
     require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'courses.php';
     require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'memberBadge.php';
     $coursesObj = new \Components\Courses\Models\Courses();
     $courses = $coursesObj->courses();
     if (isset($courses) && count($courses) > 0) {
         foreach ($courses as $course) {
             if (!$course->isAvailable()) {
                 continue;
             }
             $students = $course->students();
             $emails = array();
             if ($students && count($students) > 0) {
                 foreach ($students as $student) {
                     $emails[] = User::getInstance($student->get('user_id'))->get('email');
                 }
             }
             if (count($emails) > 0) {
                 $assertions = $badgesProvider->getAssertionsByEmailAddress($emails);
                 if (isset($assertions) && count($assertions) > 0) {
                     foreach ($assertions as $assertion) {
                         $status = false;
                         if ($assertion->IsPending) {
                             $status = false;
                         } else {
                             if ($assertion->IsAccepted) {
                                 $status = 'accept';
                             } else {
                                 $status = 'deny';
                             }
                         }
                         if ($status) {
                             preg_match('/validation\\/([[:alnum:]-]{20})/', $assertion->EvidenceUrl, $match);
                             if (isset($match[1])) {
                                 $badge = \Components\Courses\Models\MemberBadge::loadByToken($match[1]);
                                 if ($badge && !$badge->get('action')) {
                                     $badge->set('action', $status);
                                     $badge->set('action_on', Date::toSql());
                                     $badge->store();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Job is no longer active
     return true;
 }
Exemple #4
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;
 }