Repository navigation
Expand file tree
/
Copy pathupdate_libraries.php
More file actions
128 lines (106 loc) · 4.77 KB
/
Copy pathupdate_libraries.php
File metadata and controls
128 lines (106 loc) · 4.77 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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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/>.
/**
* Upgrade all libraries script.
*
* @package mod_hvp
* @author Rossco Hellmans <rosscohellmans@catalyst-au.net>
* @copyright Catalyst IT, 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once('locallib.php');
$confirm = optional_param('confirm', null, PARAM_INT);
$returnurl = new moodle_url('/mod/hvp/library_list.php');
$pageurl = new moodle_url('/mod/hvp/update_libraries.php');
$PAGE->set_url($pageurl);
admin_externalpage_setup('h5plibraries');
$PAGE->set_title("{$SITE->shortname}: " . get_string('libraries', 'hvp'));
echo $OUTPUT->header();
if ($confirm && confirm_sesskey()) {
echo $OUTPUT->heading(get_string('updatealllibraries', 'hvp'));
// We may need extra execution time and memory.
core_php_time_limit::raise(HOURSECS);
raise_memory_limit(MEMORY_EXTRA);
$progressbar = new progress_bar();
$progressbar->create();
$progressbar->update(0, 1, 'Finding libraries with updates');
// Update the hub cache first so we have the latest version info.
$editor = mod_hvp\framework::instance('editor');
$ajax = $editor->ajax;
$token = \H5PCore::createToken('editorajax');
$ajax->core->updateContentTypeCache();
$sql = "SELECT DISTINCT lhc.machine_name, lhc.title, lhc.major_version, lhc.minor_version, lhc.patch_version
FROM {hvp_libraries_hub_cache} lhc
JOIN {hvp_libraries} l
ON lhc.machine_name = l.machine_name
WHERE l.restricted = ?";
$libraries = $DB->get_records_sql($sql, [0]);
$libraries = array_filter($libraries, function ($library) {
global $DB;
// Find local library with same major + minor.
return !$DB->record_exists('hvp_libraries', [
'machine_name' => $library->machine_name,
'major_version' => $library->major_version,
'minor_version' => $library->minor_version,
'patch_version' => $library->patch_version,
]);
});
$total = count($libraries);
$counter = 0;
$queuedlibraries = [];
foreach ($libraries as $library) {
$machinename = $library->machine_name;
$librarytitle = $library->title;
$progressbar->update($counter, $total, "Creating update task for {$librarytitle}");
$counter++;
$updatelibrarytask = new mod_hvp\task\update_library_task();
$updatelibrarytask->set_custom_data([
'machinename' => $machinename,
'librarytitle' => $librarytitle,
]);
\core\task\manager::queue_adhoc_task($updatelibrarytask, true);
$version = "{$library->major_version}.{$library->minor_version}.{$library->patch_version}";
$queuedlibraries[$librarytitle] = $version;
}
if (!empty($queuedlibraries)) {
$message = 'The following libraries have been queued for updating:';
$message .= html_writer::start_tag('ul');
foreach ($queuedlibraries as $librarytitle => $version) {
$message .= html_writer::tag('li', "{$librarytitle} ({$version})");
}
$message .= html_writer::end_tag('ul');
} else {
$message = 'No libraries have been queued for updating.';
}
\core\notification::add($message, \core\notification::SUCCESS);
$progressbar->update(1, 1, get_string('completed'));
echo $OUTPUT->single_button($returnurl, get_string('upgradereturn', 'hvp'));
$upgradeurl = new moodle_url('/mod/hvp/upgrade_all_content.php');
echo $OUTPUT->single_button($upgradeurl, get_string('upgradebulkcontent', 'hvp'));
} else {
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
$params = [
'confirm' => 1,
'contextId' => context_course::instance(SITEID)->id,
];
$formcontinue = new single_button(new moodle_url('/mod/hvp/update_libraries.php', $params), get_string('yes'));
$formcancel = new single_button($returnurl, get_string('no'));
echo $OUTPUT->confirm(get_string('updatealllibrariesconfirm', 'hvp'), $formcontinue, $formcancel);
}
echo $OUTPUT->footer();