Exemplo n.º 1
0
/**
 * Returns the entries of a glossary by category.
 *
 * @param  object $glossary The glossary.
 * @param  context $context The context of the glossary.
 * @param  int $categoryid The category ID, or GLOSSARY_SHOW_* constant.
 * @param  int $from Fetch records from.
 * @param  int $limit Number of records to fetch.
 * @param  array $options Accepts:
 *                        - (bool) includenotapproved. When false, includes the non-approved entries created by
 *                          the current user. When true, also includes the ones that the user has the permission to approve.
 * @return array The first element being the recordset, the second the number of entries.
 * @since Moodle 3.1
 */
function glossary_get_entries_by_category($glossary, $context, $categoryid, $from, $limit, $options = array())
{
    $qb = new mod_glossary_entry_query_builder($glossary);
    if (!empty($options['includenotapproved']) && has_capability('mod/glossary:approve', $context)) {
        $qb->filter_by_non_approved(mod_glossary_entry_query_builder::NON_APPROVED_ALL);
    } else {
        $qb->filter_by_non_approved(mod_glossary_entry_query_builder::NON_APPROVED_SELF);
    }
    $qb->join_category($categoryid);
    $qb->join_user();
    // The first field must be the relationship ID when viewing all categories.
    if ($categoryid === GLOSSARY_SHOW_ALL_CATEGORIES) {
        $qb->add_field('id', 'entries_categories', 'cid');
    }
    $qb->add_field('*', 'entries');
    $qb->add_field('categoryid', 'entries_categories');
    $qb->add_user_fields();
    if ($categoryid === GLOSSARY_SHOW_ALL_CATEGORIES) {
        $qb->add_field('name', 'categories', 'categoryname');
        $qb->order_by('name', 'categories');
    } else {
        if ($categoryid === GLOSSARY_SHOW_NOT_CATEGORISED) {
            $qb->where('categoryid', 'entries_categories', null);
        }
    }
    // Sort on additional fields to avoid random ordering when entries share an ordering value.
    $qb->order_by('concept', 'entries');
    $qb->order_by('id', 'entries', 'ASC');
    $qb->limit($from, $limit);
    // Fetching the entries.
    $count = $qb->count_records();
    $entries = $qb->get_records();
    return array($entries, $count);
}