public function saveUserInfo() { if (!isset($_SESSION)) { session_start(); } $code = \Input::get('code'); $lti = \Input::get('lti'); $instanceFromDB = LtiConfigurations::find($lti); $clientId = $instanceFromDB['DeveloperId']; $developerSecret = $instanceFromDB['DeveloperSecret']; $opts = array('http' => array('method' => 'POST')); $context = stream_context_create($opts); $url = "https://{$_SESSION['domain']}/login/oauth2/token?client_id={$clientId}&client_secret={$developerSecret}&code={$code}"; $userTokenJSON = file_get_contents($url, false, $context, -1, 40000); $userToken = json_decode($userTokenJSON); $actualToken = $userToken->access_token; $encryptedToken = \Crypt::encrypt($actualToken); $_SESSION['userToken'] = $encryptedToken; //store encrypted token in the database $courseId = $_SESSION['courseID']; $userId = $_SESSION['userID']; //make sure we have the user stored in the user table and in the userCourse table. $roots = new Roots(); //when we get the user from the LMS it gets stored in the DB. $roots->getUser($userId); $dbHelper = new DbHelper(); $role = $dbHelper->getRole('Approver'); $userCourse = UserCourse::firstOrNew(array('user_id' => $userId, 'course_id' => $courseId)); $userCourse->user_id = $userId; $userCourse->course_id = $courseId; $userCourse->role = $role->id; $userCourse->encrypted_token = $encryptedToken; $userCourse->save(); echo "App has been approved. Please reload this page"; }
private function retrieveOrderedModuleInfo($moduleId, $courseId) { $dbHelper = new DbHelper(); $orderedModule = $dbHelper->getOrderedModuleByModuleId($courseId, $moduleId); return $orderedModule; }
public function testSubmitQuiz() { if (!isset($_SESSION)) { session_start(); } $userId = $_SESSION['userID']; $quizId = 621753; $dbHelper = new DbHelper(); $canvasHelper = new CanvasHelper(); // $canvasHelper->postQuizTakingSession($quizId); $quizSubmission = $dbHelper->getQuizSubmission($quizId, $userId); $result = $canvasHelper->postSubmitQuiz($quizSubmission); echo json_encode($result); }
public function test() { $req = new ModulesRequest(ActionType::GET, 380206, null, true, true, null, null, false); $db = new \Delphinium\Roots\DB\DbHelper(); $res = $db->getModuleData($req); echo json_encode($res); // $this->convertDatesUTCLocal(); // $now = new DateTime(date("Y-m-d")); // echo json_encode($now); // // // // $rb = new RuleBuilder; // // $bonus_90 = $rb->create('current_user_submissions', 'submission', // $rb['submission']['score']->greaterThan($rb['score_threshold']), // [ // $rb['(bonus)']->assign($rb['(bonus)']->add($rb['points'])) // ]); // // $rb['(bonus)'] = 0; // $rb['submission']['score'] = 0; // $rb['score_threshold'] = 0; // $rb['point'] = 0; // // $rg = new RuleGroup('submissionstest'); // $rg->add($bonus_90); // $rg->saveRules(); // $manager = ComponentManager::instance(); // echo json_encode($manager->listComponents()); }
public function doBltiHandshake() { //first obtain the details of the LTI configuration they chose $instanceFromDB = LtiConfigurations::find($this->property('ltiInstance')); $approver = $this->property('approver'); $arr = $this->getApproverOptions(); $approverRole = $arr[$approver]; if (!isset($_SESSION)) { session_start(); } $_SESSION['baseUrl'] = Config::get('app.url', 'backend'); $_SESSION['courseID'] = \Input::get('custom_canvas_course_id'); $_SESSION['userID'] = \Input::get('custom_canvas_user_id'); $_SESSION['domain'] = \Input::get('custom_canvas_api_domain'); //TODO: make sure this parameter below works with all other LMSs $_SESSION['lms'] = \Input::get('tool_consumer_info_product_family_code'); //to maintain the users table synchronized with Canvas, everytime a student comes in we'll check to make sure they're in the DB. //If they're not, we will pull all the students from Canvas and refresh our users table. $dbHelper = new DbHelper(); $user = $dbHelper->getUser($_SESSION['courseID'], $_SESSION['userID']); if (is_null($user)) { //get all students from Canvas $roots = new Roots(); $roots->getStudentsInCourse(); } //check to see if user is an Instructor $rolesStr = \Input::get('roles'); $consumerKey = $instanceFromDB['ConsumerKey']; $clientId = $instanceFromDB['DeveloperId']; //Check to see if the lti handshake passes $context = new Blti($consumerKey, false, false); if ($context->valid) { // query DB to see if user has token, if yes, go to LTI. $userCheck = User::where('course_id', $_SESSION['courseID'])->first(); if (!$userCheck) { //if no user is found, redirect to canvas permission page if (stristr($rolesStr, $approverRole)) { //As per my discussion with Jared, we will use the instructor's token only. This is the token that will be stored in the DB //and the one that will be used to make all requests. We will NOT store student's tokens. //TODO: take this redirectUri out into some parameter somewhere... $redirectUri = "{$_SESSION['baseUrl']}saveUserInfo?lti={$this->property('ltiInstance')}"; $url = "https://{$_SESSION['domain']}/login/oauth2/auth?client_id={$clientId}&response_type=code&redirect_uri={$redirectUri}"; $this->redirect($url); } else { echo "A(n) {$approverRole} must authorize this course. Please contact your instructor."; return; } } else { //set the professor's token $_SESSION['userToken'] = $userCheck->encrypted_token; //get the timezone $roots = new Roots(); $course = $roots->getCourse(); $account_id = $course->account_id; $account = $roots->getAccount($account_id); $_SESSION['timezone'] = new \DateTimeZone($account->default_time_zone); } } else { echo 'There is a problem. Please notify your instructor'; } }
public function getSetOfUsersMilestoneInfo($experienceInstanceId, $userIds) { //init experience variables $experienceInstance = ExperienceModel::find($experienceInstanceId); $maxExperiencePts = $experienceInstance->total_points; $utcTimeZone = new DateTimeZone('UTC'); $stDate = $experienceInstance->start_date->setTimezone($utcTimeZone); $endDate = $experienceInstance->end_date->setTimezone($utcTimeZone); $expComponent = new ExperienceComponent(); $ptsPerSecond = $expComponent->getPtsPerSecond($stDate, $endDate, $experienceInstance->total_points); $bonusPerSecond = $experienceInstance->bonus_per_day / 24 / 60 / 60; $bonusSeconds = $experienceInstance->bonus_days * 24 * 60 * 60; $penaltyPerSecond = $experienceInstance->penalty_per_day / 24 / 60 / 60; $penaltySeconds = $experienceInstance->penalty_days * 24 * 60 * 60; //get milestones $expComponent = new ExperienceComponent(); $milestonesOrderedByPointsDesc = $expComponent->getMilestonesOrderedByPointsDesc($experienceInstanceId); //get grading standards if (is_null($this->roots)) { $this->roots = new Roots(); } $standards = $this->roots->getGradingStandards(); $grading_scheme = $standards[0]->grading_scheme; //get all students in course $dbHelper = new DbHelper(); if (!isset($_SESSION)) { session_start(); } $courseId = $_SESSION['courseID']; $users = $dbHelper->getUsersInCourseWithRole($courseId, 'Learner'); $filteredUsers = array(); foreach ($userIds as $userId) { $res = array_values(array_filter($users->toArray(), function ($elem) use($userId) { return intval($elem['user']['user_id']) === intval($userId); })); $filteredUsers = array_merge($filteredUsers, $res); } $masterArr = array(); foreach ($filteredUsers as $user) { $item = $this->getUserMilestoneInfo($user['user'], $milestonesOrderedByPointsDesc, $ptsPerSecond, $stDate, $endDate, $bonusPerSecond, $bonusSeconds, $penaltyPerSecond, $penaltySeconds, $maxExperiencePts, $grading_scheme); $masterArr[] = $item; } return $masterArr; }
public function doBltiHandshake() { //first obtain the details of the LTI configuration they chose $dbHelper = new DbHelper(); $instanceFromDB = LtiConfigurations::find($this->property('ltiInstance')); $approver = $this->property('approver'); $arr = $this->getApproverOptions(); $approverRole = $arr[$approver]; if (!isset($_SESSION)) { session_start(); } $_SESSION['baseUrl'] = Config::get('app.url', 'backend'); $_SESSION['courseID'] = \Input::get('custom_canvas_course_id'); $_SESSION['userID'] = \Input::get('custom_canvas_user_id'); $_SESSION['domain'] = \Input::get('custom_canvas_api_domain'); //TODO: make sure this parameter below works with all other LMSs $_SESSION['lms'] = \Input::get('tool_consumer_info_product_family_code'); //check to see if user is an Instructor $rolesStr = \Input::get('roles'); $consumerKey = $instanceFromDB['ConsumerKey']; $clientId = $instanceFromDB['DeveloperId']; //Check to see if the lti handshake passes $context = new Blti($consumerKey, false, false); if ($context->valid) { // query DB to see if user has token, if yes, go to LTI. $userCheck = $dbHelper->getCourseApprover($_SESSION['courseID']); if (!$userCheck) { //if no user is found, redirect to canvas permission page if (stristr($rolesStr, $approverRole)) { //As per my discussion with Jared, we will use the instructor's token only. This is the token that will be stored in the DB //and the one that will be used to make all requests. We will NOT store student's tokens. //TODO: take this redirectUri out into some parameter somewhere... $redirectUri = "{$_SESSION['baseUrl']}saveUserInfo?lti={$this->property('ltiInstance')}"; $url = "https://{$_SESSION['domain']}/login/oauth2/auth?client_id={$clientId}&response_type=code&redirect_uri={$redirectUri}"; $this->redirect($url); } else { echo "A(n) {$approverRole} must authorize this course. Please contact your instructor."; return; } } else { //set the professor's token $_SESSION['userToken'] = $userCheck->encrypted_token; //get the timezone $roots = new Roots(); $course = $roots->getCourse(); $account_id = $course->account_id; $account = $roots->getAccount($account_id); $courseId = $_SESSION['courseID']; $_SESSION['timezone'] = new \DateTimeZone($account->default_time_zone); //to maintain the users table synchronized with Canvas, everytime a student comes in we'll check to make sure they're in the DB. //If they're not, we will pull all the students from Canvas and refresh our users table. $dbHelper = new DbHelper(); $user = $dbHelper->getUserInCourse($courseId, $_SESSION['userID']); if (is_null($user)) { //get all students from Canvas $roots = new Roots(); $roots->getStudentsInCourse(); } //Also, every so often (every 12 hrs?) we will check to make sure that students who have dropped the class are deleted from the users_course table //Failing to do so will make it so that when we request their submissions along with other students' submissions, the entire // call returns with an Unauthorized error message $approver = $dbHelper->getCourseApprover($courseId); $now = Carbon::now(); $updatedDate = $approver->updated_at; $diff = $updatedDate->diffInHours($now, false); if ($diff > 24) { $allStudentsDb = $dbHelper->getUsersInCourseWithRole($_SESSION['courseID'], 'Learner'); $allStudentsFromCanvas = $roots->getStudentsInCourse(); foreach ($allStudentsDb as $dbStudent) { $filteredItems = array_values(array_filter($allStudentsFromCanvas, function ($elem) use($dbStudent) { return intval($elem->user_id) === intval($dbStudent->user_id); })); if (count($filteredItems) < 1) { $dbHelper->deleteUserFromRole($courseId, $dbStudent->user_id, 'Learner'); } } //update the approver $approver->updated_at = $now; $approver->save(); } } } else { echo 'There is a problem. Please notify your instructor'; } }