Skip to content

Commit 27dc35f

Browse files
committed
Issue #66: Add search API
1 parent 9bc6456 commit 27dc35f

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed

classes/search/cms.php

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
}

lang/en/cms.php

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
103103
$string['error:no_instance_hash'] = 'Module {$a} has no instance hash.';
104104
$string['error:no_config_hash'] = 'Module {$a} has no config hash.';
105105

106+
// Search strings.
107+
$string['search:cms'] = 'CMS';
108+
$string['search:settings'] = 'Search settings';
109+
$string['search:settings:area'] = 'Search area';
110+
$string['search:settings:area_desc'] = 'Search area for global search';
111+
106112
// Site datasource strings.
107113
$string['site:displayname'] = 'Site Info';
108114

settings.php

+18
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,23 @@
5656

5757
$ADMIN->add('modcmsfolder', $settings);
5858

59+
$settings = new admin_settingpage('mod_cms_search_settings', new lang_string('search:settings', 'mod_cms'));
60+
if ($ADMIN->fulltree) {
61+
$sql = "SELECT mcf.id, mcf.name
62+
FROM {customfield_category} mcc
63+
JOIN {customfield_field} mcf ON mcf.categoryid = mcc.id
64+
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type IN ('text', 'textarea')";
65+
$areas = $DB->get_records_sql_menu($sql);
66+
67+
$settings->add(new admin_setting_configmulticheckbox(
68+
'mod_cms/search_area',
69+
get_string('search:settings:area', 'mod_cms'),
70+
get_string('search:settings:area_desc', 'mod_cms'),
71+
null,
72+
$areas,
73+
));
74+
}
75+
$ADMIN->add('modcmsfolder', $settings);
76+
5977
// Tell core we already added the settings structure.
6078
$settings = null;

0 commit comments

Comments
 (0)