MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / mnet_hosts / block_mnet_hosts.php
1 <?php
3 class block_mnet_hosts extends block_list {
4     function init() {
5         $this->title = get_string('pluginname','block_mnet_hosts') ;
6         $this->version = 2007101509;
7     }
9     function has_config() {
10         return false;
11     }
13     function applicable_formats() {
14         if (has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM), NULL, false)) {
15             return array('all' => true, 'mod' => false, 'tag' => false);
16         } else {
17             return array('site' => true);
18         }
19     }
21     function get_content() {
22         global $CFG, $USER, $DB, $OUTPUT;
24         // shortcut -  only for logged in users!
25         if (!isloggedin() || isguestuser()) {
26             return false;
27         }
29         // according to start_jump_session,
30         // remote users can't on-jump
31         // so don't show this block to them
32         if (is_mnet_remote_user($USER)) {
33             return '';
34         }
36         if (!is_enabled_auth('mnet')) {
37             // no need to query anything remote related
38             debugging( 'mnet authentication plugin is not enabled', DEBUG_ALL );
39             return '';
40         }
42         // check for outgoing roaming permission first
43         if (!has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM), NULL, false)) {
44             return '';
45         }
47         if ($this->content !== NULL) {
48             return $this->content;
49         }
51         // TODO: Test this query - it's appropriate? It works?
52         // get the hosts and whether we are doing SSO with them
53         $sql = "
54              SELECT DISTINCT
55                  h.id,
56                  h.name,
57                  h.wwwroot,
58                  a.name as application,
59                  a.display_name
60              FROM
61                  {mnet_host} h,
62                  {mnet_application} a,
63                  {mnet_host2service} h2s_IDP,
64                  {mnet_service} s_IDP,
65                  {mnet_host2service} h2s_SP,
66                  {mnet_service} s_SP
67              WHERE
68                  h.id <> ? AND
69                  h.id = h2s_IDP.hostid AND
70                  h.deleted = 0 AND
71                  h.applicationid = a.id AND
72                  h2s_IDP.serviceid = s_IDP.id AND
73                  s_IDP.name = 'sso_idp' AND
74                  h2s_IDP.publish = '1' AND
75                  h.id = h2s_SP.hostid AND
76                  h2s_SP.serviceid = s_SP.id AND
77                  s_SP.name = 'sso_idp' AND
78                  h2s_SP.publish = '1'
79              ORDER BY
80                  a.display_name,
81                  h.name";
83         $hosts = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id));
85         $this->content = new stdClass;
86         $this->content->items = array();
87         $this->content->icons = array();
88         $this->content->footer = '';
90         if ($hosts) {
91             foreach ($hosts as $host) {
92             $icon  = '<img src="'.$OUTPUT->pix_url('i/'.$host->application.'_host') . '"'.
93                 ' class="icon" alt="'.get_string('server', 'block_mnet_hosts').'" />';
95                 $this->content->icons[]=$icon;
96                 if ($host->id == $USER->mnethostid) {
97                     $this->content->items[]="<a title=\"" .s($host->name).
98                         "\" href=\"{$host->wwwroot}\">". s($host->name) ."</a>";
99                 } else {
100                     $this->content->items[]="<a title=\"" .s($host->name).
101                         "\" href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$host->id}\">" . s($host->name) ."</a>";
102                 }
103             }
104         }
106         return $this->content;
107     }