Beispiel #1
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;
 }
Beispiel #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;
     }
 }
Beispiel #3
0
 /**
  * Get url
  *
  * @return     void
  */
 private function getUrl($type = 'Claim')
 {
     $cconfig = \Component::params('com_courses');
     $request_type = $cconfig->get('badges_request_type', 'oauth');
     $badgesHandler = new \Hubzero\Badges\Wallet(strtoupper($this->get('provider_name')), $request_type);
     $badgesProvider = $badgesHandler->getProvider();
     return $badgesProvider->getUrl($type);
 }
Beispiel #4
0
 /**
  * Saves changes to a course or saves a new entry if creating
  *
  * @return void
  */
 public function saveTask($redirect = true)
 {
     // Check for request forgeries
     Request::checkToken();
     // Incoming
     $fields = Request::getVar('fields', array(), 'post');
     // Instantiate a Course object
     $model = \Components\Courses\Models\Section::getInstance($fields['id']);
     if (!$model->bind($fields)) {
         $this->setError($model->getError());
         $this->editTask($model);
         return;
     }
     $p = new \Hubzero\Config\Registry(Request::getVar('params', '', 'post'));
     // Make sure the logo gets carried over
     $op = new \Hubzero\Config\Registry($model->get('params'));
     $p->set('logo', $op->get('logo'));
     $model->set('params', $p->toString());
     if (!$model->store(true)) {
         $this->setError($model->getError());
         $this->editTask($model);
         return;
     }
     $dates = Request::getVar('dates', array(), 'post');
     //$i=0;
     //$unit_up = '';
     //$unit_down = '';
     foreach ($dates as $i => $dt) {
         /*if (!$unit_up && $i == 0)
         		{
         			$unit_up = $dt['publish_up'];
         		}
         		if (!$unit_down && $i == 0)
         		{
         			$unit_down = $dt['publish_down'];
         		}*/
         $dt['section_id'] = $model->get('id');
         $dt = $this->_datesToUTC($dt);
         $dtmodel = new \Components\Courses\Models\Section\Date($dt['id']);
         if (!$dtmodel->bind($dt)) {
             $this->setError($dtmodel->getError());
             continue;
         }
         if (!$dtmodel->store(true)) {
             $this->setError($dtmodel->getError());
             continue;
         }
         if (isset($dt['asset_group'])) {
             foreach ($dt['asset_group'] as $j => $ag) {
                 $ag = $this->_datesToUTC($ag);
                 if (!isset($ag['publish_up']) || !$ag['publish_up']) {
                     $ag['publish_up'] = $dt['publish_up'];
                 }
                 if (!isset($ag['publish_down']) || !$ag['publish_down']) {
                     $ag['publish_down'] = $dt['publish_down'];
                 }
                 $ag['section_id'] = $model->get('id');
                 $dtmodel = new \Components\Courses\Models\Section\Date($ag['id']);
                 if (!$dtmodel->bind($ag)) {
                     $this->setError($dtmodel->getError());
                     continue;
                 }
                 if (!$dtmodel->store(true)) {
                     $this->setError($dtmodel->getError());
                     continue;
                 }
                 if (isset($ag['asset_group'])) {
                     foreach ($ag['asset_group'] as $k => $agt) {
                         $agt = $this->_datesToUTC($agt);
                         if (!isset($agt['publish_up']) || !$agt['publish_up']) {
                             $agt['publish_up'] = $ag['publish_up'];
                         }
                         if (!isset($agt['publish_down']) || !$agt['publish_down']) {
                             $agt['publish_down'] = $ag['publish_down'];
                         }
                         $agt['section_id'] = $model->get('id');
                         $dtmodel = new \Components\Courses\Models\Section\Date($agt['id']);
                         if (!$dtmodel->bind($agt)) {
                             $this->setError($dtmodel->getError());
                             continue;
                         }
                         if (!$dtmodel->store(true)) {
                             $this->setError($dtmodel->getError());
                             continue;
                         }
                         if (isset($agt['asset'])) {
                             foreach ($agt['asset'] as $z => $a) {
                                 $a = $this->_datesToUTC($a);
                                 if (!isset($a['publish_up']) || !$a['publish_up']) {
                                     $a['publish_up'] = $agt['publish_up'];
                                 }
                                 if (!isset($a['publish_down']) || !$a['publish_down']) {
                                     $a['publish_down'] = $agt['publish_down'];
                                 }
                                 $a['section_id'] = $model->get('id');
                                 $dtmodel = new \Components\Courses\Models\Section\Date($a['id']);
                                 if (!$dtmodel->bind($a)) {
                                     $this->setError($dtmodel->getError());
                                     continue;
                                 }
                                 if (!$dtmodel->store(true)) {
                                     $this->setError($dtmodel->getError());
                                     continue;
                                 }
                                 //$agt['asset'][$z] = $a;
                             }
                         }
                         //$ag['asset_group'][$k] = $agt;
                     }
                 }
                 if (isset($ag['asset'])) {
                     foreach ($ag['asset'] as $z => $a) {
                         $a = $this->_datesToUTC($a);
                         if (!isset($a['publish_up']) || !$a['publish_up']) {
                             $a['publish_up'] = $ag['publish_up'];
                         }
                         if (!isset($a['publish_down']) || !$a['publish_down']) {
                             $a['publish_down'] = $ag['publish_down'];
                         }
                         $a['section_id'] = $model->get('id');
                         $dtmodel = new \Components\Courses\Models\Section\Date($a['id']);
                         if (!$dtmodel->bind($a)) {
                             $this->setError($dtmodel->getError());
                             continue;
                         }
                         if (!$dtmodel->store(true)) {
                             $this->setError($dtmodel->getError());
                             continue;
                         }
                     }
                 }
             }
         }
         if (isset($dt['asset'])) {
             foreach ($dt['asset'] as $z => $a) {
                 $a = $this->_datesToUTC($a);
                 if (!isset($a['publish_up']) || !$a['publish_up']) {
                     $a['publish_up'] = $dt['publish_up'];
                 }
                 if (!isset($a['publish_down']) || !$a['publish_down']) {
                     $a['publish_down'] = $dt['publish_down'];
                 }
                 $a['section_id'] = $model->get('id');
                 $dtmodel = new \Components\Courses\Models\Section\Date($a['id']);
                 if (!$dtmodel->bind($a)) {
                     $this->setError($dtmodel->getError());
                     continue;
                 }
                 if (!$dtmodel->store(true)) {
                     $this->setError($dtmodel->getError());
                     continue;
                 }
                 //$agt['asset'][$z] = $a;
             }
         }
     }
     // Process badge info
     $badge = Request::getVar('badge', array(), 'post', 'array', JREQUEST_ALLOWHTML);
     if (isset($badge['published']) && $badge['published']) {
         // Get courses config
         $cconfig = Component::params('com_courses');
         // Save the basic badge content
         $badge['section_id'] = $model->get('id');
         $badgeObj = new \Components\Courses\Models\Section\Badge($badge['id']);
         $badgeObj->bind($badge);
         $badgeObj->store();
         // See if we have an image coming in as well
         $badge_image = Request::getVar('badge_image', false, 'files', 'array');
         // If so, proceed with saving the image
         if (isset($badge_image['name']) && $badge_image['name']) {
             // Get the file extension
             $pathinfo = pathinfo($badge_image['name']);
             $filename = $pathinfo['filename'];
             $ext = $pathinfo['extension'];
             // Check for square and at least 420 x 420
             $dimensions = getimagesize($badge_image['tmp_name']);
             if ($dimensions[0] != $dimensions[1]) {
                 $this->setError(Lang::txt('COM_COURSES_ERROR_IMG_MUST_BE_SQUARE'));
             } else {
                 if ($dimensions[0] < 450) {
                     $this->setError(Lang::txt('COM_COURSES_ERROR_IMG_MIN_WIDTH'));
                 } else {
                     // Build the upload path if it doesn't exist
                     $uploadDirectory = PATH_APP . DS . trim($cconfig->get('uploadpath', '/site/courses'), DS);
                     $uploadDirectory .= DS . 'badges' . DS . $badgeObj->get('id') . DS;
                     // Make sure upload directory exists and is writable
                     if (!is_dir($uploadDirectory)) {
                         if (!\Filesystem::makeDirectory($uploadDirectory)) {
                             $this->setError(Lang::txt('COM_COURSES_ERROR_UNABLE_TO_CREATE_UPLOAD_PATH'));
                         }
                     }
                     if (!is_writable($uploadDirectory)) {
                         $this->setError(Lang::txt('COM_COURSES_ERROR_UPLOAD_DIRECTORY_IS_NOT_WRITABLE'));
                     }
                     // Get the final file path
                     $target_path = $uploadDirectory . 'badge.' . $ext;
                     if (!($move = move_uploaded_file($badge_image['tmp_name'], $target_path))) {
                         $this->setError(Lang::txt('COM_COURSES_ERROR_FILE_MOVE_FAILED'));
                     } else {
                         // Move successful, save the image url to the badge entry
                         $img_url = DS . 'courses' . DS . 'badge' . DS . $badgeObj->get('id') . DS . 'image';
                         $badgeObj->bind(array('img_url' => $img_url));
                         $badgeObj->store();
                     }
                 }
             }
         }
         // Process criteria text
         if (strcmp($badgeObj->get('criteria_text'), $badge['criteria'])) {
             $badgeObj->set('criteria_text_new', $badge['criteria']);
             $badgeObj->store();
             $badgeObj->set('criteria_text_new', NULL);
         }
         // If we don't already have a provider badge id set, then we're processing our initial badge creation
         if ($badgeObj->get('provider_name') && !$badgeObj->get('provider_badge_id') && $badgeObj->get('img_url')) {
             $request_type = $cconfig->get('badges_request_type', 'oauth');
             $badgesHandler = new \Hubzero\Badges\Wallet(strtoupper($badgeObj->get('provider_name')), $request_type);
             $badgesProvider = $badgesHandler->getProvider();
             if (is_object($badgesProvider)) {
                 $credentials = new stdClass();
                 $credentials->consumer_key = $cconfig->get($badgeObj->get('provider_name') . '_consumer_key', 0);
                 $credentials->consumer_secret = $cconfig->get($badgeObj->get('provider_name') . '_consumer_secret', 0);
                 $credentials->issuerId = $cconfig->get($badgeObj->get('provider_name') . '_issuer_id');
                 $badgesProvider->setCredentials($credentials);
                 $offering = \Components\Courses\Models\Offering::getInstance($model->get('offering_id'));
                 $course = \Components\Courses\Models\Course::getInstance($offering->get('course_id'));
                 $data = array();
                 $data['Name'] = $course->get('title');
                 $data['Description'] = trim($course->get('title')) . ' Badge';
                 $data['CriteriaUrl'] = rtrim(Request::root(), '/') . '/courses/badge/' . $badgeObj->get('id') . '/criteria';
                 $data['Version'] = '1';
                 $data['BadgeImageUrl'] = rtrim(Request::root(), '/') . '/' . trim($badgeObj->get('img_url'), '/');
                 if (!$credentials->consumer_key || !$credentials->consumer_secret) {
                     $this->setError(Lang::txt('COM_COURSES_ERROR_BADGE_MISSING_OPTIONS'));
                 } else {
                     try {
                         $provider_badge_id = $badgesProvider->createBadge($data);
                     } catch (Exception $e) {
                         $this->setError($e->getMessage());
                     }
                     if (isset($provider_badge_id) && $provider_badge_id) {
                         // We've successfully created a badge, so save that id to the database
                         $badgeObj->bind(array('provider_badge_id' => $provider_badge_id));
                         $badgeObj->store();
                     } else {
                         $this->setError(Lang::txt('COM_COURSES_ERROR_FAILED_TO_SAVE_BADGE'));
                     }
                 }
             }
         }
     } elseif ($badge['id']) {
         $badgeObj = new \Components\Courses\Models\Section\Badge($badge['id']);
         $badgeObj->bind(array('published' => 0));
         $badgeObj->store();
     }
     if ($this->getError()) {
         $this->setError(implode('<br />', $this->getErrors()));
         $this->editTask($model);
         return;
     }
     if ($this->_task == 'apply') {
         return $this->editTask($model);
     }
     // Output messsage and redirect
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&offering=' . $model->get('offering_id'), false), Lang::txt('COM_COURSES_ITEM_SAVED'));
 }