예제 #1
0
 /**
  * Get section badge
  *
  * @return     obj
  */
 public function badge()
 {
     if (!isset($this->_badge)) {
         $this->_badge = Section\Badge::loadBySectionId($this->get('id'));
     }
     return $this->_badge;
 }
예제 #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;
     }
 }
예제 #3
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.');
 }
예제 #4
0
 /**
  * Public url for badge info
  *
  * @return     void
  */
 public function badgeTask()
 {
     if ($badge_id = Request::getInt('badge_id', false)) {
         $badge = new Models\Section\Badge($badge_id);
         if (!$badge->get('id')) {
             throw new Exception(Lang::txt('COM_COURSES_BADGE_NOT_FOUND'), 500);
         } else {
             $this->view->badge = $badge;
             $this->view->config = $this->config;
             $this->view->action = Request::getWord('action', 'default');
             $this->view->token = Request::getVar('validation_token', false);
         }
     } else {
         throw new Exception(Lang::txt('COM_COURSES_BADGE_NOT_FOUND'), 500);
     }
     $this->view->display();
 }
예제 #5
0
 /**
  * Displays an edit form
  *
  * @return  void
  */
 public function editTask($model = null)
 {
     Request::setVar('hidemainmenu', 1);
     if (!is_object($model)) {
         // Incoming
         $id = Request::getVar('id', array(0));
         // Get the single ID we're working with
         if (is_array($id)) {
             $id = !empty($id) ? $id[0] : 0;
         }
         $model = \Components\Courses\Models\Section::getInstance($id);
     }
     $this->view->row = $model;
     if (!$this->view->row->get('offering_id')) {
         $this->view->row->set('offering_id', Request::getInt('offering', 0));
     }
     $this->view->offering = \Components\Courses\Models\Offering::getInstance($this->view->row->get('offering_id'));
     $this->view->course = \Components\Courses\Models\Course::getInstance($this->view->offering->get('course_id'));
     $this->view->badge = \Components\Courses\Models\Section\Badge::loadBySectionId($this->view->row->get('id'));
     // Set any errors
     foreach ($this->getErrors() as $error) {
         \Notify::error($error);
     }
     // Output the HTML
     $this->view->setLayout('edit')->display();
 }