source: Dev/branches/jos-branch/classes_old/AnswerSetConnector.php @ 210

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

Moved all classes into classes_old

File size: 3.8 KB
Line 
1<?php
2
3/**
4 * Description of AnswerSetConnector
5 *
6 * @author jkraaijeveld
7 */
8class AnswerSetConnector extends Connector{
9    protected $db;
10
11        /**
12         * Constructor for the AnswerSetConnector
13         */
14        public function __construct()
15        {
16                $this->fileName = 'data/answers/answersets.rdf';
17                //Ensure the required folder for this connector exists
18                if(!is_dir('data/answers'))
19                        mkdir('data/answers');
20        }
21
22        /**
23         * function get()
24         * @param type $arguments : An array having one ore more of the following elements:
25         * 'uid', 'survey', 'respondent', 'answers'.
26         */
27        public function get($arguments)
28        {
29                $this->load();
30
31                //Build the query string
32                $querystring = '
33                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
34                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
35                        SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
36                        WHERE
37                        {
38                                _answerset      predicates:resource_type        resources:answerset ;
39                                predicates:uid ?uid ;
40                                predicates:for_survey ?for_survey ;
41                                predicates:by_respondent ?by_respondent ;
42                                ' . $this->createArguments($arguments) . '
43                        }';
44                //Query the model
45                $results = $this->model->sparqlQuery($querystring);
46                $answerSets = array();
47                if(!empty($results))
48                {
49                        $this->db = new DatabaseInterface();
50                        foreach($results as $result)
51                        {
52                                $survey = $result['?for_survey']->label;
53                                $respondent = $result['?by_respondent']->label;
54                                $answers =      $this->getAnswers($result['?uid']->label);
55                                $answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers);
56                        }
57                }
58                return $answerSets;
59        }
60
61        /**
62         * function getAnswers()
63         * @param type $uid : the uid for which the answers should be retrieved.
64         */
65        private function getAnswers($uid)
66        {
67                $result = $this->model->findRegex("[(".$uid.")]", "[(given_answer)]", null);
68                $iterator = $result->getStatementIterator();
69                $answers = array();
70                while($iterator->hasNext())
71                {
72                        $element = $iterator->next();
73                        $answers[] = $element->getLabelObject();
74                }
75                return $answers;
76        }
77
78        /**
79         * function set()
80         * @param type $rToolObject : The ResearchToolObject to be saved.
81         */
82        public function set($rToolObject)
83        {
84                $this->load();
85                //If evaluation fails, some references are incorrect.
86                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
87                //TODO: Decide how to fix invalid references graciously.
88                if(!$rToolObject->evaluate())
89                        return false;
90
91                $resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid);
92                //Remove the old value stored with the given id
93                $this->model->subtract($this->model->find($resourceAnswerSet, null, null));
94
95                //Set the new values
96                $resourceAnswerSetType = new Resource(ANSWERSET);
97                $predicateRType = new Resource(RTYPE);
98                $this->model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
99
100                $answerSetId = new Literal($rToolObject->uid);
101                $predicateId = new Resource(UID);
102                $this->model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
103
104                $surveyId = new Literal($rToolObject->survey->uid);
105                $predicateSurvey = new Resource(FOR_SURVEY);
106                $this->model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
107
108                $respondentId = new Literal($rToolObject->respondent->uid);
109                $predicateRespondent = new Resource(BY_RESPONDENT);
110                $this->model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
111               
112                if(isset($rToolObject->answers))
113                {
114                        foreach($rToolObject->answers as $answer)
115                        {
116                                $answerId = new Literal($answer->uid);
117                                $predicateAnswer = new Resource(GIVEN_ANSWER);
118                                $this->model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
119                        }
120                }
121                $this->save();
122                return true;
123        }
124}
Note: See TracBrowser for help on using the repository browser.