mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-53282 tool_lp: Extend list_frameworks to support string search
This commit is contained in:
parent
1fa1336229
commit
0fe5aac733
4 changed files with 120 additions and 13 deletions
|
@ -723,9 +723,11 @@ class api {
|
|||
* - parents: All parents, grand parents, etc...
|
||||
* - self: Context passed only.
|
||||
* @param bool $onlyvisible If true return only visible frameworks
|
||||
* @param string $query A string to use to filter down the frameworks.
|
||||
* @return array of competency_framework
|
||||
*/
|
||||
public static function list_frameworks($sort, $order, $skip, $limit, $context, $includes = 'children', $onlyvisible = false) {
|
||||
public static function list_frameworks($sort, $order, $skip, $limit, $context, $includes = 'children',
|
||||
$onlyvisible = false, $query = '') {
|
||||
global $DB;
|
||||
|
||||
// Get all the relevant contexts.
|
||||
|
@ -744,6 +746,15 @@ class api {
|
|||
$inparams['visible'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query) || is_numeric($query)) {
|
||||
$sqlnamelike = $DB->sql_like('shortname', ':namelike', false);
|
||||
$sqlidnlike = $DB->sql_like('idnumber', ':idnlike', false);
|
||||
|
||||
$select .= " AND ($sqlnamelike OR $sqlidnlike) ";
|
||||
$inparams['namelike'] = '%' . $DB->sql_like_escape($query) . '%';
|
||||
$inparams['idnlike'] = '%' . $DB->sql_like_escape($query) . '%';
|
||||
}
|
||||
|
||||
return competency_framework::get_records_select($select, $inparams, $sort . ' ' . $order, '*', $skip, $limit);
|
||||
}
|
||||
|
||||
|
|
|
@ -453,7 +453,7 @@ class external extends external_api {
|
|||
PARAM_ALPHANUMEXT,
|
||||
'Column to sort by.',
|
||||
VALUE_DEFAULT,
|
||||
''
|
||||
'shortname'
|
||||
);
|
||||
$order = new external_value(
|
||||
PARAM_ALPHA,
|
||||
|
@ -485,6 +485,12 @@ class external extends external_api {
|
|||
VALUE_DEFAULT,
|
||||
false
|
||||
);
|
||||
$query = new external_value(
|
||||
PARAM_RAW,
|
||||
'A query string to filter the results',
|
||||
VALUE_DEFAULT,
|
||||
''
|
||||
);
|
||||
|
||||
$params = array(
|
||||
'sort' => $sort,
|
||||
|
@ -493,7 +499,8 @@ class external extends external_api {
|
|||
'limit' => $limit,
|
||||
'context' => self::get_context_parameters(),
|
||||
'includes' => $includes,
|
||||
'onlyvisible' => $onlyvisible
|
||||
'onlyvisible' => $onlyvisible,
|
||||
'query' => $query,
|
||||
);
|
||||
return new external_function_parameters($params);
|
||||
}
|
||||
|
@ -509,12 +516,14 @@ class external extends external_api {
|
|||
* @param array $context
|
||||
* @param bool $includes
|
||||
* @param bool $onlyvisible
|
||||
* @param string $query
|
||||
*
|
||||
* @return array
|
||||
* @throws \required_capability_exception
|
||||
* @throws invalid_parameter_exception
|
||||
*/
|
||||
public static function list_competency_frameworks($sort, $order, $skip, $limit, $context, $includes, $onlyvisible) {
|
||||
public static function list_competency_frameworks($sort, $order, $skip, $limit, $context, $includes, $onlyvisible,
|
||||
$query = '') {
|
||||
global $PAGE;
|
||||
|
||||
$params = self::validate_parameters(self::list_competency_frameworks_parameters(),
|
||||
|
@ -525,7 +534,8 @@ class external extends external_api {
|
|||
'limit' => $limit,
|
||||
'context' => $context,
|
||||
'includes' => $includes,
|
||||
'onlyvisible' => $onlyvisible
|
||||
'onlyvisible' => $onlyvisible,
|
||||
'query' => $query,
|
||||
));
|
||||
|
||||
$context = self::get_context_from_params($params['context']);
|
||||
|
@ -542,7 +552,8 @@ class external extends external_api {
|
|||
$params['limit'],
|
||||
$context,
|
||||
$params['includes'],
|
||||
$params['onlyvisible']);
|
||||
$params['onlyvisible'],
|
||||
$params['query']);
|
||||
$records = array();
|
||||
foreach ($results as $result) {
|
||||
$exporter = new competency_framework_exporter($result);
|
||||
|
|
|
@ -197,8 +197,8 @@ class tool_lp_api_testcase extends advanced_testcase {
|
|||
|
||||
// Create a list of frameworks.
|
||||
$framework1 = $lpg->create_framework(array(
|
||||
'shortname' => 'shortname_a',
|
||||
'idnumber' => 'idnumber_c',
|
||||
'shortname' => 'shortname_alpha',
|
||||
'idnumber' => 'idnumber_cinnamon',
|
||||
'description' => 'description',
|
||||
'descriptionformat' => FORMAT_HTML,
|
||||
'visible' => true,
|
||||
|
@ -206,8 +206,8 @@ class tool_lp_api_testcase extends advanced_testcase {
|
|||
));
|
||||
|
||||
$framework2 = $lpg->create_framework(array(
|
||||
'shortname' => 'shortname_b',
|
||||
'idnumber' => 'idnumber_a',
|
||||
'shortname' => 'shortname_beetroot',
|
||||
'idnumber' => 'idnumber_apple',
|
||||
'description' => 'description',
|
||||
'descriptionformat' => FORMAT_HTML,
|
||||
'visible' => true,
|
||||
|
@ -215,11 +215,11 @@ class tool_lp_api_testcase extends advanced_testcase {
|
|||
));
|
||||
|
||||
$framework3 = $lpg->create_framework(array(
|
||||
'shortname' => 'shortname_c',
|
||||
'idnumber' => 'idnumber_b',
|
||||
'shortname' => 'shortname_crisps',
|
||||
'idnumber' => 'idnumber_beer',
|
||||
'description' => 'description',
|
||||
'descriptionformat' => FORMAT_HTML,
|
||||
'visible' => true,
|
||||
'visible' => false,
|
||||
'contextid' => context_system::instance()->id
|
||||
));
|
||||
|
||||
|
@ -242,6 +242,38 @@ class tool_lp_api_testcase extends advanced_testcase {
|
|||
$this->assertEquals($framework3->get_id(), $f->get_id());
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework1->get_id(), $f->get_id());
|
||||
|
||||
// Repeat excluding the non-visible ones.
|
||||
$result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance(), 'self', true);
|
||||
$this->assertCount(2, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework2->get_id(), $f->get_id());
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework1->get_id(), $f->get_id());
|
||||
|
||||
// Search by query string, trying match on shortname.
|
||||
$result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance(), 'self', false, 'crisp');
|
||||
$this->assertCount(1, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework3->get_id(), $f->get_id());
|
||||
|
||||
// Search by query string, trying match on shortname, but hidden.
|
||||
$result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance(), 'self', true, 'crisp');
|
||||
$this->assertCount(0, $result);
|
||||
|
||||
// Search by query string, trying match on ID number.
|
||||
$result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance(), 'self', false, 'apple');
|
||||
$this->assertCount(1, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework2->get_id(), $f->get_id());
|
||||
|
||||
// Search by query string, trying match on both.
|
||||
$result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance(), 'self', false, 'bee');
|
||||
$this->assertCount(2, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework2->get_id(), $f->get_id());
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework3->get_id(), $f->get_id());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -642,6 +642,59 @@ class tool_lp_external_testcase extends externallib_advanced_testcase {
|
|||
$this->assertEquals(true, $result->visible);
|
||||
}
|
||||
|
||||
public function list_competency_frameworks_with_query() {
|
||||
$this->setUser($this->creator);
|
||||
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
|
||||
$framework1 = $lpg->create_framework(array(
|
||||
'shortname' => 'shortname_beetroot',
|
||||
'idnumber' => 'idnumber_cinnamon',
|
||||
'description' => 'description',
|
||||
'descriptionformat' => FORMAT_HTML,
|
||||
'visible' => true,
|
||||
'contextid' => context_system::instance()->id
|
||||
));
|
||||
$framework2 = $lpg->create_framework(array(
|
||||
'shortname' => 'shortname_citrus',
|
||||
'idnumber' => 'idnumber_beer',
|
||||
'description' => 'description',
|
||||
'descriptionformat' => FORMAT_HTML,
|
||||
'visible' => true,
|
||||
'contextid' => context_system::instance()->id
|
||||
));
|
||||
|
||||
// Search on both ID number and shortname.
|
||||
$result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
|
||||
array('contextid' => context_system::instance()->id), 'self', false, 'bee');
|
||||
$result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
|
||||
$this->assertCount(2, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework1->get_id(), $f->get_id());
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework2->get_id(), $f->get_id());
|
||||
|
||||
// Search on ID number.
|
||||
$result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
|
||||
array('contextid' => context_system::instance()->id), 'self', false, 'beer');
|
||||
$result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
|
||||
$this->assertCount(1, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework2->get_id(), $f->get_id());
|
||||
|
||||
// Search on shortname.
|
||||
$result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
|
||||
array('contextid' => context_system::instance()->id), 'self', false, 'cinnamon');
|
||||
$result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
|
||||
$this->assertCount(1, $result);
|
||||
$f = (object) array_shift($result);
|
||||
$this->assertEquals($framework1->get_id(), $f->get_id());
|
||||
|
||||
// No match.
|
||||
$result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
|
||||
array('contextid' => context_system::instance()->id), 'self', false, 'pwnd!');
|
||||
$result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test we can list and count competency frameworks with read permissions.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue