question MDL-19820 Updated print_header and build_navigation to OUTPUT and PAGE equiv...
[moodle.git] / question / contextmoveq.php
CommitLineData
515ed4c3 1<?php // $Id$
2/**
3 * Page for moving questions
4 *
5 * @author me@jamiep.org
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package questionbank
8 */
9
10// Includes.
11require_once(dirname(__FILE__) . '/../config.php');
12require_once(dirname(__FILE__) . '/editlib.php');
13require_once($CFG->dirroot.'/question/contextmoveq_form.php');
14
15$ids = required_param('ids',PARAM_SEQUENCE); // question ids
16
17if (!$cmid = optional_param('cmid', 0, PARAM_INT)){
18 $courseid = required_param('courseid', PARAM_INT);
19}
20
21$tocatid = required_param('tocatid', PARAM_INT);
22$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
23
24$thispageurl = new moodle_url();
25$thispageurl->params(compact('tocatid', 'ids', 'returnurl'));
26
27if ($cmid){
28 list($module, $cm) = get_module_from_cmid($cmid);
29 require_login($cm->course, false, $cm);
30 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
31 if (!$returnurl) {
32 $returnurl = "{$CFG->wwwroot}/question/edit.php?cmid={$cm->id}";
33 }
34 $thispageurl->param('cmid', $cmid);
35} elseif ($courseid) {
36 require_login($courseid, false);
37 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
38 $module = null;
39 $cm = null;
40 if (!$returnurl) {
41 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
42 }
43 $thispageurl->param('courseid', $COURSE->id);
44} else {
0be2c858 45 print_error('missingcourseorcmid', 'question');
515ed4c3 46}
489984a7 47$contexts = new question_edit_contexts($thiscontext);
515ed4c3 48
f34488b2 49list($usql, $params) = $DB->get_in_or_equal(explode(',', $ids));
515ed4c3 50
f34488b2 51if (!$questions = $DB->get_records_sql("SELECT q.*, c.contextid FROM {question} q, {question_categories} c WHERE q.id $usql AND c.id = q.category", $params)) {
515ed4c3 52 print_error('questiondoesnotexist', 'question', $returnurl);
53}
f34488b2 54if (!$tocat = $DB->get_record('question_categories', array('id' => $tocatid))){
515ed4c3 55 print_error('categorydoesnotexist', 'question', $returnurl);
56}
57$tocat->context = get_context_instance_by_id($tocat->contextid);
58require_capability('moodle/question:add', $tocat->context);
59$tocoursefilesid = get_filesdir_from_context($tocat->context);
60$urls = array();
61
62if ($tocoursefilesid == SITEID){
63 $toareaname = get_string('filesareasite', 'question');
64} else {
65 $toareaname = get_string('filesareacourse', 'question');
66}
67$fromcoursefilesid = 0;
68foreach (array_keys($questions) as $id){
69 question_require_capability_on($questions[$id], 'move');
70 get_question_options($questions[$id]);
71 $questions[$id]->context = get_context_instance_by_id($questions[$id]->contextid);
72 $thisfilesid = get_filesdir_from_context($questions[$id]->context);
73 if ($fromcoursefilesid && $thisfilesid != $fromcoursefilesid){
0be2c858 74 print_error('cannotmovequestion', 'question');
515ed4c3 75 } else {
76 $fromcoursefilesid = $thisfilesid;
77 }
78 if ($tocoursefilesid != $fromcoursefilesid){
79 $urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype]->find_file_links($questions[$id], $fromcoursefilesid));
80 }
81}
82
83$brokenurls = array();
84foreach (array_keys($urls) as $url){
85 if (!file_exists($CFG->dataroot."/$fromcoursefilesid/".$url)){
86 $brokenurls[] = $url;
87 }
88}
89if ($fromcoursefilesid == SITEID){
90 $fromareaname = get_string('filesareasite', 'question');
91} else {
92 $fromareaname = get_string('filesareacourse', 'question');
93}
94
95$contextmoveform = new question_context_move_question_form($thispageurl,
96 compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
97 'fromcoursefilesid', 'tocoursefilesid'));
98if ($contextmoveform->is_cancelled()){
99 redirect($returnurl);
100}elseif ($moveformdata = $contextmoveform->get_data()) {
a54fd2dd 101 if (isset($moveformdata->urls) && is_array($moveformdata->urls)){
515ed4c3 102 check_dir_exists($CFG->dataroot."/$tocoursefilesid/", true);
103 $flipurls = array_keys($urls);
104 //actions on files
105 foreach ($moveformdata->urls as $key => $urlaction){
106 $source = $CFG->dataroot."/$fromcoursefilesid/".$flipurls[$key];
107 $destination = $flipurls[$key];
108 if (($urlaction != QUESTION_FILEDONOTHING) && ($urlaction != QUESTION_FILEMOVELINKSONLY)){
e049dee3 109 // Ensure the target folder exists.
110 check_dir_exists(dirname($CFG->dataroot."/$tocoursefilesid/".$destination), true);
111
112 // Then make sure the destination file name does not exist. If it does, change the name to be unique.
515ed4c3 113 while (file_exists($CFG->dataroot."/$tocoursefilesid/".$destination)){
114 $matches = array();
115 //check for '_'. copyno after filename, before extension.
116 if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
117 $copyno = $matches[1]+1;
118 } else {
119 $copyno = 1;
120 }
121 //replace old copy no with incremented one.
122 $destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
123 }
124 }
125 switch ($urlaction){
126 case QUESTION_FILECOPY :
127 if (!copy($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
e049dee3 128 print_error('errorfilecannotbecopied', 'question', $returnurl, $source);
515ed4c3 129 }
130 break;
131 case QUESTION_FILEMOVE :
132 if (!rename($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
e049dee3 133 print_error('errorfilecannotbemoved', 'question', $returnurl, $source);
515ed4c3 134 }
135 break;
136 case QUESTION_FILEMOVELINKSONLY :
137 case QUESTION_FILEDONOTHING :
138 break;
139 default :
0be2c858 140 print_error('invalidaction', '', $returnurl);
515ed4c3 141 }
142 //now search and replace urls in questions.
143 switch ($urlaction){
144 case QUESTION_FILECOPY :
145 case QUESTION_FILEMOVE :
146 case QUESTION_FILEMOVELINKSONLY :
147 $url = $flipurls[$key];
148 $questionswithlinks = array_unique($urls[$url]);
149 foreach ($questionswithlinks as $questionid){
150 $QTYPES[$questions[$questionid]->qtype]->replace_file_links($questions[$questionid], $fromcoursefilesid, $tocoursefilesid, $url, $destination);
151 }
152 break;
153 case QUESTION_FILEDONOTHING :
154 break;
155 default :
0be2c858 156 print_error('invalidaction', '', $returnurl);
515ed4c3 157 break;
158 }
159
160
161 }
162 }
7fb1b88d 163
164/// Now move questions.
165 if (!question_move_questions_to_category($ids, $tocat->id)) {
166 print_error('errormovingquestions', 'question', $returnurl, $ids);
515ed4c3 167 }
168 redirect($returnurl);
169}
170
171$streditingcategories = get_string('editcategories', 'quiz');
515ed4c3 172if ($cmid) {
173 // Page header
174 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
175 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
176 ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
177 : "";
d412354c 178 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
179 $PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
515ed4c3 180} else {
181 // Print basic page layout.
182 $strupdatemodule = '';
183}
184$strmovingquestions = get_string('movingquestions', 'question');
d412354c 185$PAGE->navbar->add($strmovingquestions);
186$PAGE->set_title($strmovingquestions);
187$PAGE->set_button($strupdatemodule);
188echo $OUTPUT->header();
515ed4c3 189
190// print tabs
191if ($cm!==null) {
192 $currenttab = 'edit';
193 $mode = 'questions';
194 ${$cm->modname} = $module;
195 include($CFG->dirroot."/mod/{$cm->modname}/tabs.php");
196} else {
197 $currenttab = 'questions';
198 include('tabs.php');
199}
200//parameter for get_string
201$questionsstr = new object();
202$questionsstr->tocontext = print_context_name($tocat->context);
203$questionsstr->fromareaname = $fromareaname;
204
205//comma seperated string "'xx', 'cx', 'sdf' and 'fgdhfg'"
206$questionnamestojoin = array();
207foreach ($questions as $question){
208 $questionnamestojoin[] = $question->name;
209}
210$tojoincount = count($questionnamestojoin);
211
212if ($tojoincount > 1){
213 $a = new object();
214 $a->one = $questionnamestojoin[$tojoincount -2].'"</strong>';
215 $a->two = '<strong>"'.$questionnamestojoin[$tojoincount -1];
216 $questionnamestojoin[$tojoincount -2] = get_string('and', '', $a);
217 unset($questionnamestojoin[$tojoincount -1]);
218}
219$questionsstr->questions = '<strong>"'.join($questionnamestojoin, '"</strong>, <strong>"').'"</strong>';
220
221if (count($urls)){
222 $defaults = array();
cb29abc5 223 for ($default_key = 0; $default_key < count($urls); $default_key++){
224 $defaults['urls'][$default_key] = QUESTION_FILECOPY;
515ed4c3 225 }
226 $contextmoveform->set_data($defaults);
227
228 //some parameters for get_string
229 $questionsstr->urlcount = count($urls);
230
231 $questionsstr->toareaname = $toareaname;
232
beb677cd 233 echo $OUTPUT->box(get_string('movingquestionsandfiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
515ed4c3 234} else {
beb677cd 235 echo $OUTPUT->box(get_string('movingquestionsnofiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
515ed4c3 236}
237$contextmoveform->display();
9b59580b 238echo $OUTPUT->footer();
5a2a5331 239?>