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([{ call([{
methodname: 'local_treestudyplan_list_cohort', 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){ }])[0].then(function(response){
self.search.cohorts = response.map(self.cohortOptionModel); self.search.cohorts = response.map(self.cohortOptionModel);
}).catch(notification.exception); }).catch(notification.exception);
@ -924,7 +924,7 @@ export default {
{ {
call([{ call([{
methodname: 'local_treestudyplan_find_user', 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){ }])[0].then(function(response){
self.search.users = response.map(self.userOptionModel); self.search.users = response.map(self.userOptionModel);
}).catch(notification.exception); }).catch(notification.exception);

View File

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