source: Dev/branches/jos-branch/classes/models/AnswerSet.php @ 217

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

Added SessionInstance? and a semi-functional resultSet.

File size: 5.2 KB
Line 
1<?php
2// Survey database interface class as intermediate for storing data from the site to the RDF database
3require_once 'rdfConstants.php';
4// Include RAP Library to write RDF files
5include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
6
7/**
8 * AnswerSet is a collection of answers corresponding to a Session
9 *
10 */
11class AnswerSet extends ResearchToolObject{
12        public static $filename = 'data/results/answersets.rdf';
13
14        public $survey;
15        public $respondent;
16        public $answers;
17
18        /**
19         * Constructor for an AnswerSet object
20         * @param type $uid
21         * @param type $survey
22         * @param type $respondent
23         * @param type $answers
24         */
25        public function __construct($uid=null, $survey=null, $respondent=null, $answers=null)
26        {
27                if(!isset($uid))
28                {
29                        $uid = md5(uniqid(rand(), true));
30                }
31                $this->uid = $uid;
32                $this->survey = $survey;
33                $this->respondent = $respondent;
34                $this->answers = $answers;
35        }
36
37        /**
38         * function evaluate()
39         * evaluates the references of survey, respondent and answers.
40         */
41        public function evaluate()
42        {
43                if(is_string($this->survey))
44                {
45                        $result = Survey::get(array("uid" => $this->survey));
46                        if(!isset($result[0]))
47                                return false;
48                        $this->survey = $result[0];
49                }
50                if(is_string($this->respondent))
51                {
52                        $result = Respondent::get(array("uid" => $this->respondent));
53                        if(!isset($result[0]))
54                                return false;
55                        $this->respondent = $result[0];
56                }
57                if(!empty($this->answers) && is_string($this->answers[0]))
58                {
59                        $newanswers = array();
60                        foreach($this->answers as $answer)
61                        {
62                                $result = Answer::get(array("uid" => $answer));
63                                if(!isset($result[0]))
64                                        return false;
65                                $newanswers[] = $result[0];
66                        }
67                        $this->answers = $newanswers;
68                }
69                return true;
70        }
71
72
73        /**
74         * function save()
75         * Saves the current object into the database.
76         */
77        public function save()
78        {
79                //If evaluation fails, some references are incorrect.
80                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
81                //TODO: Decide how to fix invalid references graciously.
82                if(!$this->evaluate())
83                        return false;
84                //Ensure the required folder exists.
85                if(!is_dir('data/results/'))
86                        mkdir('data/results/');
87
88                $model = ResearchToolObject::load(AnswerSet::$filename);
89
90
91                $resourceAnswerSet = new Resource(ANSWERSET . '/' . $this->uid);
92                //Remove the old value stored with the given id
93                $model->subtract($model->find($resourceAnswerSet, null, null));
94
95                //Set the new values
96                $resourceAnswerSetType = new Resource(ANSWERSET);
97                $predicateRType = new Resource(RTYPE);
98                $model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
99
100                $answerSetId = new Literal($this->uid);
101                $predicateId = new Resource(UID);
102                $model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
103
104                $surveyId = new Literal($this->survey->uid);
105                $predicateSurvey = new Resource(FOR_SURVEY);
106                $model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
107
108                $respondentId = new Literal($this->respondent->uid);
109                $predicateRespondent = new Resource(BY_RESPONDENT);
110                $model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
111               
112                if(isset($this->answers))
113                {
114                        foreach($this->answers as $answer)
115                        {
116                                $answerId = new Literal($answer->uid);
117                                $predicateAnswer = new Resource(GIVEN_ANSWER);
118                                $model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
119                        }
120                }
121                $model->saveAs(AnswerSet::$filename, 'rdf');
122                return true;
123        }
124
125        /**
126         * function get()
127         * @param type $arguments : An array having one ore more of the following elements:
128         * 'uid', 'survey', 'respondent', 'answers'.
129         */
130        public static function get($arguments)
131        {
132                $model = ResearchToolObject::load(AnswerSet::$filename);
133
134                //Build the query string
135                $querystring = '
136                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
137                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
138                        SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
139                        WHERE
140                        {
141                                _answerset      predicates:resource_type        resources:answerset ;
142                                predicates:uid ?uid ;
143                                predicates:for_survey ?for_survey ;
144                                predicates:by_respondent ?by_respondent ;
145                                ' . ResearchToolObject::createArguments($arguments) . '
146                        }';
147                //Query the model
148                $results = $model->sparqlQuery($querystring);
149                $answerSets = array();
150                if(!empty($results))
151                {
152                        foreach($results as $result)
153                        {
154                                $survey = $result['?for_survey']->label;
155                                $respondent = $result['?by_respondent']->label;
156                                $answers =      AnswerSet::getAnswers($model, $result['?uid']->label);
157                                $answerSets[] = new AnswerSet($result['?uid']->label, $survey, $respondent, $answers);
158                        }
159                }
160                return $answerSets;
161        }
162
163        /**
164         * function getAnswers()
165         * @param type $uid : the uid for which the answers should be retrieved.
166         */
167        private static function getAnswers($model, $uid)
168        {
169                $result = $model->findRegex("[(".$uid.")]", "[(given_answer)]", null);
170                $iterator = $result->getStatementIterator();
171                $answers = array();
172                while($iterator->hasNext())
173                {
174                        $element = $iterator->next();
175                        $answers[] = $element->getLabelObject();
176                }
177                return $answers;
178        }
179}
180
Note: See TracBrowser for help on using the repository browser.