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

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

Added missing classes for the database connection

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
53     * @return type Array
54     */
55    public function get($arguments)
56    {
57        $this->load();
58       
59        $keys = array_keys($arguments);
60        //Set default values for arguments
61        $uid = "?uid"; $title = "?title"; $type = "?type"; $description = "?description"; $category = "?category"; $has_answer = "";
62        //Set the arguments if they are supplied
63        if(in_array("uid", $keys))
64            $uid = '\''.$arguments["uid"].'\'';
65        if(in_array("title", $keys))
66            $title = '\''.$arguments["title"].'\'';
67        if(in_array("type", $keys))
68            $type = '\''.$arguments["type"].'\'';
69        if(in_array("description", $keys))
70            $description = "\"".$arguments["description"]."\"";
71        if(in_array("category", $keys))
72                        $style = "\"".$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
94                predicates:question_code ' . $uid . '
95                predicates:title ' . $title . '
96                predicates:question_type ' . $type . '
97                predicates:description ' . $description . '
98                                predicates:question_category ' . $category . '
99                                ' . $has_answer . '
100            }';
101        //Create the querystring
102        $results = $this->model->sparqlQuery($querystring);
103
104       
105        $questions = array();
106        if(!empty($results))
107        {
108            foreach($results as $result)
109            {
110                $answers = $this->getAnswers($result['?uid']->label);
111                $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
112            }
113        }
114        return $questions;
115    }
116   
117    /**
118     * Gets the answers belonging to the given uid.
119     * @param type $uid
120     * @return type Array
121     */
122    private function getAnswers($uid)
123    {
124        $querystring = '
125            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
126            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
127            SELECT DISTINCT ?has_answer
128            WHERE
129            {
130                 _question      predicates:resource_type        resources:question ;
131                predicates:has_answer ?has_answer ;
132                predicates:question_code \'' . $uid . '\'
133            }';
134
135        $answers = $this->model->sparqlQuery($querystring);
136        $returnArray = array();
137        if(!empty($answers))
138        {
139            foreach($answers as $answer)
140            {
141                $returnArray[] = $answer['?has_answer']->label;
142            }
143        }
144        return $returnArray;
145    }
146
147   
148    /**
149     * Save the given ResearchTool object
150     * @param type $rToolObject
151     */
152    public function set($rToolObject)
153    {
154        $this->load();
155       
156        $resourceQuestion = new Resource(QUESTION.'/'.$rToolObject->uid);       
157        //Remove the old value stored with the given id
158        $this->model->subtract($this->model->find($resourceQuestion, null, null));
159       
160        //Save all the new values
161        $resourceQuestionType = new Resource(QUESTION);
162        $predicateRType = new Resource(RTYPE);
163        $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
164       
165        $questionQCode = new Literal($rToolObject->uid);
166        $predicateQCode = new Resource(QCODE);
167        $this->model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
168       
169        $questionTitle = new Literal($rToolObject->title);
170        $predicateTitle = new Resource(TITLE);         
171        $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));     
172
173        $questionDescription = new Literal($rToolObject->description);
174        $predicateDescription = new Resource(DESCRIPTION);
175        $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
176
177        $resourceQuestionType = new Literal($rToolObject->type);
178        $predicateQType = new Resource(QTYPE);
179        $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
180       
181        $questionQCategory = new Literal($rToolObject->category);
182        $predicateQCategory = new Resource(QCATEGORY);
183        $this->model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
184
185        if(isset($rToolObject->answers))
186        {
187            foreach($rToolObject->answers as $answer)
188            {           
189                $answerValue = new Literal($answer);
190                $predicateAnswer = new Resource(HAS_ANSWER);   
191                $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
192            }
193        }
194        $this->save();
195    }
196}
197
198?>
Note: See TracBrowser for help on using the repository browser.