Moodle APIs 3.9
Moodle 3.9.13+ (Build: 20220325)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
core_search\base_mod Class Reference

Base implementation for search areas working at module level. More...

Inheritance diagram for core_search\base_mod:
core_search\base core_search\base_activity mod_book\search\chapter mod_data\search\entry mod_forum\search\post mod_glossary\search\entry mod_wiki\search\collaborative_page mod_assign\search\activity mod_book\search\activity mod_chat\search\activity mod_choice\search\activity mod_data\search\activity mod_feedback\search\activity mod_folder\search\activity mod_forum\search\activity mod_glossary\search\activity mod_imscp\search\activity mod_label\search\activity mod_lesson\search\activity mod_lti\search\activity mod_page\search\activity mod_quiz\search\activity mod_resource\search\activity mod_scorm\search\activity mod_survey\search\activity mod_url\search\activity mod_wiki\search\activity mod_workshop\search\activity

Public Member Functions

 attach_files ($document)
 Files related to the current document are attached, to the document object ready for indexing by Global Search. More...
 
 check_access ($id)
 Can the current user see the document. More...
 
 get_area_id ()
 Returns the area id. More...
 
 get_category_names ()
 Returns a list of category names associated with the area. More...
 
 get_component_name ()
 Returns the moodle component name. More...
 
 get_component_type ()
 Returns the component type. More...
 
 get_config ()
 Returns all the search area configuration. More...
 
 get_config_var_name ()
 Returns the config var name. More...
 
 get_context_url (\core_search\document $doc)
 Returns a url to the document context. More...
 
 get_contexts_to_reindex ()
 Gets a list of all contexts to reindex when reindexing this search area. More...
 
 get_doc_icon (document $doc)
 Returns an icon instance for the document. More...
 
 get_doc_url (\core_search\document $doc)
 Returns a url to the document, it might match self::get_context_url(). More...
 
 get_document ($record, $options=array())
 Returns the document related with the provided record. More...
 
 get_document_display_title (\core_search\document $doc)
 Returns the document title to display. More...
 
 get_document_recordset ($modifiedfrom=0, context $context=null)
 Returns a recordset containing all items from this area, optionally within the given context, and including only items modifed from (>=) the specified time. More...
 
 get_last_indexing_duration ()
 Gets the length of time spent indexing this area (the last time it was indexed). More...
 
 get_recordset_by_timestamp ($modifiedfrom=0)
 Returns a recordset ordered by modification date ASC. More...
 
 get_search_fileareas ()
 Return the context info required to index files for this search area. More...
 
 get_visible_name ($lazyload=false)
 Returns the area visible name. More...
 
 is_enabled ()
 Is the search component enabled by the system administrator? More...
 
 restrict_cm_access_by_group (\cm_info $cm)
 Checks whether the content of this search area should be restricted by group for a specific module. More...
 
 set_enabled ($isenabled)
 
 supports_get_document_recordset ()
 Checks if get_document_recordset is supported for this search area. More...
 
 supports_group_restriction ()
 Indicates whether this search area may restrict access by group. More...
 
 uses_file_indexing ()
 Returns true if this area uses file indexing. More...
 

Static Public Member Functions

static get_levels ()
 Returns context levels property. More...
 
static get_settingnames ()
 Return a list of all required setting names. More...
 

Protected Member Functions

 get_cm ($modulename, $instanceid, $courseid)
 Gets the course module for the required instanceid + modulename. More...
 
 get_context_restriction_sql (\context $context=null, $modname, $modtable, $paramtype=SQL_PARAMS_QM)
 Helper function that gets SQL useful for restricting a search query given a passed-in context. More...
 
 get_contexts_to_reindex_extra_sql ()
 This can be used in subclasses to change ordering within the get_contexts_to_reindex function. More...
 
 get_course_level_context_restriction_sql (\context $context=null, $coursetable, $paramtype=SQL_PARAMS_QM)
 Helper function that gets SQL useful for restricting a search query given a passed-in context, for data stored at course level. More...
 
 get_module_name ()
 Returns the module name. More...
 

Protected Attributes

string $areaname = null
 The area name as defined in the class name.
 
string $componentname = null
 The component frankenstyle name.
 
string $componenttype = null
 The component type (core or the plugin type).
 

Static Protected Attributes

static array $levels = [CONTEXT_MODULE]
 The context levels the search area is working on. More...
 

Detailed Description

Base implementation for search areas working at module level.

