Skip to content

Commit

Permalink
MDL-49231 mod_glossary: External function get_entry_by_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 24777f7 commit 08d7954
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 5 deletions.
12 changes: 7 additions & 5 deletions mod/glossary/classes/entry_query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ class mod_glossary_entry_query_builder {
*
* @param object $glossary The glossary.
*/
public function __construct($glossary) {
$this->glossary = $glossary;
public function __construct($glossary = null) {
$this->from = sprintf('FROM {glossary_entries} %s', self::ALIAS_ENTRIES);
$this->where[] = sprintf('(%s.glossaryid = :gid OR %s.sourceglossaryid = :gid2)', self::ALIAS_ENTRIES, self::ALIAS_ENTRIES);
$this->params['gid'] = $glossary->id;
$this->params['gid2'] = $glossary->id;
if (!empty($glossary)) {
$this->glossary = $glossary;
$this->where[] = sprintf('(%s.glossaryid = :gid OR %s.sourceglossaryid = :gid2)', self::ALIAS_ENTRIES, self::ALIAS_ENTRIES);
$this->params['gid'] = $glossary->id;
$this->params['gid2'] = $glossary->id;
}
}

/**
Expand Down
58 changes: 58 additions & 0 deletions mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -1199,4 +1199,62 @@ public static function get_entries_by_search_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_entry_by_id_parameters() {
return new external_function_parameters(array(
'id' => new external_value(PARAM_INT, 'Glossary entry ID'),
));
}

/**
* Get an entry.
*
* @param int $id The entry ID.
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_entry_by_id($id) {
global $DB, $USER;

$params = self::validate_parameters(self::get_entry_by_id_parameters(), array('id' => $id));
$id = $params['id'];
$warnings = array();

// Get and validate the glossary.
$entry = $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);
list($glossary, $context) = self::validate_glossary($entry->glossaryid);

if (empty($entry->approved) && $entry->userid != $USER->id && !has_capability('mod/glossary:approve', $context)) {
throw new invalid_parameter_exception('invalidentry');
}

$entry = glossary_get_entry_by_id($id);
self::fill_entry_details($entry, $context);

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

/**
* Returns the description of the external function return value.
*
* @return external_description
* @since Moodle 3.1
*/
public static function get_entry_by_id_returns() {
return new external_single_structure(array(
'entry' => self::get_entry_return_structure(),
'warnings' => new external_warnings()
));
}

}
23 changes: 23 additions & 0 deletions mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3693,3 +3693,26 @@ function glossary_get_entries_by_search($glossary, $context, $query, $fullsearch

return array($entries, $count);
}

/**
* Fetch an entry.
*
* @param int $id The entry ID.
* @return object|false
*/
function glossary_get_entry_by_id($id) {

// Build the query.
$qb = new mod_glossary_entry_query_builder();
$qb->add_field('*', 'entries');
$qb->join_user();
$qb->add_user_fields();
$qb->where('id', 'entries', $id);

// Fetching the entries.
$entries = $qb->get_records();
if (empty($entries)) {
return false;
}
return array_pop($entries);
}
47 changes: 47 additions & 0 deletions mod/glossary/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,51 @@ public function test_get_entries_by_search() {
$this->assertEquals($e8->id, $return['entries'][1]['id']);
}

public function test_get_entry_by_id() {
$this->resetAfterTest(true);

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

$e1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
$e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
$e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
$e4 = $gg->create_content($g2, array('approved' => 1));

$this->setUser($u1);
$return = mod_glossary_external::get_entry_by_id($e1->id);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
$this->assertEquals($e1->id, $return['entry']['id']);

$return = mod_glossary_external::get_entry_by_id($e2->id);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
$this->assertEquals($e2->id, $return['entry']['id']);

try {
$return = mod_glossary_external::get_entry_by_id($e3->id);
$this->fail('Cannot view unapproved entries of others.');
} catch (invalid_parameter_exception $e) {
}

try {
$return = mod_glossary_external::get_entry_by_id($e4->id);
$this->fail('Cannot view entries from another course.');
} catch (require_login_exception $e) {
}

// An admin can be other's entries to be approved.
$this->setAdminUser();
$return = mod_glossary_external::get_entry_by_id($e3->id);
$return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
$this->assertEquals($e3->id, $return['entry']['id']);
}

}

0 comments on commit 08d7954

Please sign in to comment.