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

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

In Session: -datetime has been renamed to creationdate

Connector has been adjusted accordingly

  • Removed answersets

Added SessionInstance?: This class is for unique occurences of a session.

In SessionInstance? is a reference to the currently unimplemented ResultSet?. This class will contain all answersets and gameresults given the SessionInstance?.

File size: 5.4 KB
RevLine 
[149]1<?php
2/**
3 * Description of Session
4 *
5 * @author jkraaijeveld
6 */
7
8class Session extends ResearchToolObject
9{
10        public $title;
[159]11        public $creator;
[195]12        public $creationdate;
[149]13        public $pipeline;
14
15       
16        /**
17         * Constructor for a Session object
18         * @param type $uid
19         * @param type $title
[159]20         * @param type $creator
[149]21         * @param type $datetime
22         * @param type $pipeline
23         */
[195]24        public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null)
[149]25        {
26                if(!isset($uid))
27                {
28                        $uid = md5(uniqid(rand(), true));
29                }
30                $this->uid = $uid;
31                $this->title = $title;
[159]32                $this->creator = $creator;
[195]33                $this->creationdate = $datetime;
[149]34                $this->pipeline = $pipeline;
35        }
[170]36
[186]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
[170]70        /**
71         * function toSPSS
72         * @param type $location
73         * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt'
[186]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.
[170]77         */
[195]78        //DO NOT USE - BROKEN AS OF 23/12/2011
[170]79        public function toSPSS($location)
80        {
[186]81                $this->evaluate();
[170]82                if(!is_dir($location))
83                {
84                        mkdir($location);
85                }
[171]86                //Create the filename, open the file at the given location
[170]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                        {
[186]96                                $element->evaluate();
[170]97                                foreach($element->questions as $question)
98                                {
[186]99                                        //TODO: This might need fixing after lazy initialization refactoring.
[170]100                                        $headers[] = $element->title . '_' . $question->uid;
101                                }
102                        }
103                }
[173]104                //Return if there are no surveys.
105                if(count($headers) < 2)
106                        return;
[170]107               
[174]108                //Get the answers for all respondents
[170]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 = "";
[173]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
[174]124                        for ($i = 0; $i < count($headers); $i++)
[170]125                        {
[174]126                                if(in_array($i, array_keys($entry)))
127                                        $entryString = $entryString . $entry[$i] . "\t";       
128                                else
129                                        $entryString = $entryString . "\t";
[170]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;
[173]152                        if(in_array($answerset->respondent->name, $respondentsUsed))
[170]153                        {
[173]154                                $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
[170]155                                $entry = $answers[$entryIndex];
156                        }
157                        else
158                        {
159                                $entry = array();
[173]160                                $entry[0] = $answerset->respondent->name;
[170]161                                $entryIndex = count($respondentsUsed);
[173]162                                $respondentsUsed[] = $answerset->respondent->name;
[170]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                                        }
[174]176                                        //We all hate trailing commas
[170]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
[149]186}       
187
188
189?>
Note: See TracBrowser for help on using the repository browser.