source: Dev/trunk/classes/QuestionConnector.php @ 158

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

Refactored queries / cleaned up some code.

File size: 6.7 KB
Line 
1<?php
2
3// Survey database interface class as intermediate for storing data from the site to the RDF database
4require_once 'rdfConstants.php';
5// Include RAP Library to write RDF files
6include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
7
8
9/**
10 * Description of QuestionConnector
11 *
12 * @author jkraaijeveld
13 */
14class QuestionConnector implements IConnector {
15   
16    protected $model;
17    protected $fileName = 'data/questions/questions.rdf';
18   
19    /**
20     * Constructor for QuestionConnector
21     */
22    public function __construct()
23    {
24        //Ensure the required folder for this connector exists
25        if (!is_dir('data/questions/'))
26            mkdir('data/questions/');   
27    }
28   
29    /**
30     * function load()
31     * Loads the file into the standard MemModel.
32     */
33    public function load() {
34        //Get the Memory Model from the ModelFactory
35        $this->model = ModelFactory::getDefaultModel();
36       
37        //Ensure the required file exists before loading
38        if(file_exists($this->fileName))
39            $this->model->load($this->fileName);
40    }
41   
42    /**
43     * function save()
44     * Saves the MemModel into the given file.
45     */
46    public function save() {
47        $this->model->saveAs($this->fileName,'rdf');
48    }
49   
50    /**
51     * Get the questions corresponding to the arguments.
52         * @param type $arguments : An array having one ore more of the following elements:
53         * 'uid', 'title', 'type', 'description', 'category', 'has_answer'.
54     */
55    public function get($arguments)
56    {
57        $this->load();
58       
59        $keys = array_keys($arguments);
60        //Set default values for arguments
61        $uid = ""; $title = ""; $type = ""; $description = ""; $category = ""; $has_answer = "";
62        //Set the arguments if they are supplied
63        if(in_array("uid", $keys))
64            $uid = 'predicates:question_code \''.$arguments["uid"].'\'';
65        if(in_array("title", $keys))
66            $title = 'predicates:title \''.$arguments["title"].'\'';
67        if(in_array("type", $keys))
68            $type = 'predicates:type \''.$arguments["type"].'\'';
69        if(in_array("description", $keys))
70            $description = "predicates:description \"".$arguments["description"]."\"";
71        if(in_array("category", $keys))
72                        $style = "predicates:category \"".$arguments["category"]."\"";
73                if(in_array("answers", $keys))
74                {
75                        foreach($arguments["answers"] as $answer)
76                        {
77                                $has_answer = $has_answer . 'predicates:has_answer \'' . $answer . '\' ';
78                        }
79                }
80           
81        $querystring = '
82            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
83            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
84            SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category
85            WHERE       
86            {
87                _question       predicates:resource_type        resources:question ;               
88                predicates:question_code ?uid ;
89                predicates:title ?title ;
90                predicates:question_type ?type ;
91                predicates:description ?description ;
92                predicates:question_category ?category ;
93                                '. $uid . $title . $type . $description . $category . $has_answer . '
94            }';
95        //Create the querystring
96        $results = $this->model->sparqlQuery($querystring);
97
98       
99        $questions = array();
100        if(!empty($results))
101        {
102            foreach($results as $result)
103            {
104                $answers = $this->getAnswers($result['?uid']->label);
105                $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
106            }
107        }
108        return $questions;
109    }
110   
111    /**
112     * Gets the answers belonging to the given uid.
113     * @param type $uid : The uid of the Question in question (haha, pun).
114     */
115    private function getAnswers($uid)
116    {
117        $querystring = '
118            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
119            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
120            SELECT DISTINCT ?has_answer
121            WHERE
122            {
123                 _question      predicates:resource_type        resources:question ;
124                predicates:has_answer ?has_answer ;
125                predicates:question_code \'' . $uid . '\'
126            }';
127
128        $answers = $this->model->sparqlQuery($querystring);
129        $returnArray = array();
130        if(!empty($answers))
131        {
132            foreach($answers as $answer)
133            {
134                $returnArray[] = $answer['?has_answer']->label;
135            }
136        }
137        return $returnArray;
138    }
139
140   
141    /**
142     * Save the given ResearchTool object
143     * @param type $rToolObject : The ResearchToolObject to be saved.
144     */
145    public function set($rToolObject)
146    {
147        $this->load();
148       
149        $resourceQuestion = new Resource(QUESTION.'/'.$rToolObject->uid);       
150        //Remove the old value stored with the given id
151        $this->model->subtract($this->model->find($resourceQuestion, null, null));
152       
153        //Save all the new values
154        $resourceQuestionType = new Resource(QUESTION);
155        $predicateRType = new Resource(RTYPE);
156        $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
157       
158        $questionQCode = new Literal($rToolObject->uid);
159        $predicateQCode = new Resource(QCODE);
160        $this->model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
161       
162        $questionTitle = new Literal($rToolObject->title);
163        $predicateTitle = new Resource(TITLE);         
164        $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));     
165
166        $questionDescription = new Literal($rToolObject->description);
167        $predicateDescription = new Resource(DESCRIPTION);
168        $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
169
170        $resourceQuestionType = new Literal($rToolObject->type);
171        $predicateQType = new Resource(QTYPE);
172        $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
173       
174        $questionQCategory = new Literal($rToolObject->category);
175        $predicateQCategory = new Resource(QCATEGORY);
176        $this->model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
177
178        if(isset($rToolObject->answers))
179        {
180            foreach($rToolObject->answers as $answer)
181            {           
182                $answerValue = new Literal($answer);
183                $predicateAnswer = new Resource(HAS_ANSWER);   
184                $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
185            }
186        }
187        $this->save();
188    }
189}
190
191?>
Note: See TracBrowser for help on using the repository browser.