See Release Notes
Long Term Support Release
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 * Index teachers in a course 19 * 20 * @package core_user 21 * @author Nathan Nguyen <nathannguyen@catalyst-au.net> 22 * @copyright Catalyst IT 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core_user\search; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 require_once($CFG->dirroot . '/user/lib.php'); 31 32 /** 33 * Search for user role assignment in a course 34 * 35 * @package core_user 36 * @author Nathan Nguyen <nathannguyen@catalyst-au.net> 37 * @copyright Catalyst IT 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class course_teacher extends \core_search\base { 41 42 /** 43 * The context levels the search implementation is working on. 44 * 45 * @var array 46 */ 47 protected static $levels = [CONTEXT_COURSE]; 48 49 /** 50 * Returns the moodle component name. 51 * 52 * It might be the plugin name (whole frankenstyle name) or the core subsystem name. 53 * 54 * @return string 55 */ 56 public function get_component_name() { 57 return 'course_teacher'; 58 } 59 60 /** 61 * Returns recordset containing required data attributes for indexing. 62 * 63 * @param number $modifiedfrom 64 * @param \context|null $context Optional context to restrict scope of returned results 65 * @return \moodle_recordset|null Recordset (or null if no results) 66 */ 67 public function get_document_recordset($modifiedfrom = 0, \context $context = null) { 68 global $DB; 69 $teacherroleids = get_config('core', 'searchteacherroles'); 70 71 // Only index teacher roles. 72 if (!empty($teacherroleids)) { 73 $teacherroleids = explode(',', $teacherroleids); 74 list($insql, $inparams) = $DB->get_in_or_equal($teacherroleids, SQL_PARAMS_NAMED); 75 } else { 76 // Do not index at all. 77 list($insql, $inparams) = [' = :roleid', ['roleid' => 0]]; 78 } 79 80 $params = [ 81 'coursecontext' => CONTEXT_COURSE, 82 'modifiedfrom' => $modifiedfrom 83 ]; 84 85 $params = array_merge($params, $inparams); 86 87 $recordset = $DB->get_recordset_sql(" 88 SELECT u.*, ra.contextid, r.shortname as roleshortname, ra.id as itemid, ra.timemodified as timeassigned 89 FROM {role_assignments} ra 90 JOIN {context} ctx 91 ON ctx.id = ra.contextid 92 AND ctx.contextlevel = :coursecontext 93 JOIN {user} u 94 ON u.id = ra.userid 95 JOIN {role} r 96 ON r.id = ra.roleid 97 WHERE ra.timemodified >= :modifiedfrom AND r.id $insql 98 ORDER BY ra.timemodified ASC", $params); 99 return $recordset; 100 } 101 102 /** 103 * Returns document instances for each record in the recordset. 104 * 105 * @param \stdClass $record 106 * @param array $options 107 * @return \core_search\document 108 */ 109 public function get_document($record, $options = array()) { 110 $context = \context::instance_by_id($record->contextid); 111 112 // Content. 113 if ($context->contextlevel == CONTEXT_COURSE) { 114 $course = get_course($context->instanceid); 115 $contentdata = new \stdClass(); 116 $contentdata->role = ucfirst($record->roleshortname); 117 $contentdata->course = $course->fullname; 118 $content = get_string('content:courserole', 'core_search', $contentdata); 119 } else { 120 return false; 121 } 122 123 $doc = \core_search\document_factory::instance($record->itemid, $this->componentname, $this->areaname); 124 // Assigning properties to our document. 125 $doc->set('title', content_to_text(fullname($record), false)); 126 $doc->set('contextid', $context->id); 127 $doc->set('courseid', $context->instanceid); 128 $doc->set('itemid', $record->itemid); 129 $doc->set('modified', $record->timeassigned); 130 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); 131 $doc->set('userid', $record->id); 132 $doc->set('content', $content); 133 134 // Check if this document should be considered new. 135 if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timeassigned) { 136 $doc->set_is_new(true); 137 } 138 139 return $doc; 140 } 141 142 /** 143 * Checking whether I can access a document 144 * 145 * @param int $id user id 146 * @return int 147 */ 148 public function check_access($id) { 149 $user = $this->get_user($id); 150 if (!$user || $user->deleted) { 151 return \core_search\manager::ACCESS_DELETED; 152 } 153 154 if (user_can_view_profile($user)) { 155 return \core_search\manager::ACCESS_GRANTED; 156 } 157 158 return \core_search\manager::ACCESS_DENIED; 159 } 160 161 /** 162 * Returns a url to the document context. 163 * 164 * @param \core_search\document $doc 165 * @return \moodle_url 166 */ 167 public function get_context_url(\core_search\document $doc) { 168 $user = $this->get_user($doc->get('itemid')); 169 $courseid = $doc->get('courseid'); 170 return new \moodle_url('/user/view.php', array('id' => $user->id, 'course' => $courseid)); 171 } 172 173 /** 174 * Returns the user fullname to display as document title 175 * 176 * @param \core_search\document $doc 177 * @return string User fullname 178 */ 179 public function get_document_display_title(\core_search\document $doc) { 180 $user = $this->get_user($doc->get('itemid')); 181 return fullname($user); 182 } 183 184 /** 185 * Get user based on role assignment id 186 * 187 * @param int $itemid role assignment id 188 * @return mixed 189 */ 190 private function get_user($itemid) { 191 global $DB; 192 $sql = "SELECT u.* 193 FROM {user} u 194 JOIN {role_assignments} ra 195 ON ra.userid = u.id 196 WHERE ra.id = :raid"; 197 return $DB->get_record_sql($sql, array('raid' => $itemid)); 198 } 199 200 /** 201 * Returns a list of category names associated with the area. 202 * 203 * @return array 204 */ 205 public function get_category_names() { 206 return [\core_search\manager::SEARCH_AREA_CATEGORY_ALL, \core_search\manager::SEARCH_AREA_CATEGORY_USERS]; 207 } 208 209 /** 210 * Link to the teacher in the course 211 * 212 * @param \core_search\document $doc the document 213 * @return \moodle_url 214 */ 215 public function get_doc_url(\core_search\document $doc) { 216 return $this->get_context_url($doc); 217 } 218 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body