3 * Allows someone with appropriate permissions to move a category and associated
4 * files to another context.
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package questionbank
11 require_once("../config.php");
12 require_once($CFG->dirroot
."/question/editlib.php");
13 require_once($CFG->dirroot
."/question/contextmove_form.php");
15 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('categories');
17 // get values from form for actions on this page
18 $toparent = required_param('toparent', PARAM_SEQUENCE
);
19 $cattomove = required_param('cattomove', PARAM_INT
);
20 $totop = optional_param('totop', 0, PARAM_INT
); // optional param moves category to top of peers. Default is
21 //to add it to the bottom.
25 $onerrorurl = $CFG->wwwroot
.'/question/category.php?'.$thispageurl->get_query_string();
26 list($toparent, $contextto) = explode(',', $toparent);
27 if (!empty($toparent)){//not top level category, make it a child of $toparent
28 if (!$toparent = get_record('question_categories', 'id', $toparent)){
29 error('Invalid category id for parent!', $onerrorurl);
31 $contextto = $toparent->contextid
;
33 $toparent = new object();
35 $toparent->contextid
= $contextto;
37 if (!$cattomove = get_record('question_categories', 'id', $cattomove)){
38 error('Invalid category id to move!', $onerrorurl);
40 if ($cattomove->contextid
== $contextto){
41 error("You shouldn't have got here if you're not moving a category to another context.", $onerrorurl);
43 $cattomove->categorylist
= question_categorylist($cattomove->id
);
45 $thispageurl->params(array('cattomove'=>$cattomove->id
,
46 'toparent'=>"{$toparent->id},{$toparent->contextid}",
49 $contextfrom = get_context_instance_by_id($cattomove->contextid
);
50 $contextto = get_context_instance_by_id($contextto);
51 $contexttostring = print_context_name($contextto);
53 require_capability('moodle/question:managecategory', $contextfrom);
54 require_capability('moodle/question:managecategory', $contextto);
57 $fromcoursefilesid = get_filesdir_from_context($contextfrom);//siteid or courseid
58 $tocoursefilesid = get_filesdir_from_context($contextto);//siteid or courseid
59 if ($fromcoursefilesid != $tocoursefilesid){
60 $questions = get_records_select('question', "category IN ({$cattomove->categorylist})");
63 foreach ($questions as $id => $question){
64 $QTYPES[$questions[$id]->qtype
]->get_question_options($questions[$id]);
65 $urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype
]->find_file_links($questions[$id], $fromcoursefilesid));
72 $brokenurls = array();
73 foreach (array_keys($urls) as $url){
74 if (!file_exists($CFG->dataroot
."/$fromcoursefilesid/".$url)){
78 if ($fromcoursefilesid == SITEID
){
79 $fromareaname = get_string('filesareasite', 'question');
81 $fromareaname = get_string('filesareacourse', 'question');
83 if ($tocoursefilesid == SITEID
){
84 $toareaname = get_string('filesareasite', 'question');
86 $toareaname = get_string('filesareacourse', 'question');
88 $contextmoveform = new question_context_move_form($thispageurl,
89 compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
90 'fromcoursefilesid', 'tocoursefilesid'));
91 if ($contextmoveform->is_cancelled()){
92 $thispageurl->remove_params('cattomove', 'toparent', 'totop');
93 redirect($CFG->wwwroot
."/question/category.php?".$thispageurl->get_query_string());
94 }elseif ($moveformdata = $contextmoveform->get_data()) {
95 if (isset($moveformdata->urls
) && is_array($moveformdata->urls
)){
96 check_dir_exists($CFG->dataroot
."/$tocoursefilesid/", true);
97 $flipurls = array_keys($urls);
98 foreach ($moveformdata->urls
as $key => $urlaction){
99 $source = $CFG->dataroot
."/$fromcoursefilesid/".$flipurls[$key];
100 $destination = $flipurls[$key];
101 if (($urlaction != QUESTION_FILEDONOTHING
) && ($urlaction != QUESTION_FILEMOVELINKSONLY
)){
102 // Ensure the target folder exists.
103 check_dir_exists(dirname($CFG->dataroot
."/$tocoursefilesid/".$destination), true);
105 // Then make sure the destination file name does not exist. If it does, change the name to be unique.
106 while (file_exists($CFG->dataroot
."/$tocoursefilesid/".$destination)){
108 //check for '_'. copyno after filename, before extension.
109 if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
110 $copyno = $matches[1]+
1;
114 //replace old copy no with incremented one.
115 $destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
119 case QUESTION_FILECOPY
:
120 if (!copy($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
121 print_error('errorfilecannotbecopied', 'question', $onerrorurl, $source);
124 case QUESTION_FILEMOVE
:
125 if (!rename($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
126 print_error('errorfilecannotbemoved', 'question', $onerrorurl, $source);
129 case QUESTION_FILEDONOTHING
:
130 case QUESTION_FILEMOVELINKSONLY
:
133 error('Invalid action selected!', $onerrorurl);
136 //now search and replace urls in questions.
137 case QUESTION_FILECOPY
:
138 case QUESTION_FILEMOVE
:
139 case QUESTION_FILEMOVELINKSONLY
:
140 $url = $flipurls[$key];
141 $questionids = array_unique($urls[$url]);
142 foreach ($questionids as $questionid){
143 $question = $questions[$questionid];
144 $QTYPES[$question->qtype
]->replace_file_links($question, $fromcoursefilesid, $tocoursefilesid, $url, $destination);
147 case QUESTION_FILEDONOTHING
:
156 //adjust sortorder before we make the cat a peer of it's new peers
157 $peers = get_records_select_menu('question_categories', "contextid = {$toparent->contextid} AND ".
158 "parent = {$toparent->id}", "sortorder ASC",
160 $peers = array_keys($peers);
162 array_unshift($peers, $cattomove->id
);
164 $peers[] = $cattomove->id
;
167 foreach ($peers as $peer) {
168 if (! set_field('question_categories', "sortorder", $sortorder, "id", $peer)) {
169 print_error('listupdatefail', '', $onerrorurl);
175 $cat->id
= $cattomove->id
;
176 $cat->parent
= $toparent->id
;
177 //set context of category we are moving and all children also!
178 if (!execute_sql("UPDATE {$CFG->prefix}question_categories SET contextid = {$contextto->id} WHERE id IN ({$cattomove->categorylist})", false)){
179 error("Could not move the category '$newname' to ".$contexttostring, $onerrorurl);
181 //finally set the new parent id
182 if (!update_record("question_categories", $cat)) {
183 error("Could not update the category '$updatename'", $onerrorurl);
185 $thispageurl->remove_params('cattomove', 'toparent', 'totop');
186 redirect($CFG->wwwroot
."/question/category.php?".$thispageurl->get_query_string(array('cat'=>"{$cattomove->id},{$contextto->id}")));
189 $streditingcategories = get_string('editcategories', 'quiz');
193 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
194 ?
update_module_button($cm->id
, $COURSE->id
, get_string('modulename', $cm->modname
))
196 $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname
),
197 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id",
198 'type' => 'activity');
199 $crumbs[] = array('name' => format_string($module->name
),
200 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}",
203 // Print basic page layout.
204 $strupdatemodule = '';
208 $crumbs[] = array('name' => $streditingcategories, 'link' => $thispageurl->out(), 'type' => 'title');
209 $crumbs[] = array('name' => get_string('movingcategory', 'question'), 'link' => '', 'type' => 'title');
211 $navigation = build_navigation($crumbs);
212 print_header_simple($streditingcategories, '', $navigation, "", "", true, $strupdatemodule);
216 $currenttab = 'edit';
217 $mode = 'categories';
218 $
{$cm->modname
} = $module;
219 include($CFG->dirroot
."/mod/{$cm->modname}/tabs.php");
221 $currenttab = 'categories';
222 $context = $contexts->lowest();
225 //parameter for get_string
226 $cattomove->contextto
= $contexttostring;
229 for ($default_key = 0; $default_key < count($urls); $default_key++
){
230 $defaults['urls'][$default_key] = QUESTION_FILECOPY
;
232 $contextmoveform->set_data($defaults);
233 //some parameters for get_string
234 $cattomove->urlcount
= count($urls);
235 $cattomove->toareaname
= $toareaname;
236 $cattomove->fromareaname
= $fromareaname;
238 print_box(get_string('movingcategoryandfiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
240 print_box(get_string('movingcategorynofiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
242 $contextmoveform->display();
243 print_footer($COURSE);