Even if the search area works at multiple levels, if module is one of these levels it should extend this class, as this class provides helper methods for module level search management.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Member Function Documentation

◆ attach_files()

core_search\base::attach_files (   $document)
inherited

Files related to the current document are attached, to the document object ready for indexing by Global Search.

The default implementation retrieves all files for the file areas returned by get_search_fileareas(). If you need to filter files to specific items per file area, you will need to override this method and explicitly provide the items.

Parameters
document$documentThe current document
Return values
void

Reimplemented in mod_data\search\entry, block_html\search\content, mod_forum\search\post, and core_search\base_activity.

◆ check_access()

core_search\base::check_access (   $id)
abstractinherited

◆ get_area_id()

core_search\base::get_area_id ( )
inherited

Returns the area id.

Return values
string

◆ get_category_names()

core_search\base_mod::get_category_names ( )

Returns a list of category names associated with the area.

Return values
array

Reimplemented from core_search\base.

◆ get_cm()

core_search\base_mod::get_cm (   $modulename,
  $instanceid,
  $courseid 
)
protected

Gets the course module for the required instanceid + modulename.

The returned data depends on the logged user, when calling this through self::get_document the admin user is used so everything would be returned.

No need more internal caching here, modinfo is already cached.

Exceptions
dml_missing_record_exception
Parameters
string$modulenameThe module name
int$instanceidModule instance id (depends on the module)
int$courseidHelps speeding up things
Return values
cm_info

◆ get_component_name()

core_search\base::get_component_name ( )
inherited

Returns the moodle component name.

It might be the plugin name (whole frankenstyle name) or the core subsystem name.

Return values
string

Reimplemented in core_course\search\course, core_course\search\customfield, core_course\search\section, and core_user\search\user.

◆ get_component_type()

core_search\base::get_component_type ( )
inherited

Returns the component type.

It might be a plugintype or 'core' for core subsystems.

Return values
string

◆ get_config()

core_search\base::get_config ( )
inherited

Returns all the search area configuration.

Return values
array

◆ get_config_var_name()

core_search\base::get_config_var_name ( )
inherited

Returns the config var name.

It depends on whether it is a moodle subsystem or a plugin as plugin-related config should remain in their own scope.

Return values
stringConfig var path including the plugin (or component) and the varname

◆ get_context_restriction_sql()

core_search\base_mod::get_context_restriction_sql ( \context  $context = null,
  $modname,
  $modtable,
  $paramtype = SQL_PARAMS_QM 
)
protected

Helper function that gets SQL useful for restricting a search query given a passed-in context.

The SQL returned will be zero or more JOIN statements, surrounded by whitespace, which act as restrictions on the query based on the rows in a module table.

You can pass in a null or system context, which will both return an empty string and no params.

Returns an array with two nulls if there can be no results for the activity within this context (e.g. it is a block context).

If named parameters are used, these will be named gcrs0, gcrs1, etc. The table aliases used in SQL also all begin with gcrs, to avoid conflicts.

Parameters
context | null$contextContext to restrict the query
string$modnameName of module e.g. 'forum'
string$modtableAlias of table containing module id
int$paramtypeType of SQL parameters to use (default question mark)
Return values
arrayArray with SQL and parameters; both null if no need to query
Exceptions
coding_exceptionIf called with invalid params

◆ get_context_url()

core_search\base::get_context_url ( \core_search\document  $doc)
abstractinherited

Returns a url to the document context.

Parameters
core_search::document$doc
Return values
moodle_url

Reimplemented in core_search\base_activity, and core_search\base_block.

◆ get_contexts_to_reindex()

core_search\base_mod::get_contexts_to_reindex ( )

Gets a list of all contexts to reindex when reindexing this search area.

For modules, the default is to return all contexts for modules of that type, in order of time added (most recent first).

Return values
IteratorIterator of contexts to reindex
Exceptions
moodle_exceptionIf any DB error

Reimplemented from core_search\base.

◆ get_contexts_to_reindex_extra_sql()

core_search\base_mod::get_contexts_to_reindex_extra_sql ( )
protected

This can be used in subclasses to change ordering within the get_contexts_to_reindex function.

It returns 2 values:

  • Extra SQL joins (tables course_modules 'cm' and context 'x' already exist).
  • An ORDER BY value which must use aggregate functions, by default 'MAX(cm.added) DESC'.

