14
14
// You should have received a copy of the GNU General Public License
15
15
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
16
17
+ /**
18
+ * Step for backing up a course logs in the lifecycle process.
19
+ *
20
+ * @package tool_lcbackupcourselogstep
21
+ * @copyright 2024 Catalyst
22
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
+ */
24
+
17
25
namespace tool_lcbackupcourselogstep \lifecycle ;
18
26
27
+ defined ('MOODLE_INTERNAL ' ) || die ();
28
+
19
29
global $ CFG ;
20
30
require_once ($ CFG ->dirroot . '/admin/tool/lifecycle/step/lib.php ' );
21
31
28
38
use tool_lifecycle \step \instance_setting ;
29
39
use tool_lifecycle \step \libbase ;
30
40
31
- defined ('MOODLE_INTERNAL ' ) || die ();
32
-
41
+ /**
42
+ * Step class for the Backup Course Log Step plugin.
43
+ *
44
+ * @package tool_lcbackupcourselogstep
45
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
+ */
33
47
class step extends libbase {
34
- public function get_subpluginname ()
35
- {
48
+ /**
49
+ * Returns the subplugin name.
50
+ *
51
+ * @return string
52
+ */
53
+ public function get_subpluginname () {
36
54
return 'tool_lcbackupcourselogstep ' ;
37
55
}
38
56
57
+ /**
58
+ * Get the plugin description.
59
+ *
60
+ * @return string
61
+ */
39
62
public function get_plugin_description () {
40
63
return "Backup course log step plugin " ;
41
64
}
42
65
43
- public function process_course ($ processid , $ instanceid , $ course )
44
- {
66
+ /**
67
+ * Processes the course.
68
+ *
69
+ * @param int $processid the process id.
70
+ * @param int $instanceid step instance id.
71
+ * @param object $course the course object.
72
+ * @return step_response
73
+ */
74
+ public function process_course ($ processid , $ instanceid , $ course ) {
45
75
global $ DB ;
46
76
47
77
// Get all logs for the course with related courses and user details.
48
78
$ sql = "SELECT log.*, c.shortname as courseshortname, c.fullname as coursefullname,
49
- u1.firstname as userfirstname, u1.lastname as userlastname,
79
+ u1.firstname as userfirstname, u1.lastname as userlastname,
50
80
u2.firstname as realuserfirstname, u2.lastname as realuserlastname,
51
- u3.firstname as relateduserfirstname, u3.lastname as relateduserlastname
52
- FROM {logstore_standard_log} as log
53
- LEFT JOIN {course} as c ON log.courseid = c.id
54
- LEFT JOIN {user} as u1 ON log.userid = u1.id
55
- LEFT JOIN {user} as u2 ON log.realuserid = u2.id
56
- LEFT JOIN {user} as u3 ON log.relateduserid = u3.id
81
+ u3.firstname as relateduserfirstname, u3.lastname as relateduserlastname
82
+ FROM {logstore_standard_log} log
83
+ LEFT JOIN {course} c ON log.courseid = c.id
84
+ LEFT JOIN {user} u1 ON log.userid = u1.id
85
+ LEFT JOIN {user} u2 ON log.realuserid = u2.id
86
+ LEFT JOIN {user} u3 ON log.relateduserid = u3.id
57
87
WHERE courseid = :courseid " ;
58
88
59
89
// Get all logs for the course.
@@ -82,7 +112,7 @@ public function process_course($processid, $instanceid, $course)
82
112
83
113
// Prepare file record.
84
114
$ filerecord = [
85
- 'contextid ' => \context_course ::instance ($ course -> id )->id ,
115
+ 'contextid ' => \context_system ::instance ()->id ,
86
116
'component ' => 'tool_lcbackupcourselogstep ' ,
87
117
'filearea ' => 'course_log ' ,
88
118
'itemid ' => $ instanceid ,
@@ -99,18 +129,37 @@ public function process_course($processid, $instanceid, $course)
99
129
}
100
130
101
131
// Write data to file.
102
- dataformat::write_data_to_filearea ($ filerecord , $ fileformat , $ columns , $ logs );
132
+ $ newfile = dataformat::write_data_to_filearea ($ filerecord , $ fileformat , $ columns , $ logs );
133
+
134
+ $ DB ->insert_record ('tool_lcbackupcourselogstep_metadata ' , [
135
+ 'shortname ' => $ course ->shortname ,
136
+ 'fullname ' => $ course ->fullname ,
137
+ 'oldcourseid ' => $ course ->id ,
138
+ 'fileid ' => $ newfile ->get_id (),
139
+ 'timecreated ' => time (),
140
+ ]);
103
141
104
142
// Proceed.
105
143
return step_response::proceed ();
106
144
}
107
145
146
+ /**
147
+ * Returns instance settings for the plugin.
148
+ *
149
+ * @return array The instance settings for this step.
150
+ */
108
151
public function instance_settings () {
109
152
return [
110
153
new instance_setting ('fileformat ' , PARAM_TEXT , true ),
111
154
];
112
155
}
113
156
157
+ /**
158
+ * Extend the instance form to add file format options.
159
+ *
160
+ * @param \MoodleQuickForm $mform The form object.
161
+ * @return void
162
+ */
114
163
public function extend_add_instance_form_definition ($ mform ) {
115
164
$ fileformatoptions = [
116
165
'csv ' => 'CSV ' ,
@@ -119,7 +168,7 @@ public function extend_add_instance_form_definition($mform) {
119
168
120
169
// Check if XMLWriter is available.
121
170
// Available data formats are installed under 'dataformat' folder.
122
- // We need to install https://moodle.org/plugins/dataformat_xml
171
+ // We need to install https://moodle.org/plugins/dataformat_xml.
123
172
$ classname = 'dataformat_xml\writer ' ;
124
173
if (class_exists ($ classname )) {
125
174
$ fileformatoptions ['xml ' ] = 'XML ' ;
@@ -131,17 +180,19 @@ public function extend_add_instance_form_definition($mform) {
131
180
);
132
181
$ mform ->setType ('fileformat ' , PARAM_TEXT );
133
182
$ mform ->setDefault ('fileformat ' , 'csv ' );
134
-
135
183
}
136
184
137
- public function get_plugin_settings ()
138
- {
185
+ /**
186
+ * Returns the instance settings.
187
+ *
188
+ * @return void
189
+ */
190
+ public function get_plugin_settings () {
139
191
global $ ADMIN ;
140
192
// Page to show the logs.
141
193
$ ADMIN ->add ('lifecycle_category ' , new admin_externalpage ('tool_lcbackupcourselogstep_logs ' ,
142
194
get_string ('courselogs ' , 'tool_lcbackupcourselogstep ' ),
143
195
new moodle_url ('/admin/tool/lcbackupcourselogstep/logs.php ' )));
144
-
145
196
}
146
197
147
198
}
0 commit comments