Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
evsoldatkin committed May 17, 2023
0 parents commit af2dc1b
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 0 deletions.
26 changes: 26 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
defined('MOODLE_INTERNAL') || die();

$capabilities = [
'enrol/cito1c:enrol' => [
'archetypes' => [
'manager' => CAP_ALLOW
],
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE
],
'enrol/cito1c:manage' => [
'archetypes' => [
'manager' => CAP_ALLOW
],
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE
],
'enrol/cito1c:unenrol' => [
'archetypes' => [
'manager' => CAP_ALLOW
],
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE
]
];
28 changes: 28 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
$functions = [
'enrol_cito1c_enrol_users' => [
'classname' => 'enrol_cito1c_external',
'methodname' => 'enrol_users',
'classpath' => 'enrol/cito1c/externallib.php',
'description' => 'Enrol users from 1C',
'capabilities'=> 'enrol/cito1c:enrol',
'type' => 'write',
],
'enrol_cito1c_unenrol_users' => [
'classname' => 'enrol_cito1c_external',
'methodname' => 'unenrol_users',
'classpath' => 'enrol/cito1c/externallib.php',
'description' => 'Unenrol users from 1C',
'capabilities'=> 'enrol/cito1c:unenrol',
'type' => 'write',
]
];

$services = [
'enrol_cito1c_web_services' => [
'functions' => array_keys($functions),
'restrictedusers' => 1,
'enabled' => 1,
'shortname' => 'cito1c'
]
];
128 changes: 128 additions & 0 deletions externallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
defined('MOODLE_INTERNAL') || die();

require_once $CFG->libdir.'/externallib.php';

class enrol_cito1c_external extends external_api
{
public static function enrol_users_parameters()
{
return new external_function_parameters([
'enrolments' => new external_multiple_structure(
new external_single_structure([
'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'),
'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL),
'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL),
'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL),
'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
])
)
]);
}

public static function enrol_users($enrolments)
{
global $DB, $CFG;
require_once $CFG->libdir.'/enrollib.php';
$params = self::validate_parameters(
self::enrol_users_parameters(),
['enrolments' => $enrolments]
);
$transaction = $DB->start_delegated_transaction();
$enrol = enrol_get_plugin('cito1c');
if (empty($enrol))
throw new moodle_exception('pluginnotinstalled', 'enrol_cito1c');
foreach ($params['enrolments'] as $enrolment)
{
$context = context_course::instance($enrolment['courseid'], IGNORE_MISSING);
self::validate_context($context);
require_capability('enrol/cito1c:enrol', $context);
$roles = get_assignable_roles($context);
if (!array_key_exists($enrolment['roleid'], $roles))
{
$errorparams = new stdClass();
$errorparams->courseid = $enrolment['courseid'];
$errorparams->roleid = $enrolment['roleid'];
$errorparams->userid = $enrolment['userid'];
throw new moodle_exception('wsusercannotassign', 'enrol_cito1c', '', $errorparams);
}
$instance = $enrol->enrol_get_instance($enrolment['courseid']);
if (empty($instance))
{
$course = new \stdClass();
$course->id = $enrolment['courseid'];
$enrol->add_instance($course);
$instance = $enrol->enrol_get_instance($enrolment['courseid']);
}
$enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend']))
? ENROL_USER_SUSPENDED
: ENROL_USER_ACTIVE;
$enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0;
$enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0;
$enrol->enrol_user(
$instance,
$enrolment['userid'],
$enrolment['roleid'],
$enrolment['timestart'],
$enrolment['timeend'],
$enrolment['status']
);
}
$transaction->allow_commit();
}

public static function enrol_users_returns()
{
return null;
}

public static function unenrol_users_parameters()
{
return new external_function_parameters([
'enrolments' => new external_multiple_structure(
new external_single_structure([
'courseid' => new external_value(PARAM_INT, 'The course to unenrol the user from'),
'roleid' => new external_value(PARAM_INT, 'The user role', VALUE_OPTIONAL),
'userid' => new external_value(PARAM_INT, 'The user that is going to be unenrolled')
])
)
]);
}

