forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextmove.php
248 lines (226 loc) · 11.2 KB
/
contextmove.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php // $Id$
/**
* Allows someone with appropriate permissions to move a category and associated
* files to another context.
*
* @author Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
*/
require_once("../config.php");
require_once($CFG->dirroot."/question/editlib.php");
require_once($CFG->dirroot."/question/contextmove_form.php");
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('categories');
// get values from form for actions on this page
$toparent = required_param('toparent', PARAM_SEQUENCE);
$cattomove = required_param('cattomove', PARAM_INT);
$totop = optional_param('totop', 0, PARAM_INT); // optional param moves category to top of peers. Default is
//to add it to the bottom.
$onerrorurl = $CFG->wwwroot.'/question/category.php?'.$thispageurl->get_query_string();
list($toparent, $contextto) = explode(',', $toparent);
if (!empty($toparent)){//not top level category, make it a child of $toparent
if (!$toparent = $DB->get_record('question_categories', array('id' => $toparent))){
print_error('invalidcategoryidforparent', 'question', $onerrorurl);
}
$contextto = $toparent->contextid;
} else {
$toparent = new object();
$toparent->id = 0;
$toparent->contextid = $contextto;
}
if (!$cattomove = $DB->get_record('question_categories', array('id' => $cattomove))){
print_error('invalidcategoryidtomove', 'question', $onerrorurl);
}
if ($cattomove->contextid == $contextto){
print_error('contexterror', '', $onerrorurl);
}
$cattomove->categorylist = question_categorylist($cattomove->id);
$thispageurl->params(array('cattomove'=>$cattomove->id,
'toparent'=>"{$toparent->id},{$toparent->contextid}",
'totop'=>$totop));
$contextfrom = get_context_instance_by_id($cattomove->contextid);
$contextto = get_context_instance_by_id($contextto);
$contexttostring = print_context_name($contextto);
require_capability('moodle/question:managecategory', $contextfrom);
require_capability('moodle/question:managecategory', $contextto);
$fromcoursefilesid = get_filesdir_from_context($contextfrom);//siteid or courseid
$tocoursefilesid = get_filesdir_from_context($contextto);//siteid or courseid
if ($fromcoursefilesid != $tocoursefilesid){
list($usql, $params) = $DB->get_in_or_equal(explode(',', $cattomove->categorylist));
$questions = $DB->get_records_select('question', "category $usql", $params);
$urls = array();
if ($questions){
foreach ($questions as $id => $question){
$QTYPES[$questions[$id]->qtype]->get_question_options($questions[$id]);
$urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype]->find_file_links($questions[$id], $fromcoursefilesid));
}
}
ksort($urls);
} else {
$urls = array();
}
$brokenurls = array();
foreach (array_keys($urls) as $url){
if (!file_exists($CFG->dataroot."/$fromcoursefilesid/".$url)){
$brokenurls[] = $url;
}
}
if ($fromcoursefilesid == SITEID){
$fromareaname = get_string('filesareasite', 'question');
} else {
$fromareaname = get_string('filesareacourse', 'question');
}
if ($tocoursefilesid == SITEID){
$toareaname = get_string('filesareasite', 'question');
} else {
$toareaname = get_string('filesareacourse', 'question');
}
$contextmoveform = new question_context_move_form($thispageurl,
compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
'fromcoursefilesid', 'tocoursefilesid'));
if ($contextmoveform->is_cancelled()){
$thispageurl->remove_params('cattomove', 'toparent', 'totop');
redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string());
}elseif ($moveformdata = $contextmoveform->get_data()) {
if (isset($moveformdata->urls) && is_array($moveformdata->urls)){
check_dir_exists($CFG->dataroot."/$tocoursefilesid/", true);
$flipurls = array_keys($urls);
foreach ($moveformdata->urls as $key => $urlaction){
$source = $CFG->dataroot."/$fromcoursefilesid/".$flipurls[$key];
$destination = $flipurls[$key];
if (($urlaction != QUESTION_FILEDONOTHING) && ($urlaction != QUESTION_FILEMOVELINKSONLY)){
// Ensure the target folder exists.
check_dir_exists(dirname($CFG->dataroot."/$tocoursefilesid/".$destination), true);
// Then make sure the destination file name does not exist. If it does, change the name to be unique.
while (file_exists($CFG->dataroot."/$tocoursefilesid/".$destination)){
$matches = array();
//check for '_'. copyno after filename, before extension.
if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
$copyno = $matches[1]+1;
} else {
$copyno = 1;
}
//replace old copy no with incremented one.
$destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
}
}
switch ($urlaction){
case QUESTION_FILECOPY :
if (!copy($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
print_error('errorfilecannotbecopied', 'question', $onerrorurl, $source);
}
break;
case QUESTION_FILEMOVE :
if (!rename($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
print_error('errorfilecannotbemoved', 'question', $onerrorurl, $source);
}
break;
case QUESTION_FILEDONOTHING :
case QUESTION_FILEMOVELINKSONLY :
break;
default :
print_error('invalidaction', '', $onerrorurl);
}
switch ($urlaction){
//now search and replace urls in questions.
case QUESTION_FILECOPY :
case QUESTION_FILEMOVE :
case QUESTION_FILEMOVELINKSONLY :
$url = $flipurls[$key];
$questionids = array_unique($urls[$url]);
foreach ($questionids as $questionid){
$question = $questions[$questionid];
$QTYPES[$question->qtype]->replace_file_links($question, $fromcoursefilesid, $tocoursefilesid, $url, $destination);
}
break;
case QUESTION_FILEDONOTHING :
default :
break;
}
}
}
//adjust sortorder before we make the cat a peer of it's new peers
$peers = $DB->get_records_select_menu('question_categories',
'contextid = ? AND parent = ?', array($toparent->contextid, $toparent->id),
'sortorder ASC', 'id, 1');
$peers = array_keys($peers);
if ($totop){
array_unshift($peers, $cattomove->id);
} else {
$peers[] = $cattomove->id;
}
$sortorder = 0;
foreach ($peers as $peer) {
if (! $DB->set_field('question_categories', "sortorder", $sortorder, array("id" => $peer))) {
print_error('listupdatefail', '', $onerrorurl);
}
$sortorder++;
}
//now move category
$cat = new object();
$cat->id = $cattomove->id;
$cat->parent = $toparent->id;
//set context of category we are moving and all children also!
list($usql, $params) = $DB->get_in_or_equal(explode(',', $cattomove->categorylist));
$params = array_merge(array($contextto->id), $params);
if (!$DB->execute("UPDATE {question_categories} SET contextid = ? WHERE id $usql", $params)){
print_error('cannotmovefromto', 'question', $onerrorurl, array($newname, $contexttostring));
}
//finally set the new parent id
if (!$DB->update_record("question_categories", $cat)) {
print_error('cannotupdatecate', 'question', $onerrorurl, $updatename);
}
$thispageurl->remove_params('cattomove', 'toparent', 'totop');
redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string(array('cat'=>"{$cattomove->id},{$contextto->id}")));
}
$streditingcategories = get_string('editcategories', 'quiz');
$crumbs = array();
if ($cm!==null) {
// Page header
$strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
: "";
$crumbs[] = array('name' => get_string('modulenameplural', $cm->modname),
'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id",
'type' => 'activity');
$crumbs[] = array('name' => format_string($module->name),
'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}",
'type' => 'title');
} else {
// Print basic page layout.
$strupdatemodule = '';
}
$crumbs[] = array('name' => $streditingcategories, 'link' => $thispageurl->out(), 'type' => 'title');
$crumbs[] = array('name' => get_string('movingcategory', 'question'), 'link' => '', 'type' => 'title');
$navigation = build_navigation($crumbs);
print_header_simple($streditingcategories, '', $navigation, "", "", true, $strupdatemodule);
// print tabs
if ($cm!==null) {
$currenttab = 'edit';
$mode = 'categories';
${$cm->modname} = $module;
include($CFG->dirroot."/mod/{$cm->modname}/tabs.php");
} else {
$currenttab = 'categories';
$context = $contexts->lowest();
include('tabs.php');
}
//parameter for get_string
$cattomove->contextto = $contexttostring;
if (count($urls)){
$defaults = array();
for ($default_key = 0; $default_key < count($urls); $default_key++){
$defaults['urls'][$default_key] = QUESTION_FILECOPY;
}
$contextmoveform->set_data($defaults);
//some parameters for get_string
$cattomove->urlcount = count($urls);
$cattomove->toareaname = $toareaname;
$cattomove->fromareaname = $fromareaname;
print_box(get_string('movingcategoryandfiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
} else {
print_box(get_string('movingcategorynofiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
}
$contextmoveform->display();
print_footer($COURSE);
?>