Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
core_tag_index_builder Class Reference

Helper to build tag index. More...

Public Member Functions

 __construct ($component, $itemtype, $sql, $params, $from, $limit)
 Constructor. More...
 
 can_access_course ($courseid)
 Checks if current user has access to the course. More...
 
 get_course ($courseid)
 Retrieves a course record (only fields id,visible,fullname,shortname,cacherev). More...
 
 get_items ()
 Returns the filtered records from SQL query result. More...
 
 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. More...
 
 set_accessible ($identifier, $accessible=true)
 Marks record or group of records as accessible (or not accessible) More...
 
 walk ($callable)
 Walk through the array of items and call $callable for each of them. More...
 

Static Public Member Functions

static reset_caches ()
 Resets all course/items session caches - useful in unittests when we change users and enrolments.
 

Protected Member Functions

 init_course_access ()
 Ensures that we read the course access from the cache.
 
 init_items_access ()
 Ensures that we read the items access from the cache.
 
 prepare_sql_courses ()
 Substitute COURSEFILTER% with an expression filtering out courses where current user does not have access.
 
 prepare_sql_items ()
 Substitute ITEMFILTER% with an expression filtering out items where current user does not have access.
 
 retrieve_items ()
 Ensures that SQL query was executed and $this->items is filled.
 
 save_caches ()
 Saves course/items caches if needed.
 

Protected Attributes

array $accessibleitems
 cache of items accessibility (id => bool)
 
bool $cachechangedaccessible = false
 indicates that items cache was changed in this class and needs pushing to MUC
 
bool $cachechangedcourse = false
 indicates that course accessibiity cache was changed in this class and needs pushing to MUC
 
string $component
 component specified in the constructor
 
array $courseaccess
 cache of courses accessibility (courseid => bool)
 
array $courses
 cached courses (not pushed to MUC)
 
int $from
 index from which to return records
 
string $idfield = 'id'
 alias of the item id in the SQL result
 
array $itemkeys
 list of item ids ( array_keys($this->items) )
 
array $items
 result of SQL query
 
string $itemtype
 itemtype specified in the constructor
 
int $limit
 maximum number of records to return
 
array $params
 parameters for SQL statement
 
string $sql
 SQL statement.
 

Detailed Description

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();

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

Constructor & Destructor Documentation

◆ __construct()

core_tag_index_builder::__construct (   $component,
  $itemtype,
  $sql,
  $params,
  $from,
  $limit 
)

Constructor.

Specify the SQL query for retrieving the tagged items, SQL query must:

  • return the item id as the first field and make sure that it is unique in the result
  • provide ORDER BY that exclude any possibility of random results, if $fromctx was specified when searching for tagged items it is the best practice to make sure that items from this context are returned first.

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.

Parameters
string$componentcomponent responsible for tagging
string$itemtypetype of item that is being tagged
string$sqlSQL query that would retrieve all relevant items without permission check
array$paramsparameters for the query (must be named)
int$fromreturn a subset of records, starting at this point
int$limitreturn a subset comprising this many records in total (this field is NOT optional)

Member Function Documentation

◆ can_access_course()

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

Parameters
int$courseid
Return values
bool

◆ get_course()

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.

Parameters
int$courseid

◆ get_items()

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

Return values
array

◆ has_item_that_needs_access_check()

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: } and walk()

Return values
null|object

◆ set_accessible()

core_tag_index_builder::set_accessible (   $identifier,
  $accessible = true 
)

Marks record or group of records as accessible (or not accessible)

Parameters
int | std_Class$identifiereither record id of the item that needs to be set accessible
bool$accessiblewhether to mark as accessible or not accessible (default true)

◆ walk()

core_tag_index_builder::walk (   $callable)

Walk through the array of items and call $callable for each of them.

Parameters
callable$callable

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