[149] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Description of AnswerSetConnector
|
---|
| 5 | *
|
---|
| 6 | * @author jkraaijeveld
|
---|
| 7 | */
|
---|
[171] | 8 | class AnswerSetConnector extends Connector{
|
---|
[149] | 9 | protected $db;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Constructor for the AnswerSetConnector
|
---|
| 13 | */
|
---|
| 14 | public function __construct()
|
---|
| 15 | {
|
---|
[171] | 16 | $this->fileName = 'data/answers/answersets.rdf';
|
---|
[149] | 17 | //Ensure the required folder for this connector exists
|
---|
| 18 | if(!is_dir('data/answers'))
|
---|
| 19 | mkdir('data/answers');
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
[158] | 23 | * function get()
|
---|
| 24 | * @param type $arguments : An array having one ore more of the following elements:
|
---|
| 25 | * 'uid', 'survey', 'respondent', 'answers'.
|
---|
[149] | 26 | */
|
---|
| 27 | public function get($arguments)
|
---|
| 28 | {
|
---|
| 29 | $this->load();
|
---|
| 30 |
|
---|
| 31 | //Build the query string
|
---|
| 32 | $querystring = '
|
---|
| 33 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
| 34 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
| 35 | SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
|
---|
| 36 | WHERE
|
---|
| 37 | {
|
---|
| 38 | _answerset predicates:resource_type resources:answerset ;
|
---|
| 39 | predicates:uid ?uid ;
|
---|
| 40 | predicates:for_survey ?for_survey ;
|
---|
| 41 | predicates:by_respondent ?by_respondent ;
|
---|
[171] | 42 | ' . $this->createArguments($arguments) . '
|
---|
[149] | 43 | }';
|
---|
| 44 | //Query the model
|
---|
| 45 | $results = $this->model->sparqlQuery($querystring);
|
---|
| 46 | $answerSets = array();
|
---|
| 47 | if(!empty($results))
|
---|
| 48 | {
|
---|
| 49 | $this->db = new DatabaseInterface();
|
---|
| 50 | foreach($results as $result)
|
---|
| 51 | {
|
---|
| 52 | $survey = $this->db->get("survey", array("uid" => $result['?for_survey']->label));
|
---|
| 53 | $respondent = $this->db->get("respondent", array("uid" => $result['?by_respondent']->label));
|
---|
| 54 | $answers = $this->getAnswers($result['?uid']->label);
|
---|
| 55 | $answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | return $answerSets;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * function getAnswers()
|
---|
[158] | 63 | * @param type $uid : the uid for which the answers should be retrieved.
|
---|
[149] | 64 | */
|
---|
| 65 | private function getAnswers($uid)
|
---|
| 66 | {
|
---|
[171] | 67 | $result = $this->model->findRegex("[(".$uid.")]", "[(given_answer)]", null);
|
---|
| 68 | $iterator = $result->getStatementIterator();
|
---|
| 69 | $answers = array();
|
---|
| 70 | while($iterator->hasNext())
|
---|
[149] | 71 | {
|
---|
[171] | 72 | $element = $iterator->next();
|
---|
| 73 | $answersR = $this->db->get("answer", array("uid" => $element->getLabelObject()));
|
---|
| 74 | $answers[] = $answersR[0];
|
---|
[149] | 75 | }
|
---|
[171] | 76 | return $answers;
|
---|
[149] | 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
[158] | 80 | * function set()
|
---|
| 81 | * @param type $rToolObject : The ResearchToolObject to be saved.
|
---|
[149] | 82 | */
|
---|
| 83 | public function set($rToolObject)
|
---|
| 84 | {
|
---|
| 85 | $this->load();
|
---|
| 86 | $resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid);
|
---|
| 87 | //Remove the old value stored with the given id
|
---|
| 88 | $this->model->subtract($this->model->find($resourceAnswerSet, null, null));
|
---|
| 89 |
|
---|
| 90 | //Set the new values
|
---|
| 91 | $resourceAnswerSetType = new Resource(ANSWERSET);
|
---|
| 92 | $predicateRType = new Resource(RTYPE);
|
---|
| 93 | $this->model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
|
---|
| 94 |
|
---|
| 95 | $answerSetId = new Literal($rToolObject->uid);
|
---|
| 96 | $predicateId = new Resource(UID);
|
---|
| 97 | $this->model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
|
---|
| 98 |
|
---|
| 99 | $surveyId = new Literal($rToolObject->survey->uid);
|
---|
| 100 | $predicateSurvey = new Resource(FOR_SURVEY);
|
---|
| 101 | $this->model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
|
---|
| 102 |
|
---|
| 103 | $respondentId = new Literal($rToolObject->respondent->uid);
|
---|
| 104 | $predicateRespondent = new Resource(BY_RESPONDENT);
|
---|
| 105 | $this->model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
|
---|
| 106 |
|
---|
| 107 | if(isset($rToolObject->answers))
|
---|
| 108 | {
|
---|
| 109 | foreach($rToolObject->answers as $answer)
|
---|
| 110 | {
|
---|
| 111 | $answerId = new Literal($answer->uid);
|
---|
| 112 | $predicateAnswer = new Resource(GIVEN_ANSWER);
|
---|
| 113 | $this->model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | $this->save();
|
---|
| 117 | }
|
---|
| 118 | }
|
---|