uid = $uid; $this->title = $title; $this->location = $location; $this->facilitator = $facilitator; $this->starttime = $starttime; $this->endtime = $endtime; $this->notes = $notes; $this->session = $session; $this->resultset = $resultset; } /* * function evaluate() * Evaluates all the references of this object. */ public function evaluate() { if(is_string($this->facilitator)) { $result = User::get(array("uid" => $this->facilitator)); if(!isset($result[0])) return false; $this->facilitator = $result[0]; } if(is_string($this->session)) { $result = Session::get(array("uid" => $this->session)); if(!isset($result[0])) return false; $this->session = $result[0]; } if(is_string($this->resultset)) { $result = ResultSet::get(array("uid" => $this->resultset)); if(!isset($result[0])) return true; $newResultSets[] = $result[0]; } return true; } /** * function save() * Saves the current object into the database. */ public function save() { //If evaluation fails, some references are incorrect. //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed. //TODO: Decide how to fix invalid references graciously. if(!$this->evaluate()) return false; //Ensure the required folder exists. if(!is_dir('data/sessions/')) mkdir('data/sessions/'); $model = ResearchToolObject::load(SessionInstance::$filename); $resourceSInstance = new Resource(SESSIONINSTANCE . '/' . $this->uid); //Remove the old value stored with the given id $model->subtract($model->find($resourceSInstance, null, null)); //Set the new values $resourceSInstanceType = new Resource(SESSIONINSTANCE); $predicateType = new Resource(RTYPE); $model->add(new Statement($resourceSInstance, $predicateType, $resourceSInstanceType)); $sInstanceId = new Literal($this->uid); $predicateId = new Resource(UID); $model->add(new Statement($resourceSInstance, $predicateId, $sInstanceId)); $sInstanceTitle = new Literal($this->title); $predicateTitle = new Resource(TITLE); $model->add(new Statement($resourceSInstance, $predicateTitle, $sInstanceTitle)); $sInstanceLocation = new Literal($this->location); $predicateLocation = new Resource(LOCATION); $model->add(new Statement($resourceSInstance, $predicateLocation, $sInstanceLocation)); $sInstanceFacilitator = new Literal($this->facilitator->uid); $predicateFacilitator = new Resource(FACILITATOR); $model->add(new Statement($resourceSInstance, $predicateFacilitator, $sInstanceFacilitator)); $sInstanceStartTime = new Literal($this->starttime->getTimestamp()); $predicateStartTime = new Resource(STARTTIME); $model->add(new Statement($resourceSInstance, $predicateStartTime, $sInstanceStartTime)); $sInstanceEndTime = new Literal($this->endtime->getTimestamp()); $predicateEndTime = new Resource(ENDTIME); $model->add(new Statement($resourceSInstance, $predicateEndTime, $sInstanceEndTime)); if(isset($this->notes)) { foreach($this->notes as $note) { $sInstanceNote = new Literal($note); $predicateNote = new Resource(HAS_NOTE); $model->add(new Statement($resourceSInstance, $predicateNote, $sInstanceNote)); } } $sInstanceSession = new Literal($this->session->uid); $predicateSession = new Resource(OF_SESSION); $model->add(new Statement($resourceSInstance, $predicateSession, $sInstanceSession)); if(isset($this->resultset)) { //$sInstanceResultSet = new Literal($this->resultset->uid); $sInstanceResultSet = new Literal($this->resultset); $predicateResultSet = new Resource(HAS_RESULTSET); $model->add(new Statement($resourceSInstance, $predicateResultSet, $sInstanceResultSet)); } $model->saveAs(SessionInstance::$filename, 'rdf'); return true; } /** * function get() * @param type $arguments : An array containing one or more of the following elements: * 'uid', 'title', 'location', 'facilitator', 'starttime', 'endtime' 'notes', 'session', 'resultset' */ public static function get($arguments) { $model = ResearchToolObject::load(SessionInstance::$filename); //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?location, ?facilitator, ?starttime, ?endtime, ?of_session, ?has_resultset WHERE { _sessioninstance predicates:resource_type resources:sessioninstance ; predicates:uid ?uid ; predicates:title ?title ; predicates:location ?location ; predicates:facilitator ?facilitator ; predicates:starttime ?starttime ; predicates:endtime ?endtime ; predicates:of_session ?of_session ; ' . ResearchToolObject::createArguments($arguments) . ' }'; //Query the model $results = $model->sparqlQuery($querystring); $sessioninstances = array(); if(!empty($results)) { foreach($results as $result) { //Create a SessionInstance object out of every result. Get all the required fields as well. $notes = SessionInstance::getNotes($model, $result['?uid']->label); $resultset = SessionInstance::getResultSet($model, $result['?uid']->label); $starttime = new DateTime(); $starttime->setTimestamp(intval($result['?starttime']->label)); $endtime = new DateTime(); $endtime->setTimestamp(intval($result['?endtime']->label)); $sessioninstances[] = new SessionInstance( $result['?uid']->label, $result['?title']->label, $result['?location']->label, $result['?facilitator']->label, $starttime, $endtime, $notes, $result['?of_session'], $resultset ); } } return $sessioninstances; } /** * function getNotes() * param type $uid : The SessionInstance uid for which the notes should be retrieved. */ public static function getNotes($model, $uid) { $result = $model->findRegex("[(".$uid.")]", "[(has_note)]", null); $iterator = $result->getStatementIterator(); $notes = array(); while($iterator->hasNext()) { $element = $iterator->next(); $notes[] = $element->getLabelObject(); } return $notes; } /** * function getResultSet($uid) * param type $uid : The SessionInstance uid for which the ResultSet should be gotten. */ public static function getResultSet($model, $uid) { //TODO: Implement when ResultSet is in. return null; } }