2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 class block_mnet_hosts
extends block_list
{
27 $this->title
= get_string('pluginname','block_mnet_hosts') ;
30 function has_config() {
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);
38 return array('site' => true);
42 function get_content() {
43 global $CFG, $USER, $DB, $OUTPUT;
45 // shortcut - only for logged in users!
46 if (!isloggedin() ||
isguestuser()) {
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
;
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
;
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
;
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
;
96 if ($this->content
!== NULL) {
97 return $this->content
;
100 // TODO: Test this query - it's appropriate? It works?
101 // get the hosts and whether we are doing SSO with them
107 a.name as application,
111 {mnet_application} a,
112 {mnet_host2service} h2s_IDP,
113 {mnet_service} s_IDP,
114 {mnet_host2service} h2s_SP,
119 h.id = h2s_IDP.hostid 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
133 $hosts = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id
, $CFG->mnet_all_hosts_id
));
135 $this->content
= new stdClass();
136 $this->content
->items
= array();
137 $this->content
->icons
= array();
138 $this->content
->footer
= '';
141 foreach ($hosts as $host) {
142 if ($host->id
== $USER->mnethostid
) {
143 $url = new \
moodle_url($host->wwwroot
);
145 $url = new \
moodle_url('/auth/mnet/jump.php', array('hostid' => $host->id
));
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
))
154 return $this->content
;
158 * This block shouldn't be added to a page if the mnet authentication method is disabled.
160 * @param moodle_page $page
163 public function can_block_be_added(moodle_page
$page): bool {
164 return is_enabled_auth('mnet');