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 while (file_exists($CFG->dataroot
."/$tocoursefilesid/".$destination)){
110 //check for '_'. copyno after filename, before extension.
111 if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
112 $copyno = $matches[1]+
1;
116 //replace old copy no with incremented one.
117 $destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
121 case QUESTION_FILECOPY
:
122 if (!copy($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
123 print_error('errorfilecannotbecopied', 'question', $onerrorurl, $source);
126 case QUESTION_FILEMOVE
:
127 if (!rename($source, $CFG->dataroot
."/$tocoursefilesid/".$destination)){
128 print_error('errorfilecannotbemoved', 'question', $onerrorurl, $source);
131 case QUESTION_FILEMOVELINKSONLY
:
132 case QUESTION_FILEDONOTHING
:
135 error('Invalid action selected!', $onerrorurl);
137 //now search and replace urls in questions.
139 case QUESTION_FILECOPY
:
140 case QUESTION_FILEMOVE
:
141 case QUESTION_FILEMOVELINKSONLY
:
142 $url = $flipurls[$key];
143 $questionswithlinks = array_unique($urls[$url]);
144 foreach ($questionswithlinks as $questionid){
145 $QTYPES[$questions[$questionid]->qtype
]->replace_file_links($questions[$questionid], $fromcoursefilesid, $tocoursefilesid, $url, $destination);
148 case QUESTION_FILEDONOTHING
:
151 error('Invalid action selected!', $onerrorurl);
159 if (!execute_sql("UPDATE {$CFG->prefix}question SET category = {$tocat->id} WHERE id IN ({$ids})", false)){
160 error("Could not move the questions {$ids} to category ".$tocat->name
, $onerrorurl);
162 redirect($returnurl);
165 $streditingcategories = get_string('editcategories', 'quiz');
169 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
170 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
171 ?
update_module_button($cm->id
, $COURSE->id
, get_string('modulename', $cm->modname
))
173 $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname
),
174 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id",
175 'type' => 'activity');
176 $crumbs[] = array('name' => format_string($module->name
),
177 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}",
180 // Print basic page layout.
181 $strupdatemodule = '';
183 $strmovingquestions = get_string('movingquestions', 'question');
184 $crumbs[] = array('name' => $strmovingquestions, 'link' => '', 'type' => 'title');
186 $navigation = build_navigation($crumbs);
187 print_header_simple($strmovingquestions, '', $navigation, "", "", true, $strupdatemodule);
191 $currenttab = 'edit';
193 $
{$cm->modname
} = $module;
194 include($CFG->dirroot
."/mod/{$cm->modname}/tabs.php");
196 $currenttab = 'questions';
199 //parameter for get_string
200 $questionsstr = new object();
201 $questionsstr->tocontext
= print_context_name($tocat->context
);
202 $questionsstr->fromareaname
= $fromareaname;
204 //comma seperated string "'xx', 'cx', 'sdf' and 'fgdhfg'"
205 $questionnamestojoin = array();
206 foreach ($questions as $question){
207 $questionnamestojoin[] = $question->name
;
209 $tojoincount = count($questionnamestojoin);
211 if ($tojoincount > 1){
213 $a->one
= $questionnamestojoin[$tojoincount -2].'"</strong>';
214 $a->two
= '<strong>"'.$questionnamestojoin[$tojoincount -1];
215 $questionnamestojoin[$tojoincount -2] = get_string('and', '', $a);
216 unset($questionnamestojoin[$tojoincount -1]);
218 $questionsstr->questions
= '<strong>"'.join($questionnamestojoin, '"</strong>, <strong>"').'"</strong>';
222 for ($default_key =0; $default_key < count($urls); $default_key++
){
223 switch ($tocoursefilesid){
225 $defaults['urls'][$default_key] = QUESTION_FILEMOVE
;
228 $defaults['urls'][$default_key] = QUESTION_FILECOPY
;
233 $contextmoveform->set_data($defaults);
235 //some parameters for get_string
236 $questionsstr->urlcount
= count($urls);
238 $questionsstr->toareaname
= $toareaname;
240 print_box(get_string('movingquestionsandfiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
242 print_box(get_string('movingquestionsnofiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
244 $contextmoveform->display();
245 print_footer($COURSE);