Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace enrol_lti\local\ltiadvantage\utility;
  18  
  19  /**
  20   * Utility class for LTI Advantage messages.
  21   *
  22   * @package    enrol_lti
  23   * @copyright  2021 Jake Dallimore <jrhdallimore@gmail.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  final class message_helper {
  27  
  28      /**
  29       * Determine if the LTI roles in the launch contains any instructor or admin roles.
  30       *
  31       * @param array $jwtdata array formatted JWT data from the launch.
  32       * @return bool true if the roles contain a constructor role, false otherwise.
  33       */
  34      public static function is_instructor_launch(array $jwtdata): bool {
  35          return self::user_is_admin($jwtdata) || self::user_is_staff($jwtdata, true);
  36      }
  37  
  38      /**
  39       * Check whether the launch user is an instructor.
  40       *
  41       * @param array $jwtdata array formatted JWT data from the launch.
  42       * @param bool $includelegacyroles whether to also consider legacy simple names as valid roles.
  43       * @return bool true if the user is an instructor, false otherwise.
  44       */
  45      private static function user_is_staff(array $jwtdata, bool $includelegacyroles = false): bool {
  46          // See: http://www.imsglobal.org/spec/lti/v1p3/#role-vocabularies.
  47          // This method also provides support for (legacy, deprecated) simple names for context roles.
  48          // I.e. 'ContentDeveloper' may be supported.
  49          $launchroles = $jwtdata['https://purl.imsglobal.org/spec/lti/claim/roles'] ?? null;
  50          if ($launchroles) {
  51              $staffroles = [
  52                  'http://purl.imsglobal.org/vocab/lis/v2/membership#ContentDeveloper',
  53                  'http://purl.imsglobal.org/vocab/lis/v2/membership#Instructor',
  54                  'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistant'
  55              ];
  56  
  57              if ($includelegacyroles) {
  58                  $staffroles[] = 'ContentDeveloper';
  59                  $staffroles[] = 'Instructor';
  60                  $staffroles[] = 'Instructor#TeachingAssistant';
  61              }
  62  
  63              foreach ($staffroles as $validrole) {
  64                  if (in_array($validrole, $launchroles)) {
  65                      return true;
  66                  }
  67              }
  68          }
  69          return false;
  70      }
  71  
  72      /**
  73       * Check whether the launch user has an admin role.
  74       *
  75       * @param array $jwtdata array formatted JWT data from the launch.
  76       * @return bool true if the user is admin, false otherwise.
  77       */
  78      private static function user_is_admin(array $jwtdata): bool {
  79          // See: http://www.imsglobal.org/spec/lti/v1p3/#role-vocabularies.
  80          $launchroles = $jwtdata['https://purl.imsglobal.org/spec/lti/claim/roles'] ?? null;
  81          if ($launchroles) {
  82              $adminroles = [
  83                  'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Administrator',
  84                  'http://purl.imsglobal.org/vocab/lis/v2/system/person#Administrator'
  85              ];
  86  
  87              foreach ($adminroles as $validrole) {
  88                  if (in_array($validrole, $launchroles)) {
  89                      return true;
  90                  }
  91              }
  92          }
  93          return false;
  94      }
  95  }