2004-09-12 17:34:35 +00:00
< ? php // $Id$
2002-10-06 17:31:46 +00:00
2003-01-05 14:19:20 +00:00
require_once ( " ../../config.php " );
2005-01-08 20:06:00 +00:00
require_once ( " locallib.php " );
2002-10-14 09:07:13 +00:00
require_login ();
2005-01-22 18:17:33 +00:00
$courseid = optional_param ( 'courseid' );
$quizid = optional_param ( 'quizid' );
$page = optional_param ( 'page' , 0 );
$perpage = optional_param ( 'perpage' , 20 );
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 ) {
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
2005-02-05 17:28:06 +00:00
2005-01-22 15:42:22 +00:00
$cm = get_coursemodule_from_instance ( 'quiz' , $modform -> instance );
2005-02-05 17:28:06 +00:00
add_to_log ( $cm -> course , 'quiz' , 'editquestions' ,
2005-03-21 00:06:04 +00:00
" 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
2002-10-14 09:07:13 +00:00
} else {
if ( ! isset ( $SESSION -> modform )) {
2005-01-05 17:27:23 +00:00
// We currently also get here after editing a question by
// following the edit link on the review page. Perhaps that should be fixed.
2005-01-02 07:15:19 +00:00
error ( '' );
2002-10-14 09:07:13 +00:00
}
2005-02-05 17:28:06 +00:00
// The data is obtained from a $SESSION variable. This is mostly for historic reasons.
2005-01-22 15:42:22 +00:00
// With the way things work now it would be just as possible to get the data from the database.
2002-10-14 09:07:13 +00:00
$modform = $SESSION -> modform ;
}
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.
{
2002-10-14 12:21:18 +00:00
$modform -> grades = quiz_get_all_question_grades ( $modform -> questions , $modform -> instance );
}
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 );
if ( $questions [ 0 ] <> $up ) {
foreach ( $questions as $key => $question ) {
if ( $up == $question ) {
$swap = $questions [ $key - 1 ];
$questions [ $key - 1 ] = $question ;
$questions [ $key ] = $swap ;
break ;
}
}
$modform -> questions = implode ( " , " , $questions );
}
2005-01-22 15:42:22 +00:00
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-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 );
if ( $questions [ count ( $questions ) - 1 ] <> $down ) {
foreach ( $questions as $key => $question ) {
if ( $down == $question ) {
$swap = $questions [ $key + 1 ];
$questions [ $key + 1 ] = $question ;
$questions [ $key ] = $swap ;
break ;
}
}
$modform -> questions = implode ( " , " , $questions );
}
2005-01-22 15:42:22 +00:00
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-03-05 12:55:26 +00:00
if ( isset ( $_REQUEST [ 'addquestion' ]) and confirm_sesskey ()) { /// Add a single question to the current quiz
2005-04-12 08:48:13 +00:00
2005-03-05 12:55:26 +00:00
// add question to quiz->questions
$key = $_REQUEST [ 'addquestion' ];
2005-03-14 06:22:55 +00:00
$questions = array ();
2005-03-05 12:55:26 +00:00
if ( ! empty ( $modform -> questions )) {
$questions = explode ( " , " , $modform -> questions );
}
2005-03-14 06:22:55 +00:00
if ( ! in_array ( $key , $questions )) {
$questions [] = $key ;
}
2005-03-05 12:55:26 +00:00
$modform -> questions = implode ( " , " , $questions );
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
}
2005-04-12 08:48:13 +00:00
2005-03-05 12:55:26 +00:00
// update question grades
$questionrecord = get_record ( " quiz_questions " , " id " , $key );
if ( ! empty ( $questionrecord -> defaultgrade )) {
$modform -> grades [ $key ] = $questionrecord -> defaultgrade ;
} else if ( $questionrecord -> qtype == DESCRIPTION ){
$modform -> grades [ $key ] = 0 ;
} else {
$modform -> grades [ $key ] = 1 ;
}
quiz_questiongrades_update ( $modform -> grades , $modform -> instance );
}
if ( isset ( $_REQUEST [ 'add' ]) and confirm_sesskey ()) { /// Add selected questions to the current quiz
2003-01-05 06:45:20 +00:00
$rawquestions = $_POST ;
2002-12-30 05:10:01 +00:00
if ( ! empty ( $modform -> questions )) {
2002-10-14 15:57:33 +00:00
$questions = explode ( " , " , $modform -> questions );
}
2002-10-14 09:07:13 +00:00
foreach ( $rawquestions as $key => $value ) { // Parse input for question ids
if ( substr ( $key , 0 , 1 ) == " q " ) {
$key = substr ( $key , 1 );
2003-02-24 10:37:56 +00:00
if ( ! empty ( $questions )) {
2002-10-14 15:57:33 +00:00
foreach ( $questions as $question ) {
if ( $question == $key ) {
continue 2 ;
}
2002-10-14 09:07:13 +00:00
}
}
$questions [] = $key ;
2003-03-03 17:43:13 +00:00
$questionrecord = get_record ( " quiz_questions " , " id " , $key );
2003-07-06 04:00:05 +00:00
2003-03-03 17:43:13 +00:00
if ( ! empty ( $questionrecord -> defaultgrade )) {
2005-02-05 17:28:06 +00:00
$modform -> grades [ $key ] = $questionrecord -> defaultgrade ;
2003-07-06 04:00:05 +00:00
} else if ( $questionrecord -> qtype == DESCRIPTION ){
2005-02-05 17:28:06 +00:00
$modform -> grades [ $key ] = 0 ;
2003-03-03 17:43:13 +00:00
} else {
2005-02-05 17:28:06 +00:00
$modform -> grades [ $key ] = 1 ;
2003-03-03 17:43:13 +00:00
}
2002-10-14 09:07:13 +00:00
}
}
2002-12-30 05:10:01 +00:00
if ( ! empty ( $questions )) {
2002-10-15 10:04:28 +00:00
$modform -> questions = implode ( " , " , $questions );
} else {
$modform -> questions = " " ;
}
2005-01-22 15:42:22 +00:00
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
}
quiz_questiongrades_update ( $modform -> grades , $modform -> instance );
2002-10-14 09:07:13 +00:00
}
2005-03-06 12:00:46 +00:00
if ( isset ( $_REQUEST [ 'move' ]) and confirm_sesskey ()) { /// Move selected questions to new category
if ( ! $tocategory = get_record ( 'quiz_categories' , 'id' , $_REQUEST [ 'category' ])) {
error ( 'Invalid category' );
}
2005-03-16 12:35:19 +00:00
if ( ! isteacheredit ( $tocategory -> course )) {
error ( get_string ( 'categorynoedit' , 'quiz' , $tocategory -> name ), 'edit.php' );
2005-03-06 12:00:46 +00:00
}
$rawquestions = $_POST ;
foreach ( $rawquestions as $key => $value ) { // Parse input for question ids
if ( substr ( $key , 0 , 1 ) == " q " ) {
$key = substr ( $key , 1 );
if ( ! set_field ( 'quiz_questions' , 'category' , $tocategory -> id , 'id' , $key )) {
error ( 'Could not update category field' );
}
}
}
}
if ( isset ( $_REQUEST [ 'delete' ]) and confirm_sesskey ()) { /// Remove a question from the quiz
2002-10-14 09:07:13 +00:00
$questions = explode ( " , " , $modform -> questions );
foreach ( $questions as $key => $question ) {
if ( $question == $delete ) {
unset ( $questions [ $key ]);
2002-10-14 12:21:18 +00:00
unset ( $modform -> grades [ $question ]);
2005-03-14 06:55:01 +00:00
// Delete all responses associated with all attempts for this question in the quiz.
if ( $attempts = get_records ( 'quiz_attempts' , 'quiz' , $modform -> instance )) {
foreach ( $attempts as $attempt ) {
if ( ! delete_records ( 'quiz_responses' , 'question' , $question , 'attempt' , $attempt -> id )) {
error ( 'Could not delete all responses for this question' );
}
}
}
// Delete question from quiz_question_grades table
if ( ! delete_records ( 'quiz_question_grades' , 'quiz' , $modform -> instance , 'question' , $question )) {
error ( 'Could not delete the question from this quiz' );
2005-01-22 15:42:22 +00:00
}
2002-10-14 09:07:13 +00:00
}
}
$modform -> questions = implode ( " , " , $questions );
2005-01-22 15:42:22 +00:00
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-03-06 12:00:46 +00:00
if ( isset ( $_REQUEST [ 'deleteselected' ])) { // delete selected questions from the category
if ( isset ( $confirm ) and confirm_sesskey ()) { // teacher has already confirmed the action
if ( $confirm == md5 ( $deleteselected )) {
if ( $questionlist = explode ( ',' , $deleteselected )) {
// for each question either hide it if it is in use or delete it
foreach ( $questionlist as $questionid ) {
2005-04-12 08:48:13 +00:00
if ( record_exists ( 'quiz_responses' , 'question' , $questionid ) or
2005-03-06 12:00:46 +00:00
record_exists ( 'quiz_responses' , 'originalquestion' , $questionid ) or
record_exists ( 'quiz_question_grades' , 'question' , $questionid )) {
if ( ! set_field ( 'quiz_questions' , 'hidden' , 1 , 'id' , $questionid )) {
error ( 'Was not able to hide question' );
}
} else {
if ( ! delete_records ( " quiz_questions " , " id " , $questionid )) {
error ( " An error occurred trying to delete question (id $questionid ) " );
}
}
}
}
redirect ( " edit.php " );
} else {
error ( " Confirmation string was incorrect " );
}
2005-04-12 08:48:13 +00:00
2005-03-06 12:00:46 +00:00
} else { // teacher still has to confirm
// make a list of all the questions that are selected
$rawquestions = $_POST ;
$questionlist = '' ; // comma separated list of ids of questions to be deleted
$questionnames = '' ; // string with names of questions separated by <br /> with
// an asterix in front of those that are in use
$inuse = false ; // set to true if at least one of the questions is in use
foreach ( $rawquestions as $key => $value ) { // Parse input for question ids
if ( substr ( $key , 0 , 1 ) == " q " ) {
$key = substr ( $key , 1 );
$questionlist .= $key . ',' ;
2005-04-12 08:48:13 +00:00
if ( record_exists ( 'quiz_responses' , 'question' , $key ) or
2005-03-06 12:00:46 +00:00
record_exists ( 'quiz_responses' , 'originalquestion' , $key ) or
record_exists ( 'quiz_question_grades' , 'question' , $key )) {
$questionnames .= '* ' ;
$inuse = true ;
}
$questionnames .= get_field ( 'quiz_questions' , 'name' , 'id' , $key ) . '<br />' ;
}
}
if ( ! $questionlist ) { // no questions were selected
redirect ( 'edit.php' );
}
$questionlist = rtrim ( $questionlist , ',' );
// Add an explanation about questions in use
2005-04-12 08:48:13 +00:00
if ( $inuse ) {
2005-03-06 12:00:46 +00:00
$questionnames .= get_string ( 'questionsinuse' , 'quiz' );
}
2005-04-12 08:48:13 +00:00
print_header_simple ( $streditingquestions , '' ,
2005-03-06 12:00:46 +00:00
" <a href= \" index.php?id= $course->id\ " > $strquizzes </ a > " .
2005-04-12 08:48:13 +00:00
" -> $streditingquestions " );
notice_yesno ( get_string ( " deletequestionscheck " , " quiz " , $questionnames ),
2005-03-06 12:00:46 +00:00
" edit.php?sesskey= $USER->sesskey &deleteselected= $questionlist &confirm= " . md5 ( $questionlist ), " edit.php " );
print_footer ( $course );
exit ;
}
}
2005-01-22 18:17:33 +00:00
if ( isset ( $_REQUEST [ 'setgrades' ]) and confirm_sesskey ()) { /// The grades have been updated, so update our internal list
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 ;
2002-10-14 09:07:13 +00:00
}
}
2005-01-22 15:42:22 +00:00
if ( ! set_field ( 'quiz' , 'questions' , $modform -> questions , 'id' , $modform -> instance )) {
error ( 'Could not save question list' );
2005-01-02 07:15:19 +00:00
}
2005-01-22 15:42:22 +00:00
quiz_questiongrades_update ( $modform -> grades , $modform -> instance );
2005-01-02 07:15:19 +00:00
}
2005-02-05 17:28:06 +00:00
2005-01-22 18:17:33 +00:00
if ( isset ( $_REQUEST [ 'cat' ])) { /// coming from category selection drop-down menu
2002-10-15 17:32:50 +00:00
$modform -> category = $cat ;
2002-10-14 09:07:13 +00:00
}
2005-02-05 17:28:06 +00:00
2005-03-03 15:17:45 +00:00
if ( isset ( $_REQUEST [ 'displayoptions' ])) {
$modform -> recurse = isset ( $_REQUEST [ 'recurse' ]) ? 1 : 0 ;
$modform -> showhidden = isset ( $_REQUEST [ 'showhidden' ]);
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
}
if ( ! isset ( $modform -> recurse )) {
$modform -> recurse = 1 ;
2002-12-30 05:10:01 +00:00
}
2005-03-03 15:17:45 +00:00
if ( ! isset ( $modform -> showhidden )) {
$modform -> showhidden = false ;
}
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
if ( isset ( $modform -> instance ) and record_exists_sql ( " SELECT * FROM { $CFG -> prefix } quiz_attempts WHERE quiz = ' $modform->instance ' AND NOT (userid = ' $USER->id ') LIMIT 1 " )){
2005-02-05 21:03:04 +00:00
// one column layout with table of questions used in this 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-04-03 00:19:11 +00:00
" -> <a href= \" view.php?q= $modform->instance\ " > " .format_string( $modform->name ,true). " </ a > " .
2005-04-12 08:48:13 +00:00
" -> $streditingquiz " );
2005-02-05 21:03:04 +00:00
print_simple_box_start ( " center " );
2005-04-03 00:19:11 +00:00
print_heading ( format_string ( $modform -> name ));
2005-02-05 21:03:04 +00:00
$attemptcount = count_records_select ( " quiz_attempts " , " quiz = ' $modform->instance ' AND timefinish > 0 " );
$strviewallanswers = get_string ( " viewallanswers " , " quiz " , $attemptcount );
$strattemptsexist = get_string ( " attemptsexist " , " quiz " );
$usercount = count_records ( " quiz_grades " , " quiz " , " $modform->instance " );
$strusers = get_string ( " users " );
if ( ! $cm = get_coursemodule_from_instance ( " quiz " , $modform -> instance , $course -> id )) {
error ( " Course Module ID was incorrect " );
}
notify ( " $strattemptsexist <br /><a href= \" report.php?id= $cm->id\ " > $strviewallanswers ( $usercount $strusers ) </ a > " );
2005-03-07 19:45:39 +00:00
$sumgrades = quiz_print_question_list ( $modform -> questions , $modform -> grades , false , $modform -> instance );
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_continue ( 'view.php?q=' . $modform -> instance );
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 , '' ,
2004-12-20 14:26:37 +00:00
" <a href= \" index.php?id= $course->id\ " > $strquizzes </ a > " .
2005-04-12 08:48:13 +00:00
" -> $streditingquestions " );
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-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-04-03 00:19:11 +00:00
" -> <a href= \" view.php?q= $modform->instance\ " > " .format_string( $modform->name ,true). " </ a > " .
2005-04-12 08:48:13 +00:00
" -> $streditingquiz " );
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-04-03 00:19:11 +00:00
print_heading ( format_string ( $modform -> name ));
2005-03-07 19:45:39 +00:00
$sumgrades = quiz_print_question_list ( $modform -> questions , $modform -> grades , true , $modform -> instance );
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-02-05 21:03:04 +00:00
print_continue ( 'view.php?q=' . $modform -> instance );
2004-01-07 13:10:13 +00:00
echo '</td><td valign="top" width="50%">' ;
}
2005-01-22 18:17:33 +00:00
// non-quiz-specific column
2005-02-05 17:28:06 +00:00
print_simple_box_start ( " center " , " 100% " );
2005-01-22 18:17:33 +00:00
// starts with category selection form
2005-03-03 15:17:45 +00:00
quiz_print_category_form ( $course , $modform -> category , $modform -> recurse , $modform -> showhidden );
2004-01-07 13:10:13 +00:00
print_simple_box_end ();
2005-02-05 17:28:06 +00:00
2004-01-07 13:10:13 +00:00
print_spacer ( 5 , 1 );
2005-01-22 18:17:33 +00:00
// continues with list of questions
2005-02-05 17:28:06 +00:00
print_simple_box_start ( " center " , " 100% " );
2005-03-06 12:00:46 +00:00
quiz_print_cat_question_list ( $course , $modform -> category ,
2005-03-03 15:17:45 +00:00
isset ( $modform -> instance ), $modform -> recurse , $page , $perpage , $modform -> showhidden );
2004-01-07 13:10:13 +00:00
print_simple_box_end ();
2004-01-09 12:57:33 +00:00
if ( ! isset ( $modform -> instance )) {
2003-12-10 20:03:59 +00:00
print_continue ( " index.php?id= $modform->course " );
2005-01-22 18:17:33 +00:00
} else {
print_continue ( 'view.php?q=' . $modform -> instance );
2003-12-10 20:03:59 +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
?>