From: pkiddie <pk...@us...> - 2005-08-10 03:11:36
|
Update of /cvsroot/stack/stack-1-0/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31794/scripts Modified Files: Tag: development_xmlrqp stackXML.php Log Message: Changed file name of schemas Writing out quiz XML files Index: stackXML.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackXML.php,v retrieving revision 1.13.2.8 retrieving revision 1.13.2.9 diff -C2 -d -r1.13.2.8 -r1.13.2.9 *** stackXML.php 8 Aug 2005 11:50:29 -0000 1.13.2.8 --- stackXML.php 8 Aug 2005 22:10:24 -0000 1.13.2.9 *************** *** 99,110 **** // Make sure the question is validated. stack_question_validate($question,$errors); - - // Can't have numbered fields in the XML - // (1) Remove keyvals fields - //foreach($stackQuestion as $qfield => $attribs) { - // if ('keyval' == $attribs['type'] and array_key_exists($qfield,$question)) { - // unset($question[$qfield]); - // } - //} $xmlDoc = new DOMIT_Document(); --- 99,102 ---- *************** *** 112,127 **** $xmlDoc->appendChild($xmlDoc->createProcessingInstruction('xml', "version=\"1.0\" encoding=\"utf-8\"")); ! $questionFrag = &stack_xml_create_instance_header($xmlNamespaces,'assessmentItem'); ! //$metaElement = &new DOMIT_Element(); ! //Now construct the main body of the XML file ! //1. The metadata ! //Iterate through the question fields, then picking out the respective fields - and only write out if they contain a metatag //For this we need to iterate through both the question and stackQuestion structures. //Then we retrieve the actual data within the question structure foreach($stackQuestion as $qfield => $attribs) { if ('meta' == $attribs['type'] && array_key_exists($qfield,$question)) { switch ($attribs['metatype']) { --- 104,133 ---- $xmlDoc->appendChild($xmlDoc->createProcessingInstruction('xml', "version=\"1.0\" encoding=\"utf-8\"")); ! $questionFrag = &stack_xml_create_question_frag($question); ! //Retrieve document element, then add the instance headers to it ! $questionFrag = &stack_xml_create_instance_header($xmlNamespaces,'assessmentItem'); ! $xmlDoc->appendChild($questionFrag); ! ! return $xmlDoc; ! } ! ! function stack_xml_create_question_frag($question) ! { ! $xmlDoc = new DOMIT_Document(); ! ! $questionFrag = new DOMIT_Element('assessmentItem'); ! ! //Iterate through the question fields, then picking out the respective fields - and only write out if they contain a metatag //For this we need to iterate through both the question and stackQuestion structures. //Then we retrieve the actual data within the question structure foreach($stackQuestion as $qfield => $attribs) { + + //Now construct the main body of the XML file + //1. The metadata if ('meta' == $attribs['type'] && array_key_exists($qfield,$question)) { + //Detect the metadata type switch ($attribs['metatype']) { *************** *** 152,199 **** } ! $metaElement->setText($question[$qfield]); //Add the contents of the metadata to that tag $questionFrag->appendChild($metaElement); } ! } ! ! $xmlDoc->appendChild($questionFrag); ! ! //2. Question variables themselves ! //Iterate through stackQuestion again - dont look at metadata terms, only question, options, and potresp ! foreach($stackQuestion as $qfield => $attribs) { ! if ('meta' != $attribs['type'] && array_key_exists($qfield,$question)) { ! if (is_array($question[$qfield])) { ! //Now we must detect what type it is - other/keyval ! switch($attribs['type']) { ! case ('keyval'): ! { ! //Then write out a keyval type ! print_r($qfield); ! } ! ! case ('other'): ! { ! } ! ! default: { } } } } ! } ! ! return $xmlDoc; } - //Takes the assessmentItem fragment, constructs a filename and saves it out to XML //Returns true if file was successfully written out, false otherwise --- 158,267 ---- } ! $metaElement->setText($question[$qfield]); //Add the contents of the field to metadata tag $questionFrag->appendChild($metaElement); } ! ! //We are interested in questionOptions and questionPotResp - other 'other' fields will not get written ! else if ('other' == $attribs['type'] && array_key_exists($qfield,$question)) { ! //If the field is a potential response ! if($qfield=='questionPotResp') ! { ! //Iterate through those reponses ! foreach ($question[$qfield] as $potResp) { ! //Create a potential response element ! $potRespElem = &$xmlDoc->createElementNS('',$qfield); ! ! //Iterate through fields of a potential response ! foreach ($potResp as $potRespKey => $potRespField) { ! //Create a potential response field element ! $potRespFieldElem = &$xmlDoc->createElementNS('',$potRespKey); ! ! if (is_array($potRespField)) //Test for true/false branches { ! foreach ($potRespField as $branchKey => $branchField) { + $potRespBranchElem = &$xmlDoc->createElementNS('',$branchKey) ; + $potRespBranchElem->setText($branchField); + + $potRespFieldElem->appendChild($potRespBranchElem); } } + + else //Otherwise is not a branched element, so just write field text + { + $potRespFieldElem->setText($potRespField); + } + + $potRespElem->appendChild($potRespFieldElem);//Append each field to a single response element } + $questionFrag->appendChild($potRespElem); //Append each potential response to main question fragment + + } + } + + //If the field is a list of question options + else if ($qfield=='questionOptions') + { + //Write head element + $questionElement = &$xmlDoc->createElementNS('',$qfield); + + foreach ($question[$qfield] as $optionField => $option) { + + $optionElement = &$xmlDoc->createElementNS('',$optionField); + $optionElement->setText($option); + + $questionElement->appendChild($optionElement); + } + } + else + { + echo("The field $qfield cannot be written out"); + } + $questionFrag->appendChild($questionElement); } ! //All other elements ! else if (array_key_exists($qfield,$question)) { ! ! //Write head element ! $questionElement = &$xmlDoc->createElementNS('',$qfield); ! ! if (is_array($question[$qfield])) ! { ! //Is it of type keyval? ! if ($attribs['type']=='keyval') ! { ! foreach ($question[$qfield] as $keyvalType) { ! $keyElement = &$xmlDoc->createElementNS('',"key"); ! $keyElement->setText($keyvalType['key']); ! ! $valElement = &$xmlDoc->createElementNS('',"val"); ! $valElement->setText($keyvalType['value']); ! ! $questionElement->appendChild($keyElement); ! $questionElement->appendChild($valElement); ! } ! } ! } ! ! else ! { ! //Just a simple type - string etc, so just append the text to the node ! $questionElement->setText($question[$qfield]); ! } ! ! $questionFrag->appendChild($questionElement); ! ! } } ! return $questionFrag; ! } //Takes the assessmentItem fragment, constructs a filename and saves it out to XML //Returns true if file was successfully written out, false otherwise *************** *** 219,222 **** --- 287,346 ---- } + //Retrieve the list of questions which correspond to a particular quiz + //Then call stack_xml_create_question_new, to create each individual assessmentItem + //Wrap around a mathQuiz container + function stack_xml_create_quiz_new($quiz) + { + //foreach() + //stack_xml_create_question_frag($question); + + } + + //Writes a quiz to an XML file + function stack_xml_write_quiz_new($quiz,$directory) + { + //<TODO> How about using Quiz name? + $xml_file_name = 'stack_quiz_'.time().'.xml'; + + $xmlDoc = stack_xml_create_quiz_new($quiz); + + $success = $xmlDoc->saveXML($directory."/".$xml_file_name,true); + + if (!$success) + { + return null; + } + + return $xml_file_name; + } + + if (!$xml_file_handle) { + // error condition + echo "File could not be opened for writing."; + } else { + $xml = "<?xml version=\"1.0\"?>\n"; + $xml .= "<mathQuiz>\n"; + + $errors = NULL; + foreach ($quiz as $questionID) + { + $question = stack_db_getquestion($questionID); + stack_question_validate($question,$errors); + unset($question['questionID']); + $quizQ['assessmentItem'][]= $question; + } + + $xml .= stack_xml_create_question_frag($quizQ,4); + + $xml .= '</mathQuiz>'; + + fputs($xml_file_handle, $xml); + // close xml file + fclose($xml_file_handle); + } + + return $xml_file_name; + + } /** * Creates a XML schema header based upon a XML namespaces datastructure, defined in the following manner: |