Changeset 170


Ignore:
Timestamp:
12/02/11 13:10:30 (13 years ago)
Author:
jkraaijeveld
Message:
  • Session objects now have a 'toSPSS($location)' method, which creates a file at the specified location which can be imported into SPSS.
Location:
Dev/trunk/classes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/classes/Session.php

    r159 r170  
    3636                $this->answersets = $answersets;
    3737        }
     38
     39        /**
     40         * function toSPSS
     41         * @param type $location
     42         * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt'
     43         */
     44        public function toSPSS($location)
     45        {
     46                if(!is_dir($location))
     47                {
     48                        mkdir($location);
     49                }
     50                $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt";
     51
     52                //Make a list of all 'column headers', e.g. "respondent" and all the question codes.
     53                $headers = array();
     54                $headers[] = "respondent";
     55                foreach ($this->pipeline as $element)
     56                {
     57                        if(get_class($element) == "Survey")
     58                        {
     59                                foreach($element->questions as $question)
     60                                {
     61                                        $headers[] = $element->title . '_' . $question->uid;
     62                                }
     63                        }
     64                }
     65               
     66                $answers = $this->getAnswers($headers);
     67
     68                //Create the strings and write to file.
     69                $headerString = "";
     70                foreach($headers as $headerElement)
     71                {
     72                        $headerString = $headerString . $headerElement . "\t";
     73                }
     74                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
     75                fwrite($fileHandler, $headerString . "\n");
     76                foreach($answers as $entry)
     77                {
     78                        $entryString = "";
     79                        foreach(array_keys($entry) as $i)
     80                        {
     81                                $entryString = $entryString . $entry[$i] . "\t";       
     82                        }
     83                        fwrite($fileHandler, $entryString . "\n");     
     84                }
     85        }
     86
     87        /*
     88         * function getAnswers
     89         * @param type $headers
     90         * Based on the questions specified in the headers array, matches the answers of every
     91         * respondent to the correct questions and returns an array containing a list of answers per
     92         * respondent
     93         */
     94        private function getAnswers($headers)
     95        {
     96                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
     97                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
     98                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
     99                $respondentsUsed = array();
     100                $answers = array();
     101                foreach($this->answersets as $answerset)
     102                {
     103                        $entryIndex;
     104                        if(in_array($answerset->respondent->uid, $respondentsUsed))
     105                        {
     106                                $entryIndex = array_search($answerset->respondent->uid, $respondentsUsed);
     107                                $entry = $answers[$entryIndex];
     108                        }
     109                        else
     110                        {
     111                                $entry = array();
     112                                $entry[0] = $answerset->respondent->uid;
     113                                $entryIndex = count($respondentsUsed);
     114                                $respondentsUsed[] = $answerset->respondent->uid;
     115                        }
     116                        foreach ($answerset->answers as $answer)
     117                        {
     118                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
     119                                //Index has to be a good number, else the answer doesn't belong in this session
     120                                //Causes might be a missing question or having a wrong survey
     121                                if($index != 0)
     122                                {
     123                                        $value = "";
     124                                        foreach ($answer->values as $val)
     125                                        {
     126                                                $value = $value . $val . ",";
     127                                        }
     128                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
     129                                        $entry[$index] = $value;
     130                                }
     131                        }
     132                        $answers[$entryIndex] = $entry;
     133                }
     134                return $answers;
     135        }
     136
    38137}       
    39138
  • Dev/trunk/classes/SessionConnector.php

    r164 r170  
    115115                                $datetime = new DateTime();
    116116                                $datetime->setTimestamp(intval($result['?datetime']->label));
    117                                 $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $datetime, $pipeline, $answersets);
     117                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
    118118                        }
    119119                }
Note: See TracChangeset for help on using the changeset viewer.