Moodle PHP Documentation 4.4
Moodle 4.4.1 (Build: 20240610) (db07c09afc5)
|
Helper to build tag index. More...
Public Member Functions | |
__construct ($component, $itemtype, $sql, $params, $from, $limit) | |
Constructor. | |
can_access_course ($courseid) | |
Checks if current user has access to the course. | |
get_course ($courseid) | |
Retrieves a course record (only fields id,visible,fullname,shortname,cacherev). | |
get_items () | |
Returns the filtered records from SQL query result. | |
has_item_that_needs_access_check () | |
Returns the first row from the SQL result that we don't know whether it is accessible by user or not. | |
set_accessible ($identifier, $accessible=true) | |
Marks record or group of records as accessible (or not accessible) | |
walk ($callable) | |
Walk through the array of items and call $callable for each of them. | |
Static Public Member Functions | |
static | reset_caches () |
Resets all course/items session caches - useful in unittests when we change users and enrolments. | |
Helper to build tag index.
This can be used by components to implement tag area callbacks. This is especially useful for in-course content when we need to check and cache user's access to multiple courses. Course access and accessible items are stored in session cache with 15 minutes expiry time.
Example of usage:
$builder = new core_tag_index_builder($component, $itemtype, $sql, $params, $from, $limit); while ($item = $builder->has_item_that_needs_access_check()) { if (!$builder->can_access_course($item->courseid)) { $builder->set_accessible($item, false); } else { $accessible = true; // Check access and set $accessible respectively. $builder->set_accessible($item, $accessible); } } $items = $builder->get_items();
core_tag_index_builder::__construct | ( | $component, | |
$itemtype, | |||
$sql, | |||
$params, | |||
$from, | |||
$limit ) |
Constructor.
Specify the SQL query for retrieving the tagged items, SQL query must:
This query may also contain placeholders COURSEFILTER% or ITEMFILTER% that will be substituted with expressions excluding courses and/or filters that are already known as inaccessible.
Example: "WHERE c.id %COURSEFILTER% AND cm.id %ITEMFILTER%"
This query may contain fields to preload context if context is needed for formatting values.
It is recommended to sort by course sortorder first, this way the items from the same course will be next to each other and the sequence of courses will the same in different tag areas.
string | $component | component responsible for tagging |
string | $itemtype | type of item that is being tagged |
string | $sql | SQL query that would retrieve all relevant items without permission check |
array | $params | parameters for the query (must be named) |
int | $from | return a subset of records, starting at this point |
int | $limit | return a subset comprising this many records in total (this field is NOT optional) |
core_tag_index_builder::can_access_course | ( | $courseid | ) |
Checks if current user has access to the course.
This method calls global function can_access_course and caches results
int | $courseid |
bool |
core_tag_index_builder::get_course | ( | $courseid | ) |
Retrieves a course record (only fields id,visible,fullname,shortname,cacherev).
This method is useful because it also caches results and preloads course context.
int | $courseid |
core_tag_index_builder::get_items | ( | ) |
Returns the filtered records from SQL query result.
This function can only be executed after $builder->has_item_that_needs_access_check() returns null
array |
core_tag_index_builder::has_item_that_needs_access_check | ( | ) |
Returns the first row from the SQL result that we don't know whether it is accessible by user or not.
This will return null when we have necessary number of accessible items to return in get_items()
After analyzing you may decide to mark not only this record but all similar as accessible or not accessible. For example, if you already call get_fast_modinfo() to check this item's accessibility, why not mark all items in the same course as accessible or not accessible.
Helpful methods: set_accessible() and walk()
null|object |
core_tag_index_builder::set_accessible | ( | $identifier, | |
$accessible = true ) |
Marks record or group of records as accessible (or not accessible)
int | std_Class | $identifier | either record id of the item that needs to be set accessible |
bool | $accessible | whether to mark as accessible or not accessible (default true) |
core_tag_index_builder::walk | ( | $callable | ) |
Walk through the array of items and call $callable for each of them.
callable | $callable |