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

Performance improvement and new ws function #32

Merged
merged 3 commits into from
Mar 4, 2024
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
80 changes: 80 additions & 0 deletions classes/external/get_site_admins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace local_aspiredu\external;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/user/externallib.php');
require_once("$CFG->dirroot/lib/externallib.php");

use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_warnings;
use local_aspiredu\local\lib;

/**
* Get users by role external function.
*
* @package local_aspiredu
* @copyright 2022 3ipunt
* @author Guillermo gomez Arias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_site_admins extends external_api {

/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([]);
}

/**
* Returns a list of users given a list of roles.
*
* @param array $roleids
* @param int|null $page current page
* @param int|null $perpage items per page
* @return array of warnings and users
*/
public static function execute(): array {
global $CFG, $DB;

return [
'users' => lib::get_users($DB->get_records_list('user', 'id', explode(',', $CFG->siteadmins))),
'warnings' => [],
];
}

/**
* Describes the get_users_by_roles return value.
*
* @return external_single_structure
*/
public static function execute_returns() {
return new external_single_structure(
[
'users' => new external_multiple_structure(\core_user_external::user_description()),
'warnings' => new external_warnings(),
]
);
}
}
55 changes: 32 additions & 23 deletions classes/local/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace local_aspiredu\local;

use context_system;
use moodle_exception;

class lib {
const ASPIREDU_DISABLED = 0;
Expand Down Expand Up @@ -97,14 +98,15 @@ public static function get_users_by_capabilities(array $capabilities, int $page
if (strlen($capabilitiesids) > 1) {
$capabilitiesids = substr($capabilitiesids, 0, -1);

$sql = 'SELECT distinct ra.userid
$sql = 'SELECT * FROM {user} WHERE id IN (SELECT ra.userid
FROM {role_assignments} ra
JOIN {role_capabilities} rc ON rc.roleid = ra.roleid
JOIN {capabilities} c ON c.name = rc.capability
WHERE c.id IN (' . $capabilitiesids . ')';
WHERE c.id IN (' . $capabilitiesids . ')
GROUP BY ra.userid
)';

$usersbycapabilities = $DB->get_recordset_sql($sql, [], $page * $perpage, $perpage);
return self::get_users_external($usersbycapabilities);
return self::get_users($DB->get_recordset_sql($sql, [], $page * $perpage, $perpage));
}

return [];
Expand All @@ -123,34 +125,41 @@ public static function get_users_by_roles(array $roleids, int $page = -1, int $p

$roleids = implode(',', $roleids);

$sql = "SELECT distinct ra.userid
$sql = "SELECT * FROM {user} WHERE id IN (SELECT ra.userid
FROM {role_assignments} ra
JOIN {role} r ON r.id = ra.roleid
WHERE r.id in ($roleids)";
WHERE r.id in ($roleids)
GROUP BY ra.userid
)";

$usersbyroles = $DB->get_recordset_sql($sql, [], $page * $perpage, $perpage);
return self::get_users_external($usersbyroles);
return self::get_users($DB->get_recordset_sql($sql, [], $page * $perpage, $perpage));
}

/**
* Call get_users external function.
* Retrieve matching user.
*
* @param \moodle_recordset $useridsset
* @return array
* @throws moodle_exception
* @param array $userids the user ids to fetch.
* @return array An array of arrays containing user profiles.
* @since Moodle 2.5
*/
private static function get_users_external(\moodle_recordset $useridsset): array {
$users = [];
foreach ($useridsset as $useridindex => $userid) {
$searchparams = [['key' => 'id', 'value' => $useridindex] ];
$user = \core_user_external::get_users($searchparams)['users'];

if (isset($user[0])) {
$users[] = $user[0];
public static function get_users($users): array {
global $CFG;

require_once($CFG->dirroot . "/user/lib.php");

// Finally retrieve each users information.
$returnedusers = [];
foreach ($users as $user) {
$details = user_get_user_details_courses($user);

if (empty($details)) {
continue;
}

$returnedusers[] = (object)$details;
}
$useridsset->close();
return $users;

return $returnedusers;
}
}


8 changes: 8 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
'type' => 'read',
'capabilities' => 'moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail',
],
'local_aspiredu_get_site_admins' => [
'classname' => '\local_aspiredu\external\get_site_admins',
'methodname' => 'execute',
'description' => 'Return a list of users who are site admins',
'type' => 'read',
'capabilities' => 'moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail',
],
];

// We define the services to install as pre-build services. A pre-build service is not editable by administrator.
Expand Down Expand Up @@ -119,6 +126,7 @@
'local_aspiredu_get_plugin_info',
'local_aspiredu_get_users_by_roles',
'local_aspiredu_get_users_by_capabilities',
'local_aspiredu_get_site_admins',
],
'restrictedusers' => 1,
'enabled' => 1,
Expand Down
54 changes: 54 additions & 0 deletions tests/get_site_admins_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace local_aspiredu;

defined('MOODLE_INTERNAL') || die();

use local_aspiredu\external\get_site_admins;

global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');

/**
* The external function get_site_admins test class.
*
* @package local_aspiredu
* @copyright 2022 3ipunt
* @author Guillermo gomez Arias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \local_aspiredu\external\get_site_admins
*/
class get_site_admins_test extends \externallib_advanced_testcase {

/**
* @runInSeparateProcess
*/
public function test_get_site_admins() {
$this->resetAfterTest();

$datagenerator = $this->getDataGenerator();
$user = $datagenerator->create_user();
$user2 = $datagenerator->create_user();

$users = get_site_admins::execute();
$users = \external_api::clean_returnvalue(get_site_admins::execute_returns(), $users);

$this->assertCount(0, $users['warnings']);
$this->assertCount(1, $users['users']);
$this->assertEquals('[email protected]', $users['users'][0]['email']);
}
}
Loading