From: pkiddie <pk...@us...> - 2005-08-15 16:38:24
|
Update of /cvsroot/stack/stack-1-0/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27996/scripts Modified Files: Tag: development_xmlrqp stackDatabase.php stackQuiz.php stackUtility.php stackXML.php Log Message: New revision of DOMIT with better support for namespacing 'Import question' works Preliminary front-end script for importing questions/quizzes and question lists Index: stackQuiz.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackQuiz.php,v retrieving revision 1.5.2.1 retrieving revision 1.5.2.2 diff -C2 -d -r1.5.2.1 -r1.5.2.2 *** stackQuiz.php 10 Aug 2005 08:48:46 -0000 1.5.2.1 --- stackQuiz.php 15 Aug 2005 16:09:23 -0000 1.5.2.2 *************** *** 14,18 **** $stackQuiz['quizid']['descript'] = 'Quiz ID number'; $stackQuiz['quizid']['mysql'] = 'INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY'; ! //$stackQuiz['quizid']['type'] = 'meta'; <TODO> Why does this duff the quiz edite // 'Quiz name' --- 14,18 ---- $stackQuiz['quizid']['descript'] = 'Quiz ID number'; $stackQuiz['quizid']['mysql'] = 'INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY'; ! $stackQuiz['quizid']['type'] = 'meta'; // 'Quiz name' Index: stackXML.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackXML.php,v retrieving revision 1.13.2.13 retrieving revision 1.13.2.14 diff -C2 -d -r1.13.2.13 -r1.13.2.14 *** stackXML.php 12 Aug 2005 17:16:47 -0000 1.13.2.13 --- stackXML.php 15 Aug 2005 16:09:23 -0000 1.13.2.14 *************** *** 50,55 **** //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"); /** --- 50,55 ---- //Constants for schema file names, used throughout: creation of schema file names and referencing those schemas in XML files ! define ('questionSchemaName',"stack_question.xsd"); ! define ('quizSchemaName',"stack_quiz.xsd"); /** *************** *** 579,583 **** $xmlDoc = new DOMIT_Document(); ! $quizFrag = new DOMIT_Element('mathQuiz'); return $quListFrag; --- 579,583 ---- $xmlDoc = new DOMIT_Document(); ! $quListFrag = new DOMIT_Element('mathQuiz'); return $quListFrag; *************** *** 743,747 **** * @return string The XML file name */ ! function stack_xml_write_quiz_file($quiz, $directory) { // takes a $stackQuestion array and writes it to an XML file in $directory --- 743,747 ---- * @return string The XML file name */ ! function stack_xml_write_quiz_file_old($quiz, $directory) { // takes a $stackQuestion array and writes it to an XML file in $directory *************** *** 787,790 **** --- 787,792 ---- function stack_xml_parse_question_file($fileName) { + global $stack_root; + $xmlDoc = &new DOMIT_Document(); $xmlDoc->setNamespaceAwareness(true); *************** *** 793,800 **** //In the first instance, check what type of file we are dealing with //Retrieve the document element ! $headElementName = $xmlDoc->documentElement->nodeName; ! switch ($headElementName) ! { case('assessmentItem'): { --- 795,811 ---- //In the first instance, check what type of file we are dealing with //Retrieve the document element ! $headElement = $xmlDoc->documentElement; ! //Now retrieve the version number attached to the XML file being imported, and check for equality ! $importXmlVersion = $headElement->getAttribute('version'); ! $schemaVersion = stack_xml_check_schema_version("{$stack_root}/schemas/"); ! ! if ($importXmlVersion==null) { //Retain for compatibility - use old parsing code ! $question = stack_xml_parse_question_string($xmlDoc->toString); ! } ! ! else if ($importXmlVersion==$schemaVersion) { //Where versions match up ! switch ($headElement->nodeName) ! { case('assessmentItem'): { *************** *** 807,813 **** case('mathQuiz'): { ! //A list of questions $questionList = stack_xml_parse_question_list($xmlDoc); - break; } --- 818,823 ---- case('mathQuiz'): { ! //A list of questions or a quiz? $questionList = stack_xml_parse_question_list($xmlDoc); break; } *************** *** 816,822 **** { //Any other head element ! return null; break; } } } --- 826,838 ---- { //Any other head element ! return false; break; } + } + } + + else { //Where version number of XML data is not the same as the schema + //Throw error + return false; } } *************** *** 855,932 **** } ! function stack_xml_parse_element_into_array(&$element,&$array) ! { ! $noChildren = count($element); ! for ($i = 0; $i < $noChildren; $i++) { ! $node = &$element[$i]; ! //Add this element to the array given, depending on the type of node found ! //Does it have children ! //If so which type? ! //If node has children ! if ($node->childNodes!=null) { ! //We need to check what type these child nodes are.. ! //3==text node - not really a child as such ! if ($node->childNodes[0]->nodeType==3) ! { ! //print_r("Here2"); ! //print_r($node->nodeName); ! $array[$node->nodeName]=$node->getText(); ! } ! ! //1==element - another child, so recurse ! else if ($node->childNodes[0]->nodeType==1); { ! //print_r($node->childNodes[0]->nodeType); ! //print_r("Here"); ! stack_xml_parse_element_into_array($element[$i]->childNodes,$array[$node->nodeName]); } ! } ! ! //If node has not got children ! else ! { ! $array[$node->nodeName]=$node->getText(); } } } - - function stack_xml_parse_question(&$questionXmlDoc) - { - global $namespaces, $stackQuestion; - - $question = array(); - - //1. Retrieve dublin core elements if any exist - //$dcNodeList =&$questionXmlDoc->getElementsByTagNameNS($namespaces['dc']['namespace'],'*'); - - //1. Retrieve dublin core, learning object model and stack core elements if they exist - $elements = &$questionXmlDoc->documentElement->childNodes; - - stack_xml_parse_element_into_array($elements,$question['assessmentItem']); - - show_array($question); - - return $question; - /*// Alle Nodes des Elemnts an array übergeben - - //add ampersand (&) after equal sign for PHP4 compatability - $node =& $elements->item($i); - - //$numChildren = $node->childCount; - - for ($j = 0; $j < $numChildren; $j++) { - ///get reference to current child node - $currChildNode =& $node->childNodes[$j]; - - //echo node value of text node - echo $currChildNode->getText(); - } - } ! foreach ($nodeList as $node) ! { //If node has children if ($node->childNodes!=null) --- 871,938 ---- } ! //This function simply maps from an element in a particular namespace onto the correct stackQuestion/stackQuiz data structure field, ! //and returns that field ! //Probably could be more efficient - multiple passes through array ! function stack_xml_map_element_ns(&$element,$arrayType) ! { ! global $stackQuestion, $stackQuiz; ! //print_r($element->localName); ! ! switch($arrayType) ! { ! case('stackQuestion'): ! { ! foreach($stackQuestion as $arrayKey=>$questionField) ! { ! if((!empty($questionField['type']) && $questionField['type']=='meta')) ! { ! show_array($questionField); ! if ((!empty($questionField['metatag']) && ($questionField['metatag']==$element->localName))) { ! return $arrayKey; ! } ! ! } ! } ! break; ! } ! case('stackQuiz'): { ! foreach($stackQuiz as $arrayKey=>$quizField) { ! if((!empty($stackQuestion['type']) && $stackQuestion['type']=='meta')) ! { ! if ($stackQuestion['metatag']==$element->localName) { ! return $arrayKey; ! } ! ! } } ! break; } } } ! //Recursive function which creates the stackQuestion/stackQuiz data structure required by the parsing engine ! function stack_xml_parse_element_into_array(&$element,&$array,$arrayType) ! { ! $children = &$element->childNodes; //Retrieve all child nodes ! $noChildren = count($children); //Count the number of ! print_r($noChildren); ! ! for ($i = 0; $i < $noChildren; $i++) { ! ! $mapFromNS=false; ! $node = &$children[$i]; ! $nodeName = $node->nodeName; ! ! //Is this node the member of a metadata namespace? If so, map it onto the equivalent question field ! if (!empty($node->namespaceURI)) ! { ! print_r("Gone here!"); ! $nodeName = stack_xml_map_element_ns($node,$arrayType); ! } //We will have a nodeName at this point, whether the actual element or resolved from namespace ! //If node has children if ($node->childNodes!=null) *************** *** 936,977 **** if ($node->childNodes[0]->nodeType==3) { ! print_r($node->nodeName); ! $question[$node->nodeName]=$node->getText(); } ! //1==element - another child else if ($node->childNodes[0]->nodeType==1); ! { ! //Therefore we have an array of data ! $question[$node->nodeName]= array(); ! foreach ($node->childNodes as $child) { ! if ($child->childNodes!=null) ! { ! if ($child->childNodes[0]->nodeType==3) ! { ! $question[$node->nodeName][$child->nodeName]==$child->getText(); ! } ! ! //Must have more elements ! else if ($child->childNodes[0]->nodeType==1) ! { ! $question[$node->nodeName][$cihld->nodeName]==array(); ! } ! ! } ! //Else childnode does not contain data ! else ! { ! $question[$node->nodeName][$child->nodeName]=$child->getText(); ! ! //We need to recurse ! ! ! } --- 942,969 ---- if ($node->childNodes[0]->nodeType==3) { ! $array[$nodeName]=$node->getText(); } ! //1==element - another child, so recurse else if ($node->childNodes[0]->nodeType==1); ! { ! //Check whether there are multiple types of the same node within the XML tree ! $nodeList = &$element->getElementsByTagName("$node->nodeName"); ! $length = $nodeList->getLength(); ! //If there are multiple elements of the same name ! //Deal with arrays of values: for example potential responses and questionVar/answerVar ! if ($length>1) { ! stack_xml_parse_element_into_array($children[$i],$array[$nodeName][],$arrayType); ! } + else + { + stack_xml_parse_element_into_array($children[$i],$array[$nodeName],$arrayType); + } ! } } *************** *** 979,1056 **** else { ! print_r($node->nodeName); ! $question[$ode->nodeName]=$node->getText(); ! } ! } ! ! //Need to use nodeType ! ! //Now iterate through those nodes prepended by the dublin core namespace ! for ($i=0; $i<$dcNodeList->getLength(); $i++) ! { ! $dcNode = &$dcNodeList->item($i); ! //Check against the stackQuestion data structure ! foreach($stackQuestion as $arrayKey=>$questionField) ! { ! $nodeName=explode(":",$dcNode->nodeName); ! ! //print_r($nod ! ! if (!empty($questionField['metatag']) && $questionField['metatag']==$nodeName[1]) ! { ! //show_array($questionField); ! //We know we have found the corresponding stackQuestion tag ! //print_r($dcNode->nodeName); ! //print_r($dcNode->getText()); ! $question[$arrayKey]=$dcNode->getText(); ! break; ! } ! } } ! //2. Retrieve lom elements if any exist ! $lomNodeList = &$questionXmlDoc->getElementsByTagNameNS($namespaces['lom']['namespace'],'*'); ! //Now iterate through those nodes prepended by the dublin core namespace ! for ($i=0; $i<$lomNodeList->getLength(); $i++) ! { ! $lomNode = &$lomNodeList->item($i); ! //Check against the stackQuestion data structure ! foreach($stackQuestion as $arrayKey=>$questionField) ! { ! $nodeName=explode(":",$lomNode->nodeName); ! ! //print_r($nod ! ! if (!empty($questionField['metatag']) && $questionField['metatag']==$nodeName[1]) ! { ! //show_array($questionField); ! //We know we have found the corresponding stackQuestion tag ! //print_r($dcNode->nodeName); ! //print_r($dcNode->getText()); ! $question[$arrayKey]=$lomNode->getText(); ! break; ! } ! ! } ! } show_array($question); - //print_r($retItem->nodeName); - //print_r($retItem->getText()); - //print_r("<br>"); - - //3. Then any other elements who do not have children - //$stackNode - - /*foreach ($questionXmlDoc->documentElement->childNodes as $childElement) - { - //This is not a potential response, options, or types with keyvals - if ($childElement->childNodes==null) - { - switch($childElement->*/ - } --- 971,992 ---- else { ! $array[$nodeName]=$node->getText(); } } + } + + function stack_xml_parse_question(&$questionXmlDoc) + { + global $namespaces, $stackQuestion; + $question = array(); ! $elements = &$questionXmlDoc->documentElement; ! stack_xml_parse_element_into_array($elements,$question['assessmentItem'],'stackQuestion'); + show_array("stack_xml_parse_question"); show_array($question); + return $question; } *************** *** 1120,1153 **** //<PDK> /** ! * Checks the version of the specified schema with the version held in the stackstd.php file * * Loads the question metadata schema and checks the 'version' attribute in stackstd.php for equality. * ! * @param string $questionSchemaLocation The location of the question metadata schema for which this XML file should reflect ! * @return bool $latestVersion True if the schema is the latest version, False otherwise * * <TODO> Depending on logic - can then issue an update to the old schema */ ! function stack_schema_check_version($schemaLocation) { global $stack_ver; $schemaXmlDoc = &new DOMIT_Document(); ! $success = $schemaXmlDoc->loadXML($schemaLocation); if ($success) { //Retrieve the root element - the document element; ! $docElement = &$schemaXmlDoc->$documentElement; $versionAttr = &$docElement->getAttribute('version'); ! if(($versionAttr->nodeValue)==$stack_ver['release']) { ! ! return true; } ! else { return false; } } --- 1056,1101 ---- //<PDK> /** ! * Checks the version of the question and quiz schemas with the version number held in the stackstd.php file * * Loads the question metadata schema and checks the 'version' attribute in stackstd.php for equality. * ! * @param string $questionSchemaLocation The directory to which the question metadata schema resides ! * @return mixed $latestVersion If schema found whose version matches that in stackStd.php, return the version number. Else ! * return false. * * <TODO> Depending on logic - can then issue an update to the old schema */ ! function stack_xml_check_schema_version($schemaDirectory) { global $stack_ver; + $schemaFileName = "{$schemaDirectory}/".questionSchemaName; + $schemaXmlDoc = &new DOMIT_Document(); ! $success = @$schemaXmlDoc->loadXML($schemaFileName); //Hide warning - $success flag takes care of what to do if ($success) { //Retrieve the root element - the document element; ! $docElement = &$schemaXmlDoc->documentElement; $versionAttr = &$docElement->getAttribute('version'); ! if ($versionAttr==null) ! { ! echo("The specified schema $schemaFileName does not have a version tag attached to it. Please contact the developers for support!<br>"); ! return false; } ! else ! { ! //If the schema is the latest version ! if($versionAttr==$stack_ver['release']) { ! return $versionAttr; ! } ! ! else { return false; + } } } *************** *** 1155,1160 **** else { ! echo("The schema file $schemaLocation could not be opened. Please contact the developers!<br>"); } } --- 1103,1153 ---- else { ! echo("The schema file <b>$schemaFileName</b> does not exist. STACK will now recreate all schemas<br>"); ! return false; } + + return false; + } + + /** + * Given that the current version of the schema is outdated, inform the user that STACK is taking steps to update both schemas + * + * Present user with information regarding schema update, and call the necessary schema creation functions + * + * @param string $directory The directory to which the schemas reside + * @return void + * + */ + + function stack_xml_update_schema($directory) + { + global $stack_ver; + + echo "<html> + <head><title>STACK Schema Update</title><head> + <body> + <H1>STACK schemas need updating</H1> + <p><b>This script updates both quiz and question schema to reflect changes in this new version of STACK </b></p>"; + + echo "<b>Updating question schema to version ".$stack_ver['release']."</b><br>"; + + if(stack_xml_create_question_schema($directory)) { + echo ("<font color='green'>Question schema created successfully</font><br>"); + } + + else { + echo ("<font color='red'>Question schema not created successfully</font><br>"); + } + + echo "<b>Updating quiz schema to version ".$stack_ver['release']."</b><br>"; + + if(stack_xml_create_quiz_schema($directory)) { + echo ("<font color='green'>Quiz schema created successfully</font><br>"); + } + + else { + echo ("<font color='red'>Quiz schema not created successfully</font><br>"); + } + } *************** *** 2317,2321 **** } ! //stack_xml_create_question_schema(); ! //stack_xml_create_quiz_schema(); ?> --- 2310,2316 ---- } ! if (!stack_xml_check_schema_version("{$stack_root}/schemas/")) ! { ! stack_xml_update_schema("{$stack_root}/schemas/"); ! } ?> Index: stackUtility.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackUtility.php,v retrieving revision 1.30.2.2 retrieving revision 1.30.2.3 diff -C2 -d -r1.30.2.2 -r1.30.2.3 *** stackUtility.php 28 Jul 2005 13:36:23 -0000 1.30.2.2 --- stackUtility.php 15 Aug 2005 16:09:23 -0000 1.30.2.3 *************** *** 16,19 **** --- 16,20 ---- // (1) remove stackData.php - and its reference in stackstd.php // (2) remove stackSocketSend.php - and its reference in stackstd.php + // (3) Remove versioning from stackStd and put somewhere else. if ($stack_stand_alone) { require_once("{$stack_root}/scripts/moodlelib.php"); *************** *** 1071,1075 **** else { ! return false; } } ?> --- 1072,1077 ---- else { ! return false; ! } } ?> Index: stackDatabase.php =================================================================== RCS file: /cvsroot/stack/stack-1-0/scripts/stackDatabase.php,v retrieving revision 1.34.2.2 retrieving revision 1.34.2.3 diff -C2 -d -r1.34.2.2 -r1.34.2.3 *** stackDatabase.php 12 Aug 2005 17:16:47 -0000 1.34.2.2 --- stackDatabase.php 15 Aug 2005 16:09:23 -0000 1.34.2.3 *************** *** 1122,1128 **** $question['questionUserLastEdited'] = $user['firstname']." ".$user['lastname']; ! show_array("db:"); ! show_array($question); ! show_array($user); } --- 1122,1128 ---- $question['questionUserLastEdited'] = $user['firstname']." ".$user['lastname']; ! //show_array("db:"); ! //show_array($question); ! //show_array($user); } |