mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +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...
|
* - parents: All parents, grand parents, etc...
|
||||||
* - self: Context passed only.
|
* - self: Context passed only.
|
||||||
* @param bool $onlyvisible If true return only visible frameworks
|
* @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
|
* @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;
|
global $DB;
|
||||||
|
|
||||||
// Get all the relevant contexts.
|
// Get all the relevant contexts.
|
||||||
|
@ -744,6 +746,15 @@ class api {
|
||||||
$inparams['visible'] = 1;
|
$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);
|
return competency_framework::get_records_select($select, $inparams, $sort . ' ' . $order, '*', $skip, $limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -453,7 +453,7 @@ class external extends external_api {
|
||||||
PARAM_ALPHANUMEXT,
|
PARAM_ALPHANUMEXT,
|
||||||
'Column to sort by.',
|
'Column to sort by.',
|
||||||
VALUE_DEFAULT,
|
VALUE_DEFAULT,
|
||||||
''
|
'shortname'
|
||||||
);
|
);
|
||||||
$order = new external_value(
|
$order = new external_value(
|
||||||
PARAM_ALPHA,
|
PARAM_ALPHA,
|
||||||
|
@ -485,6 +485,12 @@ class external extends external_api {
|
||||||
VALUE_DEFAULT,
|
VALUE_DEFAULT,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
$query = new external_value(
|
||||||
|
PARAM_RAW,
|
||||||
|
'A query string to filter the results',
|
||||||
|
VALUE_DEFAULT,
|
||||||
|
''
|
||||||
|
);
|
||||||
|
|
||||||
$params = array(
|
$params = array(
|
||||||
'sort' => $sort,
|
'sort' => $sort,
|
||||||
|
@ -493,7 +499,8 @@ class external extends external_api {
|
||||||
'limit' => $limit,
|
'limit' => $limit,
|
||||||
'context' => self::get_context_parameters(),
|
'context' => self::get_context_parameters(),
|
||||||
'includes' => $includes,
|
'includes' => $includes,
|
||||||
'onlyvisible' => $onlyvisible
|
'onlyvisible' => $onlyvisible,
|
||||||
|
'query' => $query,
|
||||||
);
|
);
|
||||||
return new external_function_parameters($params);
|
return new external_function_parameters($params);
|
||||||
}
|
}
|
||||||
|
@ -509,12 +516,14 @@ class external extends external_api {
|
||||||
* @param array $context
|
* @param array $context
|
||||||
* @param bool $includes
|
* @param bool $includes
|
||||||
* @param bool $onlyvisible
|
* @param bool $onlyvisible
|
||||||
|
* @param string $query
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws \required_capability_exception
|
* @throws \required_capability_exception
|
||||||
* @throws invalid_parameter_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;
|
global $PAGE;
|
||||||
|
|
||||||
$params = self::validate_parameters(self::list_competency_frameworks_parameters(),
|
$params = self::validate_parameters(self::list_competency_frameworks_parameters(),
|
||||||
|
@ -525,7 +534,8 @@ class external extends external_api {
|
||||||
'limit' => $limit,
|
'limit' => $limit,
|
||||||
'context' => $context,
|
'context' => $context,
|
||||||
'includes' => $includes,
|
'includes' => $includes,
|
||||||
'onlyvisible' => $onlyvisible
|
'onlyvisible' => $onlyvisible,
|
||||||
|
'query' => $query,
|
||||||
));
|
));
|
||||||
|
|
||||||
$context = self::get_context_from_params($params['context']);
|
$context = self::get_context_from_params($params['context']);
|
||||||
|
@ -542,7 +552,8 @@ class external extends external_api {
|
||||||
$params['limit'],
|
$params['limit'],
|
||||||
$context,
|
$context,
|
||||||
$params['includes'],
|
$params['includes'],
|
||||||
$params['onlyvisible']);
|
$params['onlyvisible'],
|
||||||
|
$params['query']);
|
||||||
$records = array();
|
$records = array();
|
||||||
foreach ($results as $result) {
|
foreach ($results as $result) {
|
||||||
$exporter = new competency_framework_exporter($result);
|
$exporter = new competency_framework_exporter($result);
|
||||||
|
|
|
@ -197,8 +197,8 @@ class tool_lp_api_testcase extends advanced_testcase {
|
||||||
|
|
||||||
// Create a list of frameworks.
|
// Create a list of frameworks.
|
||||||
$framework1 = $lpg->create_framework(array(
|
$framework1 = $lpg->create_framework(array(
|
||||||
'shortname' => 'shortname_a',
|
'shortname' => 'shortname_alpha',
|
||||||
'idnumber' => 'idnumber_c',
|
'idnumber' => 'idnumber_cinnamon',
|
||||||
'description' => 'description',
|
'description' => 'description',
|
||||||
'descriptionformat' => FORMAT_HTML,
|
'descriptionformat' => FORMAT_HTML,
|
||||||
'visible' => true,
|
'visible' => true,
|
||||||
|
@ -206,8 +206,8 @@ class tool_lp_api_testcase extends advanced_testcase {
|
||||||
));
|
));
|
||||||
|
|
||||||
$framework2 = $lpg->create_framework(array(
|
$framework2 = $lpg->create_framework(array(
|
||||||
'shortname' => 'shortname_b',
|
'shortname' => 'shortname_beetroot',
|
||||||
'idnumber' => 'idnumber_a',
|
'idnumber' => 'idnumber_apple',
|
||||||
'description' => 'description',
|
'description' => 'description',
|
||||||
'descriptionformat' => FORMAT_HTML,
|
'descriptionformat' => FORMAT_HTML,
|
||||||
'visible' => true,
|
'visible' => true,
|
||||||
|
@ -215,11 +215,11 @@ class tool_lp_api_testcase extends advanced_testcase {
|
||||||
));
|
));
|
||||||
|
|
||||||
$framework3 = $lpg->create_framework(array(
|
$framework3 = $lpg->create_framework(array(
|
||||||
'shortname' => 'shortname_c',
|
'shortname' => 'shortname_crisps',
|
||||||
'idnumber' => 'idnumber_b',
|
'idnumber' => 'idnumber_beer',
|
||||||
'description' => 'description',
|
'description' => 'description',
|
||||||
'descriptionformat' => FORMAT_HTML,
|
'descriptionformat' => FORMAT_HTML,
|
||||||
'visible' => true,
|
'visible' => false,
|
||||||
'contextid' => context_system::instance()->id
|
'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());
|
$this->assertEquals($framework3->get_id(), $f->get_id());
|
||||||
$f = (object) array_shift($result);
|
$f = (object) array_shift($result);
|
||||||
$this->assertEquals($framework1->get_id(), $f->get_id());
|
$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);
|
$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.
|
* Test we can list and count competency frameworks with read permissions.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue