3 * Page for moving questions
5 * @author me@jamiep.org
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package questionbank
11 require_once(dirname(__FILE__
) . '/../config.php');
12 require_once(dirname(__FILE__
) . '/editlib.php');
13 require_once($CFG->dirroot
.'/question/contextmoveq_form.php');
15 $ids = required_param('ids',PARAM_SEQUENCE
); // question ids
17 if (!$cmid = optional_param('cmid', 0, PARAM_INT
)){
18 $courseid = required_param('courseid', PARAM_INT
);
21 $tocatid = required_param('tocatid', PARAM_INT
);
22 $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL
);
24 $thispageurl = new moodle_url();
25 $thispageurl->params(compact('tocatid', 'ids', 'returnurl'));
28 list($module, $cm) = get_module_from_cmid($cmid);
29 require_login($cm->course
, false, $cm);
30 $thiscontext = get_context_instance(CONTEXT_MODULE
, $cmid);
32 $returnurl = "{$CFG->wwwroot}/question/edit.php?cmid={$cm->id}";
34 $thispageurl->param('cmid', $cmid);
35 } elseif ($courseid) {
36 require_login($courseid, false);
37 $thiscontext = get_context_instance(CONTEXT_COURSE
, $courseid);
41 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
43 $thispageurl->param('courseid', $COURSE->id
);
45 error('Need to pass courseid or cmid to this script.');
47 $contexts = new question_edit_contexts($thiscontext);
50 if (!$questions = get_records_sql("SELECT q.*, c.contextid FROM {$CFG->prefix}question q, {$CFG->prefix}question_categories c WHERE q.id IN ($ids) AND c.id = q.category")) {
51 print_error('questiondoesnotexist', 'question', $returnurl);
53 if (!$tocat = get_record('question_categories', 'id', $tocatid)){
54 print_error('categorydoesnotexist', 'question', $returnurl);
56 $tocat->context
= get_context_instance_by_id($tocat->contextid
);
57 require_capability('moodle/question:add', $tocat->context
);
58 $tocoursefilesid = get_filesdir_from_context($tocat->context
);
61 if ($tocoursefilesid == SITEID
){
62 $toareaname = get_string('filesareasite', 'question');
64 $toareaname = get_string('filesareacourse', 'question');
66 $fromcoursefilesid = 0;
67 foreach (array_keys($questions) as $id){
68 question_require_capability_on($questions[$id], 'move');
69 get_question_options($questions[$id]);
70 $questions[$id]->context
= get_context_instance_by_id($questions[$id]->contextid
);
71 $thisfilesid = get_filesdir_from_context($questions[$id]->context
);
72 if ($fromcoursefilesid && $thisfilesid != $fromcoursefilesid){
73 error('You can\'t use this script to move questions that have files associated with them from different areas.');
75 $fromcoursefilesid = $thisfilesid;
77 if ($tocoursefilesid != $fromcoursefilesid){
78 $urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype
]->find_file_links($questions[$id], $fromcoursefilesid));
82 $brokenurls = array();
83 foreach (array_keys($urls) as $url){
84 if (!file_exists($CFG->dataroot
."/$fromcoursefilesid/".$url)){
88 if ($fromcoursefilesid == SITEID
){
89 $fromareaname = get_string('filesareasite', 'question');
91 $fromareaname = get_string('filesareacourse', 'question');
94 $contextmoveform = new question_context_move_question_form($thispageurl,
95 compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
96 'fromcoursefilesid', 'tocoursefilesid'));
97 if ($contextmoveform->is_cancelled()){
99 }elseif ($moveformdata = $contextmoveform->get_data()) {
100 if (isset($moveformdata->urls
) && is_array($moveformdata->urls
)){
101 check_dir_exists($CFG->dataroot
."/$tocoursefilesid/", true);
102 $flipurls = array_keys($urls);
104 foreach ($moveformdata->urls
as $key => $urlaction){
105 $source = $CFG->dataroot
."/$fromcoursefilesid/".$flipurls[$key];
106 $destination = $flipurls[$key];
107 if (($urlaction != QUESTION_FILEDONOTHING
) && ($urlaction != QUESTION_FILEMOVELINKSONLY
)){
108 // Ensure the target folder exists.
109 check_dir_exists(dirname($CFG->dataroot
."/$tocoursefilesid/".$destination), true);
111 // Then make sure the destination file name does not exist. If it does, change the name to be unique.
112 while (file_exists($CFG->dataroot
."/$tocoursefilesid/".$destination)){
114 //check for '_'. copyno after filename, before extension.
115 if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
116 $copyno = $matches[1]+
1;
120 //replace old copy no with incremented one.
121 $destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
125 case QUESTION_FILECOPY
:
126 if (!copy($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
127 print_error('errorfilecannotbecopied', 'question', $returnurl, $source);
130 case QUESTION_FILEMOVE
:
131 if (!rename($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
132 print_error('errorfilecannotbemoved', 'question', $returnurl, $source);
135 case QUESTION_FILEMOVELINKSONLY
:
136 case QUESTION_FILEDONOTHING
:
139 error('Invalid action selected!', $returnurl);
141 //now search and replace urls in questions.
143 case QUESTION_FILECOPY
:
144 case QUESTION_FILEMOVE
:
145 case QUESTION_FILEMOVELINKSONLY
:
146 $url = $flipurls[$key];
147 $questionswithlinks = array_unique($urls[$url]);
148 foreach ($questionswithlinks as $questionid){
149 $QTYPES[$questions[$questionid]->qtype
]->replace_file_links($questions[$questionid], $fromcoursefilesid, $tocoursefilesid, $url, $destination);
152 case QUESTION_FILEDONOTHING
:
155 error('Invalid action selected!', $returnurl);
163 /// Now move questions.
164 if (!question_move_questions_to_category($ids, $tocat->id
)) {
165 print_error('errormovingquestions', 'question', $returnurl, $ids);
167 redirect($returnurl);
170 $streditingcategories = get_string('editcategories', 'quiz');
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
))
178 $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname
),
179 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id",
180 'type' => 'activity');
181 $crumbs[] = array('name' => format_string($module->name
),
182 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}",
185 // Print basic page layout.
186 $strupdatemodule = '';
188 $strmovingquestions = get_string('movingquestions', 'question');
189 $crumbs[] = array('name' => $strmovingquestions, 'link' => '', 'type' => 'title');
191 $navigation = build_navigation($crumbs);
192 print_header_simple($strmovingquestions, '', $navigation, "", "", true, $strupdatemodule);
196 $currenttab = 'edit';
198 $
{$cm->modname
} = $module;
199 include($CFG->dirroot
."/mod/{$cm->modname}/tabs.php");
201 $currenttab = 'questions';
204 //parameter for get_string
205 $questionsstr = new object();
206 $questionsstr->tocontext
= print_context_name($tocat->context
);
207 $questionsstr->fromareaname
= $fromareaname;
209 //comma seperated string "'xx', 'cx', 'sdf' and 'fgdhfg'"
210 $questionnamestojoin = array();
211 foreach ($questions as $question){
212 $questionnamestojoin[] = $question->name
;
214 $tojoincount = count($questionnamestojoin);
216 if ($tojoincount > 1){
218 $a->one
= $questionnamestojoin[$tojoincount -2].'"</strong>';
219 $a->two
= '<strong>"'.$questionnamestojoin[$tojoincount -1];
220 $questionnamestojoin[$tojoincount -2] = get_string('and', '', $a);
221 unset($questionnamestojoin[$tojoincount -1]);
223 $questionsstr->questions
= '<strong>"'.join($questionnamestojoin, '"</strong>, <strong>"').'"</strong>';
227 for ($default_key = 0; $default_key < count($urls); $default_key++
){
228 $defaults['urls'][$default_key] = QUESTION_FILECOPY
;
230 $contextmoveform->set_data($defaults);
232 //some parameters for get_string
233 $questionsstr->urlcount
= count($urls);
235 $questionsstr->toareaname
= $toareaname;
237 print_box(get_string('movingquestionsandfiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
239 print_box(get_string('movingquestionsnofiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
241 $contextmoveform->display();
242 print_footer($COURSE);