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

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

In Session: -datetime has been renamed to creationdate

Connector has been adjusted accordingly

  • Removed answersets

Added SessionInstance?: This class is for unique occurences of a session.

In SessionInstance? is a reference to the currently unimplemented ResultSet?. This class will contain all answersets and gameresults given the SessionInstance?.

File size: 5.6 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', 'creationdate', 'applications', 'surveys'
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, ?creationdate
41                        WHERE
42                        {
43                                _session predicates:resource_type resources:session ;
44                                predicates:uid ?uid ;
45                                predicates:title ?title ;
46                                predicates:creator ?creator ;
47                                predicates:creationdate ?creationdate ;
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                                $creator = $result['?creator']->label;
61                                $datetime = new DateTime();
62                                $datetime->setTimestamp(intval($result['?creationdate']->label));
63                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $datetime, $pipeline);
64                        }
65                }
66                return $sessions;
67        }
68        /**
69         * function getPipeline()
70         * param type $uid : The Session uid for which the pipeline should be retrieved.
71         */
72        private function getPipeline($uid)
73        {
74                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
75                $iterator = $result->getStatementIterator();
76                $pipeline = array();
77                while($iterator->hasNext())
78                {
79                        $element = $iterator->next();
80                        $pipeline[] = $element->getLabelObject();
81                }
82                return $pipeline;
83        }
84
85        /**
86         * function getAnswerSets()
87         * @param type $uid : The Session uid for which the answerSets should be retrieved.
88         */
89        private function getAnswerSets($uid)
90        {
91                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
92                $iterator = $result->getStatementIterator();
93                $answersets = array();
94                while($iterator->hasNext())
95                {
96                        $element = $iterator->next();
97                        $answersets[] = $element->getLabelObject();
98                }
99                return $answersets;
100        }
101
102        /**
103         * function set()
104         * @param type $rToolObjects : The ResearchToolObject to be saved.
105         */
106        public function set($rToolObject)
107        {
108                $this->load();
109                //If evaluation fails, some references are incorrect.
110                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
111                //TODO: Decide how to fix invalid references graciously.
112                if(!$rToolObject->evaluate())
113                        return false;
114
115                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
116                //Remove the old value stored with the given id
117                $this->model->subtract($this->model->find($resourceSession, null, null));
118
119                //Set the new values
120                $resourceSessionType = new Resource(SESSION);
121                $predicateType = new Resource(RTYPE);
122                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
123
124                $sessionId = new Literal($rToolObject->uid);
125                $predicateId = new Resource(UID);
126                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
127
128                $sessionTitle = new Literal($rToolObject->title);
129                $predicateTitle = new Resource(TITLE);
130                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
131
132                $sessionCreator = new Literal($rToolObject->creator->uid);
133                $predicateCreator = new Resource(CREATOR);
134                $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
135
136                $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
137        //      $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
138        $predicateTimestamp = new Resource(DATETIME);
139                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
140
141                //Create a sequence to store the different applications and surveys.
142                if(isset($rToolObject->pipeline))
143                {
144                        foreach($rToolObject->pipeline as $element)
145                        {
146                                $sessionObject = new Literal($element->uid);
147                                $predicateObject = null;
148                                if(get_class($element) == "Application")
149                                        $predicateObject = new Resource(HAS_APPLICATION);
150                                else if(get_class($element) == "Survey")
151                                        $predicateObject = new Resource(HAS_SURVEY);
152                                if(isset($predicateObject))
153                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
154                        }
155                }
156
157                if(isset($rToolObject->answersets))
158                {
159                        foreach($rToolObject->answersets as $answerset)
160                        {
161                                $sessionAnswerSet = new Literal($answerset->uid);
162                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
163                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
164                        }
165                }
166
167                $this->save();
168                return true;
169        }
170}
171?>
172
Note: See TracBrowser for help on using the repository browser.