source: Dev/trunk/classes/SessionConnector.php @ 188

Last change on this file since 188 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: 5.7 KB
Line 
1<?php
2// Survey database interface class as intermediate for storing data from the site to the RDF database
3require_once 'rdfConstants.php';
4// Include RAP Library to write RDF files
5include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
6
7/**
8 * Description of SessionConnector
9 *
10 * @author jkraaijeveld
11 */
12class SessionConnector extends Connector 
13{
14        protected $db;
15
16        /**
17         * Constructor for the SessionConnector
18         */
19        public function __construct()
20        {
21                $this->fileName = 'data/sessions/sessions.rdf';
22                //Ensure the required folder for this connector exists
23                if(!is_dir('data/sessions'))
24                        mkdir('data/sessions');
25        }
26
27        /**
28         * function get()
29         * @param type $arguments : An array containing one or more of the following elements:
30         * 'uid', 'title', 'datetime', 'applications', 'surveys', 'answersets'
31         */
32        public function get($arguments)
33        {
34                $this->load();
35       
36                //Build the query string
37                $querystring = '
38                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
39                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
40                        SELECT DISTINCT ?uid, ?title, ?creator, ?datetime
41                        WHERE
42                        {
43                                _session predicates:resource_type resources:session ;
44                                predicates:uid ?uid ;
45                                predicates:title ?title ;
46                                predicates:creator ?creator ;
47                                predicates:datetime ?datetime ;
48                                ' . $this->createArguments($arguments) . '
49                        }';
50                //Query the model
51                $results = $this->model->sparqlQuery($querystring);
52                $sessions = array();
53                if(!empty($results))
54                {
55                        $this->db = new DatabaseInterface();
56                        foreach($results as $result)
57                        {
58                                //Create a session object out of every result, get all required fields as well.
59                                $pipeline = $this->getPipeline($result['?uid']->label);
60                                $answersets = $this->getAnswerSets($result['?uid']->label);
61                                $creator = $result['?creator']->label;
62                                $datetime = new DateTime();
63                                $datetime->setTimestamp(intval($result['?datetime']->label));
64                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $datetime, $pipeline, $answersets);
65                        }
66                }
67                return $sessions;
68        }
69        /**
70         * function getPipeline()
71         * param type $uid : The Session uid for which the pipeline should be retrieved.
72         */
73        private function getPipeline($uid)
74        {
75                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
76                $iterator = $result->getStatementIterator();
77                $pipeline = array();
78                while($iterator->hasNext())
79                {
80                        $element = $iterator->next();
81                        $pipeline[] = $element->getLabelObject();
82                }
83                return $pipeline;
84        }
85
86        /**
87         * function getAnswerSets()
88         * @param type $uid : The Session uid for which the answerSets should be retrieved.
89         */
90        private function getAnswerSets($uid)
91        {
92                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
93                $iterator = $result->getStatementIterator();
94                $answersets = array();
95                while($iterator->hasNext())
96                {
97                        $element = $iterator->next();
98                        $answersets[] = $element->getLabelObject();
99                }
100                return $answersets;
101        }
102
103        /**
104         * function set()
105         * @param type $rToolObjects : The ResearchToolObject to be saved.
106         */
107        public function set($rToolObject)
108        {
109                $this->load();
110                //If evaluation fails, some references are incorrect.
111                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
112                //TODO: Decide how to fix invalid references graciously.
113                if(!$rToolObject->evaluate())
114                        return false;
115
116                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
117                //Remove the old value stored with the given id
118                $this->model->subtract($this->model->find($resourceSession, null, null));
119
120                //Set the new values
121                $resourceSessionType = new Resource(SESSION);
122                $predicateType = new Resource(RTYPE);
123                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
124
125                $sessionId = new Literal($rToolObject->uid);
126                $predicateId = new Resource(UID);
127                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
128
129                $sessionTitle = new Literal($rToolObject->title);
130                $predicateTitle = new Resource(TITLE);
131                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
132
133                $sessionCreator = new Literal($rToolObject->creator->uid);
134                $predicateCreator = new Resource(CREATOR);
135                $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
136
137                $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
138        //      $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
139        $predicateTimestamp = new Resource(DATETIME);
140                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
141
142                //Create a sequence to store the different applications and surveys.
143                if(isset($rToolObject->pipeline))
144                {
145                        foreach($rToolObject->pipeline as $element)
146                        {
147                                $sessionObject = new Literal($element->uid);
148                                $predicateObject = null;
149                                if(get_class($element) == "Application")
150                                        $predicateObject = new Resource(HAS_APPLICATION);
151                                else if(get_class($element) == "Survey")
152                                        $predicateObject = new Resource(HAS_SURVEY);
153                                if(isset($predicateObject))
154                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
155                        }
156                }
157
158                if(isset($rToolObject->answersets))
159                {
160                        foreach($rToolObject->answersets as $answerset)
161                        {
162                                $sessionAnswerSet = new Literal($answerset->uid);
163                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
164                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
165                        }
166                }
167
168                $this->save();
169                return true;
170        }
171}
172?>
173
Note: See TracBrowser for help on using the repository browser.