Skip to content

Commit

Permalink
fix Moodle 4.1 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed Oct 25, 2024
1 parent 792f8a7 commit 667b133
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 116 deletions.
14 changes: 10 additions & 4 deletions local/o365/classes/webservices/create_onenoteassignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace local_o365\webservices;

use context_course;
use external_api;
use external_function_parameters;
use external_single_structure;
use external_value;

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

global $CFG;
Expand All @@ -34,7 +40,7 @@
/**
* Create assignment API class.
*/
class create_onenoteassignment extends \external_api {
class create_onenoteassignment extends external_api {
/**
* Returns description of method parameters.
*
Expand Down Expand Up @@ -65,7 +71,7 @@ public static function assignment_create($data) {
$params = self::validate_parameters(self::assignment_create_parameters(), ['data' => $data]);
$params = $params['data'];

$context = \context_course::instance($params['course']);
$context = context_course::instance($params['course']);
self::validate_context($context);

$defaults = [
Expand Down Expand Up @@ -105,7 +111,7 @@ public static function assignment_create($data) {
$modinfo = array_merge($defaults, $modinfo);
$modinfo = create_module((object)$modinfo, $course);

$modinfo = \local_o365\webservices\utils::get_assignment_return_info($modinfo->coursemodule, $modinfo->course);
$modinfo = utils::get_assignment_return_info($modinfo->coursemodule, $modinfo->course);
return ['data' => [$modinfo]];
}

Expand All @@ -115,6 +121,6 @@ public static function assignment_create($data) {
* @return external_single_structure Object describing return parameters for this webservice method.
*/
public static function assignment_create_returns() {
return \local_o365\webservices\utils::get_assignment_return_info_schema();
return utils::get_assignment_return_info_schema();
}
}
16 changes: 11 additions & 5 deletions local/o365/classes/webservices/delete_onenoteassignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace local_o365\webservices;

use context_course;
use external_api;
use external_function_parameters;
use external_single_structure;
use external_value;

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

global $CFG;
Expand All @@ -34,7 +40,7 @@
/**
* Delete assignment API class.
*/
class delete_onenoteassignment extends \external_api {
class delete_onenoteassignment extends external_api {
/**
* Returns description of method parameters.
*
Expand All @@ -61,10 +67,10 @@ public static function assignment_delete($data) {
$params = self::validate_parameters(self::assignment_delete_parameters(), ['data' => $data]);
$params = $params['data'];

list($course, $module, $assign) = \local_o365\webservices\utils::verify_assignment($params['coursemodule'],
[$course, $module, $assign] = utils::verify_assignment($params['coursemodule'],
$params['course']);

$context = \context_course::instance($params['course']);
$context = context_course::instance($params['course']);
self::validate_context($context);

// Course_delete_module will throw exception if error, so we can return true b/c if we get there it was successful.
Expand All @@ -79,8 +85,8 @@ public static function assignment_delete($data) {
*/
public static function assignment_delete_returns() {
$params = [
'result' => new \external_value(PARAM_BOOL, 'success/failure'),
'result' => new external_value(PARAM_BOOL, 'success/failure'),
];
return new \external_single_structure($params);
return new external_single_structure($params);
}
}
39 changes: 23 additions & 16 deletions local/o365/classes/webservices/read_assignments.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@

namespace local_o365\webservices;

use assign;
use context_course;
use context_module;
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use external_warnings;
use moodle_exception;
use moodle_url;

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

Expand All @@ -39,35 +49,35 @@
/**
* Get a list of assignments in one or more courses.
*/
class read_assignments extends \external_api {
class read_assignments extends external_api {

/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function assignments_read_parameters() {
return new \external_function_parameters([
'courseids' => new \external_multiple_structure(
new \external_value(PARAM_INT, 'course id, empty for retrieving all the courses where the user is enroled in'),
return new external_function_parameters([
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'course id, empty for retrieving all the courses where the user is enroled in'),
'0 or more course ids',
VALUE_DEFAULT,
[]
),
'assignmentids' => new \external_multiple_structure(
new \external_value(PARAM_INT,
'assignmentids' => new external_multiple_structure(
new external_value(PARAM_INT,
'assignment id, empty for retrieving all assignments for courses the user is enroled in'),
'0 or more assignment ids',
VALUE_DEFAULT,
[]
),
'capabilities' => new \external_multiple_structure(
new \external_value(PARAM_CAPABILITY, 'capability'),
'capabilities' => new external_multiple_structure(
new external_value(PARAM_CAPABILITY, 'capability'),
'list of capabilities used to filter courses',
VALUE_DEFAULT,
[]
),
'includenotenrolledcourses' => new \external_value(
'includenotenrolledcourses' => new external_value(
PARAM_BOOL,
'whether to return courses that the user can see even if is not enroled in. This requires the parameter ' .
'courseids to not be empty.',
Expand Down Expand Up @@ -140,7 +150,7 @@ public static function assignments_read($courseids = [], $assignmentids = [], $c
foreach ($courseids as $cid) {

try {
$context = \context_course::instance($cid);
$context = context_course::instance($cid);
self::validate_context($context);

// Check if this course was already loaded (by enrol_get_users_courses).
Expand Down Expand Up @@ -193,13 +203,12 @@ public static function assignments_read($courseids = [], $assignmentids = [], $c
// Get a list of assignments for the course.
if ($modules = get_coursemodules_in_course('assign', $courses[$id]->id, $extrafields)) {
foreach ($modules as $module) {

// Check assignment ID filter.
if (!empty($assignmentids) && !isset($assignmentids[$module->assignmentid])) {
continue;
}

$context = \context_module::instance($module->id);
$context = context_module::instance($module->id);
try {
self::validate_context($context);
require_capability('mod/assign:view', $context);
Expand Down Expand Up @@ -256,10 +265,9 @@ public static function assignments_read($courseids = [], $assignmentids = [], $c
];

// Return or not intro and file attachments depending on the plugin settings.
$assign = new \assign($context, null, null);
$assign = new assign($context, null, null);

if ($assign->show_intro()) {

[$assignment['intro'], $assignment['introformat']] = external_format_text($module->intro,
$module->introformat, $context->id, 'mod_assign', 'intro', null);

Expand All @@ -274,7 +282,7 @@ public static function assignments_read($courseids = [], $assignmentids = [], $c
$assignment['introattachments'][] = [
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => \moodle_url::make_webservice_pluginfile_url(
'fileurl' => moodle_url::make_webservice_pluginfile_url(
$context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0, '/', $filename)->out(false),
];
}
Expand Down Expand Up @@ -404,5 +412,4 @@ public static function assignments_read_returns() {
]
);
}

}
61 changes: 34 additions & 27 deletions local/o365/classes/webservices/read_courseusers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,39 @@

namespace local_o365\webservices;

use context_course;
use context_helper;
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use moodle_exception;
use stdClass;

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

global $CFG;

require_once($CFG->dirroot.'/course/modlib.php');
require_once($CFG->dirroot . '/course/modlib.php');

/**
* Get a list of students in a course by course id.
*/
class read_courseusers extends \external_api {
class read_courseusers extends external_api {
/**
* Return description of method parameters.
*
* @return \external_function_parameters
* @return external_function_parameters
*/
public static function courseusers_read_parameters() {
return new \external_function_parameters(
return new external_function_parameters(
[
'courseid' => new \external_value(PARAM_INT, 'course id'),
'limitfrom' => new \external_value(PARAM_INT, 'sql limit from', VALUE_DEFAULT, 0),
'limitnumber' => new \external_value(PARAM_INT, 'maximum number of returned users', VALUE_DEFAULT, 0),
'userids' => new \external_multiple_structure(
new \external_value(PARAM_INT, 'user id, empty to retrieve all users'),
'courseid' => new external_value(PARAM_INT, 'course id'),
'limitfrom' => new external_value(PARAM_INT, 'sql limit from', VALUE_DEFAULT, 0),
'limitnumber' => new external_value(PARAM_INT, 'maximum number of returned users', VALUE_DEFAULT, 0),
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'user id, empty to retrieve all users'),
'0 or more user ids',
VALUE_DEFAULT,
[]
Expand All @@ -71,7 +78,7 @@ public static function courseusers_read_parameters() {
*/
public static function courseusers_read($courseid, $limitfrom = 0, $limitnumber = 0, $userids = []) {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot . '/user/lib.php');

$params = self::validate_parameters(
self::courseusers_read_parameters(),
Expand Down Expand Up @@ -106,7 +113,7 @@ public static function courseusers_read($courseid, $limitfrom = 0, $limitnumber
return [];
}
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$context = \context_course::instance($courseid);
$context = context_course::instance($courseid);
self::validate_context($context);

try {
Expand All @@ -115,15 +122,15 @@ public static function courseusers_read($courseid, $limitfrom = 0, $limitnumber
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $params['courseid'];
throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
throw new moodle_exception('errorcoursecontextnotvalid', 'webservice', '', $exceptionparam);
}

require_capability('moodle/course:viewparticipants', $context);

[$enrolledsql, $enrolledparams] = get_enrolled_sql($context, $withcapability);

// For user context preloading.
$ctxselect = ', ' . \context_helper::get_preload_record_columns_sql('ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
$enrolledparams['contextlevel'] = CONTEXT_USER;

Expand Down Expand Up @@ -160,7 +167,7 @@ public static function courseusers_read($courseid, $limitfrom = 0, $limitnumber
}

// Get user info.
\context_helper::preload_from_record($user);
context_helper::preload_from_record($user);
if ($userdetails = user_get_user_details($user, $course, $userfields)) {
$users[] = $userdetails;
}
Expand All @@ -173,26 +180,26 @@ public static function courseusers_read($courseid, $limitfrom = 0, $limitnumber
/**
* Returns description of method result value
*
* @return external_description
* @return external_multiple_structure
*/
public static function courseusers_read_returns() {
return new \external_multiple_structure(
new \external_single_structure(
return new external_multiple_structure(
new external_single_structure(
[
'id' => new \external_value(PARAM_INT, 'ID of the user'),
'username' => new \external_value(PARAM_RAW, 'Username policy is defined in Moodle security config',
'id' => new external_value(PARAM_INT, 'ID of the user'),
'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config',
VALUE_OPTIONAL),
'fullname' => new \external_value(PARAM_NOTAGS, 'The fullname of the user'),
'firstname' => new \external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new \external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
'email' => new \external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
'idnumber' => new \external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
VALUE_OPTIONAL),
'lang' => new \external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
'lang' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
VALUE_OPTIONAL),
'profileimageurlsmall' => new \external_value(PARAM_URL, 'User image profile URL - small version',
'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version',
VALUE_OPTIONAL),
'profileimageurl' => new \external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL),
'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL),
]
)
);
Expand Down
16 changes: 11 additions & 5 deletions local/o365/classes/webservices/read_onenoteassignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace local_o365\webservices;

use context_course;
use external_api;
use external_function_parameters;
use external_single_structure;
use external_value;

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

global $CFG;
Expand All @@ -34,7 +40,7 @@
/**
* Read assignment API class.
*/
class read_onenoteassignment extends \external_api {
class read_onenoteassignment extends external_api {
/**
* Returns description of method parameters.
*
Expand All @@ -58,13 +64,13 @@ public static function assignment_read_parameters() {
public static function assignment_read($data) {
$params = self::validate_parameters(self::assignment_read_parameters(), ['data' => $data]);
$params = $params['data'];
list($course, $module, $assign) = \local_o365\webservices\utils::verify_assignment($params['coursemodule'],
[$course, $module, $assign] = utils::verify_assignment($params['coursemodule'],
$params['course']);

$context = \context_course::instance($params['course']);
$context = context_course::instance($params['course']);
self::validate_context($context);

$modinfo = \local_o365\webservices\utils::get_assignment_return_info($module->id, $course->id);
$modinfo = utils::get_assignment_return_info($module->id, $course->id);
return ['data' => [$modinfo]];
}

Expand All @@ -74,6 +80,6 @@ public static function assignment_read($data) {
* @return external_single_structure Object describing return parameters for this webservice method.
*/
public static function assignment_read_returns() {
return \local_o365\webservices\utils::get_assignment_return_info_schema();
return utils::get_assignment_return_info_schema();
}
}
Loading

0 comments on commit 667b133

Please sign in to comment.