See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * Search area for course custom fields. 19 * 20 * @package core_course 21 * @copyright Toni Barbera <toni@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_course\search; 26 27 use core_course\customfield\course_handler; 28 use core_customfield\data_controller; 29 use core_customfield\field_controller; 30 31 defined('MOODLE_INTERNAL') || die(); 32 33 /** 34 * Search area for course custom fields. 35 * 36 * @package core_course 37 * @copyright Toni Barbera <toni@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class customfield extends \core_search\base { 41 42 /** 43 * Custom fields are indexed at course context. 44 * 45 * @var array 46 */ 47 protected static $levels = [CONTEXT_COURSE]; 48 49 /** 50 * Returns recordset containing required data for indexing 51 * course custom fields. 52 * 53 * @param int $modifiedfrom timestamp 54 * @param \context|null $context Restriction context 55 * @return \moodle_recordset|null Recordset or null if no change possible 56 */ 57 public function get_document_recordset($modifiedfrom = 0, \context $context = null) { 58 global $DB; 59 60 list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql($context, 'c', SQL_PARAMS_NAMED); 61 if ($contextjoin === null) { 62 return null; 63 } 64 65 $fields = course_handler::create()->get_fields(); 66 if (!$fields) { 67 $fields = array(); 68 } 69 list($fieldsql, $fieldparam) = $DB->get_in_or_equal(array_keys($fields), SQL_PARAMS_NAMED, 'fld', true, 0); 70 71 // Restrict recordset to CONTEXT_COURSE (since we are implementing it to core_course\search). 72 $sql = "SELECT d.* 73 FROM {customfield_data} d 74 JOIN {course} c ON c.id = d.instanceid 75 JOIN {context} cnt ON cnt.instanceid = c.id 76 $contextjoin 77 WHERE d.timemodified >= :modifiedfrom 78 AND cnt.contextlevel = :contextlevel 79 AND d.fieldid $fieldsql 80 ORDER BY d.timemodified ASC"; 81 return $DB->get_recordset_sql($sql , array_merge($contextparams, 82 ['modifiedfrom' => $modifiedfrom, 'contextlevel' => CONTEXT_COURSE], $fieldparam)); 83 } 84 85 /** 86 * Returns the document associated with this section. 87 * 88 * @param \stdClass $record 89 * @param array $options 90 * @return \core_search\document|bool 91 */ 92 public function get_document($record, $options = array()) { 93 global $PAGE; 94 95 try { 96 $context = \context_course::instance($record->instanceid); 97 } catch (\moodle_exception $ex) { 98 // Notify it as we run here as admin, we should see everything. 99 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . 100 $ex->getMessage(), DEBUG_DEVELOPER); 101 return false; 102 } 103 104 $handler = course_handler::create(); 105 $field = $handler->get_fields()[$record->fieldid]; 106 $data = data_controller::create(0, $record, $field); 107 108 // Prepare associative array with data from DB. 109 $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); 110 $doc->set('title', content_to_text($field->get('name'), false)); 111 $doc->set('content', content_to_text($data->export_value(), FORMAT_HTML)); 112 $doc->set('contextid', $context->id); 113 $doc->set('courseid', $context->instanceid); 114 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); 115 $doc->set('modified', $record->timemodified); 116 117 // Check if this document should be considered new. 118 if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) { 119 // If the document was created after the last index time, it must be new. 120 $doc->set_is_new(true); 121 } 122 123 return $doc; 124 } 125 126 /** 127 * Whether the user can access the document or not. 128 * 129 * @param int $id The course instance id. 130 * @return int 131 */ 132 public function check_access($id) { 133 global $DB; 134 $course = $DB->get_record('course', array('id' => $id)); 135 if (!$course) { 136 return \core_search\manager::ACCESS_DELETED; 137 } 138 if (\core_course_category::can_view_course_info($course)) { 139 return \core_search\manager::ACCESS_GRANTED; 140 } 141 return \core_search\manager::ACCESS_DENIED; 142 } 143 144 /** 145 * Link to the course. 146 * 147 * @param \core_search\document $doc 148 * @return \moodle_url 149 */ 150 public function get_doc_url(\core_search\document $doc) { 151 return $this->get_context_url($doc); 152 } 153 154 /** 155 * Link to the course. 156 * 157 * @param \core_search\document $doc 158 * @return \moodle_url 159 */ 160 public function get_context_url(\core_search\document $doc) { 161 return new \moodle_url('/course/view.php', array('id' => $doc->get('courseid'))); 162 } 163 164 /** 165 * Returns the moodle component name. 166 * 167 * It might be the plugin name (whole frankenstyle name) or the core subsystem name. 168 * 169 * @return string 170 */ 171 public function get_component_name() { 172 return 'course'; 173 } 174 175 /** 176 * Returns an icon instance for the document. 177 * 178 * @param \core_search\document $doc 179 * @return \core_search\document_icon 180 */ 181 public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon { 182 return new \core_search\document_icon('i/customfield'); 183 } 184 185 /** 186 * Returns a list of category names associated with the area. 187 * 188 * @return array 189 */ 190 public function get_category_names() { 191 return [ 192 \core_search\manager::SEARCH_AREA_CATEGORY_COURSE_CONTENT, 193 \core_search\manager::SEARCH_AREA_CATEGORY_COURSES 194 ]; 195 } 196 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body