[149] | 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 SessionConnector
|
---|
| 9 | *
|
---|
| 10 | * @author jkraaijeveld
|
---|
| 11 | */
|
---|
[171] | 12 | class SessionConnector extends Connector
|
---|
[149] | 13 | {
|
---|
| 14 | protected $db;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Constructor for the SessionConnector
|
---|
| 18 | */
|
---|
| 19 | public function __construct()
|
---|
| 20 | {
|
---|
[171] | 21 | $this->fileName = 'data/sessions/sessions.rdf';
|
---|
[149] | 22 | //Ensure the required folder for this connector exists
|
---|
| 23 | if(!is_dir('data/sessions'))
|
---|
| 24 | mkdir('data/sessions');
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
[158] | 28 | * function get()
|
---|
| 29 | * @param type $arguments : An array containing one or more of the following elements:
|
---|
| 30 | * 'uid', 'title', 'datetime', 'applications', 'surveys', 'answersets'
|
---|
[149] | 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 . '>
|
---|
[159] | 40 | SELECT DISTINCT ?uid, ?title, ?creator, ?datetime
|
---|
[149] | 41 | WHERE
|
---|
| 42 | {
|
---|
| 43 | _session predicates:resource_type resources:session ;
|
---|
| 44 | predicates:uid ?uid ;
|
---|
| 45 | predicates:title ?title ;
|
---|
[159] | 46 | predicates:creator ?creator ;
|
---|
| 47 | predicates:datetime ?datetime ;
|
---|
[171] | 48 | ' . $this->createArguments($arguments) . '
|
---|
[149] | 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);
|
---|
[159] | 61 | $creator = $this->db->get("User", array("uid" => $result['?creator']->label));
|
---|
[149] | 62 | $datetime = new DateTime();
|
---|
[159] | 63 | $datetime->setTimestamp(intval($result['?datetime']->label));
|
---|
[170] | 64 | $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
|
---|
[149] | 65 | }
|
---|
| 66 | }
|
---|
| 67 | return $sessions;
|
---|
| 68 | }
|
---|
| 69 | /**
|
---|
[158] | 70 | * function getPipeline()
|
---|
| 71 | * param type $uid : The Session uid for which the pipeline should be retrieved.
|
---|
[149] | 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 | if(strpos($element->getLabelPredicate(), "has_application") != false)
|
---|
| 82 | {
|
---|
| 83 | $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
|
---|
| 84 | $pipeline[] = $applications[0];
|
---|
| 85 | }
|
---|
| 86 | else
|
---|
| 87 | {
|
---|
| 88 | $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
|
---|
| 89 | $pipeline[] = $surveys[0];
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return $pipeline;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
[158] | 96 | * function getAnswerSets()
|
---|
| 97 | * @param type $uid : The Session uid for which the answerSets should be retrieved.
|
---|
[149] | 98 | */
|
---|
| 99 | private function getAnswerSets($uid)
|
---|
| 100 | {
|
---|
| 101 | $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
|
---|
| 102 | $iterator = $result->getStatementIterator();
|
---|
| 103 | $answersets = array();
|
---|
| 104 | while($iterator->hasNext())
|
---|
| 105 | {
|
---|
| 106 | $element = $iterator->next();
|
---|
| 107 | $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
|
---|
| 108 | $answersets[] = $resultsets[0];
|
---|
| 109 | }
|
---|
| 110 | return $answersets;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
[158] | 114 | * function set()
|
---|
| 115 | * @param type $rToolObjects : The ResearchToolObject to be saved.
|
---|
[149] | 116 | */
|
---|
| 117 | public function set($rToolObject)
|
---|
| 118 | {
|
---|
| 119 | $this->load();
|
---|
| 120 | $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
|
---|
| 121 | //Remove the old value stored with the given id
|
---|
| 122 | $this->model->subtract($this->model->find($resourceSession, null, null));
|
---|
| 123 |
|
---|
| 124 | //Set the new values
|
---|
| 125 | $resourceSessionType = new Resource(SESSION);
|
---|
| 126 | $predicateType = new Resource(RTYPE);
|
---|
| 127 | $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
|
---|
| 128 |
|
---|
| 129 | $sessionId = new Literal($rToolObject->uid);
|
---|
| 130 | $predicateId = new Resource(UID);
|
---|
| 131 | $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
|
---|
| 132 |
|
---|
| 133 | $sessionTitle = new Literal($rToolObject->title);
|
---|
| 134 | $predicateTitle = new Resource(TITLE);
|
---|
| 135 | $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
|
---|
| 136 |
|
---|
[159] | 137 | $sessionCreator = new Literal($rToolObject->creator->uid);
|
---|
| 138 | $predicateCreator = new Resource(CREATOR);
|
---|
| 139 | $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
|
---|
| 140 |
|
---|
[157] | 141 | $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
|
---|
| 142 | // $sessionTimestamp = new Literal($rToolObject->datetime); // Edit of above function, allows for creation of session, but still results in errors...
|
---|
[158] | 143 | $predicateTimestamp = new Resource(DATETIME);
|
---|
[149] | 144 | $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
|
---|
| 145 |
|
---|
| 146 | //Create a sequence to store the different applications and surveys.
|
---|
| 147 | if(isset($rToolObject->pipeline))
|
---|
| 148 | {
|
---|
| 149 | foreach($rToolObject->pipeline as $element)
|
---|
| 150 | {
|
---|
| 151 | $sessionObject = new Literal($element->uid);
|
---|
| 152 | $predicateObject = null;
|
---|
| 153 | if(get_class($element) == "Application")
|
---|
| 154 | $predicateObject = new Resource(HAS_APPLICATION);
|
---|
| 155 | else if(get_class($element) == "Survey")
|
---|
| 156 | $predicateObject = new Resource(HAS_SURVEY);
|
---|
| 157 | if(isset($predicateObject))
|
---|
| 158 | $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if(isset($rToolObject->answersets))
|
---|
| 163 | {
|
---|
| 164 | foreach($rToolObject->answersets as $answerset)
|
---|
| 165 | {
|
---|
| 166 | $sessionAnswerSet = new Literal($answerset->uid);
|
---|
| 167 | $predicateAnswerSet = new Resource(HAS_ANSWERSET);
|
---|
| 168 | $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | $this->save();
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | ?>
|
---|
| 176 |
|
---|