[149] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Description of Session
|
---|
| 4 | *
|
---|
| 5 | * @author jkraaijeveld
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | class Session extends ResearchToolObject
|
---|
| 9 | {
|
---|
| 10 | public $title;
|
---|
[159] | 11 | public $creator;
|
---|
[149] | 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
|
---|
[159] | 21 | * @param type $creator
|
---|
[149] | 22 | * @param type $datetime
|
---|
| 23 | * @param type $pipeline
|
---|
| 24 | */
|
---|
[159] | 25 | public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null, $answersets = null)
|
---|
[149] | 26 | {
|
---|
| 27 | if(!isset($uid))
|
---|
| 28 | {
|
---|
| 29 | $uid = md5(uniqid(rand(), true));
|
---|
| 30 | }
|
---|
| 31 | $this->uid = $uid;
|
---|
| 32 | $this->title = $title;
|
---|
[159] | 33 | $this->creator = $creator;
|
---|
[149] | 34 | $this->datetime = $datetime;
|
---|
| 35 | $this->pipeline = $pipeline;
|
---|
| 36 | $this->answersets = $answersets;
|
---|
| 37 | }
|
---|
[170] | 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 | }
|
---|
[171] | 50 | //Create the filename, open the file at the given location
|
---|
[170] | 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 | }
|
---|
[173] | 66 | //Return if there are no surveys.
|
---|
| 67 | if(count($headers) < 2)
|
---|
| 68 | return;
|
---|
[170] | 69 |
|
---|
[174] | 70 | //Get the answers for all respondents
|
---|
[170] | 71 | $answers = $this->getAnswers($headers);
|
---|
| 72 |
|
---|
| 73 | //Create the strings and write to file.
|
---|
| 74 | $headerString = "";
|
---|
| 75 | foreach($headers as $headerElement)
|
---|
| 76 | {
|
---|
| 77 | $headerString = $headerString . $headerElement . "\t";
|
---|
| 78 | }
|
---|
| 79 | $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
|
---|
| 80 | fwrite($fileHandler, $headerString . "\n");
|
---|
| 81 | foreach($answers as $entry)
|
---|
| 82 | {
|
---|
| 83 | $entryString = "";
|
---|
[173] | 84 | //PHP doesn't automatically sort the indices so I have to do it myself
|
---|
| 85 | //Entry elements can now be printed in order to match the headers
|
---|
[174] | 86 | for ($i = 0; $i < count($headers); $i++)
|
---|
[170] | 87 | {
|
---|
[174] | 88 | if(in_array($i, array_keys($entry)))
|
---|
| 89 | $entryString = $entryString . $entry[$i] . "\t";
|
---|
| 90 | else
|
---|
| 91 | $entryString = $entryString . "\t";
|
---|
[170] | 92 | }
|
---|
| 93 | fwrite($fileHandler, $entryString . "\n");
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * function getAnswers
|
---|
| 99 | * @param type $headers
|
---|
| 100 | * Based on the questions specified in the headers array, matches the answers of every
|
---|
| 101 | * respondent to the correct questions and returns an array containing a list of answers per
|
---|
| 102 | * respondent
|
---|
| 103 | */
|
---|
| 104 | private function getAnswers($headers)
|
---|
| 105 | {
|
---|
| 106 | //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
|
---|
| 107 | //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
|
---|
| 108 | //already exists within the answers-array, use the index of the respondent. Else: make a new row.
|
---|
| 109 | $respondentsUsed = array();
|
---|
| 110 | $answers = array();
|
---|
| 111 | foreach($this->answersets as $answerset)
|
---|
| 112 | {
|
---|
| 113 | $entryIndex;
|
---|
[173] | 114 | if(in_array($answerset->respondent->name, $respondentsUsed))
|
---|
[170] | 115 | {
|
---|
[173] | 116 | $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
|
---|
[170] | 117 | $entry = $answers[$entryIndex];
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
| 121 | $entry = array();
|
---|
[173] | 122 | $entry[0] = $answerset->respondent->name;
|
---|
[170] | 123 | $entryIndex = count($respondentsUsed);
|
---|
[173] | 124 | $respondentsUsed[] = $answerset->respondent->name;
|
---|
[170] | 125 | }
|
---|
| 126 | foreach ($answerset->answers as $answer)
|
---|
| 127 | {
|
---|
| 128 | $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
|
---|
| 129 | //Index has to be a good number, else the answer doesn't belong in this session
|
---|
| 130 | //Causes might be a missing question or having a wrong survey
|
---|
| 131 | if($index != 0)
|
---|
| 132 | {
|
---|
| 133 | $value = "";
|
---|
| 134 | foreach ($answer->values as $val)
|
---|
| 135 | {
|
---|
| 136 | $value = $value . $val . ",";
|
---|
| 137 | }
|
---|
[174] | 138 | //We all hate trailing commas
|
---|
[170] | 139 | if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
|
---|
| 140 | $entry[$index] = $value;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | $answers[$entryIndex] = $entry;
|
---|
| 144 | }
|
---|
| 145 | return $answers;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[149] | 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | ?>
|
---|