-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin_convertall.php
154 lines (132 loc) · 5.53 KB
/
admin_convertall.php
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
<?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/>.
/**
* this admin screen allows converting massively resources into sharedresources indexable entries.
*
* @package mod_sharedresource
* @author Valery Fremaux <[email protected]>
* @copyright Valery Fremaux (activeprolearn.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
/*
* sharedresource_check_access does the job.
* phpcs:disable moodle.Files.RequireLogin.Missing
*/
require('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/mod/sharedresource/lib.php');
require_once($CFG->dirroot.'/mod/sharedresource/locallib.php');
require_once($CFG->dirroot.'/mod/sharedresource/forms/admin_convert_form.php');
$courseid = optional_param('course', SITEID, PARAM_INT);
$url = new moodle_url('/mod/sharedresource/admin_convertall.php', ['course' => $courseid]);
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$context = sharedresource_check_access($course);
$PAGE->set_context($context);
$PAGE->set_title(get_string('resourceconversion', 'sharedresource'));
$PAGE->set_heading(get_string('resourceconversion', 'sharedresource'));
$PAGE->set_url('/mod/sharedresource/admin_convertall.php', ['course' => $courseid]);
$PAGE->set_pagelayout('standard');
// Navigation.
$PAGE->navbar->add(get_string('resourceconversion', 'sharedresource'));
$PAGE->navbar->add(get_string('resourcetorepository', 'sharedresource'));
// Get courses.
if (empty($courseid)) {
// If no course choosen (comming from general sections) make the choice of one.
$allcourses = $DB->get_records_menu('course', null, 'shortname', 'id, fullname');
$form = new sharedresource_choosecourse_form($allcourses);
if ($form->is_cancelled()) {
redirect(new moodle_url('/local/sharedresources/index.php'));
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();
die;
} else {
$course = $DB->get_record('course', ['id' => "$courseid"], '*', MUST_EXIST);
// Back to library if cancelled.
$form = new sharedresource_choosecourse_form(null);
if ($form->is_cancelled()) {
redirect(new moodle_url('/local/sharedresources/index.php'));
}
$resources = $DB->get_records('resource', ['course' => $courseid], 'name');
$urls = $DB->get_records('url', ['course' => $courseid], 'name');
$buttonurl = new moodle_url('/course/view.php', ['id' => $courseid]);
if (empty($resources) && empty($urls)) {
echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('noresourcestoconvert', 'sharedresource'));
echo $OUTPUT->continue_button($buttonurl);
echo $OUTPUT->footer();
exit();
}
$form2 = new sharedresource_selectresources_form($url, ['resources' => $resources, 'urls' => $urls]);
if ($form2->is_cancelled()) {
if ($courseid) {
redirect($buttonurl);
} else {
redirect(new moodle_url('/resources/index.php'));
}
}
// If data submitted, proceed.
if ($data = $form2->get_data()) {
$reskeys = preg_grep("/rcnv_/" , array_keys(get_object_vars($data)));
$report = '';
if (!empty($reskeys)) {
foreach ($reskeys as $reskey) {
// Convert selected resources.
if ($data->$reskey == 1) {
$resid = str_replace('rcnv_', '', $reskey);
$resource = $DB->get_record('resource', ['id' => $resid], '*', MUST_EXIST);
if (debugging()) {
$report .= "converting resource {$resource->id} : {$resource->name}\n";
}
$report .= sharedresource_convertto($resource, 'resource');
}
}
}
$reskeys = preg_grep("/ucnv_/" , array_keys(get_object_vars($data)));
if (!empty($reskeys)) {
foreach ($reskeys as $reskey) {
// Convert selected resources.
if ($data->$reskey == 1) {
$resid = str_replace('ucnv_', '', $reskey);
$url = $DB->get_record('url', ['id' => $resid]);
if (debugging()) {
$report .= "converting url $url->id : $url->name \n";
}
$report .= sharedresource_convertto($url, 'url');
}
}
}
rebuild_course_cache($courseid);
} else {
// Print form.
echo $OUTPUT->header();
$form2->set_data(['course' => $course->id]);
$form2->display();
echo $OUTPUT->footer();
die;
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('resourceconversion', 'sharedresource'), 1);
if (!empty($report)) {
echo '<pre>';
echo $report;
echo '</pre>';
}
$courseurl = new moodle_url('/course/view.php', ['id' => $courseid]);
echo $OUTPUT->continue_button($courseurl);
echo $OUTPUT->footer();