-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-MDL-76126-course-Add-four-callbacks-for-course-forms.patch
173 lines (159 loc) · 5.81 KB
/
0001-MDL-76126-course-Add-four-callbacks-for-course-forms.patch
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
From ae60797474d3f0d8db6c89a7166561d922550d89 Mon Sep 17 00:00:00 2001
From: James Williams <[email protected]>
Date: Fri, 30 Jun 2023 22:58:46 +1000
Subject: [PATCH] MDL-76126 course: Add four callbacks for course forms like
MDL-52534.
This adds the following callbacks for plugin use with courses:
- course_edit_post_actions
- course_standard_elements
- course_definition_after_data
- course_validation
This also adds an additional get_course() getter to the course_edit_form
class, to mimic the same functionality in an otherwise cut-down class by
comparison.
---
course/edit_form.php | 67 ++++++++++++++++++++++++++++++++++++++++++++
course/lib.php | 21 ++++++++++++++
2 files changed, 88 insertions(+)
diff --git a/course/edit_form.php b/course/edit_form.php
index d0b50f3ab79..271a0624eb7 100644
--- a/course/edit_form.php
+++ b/course/edit_form.php
@@ -13,6 +13,14 @@ class course_edit_form extends moodleform {
protected $course;
protected $context;
+ /**
+ * Get the course.
+ * @return course
+ */
+ public function get_course() {
+ return $this->course;
+ }
+
/**
* Form definition.
*/
@@ -387,6 +395,9 @@ class course_edit_form extends moodleform {
$handler->set_parent_context($categorycontext); // For course handler only.
$handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
+ // Apply any plugin extensions to this course.
+ $this->plugin_extend_course_standard_elements();
+
// Add communication plugins to the form.
if (core_communication\api::is_available()) {
@@ -426,10 +437,23 @@ class course_edit_form extends moodleform {
// Prepare custom fields data.
$handler->instance_form_before_set_data($course);
+
// Finally set the current form data
$this->set_data($course);
}
+ /**
+ * Plugins can extend the course settings form.
+ */
+ protected function plugin_extend_course_standard_elements() {
+ $callbacks = get_plugins_with_function('course_standard_elements', 'lib.php');
+ foreach ($callbacks as $type => $plugins) {
+ foreach ($plugins as $plugin => $pluginfunction) {
+ $pluginfunction($this, $this->_form);
+ }
+ }
+ }
+
/**
* Fill in the current page data for this course.
*/
@@ -489,6 +513,21 @@ class course_edit_form extends moodleform {
);
$communication->form_definition_for_provider($mform);
}
+
+ // Apply any plugin extensions to this course.
+ $this->plugin_extend_course_definition_after_data();
+ }
+
+ /**
+ * Plugins can extend the course settings form after the data is set.
+ */
+ protected function plugin_extend_course_definition_after_data() {
+ $callbacks = get_plugins_with_function('course_definition_after_data', 'lib.php');
+ foreach ($callbacks as $type => $plugins) {
+ foreach ($plugins as $plugin => $pluginfunction) {
+ $pluginfunction($this, $this->_form);
+ }
+ }
}
/**
@@ -535,6 +574,34 @@ class course_edit_form extends moodleform {
$handler = core_course\customfield\course_handler::create();
$errors = array_merge($errors, $handler->instance_form_validation($data, $files));
+ // Allow plugins to extend the course fields validation.
+ $pluginerrors = $this->plugin_extend_course_validation($data);
+ if (!empty($pluginerrors)) {
+ $errors = array_merge($errors, $pluginerrors);
+ }
+
+ return $errors;
+ }
+
+ /**
+ * Extend the validation function from any other plugin.
+ *
+ * @param stdClass $data The form data.
+ * @return array $errors The list of errors keyed by element name.
+ */
+ protected function plugin_extend_course_validation($data) {
+ $errors = array();
+
+ $callbacks = get_plugins_with_function('course_validation', 'lib.php');
+ foreach ($callbacks as $type => $plugins) {
+ foreach ($plugins as $plugin => $pluginfunction) {
+ // We have exposed all the important properties with public getters - the errors array should be pass by reference.
+ $pluginerrors = $pluginfunction($this, $data);
+ if (!empty($pluginerrors)) {
+ $errors = array_merge($errors, $pluginerrors);
+ }
+ }
+ }
return $errors;
}
}
diff --git a/course/lib.php b/course/lib.php
index 89b3c638997..407bf5f35c0 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -2516,6 +2516,9 @@ function update_course($data, $editoroptions = NULL) {
$handler = core_course\customfield\course_handler::create();
$handler->instance_form_save($data);
+ // Allow plugins to extend the course data/oldcourse and handle form data.
+ $data = plugin_extend_course_edit_post_actions($data, $oldcourse);
+
// Update with the new data
$DB->update_record('course', $data);
// make sure the modinfo cache is reset
@@ -2578,6 +2581,24 @@ function update_course($data, $editoroptions = NULL) {
}
}
+/**
+ * Hook for plugins to take action when a course is created or updated.
+ *
+ * @param stdClass $data the module info
+ * @param stdClass $oldcourse the previous course object source data
+ *
+ * @return stdClass data updated by plugins.
+ */
+function plugin_extend_course_edit_post_actions($data, $oldcourse) {
+ $callbacks = get_plugins_with_function('course_edit_post_actions', 'lib.php');
+ foreach ($callbacks as $type => $plugins) {
+ foreach ($plugins as $plugin => $pluginfunction) {
+ $data = $pluginfunction($data, $oldcourse);
+ }
+ }
+ return $data;
+}
+
/**
* Calculate the average number of enrolled participants per course.
*
--
2.34.1