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

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

Implemented lazy evaluation.

  • Initially, references to other database objects are now given as an ID rather than an object.
  • Every model class now has an evaluate() function, which gets all the objects that object has references to. Returns true if it got all the values correctly, false if there are invalid references.
  • Every connector now first evaluates an object before storing, to make sure only objects with valid references get stored.
File size: 3.6 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 extends Connector{
13   
14    /**
15     * Constructor for AnswerConnector
16     */
17    public function __construct() {
18        //Ensure the required folder for this connector exists
19        if (!is_dir('data/answers/'))
20                        mkdir('data/answers/');
21                $this->fileName = 'data/answers/answers.rdf';
22    }
23   
24        /**
25         * function get()
26         * @param type $arguments: An array having one or more of the following elements:
27         * 'uid', 'question', 'values'.
28         */
29    public function get($arguments)
30        {
31                $this->load();
32                //Create the querystring
33                $querystring = '
34                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
35                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
36                        SELECT DISTINCT ?uid, ?question_code
37                        WHERE
38                        {
39                                _answer predicates:resource_type resources:answer;
40
41                                predicates:uid ?uid ;
42                                predicates:question_code ?question_code ;
43                                ' . $this->createArguments($arguments) . '
44                        }';
45
46                //Query the model
47                $results = $this->model->sparqlQuery($querystring);     
48                //An answer can have multiple values, get all these values for the answer instances found.
49                $answers = array();
50                if(!empty($results))
51                {
52                        foreach($results as $result)
53                        {
54                                $answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, $this->getValues($result['?uid']->label));
55                        }
56                }
57                return $answers;
58        }
59
60        /**
61         * function getValues()
62         * @param type $uid : The uid of the Answer for which the values should be gotten.
63         */
64        private function getValues($uid)
65        {
66                $result = $this->model->findRegex("[(".$uid.")]", "[(answered)]", null);
67                $iterator = $result->getStatementIterator();
68                $values = array();
69                while($iterator->hasNext())
70                {
71                        $element = $iterator->next();
72                        $values[] = $element->getLabelObject();
73                }
74                return $values;
75        }
76
77    /**
78         * function set()
79     * @param type $rToolObject : The ResearchToolObject to be saved.
80     */
81    public function set($rToolObject)
82    {
83                $this->load();
84                //If evaluation fails, some references are incorrect.
85                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
86                //TODO: Decide how to fix invalid references graciously.
87                if(!$rToolObject->evaluate())
88                        return false;
89        $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid);       
90        //Remove the old value stored with the given id
91        $this->model->subtract($this->model->find($resourceAnswer, null, null));
92       
93        //Save all the new values
94        $resourceAnswerType = new Resource(ANSWER);
95        $predicateRType = new Resource(RTYPE);
96        $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));
97       
98                $answerId = new Literal($rToolObject->uid);
99                $predicateId = new Resource(UID);
100                $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId));
101
102                $questionId = new Literal($rToolObject->question->uid);
103                $predicateQId = new Resource(QCODE);
104                $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId));
105
106                if(isset($rToolObject->values))
107                {
108                        foreach($rToolObject->values as $value)
109                        {
110                                $answerValue = new Literal($value);
111                                $predicateAnswer = new Resource(ANSWERED);
112                                $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue));
113                        }
114                }
115                $this->save();
116                return true;
117    }
118}
119
120?>
Note: See TracBrowser for help on using the repository browser.