-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.php
More file actions
executable file
·201 lines (166 loc) · 5.1 KB
/
lib.php
File metadata and controls
executable file
·201 lines (166 loc) · 5.1 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Plugin version and other meta-data are defined here.
*
* @package tool_copy_courses
* @copyright 2023 Jhon Rangel <jrangelardila@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_copy_courses\managment_copy_courses;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/csvlib.class.php');
require_once(__DIR__ . '/classes/managment_copy_courses.class.php');
require_once($CFG->libdir . '/filelib.php');
/**
* Plugin administration pages are defined here.
*
* @package tool_copy_courses
* @category admin
* @copyright 2023 Jhon Rangel <jrangelardila@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Retornar la data del archivo
* @param $form
* @param $formdata
* @throws moodle_exception
*/
function tool_copy_courses_validate_file($form, $formdata)
{
$file = new csv_import_reader(csv_import_reader::get_new_iid('uploaduser'), 'uploaduser');
$content = $form->get_file_content('csvfile');
$file->load_csv_content($content, null, $form->get_separator($formdata));
$file->init();
$columns = $file->get_columns();
$sorted_columns = $columns;
sort($sorted_columns);
if (sizeof($sorted_columns) != sizeof(\tool_copy_courses\managment_copy_courses::$validator)) {
throw new \moodle_exception('error_num_columns', 'tool_copy_courses');
}
if ($sorted_columns != \tool_copy_courses\managment_copy_courses::$validator) {
throw new \moodle_exception('error_in_columns', 'tool_copy_courses');
}
$column_map = array_flip($columns);
$table = new html_table();
$table->head = $sorted_columns;
$table->head[] = 'validation';
$data = [];
while ($row = $file->next()) {
$sorted_row = [];
foreach ($sorted_columns as $column) {
$sorted_row[] = $row[$column_map[$column]];
}
$validation = \tool_copy_courses\managment_copy_courses::verified_copy_data($sorted_row, $data);
$row = new html_table_row();
$row->cells = $sorted_row;
$row->cells[] = $validation[0];
$table->data[] = $row;
if ($validation[1]) {
$sorted_row[] = $validation[1];
$data[] = $sorted_row;
}
}
echo html_writer::table($table);
tool_copy_courses_save_file(json_encode($data));
}
/**
* Crear cada tarea adhoc
*
* @return void
* @throws dml_exception
*/
function tool_copy_courses_execute()
{
$data = tool_copy_courses_get_file();
foreach ($data as $item) {
$copy = new managment_copy_courses(
category: $item[0],
copyshortname: $item[1],
enddate: $item[2],
enrols: $item[3],
fullname: $item[4],
shortname: $item[5],
startdate: $item[6],
visible: $item[7]
);
$copy->created_task();
}
}
/**
* Guardar el archivo json de manera temporal
* @param $content
* @return void
* @throws dml_exception
* @throws file_exception
* @throws stored_file_creation_exception
*/
function tool_copy_courses_save_file($content)
{
tool_copy_courses_delete_file();
$context = context_system::instance();
$file_storage = get_file_storage();
$file_record = [
'contextid' => $context->id,
'component' => 'tool_copy_courses',
'filearea' => 'tempfiles',
'itemid' => 0,
'filepath' => '/',
'filename' => 'copy_courses.json',
];
$file_storage->create_file_from_string($file_record, $content);
}
/**
* Retornar el contenido del archivo
* @return false|string
* @throws coding_exception
* @throws dml_exception
*/
function tool_copy_courses_get_file()
{
$context = context_system::instance();
$file_storage = get_file_storage();
$file_record = [
'contextid' => $context->id,
'component' => 'tool_copy_courses',
'filearea' => 'tempfiles',
'itemid' => 0,
'filepath' => '/',
'filename' => 'copy_courses.json',
];
$files = $file_storage->get_area_files(
$file_record['contextid'],
$file_record['component'],
$file_record['filearea'],
$file_record['itemid'],
false
);
foreach ($files as $file) {
if ($file->get_filename() === $file_record['filename']) {
return json_decode($file->get_content(), true);
}
}
return false;
}
/**
* Borrar el archivo
* @return void
* @throws dml_exception
*/
function tool_copy_courses_delete_file()
{
$context = context_system::instance();
$file_storage = get_file_storage();
$file_record = [
'contextid' => $context->id,
'component' => 'tool_copy_courses',
'filearea' => 'tempfiles',
'itemid' => 0,
'filepath' => '/',
'filename' => 'copy_courses.json',
];
$file = $file_storage->get_file($file_record['contextid'], $file_record['component'], $file_record['filearea'], $file_record['itemid'], $file_record['filepath'], $file_record['filename']);
if ($file) {
$file->delete();
}
}