-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_site_admins webservice function
- Loading branch information
1 parent
0b41723
commit b03ea96
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |