source: Dev/trunk/classes/AnswerConnector.php @ 157

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

Edited the pipelineeditor slightly so it does not error when adding a Survey.

File size: 4.7 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 * Description of AnswerConnector
9 *
10 * @author jkraaijeveld
11 */
12class AnswerConnector implements IConnector{
13    protected $model;
14        protected $fileName = 'data/answers/answers.rdf';
15        protected $db;
16   
17    /**
18     * Constructor for AnswerConnector
19     */
20    public function __construct() {
21        //Ensure the required folder for this connector exists
22        if (!is_dir('data/answers/'))
23            mkdir('data/answers/');
24    }
25   
26    /**
27     * function load()
28     * Loads the file into the standard MemModel.
29     */
30    public function load() {
31        //Get the Memory Model from the ModelFactory
32        $this->model = ModelFactory::getDefaultModel();
33       
34        //Ensure the required file exists before loading
35        if(file_exists($this->fileName))
36            $this->model->load($this->fileName);
37    }
38   
39    /**
40     * function save()
41     * Saves the MemModel into the given file.
42     */
43    public function save() {
44        $this->model->saveAs($this->fileName,'rdf');
45    }
46   
47        /**
48         * Function get()
49         * Get the answers corresponding to the arguments
50         * 'Question' argument is supposed to be the question ID
51         * 'Values' has to be an array to make sure multiple answers can be selected.
52         */
53    public function get($arguments)
54        {
55                $this->load();
56                $keys = array_keys($arguments);
57                $uid = ""; $question = ""; $value = "";
58                if(in_array("uid", $keys))
59                        $uid = 'predicates:uid \''.$arguments["uid"].'\'';
60                if(in_array("question", $keys))
61                        $question = 'predicates:question_code \''.$arguments["question"].'\'';
62                if(in_array("values", $keys))
63                {
64                        foreach ($arguments["values"] as $val)
65                        {
66                                $value = $value . 'predicates:answered \'' . $val . '\' ';
67                        }
68                }
69                //Create the querystring
70                $querystring = '
71                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
72                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
73                        SELECT DISTINCT ?uid, ?question_code
74                        WHERE
75                        {
76                                _answer predicates:resource_type resources:answer;
77
78                                predicates:uid ?uid ;
79                                predicates:question_code ?question_code ;
80                                ' . $uid . $question . $value . '
81                        }';
82
83                //Query the model
84                $results = $this->model->sparqlQuery($querystring);     
85                //An answer can have multiple values, get all these values for the answer instances found.
86                $answers = array();
87                if(!empty($results))
88                {
89            $this->db = new DatabaseInterface();
90                        foreach($results as $result)
91                        {
92                                $values = $this->getValues($result['?uid']->label);
93                                $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label));
94                                $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values);
95                        }
96                }
97                return $answers;
98
99        }
100        /**
101         * Gets the values that belong to the answer
102         * specified by the UID
103         */
104        private function getValues($uid)
105        {
106                $querystring = '
107                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
108                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
109                        SELECT DISTINCT ?answered
110                        WHERE
111                        {
112                                _answer predicates:resource_type resources:answer ;
113                                predicates:answered ?answered ;
114                                predicates:uid \''. $uid . '\'
115                }';
116                $values = $this->model->sparqlQuery($querystring);
117                $returnArray = array();
118                if(!empty($values))
119                {
120                        foreach($values as $value)
121                        {
122                                $returnArray[] = $value['?answered']->label;
123                        }
124                }
125                return $returnArray;
126        }
127
128    /**
129     * Saves the answer Object to the RDF file.
130     * @param type $rToolObject
131     */
132    public function set($rToolObject)
133    {
134        $this->load();
135       
136        $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid);       
137        //Remove the old value stored with the given id
138        $this->model->subtract($this->model->find($resourceAnswer, null, null));
139       
140        //Save all the new values
141        $resourceAnswerType = new Resource(ANSWER);
142        $predicateRType = new Resource(RTYPE);
143        $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));
144       
145                $answerId = new Literal($rToolObject->uid);
146                $predicateId = new Resource(UID);
147                $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId));
148
149                $questionId = new Literal($rToolObject->question->uid);
150                $predicateQId = new Resource(QCODE);
151                $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId));
152
153                if(isset($rToolObject->values))
154                {
155                        foreach($rToolObject->values as $value)
156                        {
157                                $answerValue = new Literal($value);
158                                $predicateAnswer = new Resource(ANSWERED);
159                                $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue));
160                        }
161                }
162        $this->save();
163    }
164}
165
166?>
Note: See TracBrowser for help on using the repository browser.