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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Displays enrolment LTI instances.
  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  namespace enrol_lti;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->libdir . '/tablelib.php');
  32  
  33  /**
  34   * Handles displaying enrolment LTI instances.
  35   *
  36   * @package    enrol_lti
  37   * @copyright  2016 Mark Nelson <markn@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class manage_table extends \table_sql {
  41  
  42      /**
  43       * @var \enrol_plugin $ltiplugin
  44       */
  45      protected $ltiplugin;
  46  
  47      /**
  48       * @var bool $ltienabled
  49       */
  50      protected $ltienabled;
  51  
  52      /**
  53       * @var bool $canconfig
  54       */
  55      protected $canconfig;
  56  
  57      /**
  58       * @var int $courseid The course id.
  59       */
  60      protected $courseid;
  61  
  62      /**
  63       * Sets up the table.
  64       *
  65       * @param string $courseid The id of the course.
  66       */
  67      public function __construct($courseid) {
  68          parent::__construct('enrol_lti_manage_table');
  69  
  70          $this->define_columns(array(
  71              'name',
  72              'launch',
  73              'registration',
  74              'edit'
  75          ));
  76          $this->define_headers(array(
  77              get_string('name'),
  78              get_string('launchdetails', 'enrol_lti'),
  79              get_string('registrationurl', 'enrol_lti'),
  80              get_string('edit')
  81          ));
  82          $this->collapsible(false);
  83          $this->sortable(false);
  84  
  85          // Set the variables we need access to.
  86          $this->ltiplugin = enrol_get_plugin('lti');
  87          $this->ltienabled = enrol_is_enabled('lti');
  88          $this->canconfig = has_capability('moodle/course:enrolconfig', \context_course::instance($courseid));
  89          $this->courseid = $courseid;
  90  
  91          // Set help icons.
  92          $launchicon = new \help_icon('launchdetails', 'enrol_lti');
  93          $regicon = new \help_icon('registrationurl', 'enrol_lti');
  94          $this->define_help_for_headers(['1' => $launchicon, '2' => $regicon]);
  95      }
  96  
  97      /**
  98       * Generate the name column.
  99       *
 100       * @param \stdClass $tool event data.
 101       * @return string
 102       */
 103      public function col_name($tool) {
 104          $toolcontext = \context::instance_by_id($tool->contextid, IGNORE_MISSING);
 105          $name = $toolcontext ? helper::get_name($tool) : $this->get_deleted_activity_name_html($tool);
 106  
 107          return $this->get_display_text($tool, $name);
 108      }
 109  
 110      /**
 111       * Generate the launch column.
 112       *
 113       * @param \stdClass $tool instance data.
 114       * @return string
 115       */
 116      public function col_launch($tool) {
 117          global $OUTPUT;
 118  
 119          $url = helper::get_cartridge_url($tool);
 120  
 121          $cartridgeurllabel = get_string('cartridgeurl', 'enrol_lti');
 122          $cartridgeurl = $url;
 123          $secretlabel = get_string('secret', 'enrol_lti');
 124          $secret = $tool->secret;
 125          $launchurl = helper::get_launch_url($tool->id);
 126          $launchurllabel = get_string('launchurl', 'enrol_lti');
 127  
 128          $data = [
 129                  "rows" => [
 130                      [ "label" => $cartridgeurllabel, "text" => $cartridgeurl, "id" => "cartridgeurl", "hidelabel" => false ],
 131                      [ "label" => $secretlabel, "text" => $secret, "id" => "secret", "hidelabel" => false ],
 132                      [ "label" => $launchurllabel, "text" => $launchurl, "id" => "launchurl", "hidelabel" => false ],
 133                  ]
 134              ];
 135  
 136          $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
 137  
 138          return $return;
 139      }
 140  
 141      /**
 142       * Generate the Registration column.
 143       *
 144       * @param \stdClass $tool instance data.
 145       * @return string
 146       */
 147      public function col_registration($tool) {
 148          global $OUTPUT;
 149  
 150          $url = helper::get_proxy_url($tool);
 151  
 152          $toolurllabel = get_string("registrationurl", "enrol_lti");
 153          $toolurl = $url;
 154  
 155          $data = [
 156                  "rows" => [
 157                      [ "label" => $toolurllabel, "text" => $toolurl, "id" => "toolurl" , "hidelabel" => true],
 158                  ]
 159              ];
 160  
 161          $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
 162          return $return;
 163      }
 164  
 165      /**
 166       * Generate the edit column.
 167       *
 168       * @param \stdClass $tool event data.
 169       * @return string
 170       */
 171      public function col_edit($tool) {
 172          global $OUTPUT;
 173  
 174          $buttons = array();
 175  
 176          $instance = new \stdClass();
 177          $instance->id = $tool->enrolid;
 178          $instance->courseid = $tool->courseid;
 179          $instance->enrol = 'lti';
 180          $instance->status = $tool->status;
 181  
 182          $strdelete = get_string('delete');
 183          $strenable = get_string('enable');
 184          $strdisable = get_string('disable');
 185  
 186          $url = new \moodle_url('/enrol/lti/index.php',
 187              array('sesskey' => sesskey(), 'courseid' => $this->courseid, 'legacy' => 1));
 188  
 189          if ($this->ltiplugin->can_delete_instance($instance)) {
 190              $aurl = new \moodle_url($url, array('action' => 'delete', 'instanceid' => $instance->id));
 191              $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/delete', $strdelete, 'core',
 192                  array('class' => 'iconsmall')));
 193          }
 194  
 195          if ($this->ltienabled && $this->ltiplugin->can_hide_show_instance($instance)) {
 196              if ($instance->status == ENROL_INSTANCE_ENABLED) {
 197                  $aurl = new \moodle_url($url, array('action' => 'disable', 'instanceid' => $instance->id));
 198                  $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/hide', $strdisable, 'core',
 199                      array('class' => 'iconsmall')));
 200              } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
 201                  $aurl = new \moodle_url($url, array('action' => 'enable', 'instanceid' => $instance->id));
 202                  $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/show', $strenable, 'core',
 203                      array('class' => 'iconsmall')));
 204              }
 205          }
 206  
 207          if ($this->ltienabled && $this->canconfig) {
 208              $linkparams = array(
 209                  'courseid' => $instance->courseid,
 210                  'id' => $instance->id,
 211                  'type' => $instance->enrol,
 212                  'legacy' => 1,
 213                  'returnurl' => new \moodle_url('/enrol/lti/index.php',
 214                      array('courseid' => $this->courseid, 'legacy' => 1))
 215              );
 216              $editlink = new \moodle_url("/enrol/editinstance.php", $linkparams);
 217              $buttons[] = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit'), 'core',
 218                  array('class' => 'iconsmall')));
 219          }
 220  
 221          return implode(' ', $buttons);
 222      }
 223  
 224      /**
 225       * Query the reader. Store results in the object for use by build_table.
 226       *
 227       * @param int $pagesize size of page for paginated displayed table.
 228       * @param bool $useinitialsbar do you want to use the initials bar.
 229       */
 230      public function query_db($pagesize, $useinitialsbar = true) {
 231          $total = \enrol_lti\helper::count_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p0/LTI-2p0']);
 232          $this->pagesize($pagesize, $total);
 233          $tools = \enrol_lti\helper::get_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p0/LTI-2p0'],
 234              $this->get_page_start(), $this->get_page_size());
 235          $this->rawdata = $tools;
 236          // Set initial bars.
 237          if ($useinitialsbar) {
 238              $this->initialbars($total > $pagesize);
 239          }
 240      }
 241  
 242      /**
 243       * Returns text to display in the columns.
 244       *
 245       * @param \stdClass $tool the tool
 246       * @param string $text the text to alter
 247       * @return string
 248       */
 249      protected function get_display_text($tool, $text) {
 250          if ($tool->status != ENROL_INSTANCE_ENABLED) {
 251              return \html_writer::tag('div', $text, array('class' => 'dimmed_text'));
 252          }
 253  
 254          return $text;
 255      }
 256  
 257      /**
 258       * Get a warning icon, with tooltip, describing enrolment instances sharing activities which have been deleted.
 259       *
 260       * @param \stdClass $tool the tool instance record.
 261       * @return string the HTML for the name column.
 262       */
 263      protected function get_deleted_activity_name_html(\stdClass $tool): string {
 264          global $OUTPUT;
 265          $icon = \html_writer::tag(
 266              'a',
 267              $OUTPUT->pix_icon('enrolinstancewarning', get_string('deletedactivityalt' , 'enrol_lti'), 'enrol_lti'), [
 268                  "class" => "btn btn-link p-0",
 269                  "role" => "button",
 270                  "data-container" => "body",
 271                  "data-toggle" => "popover",
 272                  "data-placement" => right_to_left() ? "left" : "right",
 273                  "data-content" => get_string('deletedactivitydescription', 'enrol_lti'),
 274                  "data-html" => "true",
 275                  "tabindex" => "0",
 276                  "data-trigger" => "focus"
 277              ]
 278          );
 279          $name = \html_writer::span($icon . get_string('deletedactivity', 'enrol_lti'));
 280          if ($tool->name) {
 281              $name .= \html_writer::empty_tag('br') . \html_writer::empty_tag('br') . $tool->name;
 282          }
 283  
 284          return $name;
 285      }
 286  }