uid = $uid; $this->title = $title; $this->creator = $creator; $this->datetime = $datetime; $this->pipeline = $pipeline; $this->answersets = $answersets; } /** * function toSPSS * @param type $location * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt' */ public function toSPSS($location) { if(!is_dir($location)) { mkdir($location); } //Create the filename, open the file at the given location $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt"; //Make a list of all 'column headers', e.g. "respondent" and all the question codes. $headers = array(); $headers[] = "respondent"; foreach ($this->pipeline as $element) { if(get_class($element) == "Survey") { foreach($element->questions as $question) { $headers[] = $element->title . '_' . $question->uid; } } } //Return if there are no surveys. if(count($headers) < 2) return; //Get the answers for all respondents $answers = $this->getAnswers($headers); //Create the strings and write to file. $headerString = ""; foreach($headers as $headerElement) { $headerString = $headerString . $headerElement . "\t"; } $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use"); fwrite($fileHandler, $headerString . "\n"); foreach($answers as $entry) { $entryString = ""; //PHP doesn't automatically sort the indices so I have to do it myself //Entry elements can now be printed in order to match the headers for ($i = 0; $i < count($headers); $i++) { if(in_array($i, array_keys($entry))) $entryString = $entryString . $entry[$i] . "\t"; else $entryString = $entryString . "\t"; } fwrite($fileHandler, $entryString . "\n"); } } /* * function getAnswers * @param type $headers * Based on the questions specified in the headers array, matches the answers of every * respondent to the correct questions and returns an array containing a list of answers per * respondent */ private function getAnswers($headers) { //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering. //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which //already exists within the answers-array, use the index of the respondent. Else: make a new row. $respondentsUsed = array(); $answers = array(); foreach($this->answersets as $answerset) { $entryIndex; if(in_array($answerset->respondent->name, $respondentsUsed)) { $entryIndex = array_search($answerset->respondent->name, $respondentsUsed); $entry = $answers[$entryIndex]; } else { $entry = array(); $entry[0] = $answerset->respondent->name; $entryIndex = count($respondentsUsed); $respondentsUsed[] = $answerset->respondent->name; } foreach ($answerset->answers as $answer) { $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers); //Index has to be a good number, else the answer doesn't belong in this session //Causes might be a missing question or having a wrong survey if($index != 0) { $value = ""; foreach ($answer->values as $val) { $value = $value . $val . ","; } //We all hate trailing commas if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); } $entry[$index] = $value; } } $answers[$entryIndex] = $entry; } return $answers; } } ?>