mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-46649 gradereport_history: Add apis to get users and user count, also refactor users_ajax.php
Part of MDL-46191
This commit is contained in:
parent
d21b664956
commit
a347687b79
@ -78,4 +78,82 @@ class helper {
|
||||
return $button;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of users.
|
||||
*
|
||||
* We're interested in anyone that had a grade history in this course. This api returns a list of such users based on various
|
||||
* criteria passed.
|
||||
*
|
||||
* @param \context $context Context of the page where the results would be shown.
|
||||
* @param string $search the text to search for (empty string = find all).
|
||||
* @param int $page page number, defaults to 0.
|
||||
* @param int $perpage Number of entries to display per page, defaults to 0.
|
||||
*
|
||||
* @return array list of users.
|
||||
*/
|
||||
public static function get_users($context, $search = '', $page = 0, $perpage = 25) {
|
||||
global $DB;
|
||||
|
||||
list($sql, $params) = self::get_users_sql_and_params($context, $search);
|
||||
$limitfrom = $page * $perpage;
|
||||
$limitto = $limitfrom + $perpage;
|
||||
$users = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total number of users present for the given search criteria.
|
||||
*
|
||||
* @param \context $context Context of the page where the results would be shown.
|
||||
* @param string $search the text to search for (empty string = find all).
|
||||
*
|
||||
* @return int number of users found.
|
||||
*/
|
||||
public static function get_users_count($context, $search = '') {
|
||||
global $DB;
|
||||
|
||||
list($sql, $params) = self::get_users_sql_and_params($context, $search, true);
|
||||
return $DB->count_records_sql($sql, $params);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sql and params to use to get list of users.
|
||||
*
|
||||
* @param \context $context Context of the page where the results would be shown.
|
||||
* @param string $search the text to search for (empty string = find all).
|
||||
* @param bool $count setting this to true, returns an sql to get count only instead of the complete data records.
|
||||
*
|
||||
* @return array sql and params list
|
||||
*/
|
||||
protected static function get_users_sql_and_params($context, $search = '', $count = false) {
|
||||
|
||||
// Fields we need from the user table.
|
||||
$extrafields = get_extra_user_fields($context);
|
||||
$params = array();
|
||||
if (!empty($search)) {
|
||||
list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
|
||||
$filtersql .= ' AND ';
|
||||
} else {
|
||||
$filtersql = '';
|
||||
}
|
||||
|
||||
$ufields = \user_picture::fields('u', $extrafields).',u.username';
|
||||
if ($count) {
|
||||
$select = "SELECT COUNT(DISTINCT u.id) ";
|
||||
$orderby = "";
|
||||
} else {
|
||||
$select = "SELECT DISTINCT $ufields ";
|
||||
$orderby = " ORDER BY u.lastname ASC, u.firstname ASC";
|
||||
}
|
||||
$sql = "$select
|
||||
FROM {user} u
|
||||
JOIN {grade_grades_history} ggh ON u.id = ggh.userid
|
||||
JOIN {grade_items} gi ON gi.id = ggh.itemid
|
||||
WHERE $filtersql gi.courseid = :courseid";
|
||||
$sql .= $orderby;
|
||||
$params['courseid'] = $context->instanceid;
|
||||
|
||||
return array($sql, $params);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@
|
||||
define('AJAX_SCRIPT', true);
|
||||
|
||||
require_once(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->dirroot . '/grade/report/history/lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // Course id.
|
||||
$search = optional_param('search', '', PARAM_RAW);
|
||||
@ -47,30 +46,27 @@ $outcome->success = true;
|
||||
$outcome->response = new stdClass();
|
||||
$outcome->error = '';
|
||||
|
||||
$report = new grade_report_history($course->id, null, $context);
|
||||
$users = $report->load_users($search, $page, 25);
|
||||
$outcome->response = array('users' => $users);
|
||||
$outcome->response['totalusers'] = count($users);
|
||||
$users = \gradereport_history\helper::get_users($context, $search, $page, 25);
|
||||
$outcome->response = array('users' => array());
|
||||
$outcome->response['totalusers'] = \gradereport_history\helper::get_users_count($context, $search);;
|
||||
|
||||
$extrafields = get_extra_user_fields($context);
|
||||
$useroptions = array('link' => false, 'visibletoscreenreaders' => false);
|
||||
|
||||
foreach ($outcome->response['users'] as &$user) {
|
||||
$user->userid = $user->id;
|
||||
$user->picture = $OUTPUT->user_picture($user, $useroptions);
|
||||
$user->fullname = fullname($user);
|
||||
// Format the user record.
|
||||
foreach ($users as $user) {
|
||||
$newuser = new stdClass();
|
||||
$newuser->userid = $user->id;
|
||||
$newuser->picture = $OUTPUT->user_picture($user, $useroptions);
|
||||
$newuser->fullname = fullname($user);
|
||||
$fieldvalues = array();
|
||||
foreach ($extrafields as $field) {
|
||||
$fieldvalues[] = s($user->{$field});
|
||||
unset($user->{$field});
|
||||
}
|
||||
$user->extrafields = implode(', ', $fieldvalues);
|
||||
unset($user->id);
|
||||
$newuser->extrafields = implode(', ', $fieldvalues);
|
||||
$outcome->response['users'][] = $newuser;
|
||||
}
|
||||
// Chrome will display users in the order of the array keys, so we need
|
||||
// to ensure that the results ordered array keys. Fortunately, the JavaScript
|
||||
// does not care what the array keys are. It uses user.id where necessary.
|
||||
$outcome->response['users'] = array_values($outcome->response['users']);
|
||||
|
||||
$outcome->success = true;
|
||||
|
||||
echo json_encode($outcome);
|
||||
|
Loading…
x
Reference in New Issue
Block a user