public static function unenrol_users($enrolments)
{
global $CFG, $DB;
require_once $CFG->libdir.'/enrollib.php';
$params = self::validate_parameters(
self::unenrol_users_parameters(),
['enrolments' => $enrolments]
);
$transaction = $DB->start_delegated_transaction();
$enrol = enrol_get_plugin('cito1c');
if (empty($enrol))
throw new moodle_exception('pluginnotinstalled', 'enrol_cito1c');
foreach ($params['enrolments'] as $enrolment)
{
$context = context_course::instance($enrolment['courseid']);
self::validate_context($context);
require_capability('enrol/cito1c:unenrol', $context);
$instance = $DB->get_record('enrol', [
'courseid' => $enrolment['courseid'],
'enrol' => 'cito1c'
]);
if (!$instance)
throw new moodle_exception('wsnoinstance', 'enrol_cito1c', $enrolment);
$user = $DB->get_record('user', ['id' => $enrolment['userid']]);
if (!$user)
throw new invalid_parameter_exception('User id not exist: '.$enrolment['userid']);
$enrol->unenrol_user($instance, $enrolment['userid']);
}
$transaction->allow_commit();
}

public static function unenrol_users_returns()
{
return null;
}
}
7 changes: 7 additions & 0 deletions lang/en/enrol_cito1c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
defined('MOODLE_INTERNAL') || die();

$string['pluginname'] = 'cito-1C-enrol';
$string['pluginnotinstalled'] = 'The "cito-1C-enrol" plugin has not yet been installed';
$string['wsnoinstance'] = 'Enrolment plugin instance doesn\'t exist or is disabled for the course (id = {$a->courseid})';
$string['wsusercannotassign'] = 'You don\'t have the permission to assign this role ({$a->roleid}) to this user ({$a->userid}) in this course ({$a->courseid}).';
4 changes: 4 additions & 0 deletions lang/ru/enrol_cito1c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
defined('MOODLE_INTERNAL') || die();

$string['pluginname'] = 'ЦИТО-1С-Зачисление';
71 changes: 71 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
defined('MOODLE_INTERNAL') || die();

class enrol_cito1c_plugin extends enrol_plugin
{
public function can_delete_instance($instance)
{
$context = context_course::instance($instance->courseid);
if (!has_capability('enrol/cito1c:manage', $context))
return false;
if (!enrol_is_enabled('cito1c'))
return true;
return false;
}

public function can_hide_show_instance($instance)
{
$context = context_course::instance($instance->courseid);
return has_capability('enrol/cito1c:manage', $context);
}

public function enrol_get_instance($courseid)
{
$instance = null;
$enrolinstances = enrol_get_instances($courseid, true);
foreach ($enrolinstances as $courseenrolinstance)
{
if ($courseenrolinstance->enrol == 'cito1c')
{
$instance = $courseenrolinstance;
break;
}
}
return $instance;
}

public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid)
{
global $DB;
$record = $DB->get_record('enrol', [
'courseid' => $data->courseid,
'enrol' => 'cito1c'
]);
$newid = $instances
? $instance->id
: $this->add_instance($course, (array)$data);
$step->set_mapping('enrol', $oldid, $newid);
}

public function restore_role_assignment($instance, $roleid, $userid, $contextid)
{
global $DB;
$exists = $DB->record_exists('user_enrolments', [
'enrolid' => $instance->id,
'userid' => $userid
]);
if ($exists)
role_assign($roleid, $userid, $contextid, 'enrol_'.$instance->enrol, $instance->id);
}

public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus)
{
global $DB;
$exists = $DB->record_exists('user_enrolments', [
'enrolid' => $instance->id,
'userid' => $userid
]);
if (!$exists)
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status);
}
}
24 changes: 24 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
require_once '../../config.php';
global $CFG, $USER;
require_once $CFG->dirroot.'/lib/externallib.php';
$USER->ignoresesskey = true;
$params = [
'courseid' => required_param('courseid', PARAM_INT),
'roleid' => 3,
'userid' => required_param('userid', PARAM_INT)
];
$params = [
'enrolments' => [$params]
];
$result = \core_external\external_api::call_external_function('enrol_cito1c_enrol_users', $params);
if ($result['error'] === false)
{
$result = \core_external\external_api::call_external_function('enrol_cito1c_unenrol_users', $params);
if ($result['error'] === false)
{
echo 'Test passed';
die;
}
}
echo 'Test failed';
6 changes: 6 additions & 0 deletions version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'enrol_cito1c';
$plugin->requires = 2018050800;
$plugin->version = 2023051702;

0 comments on commit af2dc1b

Please sign in to comment.