2004-09-12 17:34:35 +00:00
< ? php // $Id$
2005-05-06 06:24:04 +00:00
/**
* Page to edit quizzes
*
* This page generally has two columns :
* The right column lists all available questions in a chosen category and
* allows them to be edited or more to be added . This column is only there if
* the quiz does not already have student attempts
* The left column lists all questions that have been added to the current quiz .
* This column is only there if this page has been called in the context of a quiz .
* The lecturer can add questions from the right hand list to the quiz or remove them
*
* The script also processes a number of actions :
* Actions affecting a quiz :
* up and down Changes the order of questions and page breaks
* addquestion Adds a single question to the quiz
* add Adds several selected questions to the quiz
* addrandom Adds a certain number of random questions to the quiz
* delete Removes a question from the quiz
2006-02-19 16:34:06 +00:00
* savechanges Saves the order and grades for questions in the quiz
2005-05-06 06:24:04 +00:00
* repaginate Re - paginates the quiz
* Actions affecting the question pool :
* move Moves a question to a different category
* deleteselected Deletes the selected questions from the category
* Other actions :
* cat Chooses the category
* displayoptions Sets display options
*
* @ version $Id $
* @ author Martin Dougiamas and many others . This has recently been extensively
* rewritten by Gustav Delius and other members of the Serving Mathematics project
* { @ link http :// maths . york . ac . uk / serving_maths }
* @ license http :// www . gnu . org / copyleft / gpl . html GNU Public License
* @ package quiz
*/
2003-01-05 14:19:20 +00:00
require_once ( " ../../config.php " );
2006-02-24 10:43:06 +00:00
require_once ( $CFG -> dirroot . '/mod/quiz/editlib.php' );
2002-10-14 09:07:13 +00:00
require_login ();
2005-09-14 11:17:24 +00:00
$courseid = optional_param ( 'courseid' );
$quizid = optional_param ( 'quizid' );
2003-12-10 20:03:59 +00:00
2005-03-06 12:00:46 +00:00
$strquizzes = get_string ( 'modulenameplural' , 'quiz' );
2005-04-12 08:48:13 +00:00
$strquiz = get_string ( 'modulename' , 'quiz' );
$streditingquestions = get_string ( 'editquestions' , " quiz " );
$streditingquiz = get_string ( " editinga " , " moodle " , $strquiz );
2005-03-06 12:00:46 +00:00
2005-01-22 18:17:33 +00:00
if ( $modform = data_submitted () and ! empty ( $modform -> course )) { // data submitted
2002-10-14 09:07:13 +00:00
$SESSION -> modform = $modform ; // Save the form in the current session
2005-01-02 07:15:19 +00:00
} else if ( $quizid ) {
2005-05-10 10:12:24 +00:00
if ( isset ( $SESSION -> modform -> id ) and $SESSION -> modform -> id == $quizid ) {
2005-05-06 06:24:04 +00:00
// modform for this quiz already exists, use it
$modform = $SESSION -> modform ;
} else {
// create new modform from database
if ( ! $modform = get_record ( 'quiz' , 'id' , $quizid )) {
error ( " The required quiz doesn't exist " );
}
$modform -> instance = $modform -> id ;
$SESSION -> modform = $modform ; // Save the form in the current session
$cm = get_coursemodule_from_instance ( 'quiz' , $modform -> instance );
$modform -> cmid = $cm -> id ;
// We don't log all visits to this page but only those that recreate modform
add_to_log ( $cm -> course , 'quiz' , 'editquestions' ,
" view.php?id= $cm->id " ,
" $quizid " , $cm -> id );
2005-01-02 07:15:19 +00:00
}
2003-12-10 20:03:59 +00:00
} else if ( $courseid ) { // Page retrieve through "Edit Questions" link - no quiz selected
$modform -> course = $courseid ;
unset ( $modform -> instance );
$SESSION -> modform = $modform ; // Save the form in the current session
2005-02-05 17:28:06 +00:00
2005-01-22 15:42:22 +00:00
add_to_log ( $courseid , 'quiz' , 'editquestions' , " index.php?id= $courseid " );
2005-02-05 17:28:06 +00:00
2005-09-14 11:17:24 +00:00
} else if ( ! empty ( $sortorder )) {
// no quiz or course was specified so we need to use the stored modform
if ( isset ( $SESSION -> modform )) {
$modform = $SESSION -> modform ;
} else {
exit ;
}
2002-10-14 09:07:13 +00:00
} else {
2005-06-04 09:58:35 +00:00
// we might get here after editing a question in
// a popup window. So close window automatically.
2005-05-19 08:05:58 +00:00
?>
< script type = " text/javascript " >
<!--
if ( self . name == 'editquestion' ) {
self . close ();
}
-->
</ script >
< noscript >
< ? php notify ( get_string ( 'pleaseclose' , 'quiz' )); ?>
</ noscript >
< ? php
2005-06-04 09:58:35 +00:00
// no quiz or course was specified so we need to use the stored modform
2005-09-14 11:17:24 +00:00
if ( isset ( $SESSION -> modform )) {
2005-06-04 09:58:35 +00:00
$modform = $SESSION -> modform ;
} else {
2005-05-17 22:22:29 +00:00
exit ;
2002-10-14 09:07:13 +00:00
}
}
if ( ! $course = get_record ( " course " , " id " , $modform -> course )) {
error ( " This course doesn't exist " );
}
2005-02-16 10:40:48 +00:00
require_login ( $course -> id , false );
2002-10-14 09:07:13 +00:00
2003-12-10 20:03:59 +00:00
if ( ! isteacheredit ( $course -> id )) {
2002-10-14 09:07:13 +00:00
error ( " You can't modify this course! " );
}
2003-12-10 20:03:59 +00:00
if ( isset ( $modform -> instance )
&& empty ( $modform -> grades )) // Construct an array to hold all the grades.
{
2005-05-06 06:24:04 +00:00
$modform -> grades = quiz_get_all_question_grades ( $modform );
2002-10-14 12:21:18 +00:00
}
2005-01-22 18:17:33 +00:00
/// Now, check for commands on this page and modify variables as necessary
2002-10-14 09:07:13 +00:00
2005-01-22 18:17:33 +00:00
if ( isset ( $_REQUEST [ 'up' ]) and confirm_sesskey ()) { /// Move the given question up a slot
2002-10-14 09:07:13 +00:00
$questions = explode ( " , " , $modform -> questions );
2005-05-06 06:24:04 +00:00
if ( $up > 0 and isset ( $questions [ $up ])) {
$prevkey = ( $questions [ $up - 1 ] == 0 ) ? $up - 2 : $up - 1 ;
$swap = $questions [ $prevkey ];
$questions [ $prevkey ] = $questions [ $up ];
$questions [ $up ] = $swap ;
2002-10-14 09:07:13 +00:00
$modform -> questions = implode ( " , " , $questions );
2005-05-06 06:24:04 +00:00
// Always have a page break at the end
$modform -> questions = $modform -> questions . ',0' ;
// Avoid duplicate page breaks
$modform -> questions = str_replace ( ',0,0' , ',0' , $modform -> questions );
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
}
2005-01-22 15:42:22 +00:00
}
2002-10-14 09:07:13 +00:00
}
2005-01-22 18:17:33 +00:00
if ( isset ( $_REQUEST [ 'down' ]) and confirm_sesskey ()) { /// Move the given question down a slot
2002-10-14 09:07:13 +00:00
$questions = explode ( " , " , $modform -> questions );
2005-05-06 06:24:04 +00:00
if ( $down < count ( $questions )) {
$nextkey = ( $questions [ $down + 1 ] == 0 ) ? $down + 2 : $down + 1 ;
$swap = $questions [ $nextkey ];
$questions [ $nextkey ] = $questions [ $down ];
$questions [ $down ] = $swap ;
2002-10-14 09:07:13 +00:00
$modform -> questions = implode ( " , " , $questions );
2005-05-06 06:24:04 +00:00
// Avoid duplicate page breaks
$modform -> questions = str_replace ( ',0,0' , ',0' , $modform -> questions );
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
}
2005-01-22 15:42:22 +00:00
}
2002-10-14 09:07:13 +00:00
}
2005-03-05 12:55:26 +00:00
if ( isset ( $_REQUEST [ 'addquestion' ]) and confirm_sesskey ()) { /// Add a single question to the current quiz
2005-05-06 06:24:04 +00:00
quiz_add_quiz_question ( $_REQUEST [ 'addquestion' ], $modform );
}
2005-04-12 08:48:13 +00:00
2005-05-06 06:24:04 +00:00
if ( isset ( $_REQUEST [ 'add' ]) and confirm_sesskey ()) { /// Add selected questions to the current quiz
foreach ( $_POST as $key => $value ) { // Parse input for question ids
if ( substr ( $key , 0 , 1 ) == " q " ) {
quiz_add_quiz_question ( substr ( $key , 1 ), $modform );
}
2005-03-05 12:55:26 +00:00
}
}
2005-05-06 06:24:04 +00:00
if ( isset ( $_REQUEST [ 'addrandom' ]) and confirm_sesskey ()) { /// Add random questions to the quiz
$recurse = optional_param ( 'recurse' , 0 , PARAM_BOOL );
$categoryid = required_param ( 'categoryid' , PARAM_INT );
$randomcount = required_param ( 'randomcount' , PARAM_INT );
// load category
if ( ! $category = get_record ( 'quiz_categories' , 'id' , $categoryid )) {
error ( 'Category ID is incorrect' );
2002-10-14 15:57:33 +00:00
}
2005-05-06 06:24:04 +00:00
// find existing random questions in this category
$random = RANDOM ;
if ( $existingquestions = get_records_select ( 'quiz_questions' , " qtype = ' $random ' AND category = ' $category->id ' " )) {
// now remove the ones that are already used in this quiz
if ( $questionids = explode ( ',' , $modform -> questions )) {
foreach ( $questionids as $questionid ) {
unset ( $existingquestions [ $questionid ]);
2002-10-14 09:07:13 +00:00
}
2005-05-06 06:24:04 +00:00
}
// now take as many of these as needed
$i = 0 ;
while (( $existingquestion = array_pop ( $existingquestions )) and ( $i < $randomcount )) {
if ( $existingquestion -> questiontext == $recurse ) {
// this question has the right recurse property, so use it
quiz_add_quiz_question ( $existingquestion -> id , $modform );
$i ++ ;
}
}
$randomcreate = $randomcount - $i ; // the number of additional random questions needed.
} else {
$randomcreate = $randomcount ;
}
2003-07-06 04:00:05 +00:00
2005-05-06 06:24:04 +00:00
if ( $randomcreate > 0 ) {
$form -> name = get_string ( 'random' , 'quiz' ) . ' (' . $category -> name . ')' ;
$form -> questiontext = $recurse ; // we use the questiontext field to store the info
// on whether to include questions in subcategories
$form -> questiontextformat = 0 ;
$form -> image = '' ;
$form -> defaultgrade = 1 ;
$form -> hidden = 1 ;
for ( $i = 0 ; $i < $randomcreate ; $i ++ ) {
$form -> stamp = make_unique_id_code (); // Set the unique code (not to be changed)
$question = new stdClass ;
$question -> category = $category -> id ;
$question -> qtype = RANDOM ;
$question = $QUIZ_QTYPES [ RANDOM ] -> save_question ( $question , $form , $course );
if ( ! isset ( $question -> id )) {
error ( 'Could not insert new random question!' );
2003-03-03 17:43:13 +00:00
}
2005-05-06 06:24:04 +00:00
quiz_add_quiz_question ( $question -> id , $modform );
2002-10-14 09:07:13 +00:00
}
}
2005-05-06 06:24:04 +00:00
}
if ( isset ( $_REQUEST [ 'repaginate' ]) and confirm_sesskey ()) { /// Re-paginate the quiz
if ( isset ( $_REQUEST [ 'questionsperpage' ])) {
$modform -> questionsperpage = required_param ( 'questionsperpage' , 1 , PARAM_INT );
if ( ! set_field ( 'quiz' , 'questionsperpage' , $modform -> questionsperpage , 'id' , $modform -> id )) {
error ( 'Could not save number of questions per page' );
}
2002-10-15 10:04:28 +00:00
}
2005-05-06 06:24:04 +00:00
$modform -> questions = quiz_repaginate ( $modform -> questions , $modform -> questionsperpage );
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> id )) {
error ( 'Could not save layout' );
2005-01-22 15:42:22 +00:00
}
2002-10-14 09:07:13 +00:00
}
2005-03-06 12:00:46 +00:00
if ( isset ( $_REQUEST [ 'delete' ]) and confirm_sesskey ()) { /// Remove a question from the quiz
2005-05-06 06:24:04 +00:00
quiz_delete_quiz_question ( $_REQUEST [ 'delete' ], $modform );
2002-10-14 09:07:13 +00:00
}
2006-02-19 16:34:06 +00:00
if ( isset ( $_REQUEST [ 'savechanges' ]) and confirm_sesskey ()) {
/// We need to save the new ordering (if given) and the new grades
$oldquestions = explode ( " , " , $modform -> questions ); // the questions in the old order
$questions = array (); // for questions in the new order
2003-01-05 06:45:20 +00:00
$rawgrades = $_POST ;
2002-10-15 12:54:11 +00:00
unset ( $modform -> grades );
2002-10-14 09:07:13 +00:00
foreach ( $rawgrades as $key => $value ) { // Parse input for question -> grades
if ( substr ( $key , 0 , 1 ) == " q " ) {
$key = substr ( $key , 1 );
2002-10-14 12:21:18 +00:00
$modform -> grades [ $key ] = $value ;
2005-05-06 06:24:04 +00:00
quiz_update_question_instance ( $modform -> grades [ $key ], $key , $modform -> instance );
2006-02-19 16:34:06 +00:00
} elseif ( substr ( $key , 0 , 1 ) == " o " ) { // Parse input for ordering info
$key = substr ( $key , 1 );
$questions [ $value ] = $oldquestions [ $key ];
}
}
// If ordering info was given, reorder the questions
if ( $questions ) {
ksort ( $questions );
$modform -> questions = implode ( " , " , $questions );
// Always have a page break at the end
$modform -> questions = $modform -> questions . ',0' ;
// Avoid duplicate page breaks
while ( strpos ( $modform -> questions , ',0,0' )) {
$modform -> questions = str_replace ( ',0,0' , ',0' , $modform -> questions );
}
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
2002-10-14 09:07:13 +00:00
}
}
2005-05-06 06:24:04 +00:00
// If rescaling is required save the new maximum
if ( isset ( $_REQUEST [ 'maxgrade' ])) {
$modform -> grade = optional_param ( 'maxgrade' , 0 );
if ( ! set_field ( 'quiz' , 'grade' , $modform -> grade , 'id' , $modform -> instance )) {
error ( 'Could not set new maximal grade for quiz' );
}
2005-01-02 07:15:19 +00:00
}
}
2005-02-05 17:28:06 +00:00
2005-05-06 06:24:04 +00:00
if ( isset ( $_REQUEST [ 'showbreaks' ])) {
$SESSION -> quiz_showbreaks = optional_param ( 'showbreaks' , 0 , PARAM_BOOL );
2006-02-19 16:34:06 +00:00
$SESSION -> quiz_reordertool = optional_param ( 'reordertool' , 0 , PARAM_BOOL );
2005-05-06 06:24:04 +00:00
}
/// Delete any teacher preview attempts if the quiz has been modified
2005-05-07 13:30:35 +00:00
if ( isset ( $_REQUEST [ 'setgrades' ]) or isset ( $_REQUEST [ 'delete' ]) or isset ( $_REQUEST [ 'repaginate' ]) or isset ( $_REQUEST [ 'addrandom' ]) or isset ( $_REQUEST [ 'addquestion' ]) or isset ( $_REQUEST [ 'up' ]) or isset ( $_REQUEST [ 'down' ]) or isset ( $_REQUEST [ 'add' ])) {
2005-05-06 06:24:04 +00:00
delete_records ( 'quiz_attempts' , 'preview' , '1' , 'quiz' , $modform -> id );
2005-01-22 18:17:33 +00:00
}
2005-02-05 17:28:06 +00:00
2005-01-22 18:17:33 +00:00
/// all commands have been dealt with, now print the page
2002-10-14 09:07:13 +00:00
2005-02-05 17:28:06 +00:00
if ( empty ( $modform -> category ) or ! record_exists ( 'quiz_categories' , 'id' , $modform -> category )) {
2005-01-06 08:46:12 +00:00
$category = quiz_get_default_category ( $course -> id );
$modform -> category = $category -> id ;
2005-01-05 17:27:23 +00:00
}
2005-05-06 06:24:04 +00:00
if ( ! isset ( $SESSION -> quiz_showbreaks )) {
$SESSION -> quiz_showbreaks = ( $CFG -> quiz_questionsperpage < 2 ) ? 0 : 1 ;
2005-03-03 15:17:45 +00:00
}
2006-02-19 16:34:06 +00:00
if ( ! isset ( $SESSION -> quiz_reordertool )) {
$SESSION -> quiz_reordertool = 0 ;
}
2002-12-30 05:10:01 +00:00
2002-10-14 09:07:13 +00:00
$SESSION -> modform = $modform ;
// Print basic page layout.
2005-02-06 07:45:40 +00:00
2005-05-06 06:24:04 +00:00
if ( isset ( $modform -> instance ) and record_exists_sql ( " SELECT * FROM { $CFG -> prefix } quiz_attempts WHERE quiz = ' $modform->instance ' AND preview = '0' LIMIT 1 " )){
// one column layout with table of questions used in this quiz
$strupdatemodule = isteacheredit ( $course -> id )
? update_module_button ( $modform -> cmid , $course -> id , get_string ( 'modulename' , 'quiz' ))
: " " ;
2005-04-12 08:48:13 +00:00
print_header_simple ( $streditingquiz , '' ,
2005-02-05 21:03:04 +00:00
" <a href= \" index.php?id= $course->id\ " > $strquizzes </ a > " .
2005-05-06 06:24:04 +00:00
" -> <a href= \" view.php?q= $modform->instance\ " > " .format_string( $modform->name ).'</a>'.
" -> $streditingquiz " , " " , " " ,
true , $strupdatemodule );
$currenttab = 'edit' ;
2006-02-09 13:57:22 +00:00
$mode = 'editq' ;
2005-05-06 06:24:04 +00:00
$quiz = & $modform ;
include ( 'tabs.php' );
2005-02-05 21:03:04 +00:00
print_simple_box_start ( " center " );
2005-05-06 06:24:04 +00:00
$attemptcount = count_records ( 'quiz_attempts' , 'quiz' , $modform -> instance , 'preview' , 0 );
2005-02-05 21:03:04 +00:00
$strviewallanswers = get_string ( " viewallanswers " , " quiz " , $attemptcount );
$strattemptsexist = get_string ( " attemptsexist " , " quiz " );
2005-05-06 06:24:04 +00:00
$usercount = count_records_select ( 'quiz_attempts' , " quiz = ' $modform->id ' AND preview = '0' " , 'COUNT(DISTINCT userid)' );
$strusers = $course -> students ;
2005-02-05 21:03:04 +00:00
if ( ! $cm = get_coursemodule_from_instance ( " quiz " , $modform -> instance , $course -> id )) {
error ( " Course Module ID was incorrect " );
}
2005-05-09 13:07:11 +00:00
echo " <center> \n " ;
2005-06-05 20:51:15 +00:00
echo " $strattemptsexist <br /><a href= \" report.php?mode=overview&id= $cm->id\ " > $strviewallanswers ( $usercount $strusers ) </ a > " ;
2005-05-09 13:07:11 +00:00
echo " <form target= \" _parent \" method= \" get \" action= \" $CFG->wwwroot /mod/quiz/edit.php \" > \n " ;
echo " <input type= \" hidden \" name= \" courseid \" value= \" $course->id\ " /> \n " ;
echo " <input type= \" submit \" value= \" " . get_string ( " editcatquestions " , " quiz " ) . " \" /> \n " ;
echo " </form> " ;
2005-06-05 20:51:15 +00:00
echo " </center><br/ > \n " ;
2005-05-09 13:07:11 +00:00
2006-02-19 16:34:06 +00:00
$sumgrades = quiz_print_question_list ( $modform , false , $SESSION -> quiz_showbreaks , $SESSION -> quiz_reordertool );
2005-02-05 21:03:04 +00:00
if ( ! set_field ( 'quiz' , 'sumgrades' , $sumgrades , 'id' , $modform -> instance )) {
error ( 'Failed to set sumgrades' );
}
print_simple_box_end ();
print_footer ( $course );
exit ;
}
2002-10-14 09:07:13 +00:00
2004-01-09 12:57:33 +00:00
if ( ! isset ( $modform -> instance )) {
2005-01-22 18:17:33 +00:00
// one column layout for non-quiz-specific editing page
2005-04-12 08:48:13 +00:00
print_header_simple ( $streditingquestions , '' ,
2006-02-17 17:15:17 +00:00
" $streditingquestions " );
print_heading_with_help ( get_string ( 'questions' , 'quiz' ), 'questionbank' );
2004-01-07 13:10:13 +00:00
echo '<table align="center" border="0" cellpadding="2" cellspacing="0">' ;
echo '<tr><td valign="top">' ;
2002-10-14 09:07:13 +00:00
2004-01-07 13:10:13 +00:00
} else {
2005-01-22 18:17:33 +00:00
// two column layout with quiz info in left column
2005-05-06 06:24:04 +00:00
$strupdatemodule = isteacheredit ( $course -> id )
? update_module_button ( $modform -> cmid , $course -> id , get_string ( 'modulename' , 'quiz' ))
: " " ;
2005-04-12 08:48:13 +00:00
print_header_simple ( $streditingquiz , '' ,
2004-12-20 14:26:37 +00:00
" <a href= \" index.php?id= $course->id\ " > $strquizzes </ a > " .
2005-05-06 06:24:04 +00:00
" -> <a href= \" view.php?q= $modform->instance\ " > " .format_string( $modform->name ).'</a>'.
" -> $streditingquiz " ,
" " , " " , true , $strupdatemodule );
$currenttab = 'edit' ;
2006-02-09 13:57:22 +00:00
$mode = 'editq' ;
2005-05-06 06:24:04 +00:00
$quiz = & $modform ;
include ( 'tabs.php' );
2004-01-07 13:10:13 +00:00
echo '<table border="0" width="100%" cellpadding="2" cellspacing="0">' ;
echo '<tr><td width="50%" valign="top">' ;
2005-02-05 17:28:06 +00:00
print_simple_box_start ( " center " , " 100% " );
2005-05-06 06:24:04 +00:00
2006-02-19 16:34:06 +00:00
$sumgrades = quiz_print_question_list ( $modform , true , $SESSION -> quiz_showbreaks , $SESSION -> quiz_reordertool );
2005-01-22 16:13:08 +00:00
if ( ! set_field ( 'quiz' , 'sumgrades' , $sumgrades , 'id' , $modform -> instance )) {
error ( 'Failed to set sumgrades' );
}
2004-05-19 03:22:21 +00:00
2002-10-14 09:07:13 +00:00
print_simple_box_end ();
2005-05-06 06:24:04 +00:00
2004-01-07 13:10:13 +00:00
echo '</td><td valign="top" width="50%">' ;
}
2006-02-24 10:43:06 +00:00
require ( $CFG -> dirroot . '/question/showbank.php' );
2006-02-17 17:15:17 +00:00
2005-02-05 21:03:04 +00:00
echo '</td></tr>' ;
echo '</table>' ;
2003-12-10 20:03:59 +00:00
2002-10-14 09:07:13 +00:00
print_footer ( $course );
2002-10-06 17:31:46 +00:00
?>