-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrol.php
142 lines (116 loc) · 4.09 KB
/
enrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Stripe Membership enrolment plugin enrolment method.
*
* @package enrol_stripe_membership
* @copyright Your Name
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/enrollib.php');
/**
* Stripe Membership enrolment plugin implementation.
*/
class enrol_stripe_membership extends enrol_plugin {
/**
* Add enrolment instance to a course.
*
* @param stdClass $course The course to add instance to.
* @param array $fields Optional fields for enrolment instance.
* @return int ID of new enrolment instance.
*/
public function add_instance($course, array $fields = null) {
global $DB;
$fields = (array)$fields;
$fields['status'] = isset($fields['status']) ? $fields['status'] : ENROL_INSTANCE_ENABLED;
$fields['courseid'] = $course->id;
return $DB->insert_record('enrol', $fields);
}
/**
* Check if a user can enrol.
*
* @param stdClass $instance The enrolment instance.
* @return bool True if user can enrol.
*/
public function can_enrol($instance) {
return true; // Modify based on conditions (e.g., user roles).
}
/**
* Enrol a user into a course.
*
* @param stdClass $instance The enrolment instance.
* @param int $userid User ID.
* @param int|null $roleid Role ID (default: student).
* @param int $timestart Start time for enrolment.
* @param int $timeend End time for enrolment.
* @param int $status Enrolment status (default: ENROL_USER_ACTIVE).
*/
public function enrol_user($instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = ENROL_USER_ACTIVE) {
global $DB;
$context = context_course::instance($instance->courseid);
if ($roleid) {
role_assign($roleid, $userid, $context->id);
}
$ue = $DB->get_record('user_enrolments', [
'enrolid' => $instance->id,
'userid' => $userid
]);
if (!$ue) {
$record = new stdClass();
$record->status = $status;
$record->enrolid = $instance->id;
$record->userid = $userid;
$record->timestart = $timestart;
$record->timeend = $timeend;
$record->timecreated = time();
$record->timemodified = time();
$DB->insert_record('user_enrolments', $record);
}
}
/**
* Unenrol a user from a course.
*
* @param stdClass $instance The enrolment instance.
* @param int $userid User ID.
*/
public function unenrol_user($instance, $userid) {
global $DB;
$context = context_course::instance($instance->courseid);
role_unassign_all(['userid' => $userid, 'contextid' => $context->id], true);
$DB->delete_records('user_enrolments', [
'enrolid' => $instance->id,
'userid' => $userid
]);
}
/**
* Return enrolment instance name for display.
*
* @param stdClass $instance The enrolment instance.
* @return string Enrolment instance name.
*/
public function get_instance_name($instance) {
if (!empty($instance->name)) {
return format_string($instance->name, true, [
'context' => context_course::instance($instance->courseid)
]);
}
return get_string('pluginname', 'enrol_stripe_membership');
}
/**
* Process Stripe webhook notifications.
*
* @param string $payload Raw webhook payload.
*/
public function process_webhook($payload) {
global $DB;
$event = json_decode($payload);
if ($event->type === 'checkout.session.completed') {
$userid = $event->data->object->client_reference_id;
$instanceid = $event->data->object->metadata->instanceid;
$instance = $DB->get_record('enrol', [
'id' => $instanceid
], '*', MUST_EXIST);
$this->enrol_user($instance, $userid, 5); // Default role: student.
}
}
}