Skip to content

Commit

Permalink
MDL-31989 search: get_config method to search areas
Browse files Browse the repository at this point in the history
Also content_to_text moved to weblib.
  • Loading branch information
David Monllao authored and danpoltawski committed Feb 23, 2016
1 parent f900b2b commit 69d6602
Show file tree
Hide file tree
Showing 25 changed files with 110 additions and 107 deletions.
2 changes: 1 addition & 1 deletion admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@
$searchareas = \core_search\manager::get_search_areas_list();
foreach ($searchareas as $areaid => $searcharea) {
list($componentname, $varname) = $searcharea->get_config_var_name();
$temp->add(new admin_setting_configcheckbox($componentname . '/enable' . $varname, $searcharea->get_visible_name(true),
$temp->add(new admin_setting_configcheckbox($componentname . '/' . $varname . '_enabled', $searcharea->get_visible_name(true),
'', 1, 1, 0));
}
$ADMIN->add('searchplugins', $temp);
Expand Down
1 change: 1 addition & 0 deletions lang/en/role.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@
$string['roletoassign'] = 'Role to assign';
$string['roletooverride'] = 'Role to override';
$string['safeoverridenotice'] = 'Note: Capabilities with higher risks are locked because you are only allowed to override safe capabilities.';
$string['search:query'] = 'Perform site-wide searches';
$string['selectanotheruser'] = 'Select another user';
$string['selectauser'] = 'Select a user';
$string['selectrole'] = 'Select a role';
Expand Down
10 changes: 7 additions & 3 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,11 @@ public function add($setting) {
return false;
}

$this->settings->{$setting->name} = $setting;
$name = $setting->name;
if ($setting->plugin) {
$name = $setting->plugin . $name;
}
$this->settings->{$name} = $setting;
return true;
}

Expand Down Expand Up @@ -9333,7 +9337,7 @@ public function output_html($data, $query='') {
foreach ($searchareas as $areaid => $searcharea) {
list($componentname, $varname) = $searcharea->get_config_var_name();
if (!$anyenabled) {
$anyenabled = get_config($componentname, 'enable' . $varname);
$anyenabled = get_config($componentname, $varname . '_enabled');
}
if (!$anyindexed) {
$anyindexed = get_config($componentname, $varname . '_indexingstart');
Expand Down Expand Up @@ -9383,7 +9387,7 @@ public function output_html($data, $query='') {

// Available areas.
$row = array();
$url = new moodle_url('/admin/settings.php?section=manageglobalsearch#id_s_mod_assign_enablesearch_activity');
$url = new moodle_url('/admin/settings.php?section=manageglobalsearch#admin-searchengine');
$row[0] = '3. ' . html_writer::tag('a', get_string('enablesearchareas', 'admin'),
array('href' => $url));

Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/search-input.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/amd/src/search-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ define(['jquery'], function($) {

var windowWidth = $(document).width();

// We are only interested in enter and space keys (accessibility).
if (ev.type === 'keydown' && ev.keyCode !== 13 && ev.keyCode !== 32) {
return;
}

if (windowWidth <= 767 && (ev.type === 'click' || ev.type === 'keydown')) {
// Move to the search page when using small window sizes as the input requires too much space.
submitForm();
Expand Down
15 changes: 15 additions & 0 deletions lib/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -2057,5 +2057,20 @@
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
)
),

// Perform site-wide search queries through the search API.
'moodle/search:query' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'guest' => CAP_ALLOW,
'user' => CAP_ALLOW,
'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
)

);
21 changes: 0 additions & 21 deletions lib/editorlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,6 @@ function editors_head_setup() {
}
}

/**
* Converts editor text input in different formats to plain text.
*
* @param string $content The text as entered by the user
* @param int $contentformat FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN or FORMAT_MARKDOWN
* @return string Plain text.
*/
function editor_input_to_text($content, $contentformat) {

switch ($contentformat) {
case FORMAT_PLAIN:
return $content;
case FORMAT_MARKDOWN:
$html = markdown_to_html($content);
return html_to_text($html, 75, false);
default:
// FORMAT_HTML and FORMAT_MOODLE.
return html_to_text($content, 75, false);
}
}

/**
* Base abstract text editor class.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2644,7 +2644,7 @@ public function add_front_page_course_essentials(navigation_node $coursenode, st
}

// Search.
if (!empty($CFG->enableglobalsearch)) {
if (!empty($CFG->enableglobalsearch) && has_capability('moodle/search:query', $sitecontext)) {
$node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'),
self::TYPE_SETTING, null, 'search');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3133,7 +3133,7 @@ public function search_box($id = false) {
// Accessing $CFG directly as using \core_search::is_global_search_enabled would
// result in an extra included file for each site, even the ones where global search
// is disabled.
if (empty($CFG->enableglobalsearch)) {
if (empty($CFG->enableglobalsearch) || !has_capability('moodle/search:query', context_system::instance())) {
return '';
}

Expand All @@ -3153,7 +3153,7 @@ public function search_box($id = false) {
$inputattrs = array('type' => 'text', 'name' => 'q', 'placeholder' => get_string('search', 'search'),
'size' => 13, 'tabindex' => -1, 'id' => 'id_q_' . $id);

$contents = html_writer::tag('label', get_string('search', 'search'),
$contents = html_writer::tag('label', get_string('enteryoursearchquery', 'search'),
array('for' => 'id_q_' . $id, 'class' => 'accesshide')) . html_writer::tag('input', '', $inputattrs);
$searchinput = html_writer::tag('form', $contents, $formattrs);

Expand Down
21 changes: 21 additions & 0 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,27 @@ function html_to_text($html, $width = 75, $dolinks = true) {
return $result;
}

/**
* Converts content introduced in an editor to plain text.
*
* @param string $content The text as entered by the user
* @param int $contentformat The text format: FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN or FORMAT_MARKDOWN
* @return string Plain text.
*/
function content_to_text($content, $contentformat) {

switch ($contentformat) {
case FORMAT_PLAIN:
return $content;
case FORMAT_MARKDOWN:
$html = markdown_to_html($content);
return html_to_text($html, 75, false);
default:
// FORMAT_HTML and FORMAT_MOODLE.
return html_to_text($content, 75, false);
}
}

/**
* This function will highlight search words in a given string
*
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/classes/search/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function get_document($record) {
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', $record->subject);
$doc->set('content', editor_input_to_text($record->message, $record->messageformat));
$doc->set('content', content_to_text($record->message, $record->messageformat));
$doc->set('contextid', $context->id);
$doc->set('type', \core_search\manager::TYPE_TEXT);
$doc->set('courseid', $record->courseid);
Expand Down
4 changes: 2 additions & 2 deletions mod/forum/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function test_search_enabled() {
// Enabled by default once global search is enabled.
$this->assertTrue($searcharea->is_enabled());

set_config('enable' . $varname, false, $componentname);
set_config($varname . '_enabled', false, $componentname);
$this->assertFalse($searcharea->is_enabled());

set_config('enable' . $varname, true, $componentname);
set_config($varname . '_enabled', true, $componentname);
$this->assertTrue($searcharea->is_enabled());
}

Expand Down
2 changes: 1 addition & 1 deletion mod/glossary/classes/search/entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function get_document($entry) {
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($entry->id, $this->componentname, $this->areaname);
$doc->set('title', $entry->concept);
$doc->set('content', editor_input_to_text($entry->definition, $entry->definitionformat));
$doc->set('content', content_to_text($entry->definition, $entry->definitionformat));
$doc->set('contextid', $context->id);
$doc->set('type', \core_search\manager::TYPE_TEXT);
$doc->set('courseid', $entry->course);
Expand Down
4 changes: 2 additions & 2 deletions mod/glossary/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public function test_search_enabled() {
// Enabled by default once global search is enabled.
$this->assertTrue($searcharea->is_enabled());

set_config('enable' . $varname, false, $componentname);
set_config($varname . '_enabled', false, $componentname);
$this->assertFalse($searcharea->is_enabled());

set_config('enable' . $varname, true, $componentname);
set_config($varname . '_enabled', true, $componentname);
$this->assertTrue($searcharea->is_enabled());
}

Expand Down
4 changes: 2 additions & 2 deletions mod/page/classes/search/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public function get_document($record) {
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', $record->name);
$doc->set('content', editor_input_to_text($record->content, $record->contentformat));
$doc->set('content', content_to_text($record->content, $record->contentformat));
$doc->set('contextid', $context->id);
$doc->set('type', \core_search\manager::TYPE_TEXT);
$doc->set('courseid', $record->course);
$doc->set('modified', $record->timemodified);
$doc->set('description1', editor_input_to_text($record->intro, $record->introformat));
$doc->set('description1', content_to_text($record->intro, $record->introformat));

return $doc;
}
Expand Down
22 changes: 20 additions & 2 deletions search/classes/area/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function get_visible_name($lazyload = false) {
*
* It depends on whether it is a moodle subsystem or a plugin as plugin-related config should remain in their own scope.
*
* @access private
* @return string Config var path including the plugin (or component) and the varname
*/
public function get_config_var_name() {
Expand All @@ -157,14 +158,31 @@ public function get_config_var_name() {
return array($this->componentname, 'search_' . $this->areaname);
}

/**
* Returns all the search area configuration.
*
* @return array
*/
public function get_config() {
list($componentname, $varname) = $this->get_config_var_name();

$config = [];
$settingnames = array('_enabled', '_indexingstart', '_indexingend', '_lastindexrun', '_docsignored', '_docsprocessed', '_recordsprocessed');
foreach ($settingnames as $name) {
$config[$varname . $name] = get_config($componentname, $varname . $name);
}

return $config;
}

/**
* Is the search component enabled by the system administrator?
*
* @return bool
*/
public function is_enabled() {
list($componentname, $varname) = $this->get_config_var_name();
return (bool)get_config($componentname, 'enable' . $varname);
return (bool)get_config($componentname, $varname . '_enabled');
}

/**
Expand Down Expand Up @@ -193,7 +211,7 @@ abstract public function get_recordset_by_timestamp($modifiedfrom = 0);
* Internally it should use \core_search\document to standarise the documents before sending them to the search engine.
*
* Search areas should send plain text to the search engine, use the following function to convert any user
* input data to plain text: {@link editor_input_to_text}
* input data to plain text: {@link content_to_text}
*
* @param \stdClass $record A record containing, at least, the indexed document id and a modified timestamp
* @return \core_search\document
Expand Down
2 changes: 1 addition & 1 deletion search/classes/area/base_activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get_document($record) {
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', $record->name);
$doc->set('content', editor_input_to_text($record->intro, $record->introformat));
$doc->set('content', content_to_text($record->intro, $record->introformat));
$doc->set('contextid', $context->id);
$doc->set('type', \core_search\manager::TYPE_TEXT);
$doc->set('courseid', $record->course);
Expand Down
3 changes: 2 additions & 1 deletion search/classes/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ public function set($fieldname, $value) {
if ($fielddata['type'] === 'int' || $fielddata['type'] === 'tdate') {
$this->data[$fieldname] = intval($value);
} else {
$this->data[$fieldname] = trim($value, "\r\n");
// Clean up line breaks and extra spaces.
$this->data[$fieldname] = preg_replace("/\s+/", ' ', trim($value, "\r\n"));
}

return $this->data[$fieldname];
Expand Down
3 changes: 3 additions & 0 deletions search/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ abstract function commit();
/**
* Executes the query on the engine.
*
* Implementations of this function should check user context array to limit the results to contexts where the
* user have access.
*
* @param stdClass $filters Query and filters to apply.
* @param array $usercontexts Contexts where the user has access. True if the user can access all contexts.
* @return \core_search\document[] Results or false if no results
Expand Down
23 changes: 12 additions & 11 deletions search/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ protected function get_areas_user_accesses() {

// Get the courses where the current user has access.
$courses = enrol_get_my_courses(array('id', 'cacherev'));
if (isloggedin() || (isguestuser() && !$CFG->forcelogin)) {
$courses[SITEID] = get_course(SITEID);
}
$courses[SITEID] = get_course(SITEID);
$site = \course_modinfo::instance(SITEID);
foreach ($courses as $course) {

Expand Down Expand Up @@ -391,6 +389,9 @@ protected function get_areas_user_accesses() {
/**
* Returns documents from the engine based on the data provided.
*
* This function does not perform any kind of security checking, the caller code
* should check that the current user have moodle/search:query capability.
*
* It might return the results from the cache instead.
*
* @param stdClass $formdata
Expand Down Expand Up @@ -579,15 +580,15 @@ public function reset_config($areaid = false) {
}

foreach ($searchareas as $searcharea) {
list($componentname, $varname) = $searcharea->get_config_var_name();
$config = $searcharea->get_config();

list($areaid, $varname) = $searcharea->get_config_var_name();

set_config($varname . '_indexingstart', 0, $areaid);
set_config($varname . '_indexingend', 0, $areaid);
set_config($varname . '_lastindexrun', 0, $areaid);
set_config($varname . '_docsignored', 0, $areaid);
set_config($varname . '_docsprocessed', 0, $areaid);
set_config($varname . '_recordsprocessed', 0, $areaid);
foreach ($config as $key => $value) {
// We reset them all but the enable/disabled one.
if ($key !== $varname . '_enabled') {
set_config($key, 0, $componentname);
}
}
}
}

Expand Down
Loading

0 comments on commit 69d6602

Please sign in to comment.