source: Dev/trunk/classes/Session.php @ 174

Last change on this file since 174 was 174, checked in by jkraaijeveld, 13 years ago

.... and finally, fixed toSPSS to ensure the ordering stays correct if there are answers missing, something which can happen quite often.

File size: 4.2 KB
Line 
1<?php
2/**
3 * Description of Session
4 *
5 * @author jkraaijeveld
6 */
7
8class Session extends ResearchToolObject
9{
10        public $title;
11        public $creator;
12        public $datetime;
13        public $pipeline;
14        public $answersets;
15
16       
17        /**
18         * Constructor for a Session object
19         * @param type $uid
20         * @param type $title
21         * @param type $creator
22         * @param type $datetime
23         * @param type $pipeline
24         */
25        public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null, $answersets = null)
26        {
27                if(!isset($uid))
28                {
29                        $uid = md5(uniqid(rand(), true));
30                }
31                $this->uid = $uid;
32                $this->title = $title;
33                $this->creator = $creator;
34                $this->datetime = $datetime;
35                $this->pipeline = $pipeline;
36                $this->answersets = $answersets;
37        }
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                //Create the filename, open the file at the given location
51                $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt";
52
53                //Make a list of all 'column headers', e.g. "respondent" and all the question codes.
54                $headers = array();
55                $headers[] = "respondent";
56                foreach ($this->pipeline as $element)
57                {
58                        if(get_class($element) == "Survey")
59                        {
60                                foreach($element->questions as $question)
61                                {
62                                        $headers[] = $element->title . '_' . $question->uid;
63                                }
64                        }
65                }
66                //Return if there are no surveys.
67                if(count($headers) < 2)
68                        return;
69               
70                //Get the answers for all respondents
71                $answers = $this->getAnswers($headers);
72
73                //Create the strings and write to file.
74                $headerString = "";
75                foreach($headers as $headerElement)
76                {
77                        $headerString = $headerString . $headerElement . "\t";
78                }
79                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
80                fwrite($fileHandler, $headerString . "\n");
81                foreach($answers as $entry)
82                {
83                        $entryString = "";
84                        //PHP doesn't automatically sort the indices so I have to do it myself
85                        //Entry elements can now be printed in order to match the headers
86                        for ($i = 0; $i < count($headers); $i++)
87                        {
88                                if(in_array($i, array_keys($entry)))
89                                        $entryString = $entryString . $entry[$i] . "\t";       
90                                else
91                                        $entryString = $entryString . "\t";
92                        }
93                        fwrite($fileHandler, $entryString . "\n");     
94                }
95        }
96
97        /*
98         * function getAnswers
99         * @param type $headers
100         * Based on the questions specified in the headers array, matches the answers of every
101         * respondent to the correct questions and returns an array containing a list of answers per
102         * respondent
103         */
104        private function getAnswers($headers)
105        {
106                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
107                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
108                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
109                $respondentsUsed = array();
110                $answers = array();
111                foreach($this->answersets as $answerset)
112                {
113                        $entryIndex;
114                        if(in_array($answerset->respondent->name, $respondentsUsed))
115                        {
116                                $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
117                                $entry = $answers[$entryIndex];
118                        }
119                        else
120                        {
121                                $entry = array();
122                                $entry[0] = $answerset->respondent->name;
123                                $entryIndex = count($respondentsUsed);
124                                $respondentsUsed[] = $answerset->respondent->name;
125                        }
126                        foreach ($answerset->answers as $answer)
127                        {
128                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
129                                //Index has to be a good number, else the answer doesn't belong in this session
130                                //Causes might be a missing question or having a wrong survey
131                                if($index != 0)
132                                {
133                                        $value = "";
134                                        foreach ($answer->values as $val)
135                                        {
136                                                $value = $value . $val . ",";
137                                        }
138                                        //We all hate trailing commas
139                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
140                                        $entry[$index] = $value;
141                                }
142                        }
143                        $answers[$entryIndex] = $entry;
144                }
145                return $answers;
146        }
147
148}       
149
150
151?>
Note: See TracBrowser for help on using the repository browser.