source: Dev/branches/jos-branch/classes/QuestionConnector.php @ 138

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

Now has Survey support

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