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

Last change on this file since 136 was 131, checked in by jkraaijeveld, 14 years ago

Deleted previous database classes
Submitted (partial) new database connection

File size: 6.5 KB
RevLine 
[131]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";
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           
74        $querystring = '
75         PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
76            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
77            SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category
78            WHERE       
79            {
80                _question       predicates:resource_type        resources:question ;               
81                predicates:question_code ?uid ;
82                predicates:title ?title ;
83                predicates:question_type ?type ;
84                predicates:description ?description ;
85                predicates:question_category ?category ;
86
87                predicates:question_code ' . $uid . '
88                predicates:title ' . $title . '
89                predicates:question_type ' . $type . '
90                predicates:description ' . $description . '
91                predicates:question_category ' . $category . '
92            }';
93        //Create the querystring
94        $results = $this->model->sparqlQuery($querystring);
95       
96        $questions = array();
97        if(!empty($results))
98        {
99            foreach($results as $result)
100            {
101                $answers = $this->getAnswers($result['?uid']->label);
102                $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
103            }
104        }
105        return $questions;
106    }
107   
108    /**
109     * Gets the answers belonging to the given uid.
110     * @param type $uid
111     * @return type Array
112     */
113    private function getAnswers($uid)
114    {
115        $querystring = '
116            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
117            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
118            SELECT DISTINCT ?has_answer
119            WHERE
120            {
121                 _question      predicates:resource_type        resources:question ;
122                predicates:has_answer ?has_answer ;
123                predicates:question_code \'' . $uid . '\'
124            }';
125
126        $answers = $this->model->sparqlQuery($querystring);
127        $returnArray = array();
128        if(!empty($answers))
129        {
130            foreach($answers as $answer)
131            {
132                $returnArray[] = $answer['?has_answer']->label;
133            }
134        }
135        return $returnArray;
136    }
137
138   
139    /**
140     * Save the given ResearchTool object
141     * @param type $rToolObject
142     */
143    public function set($rToolObject)
144    {
145        $this->load();
146       
147        $resourceQuestion = new Resource(QUESTION.'/'.$rToolObject->uid);       
148        //Remove the old value stored with the given id
149        $this->model->subtract($this->model->find($resourceQuestion, null, null));
150       
151        //Save all the new values
152        $resourceQuestionType = new Resource(QUESTION);
153        $predicateRType = new Resource(RTYPE);
154        $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
155       
156        $questionQCode = new Literal($rToolObject->uid);
157        $predicateQCode = new Resource(QCODE);
158        $this->model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
159       
160        $questionTitle = new Literal($rToolObject->title);
161        $predicateTitle = new Resource(TITLE);         
162        $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));     
163
164        $questionDescription = new Literal($rToolObject->description);
165        $predicateDescription = new Resource(DESCRIPTION);
166        $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
167
168        $resourceQuestionType = new Literal($rToolObject->type);
169        $predicateQType = new Resource(QTYPE);
170        $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
171       
172        $questionQCategory = new Literal($rToolObject->category);
173        $predicateQCategory = new Resource(QCATEGORY);
174        $this->model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
175
176        if(isset($rToolObject->answers))
177        {
178            foreach($rToolObject->answers as $answer)
179            {           
180                $answerValue = new Literal($answer);
181                $predicateAnswer = new Resource(HAS_ANSWER);   
182                $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
183            }
184        }
185        $this->save();
186    }
187}
188
189?>
Note: See TracBrowser for help on using the repository browser.