source: Dev/trunk/classes/SurveyConnector.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.5 KB
Line 
1<?php
2
3/**
4 * Description of SurveyConnector
5 *
6 * @author jkraaijeveld
7 */
8class SurveyConnector extends Connector{
9    protected $db;
10
11    /**
12     * Constructor for ApplicationConnector.
13     */
14    public function __construct()
15    {
16                $this->fileName = 'data/surveys/surveys.rdf';
17        //Ensure the required folder for this connector exists
18        if (!is_dir('data/surveys/'))
19            mkdir('data/surveys/');     
20    }
21   
22    /**
23     * function get($arguments)
24     * Gets the array of Survey objects belonging to arguments supplied.
25     * @param type $arguments : An array containing zero or more of the following keys:
26     *                          'uid', 'title', 'description', 'questions'
27     * Note: questions has to be an array of question IDs (codes) for this filter to work.
28     */
29    public function get($arguments)
30    {
31        $this->load();
32       
33        //Build the query string
34        $querystring = '
35            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
36            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
37            SELECT DISTINCT ?uid, ?title, ?description, ?creator
38            WHERE
39            {
40                _survey     predicates:resource_type    resources:survey ;
41                predicates:uid ?uid ;
42                predicates:title ?title ;
43                                predicates:description ?description ;
44                                predicates:creator ?creator ;
45                                ' . $this->createArguments($arguments) . '
46            }';
47       
48        //Query the model
49        $results = $this->model->sparqlQuery($querystring);
50       
51        //Create the corresponding Survey objects
52        $surveys = array();
53        if(!empty($results))
54        {
55            $this->db = new DatabaseInterface();
56            foreach($results as $result)
57            {
58                                $questions = $this->getQuestions($result['?uid']->label);
59                                $creator = $this->db->get("user", array("uid" => $result['?creator']->label));
60                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions);
61            }
62        }
63        return $surveys;
64    }
65   
66    /**
67     * Gets the questions corresponding to the survey
68     * @param type $uid : The uid for which the questions should be gotten.
69     */
70    private function getQuestions($uid)
71    {
72                $result = $this->model->findRegex("[(".$uid.")]", "[(has_question)]", null);
73                $iterator = $result->getStatementIterator();
74                $questions = array();
75                while($iterator->hasNext())
76                {
77                        $element = $iterator->next();
78                        $result = $this->db->get("question", array("code" => $element->getLabelObject()));
79                        $questions[] = $result[0];
80                }
81                return $questions;
82    }
83   
84    /**
85     * Saves the given Survey object.
86     * @param type $rToolObject : The ResearchToolObject to be saved.
87     */
88    public function set($rToolObject)
89    {
90        $this->load();
91       
92        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
93        //Remove the old value stored with the given id
94        $this->model->subtract($this->model->find($resourceSurvey, null, null));
95       
96        $resourceSurveyType = new Resource(SURVEY);
97        $predicateRType = new Resource(RTYPE);
98        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
99
100        $predicateUniqueID = new Resource(UID);
101        $literalSurveyID = new Literal($rToolObject->uid);
102        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
103
104        $predicateTitle = new Resource(TITLE);         
105        $surveyTitle = new Literal($rToolObject->title);
106        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));         
107
108        $predicateDescription = new Resource(DESCRIPTION);
109        $surveyDescription = new Literal($rToolObject->description);
110        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
111       
112                $predicateCreator = new Resource(CREATOR);
113                $surveyCreator = new Literal($rToolObject->creator->uid);
114                $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
115
116        if(isset($rToolObject->questions))
117        {
118            foreach($rToolObject->questions as $question)
119            {
120                $questionUid = new Literal($question->uid);
121                $predicateQuid = new Resource(HAS_QUESTION);
122                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
123            }
124        }
125        $this->save();
126    }
127}
128
129?>
Note: See TracBrowser for help on using the repository browser.