[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 | }';
|
---|
[171] | 50 | echo $querystring;
|
---|
[149] | 51 | //Query the model
|
---|
| 52 | $results = $this->model->sparqlQuery($querystring);
|
---|
| 53 | $sessions = array();
|
---|
| 54 | if(!empty($results))
|
---|
| 55 | {
|
---|
| 56 | $this->db = new DatabaseInterface();
|
---|
| 57 | foreach($results as $result)
|
---|
| 58 | {
|
---|
| 59 | //Create a session object out of every result, get all required fields as well.
|
---|
| 60 | $pipeline = $this->getPipeline($result['?uid']->label);
|
---|
| 61 | $answersets = $this->getAnswerSets($result['?uid']->label);
|
---|
[159] | 62 | $creator = $this->db->get("User", array("uid" => $result['?creator']->label));
|
---|
[149] | 63 | $datetime = new DateTime();
|
---|
[159] | 64 | $datetime->setTimestamp(intval($result['?datetime']->label));
|
---|
[170] | 65 | $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
|
---|
[149] | 66 | }
|
---|
| 67 | }
|
---|
| 68 | return $sessions;
|
---|
| 69 | }
|
---|
| 70 | /**
|
---|
[158] | 71 | * function getPipeline()
|
---|
| 72 | * param type $uid : The Session uid for which the pipeline should be retrieved.
|
---|
[149] | 73 | */
|
---|
| 74 | private function getPipeline($uid)
|
---|
| 75 | {
|
---|
| 76 | $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
|
---|
| 77 | $iterator = $result->getStatementIterator();
|
---|
| 78 | $pipeline = array();
|
---|
| 79 | while($iterator->hasNext())
|
---|
| 80 | {
|
---|
| 81 | $element = $iterator->next();
|
---|
| 82 | if(strpos($element->getLabelPredicate(), "has_application") != false)
|
---|
| 83 | {
|
---|
| 84 | $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
|
---|
| 85 | $pipeline[] = $applications[0];
|
---|
| 86 | }
|
---|
| 87 | else
|
---|
| 88 | {
|
---|
| 89 | $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
|
---|
| 90 | $pipeline[] = $surveys[0];
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | return $pipeline;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
[158] | 97 | * function getAnswerSets()
|
---|
| 98 | * @param type $uid : The Session uid for which the answerSets should be retrieved.
|
---|
[149] | 99 | */
|
---|
| 100 | private function getAnswerSets($uid)
|
---|
| 101 | {
|
---|
| 102 | $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
|
---|
| 103 | $iterator = $result->getStatementIterator();
|
---|
| 104 | $answersets = array();
|
---|
| 105 | while($iterator->hasNext())
|
---|
| 106 | {
|
---|
| 107 | $element = $iterator->next();
|
---|
| 108 | $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
|
---|
| 109 | $answersets[] = $resultsets[0];
|
---|
| 110 | }
|
---|
| 111 | return $answersets;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
[158] | 115 | * function set()
|
---|
| 116 | * @param type $rToolObjects : The ResearchToolObject to be saved.
|
---|
[149] | 117 | */
|
---|
| 118 | public function set($rToolObject)
|
---|
| 119 | {
|
---|
| 120 | $this->load();
|
---|
| 121 | $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
|
---|
| 122 | //Remove the old value stored with the given id
|
---|
| 123 | $this->model->subtract($this->model->find($resourceSession, null, null));
|
---|
| 124 |
|
---|
| 125 | //Set the new values
|
---|
| 126 | $resourceSessionType = new Resource(SESSION);
|
---|
| 127 | $predicateType = new Resource(RTYPE);
|
---|
| 128 | $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
|
---|
| 129 |
|
---|
| 130 | $sessionId = new Literal($rToolObject->uid);
|
---|
| 131 | $predicateId = new Resource(UID);
|
---|
| 132 | $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
|
---|
| 133 |
|
---|
| 134 | $sessionTitle = new Literal($rToolObject->title);
|
---|
| 135 | $predicateTitle = new Resource(TITLE);
|
---|
| 136 | $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
|
---|
| 137 |
|
---|
[159] | 138 | $sessionCreator = new Literal($rToolObject->creator->uid);
|
---|
| 139 | $predicateCreator = new Resource(CREATOR);
|
---|
| 140 | $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
|
---|
| 141 |
|
---|
[157] | 142 | $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
|
---|
| 143 | // $sessionTimestamp = new Literal($rToolObject->datetime); // Edit of above function, allows for creation of session, but still results in errors...
|
---|
[158] | 144 | $predicateTimestamp = new Resource(DATETIME);
|
---|
[149] | 145 | $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
|
---|
| 146 |
|
---|
| 147 | //Create a sequence to store the different applications and surveys.
|
---|
| 148 | if(isset($rToolObject->pipeline))
|
---|
| 149 | {
|
---|
| 150 | foreach($rToolObject->pipeline as $element)
|
---|
| 151 | {
|
---|
| 152 | $sessionObject = new Literal($element->uid);
|
---|
| 153 | $predicateObject = null;
|
---|
| 154 | if(get_class($element) == "Application")
|
---|
| 155 | $predicateObject = new Resource(HAS_APPLICATION);
|
---|
| 156 | else if(get_class($element) == "Survey")
|
---|
| 157 | $predicateObject = new Resource(HAS_SURVEY);
|
---|
| 158 | if(isset($predicateObject))
|
---|
| 159 | $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if(isset($rToolObject->answersets))
|
---|
| 164 | {
|
---|
| 165 | foreach($rToolObject->answersets as $answerset)
|
---|
| 166 | {
|
---|
| 167 | $sessionAnswerSet = new Literal($answerset->uid);
|
---|
| 168 | $predicateAnswerSet = new Resource(HAS_ANSWERSET);
|
---|
| 169 | $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | $this->save();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | ?>
|
---|
| 177 |
|
---|