moodle/question/contextmoveq.php

240 lines
9.3 KiB
PHP
Raw Normal View History

2007-08-09 22:44:14 +00:00
<?php // $Id$
/**
* Page for moving questions
*
* @author me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
*/
// Includes.
require_once(dirname(__FILE__) . '/../config.php');
require_once(dirname(__FILE__) . '/editlib.php');
require_once($CFG->dirroot.'/question/contextmoveq_form.php');
$ids = required_param('ids',PARAM_SEQUENCE); // question ids
if (!$cmid = optional_param('cmid', 0, PARAM_INT)){
$courseid = required_param('courseid', PARAM_INT);
}
$tocatid = required_param('tocatid', PARAM_INT);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
$thispageurl = new moodle_url();
$thispageurl->params(compact('tocatid', 'ids', 'returnurl'));
if ($cmid){
list($module, $cm) = get_module_from_cmid($cmid);
require_login($cm->course, false, $cm);
$thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
if (!$returnurl) {
$returnurl = "{$CFG->wwwroot}/question/edit.php?cmid={$cm->id}";
}
$thispageurl->param('cmid', $cmid);
} elseif ($courseid) {
require_login($courseid, false);
$thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
$module = null;
$cm = null;
if (!$returnurl) {
$returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
}
$thispageurl->param('courseid', $COURSE->id);
} else {
2008-05-21 07:53:23 +00:00
print_error('missingcourseorcmid', 'question');
2007-08-09 22:44:14 +00:00
}
$contexts = new question_edit_contexts($thiscontext);
2007-08-09 22:44:14 +00:00
list($usql, $params) = $DB->get_in_or_equal(explode(',', $ids));
2007-08-09 22:44:14 +00:00
if (!$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)) {
2007-08-09 22:44:14 +00:00
print_error('questiondoesnotexist', 'question', $returnurl);
}
if (!$tocat = $DB->get_record('question_categories', array('id' => $tocatid))){
2007-08-09 22:44:14 +00:00
print_error('categorydoesnotexist', 'question', $returnurl);
}
$tocat->context = get_context_instance_by_id($tocat->contextid);
require_capability('moodle/question:add', $tocat->context);
$tocoursefilesid = get_filesdir_from_context($tocat->context);
$urls = array();
if ($tocoursefilesid == SITEID){
$toareaname = get_string('filesareasite', 'question');
} else {
$toareaname = get_string('filesareacourse', 'question');
}
$fromcoursefilesid = 0;
foreach (array_keys($questions) as $id){
question_require_capability_on($questions[$id], 'move');
get_question_options($questions[$id]);
$questions[$id]->context = get_context_instance_by_id($questions[$id]->contextid);
$thisfilesid = get_filesdir_from_context($questions[$id]->context);
if ($fromcoursefilesid && $thisfilesid != $fromcoursefilesid){
2008-05-21 07:53:23 +00:00
print_error('cannotmovequestion', 'question');
2007-08-09 22:44:14 +00:00
} else {
$fromcoursefilesid = $thisfilesid;
}
if ($tocoursefilesid != $fromcoursefilesid){
$urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype]->find_file_links($questions[$id], $fromcoursefilesid));
}
}
$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');
}
$contextmoveform = new question_context_move_question_form($thispageurl,
compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
'fromcoursefilesid', 'tocoursefilesid'));
if ($contextmoveform->is_cancelled()){
redirect($returnurl);
}elseif ($moveformdata = $contextmoveform->get_data()) {
if (isset($moveformdata->urls) && is_array($moveformdata->urls)){
2007-08-09 22:44:14 +00:00
check_dir_exists($CFG->dataroot."/$tocoursefilesid/", true);
$flipurls = array_keys($urls);
//actions on files
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.
2007-08-09 22:44:14 +00:00
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', $returnurl, $source);
2007-08-09 22:44:14 +00:00
}
break;
case QUESTION_FILEMOVE :
if (!rename($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
print_error('errorfilecannotbemoved', 'question', $returnurl, $source);
2007-08-09 22:44:14 +00:00
}
break;
case QUESTION_FILEMOVELINKSONLY :
case QUESTION_FILEDONOTHING :
break;
default :
2008-05-21 07:53:23 +00:00
print_error('invalidaction', '', $returnurl);
2007-08-09 22:44:14 +00:00
}
//now search and replace urls in questions.
switch ($urlaction){
case QUESTION_FILECOPY :
case QUESTION_FILEMOVE :
case QUESTION_FILEMOVELINKSONLY :
$url = $flipurls[$key];
$questionswithlinks = array_unique($urls[$url]);
foreach ($questionswithlinks as $questionid){
$QTYPES[$questions[$questionid]->qtype]->replace_file_links($questions[$questionid], $fromcoursefilesid, $tocoursefilesid, $url, $destination);
}
break;
case QUESTION_FILEDONOTHING :
break;
default :
2008-05-21 07:53:23 +00:00
print_error('invalidaction', '', $returnurl);
2007-08-09 22:44:14 +00:00
break;
}
}
}
/// Now move questions.
if (!question_move_questions_to_category($ids, $tocat->id)) {
print_error('errormovingquestions', 'question', $returnurl, $ids);
2007-08-09 22:44:14 +00:00
}
redirect($returnurl);
}
$streditingcategories = get_string('editcategories', 'quiz');
if ($cmid) {
// Page header
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$strupdatemodule = has_capability('moodle/course:manageactivities', $context)
? $OUTPUT->update_module_button($cm->id, $cm->modname)
2007-08-09 22:44:14 +00:00
: "";
$PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
$PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
2007-08-09 22:44:14 +00:00
} else {
// Print basic page layout.
$strupdatemodule = '';
}
$strmovingquestions = get_string('movingquestions', 'question');
$PAGE->navbar->add($strmovingquestions);
$PAGE->set_title($strmovingquestions);
$PAGE->set_button($strupdatemodule);
echo $OUTPUT->header();
2007-08-09 22:44:14 +00:00
// print tabs
if ($cm!==null) {
$currenttab = 'edit';
$mode = 'questions';
${$cm->modname} = $module;
include($CFG->dirroot."/mod/{$cm->modname}/tabs.php");
} else {
$currenttab = 'questions';
include('tabs.php');
}
//parameter for get_string
$questionsstr = new object();
$questionsstr->tocontext = print_context_name($tocat->context);
$questionsstr->fromareaname = $fromareaname;
//comma seperated string "'xx', 'cx', 'sdf' and 'fgdhfg'"
$questionnamestojoin = array();
foreach ($questions as $question){
$questionnamestojoin[] = $question->name;
}
$tojoincount = count($questionnamestojoin);
if ($tojoincount > 1){
$a = new object();
$a->one = $questionnamestojoin[$tojoincount -2].'"</strong>';
$a->two = '<strong>"'.$questionnamestojoin[$tojoincount -1];
$questionnamestojoin[$tojoincount -2] = get_string('and', '', $a);
unset($questionnamestojoin[$tojoincount -1]);
}
$questionsstr->questions = '<strong>"'.join($questionnamestojoin, '"</strong>, <strong>"').'"</strong>';
if (count($urls)){
$defaults = array();
for ($default_key = 0; $default_key < count($urls); $default_key++){
$defaults['urls'][$default_key] = QUESTION_FILECOPY;
2007-08-09 22:44:14 +00:00
}
$contextmoveform->set_data($defaults);
//some parameters for get_string
$questionsstr->urlcount = count($urls);
$questionsstr->toareaname = $toareaname;
echo $OUTPUT->box(get_string('movingquestionsandfiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
2007-08-09 22:44:14 +00:00
} else {
echo $OUTPUT->box(get_string('movingquestionsnofiles', 'question', $questionsstr), 'boxwidthnarrow boxaligncenter generalbox');
2007-08-09 22:44:14 +00:00
}
$contextmoveform->display();
echo $OUTPUT->footer();
?>