Exemplo n.º 1
0
/**
 * Returns the entries of a glossary by author.
 *
 * @param  object $glossary The glossary.
 * @param  context $context The context of the glossary.
 * @param  string $letter The letter
 * @param  string $field The field to search: FIRSTNAME or LASTNAME.
 * @param  string $sort The sorting: ASC or DESC.
 * @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_author($glossary, $context, $letter, $field, $sort, $from, $limit, $options = array())
{
    $firstnamefirst = $field === 'FIRSTNAME';
    $qb = new mod_glossary_entry_query_builder($glossary);
    if ($letter != 'ALL' && $letter != 'SPECIAL' && core_text::strlen($letter)) {
        $qb->filter_by_author_letter($letter, $firstnamefirst);
    }
    if ($letter == 'SPECIAL') {
        $qb->filter_by_author_non_letter($firstnamefirst);
    }
    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->add_field('*', 'entries');
    $qb->join_user(true);
    $qb->add_user_fields();
    $qb->order_by_author($firstnamefirst, $sort);
    $qb->order_by('concept', 'entries');
    $qb->order_by('id', 'entries', 'ASC');
    // Sort on ID to avoid random ordering when entries share an ordering value.
    $qb->limit($from, $limit);
    // Fetching the entries.
    $count = $qb->count_records();
    $entries = $qb->get_records();
    return array($entries, $count);
}