[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/lti/ -> tool.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * The main entry point for the external system.
  19   *
  20   * @package    enrol_lti
  21   * @copyright  2016 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../config.php');
  26  require_once($CFG->dirroot . '/user/lib.php');
  27  require_once($CFG->dirroot . '/enrol/lti/ims-blti/blti.php');
  28  
  29  $toolid = required_param('id', PARAM_INT);
  30  
  31  // Get the tool.
  32  $tool = \enrol_lti\helper::get_lti_tool($toolid);
  33  
  34  // Create the BLTI request.
  35  $ltirequest = new BLTI($tool->secret, false, false);
  36  
  37  // Correct launch request.
  38  if ($ltirequest->valid) {
  39      // Check if the authentication plugin is disabled.
  40      if (!is_enabled_auth('lti')) {
  41          print_error('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
  42          exit();
  43      }
  44  
  45      // Check if the enrolment plugin is disabled.
  46      if (!enrol_is_enabled('lti')) {
  47          print_error('enrolisdisabled', 'enrol_lti');
  48          exit();
  49      }
  50  
  51      // Check if the enrolment instance is disabled.
  52      if ($tool->status != ENROL_INSTANCE_ENABLED) {
  53          print_error('enrolisdisabled', 'enrol_lti');
  54          exit();
  55      }
  56  
  57      // Before we do anything check that the context is valid.
  58      $context = context::instance_by_id($tool->contextid);
  59  
  60      // Set the user data.
  61      $user = new stdClass();
  62      $user->username = \enrol_lti\helper::create_username($ltirequest->info['oauth_consumer_key'], $ltirequest->info['user_id']);
  63      if (!empty($ltirequest->info['lis_person_name_given'])) {
  64          $user->firstname = $ltirequest->info['lis_person_name_given'];
  65      } else {
  66          $user->firstname = $ltirequest->info['user_id'];
  67      }
  68      if (!empty($ltirequest->info['lis_person_name_family'])) {
  69          $user->lastname = $ltirequest->info['lis_person_name_family'];
  70      } else {
  71          $user->lastname = $ltirequest->info['context_id'];
  72      }
  73  
  74      $user->email = \core_user::clean_field($ltirequest->getUserEmail(), 'email');
  75  
  76      // Get the user data from the LTI consumer.
  77      $user = \enrol_lti\helper::assign_user_tool_data($tool, $user);
  78  
  79      // Check if the user exists.
  80      if (!$dbuser = $DB->get_record('user', array('username' => $user->username, 'deleted' => 0))) {
  81          // If the email was stripped/not set then fill it with a default one. This
  82          // stops the user from being redirected to edit their profile page.
  83          if (empty($user->email)) {
  84              $user->email = $user->username .  "@example.com";
  85          }
  86  
  87          $user->auth = 'lti';
  88          $user->id = user_create_user($user);
  89  
  90          // Get the updated user record.
  91          $user = $DB->get_record('user', array('id' => $user->id));
  92      } else {
  93          if (\enrol_lti\helper::user_match($user, $dbuser)) {
  94              $user = $dbuser;
  95          } else {
  96              // If email is empty remove it, so we don't update the user with an empty email.
  97              if (empty($user->email)) {
  98                  unset($user->email);
  99              }
 100  
 101              $user->id = $dbuser->id;
 102              user_update_user($user);
 103  
 104              // Get the updated user record.
 105              $user = $DB->get_record('user', array('id' => $user->id));
 106          }
 107      }
 108  
 109      // Update user image.
 110      $image = false;
 111      if (!empty($ltirequest->info['user_image'])) {
 112          $image = $ltirequest->info['user_image'];
 113      } else if (!empty($ltirequest->info['custom_user_image'])) {
 114          $image = $ltirequest->info['custom_user_image'];
 115      }
 116  
 117      // Check if there is an image to process.
 118      if ($image) {
 119          \enrol_lti\helper::update_user_profile_image($user->id, $image);
 120      }
 121  
 122      // Check if we are an instructor.
 123      $isinstructor = $ltirequest->isInstructor();
 124  
 125      if ($context->contextlevel == CONTEXT_COURSE) {
 126          $courseid = $context->instanceid;
 127          $urltogo = new moodle_url('/course/view.php', array('id' => $courseid));
 128  
 129          // May still be set from previous session, so unset it.
 130          unset($SESSION->forcepagelayout);
 131      } else if ($context->contextlevel == CONTEXT_MODULE) {
 132          $cmid = $context->instanceid;
 133          $cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
 134          $urltogo = new moodle_url('/mod/' . $cm->modname . '/view.php', array('id' => $cm->id));
 135  
 136          // If we are a student in the course module context we do not want to display blocks.
 137          if (!$isinstructor) {
 138              // Force the page layout.
 139              $SESSION->forcepagelayout = 'embedded';
 140          } else {
 141              // May still be set from previous session, so unset it.
 142              unset($SESSION->forcepagelayout);
 143          }
 144      } else {
 145          print_error('invalidcontext');
 146          exit();
 147      }
 148  
 149      // Enrol the user in the course with no role.
 150      $result = \enrol_lti\helper::enrol_user($tool, $user->id);
 151  
 152      // Display an error, if there is one.
 153      if ($result !== \enrol_lti\helper::ENROLMENT_SUCCESSFUL) {
 154          print_error($result, 'enrol_lti');
 155          exit();
 156      }
 157  
 158      // Give the user the role in the given context.
 159      $roleid = $isinstructor ? $tool->roleinstructor : $tool->rolelearner;
 160      role_assign($roleid, $user->id, $tool->contextid);
 161  
 162      // Login user.
 163      $sourceid = (!empty($ltirequest->info['lis_result_sourcedid'])) ? $ltirequest->info['lis_result_sourcedid'] : '';
 164      $serviceurl = (!empty($ltirequest->info['lis_outcome_service_url'])) ? $ltirequest->info['lis_outcome_service_url'] : '';
 165  
 166      // Check if we have recorded this user before.
 167      if ($userlog = $DB->get_record('enrol_lti_users', array('toolid' => $tool->id, 'userid' => $user->id))) {
 168          if ($userlog->sourceid != $sourceid) {
 169              $userlog->sourceid = $sourceid;
 170          }
 171          if ($userlog->serviceurl != $serviceurl) {
 172              $userlog->serviceurl = $serviceurl;
 173          }
 174          $userlog->lastaccess = time();
 175          $DB->update_record('enrol_lti_users', $userlog);
 176      } else {
 177          // Add the user details so we can use it later when syncing grades and members.
 178          $userlog = new stdClass();
 179          $userlog->userid = $user->id;
 180          $userlog->toolid = $tool->id;
 181          $userlog->serviceurl = $serviceurl;
 182          $userlog->sourceid = $sourceid;
 183          $userlog->consumerkey = $ltirequest->info['oauth_consumer_key'];
 184          $userlog->consumersecret = $tool->secret;
 185          $userlog->lastgrade = 0;
 186          $userlog->lastaccess = time();
 187          $userlog->timecreated = time();
 188  
 189          if (!empty($ltirequest->info['ext_ims_lis_memberships_url'])) {
 190              $userlog->membershipsurl = $ltirequest->info['ext_ims_lis_memberships_url'];
 191          } else {
 192              $userlog->membershipsurl = '';
 193          }
 194  
 195          if (!empty($ltirequest->info['ext_ims_lis_memberships_id'])) {
 196              $userlog->membershipsid = $ltirequest->info['ext_ims_lis_memberships_id'];
 197          } else {
 198              $userlog->membershipsid = '';
 199          }
 200          $DB->insert_record('enrol_lti_users', $userlog);
 201      }
 202  
 203      // Finalise the user log in.
 204      complete_user_login($user);
 205  
 206      if (empty($CFG->allowframembedding)) {
 207          // Provide an alternative link.
 208          $stropentool = get_string('opentool', 'enrol_lti');
 209          echo html_writer::tag('p', get_string('frameembeddingnotenabled', 'enrol_lti'));
 210          echo html_writer::link($urltogo, $stropentool, array('target' => '_blank'));
 211      } else {
 212          // All done, redirect the user to where they want to go.
 213          redirect($urltogo);
 214      }
 215  } else {
 216      echo $ltirequest->message;
 217  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1