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

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

Implemented lazy evaluation.

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