source: Dev/trunk/classes/SurveyConnector.php @ 158

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

Refactored queries / cleaned up some code.

File size: 6.6 KB
Line 
1<?php
2
3/**
4 * Description of SurveyConnector
5 *
6 * @author jkraaijeveld
7 */
8class SurveyConnector implements IConnector{
9    protected $model;
10    protected $fileName = 'data/surveys/surveys.rdf';
11    protected $db;
12
13    /**
14     * Constructor for ApplicationConnector.
15     */
16    public function __construct()
17    {
18        //Ensure the required folder for this connector exists
19        if (!is_dir('data/surveys/'))
20            mkdir('data/surveys/');     
21    }
22   
23    /**
24     * function load()
25     * Loads the file into the standard MemModel.
26     */
27    public function load() {
28        //Get the Memory Model from the ModelFactory
29        $this->model = ModelFactory::getDefaultModel();
30       
31        //Ensure the required file exists before loading
32        if(file_exists($this->fileName))
33            $this->model->load($this->fileName);
34    }
35   
36    /**
37     * function save()
38     * Saves the MemModel into the given file.
39     */
40    public function save() {
41        $this->model->saveAs($this->fileName,'rdf');
42    }
43   
44    /**
45     * function get($arguments)
46     * Gets the array of Survey objects belonging to arguments supplied.
47     * @param type $arguments : An array containing zero or more of the following keys:
48     *                          'uid', 'title', 'description', 'questions'
49     * Note: questions has to be an array of question IDs (codes) for this filter to work.
50     */
51    public function get($arguments)
52    {
53        $this->load();
54       
55        $keys = array_keys($arguments);
56        //Set default values for arguments
57        $uid = ""; $title = ""; $description = ""; $creator = ""; $questions = "";
58        if(in_array("uid", $keys))
59            $uid = 'predicates:uid \''.$arguments["uid"].'\'';
60        if(in_array("title", $keys))
61            $title = 'predicates:title \''.$arguments["title"].'\'';
62        if(in_array("description", $keys))
63                        $description = "predicates:description \"".$arguments["description"]."\"";
64                if(in_array("creator", $keys))
65                        $creator = "predicates:creator \"".$arguments["creator"]."\"";
66        if(in_array("questions", $keys))
67        {
68            //Append the questions string to filter for questions properly
69            foreach ($arguments["questions"] as $questionUid)
70            {
71                $questions = $questions . 'predicates:has_question \'' . $questionUid . '\' ';
72            }
73        }
74       
75        //Build the query string
76        $querystring = '
77            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
78            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
79            SELECT DISTINCT ?uid, ?title, ?description, ?creator
80            WHERE
81            {
82                _survey     predicates:resource_type    resources:survey ;
83                predicates:uid ?uid ;
84                predicates:title ?title ;
85                                predicates:description ?description ;
86                                predicates:creator ?creator ;
87                                ' . $uid . $title . $description . $creator . $questions . '
88            }';
89       
90        //Query the model
91        $results = $this->model->sparqlQuery($querystring);
92       
93        //Create the corresponding Survey objects
94        $surveys = array();
95        if(!empty($results))
96        {
97            $this->db = new DatabaseInterface();
98            foreach($results as $result)
99            {
100                                $questions = $this->getQuestions($result['?uid']->label);
101                                $creator = $this->db->get("user", array("uid" => $result['?creator']->label));
102                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions);
103            }
104        }
105        return $surveys;
106    }
107   
108    /**
109     * Gets the questions corresponding to the survey
110     * @param type $uid : The uid for which the questions should be gotten.
111     */
112    private function getQuestions($uid)
113    {
114        //Create the querystring. Note that the UID is the only argument in this case.
115        $querystring = '
116            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
117            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
118            SELECT DISTINCT ?has_question
119            WHERE
120            {
121                 _survey        predicates:resource_type        resources:survey ;
122                predicates:has_question ?has_question ;
123                predicates:uid \'' . $uid . '\'
124            }';
125
126        $results = $this->model->sparqlQuery($querystring);
127        $questions = array();
128        //For every question id, get the corresponding question
129        if(!empty($results))
130        {
131            foreach($results as $questionId)
132            {
133                $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label));
134                $questions[] = $resultQ[0];
135            }
136        }
137        return $questions;
138    }
139   
140    /**
141     * Saves the given Survey object.
142     * @param type $rToolObject : The ResearchToolObject to be saved.
143     */
144    public function set($rToolObject)
145    {
146        $this->load();
147       
148        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
149        //Remove the old value stored with the given id
150        $this->model->subtract($this->model->find($resourceSurvey, null, null));
151       
152        $resourceSurveyType = new Resource(SURVEY);
153        $predicateRType = new Resource(RTYPE);
154        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
155
156        $predicateUniqueID = new Resource(UID);
157        $literalSurveyID = new Literal($rToolObject->uid);
158        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
159
160        $predicateTitle = new Resource(TITLE);         
161        $surveyTitle = new Literal($rToolObject->title);
162        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));         
163
164        $predicateDescription = new Resource(DESCRIPTION);
165        $surveyDescription = new Literal($rToolObject->description);
166        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
167       
168                $predicateCreator = new Resource(CREATOR);
169                $surveyCreator = new Literal($rToolObject->creator->uid);
170                $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
171
172        if(isset($rToolObject->questions))
173        {
174            foreach($rToolObject->questions as $question)
175            {
176                $questionUid = new Literal($question->uid);
177                $predicateQuid = new Resource(HAS_QUESTION);
178                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
179            }
180        }
181        $this->save();
182    }
183}
184
185?>
Note: See TracBrowser for help on using the repository browser.