|
| 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 | + * Define search area. |
| 19 | + * |
| 20 | + * @package mod_cms |
| 21 | + * @author Tomo Tsuyuki <[email protected]> |
| 22 | + * @copyright 2024 Catalyst IT |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 24 | + */ |
| 25 | +namespace mod_cms\search; |
| 26 | + |
| 27 | +defined('MOODLE_INTERNAL') || die(); |
| 28 | + |
| 29 | +require_once($CFG->dirroot . '/mod/cms/lib.php'); |
| 30 | + |
| 31 | +/** |
| 32 | + * Define search area. |
| 33 | + * |
| 34 | + * @package mod_cms |
| 35 | + * @author Tomo Tsuyuki <[email protected]> |
| 36 | + * @copyright 2024 Catalyst IT |
| 37 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 38 | + */ |
| 39 | +class cms extends \core_search\base_mod { |
| 40 | + |
| 41 | + /** |
| 42 | + * @var array Internal quick static cache. |
| 43 | + */ |
| 44 | + protected $cmsdata = []; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns recordset containing required data for indexing cms data records. |
| 48 | + * |
| 49 | + * @param int $modifiedfrom timestamp |
| 50 | + * @param \context|null $context Optional context to restrict scope of returned results |
| 51 | + * @return moodle_recordset|null Recordset (or null if no results) |
| 52 | + */ |
| 53 | + public function get_document_recordset($modifiedfrom = 0, \context $context = null) { |
| 54 | + global $DB; |
| 55 | + |
| 56 | + list ($contextjoin, $contextparams) = $this->get_context_restriction_sql( |
| 57 | + $context, 'cms', 'mc'); |
| 58 | + if ($contextjoin === null) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + $searcharea = explode(',', get_config('mod_cms', 'search_area')); |
| 63 | + if (empty($searcharea)) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + list($insql, $inparams) = $DB->get_in_or_equal($searcharea); |
| 67 | + |
| 68 | + $sql = "SELECT mcd.*, mc.id AS cmsid, mc.course AS courseid |
| 69 | + FROM {customfield_data} mcd |
| 70 | + JOIN {cms} mc ON mc.id = mcd.instanceid |
| 71 | + $contextjoin |
| 72 | + WHERE mcd.timemodified >= ? AND mcd.fieldid " . $insql . " ORDER BY mcd.timemodified ASC"; |
| 73 | + return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom], $inparams)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the document associated with this data id. |
| 78 | + * |
| 79 | + * @param stdClass $record |
| 80 | + * @param array $options |
| 81 | + * @return \core_search\document |
| 82 | + */ |
| 83 | + public function get_document($record, $options = []) { |
| 84 | + try { |
| 85 | + $cm = $this->get_cm('cms', $record->cmsid, $record->courseid); |
| 86 | + $context = \context_module::instance($cm->id); |
| 87 | + } catch (\dml_missing_record_exception $ex) { |
| 88 | + // Notify it as we run here as admin, we should see everything. |
| 89 | + debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . |
| 90 | + $ex->getMessage(), DEBUG_DEVELOPER); |
| 91 | + return false; |
| 92 | + } catch (\dml_exception $ex) { |
| 93 | + // Notify it as we run here as admin, we should see everything. |
| 94 | + debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + // Prepare associative array with data from DB. |
| 99 | + $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); |
| 100 | + $doc->set('title', content_to_text($record->value, false)); |
| 101 | + $doc->set('content', content_to_text($record->value, $record->valueformat)); |
| 102 | + $doc->set('contextid', $context->id); |
| 103 | + $doc->set('courseid', $record->courseid); |
| 104 | + $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); |
| 105 | + $doc->set('modified', $record->timemodified); |
| 106 | + |
| 107 | + // Check if this document should be considered new. |
| 108 | + if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) { |
| 109 | + // If the document was created after the last index time, it must be new. |
| 110 | + $doc->set_is_new(true); |
| 111 | + } |
| 112 | + |
| 113 | + return $doc; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Whether the user can access the document or not. |
| 118 | + * |
| 119 | + * @param int $id data id |
| 120 | + * @return bool |
| 121 | + */ |
| 122 | + public function check_access($id) { |
| 123 | + try { |
| 124 | + $data = $this->get_data($id); |
| 125 | + $cminfo = $this->get_cm('cms', $data->instanceid, $data->courseid); |
| 126 | + } catch (\dml_missing_record_exception $ex) { |
| 127 | + return \core_search\manager::ACCESS_DELETED; |
| 128 | + } catch (\dml_exception $ex) { |
| 129 | + return \core_search\manager::ACCESS_DENIED; |
| 130 | + } |
| 131 | + |
| 132 | + // Recheck uservisible although it should have already been checked in core_search. |
| 133 | + if ($cminfo->uservisible === false) { |
| 134 | + return \core_search\manager::ACCESS_DENIED; |
| 135 | + } |
| 136 | + |
| 137 | + $context = \context_module::instance($cminfo->id); |
| 138 | + |
| 139 | + if (!has_capability('mod/cms:view', $context)) { |
| 140 | + return \core_search\manager::ACCESS_DENIED; |
| 141 | + } |
| 142 | + |
| 143 | + return \core_search\manager::ACCESS_GRANTED; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Link to the cms. |
| 148 | + * |
| 149 | + * @param \core_search\document $doc |
| 150 | + * @return \moodle_url |
| 151 | + */ |
| 152 | + public function get_doc_url(\core_search\document $doc) { |
| 153 | + return $this->get_context_url($doc); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Link to the cms. |
| 158 | + * |
| 159 | + * @param \core_search\document $doc |
| 160 | + * @return \moodle_url |
| 161 | + */ |
| 162 | + public function get_context_url(\core_search\document $doc) { |
| 163 | + $contextmodule = \context::instance_by_id($doc->get('contextid')); |
| 164 | + return new \moodle_url('/mod/cms/view.php', ['id' => $contextmodule->instanceid]); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns the specified data from its internal cache. |
| 169 | + * |
| 170 | + * @throws \dml_missing_record_exception |
| 171 | + * @param int $id |
| 172 | + * @return stdClass |
| 173 | + */ |
| 174 | + protected function get_data($id) { |
| 175 | + global $DB; |
| 176 | + if (empty($this->cmsdata[$id])) { |
| 177 | + $sql = "SELECT mcd.*, mc.id AS cmsid, mc.course AS courseid |
| 178 | + FROM {customfield_data} mcd |
| 179 | + JOIN {cms} mc ON mc.id = mcd.instanceid |
| 180 | + WHERE mcd.id = :id"; |
| 181 | + $this->cmsdata[$id] = $DB->get_record_sql($sql, ['id' => $id]); |
| 182 | + if (!$this->cmsdata[$id]) { |
| 183 | + throw new \dml_missing_record_exception('cms_data'); |
| 184 | + } |
| 185 | + } |
| 186 | + return $this->cmsdata[$id]; |
| 187 | + } |
| 188 | +} |
0 commit comments