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.

Location:
Dev/branches/jos-branch/classes
Files:
4 added
6 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}
  • Dev/branches/jos-branch/classes/DatabaseInterface.php

    r141 r143  
    2222    private $surveyConnector;
    2323    private $respondentConnector;
    24     private $answerConnector;
     24        private $answerConnector;
     25        private $answerSetConnector;
     26        private $sessionConnector;
    2527   
    2628    /**
     
    3436        $this->surveyConnector = new SurveyConnector();
    3537        $this->respondentConnector = new RespondentConnector();
    36         $this->answerConnector = new AnswerConnector();
     38                $this->answerConnector = new AnswerConnector();
     39                $this->answerSetConnector = new AnswerSetConnector();
     40                $this->sessionConnector = new SessionConnector();
    3741    }
    3842   
     
    6468            case "answer":
    6569                return $this->answerConnector->get($arguments);
    66                 break;
    67                
     70                                break;
     71                        case "answerset":
     72                                return $this->answerSetConnector->get($arguments);
     73                        case "session":
     74                                return $this->sessionConnector->get($arguments);
     75
    6876        }
    6977    }
     
    8795                break;
    8896            case "Survey":
    89                 $this->surveyConnector->set($rToolObject);
     97                                $this->surveyConnector->set($rToolObject);
     98                                break;
    9099            case "Respondent":
    91                 $this->respondentConnector->set($rToolObject);
     100                                $this->respondentConnector->set($rToolObject);
     101                                break;
    92102            case "Answer":
    93                 $this->answerConnector->set($rToolObject);
     103                                $this->answerConnector->set($rToolObject);
     104                                break;
     105                        case "AnswerSet":
     106                                $this->answerSetConnector->set($rToolObject);
     107                                break;
     108                        case "Session":
     109                                $this->sessionConnector->set($rToolObject);
     110                                break;
     111
    94112        }
    95113    }
  • Dev/branches/jos-branch/classes/Question.php

    r130 r143  
    2929    public function __construct($uid, $title = null, $type = null, $description = null, $category = null, $answers = null)
    3030    {
     31                if(!isset($uid))
     32                {
     33                        $uid = md5(uniqid(rand(), true));
     34                }
    3135        $this->uid = $uid;
    3236        $this->title = $title;
  • Dev/branches/jos-branch/classes/QuestionConnector.php

    r138 r143  
    5959        $keys = array_keys($arguments);
    6060        //Set default values for arguments
    61         $uid = "?uid"; $title = "?title"; $type = "?type"; $description = "?description"; $category = "?category";
     61        $uid = "?uid"; $title = "?title"; $type = "?type"; $description = "?description"; $category = "?category"; $has_answer = "";
    6262        //Set the arguments if they are supplied
    6363        if(in_array("uid", $keys))
     
    7070            $description = "\"".$arguments["description"]."\"";
    7171        if(in_array("category", $keys))
    72             $style = "\"".$arguments["category"]."\"";
     72                        $style = "\"".$arguments["category"]."\"";
     73                if(in_array("answers", $keys))
     74                {
     75                        foreach($arguments["answers"] as $answer)
     76                        {
     77                                $has_answer = $has_answer . 'predicates:has_answer \'' . $answer . '\' ';
     78                        }
     79                }
    7380           
    7481        $querystring = '
     
    8996                predicates:question_type ' . $type . '
    9097                predicates:description ' . $description . '
    91                 predicates:question_category ' . $category . '
     98                                predicates:question_category ' . $category . '
     99                                ' . $has_answer . '
    92100            }';
    93101        //Create the querystring
  • Dev/branches/jos-branch/classes/RespondentConnector.php

    r140 r143  
    7373            WHERE       
    7474            {
    75                     _user       predicates:resource_type        resources:user ;
     75                    _respondent predicates:resource_type        resources:respondent ;
    7676                                        predicates:uid ?uid ;
    7777                                        predicates:name ?name ;
    7878                                        predicates:password ?password ;
    79                                         predicates:uid ' . $uid . '
     79                                predicates:uid ' . $uid . '
    8080                                        predicates:name ' . $name . '
    8181                                        predicates:password ' . $password . '
    8282            }';
    83         echo $querystring;
    8483        //Query the model
    8584        $results = $this->model->sparqlQuery($querystring);
    86         $respondents = array();
     85                $respondents = array();
    8786        if(!empty($results))
    8887        {
     
    107106        $this->model->subtract($this->model->find($resourceRespondent, null, null));
    108107
     108                //Set the new values
    109109        $resourceRespondentType = new Resource(RESPONDENT);
    110110        $predicateRType = new Resource(RTYPE);
  • Dev/branches/jos-branch/classes/SurveyConnector.php

    r138 r143  
    9595        if(!empty($results))
    9696        {
    97             $this->db = new DatabaseInterface();
    9897            foreach($results as $result)
    9998            {
     
    128127        if(!empty($results))
    129128        {
     129            $this->db = new DatabaseInterface();
    130130            foreach($results as $questionId)
    131131            {
Note: See TracChangeset for help on using the changeset viewer.