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

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

Implemented lazy evaluation.

  • Initially, references to other database objects are now given as an ID rather than an object.
  • Every model class now has an evaluate() function, which gets all the objects that object has references to. Returns true if it got all the values correctly, false if there are invalid references.
  • Every connector now first evaluates an object before storing, to make sure only objects with valid references get stored.
File size: 4.6 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 = $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                        $questions[] = $element->getLabelObject();
79                }
80                return $questions;
81    }
82   
83    /**
84     * Saves the given Survey object.
85     * @param type $rToolObject : The ResearchToolObject to be saved.
86     */
87    public function set($rToolObject)
88    {
89        $this->load();
90       
91                //If evaluation fails, some references are incorrect.
92                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
93                //TODO: Decide how to fix invalid references graciously.
94                if(!$rToolObject->evaluate())
95                        return false;
96
97        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
98        //Remove the old value stored with the given id
99        $this->model->subtract($this->model->find($resourceSurvey, null, null));
100       
101        $resourceSurveyType = new Resource(SURVEY);
102        $predicateRType = new Resource(RTYPE);
103        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
104
105        $predicateUniqueID = new Resource(UID);
106        $literalSurveyID = new Literal($rToolObject->uid);
107        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
108
109        $predicateTitle = new Resource(TITLE);         
110        $surveyTitle = new Literal($rToolObject->title);
111        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));         
112
113        $predicateDescription = new Resource(DESCRIPTION);
114        $surveyDescription = new Literal($rToolObject->description);
115        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
116       
117                $predicateCreator = new Resource(CREATOR);
118                $surveyCreator = new Literal($rToolObject->creator->uid);
119                $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
120
121        if(isset($rToolObject->questions))
122        {
123            foreach($rToolObject->questions as $question)
124            {
125                $questionUid = new Literal($question->uid);
126                $predicateQuid = new Resource(HAS_QUESTION);
127                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
128            }
129                }
130                $this->save();
131                return true;
132    }
133}
134
135?>
Note: See TracBrowser for help on using the repository browser.