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

Last change on this file since 170 was 170, checked in by jkraaijeveld, 13 years ago
  • Session objects now have a 'toSPSS($location)' method, which creates a file at the specified location which can be imported into SPSS.
File size: 3.7 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                $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt";
51
52                //Make a list of all 'column headers', e.g. "respondent" and all the question codes.
53                $headers = array();
54                $headers[] = "respondent";
55                foreach ($this->pipeline as $element)
56                {
57                        if(get_class($element) == "Survey")
58                        {
59                                foreach($element->questions as $question)
60                                {
61                                        $headers[] = $element->title . '_' . $question->uid;
62                                }
63                        }
64                }
65               
66                $answers = $this->getAnswers($headers);
67
68                //Create the strings and write to file.
69                $headerString = "";
70                foreach($headers as $headerElement)
71                {
72                        $headerString = $headerString . $headerElement . "\t";
73                }
74                $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
75                fwrite($fileHandler, $headerString . "\n");
76                foreach($answers as $entry)
77                {
78                        $entryString = "";
79                        foreach(array_keys($entry) as $i)
80                        {
81                                $entryString = $entryString . $entry[$i] . "\t";       
82                        }
83                        fwrite($fileHandler, $entryString . "\n");     
84                }
85        }
86
87        /*
88         * function getAnswers
89         * @param type $headers
90         * Based on the questions specified in the headers array, matches the answers of every
91         * respondent to the correct questions and returns an array containing a list of answers per
92         * respondent
93         */
94        private function getAnswers($headers)
95        {
96                //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
97                //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
98                //already exists within the answers-array, use the index of the respondent. Else: make a new row.
99                $respondentsUsed = array();
100                $answers = array();
101                foreach($this->answersets as $answerset)
102                {
103                        $entryIndex;
104                        if(in_array($answerset->respondent->uid, $respondentsUsed))
105                        {
106                                $entryIndex = array_search($answerset->respondent->uid, $respondentsUsed);
107                                $entry = $answers[$entryIndex];
108                        }
109                        else
110                        {
111                                $entry = array();
112                                $entry[0] = $answerset->respondent->uid;
113                                $entryIndex = count($respondentsUsed);
114                                $respondentsUsed[] = $answerset->respondent->uid;
115                        }
116                        foreach ($answerset->answers as $answer)
117                        {
118                                $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
119                                //Index has to be a good number, else the answer doesn't belong in this session
120                                //Causes might be a missing question or having a wrong survey
121                                if($index != 0)
122                                {
123                                        $value = "";
124                                        foreach ($answer->values as $val)
125                                        {
126                                                $value = $value . $val . ",";
127                                        }
128                                        if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
129                                        $entry[$index] = $value;
130                                }
131                        }
132                        $answers[$entryIndex] = $entry;
133                }
134                return $answers;
135        }
136
137}       
138
139
140?>
Note: See TracBrowser for help on using the repository browser.