Fixed issue in search for cohorts/users

This commit is contained in:
PMKuipers 2024-06-01 08:42:18 +02:00
parent 3d48f290d6
commit 742f9ef772
4 changed files with 20 additions and 31 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -867,7 +867,7 @@ export default {
{
call([{
methodname: 'local_treestudyplan_list_cohort',
args: { like: searchtext, exclude_id: self.value.id}
args: { like: searchtext, studyplan_id: self.value.id}
}])[0].then(function(response){
self.search.cohorts = response.map(self.cohortOptionModel);
}).catch(notification.exception);
@ -924,7 +924,7 @@ export default {
{
call([{
methodname: 'local_treestudyplan_find_user',
args: { like: searchtext, exclude_id: self.value.id}
args: { like: searchtext, studyplan_id: self.value.id}
}])[0].then(function(response){
self.search.users = response.map(self.userOptionModel);
}).catch(notification.exception);

View File

@ -156,9 +156,8 @@ class associationservice extends \external_api {
*/
public static function list_cohort_parameters() : \external_function_parameters {
return new \external_function_parameters( [
'like' => new \external_value(PARAM_TEXT, 'search text', VALUE_OPTIONAL),
'exclude_id' => new \external_value(PARAM_INT, 'exclude members of this studyplan', VALUE_OPTIONAL),
'context_ic' => new \external_value(PARAM_INT, 'context for this request', VALUE_OPTIONAL),
'like' => new \external_value(PARAM_TEXT, 'search text'),
'studyplan_id' => new \external_value(PARAM_INT, 'id of studyplan to list for'),
] );
}
@ -172,15 +171,15 @@ class associationservice extends \external_api {
/**
* Search cohorts for matching string
* @param string $like String to match cohorts with
* @param null $excludeid Do not include these cohorts
* @param int $contextid Context to search (default system)
* @param int $studyplan_id Do not include these cohorts
* @return array
*/
public static function list_cohort($like = '', $excludeid = null, $contextid = 1) {
public static function list_cohort($like, $studyplan_id) {
global $CFG, $DB;
// Only allow this if the user has the right to edit in this context.
$context = webservicehelper::find_context($contextid);
$studyplan = studyplan::find_by_id($studyplan_id);
$context = $studyplan->context();
webservicehelper::require_capabilities(self::CAP_EDIT, $context);
$pattern = "%{$like}%";
@ -188,17 +187,9 @@ class associationservice extends \external_api {
$params = ["pattern_nm" => $pattern, "pattern_id" => $pattern, ];
$sql = "SELECT DISTINCT c.* from {cohort} c LEFT JOIN {local_treestudyplan_cohort} j ON c.id = j.cohort_id
WHERE c.visible = 1 AND(name LIKE :pattern_nm OR idnumber LIKE :pattern_id)";
if (isset($excludeid) && is_numeric($excludeid)) {
$sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $excludeid;
}
if ($contextid > 1) {
// System context returns all cohorts, including system cohorts.
// Otherwise, .
$sql .= " AND contextid = :context_id";
$params['context_id'] = $contextid;
}
WHERE c.visible = 1 AND(name LIKE :pattern_nm OR idnumber LIKE :pattern_id)
AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $studyplan_id;
$cohorts = [];
$rs = $DB->get_recordset_sql($sql, $params);
@ -215,8 +206,7 @@ class associationservice extends \external_api {
public static function find_user_parameters() : \external_function_parameters {
return new \external_function_parameters( [
'like' => new \external_value(PARAM_TEXT, 'search text'),
'exclude_id' => new \external_value(PARAM_INT, 'exclude members of this studyplan', VALUE_OPTIONAL),
'context_id' => new \external_value(PARAM_INT, 'context for this request', VALUE_OPTIONAL),
'studyplan_id' => new \external_value(PARAM_INT, 'id of studyplan to list for'),
] );
}
@ -234,11 +224,12 @@ class associationservice extends \external_api {
* @param int $contextid Context to search (default system)
* @return array
*/
public static function find_user($like, $excludeid = null, $contextid = 1) {
public static function find_user($like, $studyplan_id) {
global $CFG, $DB;
// Only allow this if the user has the right to edit in this context.
$context = webservicehelper::find_context($contextid);
$studyplan = studyplan::find_by_id($studyplan_id);
$context = $studyplan->context();
webservicehelper::require_capabilities(self::CAP_EDIT, $context);
$pattern = "%{$like}%";
@ -247,11 +238,9 @@ class associationservice extends \external_api {
"pattern_un" => $pattern,
];
$sql = "SELECT DISTINCT u.* from {user} u LEFT JOIN {local_treestudyplan_user} j ON u.id = j.user_id
WHERE u.deleted != 1 AND (firstname LIKE :pattern_fn OR lastname LIKE :pattern_ln OR username LIKE :pattern_un)";
if (isset($excludeid) && is_numeric($excludeid)) {
$sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $excludeid;
}
WHERE u.deleted != 1 AND (firstname LIKE :pattern_fn OR lastname LIKE :pattern_ln OR username LIKE :pattern_un)
AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $studyplan_id;
$users = [];
$rs = $DB->get_recordset_sql($sql, $params);