-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin_convertback.php
140 lines (125 loc) · 5.28 KB
/
admin_convertback.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
<?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 sharedresources into local resources or urls.
*
* @package mod_sharedresource
* @author Valery Fremaux <[email protected]>
* @copyright Valery Fremaux (activeprolearn.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once('../../config.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);
if ($courseid > SITEID) {
if (!$course = $DB->get_record('course', ['id' => "$courseid"])) {
throw new moodle_exception('coursemisconf');
}
// Security.
$context = context_course::instance($courseid);
require_login($course);
require_capability('moodle/course:manageactivities', $context);
$PAGE->set_context($context);
} else {
$systemcontext = context_system::instance();
require_login();
require_capability('repository/sharedresources:manage', $systemcontext);
$PAGE->set_context($systemcontext);
}
$PAGE->set_title(get_string('resourceconversion', 'sharedresource'));
$PAGE->set_heading(get_string('resourceconversion', 'sharedresource'));
$url = new moodle_url('/mod/sharedresource/admin_convertback.php', ['course' => $courseid]);
$PAGE->set_url($url);
$PAGE->navbar->add(get_string('resourceconversion', 'sharedresource'));
$PAGE->navbar->add(get_string('repositorytoresource', 'sharedresource'));
$report = '';
// Get courses.
if (empty($courseid)) {
$alllps = $DB->get_records_menu('course', ['format' => 'learning'], 'shortname', 'id,id');
$form = new sharedresource_choosecourse_form($alllps);
echo $OUTPUT->header();
echo ($OUTPUT->heading(get_string('resourceconversion', 'sharedresource'), 1));
$form->display();
echo $OUTPUT->footer();
exit();
} else {
$sharedresources = $DB->get_records('sharedresource', ['course' => $courseid], 'name');
if (empty($sharedresources)) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('resourceconversion', 'sharedresource'), 1);
echo $OUTPUT->notification(get_string('noresourcestoconvert', 'sharedresource'));
echo $OUTPUT->continue_button(new moodle_url('/course/view.php', ['id' => $courseid, 'action' => 'activities']));
echo $OUTPUT->footer();
exit();
}
// Filter convertible resources :
// We only can convert back non effectively shared resources.
foreach ($sharedresources as $id => $sharedresource) {
$select = "
course <> ? AND
identifier = ?
";
if ($DB->count_records_select('sharedresource', $select, [$courseid, $sharedresource->identifier]) != 0) {
unset($sharedresources[$id]);
}
}
$form2 = new sharedresource_selectresources_form($url, ['resources' => $sharedresources]);
if ($form2->is_cancelled()) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('resourceconversion', 'sharedresource'));
echo $OUTPUT->notification(get_string('conversioncancelled', 'sharedresource'));
$buttonurl = new moodle_url('/course/view.php', ['id' => $courseid]);
echo $OUTPUT->continue_button($buttonurl);
echo $OUTPUT->footer();
exit;
}
// If data submitted, proceed.
if ($data = $form2->get_data()) {
$reskeys = preg_grep("/cnv_/" , array_keys(get_object_vars($data)));
if (!empty($reskeys)) {
foreach ($reskeys as $reskey) {
// Convert selected resources.
if ($data->$reskey == 1) {
$resid = str_replace('rcnv_', '', $reskey);
$sharedresource = $DB->get_record('sharedresource', ['id' => $resid]);
$report .= get_string('convertingsharedresource', 'sharedresource', $sharedresource);
sharedresource_convertfrom($sharedresource, $report);
}
}
}
rebuild_course_cache($courseid);
} else {
// Print form.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('resourceconversion', 'sharedresource'));
$formdata = new StdClass;
$formdata->course = $courseid;
$form2->set_data($formdata);
$form2->display();
echo $OUTPUT->footer();
exit;
}
}
echo $OUTPUT->header();
if (!empty($report)) {
echo '<pre>';
echo $report;
echo '</pre>';
}
echo $OUTPUT->continue_button(new moodle_url('/course/view.php', ['id' => $courseid]));
echo $OUTPUT->footer();