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

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

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File size: 4.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 extends Connector {
15   
16   
17    /**
18     * Constructor for QuestionConnector
19     */
20    public function __construct()
21    {
22                $this->fileName = 'data/questions/questions.rdf';
23        //Ensure the required folder for this connector exists
24        if (!is_dir('data/questions/'))
25            mkdir('data/questions/');   
26    }
27   
28    /**
29     * Get the questions corresponding to the arguments.
30         * @param type $arguments : An array having one ore more of the following elements:
31         * 'code', 'title', 'type', 'description', 'category', 'definedanswers'.
32     */
33    public function get($arguments)
34    {
35        $this->load();
36           
37        $querystring = '
38            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
39            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
40            SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category
41            WHERE       
42            {
43                _question       predicates:resource_type        resources:question ;               
44                predicates:question_code ?uid ;
45                predicates:title ?title ;
46                predicates:question_type ?type ;
47                predicates:description ?description ;
48                predicates:question_category ?category ;
49                                '. $this->createArguments($arguments) . '
50        }';
51
52               
53        //Create the querystring
54        $results = $this->model->sparqlQuery($querystring);
55
56       
57        $questions = array();
58        if(!empty($results))
59        {
60            foreach($results as $result)
61            {
62                $answers = $this->getAnswers($result['?uid']->label);
63                $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
64            }
65        }
66        return $questions;
67    }
68   
69    /**
70     * Gets the answers belonging to the given uid.
71     * @param type $uid : The uid of the Question in question (haha, pun).
72     */
73    private function getAnswers($uid)
74    {
75                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answer)]", null);
76                $iterator = $result->getStatementIterator();
77                $answers = array();
78                while($iterator->hasNext())
79                {
80                        $element = $iterator->next();
81                        $answers[] = $element->getLabelObject();
82                }
83                return $answers;
84
85        }
86   
87    /**
88     * Save the given ResearchTool object
89     * @param type $rToolObject : The ResearchToolObject to be saved.
90     */
91    public function set($rToolObject)
92    {
93        $this->load();
94       
95        $resourceQuestion = new Resource(QUESTION.'/'.$rToolObject->uid);       
96        //Remove the old value stored with the given id
97        $this->model->subtract($this->model->find($resourceQuestion, null, null));
98       
99        //Save all the new values
100        $resourceQuestionType = new Resource(QUESTION);
101        $predicateRType = new Resource(RTYPE);
102        $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
103       
104        $questionQCode = new Literal($rToolObject->uid);
105        $predicateQCode = new Resource(QCODE);
106        $this->model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
107       
108        $questionTitle = new Literal($rToolObject->title);
109        $predicateTitle = new Resource(TITLE);         
110        $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));     
111
112        $questionDescription = new Literal($rToolObject->description);
113        $predicateDescription = new Resource(DESCRIPTION);
114        $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
115
116        $resourceQuestionType = new Literal($rToolObject->type);
117        $predicateQType = new Resource(QTYPE);
118        $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
119       
120        $questionQCategory = new Literal($rToolObject->category);
121        $predicateQCategory = new Resource(QCATEGORY);
122        $this->model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
123
124        if(isset($rToolObject->answers))
125        {
126            foreach($rToolObject->answers as $answer)
127            {           
128                $answerValue = new Literal($answer);
129                $predicateAnswer = new Resource(HAS_ANSWER);   
130                $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
131            }
132        }
133        $this->save();
134    }
135}
136
137?>
Note: See TracBrowser for help on using the repository browser.