From: Juliette W. <jv...@us...> - 2005-06-14 16:34:06
|
Update of /cvsroot/stack/stack-1-0 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30619 Modified Files: Tag: frontend_dev question_bank.php Added Files: Tag: frontend_dev question_bank_util.php Log Message: Refactored some of the question bank code. Incomplete and could do with more testing. --- NEW FILE: question_bank_util.php --- <?php /** * * Welcome to STACK. A system for teaching and assessment using a * computer algebra kernel. * <br> * This file is licensed under the GPL License. * <br> * A copy of the license is in your STACK distribution called * license.txt. If you are missing this file you can obtain * it from: * http://www.stack.bham.ac.uk/license.txt * <br> * Copyright (c) 2005, Christopher James Sangwin * * @author Chris Sangwin C.J...@bh... * @author Laura Naismith L.N...@bh... * * * This file contains the logic of interactions with the site as * an editor of questions and the question bank. * * @package frontend * @subpackage Stack */ function stack_question_in_session() { if (array_key_exists('question',$_SESSION)) { return TRUE; } else { return FALSE; } } function stack_get_question_from_session() { $question = 'unknown'; // Get any $question from the current $_SESSION if (array_key_exists('question',$_SESSION)) { foreach ($stackQuestion as $qfield => $val) { if (array_key_exists($qfield,$_SESSION['question'])) { if ('' != $_SESSION['question'][$qfield]) { $question[$qfield] = $_SESSION['question'][$qfield]; } } } unset($_SESSION['question']); } return $question; } function stack_get_questionInst_from_session() { $questionInst = 'unknown'; // Get any $questionInst from the current $_SESSION if (array_key_exists('questionInst',$_SESSION)) { $questionInst=$_SESSION['questionInst']; unset($_SESSION['questionInst']); } return $questionInst; } function stack_get_question_from_post() { // Get any $_POSTED data // This should over write any $_SESSION data, as it may have been entered in a form. foreach( $stackQuestion as $qfield => $attribs) { if ( array_key_exists($qfield,$_POST) ) { if ('questionOptions' == $qfield) { $question[$qfield] = $_POST[$qfield]; } else if ('questionPotResp' == $qfield ) { $question[$qfield] = $_POST[$qfield]; } else if ( '' !=trim($_POST[$qfield]) ) { // We need this: all fields on form will be present in $_POST. $question[$qfield] = $_POST[$qfield]; } } } return $question; } // $source should be 'from database', 'new', 'session' or 'post' function stack_get_question($source) { if ('from_database' == $source) { $questionID = $_POST['questionID']; $question=stack_db_getquestion($questionID); } else if ('new' == $source) { $question['questionID']='0'; $question['questionAnsKey']='ans1'; } else if ('session' == $source) { $question = stack_get_question_from_session(); } else if ('post' == $source) { $stack_get_question_from_post(); } else { echo "Invalid arg to stack_get_question"; // Error! How are we handling these? } } ?> Index: question_bank.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/question_bank.php,v retrieving revision 1.3.2.3 retrieving revision 1.3.2.4 diff -C2 -d -r1.3.2.3 -r1.3.2.4 *** question_bank.php 14 Jun 2005 16:01:04 -0000 1.3.2.3 --- question_bank.php 14 Jun 2005 16:33:56 -0000 1.3.2.4 *************** *** 35,38 **** --- 35,39 ---- include('html/javascripthead.html'); include('html/inputtool.html'); + include('question_bank_util.php'); $question = ''; *************** *** 65,123 **** // action = startover Begin again. - - include('html/pagehead.php'); ! ! // (2.1) edit, try or store. ! // There should be a question in the $POST or $SESSION if ('edit' == $action or 'mark' == $action or 'validate' == $action or 'store_in_db' == $action or 'store_new_db' == $action or 'try' == $action ! or 'new_version' == $action or 'export_xml' == $action) { ! ! // Get any $question from the current $_SESSION ! if (array_key_exists('question',$_SESSION)) ! { ! foreach ($stackQuestion as $qfield => $val) ! { ! if (array_key_exists($qfield,$_SESSION['question'])) ! { if ('' != $_SESSION['question'][$qfield]) ! { $question[$qfield] = $_SESSION['question'][$qfield];} ! } ! } ! unset($_SESSION['question']); ! } ! ! // Get any $questionInst from the current $_SESSION ! if (array_key_exists('questionInst',$_SESSION)) { ! $questionInst=$_SESSION['questionInst']; ! unset($_SESSION['questionInst']); ! } ! ! // Get any $_POSTED data ! // This should over write any $_SESSION data, as it may have been entered in a form. ! foreach( $stackQuestion as $qfield => $attribs) ! { ! if ( array_key_exists($qfield,$_POST) ) ! { ! if ('questionOptions' == $qfield) { ! $question[$qfield] = $_POST[$qfield]; ! } ! else if ('questionPotResp' == $qfield ) { ! $question[$qfield] = $_POST[$qfield]; ! } ! else if ( '' !=trim($_POST[$qfield]) ) { ! // We need this: all fields on form will be present in $_POST. ! $question[$qfield] = $_POST[$qfield]; ! } ! } ! } } // End (2.1) // (2.2) Get a question from the database. ! if ('edit_from_db'==$action or 'try_from_db'==$action or 'export_xml_db'==$action) { $questionID = $_POST['questionID']; $question=stack_db_getquestion($questionID); --- 66,106 ---- // action = startover Begin again. include('html/pagehead.php'); ! ! $get_question = FALSE; ! if ('edit' == $action or 'mark' == $action or 'validate' == $action or 'store_in_db' == $action or 'store_new_db' == $action or 'try' == $action ! or 'new_version' == $action or 'export_xml' == $action) { ! $get_question = TRUE; ! } ! ! $get_question_from_database = FALSE; ! ! if ('edit_from_db'==$action or 'try_from_db'==$action or 'export_xml_db'==$action) { ! $get_question_from_database = TRUE; ! } ! $get_new_question = FALSE; ! ! if ('start_new' == $action) { ! $get_new_question = TRUE; ! } ! // (2.1) edit, try or store. ! // There should be a question in the $POST or $SESSION ! if ($get_question) { + // $_POSTED data should over write any $_SESSION data, as it may have been + // entered in a form. + $question = stack_get_question_from_session(); + $questionInst = stack_get_questionInst_from_session(); + $question = stack_get_question_from_post(); } + // End (2.1) // (2.2) Get a question from the database. ! if ($get_question_from_database) { $questionID = $_POST['questionID']; $question=stack_db_getquestion($questionID); *************** *** 126,130 **** // (2.3) Provide a minimal blank question ! if ('start_new' == $action) { $question['questionID']='0'; $question['questionAnsKey']='ans1'; --- 109,113 ---- // (2.3) Provide a minimal blank question ! if ($get_new_question) { $question['questionID']='0'; $question['questionAnsKey']='ans1'; |