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

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

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File size: 3.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 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               
67                $answers = $this->getAnswers($headers);
68
69                //Create the strings and write to file.
70                $headerString = "";
71                foreach($headers as $headerElement)
72                {
73                        $headerString = $headerString . $headerElement . "\t";
74                }
75                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
76                fwrite($fileHandler, $headerString . "\n");
77                foreach($answers as $entry)
78                {
79                        $entryString = "";
80                        foreach(array_keys($entry) as $i)
81                        {
82                                $entryString = $entryString . $entry[$i] . "\t";       
83                        }
84                        fwrite($fileHandler, $entryString . "\n");     
85                }
86        }
87
88        /*
89         * function getAnswers
90         * @param type $headers
91         * Based on the questions specified in the headers array, matches the answers of every
92         * respondent to the correct questions and returns an array containing a list of answers per
93         * respondent
94         */
95        private function getAnswers($headers)
96        {
97                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
98                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
99                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
100                $respondentsUsed = array();
101                $answers = array();
102                foreach($this->answersets as $answerset)
103                {
104                        $entryIndex;
105                        if(in_array($answerset->respondent->uid, $respondentsUsed))
106                        {
107                                $entryIndex = array_search($answerset->respondent->uid, $respondentsUsed);
108                                $entry = $answers[$entryIndex];
109                        }
110                        else
111                        {
112                                $entry = array();
113                                $entry[0] = $answerset->respondent->uid;
114                                $entryIndex = count($respondentsUsed);
115                                $respondentsUsed[] = $answerset->respondent->uid;
116                        }
117                        foreach ($answerset->answers as $answer)
118                        {
119                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
120                                //Index has to be a good number, else the answer doesn't belong in this session
121                                //Causes might be a missing question or having a wrong survey
122                                if($index != 0)
123                                {
124                                        $value = "";
125                                        foreach ($answer->values as $val)
126                                        {
127                                                $value = $value . $val . ",";
128                                        }
129                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
130                                        $entry[$index] = $value;
131                                }
132                        }
133                        $answers[$entryIndex] = $entry;
134                }
135                return $answers;
136        }
137
138}       
139
140
141?>
Note: See TracBrowser for help on using the repository browser.