Note the query already includes a GROUP BY on the context fields, so if your joins result in multiple rows, you can use aggregate functions in the ORDER BY. See forum for an example.

Return values
string[]Array with 2 elements; extra joins for the query, and ORDER BY value

Reimplemented in mod_forum\search\post.

◆ get_course_level_context_restriction_sql()

core_search\base::get_course_level_context_restriction_sql ( \context  $context = null,
  $coursetable,
  $paramtype = SQL_PARAMS_QM 
)
protectedinherited

Helper function that gets SQL useful for restricting a search query given a passed-in context, for data stored at course level.

The SQL returned will be zero or more JOIN statements, surrounded by whitespace, which act as restrictions on the query based on the rows in a module table.

You can pass in a null or system context, which will both return an empty string and no params.

Returns an array with two nulls if there can be no results for a course within this context.

If named parameters are used, these will be named gclcrs0, gclcrs1, etc. The table aliases used in SQL also all begin with gclcrs, to avoid conflicts.

Parameters
context | null$contextContext to restrict the query
string$coursetableName of alias for course table e.g. 'c'
int$paramtypeType of SQL parameters to use (default question mark)
Return values
arrayArray with SQL and parameters; both null if no need to query
Exceptions
coding_exceptionIf called with invalid params

◆ get_doc_icon()

core_search\base_mod::get_doc_icon ( document  $doc)

Returns an icon instance for the document.

Parameters
core_search::document$doc
Return values
core_search::document_icon

Reimplemented from core_search\base.

◆ get_doc_url()

core_search\base::get_doc_url ( \core_search\document  $doc)
abstractinherited

Returns a url to the document, it might match self::get_context_url().

Parameters
core_search::document$doc
Return values
moodle_url

Reimplemented in core_search\base_activity, and core_search\base_block.

◆ get_document()

core_search\base::get_document (   $record,
  $options = array() 
)
abstractinherited

Returns the document related with the provided record.

This method receives a record with the document id and other info returned by get_recordset_by_timestamp or get_recordset_by_contexts that might be useful here. The idea is to restrict database queries to minimum as this function will be called for each document to index. As an alternative, use cached data.

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: content_to_text

Valid keys for the options array are: indexfiles => File indexing is enabled if true. lastindexedtime => The last time this area was indexed. 0 if never indexed.

The lastindexedtime value is not set if indexing a specific context rather than the whole system.

Parameters
stdClass$recordA record containing, at least, the indexed document id and a modified timestamp
array$optionsOptions for document creation
Return values
core_search::document

Reimplemented in mod_data\search\entry, mod_glossary\search\entry, block_html\search\content, core_course\search\course, core_course\search\customfield, core_course\search\section, core_message\search\base_message, core_message\search\message_received, core_message\search\message_sent, mod_book\search\chapter, mod_forum\search\post, mod_page\search\activity, mod_url\search\activity, mod_wiki\search\collaborative_page, core_search\base_activity, and core_user\search\user.

◆ get_document_display_title()

core_search\base::get_document_display_title ( \core_search\document  $doc)
inherited

Returns the document title to display.

Allow to customize the document title string to display.

Parameters
core_search::document$doc
Return values
stringDocument title to display in the search results page

◆ get_document_recordset()

core_search\base::get_document_recordset (   $modifiedfrom = 0,
context  $context = null 
)
inherited

Returns a recordset containing all items from this area, optionally within the given context, and including only items modifed from (>=) the specified time.

The recordset must be ordered in ascending order of modified time.

