|
| 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 | + * Block Course_Contacts main file. |
| 19 | + * |
| 20 | + * @package block_course_contacts |
| 21 | + * @author Mark Ward |
| 22 | + * 2016 Richard Oelmann |
| 23 | + * @copyright Mark Ward |
| 24 | + * @credits 2016 R. Oelmann |
| 25 | + * |
| 26 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 27 | + */ |
| 28 | + |
| 29 | +class block_course_contacts extends block_base { |
| 30 | + public function init() { |
| 31 | + global $USER; |
| 32 | + $this->title = get_string('course_contacts', 'block_course_contacts'); |
| 33 | + } |
| 34 | + |
| 35 | + // A custom function for shortening names. |
| 36 | + public function shorten_name($lname) { |
| 37 | + if (strpos($lname, '-')) { |
| 38 | + $names = explode('-', $lname); |
| 39 | + $lname = ''; |
| 40 | + foreach ($names as $name) { |
| 41 | + if (strlen($name) > 6) { |
| 42 | + $name = substr($name, 0, 1); |
| 43 | + } |
| 44 | + $lname .= $name."-"; |
| 45 | + } |
| 46 | + $lname = substr($lname, 0, -1); |
| 47 | + } |
| 48 | + if (strpos($lname, ' ')) { |
| 49 | + $names = explode(' ', $lname); |
| 50 | + $lname = $names[0]; |
| 51 | + } |
| 52 | + return $lname; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Gets all the users assigned this role in this context or higher |
| 57 | + * |
| 58 | + * @param int $roleid (can also be an array of ints!) |
| 59 | + * @param context $context |
| 60 | + * @param bool $parent if true, get list of users assigned in higher context too |
| 61 | + * @param string $fields fields from user (u.) , role assignment (ra) or role (r.) |
| 62 | + * @param string $sort sort from user (u.) , role assignment (ra) or role (r.) |
| 63 | + * @param bool $gethiddenignored use enrolments instead |
| 64 | + * @param string $group defaults to '' |
| 65 | + * @param mixed $limitfrom defaults to '' |
| 66 | + * @param mixed $limitnum defaults to '' |
| 67 | + * @param string $extrawheretest defaults to '' |
| 68 | + * @param string|array $whereparams defaults to '' |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + private function get_role_users($roleid, $context, $parent = false, $fields = '', |
| 72 | + $sort = 'u.lastname, u.firstname', $gethiddenignored = null, $group = '', |
| 73 | + $limitfrom = '', $limitnum = '', $extrawheretest = '', $whereparams = array()) { |
| 74 | + global $DB; |
| 75 | + |
| 76 | + if (empty($fields)) { |
| 77 | + $fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '. |
| 78 | + 'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '. |
| 79 | + 'u.country, u.picture, u.idnumber, u.department, u.institution, '. |
| 80 | + 'u.lang, u.timezone, u.lastaccess, u.mnethostid, r.name AS rolename, r.sortorder'; |
| 81 | + } |
| 82 | + |
| 83 | + $parentcontexts = ''; |
| 84 | + if ($parent) { |
| 85 | + $parentcontexts = substr($context->path, 1); // Kill leading slash. |
| 86 | + $parentcontexts = str_replace('/', ',', $parentcontexts); |
| 87 | + if ($parentcontexts !== '') { |
| 88 | + $parentcontexts = ' OR ra.contextid IN ('.$parentcontexts.' )'; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if ($roleid) { |
| 93 | + list($rids, $params) = $DB->get_in_or_equal($roleid, SQL_PARAMS_QM); |
| 94 | + $roleselect = "AND ra.roleid $rids"; |
| 95 | + } else { |
| 96 | + $params = array(); |
| 97 | + $roleselect = ''; |
| 98 | + } |
| 99 | + |
| 100 | + if ($group) { |
| 101 | + $groupjoin = "JOIN {groups_members} gm ON gm.userid = u.id"; |
| 102 | + $groupselect = " AND gm.groupid = ? "; |
| 103 | + $params[] = $group; |
| 104 | + } else { |
| 105 | + $groupjoin = ''; |
| 106 | + $groupselect = ''; |
| 107 | + } |
| 108 | + |
| 109 | + array_unshift($params, $context->id); |
| 110 | + |
| 111 | + if ($extrawheretest) { |
| 112 | + $extrawheretest = ' AND ' . $extrawheretest; |
| 113 | + $params = array_merge($params, $whereparams); |
| 114 | + } |
| 115 | + |
| 116 | + $sql = "SELECT $fields, ra.roleid |
| 117 | + FROM {role_assignments} ra |
| 118 | + JOIN {user} u ON u.id = ra.userid |
| 119 | + JOIN {role} r ON ra.roleid = r.id |
| 120 | + $groupjoin |
| 121 | + WHERE (ra.contextid = ? $parentcontexts) |
| 122 | + $roleselect |
| 123 | + $groupselect |
| 124 | + $extrawheretest |
| 125 | + GROUP BY $fields, ra.roleid |
| 126 | + ORDER BY $sort"; // Join now so that we can just use fullname() later. |
| 127 | + |
| 128 | + return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum); |
| 129 | + } |
| 130 | + |
| 131 | + public function get_content() { |
| 132 | + global $CFG, $DB, $OUTPUT, $USER, $COURSE; |
| 133 | + |
| 134 | + // If the user hasnt configured the plugin, set these as defaults. |
| 135 | + if (empty($this->config)) { |
| 136 | + $this->config = new stdclass(); |
| 137 | + $this->config->role_3 = 1; |
| 138 | + $this->config->email = 1; |
| 139 | + $this->config->message = 1; |
| 140 | + $this->config->phone = 0; |
| 141 | + } |
| 142 | + |
| 143 | + $courseid = $this->page->course->id; |
| 144 | + |
| 145 | + if ($this->content !== null) { |
| 146 | + return $this->content; |
| 147 | + } |
| 148 | + |
| 149 | + $this->content = new stdClass; |
| 150 | + |
| 151 | + $context = $this->page->context; |
| 152 | + $content = ''; |
| 153 | + // Find the roles available on this course. |
| 154 | + $roles = array_reverse(get_default_enrol_roles($context, null), true); |
| 155 | + $content .= html_writer::start_tag('div', array('class' => 'box')); |
| 156 | + |
| 157 | + // How are we going to sort the contacts? |
| 158 | + $orderby = 'u.lastname'; // Default. |
| 159 | + if (!empty($this->config->sortby)) { |
| 160 | + switch($this->config->sortby) { |
| 161 | + case 0: |
| 162 | + $orderby = 'u.lastname, u.firstname'; |
| 163 | + break; |
| 164 | + case 1: |
| 165 | + $orderby = 'u.lastaccess DESC'; |
| 166 | + break; |
| 167 | + case 2: |
| 168 | + $orderby = 'MIN(ra.timemodified)'; |
| 169 | + break; |
| 170 | + default: |
| 171 | + $orderby = 'u.lastname, u.firstname'; |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Step through each role and check that the config is set to display. |
| 177 | + $inherit = 0; |
| 178 | + if (isset($this->config->inherit)) { |
| 179 | + $inherit = $this->config->inherit; |
| 180 | + } |
| 181 | + $userfields = 'u.id,u.lastaccess,u.firstname,u.lastname,u.email,u.phone1,u.picture,u.imagealt, |
| 182 | + u.firstnamephonetic,u.lastnamephonetic,u.middlename,u.alternatename'; |
| 183 | + // Debugging($context->id); |
| 184 | + foreach ($roles as $key => $role) { |
| 185 | + $att = 'role_'.$key; |
| 186 | + if (!empty($this->config->$att)) { |
| 187 | + if ($this->config->$att == 1) { |
| 188 | + $contacts = $this->get_role_users($key, $context, $inherit, $userfields, $orderby, null, '', '', 30); |
| 189 | + |
| 190 | + // Because the role search finds the custom name and the proper name in brackets. |
| 191 | + if (!empty($contacts)) { |
| 192 | + if ($shortened = strstr($role, '(', true)) { |
| 193 | + $content .= html_writer::tag('h2', trim($shortened)); |
| 194 | + } else { |
| 195 | + $content .= html_writer::tag('h2', $role); |
| 196 | + } |
| 197 | + } |
| 198 | + // Now display each contact. |
| 199 | + foreach ($contacts as $contact) { |
| 200 | + |
| 201 | + $content .= html_writer::start_tag('div', array('class' => 'ccard')); |
| 202 | + $content .= $OUTPUT->user_picture($contact, array('size' => 50)); |
| 203 | + $content .= html_writer::start_tag('div', array('class' => 'info')); |
| 204 | + if ($contact->lastaccess > (time() - 300)) { |
| 205 | + // Online :)! |
| 206 | + $status = 'online'; |
| 207 | + } else { |
| 208 | + // Offline :(! |
| 209 | + $status = 'offline'; |
| 210 | + } |
| 211 | + $content .= html_writer::start_tag('div', array('class' => 'name '.$status)); |
| 212 | + $content .= $this->shorten_name($contact->firstname)." ".$this->shorten_name($contact->lastname); |
| 213 | + $content .= html_writer::end_tag('div'); |
| 214 | + $content .= html_writer::empty_tag('img', array( |
| 215 | + 'src' => $OUTPUT->pix_url($status, 'block_course_contacts'), |
| 216 | + 'title' => get_string($status, 'block_course_contacts'), |
| 217 | + 'alt' => get_string($status, 'block_course_contacts'), |
| 218 | + 'class' => 'status')); |
| 219 | + $content .= html_writer::empty_tag('hr'); |
| 220 | + $content .= html_writer::start_tag('div', array('class' => 'comms')); |
| 221 | + |
| 222 | + // Unless they are us. |
| 223 | + if ($USER->id != $contact->id) { |
| 224 | + // Should we display email? |
| 225 | + if ($this->config->email == 1) { |
| 226 | + // RO - removed, causing errors, retained for dev |
| 227 | + // if ($CFG->block_co_co_simpleemail) { |
| 228 | + // $url = new moodle_url('/blocks/course_contacts/email.php', array( |
| 229 | + // 'touid' => $contact->id, 'cid'=>$COURSE->id)); |
| 230 | + // } else { |
| 231 | + $url = 'mailto:'.strtolower($contact->email); |
| 232 | + // } |
| 233 | + $content .= html_writer::link($url, html_writer::empty_tag('img', array( |
| 234 | + 'src' => $OUTPUT->pix_url('mail', 'block_course_contacts'), |
| 235 | + 'title' => get_string('email', 'block_course_contacts').' '.$contact->firstname, |
| 236 | + 'alt' => get_string('email', 'block_course_contacts').' '.$contact->firstname)), |
| 237 | + array('target' => '_blank')); |
| 238 | + } |
| 239 | + // What about messages? |
| 240 | + if ($this->config->message == 1) { |
| 241 | + $url = new moodle_url('/message/index.php', array('id' => $contact->id)); |
| 242 | + $content .= html_writer::link($url, html_writer::empty_tag('img', array( |
| 243 | + 'src' => $OUTPUT->pix_url('message', 'block_course_contacts'), |
| 244 | + 'title' => get_string('message', 'block_course_contacts').' '.$contact->firstname, |
| 245 | + 'alt' => get_string('message', 'block_course_contacts').' '.$contact->firstname)), |
| 246 | + array('target' => '_blank')); |
| 247 | + } |
| 248 | + // And phone numbers? |
| 249 | + if ($this->config->phone == 1 && $contact->phone1 != "") { |
| 250 | + $url = 'tel:'.$contact->phone1; |
| 251 | + $content .= html_writer::link($url, html_writer::empty_tag('img', array( |
| 252 | + 'src' => $OUTPUT->pix_url('phone', 'block_course_contacts'), |
| 253 | + 'title' => get_string('phone', 'block_course_contacts').' '.$contact->phone1, |
| 254 | + 'alt' => get_string('phone', 'block_course_contacts').' '.$contact->phone1)), |
| 255 | + array()); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + $content .= html_writer::end_tag('div'); |
| 260 | + $content .= html_writer::end_tag('div'); |
| 261 | + $content .= html_writer::end_tag('div'); |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + $content .= html_writer::end_tag('div'); |
| 267 | + |
| 268 | + $this->content->text = $content; |
| 269 | + return $this->content; |
| 270 | + } |
| 271 | + |
| 272 | + public function instance_allow_config() { |
| 273 | + return true; |
| 274 | + } |
| 275 | + |
| 276 | +} |
0 commit comments