Ignore:
Timestamp:
12/02/11 15:26:01 (13 years ago)
Author:
jkraaijeveld
Message:

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/classes/AnswerConnector.php

    r158 r171  
    1010 * @author jkraaijeveld
    1111 */
    12 class AnswerConnector implements IConnector{
    13     protected $model;
    14         protected $fileName = 'data/answers/answers.rdf';
     12class AnswerConnector extends Connector{
    1513        protected $db;
    1614   
     
    2119        //Ensure the required folder for this connector exists
    2220        if (!is_dir('data/answers/'))
    23             mkdir('data/answers/');
    24     }
    25    
    26     /**
    27      * function load()
    28      * Loads the file into the standard MemModel.
    29      */
    30     public function load() {
    31         //Get the Memory Model from the ModelFactory
    32         $this->model = ModelFactory::getDefaultModel();
    33        
    34         //Ensure the required file exists before loading
    35         if(file_exists($this->fileName))
    36             $this->model->load($this->fileName);
    37     }
    38    
    39     /**
    40      * function save()
    41      * Saves the MemModel into the given file.
    42      */
    43     public function save() {
    44         $this->model->saveAs($this->fileName,'rdf');
     21                        mkdir('data/answers/');
     22                $this->fileName = 'data/answers/answers.rdf';
    4523    }
    4624   
     
    5331        {
    5432                $this->load();
    55                 $keys = array_keys($arguments);
    56                 $uid = ""; $question = ""; $value = "";
    57                 if(in_array("uid", $keys))
    58                         $uid = 'predicates:uid \''.$arguments["uid"].'\'';
    59                 if(in_array("question", $keys))
    60                         $question = 'predicates:question_code \''.$arguments["question"].'\'';
    61                 if(in_array("values", $keys))
    62                 {
    63                         foreach ($arguments["values"] as $val)
    64                         {
    65                                 $value = $value . 'predicates:answered \'' . $val . '\' ';
    66                         }
    67                 }
    6833                //Create the querystring
    6934                $querystring = '
     
    7742                                predicates:uid ?uid ;
    7843                                predicates:question_code ?question_code ;
    79                                 ' . $uid . $question . $value . '
     44                                ' . $this->createArguments($arguments) . '
    8045                        }';
    8146
     
    8954                        foreach($results as $result)
    9055                        {
    91                                 $values = $this->getValues($result['?uid']->label);
    92                                 $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label));
    93                                 $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values);
     56                                $questionResult = $this->db->get("question", array("code" => $result['?question_code']->label));
     57                                $answers[] = new Answer($result['?uid']->label, $questionResult[0], $this->getValues($result['?uid']->label));
    9458                        }
    9559                }
    9660                return $answers;
     61        }
    9762
    98         }
    9963        /**
    10064         * function getValues()
     
    10367        private function getValues($uid)
    10468        {
    105                 $querystring = '
    106                         PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    107                         PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    108                         SELECT DISTINCT ?answered
    109                         WHERE
    110                         {
    111                                 _answer predicates:resource_type resources:answer ;
    112                                 predicates:answered ?answered ;
    113                                 predicates:uid \''. $uid . '\'
    114                 }';
    115                 $values = $this->model->sparqlQuery($querystring);
    116                 $returnArray = array();
    117                 if(!empty($values))
     69                $result = $this->model->findRegex("[(".$uid.")]", "[(answered)]", null);
     70                $iterator = $result->getStatementIterator();
     71                $values = array();
     72                while($iterator->hasNext())
    11873                {
    119                         foreach($values as $value)
    120                         {
    121                                 $returnArray[] = $value['?answered']->label;
    122                         }
     74                        $element = $iterator->next();
     75                        $values[] = $element->getLabelObject();
    12376                }
    124                 return $returnArray;
     77                return $values;
    12578        }
    12679
Note: See TracChangeset for help on using the changeset viewer.