Skip to content

Commit

Permalink
MDL-71571 enrol_meta: Add webservices for adding and deleting instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabalin committed Sep 29, 2021
1 parent 214adb7 commit 80bcf0d
Show file tree
Hide file tree
Showing 7 changed files with 645 additions and 1 deletion.
164 changes: 164 additions & 0 deletions enrol/meta/classes/external/add_instances.php
@@ -0,0 +1,164 @@
<?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 enrol_meta\external;

use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use invalid_parameter_exception;
use context_course;
use moodle_exception;

/**
* Web service function relating to add enrol meta instances
*
* @package enrol_meta
* @copyright 2021 WKS KV Bildung
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_instances extends external_api {

/**
* Parameters for adding meta enrolment instances
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'instances' => new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
'creategroup' => new external_value(PARAM_BOOL,
'Creates group in meta course named after linked course and '
. 'puts all enrolled users in this group', VALUE_DEFAULT, false),
]
), 'List of course meta enrolment instances to create.', VALUE_DEFAULT, []
),
]);
}

/**
* Adding meta enrolment instances
*
* @param array $instances
* @return array
*/
public static function execute(array $instances): array {
global $DB;
// Parameter validation.
$params = self::validate_parameters(self::execute_parameters(), [
'instances' => $instances,
]);

if (!count($params['instances'])) {
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
}

$result = [];
foreach ($params['instances'] as $instance) {
// Ensure the metacourse exists.
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
if (!$metacourserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the current user is allowed to access metacourse.
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
try {
self::validate_context($contextmeta);
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}

// Ensure the linked course exists.
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
if (!$courserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}

// Ensure the current user is allowed to access linked course.
$context = context_course::instance($instance['courseid'], IGNORE_MISSING);
try {
self::validate_context($context);
if (!$courserecord->visible) {
require_capability('moodle/course:viewhiddencourses', $context);
}
require_capability('enrol/meta:selectaslinked', $context);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}

// Check for existing meta course link.
$enrolrecord = $DB->get_record('enrol',
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
if ($enrolrecord) {
// Link exists.
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => false,
];
continue;
}

// Check for permission to create group.
if ($instance['creategroup']) {
try {
require_capability('moodle/course:managegroups', $context);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wscannotcreategroup', 'enrol_meta', $instance['courseid']));
}
}

// Create instance.
$enrolplugin = enrol_get_plugin('meta');
$fields = [
'customint1' => $instance['courseid'],
'customint2' => $instance['creategroup'] ? ENROL_META_CREATE_GROUP : 0,
];
$addresult = $enrolplugin->add_instance($metacourserecord, $fields);
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => (bool) $addresult,
];
}

return $result;
}

/**
* Return for adding enrolment instances.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
'status' => new external_value(PARAM_BOOL, 'True on success, false if link already exists.'),
]
), 'List of course meta enrolment instances that were created.', VALUE_DEFAULT, []
);
}
}
138 changes: 138 additions & 0 deletions enrol/meta/classes/external/delete_instances.php
@@ -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 enrol_meta\external;

use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use invalid_parameter_exception;
use context_course;
use moodle_exception;

/**
* Web service function relating to add enrol meta instances
*
* @package enrol_meta
* @copyright 2021 WKS KV Bildung
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_instances extends external_api {

/**
* Parameters for deleting meta enrolment instances
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'instances' => new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course with meta enrolment.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
]
), 'List of course meta enrolment instances to delete.', VALUE_DEFAULT, []
),
]);
}

/**
* Deleting meta enrolment instances
*
* @param array $instances
* @return array
*/
public static function execute(array $instances): array {
global $DB;
// Parameter validation.
$params = self::validate_parameters(self::execute_parameters(), [
'instances' => $instances,
]);

if (!count($params['instances'])) {
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
}

$result = [];
foreach ($params['instances'] as $instance) {
// Ensure the metacourse exists.
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
if (!$metacourserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the current user is allowed to access metacourse.
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
try {
self::validate_context($contextmeta);
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}

// Ensure the linked course exists.
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
if (!$courserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}
// It is probably needed to check if user is allowed to access linked course.
// but can_delete_instance does not do that, so we stop permission check here.

// Check for existing meta course link.
$enrolrecord = $DB->get_record('enrol',
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
if ($enrolrecord) {
// Link exists. Delete instance.
$enrolplugin = enrol_get_plugin('meta');
$enrolplugin->delete_instance($enrolrecord);

$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => true,
];
continue;
}
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => false,
];
}

return $result;
}

/**
* Return for deleting enrolment instances.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is deleted.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course that was meta linked.'),
'status' => new external_value(PARAM_BOOL, 'True on success, false if meta link did not exist.'),

]
), 'List of course meta enrolment instances that were deleted.', VALUE_DEFAULT, []
);
}
}
46 changes: 46 additions & 0 deletions enrol/meta/db/services.php
@@ -0,0 +1,46 @@
<?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/>.

/**
* Meta enrol external functions and service definitions.
*
* @package enrol_meta
* @copyright 2021 WKS KV Bildung
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

$functions = [
'enrol_meta_add_instances' => [
'classname' => \enrol_meta\external\add_instances::class,
'methodname' => 'execute',
'description' => 'Add meta enrolment instances',
'capabilities' => 'enrol/meta:config',
'type' => 'write',
'ajax' => true,
'loginrequired' => true,
],
'enrol_meta_delete_instances' => [
'classname' => \enrol_meta\external\delete_instances::class,
'methodname' => 'execute',
'description' => 'Delete meta enrolment instances',
'capabilities' => 'enrol/meta:config',
'type' => 'write',
'ajax' => true,
'loginrequired' => true,
],
];
4 changes: 4 additions & 0 deletions enrol/meta/lang/en/enrol_meta.php
Expand Up @@ -39,3 +39,7 @@
$string['syncall'] = 'Synchronise all enrolled users';
$string['syncall_desc'] = 'If enabled all enrolled users are synchronised even if they have no role in parent course, if disabled only users that have at least one synchronised role are enrolled in child course.';
$string['privacy:metadata:core_group'] = 'Enrol meta plugin can create a new group or use an existing group to add all the participants of the course linked.';
$string['wscannotcreategroup'] = 'No permission to create group in linked course id = {$a}.';
$string['wsinvalidcourse'] = 'Course id = {$a} does not exist or no permission to link in meta enrolment.';
$string['wsinvalidmetacourse'] = 'Meta course id = {$a} does not exist or no permission to add enrolment instance.';
$string['wsnoinstancesspecified'] = 'No instances specified';

0 comments on commit 80bcf0d

Please sign in to comment.