Ignore:
Timestamp:
10/31/11 17:12:28 (14 years ago)
Author:
jkraaijeveld
Message:

Made lots of new connectors, stranded on Sessionconnector for now. Need to find a way to retain the order of applicaties and surveys of a pipeline.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/jos-branch/classes/AnswerConnector.php

    r141 r143  
    1212class AnswerConnector implements IConnector{
    1313    protected $model;
    14     protected $fileName = 'data/users/users.rdf';
     14        protected $fileName = 'data/answers/answers.rdf';
     15        protected $db;
    1516   
    1617    /**
     
    4445    }
    4546   
     47        /**
     48         * Function get()
     49         * Get the answers corresponding to the arguments
     50         * 'Question' argument is supposed to be the question ID
     51         * 'Values' has to be an array to make sure multiple answers can be selected.
     52         */
    4653    public function get($arguments)
    47     {
    48        
    49     }
    50    
     54        {
     55                $this->load();
     56                $keys = array_keys($arguments);
     57                $uid = "?uid"; $question = "?question_code"; $value     = "";
     58                if(in_array("uid", $keys))
     59                        $uid = '\''.$arguments["uid"].'\'';
     60                if(in_array("question", $keys))
     61                        $question = '\''.$arguments["question"].'\'';
     62                if(in_array("values", $keys))
     63                {
     64                        foreach ($arguments["values"] as $val)
     65                        {
     66                                $value = $value . 'predicates:answered \'' . $val . '\' ';
     67                        }
     68                }
     69                //Create the querystring
     70                $querystring = '
     71                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     72                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     73                        SELECT DISTINCT ?uid, ?question_code
     74                        WHERE
     75                        {
     76                                _answer predicates:resource_type resources:answer;
     77
     78                                predicates:uid ?uid ;
     79                                predicates:question_code ?question_code;
     80
     81                                predicates:uid ' . $uid . '
     82                                predicates:question_code ' . $question . '     
     83                                ' . $value . '
     84                        }';
     85
     86                //Query the model
     87                $results = $this->model->sparqlQuery($querystring);     
     88                //An answer can have multiple values, get all these values for the answer instances found.
     89                $answers = array();
     90                if(!empty($results))
     91                {
     92            $this->db = new DatabaseInterface();
     93                        foreach($results as $result)
     94                        {
     95                                $values = $this->getValues($result['?uid']->label);
     96                                $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label));
     97                                $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values);
     98                        }
     99                }
     100                return $answers;
     101
     102        }
     103        /**
     104         * Gets the values that belong to the answer
     105         * specified by the UID
     106         */
     107        private function getValues($uid)
     108        {
     109                $querystring = '
     110                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     111                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     112                        SELECT DISTINCT ?answered
     113                        WHERE
     114                        {
     115                                _answer predicates:resource_type resources:answer ;
     116                                predicates:answered ?answered ;
     117                                predicates:uid \''. $uid . '\'
     118                }';
     119                $values = $this->model->sparqlQuery($querystring);
     120                $returnArray = array();
     121                if(!empty($values))
     122                {
     123                        foreach($values as $value)
     124                        {
     125                                $returnArray[] = $value['?answered']->label;
     126                        }
     127                }
     128                return $returnArray;
     129        }
     130
    51131    /**
    52      *
     132     * Saves the answer Object to the RDF file.
    53133     * @param type $rToolObject
    54134     */
    55135    public function set($rToolObject)
    56136    {
     137        $this->load();
    57138       
     139        $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid);       
     140        //Remove the old value stored with the given id
     141        $this->model->subtract($this->model->find($resourceAnswer, null, null));
     142       
     143        //Save all the new values
     144        $resourceAnswerType = new Resource(ANSWER);
     145        $predicateRType = new Resource(RTYPE);
     146        $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));
     147       
     148                $answerId = new Literal($rToolObject->uid);
     149                $predicateId = new Resource(UID);
     150                $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId));
     151
     152                $questionId = new Literal($rToolObject->question->uid);
     153                $predicateQId = new Resource(QCODE);
     154                $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId));
     155
     156                if(isset($rToolObject->values))
     157                {
     158                        foreach($rToolObject->values as $value)
     159                        {
     160                                $answerValue = new Literal($value);
     161                                $predicateAnswer = new Resource(ANSWERED);
     162                                $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue));
     163                        }
     164                }
     165        $this->save();
    58166    }
    59167}
Note: See TracChangeset for help on using the changeset viewer.