cmsdirectory."/quiz-formatted-results"; $user = $config->username; $pass = $config->password; //supplies WSDL $client = new SoapClient($config->CMSURL . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); $formstr = file_get_contents($config->quiz); $form = new SimpleXMLElement("".get_string_between($formstr,'','')); buildResults(); buildOutput(); outputToCMS(); } //Parses data from $in into a multi-dimensional array //Array of questions, with each element being an array of answers //Value for each answer is number of times it was chosen function buildResults() { global $results; global $in; global $form; global $correctanswers; global $numcorrectanswers; foreach($in->quiz as $quiz) { //if the quiz in the quizresults matches the current quiz if($quiz['id'] == $form->input[0]['value']) { foreach($quiz->response->answers->children() as $child) { $correctanswers[$child->getName()] = $child['answer']; } foreach($quiz->response as $response) { foreach($response->answers[0] as $question => $answer) { if (!strcasecmp((string)$correctanswers[$question], (string)$answer)) { if(!isset($numcorrectanswers[$question])) { $numcorrectanswers[$question] = 1; } else { $numcorrectanswers[$question] += 1; } } $answer = "_".$answer; $answer = str_replace(" ","-",$answer); if(!isset($results[$question][$answer])) { $results[$question][$answer] = 1; } else { $results[$question][$answer] += 1; } } } break; } } } //Re-parses the xml into a style that is much more readable to the stylesheet function buildOutput() { global $out; global $results; global $correctanswers; global $numcorrectanswers; readXMLBlock(); foreach($results as $question => $answer) { if(!isset($out->$question)) { $q = $out->addChild($question); $q->addAttribute('answer',$correctanswers[$question]); if (isset($numcorrectanswers[$question])) { $q->addChild('numcorrectanswers',$numcorrectanswers[$question]); } else { $q->addChild('numcorrectanswers',0); } } else { $out->$question->numcorrectanswers = $out->$question->numcorrectanswers + $numcorrectanswers[$question]; } foreach($answer as $label => $value) { if(!isset($out->$question->$label)) { $out->$question->addChild($label,$value); } else { $out->$question->$label = $out->$question->$label + 1; // += operator not functional simpleXML } if(!isset($out->$question->totalanswers)) { $out->$question->addChild(totalanswers,$value); } else { $out->$question->totalanswers = $out->$question->totalanswers + 1; } } } } //Sets up parameters and then writes to CMS function outputToCMS() { global $out; global $config; global $client; global $fullpath; global $user; global $pass; $data = $out->asXML(); //build params $edit_params = array ( 'authentication' => array( 'password' => $pass . "", 'username' => $user . "" ), 'asset' => array( 'xmlBlock' => array( 'name' => basename($fullpath), 'parentFolderPath' => dirname($fullpath), 'path' => $fullpath, 'metadataSetPath' => "/Default", 'xml' => $data ))); try { //first, attempt to edit the block $client->edit($edit_params); //if edit fails, then block did not exist, so create from scratch if (substr_count($client->__getLastResponse(), "false") > 0) { $edit_params['asset']['xmlBlock']['path'] = ""; $client->create($edit_params); } } catch (Exception $e) { print("Block creation/edit request failed to conform to the WSDL.\n"); print($client->__getLastResponse()); } } //Reads data from the block in the CMS if it already exists function readXMLBlock() { global $out; global $config; global $client; global $user; global $pass; global $fullpath; global $form; //build params $read_params = array ( 'authentication' => array( 'password' => $pass . "", 'username' => $user . "" ), 'identifier' => array( 'path' => $fullpath, 'type' => "block" )); try { //attempt to read the block $xmlstr = $client->read($read_params); //if read fails then it does not exist, so initialize $out to a new object //otherwise initialize $out to the data obtained from the block if (substr_count($client->__getLastResponse(), "false") > 0) { $out = new SimpleXMLElement(" "); } else { $xmlstr = $xmlstr->readReturn->asset->xmlBlock->xml; $out = new SimpleXMLElement($xmlstr); } } catch (Exception $e) { print("Block read request failed to conform to the WSDL.\n"); print($client->__getLastResponse()); } } //From PHP.net function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } ?>