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

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

Now has Survey support

File size: 6.2 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"; $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("questions", $keys))
65        {
66            //Append the questions string to filter for questions properly
67            foreach ($arguments["questions"] as $questionUid)
68            {
69                $questions = $questions . 'predicates:has_question \'' . $questionUid . '\' ';
70            }
71        }
72       
73        //Build the query string
74        $querystring = '
75            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
76            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
77            SELECT DISTINCT ?uid, ?title, ?description
78            WHERE
79            {
80                _survey     predicates:resource_type    resources:survey ;
81                predicates:uid ?uid ;
82                predicates:title ?title ;
83                predicates:description ?description ;
84                predicates:uid ' . $uid . '
85                predicates:title ' . $title . '
86                predicates:description ' . $description . '
87                ' . $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                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $questions);
102            }
103        }
104        return $surveys;
105    }
106   
107    /**
108     * Gets the questions corresponding to the survey
109     * @param type $uid
110     */
111    private function getQuestions($uid)
112    {
113        //Create the querystring. Note that the UID is the only argument in this case.
114        $querystring = '
115            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
116            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
117            SELECT DISTINCT ?has_question
118            WHERE
119            {
120                 _survey        predicates:resource_type        resources:survey ;
121                predicates:has_question ?has_question ;
122                predicates:uid \'' . $uid . '\'
123            }';
124
125        $results = $this->model->sparqlQuery($querystring);
126        $questions = array();
127        //For every question id, get the corresponding question
128        if(!empty($results))
129        {
130            foreach($results as $questionId)
131            {
132                $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label));
133                $questions[] = $resultQ[0];
134            }
135        }
136        return $questions;
137    }
138   
139    /**
140     * Saves the given Survey object.
141     * @param type $rToolObject
142     */
143    public function set($rToolObject)
144    {
145        $this->load();
146       
147        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
148        //Remove the old value stored with the given id
149        $this->model->subtract($this->model->find($resourceSurvey, null, null));
150       
151        $resourceSurveyType = new Resource(SURVEY);
152        $predicateRType = new Resource(RTYPE);
153        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
154
155        $predicateUniqueID = new Resource(UID);
156        $literalSurveyID = new Literal($rToolObject->uid);
157        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
158
159        $predicateTitle = new Resource(TITLE);         
160        $surveyTitle = new Literal($rToolObject->title);
161        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));         
162
163        $predicateDescription = new Resource(DESCRIPTION);
164        $surveyDescription = new Literal($rToolObject->description);
165        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
166       
167        if(isset($rToolObject->questions))
168        {
169            foreach($rToolObject->questions as $question)
170            {
171                $questionUid = new Literal($question->uid);
172                $predicateQuid = new Resource(HAS_QUESTION);
173                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
174            }
175        }
176        $this->save();
177    }
178}
179
180?>
Note: See TracBrowser for help on using the repository browser.