source: Dev/branches/jos-branch/classes_old/Session.php @ 210

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

Moved all classes into classes_old

File size: 5.4 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 $creationdate;
13        public $pipeline;
14
15       
16        /**
17         * Constructor for a Session object
18         * @param type $uid
19         * @param type $title
20         * @param type $creator
21         * @param type $datetime
22         * @param type $pipeline
23         */
24        public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null)
25        {
26                if(!isset($uid))
27                {
28                        $uid = md5(uniqid(rand(), true));
29                }
30                $this->uid = $uid;
31                $this->title = $title;
32                $this->creator = $creator;
33                $this->creationdate = $datetime;
34                $this->pipeline = $pipeline;
35        }
36
37        /*
38         * function evaluate(()
39         * Evaluates all the references of this object.
40         */
41        public function evaluate()
42        {
43                $dbi = new DatabaseInterface();
44                if(is_string($this->creator))
45                {
46                        $result = $dbi->get("user", array("uid" => $this->creator));
47                        if(!isset($result[0]))
48                                return false;
49                        $this->creator = $result[0];
50                }
51                if(!empty($this->pipeline) && is_string($this->pipeline[0]))
52                {
53                        $newPipeline = array();
54                        foreach($this->pipeline as $element)
55                        {
56                                //Check if the element's UID can be found in surveys, if not:
57                                //Check applications. If it isn't in either: invalid reference.
58                                $result = $dbi->get("survey", array("uid" => $element));
59                                if(!isset($result[0]))
60                                        $result = $dbi->get("application", array("uid" => $element));
61                                if(!isset($result[0]))
62                                        return false;
63                                $newPipeline[] = $result[0];
64                        }
65                        $this->pipeline = $newPipeline;
66                }
67                return true;
68        }
69
70        /**
71         * function toSPSS
72         * @param type $location
73         * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt'
74         * One big note about toSPSS: If this session contains multiple instances of THE SAME Survey,
75         * the world burns, people die, etcetera. (or, in a more fortunate case, the latest values
76         * are written to the file whilst the others get lost.
77         */
78        //DO NOT USE - BROKEN AS OF 23/12/2011
79        public function toSPSS($location)
80        {
81                $this->evaluate();
82                if(!is_dir($location))
83                {
84                        mkdir($location);
85                }
86                //Create the filename, open the file at the given location
87                $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt";
88
89                //Make a list of all 'column headers', e.g. "respondent" and all the question codes.
90                $headers = array();
91                $headers[] = "respondent";
92                foreach ($this->pipeline as $element)
93                {
94                        if(get_class($element) == "Survey")
95                        {
96                                $element->evaluate();
97                                foreach($element->questions as $question)
98                                {
99                                        //TODO: This might need fixing after lazy initialization refactoring.
100                                        $headers[] = $element->title . '_' . $question->uid;
101                                }
102                        }
103                }
104                //Return if there are no surveys.
105                if(count($headers) < 2)
106                        return;
107               
108                //Get the answers for all respondents
109                $answers = $this->getAnswers($headers);
110
111                //Create the strings and write to file.
112                $headerString = "";
113                foreach($headers as $headerElement)
114                {
115                        $headerString = $headerString . $headerElement . "\t";
116                }
117                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
118                fwrite($fileHandler, $headerString . "\n");
119                foreach($answers as $entry)
120                {
121                        $entryString = "";
122                        //PHP doesn't automatically sort the indices so I have to do it myself
123                        //Entry elements can now be printed in order to match the headers
124                        for ($i = 0; $i < count($headers); $i++)
125                        {
126                                if(in_array($i, array_keys($entry)))
127                                        $entryString = $entryString . $entry[$i] . "\t";       
128                                else
129                                        $entryString = $entryString . "\t";
130                        }
131                        fwrite($fileHandler, $entryString . "\n");     
132                }
133        }
134
135        /*
136         * function getAnswers
137         * @param type $headers
138         * Based on the questions specified in the headers array, matches the answers of every
139         * respondent to the correct questions and returns an array containing a list of answers per
140         * respondent
141         */
142        private function getAnswers($headers)
143        {
144                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
145                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
146                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
147                $respondentsUsed = array();
148                $answers = array();
149                foreach($this->answersets as $answerset)
150                {
151                        $entryIndex;
152                        if(in_array($answerset->respondent->name, $respondentsUsed))
153                        {
154                                $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
155                                $entry = $answers[$entryIndex];
156                        }
157                        else
158                        {
159                                $entry = array();
160                                $entry[0] = $answerset->respondent->name;
161                                $entryIndex = count($respondentsUsed);
162                                $respondentsUsed[] = $answerset->respondent->name;
163                        }
164                        foreach ($answerset->answers as $answer)
165                        {
166                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
167                                //Index has to be a good number, else the answer doesn't belong in this session
168                                //Causes might be a missing question or having a wrong survey
169                                if($index != 0)
170                                {
171                                        $value = "";
172                                        foreach ($answer->values as $val)
173                                        {
174                                                $value = $value . $val . ",";
175                                        }
176                                        //We all hate trailing commas
177                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
178                                        $entry[$index] = $value;
179                                }
180                        }
181                        $answers[$entryIndex] = $entry;
182                }
183                return $answers;
184        }
185
186}       
187
188
189?>
Note: See TracBrowser for help on using the repository browser.