[217] | 1 | <?php
|
---|
| 2 | // Survey database interface class as intermediate for storing data from the site to the RDF database
|
---|
| 3 | require_once 'rdfConstants.php';
|
---|
| 4 | // Include RAP Library to write RDF files
|
---|
| 5 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * Description of SessionInstance
|
---|
| 9 | *
|
---|
| 10 | * @author jkraaijeveld
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | class 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 true;
|
---|
| 82 | $newResultSets[] = $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 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
| 188 | }';
|
---|
| 189 | //Query the model
|
---|
| 190 | $results = $model->sparqlQuery($querystring);
|
---|
| 191 | $sessioninstances = array();
|
---|
| 192 | if(!empty($results))
|
---|
| 193 | {
|
---|
| 194 | foreach($results as $result)
|
---|
| 195 | {
|
---|
| 196 | //Create a SessionInstance object out of every result. Get all the required fields as well.
|
---|
| 197 | $notes = SessionInstance::getNotes($model, $result['?uid']->label);
|
---|
| 198 | $resultset = SessionInstance::getResultSet($model, $result['?uid']->label);
|
---|
| 199 | $starttime = new DateTime();
|
---|
| 200 | $starttime->setTimestamp(intval($result['?starttime']->label));
|
---|
| 201 | $endtime = new DateTime();
|
---|
| 202 | $endtime->setTimestamp(intval($result['?endtime']->label));
|
---|
| 203 | $sessioninstances[] = new SessionInstance(
|
---|
| 204 | $result['?uid']->label,
|
---|
| 205 | $result['?title']->label,
|
---|
| 206 | $result['?location']->label,
|
---|
| 207 | $result['?facilitator']->label,
|
---|
| 208 | $starttime,
|
---|
| 209 | $endtime,
|
---|
| 210 | $notes,
|
---|
| 211 | $result['?of_session'],
|
---|
| 212 | $resultset
|
---|
| 213 | );
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | return $sessioninstances;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
| 220 | * function getNotes()
|
---|
| 221 | * param type $uid : The SessionInstance uid for which the notes should be retrieved.
|
---|
| 222 | */
|
---|
| 223 | public static function getNotes($model, $uid)
|
---|
| 224 | {
|
---|
| 225 | $result = $model->findRegex("[(".$uid.")]", "[(has_note)]", null);
|
---|
| 226 | $iterator = $result->getStatementIterator();
|
---|
| 227 | $notes = array();
|
---|
| 228 | while($iterator->hasNext())
|
---|
| 229 | {
|
---|
| 230 | $element = $iterator->next();
|
---|
| 231 | $notes[] = $element->getLabelObject();
|
---|
| 232 | }
|
---|
| 233 | return $notes;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * function getResultSet($uid)
|
---|
| 238 | * param type $uid : The SessionInstance uid for which the ResultSet should be gotten.
|
---|
| 239 | */
|
---|
| 240 | public static function getResultSet($model, $uid)
|
---|
| 241 | {
|
---|
| 242 | //TODO: Implement when ResultSet is in.
|
---|
| 243 | return null;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | }
|
---|