2009-11-04 11:57:52 +00:00
< ? php
2007-08-09 22:44:14 +00:00
/**
* 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 );
2010-01-16 15:39:56 +00:00
$thispageurl = new moodle_url ( '/question/contextmoveq.php' );
2007-08-09 22:44:14 +00:00
$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
}
2007-10-03 03:16:27 +00:00
$contexts = new question_edit_contexts ( $thiscontext );
2007-08-09 22:44:14 +00:00
2008-06-06 14:43:15 +00:00
list ( $usql , $params ) = $DB -> get_in_or_equal ( explode ( ',' , $ids ));
2007-08-09 22:44:14 +00:00
2008-06-06 14:43:15 +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 );
}
2008-06-06 14:43:15 +00:00
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 ()) {
2007-08-14 11:41:47 +00:00
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 )){
2007-11-28 16:43:20 +00:00
// 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 )){
2007-11-28 16:43:20 +00:00
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 )){
2007-11-28 16:43:20 +00:00
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 ;
}
}
}
2008-05-09 15:05:36 +00:00
/// 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' );
$strmovingquestions = get_string ( 'movingquestions' , 'question' );
2009-10-16 03:22:20 +00:00
$PAGE -> set_url ( $thispageurl -> out ());
2009-09-07 02:36:39 +00:00
$PAGE -> navbar -> add ( $strmovingquestions );
$PAGE -> set_title ( $strmovingquestions );
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 ();
2007-12-20 11:28:18 +00:00
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 ;
2009-08-10 05:01:23 +00:00
echo $OUTPUT -> box ( get_string ( 'movingquestionsandfiles' , 'question' , $questionsstr ), 'boxwidthnarrow boxaligncenter generalbox' );
2007-08-09 22:44:14 +00:00
} else {
2009-08-10 05:01:23 +00:00
echo $OUTPUT -> box ( get_string ( 'movingquestionsnofiles' , 'question' , $questionsstr ), 'boxwidthnarrow boxaligncenter generalbox' );
2007-08-09 22:44:14 +00:00
}
$contextmoveform -> display ();
2009-08-06 14:19:16 +00:00
echo $OUTPUT -> footer ();
2009-11-04 11:57:52 +00:00