first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+240
View File
@@ -0,0 +1,240 @@
<?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 core_grades\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/gradelib.php");
require_once("$CFG->dirroot/grade/edit/tree/lib.php");
/**
* Create gradecategories webservice.
*
* @package core_grades
* @copyright 2021 Peter Burnett <peterburnett@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.11
*/
class create_gradecategories extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.11
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'courseid' => new external_value(PARAM_INT, 'id of course', VALUE_REQUIRED),
'categories' => new external_multiple_structure(
new external_single_structure([
'fullname' => new external_value(PARAM_TEXT, 'fullname of category', VALUE_REQUIRED),
'options' => new external_single_structure([
'aggregation' => new external_value(PARAM_INT, 'aggregation method', VALUE_OPTIONAL),
'aggregateonlygraded' => new external_value(PARAM_BOOL, 'exclude empty grades', VALUE_OPTIONAL),
'aggregateoutcomes' => new external_value(PARAM_BOOL, 'aggregate outcomes', VALUE_OPTIONAL),
'droplow' => new external_value(PARAM_INT, 'drop low grades', VALUE_OPTIONAL),
'itemname' => new external_value(PARAM_TEXT, 'the category total name', VALUE_OPTIONAL),
'iteminfo' => new external_value(PARAM_TEXT, 'the category iteminfo', VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_TEXT, 'the category idnumber', VALUE_OPTIONAL),
'gradetype' => new external_value(PARAM_INT, 'the grade type', VALUE_OPTIONAL),
'grademax' => new external_value(PARAM_INT, 'the grade max', VALUE_OPTIONAL),
'grademin' => new external_value(PARAM_INT, 'the grade min', VALUE_OPTIONAL),
'gradepass' => new external_value(PARAM_INT, 'the grade to pass', VALUE_OPTIONAL),
'display' => new external_value(PARAM_INT, 'the display type', VALUE_OPTIONAL),
'decimals' => new external_value(PARAM_INT, 'the decimal count', VALUE_OPTIONAL),
'hiddenuntil' => new external_value(PARAM_INT, 'grades hidden until', VALUE_OPTIONAL),
'locktime' => new external_value(PARAM_INT, 'lock grades after', VALUE_OPTIONAL),
'weightoverride' => new external_value(PARAM_BOOL, 'weight adjusted', VALUE_OPTIONAL),
'aggregationcoef2' => new external_value(PARAM_RAW, 'weight coefficient', VALUE_OPTIONAL),
'parentcategoryid' => new external_value(PARAM_INT, 'The parent category id', VALUE_OPTIONAL),
'parentcategoryidnumber' => new external_value(PARAM_TEXT,
'the parent category idnumber', VALUE_OPTIONAL),
], 'optional category data', VALUE_DEFAULT, []),
], 'Category to create', VALUE_REQUIRED)
, 'Categories to create', VALUE_REQUIRED)
]
);
}
/**
* Creates gradecategories inside of the specified course.
*
* @param int $courseid the courseid to create the gradecategory in.
* @param array $categories the categories to create.
* @return array array of created categoryids and warnings.
* @since Moodle 3.11
*/
public static function execute(int $courseid, array $categories): array {
$params = self::validate_parameters(self::execute_parameters(),
['courseid' => $courseid, 'categories' => $categories]);
// Now params are validated, update the references.
$courseid = $params['courseid'];
$categories = $params['categories'];
// Check that the context and permissions are OK.
$context = \context_course::instance($courseid);
self::validate_context($context);
require_capability('moodle/grade:manage', $context);
return self::create_gradecategories_from_data($courseid, $categories);
}
/**
* Returns description of method result value
*
* @return external_single_structure
* @since Moodle 3.11
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'categoryids' => new external_multiple_structure(
new external_value(PARAM_INT, 'created cateogry ID')
),
'warnings' => new external_warnings(),
]);
}
/**
* Takes an array of categories and creates the inside the category tree for the supplied courseid.
*
* @param int $courseid the courseid to create the categories inside of.
* @param array $categories the categories to create.
* @return array array of results and warnings.
*/
public static function create_gradecategories_from_data(int $courseid, array $categories): array {
global $CFG, $DB;
$defaultparentcat = \grade_category::fetch_course_category($courseid);
// Setup default data so WS call needs to contain only data to set.
// This is not done in the Parameters, so that the array of options can be optional.
$defaultdata = [
'aggregation' => grade_get_setting($courseid, 'aggregation', $CFG->grade_aggregation, true),
'aggregateonlygraded' => 1,
'aggregateoutcomes' => 0,
'droplow' => 0,
'grade_item_itemname' => '',
'grade_item_iteminfo' => '',
'grade_item_idnumber' => '',
'grade_item_gradetype' => GRADE_TYPE_VALUE,
'grade_item_grademax' => 100,
'grade_item_grademin' => 1,
'grade_item_gradepass' => 1,
'grade_item_display' => GRADE_DISPLAY_TYPE_DEFAULT,
// Hack. This must be -2 to use the default setting.
'grade_item_decimals' => -2,
'grade_item_hiddenuntil' => 0,
'grade_item_locktime' => 0,
'grade_item_weightoverride' => 0,
'grade_item_aggregationcoef2' => 0,
'parentcategory' => $defaultparentcat->id
];
// Most of the data items need boilerplate prepended. These are the exceptions.
$ignorekeys = [
'aggregation',
'aggregateonlygraded',
'aggregateoutcomes',
'droplow',
'parentcategoryid',
'parentcategoryidnumber'
];
$createdcats = [];
foreach ($categories as $category) {
// Setup default data so WS call needs to contain only data to set.
// This is not done in the Parameters, so that the array of options can be optional.
$data = $defaultdata;
$data['fullname'] = $category['fullname'];
foreach ($category['options'] as $key => $value) {
if (!in_array($key, $ignorekeys)) {
$fullkey = 'grade_item_' . $key;
$data[$fullkey] = $value;
} else {
$data[$key] = $value;
}
}
// Handle parent category special case.
// This figures the parent category id from the provided id OR idnumber.
if (array_key_exists('parentcategoryid', $category['options']) && $parentcat = $DB->get_record('grade_categories',
['id' => $category['options']['parentcategoryid'], 'courseid' => $courseid])) {
$data['parentcategory'] = $parentcat->id;
} else if (array_key_exists('parentcategoryidnumber', $category['options']) &&
$parentcatgradeitem = $DB->get_record('grade_items', [
'itemtype' => 'category',
'courseid' => $courseid,
'idnumber' => $category['options']['parentcategoryidnumber']
], '*', IGNORE_MULTIPLE)) {
if ($parentcat = $DB->get_record('grade_categories',
['courseid' => $courseid, 'id' => $parentcatgradeitem->iteminstance])) {
$data['parentcategory'] = $parentcat->id;
}
}
// Create new gradecategory item.
$gradecategory = new \grade_category(['courseid' => $courseid], false);
$gradecategory->apply_default_settings();
$gradecategory->apply_forced_settings();
// Data Validation.
if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
if (empty($data['grade_item_scaleid'])) {
$warnings[] = ['item' => 'scaleid', 'warningcode' => 'invalidscale',
'message' => get_string('missingscale', 'grades')];
}
}
if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
$data['grade_item_grademax'] < $data['grade_item_grademin'])) {
$warnings[] = ['item' => 'grademax', 'warningcode' => 'invalidgrade',
'message' => get_string('incorrectminmax', 'grades')];
}
}
if (!empty($warnings)) {
return ['categoryids' => [], 'warnings' => $warnings];
}
// Now call the update function with data. Transactioned so the gradebook isn't broken on bad data.
// This is done per-category so that children can correctly discover the parent categories.
try {
$transaction = $DB->start_delegated_transaction();
\grade_edit_tree::update_gradecategory($gradecategory, (object) $data);
$transaction->allow_commit();
$createdcats[] = $gradecategory->id;
} catch (\Exception $e) {
// If the submitted data was broken for any reason.
$warnings['database'] = $e->getMessage();
$transaction->rollback($e);
return ['warnings' => $warnings];
}
}
return['categoryids' => $createdcats, 'warnings' => []];
}
}
@@ -0,0 +1,182 @@
<?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 core_grades\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\restricted_context_exception;
use moodle_url;
use core_user;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/grade/lib.php');
/**
* Get the enrolled users within and map some fields to the returned array of user objects.
*
* @package core_grades
* @copyright 2022 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.1
* @deprecated
*/
class get_enrolled_users_for_search_widget extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @deprecated since 4.2
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
'actionbaseurl' => new external_value(PARAM_URL, 'The base URL for the user option', VALUE_REQUIRED),
'groupid' => new external_value(PARAM_INT, 'Group Id', VALUE_DEFAULT, 0)
]
);
}
/**
* Given a course ID find the enrolled users within and map some fields to the returned array of user objects.
*
* @param int $courseid
* @param string $actionbaseurl The base URL for the user option.
* @param int|null $groupid
* @return array Users and warnings to pass back to the calling widget.
* @throws coding_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
* @deprecated since 4.2
*/
public static function execute(int $courseid, string $actionbaseurl, ?int $groupid = 0): array {
global $DB, $PAGE;
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'actionbaseurl' => $actionbaseurl,
'groupid' => $groupid
]
);
$warnings = [];
$coursecontext = \context_course::instance($params['courseid']);
parent::validate_context($coursecontext);
require_capability('moodle/course:viewparticipants', $coursecontext);
$course = $DB->get_record('course', ['id' => $params['courseid']]);
// Create a graded_users_iterator because it will properly check the groups etc.
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
$gui = new \graded_users_iterator($course, null, $params['groupid']);
$gui->require_active_enrolment($showonlyactiveenrol);
$gui->init();
$users = [];
$userfieldsapi = \core_user\fields::for_identity($coursecontext, false)->with_userpic();
$extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
while ($userdata = $gui->next_user()) {
$guiuser = $userdata->user;
$user = new \stdClass();
$user->fullname = fullname($guiuser);
foreach (\core_user\fields::get_name_fields() as $field) {
$user->$field = $guiuser->$field ?? null;
}
$user->id = $guiuser->id;
$user->url = (new moodle_url($actionbaseurl, ['id' => $courseid, 'userid' => $guiuser->id]))->out(false);
$userpicture = new \user_picture($guiuser);
$userpicture->size = 1;
$user->profileimage = $userpicture->get_url($PAGE)->out(false);
foreach ($extrauserfields as $field) {
$user->$field = $userdata->user->$field ?? null;
}
$user->active = false;
$users[] = $user;
}
$gui->close();
return [
'users' => $users,
'warnings' => $warnings,
];
}
/**
* Returns description of method result value.
*
* @return external_single_structure
* @deprecated since 4.2
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'users' => new external_multiple_structure(self::user_description()),
'warnings' => new external_warnings(),
]);
}
/**
* Create user return value description.
*
* @return external_description
*/
public static function user_description(): external_description {
$userfields = [
'id' => new external_value(core_user::get_property_type('id'), 'ID of the user'),
'profileimage' => new external_value(
PARAM_URL,
'The location of the users larger image',
VALUE_OPTIONAL
),
'url' => new external_value(
PARAM_URL,
'The link to the user report',
VALUE_OPTIONAL
),
'fullname' => new external_value(PARAM_TEXT, 'The full name of the user', VALUE_OPTIONAL),
'email' => new external_value(
core_user::get_property_type('email'),
'An email address - allow email as root@localhost',
VALUE_OPTIONAL),
'active' => new external_value(PARAM_BOOL, 'Are we currently on this item?', VALUE_REQUIRED)
];
return new external_single_structure($userfields);
}
/**
* Mark the function as deprecated.
* @return bool
*/
public static function execute_is_deprecated() {
return true;
}
}
@@ -0,0 +1,138 @@
<?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 core_grades\external;
use core_user_external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\restricted_context_exception;
use user_picture;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->dirroot .'/user/externallib.php');
/**
* Get the enrolled users within and map some fields to the returned array of user objects.
*
* @package core_grades
* @copyright 2022 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.2
*/
class get_enrolled_users_for_selector extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
'groupid' => new external_value(PARAM_INT, 'Group Id', VALUE_DEFAULT, 0)
]
);
}
/**
* Given a course ID find the enrolled users within and map some fields to the returned array of user objects.
*
* @param int $courseid
* @param int|null $groupid
* @return array Users and warnings to pass back to the calling widget.
* @throws coding_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
*/
public static function execute(int $courseid, ?int $groupid = 0): array {
global $DB, $PAGE;
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'groupid' => $groupid
]
);
$warnings = [];
$coursecontext = \context_course::instance($params['courseid']);
parent::validate_context($coursecontext);
require_capability('moodle/course:viewparticipants', $coursecontext);
$course = $DB->get_record('course', ['id' => $params['courseid']]);
// Create a graded_users_iterator because it will properly check the groups etc.
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
$gui = new \graded_users_iterator($course, null, $params['groupid']);
$gui->require_active_enrolment($showonlyactiveenrol);
$gui->init();
$users = [];
$userfieldsapi = \core_user\fields::for_identity($coursecontext, false)->with_userpic();
$extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
while ($userdata = $gui->next_user()) {
$userforselector = new \stdClass();
$userforselector->id = $userdata->user->id;
$userforselector->fullname = fullname($userdata->user);
foreach (\core_user\fields::get_name_fields() as $field) {
$userforselector->$field = $userdata->user->$field ?? null;
}
$userpicture = new user_picture($userdata->user);
$userpicture->size = 1;
$userforselector->profileimageurl = $userpicture->get_url($PAGE)->out(false);
$userpicture->size = 0; // Size f2.
$userforselector->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
foreach ($extrauserfields as $field) {
$userforselector->$field = $userdata->user->$field ?? null;
}
$users[] = $userforselector;
}
$gui->close();
return [
'users' => $users,
'warnings' => $warnings,
];
}
/**
* Returns description of method result value.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'users' => new external_multiple_structure(core_user_external::user_description()),
'warnings' => new external_warnings(),
]);
}
}
+114
View File
@@ -0,0 +1,114 @@
<?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 core_grades\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use invalid_parameter_exception;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/grade/lib.php');
/**
* Web service to fetch students feedback for a grade item.
*
* @package core_grades
* @copyright 2023 Kevin Percy <kevin.percy@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_feedback extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED),
'userid' => new external_value(PARAM_INT, 'User ID', VALUE_REQUIRED),
'itemid' => new external_value(PARAM_INT, 'Grade Item ID', VALUE_REQUIRED)
]
);
}
/**
* Given a user ID and grade item ID, return feedback and user details.
*
* @param int $courseid The course ID.
* @param int $userid
* @param int $itemid
* @return array Feedback and user details
*/
public static function execute(int $courseid, int $userid, int $itemid): array {
global $OUTPUT, $CFG;
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'userid' => $userid,
'itemid' => $itemid
]
);
$context = \context_course::instance($courseid);
parent::validate_context($context);
require_capability('gradereport/grader:view', $context);
$gtree = new \grade_tree($params['courseid'], false, false, null, !$CFG->enableoutcomes);
$gradeitem = $gtree->get_item($params['itemid']);
// If Item ID is not part of Course ID, $gradeitem will be set to false.
if ($gradeitem === false) {
throw new invalid_parameter_exception('Course ID and item ID mismatch');
}
$grade = $gradeitem->get_grade($params['userid'], false);
$user = \core_user::get_user($params['userid']);
$extrafields = \core_user\fields::get_identity_fields($context);
return [
'feedbacktext' => $grade->feedback,
'title' => $gradeitem->get_name(true),
'fullname' => fullname($user),
'picture' => $OUTPUT->user_picture($user, ['size' => 50, 'link' => false]),
'additionalfield' => empty($extrafields) ? '' : $user->{$extrafields[0]},
];
}
/**
* Describes the return structure.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'feedbacktext' => new external_value(PARAM_RAW, 'The full feedback text'),
'title' => new external_value(PARAM_TEXT, 'Title of the grade item that the feedback is for'),
'fullname' => new external_value(PARAM_TEXT, 'Students name'),
'picture' => new external_value(PARAM_RAW, 'Students picture'),
'additionalfield' => new external_value(PARAM_RAW, 'Additional field for the user (email or ID number, for example)'),
]);
}
}
+122
View File
@@ -0,0 +1,122 @@
<?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 core_grades\external;
use coding_exception;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\restricted_context_exception;
use core_user_external;
use invalid_parameter_exception;
use moodle_exception;
use user_picture;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/user/externallib.php');
/**
* Get the gradable users in a course.
*
* @package core_grades
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_gradable_users extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
'groupid' => new external_value(PARAM_INT, 'Group Id', VALUE_DEFAULT, 0),
'onlyactive' => new external_value(PARAM_BOOL, 'Only active enrolment', VALUE_DEFAULT, false),
]
);
}
/**
* Given a course ID find the gradable users within a group.
*
* @param int $courseid Course ID
* @param int|null $groupid Group ID
* @param bool $onlyactive Whether we should only return active enrolments.
* @return array Users and warnings.
* @throws coding_exception
* @throws invalid_parameter_exception
* @throws moodle_exception
* @throws restricted_context_exception
*/
public static function execute(int $courseid, ?int $groupid = 0, bool $onlyactive = false): array {
global $DB, $PAGE;
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'groupid' => $groupid,
'onlyactive' => $onlyactive,
]
);
$warnings = [];
$coursecontext = \context_course::instance($params['courseid']);
parent::validate_context($coursecontext);
require_capability('moodle/course:viewparticipants', $coursecontext);
$course = $DB->get_record('course', ['id' => $params['courseid']]);
$onlyactive = $onlyactive || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
$users = get_gradable_users($course->id, $params['groupid'], $onlyactive);
$users = array_map(function ($user) use ($PAGE) {
$user->fullname = fullname($user);
$userpicture = new user_picture($user);
$userpicture->size = 1;
$user->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
$user->profileimageurl = $userpicture->get_url($PAGE)->out(false);
return $user;
}, $users);
sort($users);
return [
'users' => $users,
'warnings' => $warnings,
];
}
/**
* Returns description of method result value.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'users' => new external_multiple_structure(core_user_external::user_description()),
'warnings' => new external_warnings(),
]);
}
}
+121
View File
@@ -0,0 +1,121 @@
<?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 core_grades\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/grade/lib.php');
/**
* Web service to return the grade tree structure for a given course.
*
* @package core_grades
* @copyright 2023 Mihail Geshoski <mihail@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_grade_tree extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED)
]
);
}
/**
* Given a course ID, return the grade tree structure for that course.
*
* @param int $courseid The course ID.
* @return string JSON encoded data representing the course grade tree structure.
*/
public static function execute(int $courseid): string {
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid
]
);
$context = \context_course::instance($params['courseid']);
parent::validate_context($context);
// Make sure that the user has the capability to view the full grade tree in the course.
require_capability('moodle/grade:viewall', $context);
// Get the course grade category.
$coursegradecategory = \grade_category::fetch_course_category($params['courseid']);
$coursegradetree = self::generate_course_grade_tree($coursegradecategory);
return json_encode($coursegradetree);
}
/**
* Describes the return structure.
*
* @return external_value
*/
public static function execute_returns(): external_value {
return new external_value(PARAM_RAW, 'JSON encoded data representing the course grade tree structure.');
}
/**
* Recursively generates the course grade tree structure.
*
* @param \grade_category $gradecategory The course grade category.
* @return array The course grade tree structure.
*/
private static function generate_course_grade_tree(\grade_category $gradecategory): array {
$gradecategorydata = [
'id' => $gradecategory->id,
'name' => $gradecategory->get_name(),
'iscategory' => true,
'haschildcategories' => false
];
// Get the children of the grade category.
if ($gradecategorychildren = $gradecategory->get_children()) {
foreach ($gradecategorychildren as $child) {
// If the child is a grade category, recursively generate the grade tree structure for that category.
if ($child['object'] instanceof \grade_category) {
$gradecategorydata['haschildcategories'] = true;
$gradecategorydata['children'][] = self::generate_course_grade_tree($child['object']);
} else { // Otherwise, add the grade item to the grade tree structure.
$gradecategorydata['children'][] = [
'id' => $child['object']->id,
'name' => $child['object']->get_name(),
'iscategory' => false,
'children' => null
];
}
}
} else { // If the grade category has no children, set the children property to null.
$gradecategorydata['children'] = null;
}
return $gradecategorydata;
}
}
+106
View File
@@ -0,0 +1,106 @@
<?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 core_grades\external;
defined('MOODLE_INTERNAL') || die;
use context_course;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\restricted_context_exception;
use grade_item;
require_once($CFG->libdir . '/gradelib.php');
/**
* External grade get gradeitems API implementation
*
* @package core_grades
* @copyright 2023 Mathew May <mathew.solutions>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_gradeitems extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED)
]
);
}
/**
* Given a course ID find the grading objects and return their names & IDs.
*
* @param int $courseid
* @return array
* @throws restricted_context_exception
* @throws \invalid_parameter_exception
*/
public static function execute(int $courseid): array {
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid
]
);
$warnings = [];
$context = context_course::instance($params['courseid']);
parent::validate_context($context);
$allgradeitems = grade_item::fetch_all(['courseid' => $params['courseid']]);
$gradeitems = array_filter($allgradeitems, function($item) {
$item->itemname = $item->get_name();
$item->category = $item->get_parent_category()->get_name();
return $item->gradetype != GRADE_TYPE_NONE && !$item->is_category_item() && !$item->is_course_item();
});
return [
'gradeItems' => $gradeitems,
'warnings' => $warnings,
];
}
/**
* Returns description of what gradeitems fetch should return.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'gradeItems' => new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_ALPHANUM, 'An ID for the grade item', VALUE_REQUIRED),
'itemname' => new external_value(PARAM_RAW, 'The full name of the grade item', VALUE_REQUIRED),
'category' => new external_value(PARAM_TEXT, 'The grade category of the grade item', VALUE_OPTIONAL),
])
),
'warnings' => new external_warnings(),
]);
}
}
+159
View File
@@ -0,0 +1,159 @@
<?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 core_grades\external;
use context_course;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
/**
* External group report API implementation
*
* @package core_grades
* @copyright 2022 Mathew May <mathew.solutions>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated
*/
class get_groups_for_search_widget extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @deprecated since 4.2
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
'actionbaseurl' => new external_value(PARAM_URL, 'The base URL for the group action', VALUE_REQUIRED)
]
);
}
/**
* Given a course ID find the existing user groups and map some fields to the returned array of group objects.
*
* @param int $courseid
* @param string $actionbaseurl The base URL for the group action.
* @return array Groups and warnings to pass back to the calling widget.
* @deprecated since 4.2
*/
public static function execute(int $courseid, string $actionbaseurl): array {
global $DB, $USER, $COURSE;
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
'actionbaseurl' => $actionbaseurl
]
);
$warnings = [];
$context = context_course::instance($params['courseid']);
parent::validate_context($context);
$mappedgroups = [];
$course = $DB->get_record('course', ['id' => $params['courseid']]);
// Initialise the grade tracking object.
if ($groupmode = $course->groupmode) {
$aag = has_capability('moodle/site:accessallgroups', $context);
$usergroups = [];
$groupuserid = 0;
if ($groupmode == VISIBLEGROUPS || $aag) {
// Get user's own groups and put to the top.
$usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
} else {
$groupuserid = $USER->id;
}
$allowedgroups = groups_get_all_groups($course->id, $groupuserid, $course->defaultgroupingid);
$allgroups = array_merge($allowedgroups, $usergroups);
// Filter out any duplicate groups.
$groupsmenu = array_intersect_key($allgroups, array_unique(array_column($allgroups, 'name')));
if (!$allowedgroups || $groupmode == VISIBLEGROUPS || $aag) {
array_unshift($groupsmenu, (object) [
'id' => 0,
'name' => get_string('allparticipants'),
]);
}
$mappedgroups = array_map(function($group) use ($COURSE, $actionbaseurl, $context) {
$url = new \moodle_url($actionbaseurl, [
'id' => $COURSE->id,
'group' => $group->id
]);
return (object) [
'id' => $group->id,
'name' => format_string($group->name, true, ['context' => $context]),
'url' => $url->out(false),
'active' => false
];
}, $groupsmenu);
}
return [
'groups' => $mappedgroups,
'warnings' => $warnings,
];
}
/**
* Returns description of what the group search for the widget should return.
*
* @return external_single_structure
* @deprecated since 4.2
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'groups' => new external_multiple_structure(self::group_description()),
'warnings' => new external_warnings(),
]);
}
/**
* Create group return value description.
*
* @return external_description
*/
public static function group_description(): external_description {
$groupfields = [
'id' => new external_value(PARAM_ALPHANUM, 'An ID for the group', VALUE_REQUIRED),
'url' => new external_value(PARAM_URL, 'The link that applies the group action', VALUE_REQUIRED),
'name' => new external_value(PARAM_TEXT, 'The full name of the group', VALUE_REQUIRED),
'active' => new external_value(PARAM_BOOL, 'Are we currently on this item?', VALUE_REQUIRED)
];
return new external_single_structure($groupfields);
}
/**
* Mark the function as deprecated.
* @return bool
*/
public static function execute_is_deprecated() {
return true;
}
}