$user->email = \core_user::clean_field($ltirequest->getUserEmail(), 'email'); // Get the user data from the LTI consumer. $user = \enrol_lti\helper::assign_user_tool_data($tool, $user); // Check if the user exists. if (!($dbuser = $DB->get_record('user', array('username' => $user->username, 'deleted' => 0)))) { // If the email was stripped/not set then fill it with a default one. This // stops the user from being redirected to edit their profile page. if (empty($user->email)) { $user->email = $user->username . "@example.com"; } $user->auth = 'lti'; $user->id = user_create_user($user); // Get the updated user record. $user = $DB->get_record('user', array('id' => $user->id)); } else { if (\enrol_lti\helper::user_match($user, $dbuser)) { $user = $dbuser; } else { // If email is empty remove it, so we don't update the user with an empty email. if (empty($user->email)) { unset($user->email); } $user->id = $dbuser->id; user_update_user($user); // Get the updated user record. $user = $DB->get_record('user', array('id' => $user->id)); } } // Update user image. $image = false; if (!empty($ltirequest->info['user_image'])) {
/** * Override onLaunch with tool logic. * @return void */ protected function onLaunch() { global $DB, $SESSION, $CFG; // Check for valid consumer. if (empty($this->consumer) || $this->dataConnector->loadToolConsumer($this->consumer) === false) { $this->ok = false; $this->message = get_string('invalidtoolconsumer', 'enrol_lti'); return; } $url = helper::get_launch_url($this->tool->id); // If a tool proxy has been stored for the current consumer trying to access a tool, // check that the tool is being launched from the correct url. $correctlaunchurl = false; if (!empty($this->consumer->toolProxy)) { $proxy = json_decode($this->consumer->toolProxy); $handlers = $proxy->tool_profile->resource_handler; foreach ($handlers as $handler) { foreach ($handler->message as $message) { $handlerurl = new moodle_url($message->path); $fullpath = $handlerurl->out(false); if ($message->message_type == "basic-lti-launch-request" && $fullpath == $url) { $correctlaunchurl = true; break 2; } } } } else { if ($this->tool->secret == $this->consumer->secret) { // Test if the LTI1 secret for this tool is being used. Then we know the correct tool is being launched. $correctlaunchurl = true; } } if (!$correctlaunchurl) { $this->ok = false; $this->message = get_string('invalidrequest', 'enrol_lti'); return; } // Before we do anything check that the context is valid. $tool = $this->tool; $context = context::instance_by_id($tool->contextid); // Set the user data. $user = new stdClass(); $user->username = helper::create_username($this->consumer->getKey(), $this->user->ltiUserId); if (!empty($this->user->firstname)) { $user->firstname = $this->user->firstname; } else { $user->firstname = $this->user->getRecordId(); } if (!empty($this->user->lastname)) { $user->lastname = $this->user->lastname; } else { $user->lastname = $this->tool->contextid; } $user->email = core_user::clean_field($this->user->email, 'email'); // Get the user data from the LTI consumer. $user = helper::assign_user_tool_data($tool, $user); // Check if the user exists. if (!($dbuser = $DB->get_record('user', ['username' => $user->username, 'deleted' => 0]))) { // If the email was stripped/not set then fill it with a default one. This // stops the user from being redirected to edit their profile page. if (empty($user->email)) { $user->email = $user->username . "@example.com"; } $user->auth = 'lti'; $user->id = \user_create_user($user); // Get the updated user record. $user = $DB->get_record('user', ['id' => $user->id]); } else { if (helper::user_match($user, $dbuser)) { $user = $dbuser; } else { // If email is empty remove it, so we don't update the user with an empty email. if (empty($user->email)) { unset($user->email); } $user->id = $dbuser->id; \user_update_user($user); // Get the updated user record. $user = $DB->get_record('user', ['id' => $user->id]); } } // Update user image. if (isset($this->user) && isset($this->user->image) && !empty($this->user->image)) { $image = $this->user->image; } else { // Use custom_user_image parameter as a fallback. $image = $this->resourceLink->getSetting('custom_user_image'); } // Check if there is an image to process. if ($image) { helper::update_user_profile_image($user->id, $image); } // Check if we need to force the page layout to embedded. $isforceembed = $this->resourceLink->getSetting('custom_force_embed') == 1; // Check if we are an instructor. $isinstructor = $this->user->isStaff() || $this->user->isAdmin(); if ($context->contextlevel == CONTEXT_COURSE) { $courseid = $context->instanceid; $urltogo = new moodle_url('/course/view.php', ['id' => $courseid]); } else { if ($context->contextlevel == CONTEXT_MODULE) { $cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST); $urltogo = new moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]); // If we are a student in the course module context we do not want to display blocks. if (!$isforceembed && !$isinstructor) { $isforceembed = true; } } else { print_error('invalidcontext'); exit; } } // Force page layout to embedded if necessary. if ($isforceembed) { $SESSION->forcepagelayout = 'embedded'; } else { // May still be set from previous session, so unset it. unset($SESSION->forcepagelayout); } // Enrol the user in the course with no role. $result = helper::enrol_user($tool, $user->id); // Display an error, if there is one. if ($result !== helper::ENROLMENT_SUCCESSFUL) { print_error($result, 'enrol_lti'); exit; } // Give the user the role in the given context. $roleid = $isinstructor ? $tool->roleinstructor : $tool->rolelearner; role_assign($roleid, $user->id, $tool->contextid); // Login user. $sourceid = $this->user->ltiResultSourcedId; $serviceurl = $this->resourceLink->getSetting('lis_outcome_service_url'); // Check if we have recorded this user before. if ($userlog = $DB->get_record('enrol_lti_users', ['toolid' => $tool->id, 'userid' => $user->id])) { if ($userlog->sourceid != $sourceid) { $userlog->sourceid = $sourceid; } if ($userlog->serviceurl != $serviceurl) { $userlog->serviceurl = $serviceurl; } $userlog->lastaccess = time(); $DB->update_record('enrol_lti_users', $userlog); } else { // Add the user details so we can use it later when syncing grades and members. $userlog = new stdClass(); $userlog->userid = $user->id; $userlog->toolid = $tool->id; $userlog->serviceurl = $serviceurl; $userlog->sourceid = $sourceid; $userlog->consumerkey = $this->consumer->getKey(); $userlog->consumersecret = $tool->secret; $userlog->lastgrade = 0; $userlog->lastaccess = time(); $userlog->timecreated = time(); $userlog->membershipsurl = $this->resourceLink->getSetting('ext_ims_lis_memberships_url'); $userlog->membershipsid = $this->resourceLink->getSetting('ext_ims_lis_memberships_id'); $DB->insert_record('enrol_lti_users', $userlog); } // Finalise the user log in. complete_user_login($user); // Everything's good. Set appropriate OK flag and message values. $this->ok = true; $this->message = get_string('success'); if (empty($CFG->allowframembedding)) { // Provide an alternative link. $stropentool = get_string('opentool', 'enrol_lti'); echo html_writer::tag('p', get_string('frameembeddingnotenabled', 'enrol_lti')); echo html_writer::link($urltogo, $stropentool, ['target' => '_blank']); } else { // All done, redirect the user to where they want to go. redirect($urltogo); } }