Skip to content
Permalink
Browse files
Merge branch 'MDL-60579-master' of https://github.com/mihailges/moodle
  • Loading branch information
David Monllao committed Nov 8, 2017
2 parents fcafb22 + c835126 commit 4e73acf3cf583bb9f4a51b52e6018d3cd0ef4a55
@@ -909,6 +909,89 @@ public static function edit_user_enrolment_returns() {
)
);
}

/**
* Returns description of unenrol_user_enrolment() parameters
*
* @return external_function_parameters
*/
public static function unenrol_user_enrolment_parameters() {
return new external_function_parameters(
array(
'ueid' => new external_value(PARAM_INT, 'User enrolment ID')
)
);
}

/**
* External function that unenrols a given user enrolment.
*
* @param int $ueid The user enrolment ID.
* @return array An array consisting of the processing result, errors.
*/
public static function unenrol_user_enrolment($ueid) {
global $CFG, $DB, $PAGE;

$params = self::validate_parameters(self::unenrol_user_enrolment_parameters(), [
'ueid' => $ueid
]);

$result = false;
$errors = [];

$userenrolment = $DB->get_record('user_enrolments', ['id' => $params['ueid']], '*');
if ($userenrolment) {
$userid = $userenrolment->userid;
$enrolid = $userenrolment->enrolid;
$enrol = $DB->get_record('enrol', ['id' => $enrolid], '*', MUST_EXIST);
$courseid = $enrol->courseid;
$course = get_course($courseid);
$context = context_course::instance($course->id);
self::validate_context($context);
} else {
$validationerrors['invalidrequest'] = get_string('invalidrequest', 'enrol');
}

// If the userenrolment exists, unenrol the user.
if (!isset($validationerrors)) {
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$result = $manager->unenrol_user($userenrolment);
} else {
foreach ($validationerrors as $key => $errormessage) {
$errors[] = (object)[
'key' => $key,
'message' => $errormessage
];
}
}

return [
'result' => $result,
'errors' => $errors,
];
}

/**
* Returns description of unenrol_user_enrolment() result value
*
* @return external_description
*/
public static function unenrol_user_enrolment_returns() {
return new external_single_structure(
array(
'result' => new external_value(PARAM_BOOL, 'True if the user\'s enrolment was successfully updated'),
'errors' => new external_multiple_structure(
new external_single_structure(
array(
'key' => new external_value(PARAM_TEXT, 'The data that failed the validation'),
'message' => new external_value(PARAM_TEXT, 'The error message'),
)
), 'List of validation errors'
),
)
);
}
}

/**
@@ -764,4 +764,69 @@ public function test_edit_user_enrolment() {
$ue = $DB->get_record('user_enrolments', ['id' => $ueid], '*', MUST_EXIST);
$this->assertEquals(ENROL_USER_SUSPENDED, $ue->status);
}

/**
* Test for core_enrol_external::unenrol_user_enrolment().
*/
public function test_unenerol_user_enrolment() {
global $DB;

$this->resetAfterTest(true);
$datagen = $this->getDataGenerator();

/** @var enrol_manual_plugin $manualplugin */
$manualplugin = enrol_get_plugin('manual');
$this->assertNotNull($manualplugin);

$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
$teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST);
$course = $datagen->create_course();
$user = $datagen->create_user();
$teacher = $datagen->create_user();

$instanceid = null;
$instances = enrol_get_instances($course->id, true);
foreach ($instances as $inst) {
if ($inst->enrol == 'manual') {
$instanceid = (int)$inst->id;
break;
}
}
if (empty($instanceid)) {
$instanceid = $manualplugin->add_default_instance($course);
if (empty($instanceid)) {
$instanceid = $manualplugin->add_instance($course);
}
}
$this->assertNotNull($instanceid);

$instance = $DB->get_record('enrol', ['id' => $instanceid], '*', MUST_EXIST);
$manualplugin->enrol_user($instance, $user->id, $studentroleid, 0, 0, ENROL_USER_ACTIVE);
$manualplugin->enrol_user($instance, $teacher->id, $teacherroleid, 0, 0, ENROL_USER_ACTIVE);
$ueid = (int)$DB->get_field(
'user_enrolments',
'id',
['enrolid' => $instance->id, 'userid' => $user->id],
MUST_EXIST
);

// Login as teacher.
$this->setUser($teacher);

// Invalid data by passing invalid ueid.
$data = core_enrol_external::unenrol_user_enrolment(101010);
$data = external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data);
$this->assertFalse($data['result']);
$this->assertNotEmpty($data['errors']);

// Valid data.
$data = core_enrol_external::unenrol_user_enrolment($ueid);
$data = external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data);
$this->assertTrue($data['result']);
$this->assertEmpty($data['errors']);

// Check unenrol user enrolment.
$ue = $DB->count_records('user_enrolments', ['id' => $ueid]);
$this->assertEquals(0, $ue);
}
}
@@ -96,6 +96,7 @@
$string['instanceeditselfwarningtext'] = 'You are enrolled into this course through this enrolment method, changes may affect your access to this course.';
$string['invalidenrolinstance'] = 'Invalid enrolment instance';
$string['invalidrole'] = 'Invalid role';
$string['invalidrequest'] = 'Invalid request';
$string['manageenrols'] = 'Manage enrol plugins';
$string['manageinstance'] = 'Manage';
$string['migratetomanual'] = 'Migrate to manual enrolments';
@@ -546,6 +546,14 @@
'type' => 'write',
'ajax' => true,
),
'core_enrol_unenrol_user_enrolment' => array(
'classname' => 'core_enrol_external',
'methodname' => 'unenrol_user_enrolment',
'classpath' => 'enrol/externallib.php',
'description' => 'External function that unenrols a given user enrolment',
'type' => 'write',
'ajax' => true,
),
'core_fetch_notifications' => array(
'classname' => 'core_external',
'methodname' => 'fetch_notifications',

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,64 @@
// 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/>.

/**
* Name and page filter JS module for the course participants page.
*
* @module core_user/name_page_filter
* @package core_user
* @copyright 2017 Mihail Geshoski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core_user/unified_filter'],
function($, UnifiedFilter) {

/**
* Selectors.
*
* @access private
* @type {{NAME_FILTERS: string, PAGE_FILTERS: string}}
*/
var SELECTORS = {
NAME_FILTERS: 'a.letter',
PAGE_FILTERS: 'a.page-link'
};

/**
* Init function.
*
* @method init
* @private
*/
var init = function() {
$(SELECTORS.NAME_FILTERS + ', ' + SELECTORS.PAGE_FILTERS).on('click', function(e) {
e.preventDefault();
var href = $(this).attr('href');
UnifiedFilter.getForm().attr('action', href);
UnifiedFilter.getForm().submit();
});
};

return /** @alias module:core/form-autocomplete */ {
// Public variables and functions.
/**
* Initialise the name and page user filter.
*
* @method init
*/
'init': function() {
init();
}
};
});

0 comments on commit 4e73acf

Please sign in to comment.