From: pkiddie <pk...@us...> - 2005-08-10 08:45:10
|
Update of /cvsroot/stack/stack-1-0/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15083/scripts Modified Files: Tag: development_xmlrqp stackXML.php Log Message: Editquiz.php added to, to allow the exporting of quiz XML files Lists of questions now exportable with new code in stackXML StackQuiz data structure updated Index: stackXML.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackXML.php,v retrieving revision 1.13.2.9 retrieving revision 1.13.2.10 diff -C2 -d -r1.13.2.9 -r1.13.2.10 *** stackXML.php 8 Aug 2005 22:10:24 -0000 1.13.2.9 --- stackXML.php 10 Aug 2005 08:44:59 -0000 1.13.2.10 *************** *** 1,5 **** <?php - require_once("{$stack_root}/other/domit/xml_domit_include.php"); - /** * Defines XML handling within STACK --- 1,3 ---- *************** *** 9,12 **** --- 7,12 ---- */ + require_once("{$stack_root}/other/domit/xml_domit_include.php"); + //<PDK> sqlToSchemaMapping array maps basic SQL data types used in STAKCK to their respective XML schema data types $sqlToSchemaMapping['CHAR']='string'; *************** *** 31,35 **** $namespaces['dc']['namespace'] = 'http://purl.org/dc/elements/1.1/'; //Dublin core namespace $namespaces['dc']['id'] = 'dc'; //The ID prepended to our elements that use DC ! $namespaces['dc']['location'] = 'dc.xsd'; //The actual location of the dublin core schema $namespaces['lom']['namespace'] = 'http://www.imsglobal.org/xsd/imsmd_v1p2';//etc. --- 31,35 ---- $namespaces['dc']['namespace'] = 'http://purl.org/dc/elements/1.1/'; //Dublin core namespace $namespaces['dc']['id'] = 'dc'; //The ID prepended to our elements that use DC ! $namespaces['dc']['location'] = 'dc.xsd'; //The actual location of the dublin core schema $namespaces['lom']['namespace'] = 'http://www.imsglobal.org/xsd/imsmd_v1p2';//etc. *************** *** 40,44 **** //As above... $xmlNamespaces['dc']['namespace'] = 'http://purl.org/dc/elements/1.1/'; //Dublin core namespace ! $xmlNamespaces['dc']['id'] = 'dc'; //The ID prepended to our elements that use DC //The actual location of the dublin core schema $xmlNamespaces['lom']['namespace'] = 'http://www.imsglobal.org/xsd/imsmd_v1p2';//etc. --- 40,44 ---- //As above... $xmlNamespaces['dc']['namespace'] = 'http://purl.org/dc/elements/1.1/'; //Dublin core namespace ! $xmlNamespaces['dc']['id'] = 'dc'; //The ID prepended to our elements that use DC //The actual location of the dublin core schema $xmlNamespaces['lom']['namespace'] = 'http://www.imsglobal.org/xsd/imsmd_v1p2';//etc. *************** *** 47,53 **** //Different namespace here - schema instance $xmlNamespaces['xsi']['namespace'] = 'http://www.w3.org/2001/XMLSchema-instance'; //Schema instance - required for XML files that reflect ! $xmlNamespaces['xsi']['id'] = 'xsi'; //Particular schema ! $xmlNamespaces['xsi']['location'] = 'c:/Program Files/EasyPHP1-8/www/stack-1-0/schemas/stack-question-1.0.xsd'; //Actual location of our schema /** --- 47,55 ---- //Different namespace here - schema instance $xmlNamespaces['xsi']['namespace'] = 'http://www.w3.org/2001/XMLSchema-instance'; //Schema instance - required for XML files that reflect ! $xmlNamespaces['xsi']['id'] = 'xsi'; //Particular schema + //Constants for schema file names, used throughout: creation of schema file names and referencing those schemas in XML files + define ('questionSchemaName',"stack_question-{$stack_ver['release']}.xsd"); + define ('quizSchemaName',"stack_quiz-{$stack_ver['release']}.xsd"); /** *************** *** 58,62 **** * @return string The XML string representing the question */ ! function stack_xml_create_question($question) { // Takes a $stackQuestion array, and returns an XML string global $stackQuestion; --- 60,64 ---- * @return string The XML string representing the question */ ! /*function stack_xml_create_question($question) { // Takes a $stackQuestion array, and returns an XML string global $stackQuestion; *************** *** 85,95 **** return $xml; ! } //Creates an AssessmentItem fragment, and returns the head DOMIT_Element function stack_xml_create_question_new($question) { - global $stackQuestion, $stack_ver, $xmlNamespaces; - // Don't export questionID if (array_key_exists('questionID',$question)) { --- 87,104 ---- return $xml; ! }*/ //Creates an AssessmentItem fragment, and returns the head DOMIT_Element + + /** + * Writes a single stackQuestion data structure as XML, and returns the containing + * XML document to the calling function + * + * @see stack_xml_create_question_frag_new() + * @param array $question A stackQuestion data structure + * @return DOMIT_Document The XML document representing the question + */ function stack_xml_create_question_new($question) { // Don't export questionID if (array_key_exists('questionID',$question)) { *************** *** 104,119 **** $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(); --- 113,132 ---- $xmlDoc->appendChild($xmlDoc->createProcessingInstruction('xml', "version=\"1.0\" encoding=\"utf-8\"")); ! $questionFrag = &stack_xml_create_question_frag_new($question); ! ! //questionFrag head node is <assessmentItem> ! //We need to add namespaces etc to it. ! stack_xml_create_question_instance_header($questionFrag); ! $xmlDoc->appendChild($questionFrag); //Append questionFrag to an DOM Document return $xmlDoc; } ! //Takes a question and returns an AssessmentItem element to the calling function ! function stack_xml_create_question_frag_new($question) { + global $stackQuestion, $stack_ver, $xmlNamespaces; + $xmlDoc = new DOMIT_Document(); *************** *** 269,273 **** { //Create question filename ! if ($question['questionName'] != '') { $xml_file_name = $question['questionName'].".xml"; } else { --- 282,286 ---- { //Create question filename ! if (!empty($question['questionName'])) { $xml_file_name = $question['questionName'].".xml"; } else { *************** *** 292,305 **** 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); --- 305,451 ---- function stack_xml_create_quiz_new($quiz) { ! $xmlDoc = new DOMIT_Document(); ! $xmlDoc->setNamespaceAwareness(true); //We need namespace awareness here ! $xmlDoc->appendChild($xmlDoc->createProcessingInstruction('xml', "version=\"1.0\" encoding=\"utf-8\"")); ! ! // Don't export quizID ! if (array_key_exists('quizid',$quiz)) { ! unset($quiz['quizid']); ! } ! ! $quizFrag = stack_xml_create_quiz_frag_new($quiz); ! ! //show_array($quiz); + //<TODO> Does the quiz have any questions attached. If not, is there any point writing out?? + if (!empty($quiz['questions'])) + { + //Retrieve each question individually + foreach ($quiz['questions'] as $question) + { + + $question = stack_db_getquestion($question['questionID']); //Get the questionId from the question + stack_question_validate($question,$errors); //Validate it + + unset($question['questionID']); + + $questionFrag = &stack_xml_create_question_frag_new($question); + $quizFrag->appendChild($questionFrag); + } + } + + //quizItem head node is <mathQuiz> + //We need to add namespaces etc to it. + stack_xml_create_quiz_instance_header($quizFrag); + $xmlDoc->appendChild($quizFrag); //Append quizItem to DOM Document... return to calling function + return $xmlDoc; } + //<TODO> Here we need to write out quiz level metadata and options + function stack_xml_create_quiz_frag_new($quiz) + { + global $stackQuiz, $xmlNamespaces; + + $xmlDoc = new DOMIT_Document(); + + $quizFrag = new DOMIT_Element('mathQuiz'); + + foreach($stackQuiz as $quizField => $attribs) + { + if ('meta' == $attribs['type'] && array_key_exists($quizField,$quiz)) { + //Detect the metadata type + switch ($attribs['metatype']) + { + case('dublincore'): + { + $metaElement =&$xmlDoc->createElementNS($xmlNamespaces['dc']['namespace'],"{$xmlNamespaces['dc']['id']}:{$attribs['metatag']}"); + break; + } + + case('lom'): + { + $metaElement =&$xmlDoc->createElementNS($xmlNamespaces['lom']['namespace'],"{$xmlNamespaces['lom']['id']}:{$attribs['metatag']}"); + break; + } + + case('stack'): + { + $metaElement =&$xmlDoc->createElementNS('',"{$attribs['metatag']}"); + break; + } + + case('default'): + { + $metaElement =&$xmlDoc->createElementNS('',"{$attribs['metatag']}"); + break; + } + + } + + $metaElement->setText($quiz[$quizField]); //Add the contents of the field to metadata tag + $quizFrag->appendChild($metaElement); + } + + //We are interested in quizOptions + else if ('other' == $attribs['type'] && array_key_exists($quizField,$quiz)) { + + if ($quizField=='quizOptions') + { + //Write head element + $quizElement = &$xmlDoc->createElementNS('',$quizField); + + if (is_array('quizOptions')) + { + foreach ($quiz[$quizField] as $optionField => $option) { + + $optionElement = &$xmlDoc->createElementNS('',$optionField); + $optionElement->setText($option); + + $quizElement->appendChild($optionElement); + } + } + } + + else + { + echo("The field $quizField cannot be written out"); + } + + $quizFrag->appendChild($quizElement); + } + + //All other elements + else if (array_key_exists($quizField,$quiz)) { + + //Write head element + $quizElement = &$xmlDoc->createElementNS('',$quizField); + + //Just a simple type - string etc, so just append the text to the node + $quizElement->setText($quiz[$quizField]); + + $quizFrag->appendChild($quizElement); + + } + } + + return $quizFrag; + } + + //Writes a quiz to an XML file function stack_xml_write_quiz_new($quiz,$directory) { //<TODO> How about using Quiz name? ! if (!empty($quiz['quizName'])) ! { ! $xml_file_name = 'stack_quiz_'.$quiz['quizName'].'.xml'; ! } ! ! //Append time if no name has been found ! else ! { ! $xml_file_name = 'stack_quiz_'.time().'.xml'; ! } ! $xmlDoc = stack_xml_create_quiz_new($quiz); *************** *** 313,348 **** 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: * $namespaces['xs']['namespace'] = Uri where external schema is located * $namespaces['xs']['id'] = Id of which elements which are defined from this schema are prepended with --- 459,530 ---- return $xml_file_name; + } ! //The only difference between a quiz and a list of questions is that a list ! //of questions will not have metadata/quiz level options attached to it ! //Thus the main container is still mathQuiz, but this contains multiple assessmentItems ! function stack_xml_write_question_list($questionList,$directory) ! { ! $xml_file_name = 'stack_quiz_'.time().'.xml'; ! ! $xmlDoc = stack_xml_create_question_list($questionList); ! ! $success = $xmlDoc->saveXML($directory."/".$xml_file_name,true); ! ! if (!$success) ! { ! return null; ! } ! ! return $xml_file_name; ! } ! function stack_xml_create_question_list($questionList) ! { ! $xmlDoc = new DOMIT_Document(); ! $xmlDoc->setNamespaceAwareness(true); //We need namespace awareness here ! $xmlDoc->appendChild($xmlDoc->createProcessingInstruction('xml', "version=\"1.0\" encoding=\"utf-8\"")); ! ! //show_array($questionList); ! $quizFrag = stack_xml_create_question_list_frag($questionList); ! ! //<TODO> Does the quiz have any questions attached. If not, is there any point writing out?? ! if (!empty($questionList)) ! { ! //Retrieve each question individually ! foreach ($questionList as $questionID) ! { ! $question = stack_db_getquestion($questionID); //Get the questionId from the question ! stack_question_validate($question,$errors); //Validate it ! ! unset($question['questionID']); ! ! $questionFrag = &stack_xml_create_question_frag_new($question); ! $quizFrag->appendChild($questionFrag); ! } ! } ! ! //quizItem head node is <mathQuiz> ! //We need to add namespaces etc to it. ! stack_xml_create_quiz_instance_header($quizFrag); ! $xmlDoc->appendChild($quizFrag); //Append quizItem to DOM Document... return to calling function ! return $xmlDoc; ! } ! function stack_xml_create_question_list_frag() ! { ! global $stackQuiz, $xmlNamespaces; ! ! $xmlDoc = new DOMIT_Document(); ! ! $quizFrag = new DOMIT_Element('mathQuiz'); + return $quizFrag; } + /** ! * Takes the head element of a a question XML file and adds namespacing and version information to it. * $namespaces['xs']['namespace'] = Uri where external schema is located * $namespaces['xs']['id'] = Id of which elements which are defined from this schema are prepended with *************** *** 353,376 **** * @param array $namespacesArray The above Xmlnamespaces data structure * @param string $rootElem The name of the root Xml element ! * @return DOMIT_Element $schemaHeader The XML schema header, which may be appended onto the DOM Document root node */ ! function stack_xml_create_instance_header($namespacesArray,$rootName) { ! global $stack_ver; //Retrieve current release of STACK ! //Contruct the header for the schema ! $schemaHeader = new DOMIT_Element($rootName); ! foreach ($namespacesArray as $namespaceKey=>$namespace) //Iterate through each namespace and add to schema header { $namespaceAttr = &new DOMIT_Attr('xmlns:'.$namespaceKey); $namespaceAttr->nodeValue=$namespace['namespace']; ! $schemaHeader->setAttributeNode($namespaceAttr); } //Now must give location of schema from which this instance has been derived from $schemaLocationAttr = new DOMIT_Attr('xsi:noNamespaceSchemaLocation'); ! $schemaLocationAttr->nodeValue=$namespacesArray['xsi']['location']; //Used to ensure latest schema version is used --- 535,579 ---- * @param array $namespacesArray The above Xmlnamespaces data structure * @param string $rootElem The name of the root Xml element ! * @return void */ ! function stack_xml_create_question_instance_header(&$headElement) { ! global $stack_ver, $xmlNamespaces, $stack_root; //Retrieve current release of STACK ! foreach ($xmlNamespaces as $namespaceKey=>$namespace) //Iterate through each namespace and add to schema header ! { ! $namespaceAttr = &new DOMIT_Attr('xmlns:'.$namespaceKey); ! $namespaceAttr->nodeValue=$namespace['namespace']; ! ! $headElement->setAttributeNode($namespaceAttr); ! } ! //Now must give location of schema from which this instance has been derived from ! $schemaLocationAttr = new DOMIT_Attr('xsi:noNamespaceSchemaLocation'); ! $schemaLocationAttr->nodeValue="{$stack_root}/schemas/".questionSchemaName; ! ! //Used to ensure latest schema version is used ! $schemaVersionAttr = new DOMIT_Attr('version'); ! $schemaVersionAttr->nodeValue=$stack_ver['release']; ! ! $headElement->setAttributeNode($schemaLocationAttr); ! $headElement->setAttributeNode($schemaVersionAttr); ! } ! ! function stack_xml_create_quiz_instance_header(&$headElement) ! { ! global $stack_ver, $xmlNamespaces, $stack_root; //Retrieve current release of STACK ! ! foreach ($xmlNamespaces as $namespaceKey=>$namespace) //Iterate through each namespace and add to schema header { $namespaceAttr = &new DOMIT_Attr('xmlns:'.$namespaceKey); $namespaceAttr->nodeValue=$namespace['namespace']; ! $headElement->setAttributeNode($namespaceAttr); } //Now must give location of schema from which this instance has been derived from $schemaLocationAttr = new DOMIT_Attr('xsi:noNamespaceSchemaLocation'); ! $schemaLocationAttr->nodeValue="{$stack_root}/schemas/".quizSchemaName; //Used to ensure latest schema version is used *************** *** 378,386 **** $schemaVersionAttr->nodeValue=$stack_ver['release']; ! $schemaHeader->setAttributeNode($schemaLocationAttr); ! $schemaHeader->setAttributeNode($schemaVersionAttr); ! ! return $schemaHeader; } /** --- 581,588 ---- $schemaVersionAttr->nodeValue=$stack_ver['release']; ! $headElement->setAttributeNode($schemaLocationAttr); ! $headElement->setAttributeNode($schemaVersionAttr); } + /** *************** *** 704,708 **** //Append root schema node onto DOM object and save $dom->appendChild($questionFrag); ! $success = $dom->saveXML($directory."/".$schema_file_name,true); return $success; --- 906,910 ---- //Append root schema node onto DOM object and save $dom->appendChild($questionFrag); ! $success = $dom->saveXML($directory."/".quizSchemaName,true); return $success; *************** *** 720,726 **** global $xmlElements, $stack_ver; - //File name of schema has version number of STACK clearly identified - $schema_file_name='stack_question-'.$stack_ver['release'].'.xsd'; - $dom = new DOMIT_Document(); --- 922,925 ---- *************** *** 736,740 **** //Append root schema node onto DOM object and save $dom->appendChild($questionFrag); ! $success = $dom->saveXML($directory."/".$schema_file_name,true); return $success; --- 935,939 ---- //Append root schema node onto DOM object and save $dom->appendChild($questionFrag); ! $success = $dom->saveXML($directory."/".questionSchemaName,true); return $success; |