Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load teacher names in dialogue creation with large number of users and groups in a course #137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions classes/external/search_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use core_user_external;
use context_module;
use moodle_exception;
use course_enrolment_manager;
use core\session\exception;

/**
Expand Down Expand Up @@ -120,7 +119,7 @@ public static function execute(int $cmid, string $search, bool $searchanywhere,
$groups);

} else {
$manager = new course_enrolment_manager($PAGE, $course);
$manager = new \mod_dialogue\local\course_enrolment_manager($PAGE, $course);
$users = $manager->search_users($params['search'],
$params['searchanywhere'],
$params['page'],
Expand All @@ -139,10 +138,6 @@ public static function execute(int $cmid, string $search, bool $searchanywhere,
if ($user->id == $USER->id) {
continue;
}
if (!has_capability('mod/dialogue:receive', $context, $user)) {
// User does not have the ability to receive so remove them.
continue;
}
if ($userdetails = user_get_user_details($user, $course, $requiredfields)) {
$results[] = $userdetails;
}
Expand Down
55 changes: 55 additions & 0 deletions classes/local/course_enrolment_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,59 @@ public function search_users_with_groups(string $search = '', bool $searchanywhe
$params = array_merge($params, $inparams);
return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, false);
}

/**
* Searches through the enrolled users in this course based on search_users but with mod_dialogue permission.
*
* @param string $search The search term.
* @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
* @param int $page Starting at 0.
* @param int $perpage Number of users returned per page.
* @param bool $returnexactcount Return the exact total users using count_record or not.
* @param ?int $contextid Context ID we are in - we might use search on activity level and its group mode can be different from course group mode.
* @return array with two or three elements:
* int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
* array users List of user objects returned by the query.
* boolean moreusers True if there are still more users, otherwise is False.
*/
public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,
bool $returnexactcount = false, ?int $contextid = null) {
global $USER;

[$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere);

if (isset($contextid)) {
// If contextid is set, we need to determine the group mode that should be used (module or course).
[$context, $course, $cm] = get_context_info_array($contextid);
// If cm instance is returned, then use the group mode from the module, otherwise get the course group mode.
$groupmode = $cm ? groups_get_activity_groupmode($cm, $course) : groups_get_course_groupmode($this->course);
} else {
// Otherwise, default to the group mode of the course.
$context = $this->context;
$groupmode = groups_get_course_groupmode($this->course);
}

if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
$groups = groups_get_all_groups($this->course->id, $USER->id, 0, 'g.id');
$groupids = array_column($groups, 'id');
if (!$groupids) {
return ['totalusers' => 0, 'users' => [], 'moreusers' => false];
}
} else {
$groupids = [];
}

[$enrolledsql, $enrolledparams] = get_enrolled_sql($context, 'mod/dialogue:receive', $groupids);

$fields = 'SELECT ' . $ufields;
$countfields = 'SELECT COUNT(u.id)';
$sql = " FROM {user} u
$joins
JOIN ($enrolledsql) je ON je.id = u.id
WHERE $wherecondition";

$params = array_merge($params, $enrolledparams);

return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
}
}
Loading