-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettemplates.php
More file actions
194 lines (173 loc) · 8.46 KB
/
Copy pathsettemplates.php
File metadata and controls
194 lines (173 loc) · 8.46 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
<?php
// This file is part of local_template_import for Moodle - http://moodle.org/
//
// It is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Set templates page for local_template_import
*
* @package local_template_import
* @copyright 2020 onwards Stefan Weber <stewe1@gmx.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(dirname(dirname(dirname(__FILE__))) . '/config.php');
global $DB, $USER;
// Get params.
$contextid = required_param('contextid', PARAM_INT);
list($context, $course, $cm) = get_context_info_array($contextid);
if (!$course = $DB->get_record('course', array('id' => $course->id))) {
print_error('invalidcourseid');
}
$pattern = get_config('local_template_import', 'templatenames');
$patternlength = strlen($pattern);
// Security and access check.
require_login($course, null, $cm);
require_capability('local/template_import:createtemplatefile', $context);
// Create page.
$PAGE->set_pagelayout('admin');
$PAGE->set_url(new moodle_url($CFG->wwwroot . '/local/template_import/settemplates.php',
array('contextid' => $contextid)));
$PAGE->set_title(get_string('pluginname', 'local_template_import'));
$PAGE->set_heading(get_string('pluginname', 'local_template_import'));
$PAGE->set_context($context);
echo $OUTPUT->header();
echo html_writer::start_tag('ul');
// Get current list of templates.
$templatelist = $DB->get_records('local_template_import_files');
// Update current list of templates.
foreach ($templatelist as $template) {
// Remove no longer existing files from our list of templates.
$conditions = array('id' => $template->fileid);
if (!$DB->record_exists('files', $conditions)) {
$conditions = array('fileid' => $template->fileid);
$DB->delete_records('local_template_import_files', $conditions);
echo html_writer::start_tag('li', array('class' => 'alert-warning'));
echo get_string('backupfilenolongerexists', 'local_template_import', array(
'filename' => $template->filename,
'identifier' => $template->identifier
));
echo html_writer::end_tag('li');
}
// Remove backup files that no longer match the template prefix.
if (!$pattern == substr($template->filename, 0, $patternlength)) {
$conditions = array('filename' => $template->filename);
$DB->delete_records('local_template_import_files', $conditions);
echo html_writer::start_tag('li', array('class' => 'alert-warning'));
echo get_string('backupfilenolongermatches', 'local_template_import', array(
'filename' => $template->filename,
'identifier' => $template->identifier
));
echo html_writer::end_tag('li');
}
}
// Get all backup files from this course
// (sorting them out later is easier than using SQL LIKE)
$conditions = array('contextid' => $context->id, 'component' => 'backup');
$sort = 'timemodified DESC';
$fields = 'id, contenthash, pathnamehash, filename, timemodified';
$allbackupfiles = $DB->get_records('files', $conditions, $sort, $fields);
// Filter backupfiles which match our pattern.
foreach ($allbackupfiles as $id => $backupfile) {
if ($pattern == substr($backupfile->filename, 0, $patternlength)) {
// Get identifier for this backup file.
$identifier = substr($backupfile->filename, $patternlength);
$identifier = str_replace('.mbz', '', $identifier);
// Add to list of backupfiles to be added to template list.
$fileobj = (object) array(
'fileid' => $id,
'identifier' => $identifier,
'filename' => $backupfile->filename,
);
$backupfiles[$id] = $fileobj;
}
}
// Add new files to our list of templates.
if (isset($backupfiles)) {
foreach ($backupfiles as $id => $backupfile) {
// Get identifier for this backup file.
$identifier = substr($backupfile->filename, $patternlength);
$identifier = str_replace('.mbz', '', $identifier);
// Check if this file already exists.
$conditions = array('fileid' => $id);
if ($DB->record_exists('local_template_import_files', $conditions)) {
echo html_writer::start_tag('li', array('class' => 'alert-warning'));
echo get_string('backupfilealreadyonlist', 'local_template_import', array(
'filename' => $backupfile->filename,
'identifier' => $backupfile->identifier
));
echo html_writer::end_tag('li');
continue;
}
// Check if file with this identifier already exists.
$conditions = array('identifier' => $identifier);
if ($DB->record_exists('local_template_import_files', $conditions)) {
echo html_writer::start_tag('li', array('class' => 'alert-danger'));
echo get_string('backupfileduplicate', 'local_template_import', array(
'filename' => $backupfile->filename,
'identifier' => $backupfile->identifier
));
echo html_writer::end_tag('li');
} else {
// Get user.
$backupfile->addedby = $USER->id;
// Get timestamp.
$now = new DateTime("now", core_date::get_user_timezone_object());
$timestamp = $now->getTimestamp();
$backupfile->timeadded = $timestamp;
$backupfile->contextid = $context->id;
// Add this file to the list of available templates.
$DB->insert_record('local_template_import_files', $backupfile);
echo html_writer::start_tag('li', array('class' => 'alert-success'));
echo get_string('backupfileadded', 'local_template_import', array(
'filename' => $backupfile->filename,
'identifier' => $backupfile->identifier
));
echo html_writer::end_tag('li');
}
}
} else {
// No suitable template files found.
echo html_writer::start_tag('li', array('class' => 'alert-warning'));
echo get_string('backupfilesnotfound', 'local_template_import', $pattern);
echo html_writer::end_tag('li');
}
echo html_writer::end_tag('ul');
// Show current list of templates.
$templatelist = $DB->get_records('local_template_import_files');
echo html_writer::tag('h3', get_string('backupfileslist', 'local_template_import'));
// TODO: turn this into a proper moodle table
echo html_writer::start_tag('table');
echo html_writer::start_tag('tr');
echo "<th class='p-1'>" . get_string('table_identifier', 'local_template_import') . "</th>
<th class='p-1'>" . get_string('table_filename', 'local_template_import') . "</th>
<th class='p-1'>" . get_string('table_addedby', 'local_template_import') . "</th>
<th class='p-1'>" . get_string('table_timeadded', 'local_template_import') . "</th>
<th class='p-1'>" . get_string('table_location', 'local_template_import') . "</th>";
echo html_writer::end_tag('tr');
foreach ($templatelist as $template) {
echo html_writer::start_tag('tr');
$username = $DB->get_field('user', 'username', array('id' => $template->addedby));
$filelink = new moodle_url('/backup/restorefile.php', array('contextid' => $template->contextid));
echo "<td class='p-1'>" . $template->identifier . "</td>
<td class='p-1'>" . $template->filename . "</td>
<td class='p-1'>" . $username . "</td>
<td class='p-1'>" . userdate($template->timeadded) . '</td>
<td class="p-1"><a href="' . $filelink . '">' .
$template->contextid . "</td>";
echo html_writer::end_tag('tr');
}
echo html_writer::end_tag('table');
$returnurl = new moodle_url('/course/view.php', array('id' => $course->id));
echo '<div class="p-1 text-center"><a class="btn btn-primary" href="'
. $returnurl . '"> ' . get_string('back') . '</a></div>';
echo $OUTPUT->footer();