1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * User searching requests.
19 *
20 * @package    gradereport_history
21 * @copyright  2013 NetSpot Pty Ltd (https://www.netspot.com.au)
22 * @author     Adam Olley <adam.olley@netspot.com.au>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26define('AJAX_SCRIPT', true);
27
28require_once(__DIR__ . '/../../../config.php');
29
30$id = required_param('id', PARAM_INT); // Course id.
31$search = optional_param('search', '', PARAM_RAW);
32$page = optional_param('page', 0, PARAM_INT);
33
34$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
35$context = context_course::instance($course->id, MUST_EXIST);
36
37if ($course->id == SITEID) {
38    throw new moodle_exception('invalidcourse');
39}
40
41require_sesskey();
42require_login($course);
43require_capability('gradereport/history:view', $context);
44require_capability('moodle/grade:viewall', $context);
45
46$outcome = new stdClass();
47$outcome->success = true;
48$outcome->error = '';
49
50$users = \gradereport_history\helper::get_users($context, $search, $page, 25);
51$outcome->response = array('users' => array());
52$outcome->response['totalusers'] = \gradereport_history\helper::get_users_count($context, $search);;
53
54$extrafields = get_extra_user_fields($context);
55$useroptions = array('link' => false, 'visibletoscreenreaders' => false);
56
57// Format the user record.
58foreach ($users as $user) {
59    $newuser = new stdClass();
60    $newuser->userid = $user->id;
61    $newuser->picture = $OUTPUT->user_picture($user, $useroptions);
62    $newuser->fullname = fullname($user);
63    $fieldvalues = array();
64    foreach ($extrafields as $field) {
65        $fieldvalues[] = s($user->{$field});
66    }
67    $newuser->extrafields = implode(', ', $fieldvalues);
68    $outcome->response['users'][] = $newuser;
69}
70
71$outcome->success = true;
72
73echo $OUTPUT->header();
74echo json_encode($outcome);
75echo $OUTPUT->footer();
76