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

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

Bugfix in toSPSS(): entry elements were printed in an incorrect order. They now properly match the question stated in the header.

File size: 4.0 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                $answers = $this->getAnswers($headers);
71
72                //Create the strings and write to file.
73                $headerString = "";
74                foreach($headers as $headerElement)
75                {
76                        $headerString = $headerString . $headerElement . "\t";
77                }
78                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
79                fwrite($fileHandler, $headerString . "\n");
80                foreach($answers as $entry)
81                {
82                        $entryString = "";
83                        //PHP doesn't automatically sort the indices so I have to do it myself
84                        $keys = array_keys($entry);
85                        asort($keys);
86                        //Entry elements can now be printed in order to match the headers
87                        foreach($keys as $i)
88                        {
89                                $entryString = $entryString . $entry[$i] . "\t";       
90                        }
91                        fwrite($fileHandler, $entryString . "\n");     
92                }
93        }
94
95        /*
96         * function getAnswers
97         * @param type $headers
98         * Based on the questions specified in the headers array, matches the answers of every
99         * respondent to the correct questions and returns an array containing a list of answers per
100         * respondent
101         */
102        private function getAnswers($headers)
103        {
104                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
105                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
106                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
107                $respondentsUsed = array();
108                $answers = array();
109                foreach($this->answersets as $answerset)
110                {
111                        $entryIndex;
112                        if(in_array($answerset->respondent->name, $respondentsUsed))
113                        {
114                                $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
115                                $entry = $answers[$entryIndex];
116                        }
117                        else
118                        {
119                                $entry = array();
120                                $entry[0] = $answerset->respondent->name;
121                                $entryIndex = count($respondentsUsed);
122                                $respondentsUsed[] = $answerset->respondent->name;
123                        }
124                        foreach ($answerset->answers as $answer)
125                        {
126                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
127                                //Index has to be a good number, else the answer doesn't belong in this session
128                                //Causes might be a missing question or having a wrong survey
129                                if($index != 0)
130                                {
131                                        $value = "";
132                                        foreach ($answer->values as $val)
133                                        {
134                                                $value = $value . $val . ",";
135                                        }
136                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
137                                        $entry[$index] = $value;
138                                }
139                        }
140                        $answers[$entryIndex] = $entry;
141                }
142                return $answers;
143        }
144
145}       
146
147
148?>
Note: See TracBrowser for help on using the repository browser.