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

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

Added missing classes for the database connection

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 = "?uid"; $title = "?title"; $description = "?description"; $creator = "?creator"; $questions = "";
58        if(in_array("uid", $keys))
59            $uid = '\''.$arguments["uid"].'\'';
60        if(in_array("title", $keys))
61            $title = '\''.$arguments["title"].'\'';
62        if(in_array("description", $keys))
63                        $description = "\"".$arguments["description"]."\"";
64                if(in_array("creator", $keys))
65                        $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:uid ' . $uid . '
87                predicates:title ' . $title . '
88                                predicates:description ' . $description . '
89                                predicates:creator ' . $creator . '
90                ' . $questions . '
91            }';
92       
93        //Query the model
94        $results = $this->model->sparqlQuery($querystring);
95       
96        //Create the corresponding Survey objects
97        $surveys = array();
98        if(!empty($results))
99        {
100            $this->db = new DatabaseInterface();
101            foreach($results as $result)
102            {
103                                $questions = $this->getQuestions($result['?uid']->label);
104                                $creator = $this->db->get("user", array("uid" => $result['?creator']->label));
105                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions);
106            }
107        }
108        return $surveys;
109    }
110   
111    /**
112     * Gets the questions corresponding to the survey
113     * @param type $uid
114     */
115    private function getQuestions($uid)
116    {
117        //Create the querystring. Note that the UID is the only argument in this case.
118        $querystring = '
119            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
120            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
121            SELECT DISTINCT ?has_question
122            WHERE
123            {
124                 _survey        predicates:resource_type        resources:survey ;
125                predicates:has_question ?has_question ;
126                predicates:uid \'' . $uid . '\'
127            }';
128
129        $results = $this->model->sparqlQuery($querystring);
130        $questions = array();
131        //For every question id, get the corresponding question
132        if(!empty($results))
133        {
134            foreach($results as $questionId)
135            {
136                $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label));
137                $questions[] = $resultQ[0];
138            }
139        }
140        return $questions;
141    }
142   
143    /**
144     * Saves the given Survey object.
145     * @param type $rToolObject
146     */
147    public function set($rToolObject)
148    {
149        $this->load();
150       
151        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
152        //Remove the old value stored with the given id
153        $this->model->subtract($this->model->find($resourceSurvey, null, null));
154       
155        $resourceSurveyType = new Resource(SURVEY);
156        $predicateRType = new Resource(RTYPE);
157        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
158
159        $predicateUniqueID = new Resource(UID);
160        $literalSurveyID = new Literal($rToolObject->uid);
161        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
162
163        $predicateTitle = new Resource(TITLE);         
164        $surveyTitle = new Literal($rToolObject->title);
165        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));         
166
167        $predicateDescription = new Resource(DESCRIPTION);
168        $surveyDescription = new Literal($rToolObject->description);
169        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
170       
171                $predicateCreator = new Resource(CREATOR);
172                $surveyCreator = new Literal($rToolObject->creator->uid);
173                $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
174
175        if(isset($rToolObject->questions))
176        {
177            foreach($rToolObject->questions as $question)
178            {
179                $questionUid = new Literal($question->uid);
180                $predicateQuid = new Resource(HAS_QUESTION);
181                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
182            }
183        }
184        $this->save();
185    }
186}
187
188?>
Note: See TracBrowser for help on using the repository browser.