Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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   * MNet hosts block.
  19   *
  20   * @package    block_mnet_hosts
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  class block_mnet_hosts extends block_list {
  26      function init() {
  27          $this->title = get_string('pluginname','block_mnet_hosts') ;
  28      }
  29  
  30      function has_config() {
  31          return false;
  32      }
  33  
  34      function applicable_formats() {
  35          if (has_capability('moodle/site:mnetlogintoremote', context_system::instance(), NULL, false)) {
  36              return array('all' => true, 'mod' => false, 'tag' => false);
  37          } else {
  38              return array('site' => true);
  39          }
  40      }
  41  
  42      function get_content() {
  43          global $CFG, $USER, $DB, $OUTPUT;
  44  
  45          // shortcut -  only for logged in users!
  46          if (!isloggedin() || isguestuser()) {
  47              return false;
  48          }
  49  
  50          if (\core\session\manager::is_loggedinas()) {
  51              $this->content = new stdClass();
  52              $this->content->footer = html_writer::tag('span',
  53                  get_string('notpermittedtojumpas', 'mnet'));
  54              return $this->content;
  55          }
  56  
  57          // according to start_jump_session,
  58          // remote users can't on-jump
  59          // so don't show this block to them
  60          if (is_mnet_remote_user($USER)) {
  61              if (debugging() and !empty($CFG->debugdisplay)) {
  62                  $this->content = new stdClass();
  63                  $this->content->footer = html_writer::tag('span',
  64                      get_string('error_localusersonly', 'block_mnet_hosts'),
  65                      array('class' => 'error'));
  66                  return $this->content;
  67              } else {
  68                  return '';
  69              }
  70          }
  71  
  72          if (!is_enabled_auth('mnet')) {
  73              if (debugging() and !empty($CFG->debugdisplay)) {
  74                  $this->content = new stdClass();
  75                  $this->content->footer = html_writer::tag('span',
  76                      get_string('error_authmnetneeded', 'block_mnet_hosts'),
  77                      array('class' => 'error'));
  78                  return $this->content;
  79              } else {
  80                  return '';
  81              }
  82          }
  83  
  84          if (!has_capability('moodle/site:mnetlogintoremote', context_system::instance(), NULL, false)) {
  85              if (debugging() and !empty($CFG->debugdisplay)) {
  86                  $this->content = new stdClass();
  87                  $this->content->footer = html_writer::tag('span',
  88                      get_string('error_roamcapabilityneeded', 'block_mnet_hosts'),
  89                      array('class' => 'error'));
  90                  return $this->content;
  91              } else {
  92                  return '';
  93              }
  94          }
  95  
  96          if ($this->content !== NULL) {
  97              return $this->content;
  98          }
  99  
 100          // TODO: Test this query - it's appropriate? It works?
 101          // get the hosts and whether we are doing SSO with them
 102          $sql = "
 103               SELECT DISTINCT
 104                   h.id,
 105                   h.name,
 106                   h.wwwroot,
 107                   a.name as application,
 108                   a.display_name
 109               FROM
 110                   {mnet_host} h,
 111                   {mnet_application} a,
 112                   {mnet_host2service} h2s_IDP,
 113                   {mnet_service} s_IDP,
 114                   {mnet_host2service} h2s_SP,
 115                   {mnet_service} s_SP
 116               WHERE
 117                   h.id <> ? AND
 118                   h.id <> ? AND
 119                   h.id = h2s_IDP.hostid AND
 120                   h.deleted = 0 AND
 121                   h.applicationid = a.id AND
 122                   h2s_IDP.serviceid = s_IDP.id AND
 123                   s_IDP.name = 'sso_idp' AND
 124                   h2s_IDP.publish = '1' AND
 125                   h.id = h2s_SP.hostid AND
 126                   h2s_SP.serviceid = s_SP.id AND
 127                   s_SP.name = 'sso_idp' AND
 128                   h2s_SP.publish = '1'
 129               ORDER BY
 130                   a.display_name,
 131                   h.name";
 132  
 133          $hosts = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id, $CFG->mnet_all_hosts_id));
 134  
 135          $this->content = new stdClass();
 136          $this->content->items = array();
 137          $this->content->icons = array();
 138          $this->content->footer = '';
 139  
 140          if ($hosts) {
 141              foreach ($hosts as $host) {
 142                  if ($host->id == $USER->mnethostid) {
 143                      $url = new \moodle_url($host->wwwroot);
 144                  } else {
 145                      $url = new \moodle_url('/auth/mnet/jump.php', array('hostid' => $host->id));
 146                  }
 147                  $this->content->items[] = html_writer::tag('a',
 148                      $OUTPUT->pix_icon("i/{$host->application}_host", get_string('server', 'block_mnet_hosts')) . s($host->name),
 149                      array('href' => $url->out(), 'title' => s($host->name))
 150                  );
 151              }
 152          }
 153  
 154          return $this->content;
 155      }
 156  
 157      /**
 158       * This block shouldn't be added to a page if the mnet authentication method is disabled.
 159       *
 160       * @param moodle_page $page
 161       * @return bool
 162       */
 163      public function can_block_be_added(moodle_page $page): bool {
 164          return is_enabled_auth('mnet');
 165      }
 166  }