source: Dev/branches/rest-dojo-ui/server/classes/models/Answer.php @ 302

Last change on this file since 302 was 302, checked in by hendrikvanantwerpen, 13 years ago

Merge jos-branch 285:298

File size: 4.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 Answer
9 *
10 * @author jkraaijeveld
11 */
12class Answer extends ResearchToolObject {
13
14        /**
15         * static function create()
16         * @param type $obj : The object which should be converted to an Answer
17         */
18        public static function create($obj)
19        {
20                return new Answer($obj->uid, $obj->question, $obj->values);
21        }
22
23        public static $filename = 'data/results/answers.rdf';
24
25    public $question;
26        public $values;
27   
28    /**
29     * Constructor for an Answer object
30     * @param type $uid : The uid of the Answer object.
31     * @param type $question : The Question object this Answer answers.
32     * @param type $values : An array of strings, containing the answers.
33     */
34    public function __construct($uid = null, $question = null, $values = null) {
35      if(!isset($uid))
36      {
37            $uid = md5(uniqid(rand(), true));
38      }
39      $this->uid = $uid;
40      $this->question = $question;
41      $this->values = $values;
42        }
43       
44        /**
45         * function evaluate()
46         * Evaluates the references and fills in the objects for them instead.
47         */
48        public function evaluate()
49        {
50                if(is_string($this->question))
51                {
52                        $ans = ResearchToolObject::stripUri($this->question);
53                        $result = Question::get(array("uid" => $ans["uid"]));
54                        if(!isset($result[0]))
55                                return false;
56                        $this->question = $result[0];
57                }
58                return true;
59        }
60
61
62        /**
63         * function get()
64         * @param type $arguments: An array having one or more of the following elements:
65         * 'uid', 'question', 'values'.
66         */
67    public static function get($arguments)
68        {
69                $model = ResearchToolObject::load(Answer::$filename);
70                //Create the querystring
71                $querystring = '
72                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
73                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
74                        SELECT DISTINCT ?uid, ?to_question
75                        WHERE
76                        {
77                                _answer predicates:resource_type resources:answer;
78
79                                predicates:uid ?uid ;
80                                predicates:to_question ?to_question ;
81                                ' . ResearchToolObject::createArguments($arguments) . '
82                        }';
83
84                //Query the model
85                $results = $model->sparqlQuery($querystring);   
86                //An answer can have multiple values, get all these values for the answer instances found.
87                $answers = array();
88                if(!empty($results))
89                {
90                        foreach($results as $result)
91                        {
92                                $answers[] = new Answer($result['?uid']->label, $result['?to_question']->uri, Answer::getValues($model, $result['?uid']->label));
93                        }
94                }
95                return $answers;
96        }
97
98        /**
99         * function getValues()
100         * @param type $uid : The uid of the Answer for which the values should be gotten.
101         */
102        private static function getValues($model, $uid)
103        {
104                $result = $model->findRegex("[(".$uid.")]", "[(answered)]", null);
105                $iterator = $result->getStatementIterator();
106                $values = array();
107                while($iterator->hasNext())
108                {
109                        $element = $iterator->next();
110                        $values[] = $element->getLabelObject();
111                }
112                return $values;
113        }
114
115    /**
116         * function save()
117         * Saves the current object into the database.
118     */
119    public function save()
120    {
121                //If evaluation fails, some references are incorrect.
122                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
123                //TODO: Decide how to fix invalid references graciously.
124                if(!$this->evaluate())
125                        throw new Exception('Evaluation failed.');
126
127                //Ensure the required folder exists.
128                if(!is_dir('data/results'))
129                        mkdir('data/results');
130                $model = ResearchToolObject::load(Answer::$filename);
131
132        $resourceAnswer = new Resource(ANSWER.'/'.$this->uid);       
133        //Remove the old value stored with the given id
134        $model->subtract($model->find($resourceAnswer, null, null));
135       
136        //Save all the new values
137        $resourceAnswerType = new Resource(ANSWER);
138        $predicateRType = new Resource(RTYPE);
139        $model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));
140       
141                $answerId = new Literal($this->uid);
142                $predicateId = new Resource(UID);
143                $model->add(new Statement($resourceAnswer, $predicateId, $answerId));
144
145                $questionId = new Resource(QUESTION . '/' . $this->question->uid);
146                $predicateQId = new Resource(TO_QUESTION);
147                $model->add(new Statement($resourceAnswer, $predicateQId, $questionId));
148
149                if(isset($this->values))
150                {
151                        foreach($this->values as $value)
152                        {
153                                $answerValue = new Literal($value);
154                                $predicateAnswer = new Resource(ANSWERED);
155                                $model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue));
156                        }
157                }
158                $model->saveAs(Answer::$filename, 'rdf');
159                return true;
160        }
161}
162
163?>
Note: See TracBrowser for help on using the repository browser.