Skip to content

Commit

Permalink
MDL-49231 mod_glossary: External function get_entries_by_date
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart authored and jleyva committed Dec 31, 2015
1 parent fe11f9a commit 6a273d5
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 0 deletions.
131 changes: 131 additions & 0 deletions mod/glossary/classes/external.php
Expand Up @@ -531,4 +531,135 @@ public static function get_entries_by_letter_returns() {
'warnings' => new external_warnings()
));
}

/**
* Returns the description of the external function parameters.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_entries_by_date_parameters() {
return new external_function_parameters(array(
'id' => new external_value(PARAM_INT, 'Glossary entry ID'),
'order' => new external_value(PARAM_ALPHA, 'Order the records by: \'CREATION\' or \'UPDATE\'.',
VALUE_DEFAULT, 'UPDATE'),
'sort' => new external_value(PARAM_ALPHA, 'The direction of the order: \'ASC\' or \'DESC\'', VALUE_DEFAULT, 'DESC'),
'from' => new external_value(PARAM_INT, 'Start returning records from here', VALUE_DEFAULT, 0),
'limit' => new external_value(PARAM_INT, 'Number of records to return', VALUE_DEFAULT, 20),
'options' => new external_single_structure(array(
'includenotapproved' => new external_value(PARAM_BOOL, 'When false, includes the non-approved entries created by' .
' the user. When true, also includes the ones that the user has the permission to approve.', VALUE_DEFAULT, 0)
), 'An array of options', VALUE_DEFAULT, array())
));
}

/**
* Browse a glossary entries by date.
*
* @param int $id The glossary ID.
* @param string $order The way to order the records.
* @param string $sort The direction of the order.
* @param int $from Start returning records from here.
* @param int $limit Number of records to return.
* @param array $options Array of options.
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_entries_by_date($id, $order = 'UPDATE', $sort = 'DESC', $from = 0, $limit = 20,
$options = array()) {
global $DB, $USER;

$params = self::validate_parameters(self::get_entries_by_date_parameters(), array(
'id' => $id,
'order' => core_text::strtoupper($order),
'sort' => core_text::strtoupper($sort),
'from' => $from,
'limit' => $limit,
'options' => $options,
));
$id = $params['id'];
$order = $params['order'];
$sort = $params['sort'];
$from = $params['from'];
$limit = $params['limit'];
$options = $params['options'];
$warnings = array();

if (!in_array($order, array('CREATION', 'UPDATE'))) {
throw new invalid_parameter_exception('invalidorder');
} else if (!in_array($sort, array('ASC', 'DESC'))) {
throw new invalid_parameter_exception('invalidsort');
}

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
if (!in_array('date', $modes)) {
throw new invalid_parameter_exception('invalidbrowsemode');
}

// Preparing the query.
$params = array();
$approvedsql = '(ge.approved <> 0 OR ge.userid = :myid)';
if (!empty($options['includenotapproved']) && has_capability('mod/glossary:approve', $context)) {
$approvedsql = '1 = 1';
}

$userfields = user_picture::fields('u', null, 'userdataid', 'userdata');

$sqlselectcount = "SELECT COUNT('x')";
$sqlselect = "SELECT ge.*, $userfields";
$sql = " FROM {glossary_entries} ge
LEFT JOIN {user} u ON ge.userid = u.id
WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2)
AND $approvedsql";

$sqlorder = ' ORDER BY ';
if ($order == 'CREATION') {
$sqlorder .= 'ge.timecreated';
} else {
$sqlorder .= 'ge.timemodified';
}
$sqlorder .= ' ' . $sort;

$params['gid1'] = $glossary->id;
$params['gid2'] = $glossary->id;
$params['myid'] = $USER->id;

// Fetching the entries.
$count = $DB->count_records_sql($sqlselectcount . $sql, $params);
$entries = $DB->get_records_sql($sqlselect . $sql . $sqlorder, $params, $from, $limit);
foreach ($entries as $key => $entry) {
self::fill_entry_details($entry, $context);
}

return array(
'count' => $count,
'entries' => $entries,
'warnings' => $warnings
);
}

/**
* Returns the description of the external function return value.
*
* @return external_description
* @since Moodle 3.1
*/
public static function get_entries_by_date_returns() {
return new external_single_structure(array(
'count' => new external_value(PARAM_INT, 'The total number of records matching the request.'),
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'warnings' => new external_warnings()
));
}

}
9 changes: 9 additions & 0 deletions mod/glossary/db/services.php
Expand Up @@ -58,4 +58,13 @@
'type' => 'read',
'capabilities' => 'mod/glossary:view'
),

'mod_glossary_get_entries_by_date' => array(
'classname' => 'mod_glossary_external',
'methodname' => 'get_entries_by_date',
'description' => 'Browse entries by date.',
'type' => 'read',
'capabilities' => 'mod/glossary:view'
),

);
89 changes: 89 additions & 0 deletions mod/glossary/tests/external_test.php
Expand Up @@ -285,4 +285,93 @@ public function test_get_entries_by_letter_with_parameters() {
$this->assertEquals($e1b->id, $return['entries'][1]['id']);
}

public function test_get_entries_by_date() {
global $DB;
$this->resetAfterTest(true);

// Generate all the things.
$gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$c1 = $this->getDataGenerator()->create_course();
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
$g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
$u1 = $this->getDataGenerator()->create_user();
$ctx = context_module::instance($g1->cmid);
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);

$now = time();
$e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => $u1->id,
'timecreated' => 1, 'timemodified' => $now + 3600));
$e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => $u1->id,
'timecreated' => $now + 3600, 'timemodified' => 1));
$e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id,
'timecreated' => $now + 1, 'timemodified' => $now + 1));
$e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
'timecreated' => $now + 2, 'timemodified' => $now + 2));
$e2a = $gg->create_content($g2);

$this->setAdminUser($u1);

// Ordering by time modified descending.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'DESC', 0, 20, array());
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$this->assertEquals($e1b->id, $return['entries'][2]['id']);

// Ordering by time modified ascending.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'ASC', 0, 20, array());
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1b->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$this->assertEquals($e1a->id, $return['entries'][2]['id']);

// Ordering by time created asc.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20, array());
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$this->assertEquals($e1b->id, $return['entries'][2]['id']);

// Ordering by time created descending.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'DESC', 0, 20, array());
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1b->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$this->assertEquals($e1a->id, $return['entries'][2]['id']);

// Ordering including to approve.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(4, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1a->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$this->assertEquals($e1d->id, $return['entries'][2]['id']);
$this->assertEquals($e1b->id, $return['entries'][3]['id']);

// Ordering including to approve and pagination.
$return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 2,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(2, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1a->id, $return['entries'][0]['id']);
$this->assertEquals($e1c->id, $return['entries'][1]['id']);
$return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 2, 2,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
$this->assertCount(2, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1d->id, $return['entries'][0]['id']);
$this->assertEquals($e1b->id, $return['entries'][1]['id']);
}
}

0 comments on commit 6a273d5

Please sign in to comment.