Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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 mod_lti\reportbuilder\local\entities;
  18  
  19  use core_reportbuilder\local\filters\select;
  20  use core_reportbuilder\local\filters\text;
  21  use lang_string;
  22  use core_reportbuilder\local\entities\base;
  23  use core_reportbuilder\local\report\column;
  24  use core_reportbuilder\local\report\filter;
  25  
  26  /**
  27   * Course external tools entity class implementation.
  28   *
  29   * Defines all the columns and filters that can be added to reports that use this entity.
  30   *
  31   * @package    mod_lti
  32   * @copyright  2023 Jake Dallimore <jrhdallimore@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class tool_types extends base {
  36  
  37      /**
  38       * Database tables that this entity uses and their default aliases
  39       *
  40       * @return array
  41       */
  42      protected function get_default_table_aliases(): array {
  43          return ['lti_types' => 'tt', 'lti' => 'ti'];
  44      }
  45  
  46      /**
  47       * The default title for this entity
  48       *
  49       * @return lang_string
  50       */
  51      protected function get_default_entity_title(): lang_string {
  52          return new lang_string('entitycourseexternaltools', 'mod_lti');
  53      }
  54  
  55      /**
  56       * Initialize the entity
  57       *
  58       * @return base
  59       */
  60      public function initialise(): base {
  61          $columns = $this->get_all_columns();
  62          foreach ($columns as $column) {
  63              $this->add_column($column);
  64          }
  65  
  66          $filters = $this->get_all_filters();
  67          foreach ($filters as $filter) {
  68              $this->add_filter($filter);
  69          }
  70  
  71          return $this;
  72      }
  73  
  74      /**
  75       * Returns list of all available columns
  76       *
  77       * @return column[]
  78       */
  79      protected function get_all_columns(): array {
  80          $tablealias = $this->get_table_alias('lti_types');
  81  
  82          // Name column.
  83          $columns[] = (new column(
  84              'name',
  85              new lang_string('name', 'core'),
  86              $this->get_entity_name()
  87          ))
  88              ->add_joins($this->get_joins())
  89              ->set_type(column::TYPE_TEXT)
  90              ->add_fields("{$tablealias}.name, {$tablealias}.icon")
  91              ->set_is_sortable(true)
  92              ->add_callback(static function(string $name, \stdClass $data) {
  93                  global $OUTPUT;
  94  
  95                  $iconurl = $data->icon ?: $OUTPUT->image_url('monologo', 'lti')->out();
  96                  $iconclass = $data->icon ? ' nofilter' : '';
  97                  $iconcontainerclass = 'activityiconcontainer smaller content';
  98                  $name = $data->name;
  99                  $img = \html_writer::img($iconurl, get_string('courseexternaltooliconalt', 'mod_lti', $name),
 100                      ['class' => 'activityicon' . $iconclass]);
 101                  $name = \html_writer::span($name, 'align-self-center');
 102                  return \html_writer::div(\html_writer::div($img, 'mr-2 '.$iconcontainerclass) . $name, 'd-flex');
 103              });
 104  
 105          // Description column.
 106          $columns[] = (new column(
 107              'description',
 108              new lang_string('description', 'core'),
 109              $this->get_entity_name()
 110          ))
 111              ->add_joins($this->get_joins())
 112              ->set_type(column::TYPE_TEXT)
 113              ->add_field("{$tablealias}.description")
 114              ->set_is_sortable(true);
 115  
 116          // Course column.
 117          $columns[] = (new column(
 118              'course',
 119              new lang_string('course', 'core'),
 120              $this->get_entity_name()
 121          ))
 122              ->add_joins($this->get_joins())
 123              ->set_type(column::TYPE_INTEGER)
 124              ->add_field("{$tablealias}.course")
 125              ->set_is_sortable(true);
 126  
 127          // LTI Version column.
 128          $columns[] = (new column(
 129              'ltiversion',
 130              new lang_string('version'),
 131              $this->get_entity_name()
 132          ))
 133              ->add_joins($this->get_joins())
 134              ->set_type(column::TYPE_TEXT)
 135              ->add_field("{$tablealias}.ltiversion")
 136              ->set_is_sortable(true);
 137  
 138          return $columns;
 139      }
 140  
 141      /**
 142       * Return list of all available filters
 143       *
 144       * @return filter[]
 145       */
 146      protected function get_all_filters(): array {
 147          $tablealias = $this->get_table_alias('lti_types');
 148  
 149          return [
 150              // Name filter.
 151              (new filter(
 152                  text::class,
 153                  'name',
 154                  new lang_string('name'),
 155                  $this->get_entity_name(),
 156                  "{$tablealias}.name"
 157              ))
 158                  ->add_joins($this->get_joins()),
 159  
 160              // Description filter.
 161              (new filter(
 162                  text::class,
 163                  'description',
 164                  new lang_string('description'),
 165                  $this->get_entity_name(),
 166                  "{$tablealias}.description"
 167              ))
 168                  ->add_joins($this->get_joins()),
 169  
 170              // LTI Version filter.
 171              (new filter(
 172                  select::class,
 173                  'ltiversion',
 174                  new lang_string('version'),
 175                  $this->get_entity_name(),
 176                  "{$tablealias}.ltiversion"
 177              ))
 178                  ->add_joins($this->get_joins())
 179                  ->set_options_callback(static function() : array {
 180                      return ['LTI-1p0' => 'Legacy LTI', '1.3.0' => "LTI Advantage"];
 181                  })
 182          ];
 183      }
 184  }