Skip to content

Commit

Permalink
MDL-49231 mod_glossary: External function get_entries_by_author_id
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 f7d9cd2 commit e70f58c
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 0 deletions.
140 changes: 140 additions & 0 deletions mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,4 +1166,144 @@ public static function get_entries_by_author_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_author_id_parameters() {
return new external_function_parameters(array(
'id' => new external_value(PARAM_INT, 'Glossary entry ID'),
'authorid' => new external_value(PARAM_INT, 'The author ID'),
'order' => new external_value(PARAM_ALPHA, 'Order by: \'CONCEPT\', \'CREATION\' or \'UPDATE\'', VALUE_DEFAULT,
'CONCEPT'),
'sort' => new external_value(PARAM_ALPHA, 'The direction of the order: \'ASC\' or \'DESC\'', VALUE_DEFAULT, 'ASC'),
'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 author.
*
* @param int $id The glossary ID.
* @param int $authorid The author ID.
* @param string $order The way to order the results.
* @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_author_id($id, $authorid, $order = 'CONCEPT', $sort = 'ASC', $from = 0, $limit = 20,
$options = array()) {
global $DB, $USER;

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

if (!in_array($order, array('CONCEPT', '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('author', $modes)) {
throw new invalid_parameter_exception('invalidbrowsemode');
}

// Preparing the query.
$params = array();

$approvedsql = '(ge.approved <> 0 OR ge.userid = :myid)';
$params['myid'] = $USER->id;
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
JOIN {user} u ON ge.userid = u.id
WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2)
AND $approvedsql
AND ge.userid = :uid";
$params['uid'] = $authorid;
$params['gid1'] = $glossary->id;
$params['gid2'] = $glossary->id;

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

} else if ($order == 'UPDATE') {
$sqlorder .= 'ge.timemodified';

} else {
$sqlorder .= 'ge.concept';
}
$sqlorder .= ' ' . $sort;

// 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_author_id_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()
));
}
}
109 changes: 109 additions & 0 deletions mod/glossary/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,113 @@ public function test_get_entries_by_author() {
$this->assertEquals($e1a2->id, $return['entries'][2]['id']);
$this->assertEquals($e1a3->id, $return['entries'][3]['id']);
}

public function test_get_entries_by_author_id() {
$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, 'displayformat' => 'entrylist'));
$u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
$u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
$u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
$u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
$ctx = context_module::instance($g1->cmid);
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);

$e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Zoom',
'timecreated' => 3600, 'timemodified' => time() - 3600));
$e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Alpha'));
$e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Dog',
'timecreated' => 1, 'timemodified' => time() - 1800));
$e1a4 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id, 'concept' => 'Bird'));
$e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
$e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));

$this->setAdminUser();

// Standard request.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a2->id, $return['entries'][0]['id']);
$this->assertEquals($e1a3->id, $return['entries'][1]['id']);
$this->assertEquals($e1a1->id, $return['entries'][2]['id']);

// Standard request descending.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'DESC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a1->id, $return['entries'][0]['id']);
$this->assertEquals($e1a3->id, $return['entries'][1]['id']);
$this->assertEquals($e1a2->id, $return['entries'][2]['id']);

// Requesting ordering by time created.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'ASC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a3->id, $return['entries'][0]['id']);
$this->assertEquals($e1a1->id, $return['entries'][1]['id']);
$this->assertEquals($e1a2->id, $return['entries'][2]['id']);

// Requesting ordering by time created descending.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'DESC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a2->id, $return['entries'][0]['id']);
$this->assertEquals($e1a1->id, $return['entries'][1]['id']);
$this->assertEquals($e1a3->id, $return['entries'][2]['id']);

// Requesting ordering by time modified.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'ASC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a1->id, $return['entries'][0]['id']);
$this->assertEquals($e1a3->id, $return['entries'][1]['id']);
$this->assertEquals($e1a2->id, $return['entries'][2]['id']);

// Requesting ordering by time modified descending.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'DESC', 0, 20);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(3, $return['entries']);
$this->assertEquals(3, $return['count']);
$this->assertEquals($e1a2->id, $return['entries'][0]['id']);
$this->assertEquals($e1a3->id, $return['entries'][1]['id']);
$this->assertEquals($e1a1->id, $return['entries'][2]['id']);

// Including non approved.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(4, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1a2->id, $return['entries'][0]['id']);
$this->assertEquals($e1a4->id, $return['entries'][1]['id']);
$this->assertEquals($e1a3->id, $return['entries'][2]['id']);
$this->assertEquals($e1a1->id, $return['entries'][3]['id']);

// Pagination.
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 2,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(2, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1a2->id, $return['entries'][0]['id']);
$this->assertEquals($e1a4->id, $return['entries'][1]['id']);
$return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 1, 2,
array('includenotapproved' => true));
$return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
$this->assertCount(2, $return['entries']);
$this->assertEquals(4, $return['count']);
$this->assertEquals($e1a4->id, $return['entries'][0]['id']);
$this->assertEquals($e1a3->id, $return['entries'][1]['id']);
}

}

0 comments on commit e70f58c

Please sign in to comment.