source: Dev/branches/jQueryUI/server/classes/models/SessionInstance.php @ 249

Last change on this file since 249 was 249, checked in by hendrikvanantwerpen, 13 years ago

This one's for Subversion, because it's so close...

First widget (stripped down sequencer).
Seperated client and server code in two direcotry trees.

File size: 7.5 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 SessionInstance
9 *
10 * @author jkraaijeveld
11 */
12
13class SessionInstance extends ResearchToolObject
14{
15        public static $filename = 'data/sessions/sessioninstances.rdf';
16
17        public $title;
18        public $location;
19        public $facilitator;
20        public $starttime;
21        public $endtime;
22        public $notes;
23        public $session;
24        public $resultset;
25
26        /**
27         * Constructor for a SessionInstance object
28         * @param type $uid
29         * @param type $title
30         * @param type $location
31         * @param type $facilitator
32         * @param type $starttime
33         * @param type $endtime
34         * @param type $notes
35         * @param type $session
36         * @param type $resultset
37         */
38        public function __construct($uid = null, $title = null, $location = null, $facilitator = null, $starttime = null, $endtime = null, $notes = null, $session = null, $resultset = null)
39        {
40                if(!isset($uid))
41                {
42                        $uid = md5(uniqid(rand(), true));
43                }
44                $this->uid = $uid;
45                $this->title = $title;
46                $this->location = $location;
47                $this->facilitator = $facilitator;
48                $this->starttime = $starttime;
49                $this->endtime = $endtime;
50                $this->notes = $notes;
51                $this->session = $session;
52                $this->resultset = $resultset;
53        }
54
55        /*
56         * function evaluate()
57         * Evaluates all the references of this object.
58         */
59        public function evaluate()
60        {
61                if(is_string($this->facilitator))
62                {
63                        $result = User::get(array("uid" => $this->facilitator));
64                        if(!isset($result[0]))
65                                return false;
66                        $this->facilitator = $result[0];
67                }
68
69                if(is_string($this->session))
70                {
71                        $result = Session::get(array("uid" => $this->session));
72                        if(!isset($result[0]))
73                                return false;
74                        $this->session = $result[0];
75                }
76
77                if(is_string($this->resultset))
78                {
79                        $result = ResultSet::get(array("uid" => $this->resultset));
80                        if(!isset($result[0]))
81                                return false;
82                        $this->resultset = $result[0];
83                }
84                return true;
85        }
86
87        /**
88         * function save()
89         * Saves the current object into the database.
90         */
91        public function save()
92        {
93                //If evaluation fails, some references are incorrect.
94                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
95                //TODO: Decide how to fix invalid references graciously.
96                if(!$this->evaluate())
97                        return false;
98
99                //Ensure the required folder exists.
100                if(!is_dir('data/sessions/'))
101                        mkdir('data/sessions/');
102
103                $model = ResearchToolObject::load(SessionInstance::$filename);
104
105                $resourceSInstance = new Resource(SESSIONINSTANCE . '/' . $this->uid);
106                //Remove the old value stored with the given id
107                $model->subtract($model->find($resourceSInstance, null, null));
108
109                //Set the new values
110                $resourceSInstanceType = new Resource(SESSIONINSTANCE);
111                $predicateType = new Resource(RTYPE);
112                $model->add(new Statement($resourceSInstance, $predicateType, $resourceSInstanceType));
113
114                $sInstanceId = new Literal($this->uid);
115                $predicateId = new Resource(UID);
116                $model->add(new Statement($resourceSInstance, $predicateId, $sInstanceId));
117
118                $sInstanceTitle = new Literal($this->title);
119                $predicateTitle = new Resource(TITLE);
120                $model->add(new Statement($resourceSInstance, $predicateTitle, $sInstanceTitle));
121
122                $sInstanceLocation = new Literal($this->location);
123                $predicateLocation = new Resource(LOCATION);
124                $model->add(new Statement($resourceSInstance, $predicateLocation, $sInstanceLocation));
125
126                $sInstanceFacilitator = new Literal($this->facilitator->uid);
127                $predicateFacilitator = new Resource(FACILITATOR);
128                $model->add(new Statement($resourceSInstance, $predicateFacilitator, $sInstanceFacilitator));
129
130                $sInstanceStartTime = new Literal($this->starttime->getTimestamp());
131                $predicateStartTime = new Resource(STARTTIME);
132                $model->add(new Statement($resourceSInstance, $predicateStartTime, $sInstanceStartTime));
133
134                $sInstanceEndTime = new Literal($this->endtime->getTimestamp());
135                $predicateEndTime = new Resource(ENDTIME);
136                $model->add(new Statement($resourceSInstance, $predicateEndTime, $sInstanceEndTime));
137
138                if(isset($this->notes))
139                {
140                        foreach($this->notes as $note)
141                        {
142                                $sInstanceNote = new Literal($note);
143                                $predicateNote = new Resource(HAS_NOTE);
144                                $model->add(new Statement($resourceSInstance, $predicateNote, $sInstanceNote));
145                        }
146                }
147
148                $sInstanceSession = new Literal($this->session->uid);
149                $predicateSession = new Resource(OF_SESSION);
150                $model->add(new Statement($resourceSInstance, $predicateSession, $sInstanceSession));
151
152                if(isset($this->resultset))
153                {
154                        //$sInstanceResultSet = new Literal($this->resultset->uid);
155                        $sInstanceResultSet = new Literal($this->resultset);
156                        $predicateResultSet = new Resource(HAS_RESULTSET);
157                        $model->add(new Statement($resourceSInstance, $predicateResultSet, $sInstanceResultSet));
158                }
159                $model->saveAs(SessionInstance::$filename, 'rdf');
160                return true;
161        }
162
163        /**
164         * function get()
165         * @param type $arguments : An array containing one or more of the following elements:
166         * 'uid', 'title', 'location', 'facilitator', 'starttime', 'endtime' 'notes', 'session', 'resultset'
167         */
168        public static function get($arguments)
169        {
170                $model = ResearchToolObject::load(SessionInstance::$filename);
171
172                //Build the query string
173                $querystring = '
174                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
175                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
176                        SELECT DISTINCT ?uid, ?title, ?location, ?facilitator, ?starttime, ?endtime, ?of_session, ?has_resultset
177                        WHERE
178                        {
179                                _sessioninstance predicates:resource_type resources:sessioninstance ;
180                                predicates:uid ?uid ;
181                                predicates:title ?title ;
182                                predicates:location ?location ;
183                                predicates:facilitator ?facilitator ;
184                                predicates:starttime ?starttime ;
185                                predicates:endtime ?endtime ;
186                                predicates:of_session ?of_session ;
187                                predicates:has_resultset ?has_resultset ;
188                                ' . ResearchToolObject::createArguments($arguments) . '
189        }';
190                //Query the model
191                $results = $model->sparqlQuery($querystring);
192                $sessioninstances = array();
193                if(!empty($results))
194                {       
195                        foreach($results as $result)
196                        {
197                                //Create a SessionInstance object out of every result. Get all the required fields as well.
198                                $notes = SessionInstance::getNotes($model, $result['?uid']->label);
199                                $resultset = isset($result['?has_resultset']) ? $result['?has_resultset']->label : null;
200                                $starttime = new DateTime();
201                                $starttime->setTimestamp(intval($result['?starttime']->label));
202                                $endtime = new DateTime();
203                                $endtime->setTimestamp(intval($result['?endtime']->label));
204                                $sessioninstances[] = new SessionInstance( 
205                                        $result['?uid']->label,
206                                        $result['?title']->label,
207                                        $result['?location']->label,
208                                        $result['?facilitator']->label,
209                                        $starttime,
210                                        $endtime,
211                                        $notes,
212                                        $result['?of_session'],
213                                        $resultset
214                                );
215                        }
216                }
217                return $sessioninstances;
218        }
219       
220        /**
221         * function getNotes()
222         * param type $uid : The SessionInstance uid for which the notes should be retrieved.
223         */
224        public static function getNotes($model, $uid)
225        {
226                $result = $model->findRegex("[(".$uid.")]", "[(has_note)]", null);
227                $iterator = $result->getStatementIterator();
228                $notes = array();
229                while($iterator->hasNext())
230                {
231                        $element = $iterator->next();
232                        $notes[] = $element->getLabelObject();
233                }
234                return $notes;
235        }
236
237
238}
Note: See TracBrowser for help on using the repository browser.