Each record can include any data self::get_document might need. It must include an 'id' field,a unique identifier (in this area's scope) of a document to index in the search engine. If the indexed content field can contain embedded files, the 'id' value should match the filearea itemid.

The return value can be a recordset, null (if this area does not provide any results in the given context and there is no need to do a database query to find out), or false (if this facility is not currently supported by this search area).

If this function returns false, then:

  • If indexing the entire system (no context restriction) the search indexer will try get_recordset_by_timestamp instead
  • If trying to index a context (e.g. when restoring a course), the search indexer will not index this area, so that restored content may not be indexed.

The default implementation returns false, indicating that this facility is not supported and the older get_recordset_by_timestamp function should be used.

This function must accept all possible values for the $context parameter. For example, if you are implementing this function for the forum module, it should still operate correctly if called with the context for a glossary module, or for the HTML block. (In these cases where it will not return any data, it may return null.)

The $context parameter can also be null or the system context; both of these indicate that all data, without context restriction, should be returned.

Parameters
int$modifiedfromReturn only records modified after this date
context | null$contextContext (null means no context restriction)
Return values
moodle_recordset|null|falseRecordset / null if no results / false if not supported
Since
Moodle 3.4

Reimplemented in core_course\search\course, core_course\search\customfield, core_course\search\section, core_message\search\message_received, core_message\search\message_sent, mod_book\search\chapter, mod_data\search\entry, mod_forum\search\post, mod_glossary\search\entry, mod_survey\search\activity, mod_wiki\search\collaborative_page, core_search\base_activity, core_search\base_block, and core_user\search\user.

◆ get_last_indexing_duration()

core_search\base::get_last_indexing_duration ( )
inherited

Gets the length of time spent indexing this area (the last time it was indexed).

Return values
int|boolTime in seconds spent indexing this area last time, false if never indexed

◆ get_levels()

static core_search\base::get_levels ( )
staticinherited

Returns context levels property.

Return values
int

◆ get_module_name()

core_search\base_mod::get_module_name ( )
protected

Returns the module name.

Return values
string

◆ get_recordset_by_timestamp()

core_search\base::get_recordset_by_timestamp (   $modifiedfrom = 0)
inherited

Returns a recordset ordered by modification date ASC.

Each record can include any data self::get_document might need but it must:

  • Include an 'id' field: Unique identifier (in this area's scope) of a document to index in the search engine If the indexed content field can contain embedded files, the 'id' value should match the filearea itemid.
  • Only return data modified since $modifiedfrom, including $modifiedform to prevent some records from not being indexed (e.g. your-timemodified-fieldname >= $modifiedfrom)
  • Order the returned data by time modified in ascending order, as core_search::manager will need to store the modified time of the last indexed document.

Since Moodle 3.4, subclasses should instead implement get_document_recordset, which has an additional context parameter. This function continues to work for implementations which haven't been updated, or where the context parameter is not required.

Parameters
int$modifiedfrom
Return values
moodle_recordset

◆ get_search_fileareas()

core_search\base::get_search_fileareas ( )
inherited

◆ get_settingnames()

static core_search\base::get_settingnames ( )
staticinherited

Return a list of all required setting names.

Return values
array

◆ get_visible_name()

core_search\base::get_visible_name (   $lazyload = false)
inherited

Returns the area visible name.

Parameters
bool$lazyloadUsually false, unless when in admin settings.
Return values
string

◆ is_enabled()

core_search\base::is_enabled ( )
inherited

Is the search component enabled by the system administrator?

Return values
bool

◆ restrict_cm_access_by_group()

core_search\base_mod::restrict_cm_access_by_group ( \cm_info  $cm)

Checks whether the content of this search area should be restricted by group for a specific module.

Called at query time.

The default behaviour simply checks if the effective group mode is SEPARATEGROUPS, which is probably correct for most cases.

If restricted by group, the search query will (where supported by the engine) filter out results for groups the user does not belong to, unless the user has 'access all groups' for the activity. This affects only documents which set the 'groupid' field; results with no groupid will not be restricted.

Even if you return true to this function, you may still need to do group access checks in check_access, because the search engine may not support group restrictions.

Parameters
cm_info$cm
Return values
boolTrue to restrict by group

◆ supports_get_document_recordset()

core_search\base::supports_get_document_recordset ( )
inherited

Checks if get_document_recordset is supported for this search area.

For many uses you can simply call get_document_recordset and see if it returns false, but this function is useful when you don't want to actually call the function right away.

◆ supports_group_restriction()

core_search\base_mod::supports_group_restriction ( )

Indicates whether this search area may restrict access by group.

This should return true if the search area (sometimes) sets the 'groupid' schema field, and false if it never sets that field.

(If this function returns false, but the field is set, then results may be restricted unintentionally.)

If this returns true, the search engine will automatically apply group restrictions in some cases (by default, where a module is configured to use separate groups). See function restrict_cm_access_by_group().

Return values
bool

Reimplemented in mod_data\search\entry, mod_forum\search\post, and mod_wiki\search\collaborative_page.

◆ uses_file_indexing()

core_search\base::uses_file_indexing ( )
inherited

Member Data Documentation

◆ $levels

array core_search\base_mod::$levels = [CONTEXT_MODULE]
staticprotected

The context levels the search area is working on.

This can be overwriten by the search area if it works at multiple levels.


The documentation for this class was generated from the following file: