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/SurveyConnector.php

    r158 r171  
    66 * @author jkraaijeveld
    77 */
    8 class SurveyConnector implements IConnector{
    9     protected $model;
    10     protected $fileName = 'data/surveys/surveys.rdf';
     8class SurveyConnector extends Connector{
    119    protected $db;
    1210
     
    1614    public function __construct()
    1715    {
     16                $this->fileName = 'data/surveys/surveys.rdf';
    1817        //Ensure the required folder for this connector exists
    1918        if (!is_dir('data/surveys/'))
    2019            mkdir('data/surveys/');     
    21     }
    22    
    23     /**
    24      * function load()
    25      * Loads the file into the standard MemModel.
    26      */
    27     public function load() {
    28         //Get the Memory Model from the ModelFactory
    29         $this->model = ModelFactory::getDefaultModel();
    30        
    31         //Ensure the required file exists before loading
    32         if(file_exists($this->fileName))
    33             $this->model->load($this->fileName);
    34     }
    35    
    36     /**
    37      * function save()
    38      * Saves the MemModel into the given file.
    39      */
    40     public function save() {
    41         $this->model->saveAs($this->fileName,'rdf');
    4220    }
    4321   
     
    5331        $this->load();
    5432       
    55         $keys = array_keys($arguments);
    56         //Set default values for arguments
    57         $uid = ""; $title = ""; $description = ""; $creator = ""; $questions = "";
    58         if(in_array("uid", $keys))
    59             $uid = 'predicates:uid \''.$arguments["uid"].'\'';
    60         if(in_array("title", $keys))
    61             $title = 'predicates:title \''.$arguments["title"].'\'';
    62         if(in_array("description", $keys))
    63                         $description = "predicates:description \"".$arguments["description"]."\"";
    64                 if(in_array("creator", $keys))
    65                         $creator = "predicates:creator \"".$arguments["creator"]."\"";
    66         if(in_array("questions", $keys))
    67         {
    68             //Append the questions string to filter for questions properly
    69             foreach ($arguments["questions"] as $questionUid)
    70             {
    71                 $questions = $questions . 'predicates:has_question \'' . $questionUid . '\' ';
    72             }
    73         }
    74        
    7533        //Build the query string
    7634        $querystring = '
     
    8543                                predicates:description ?description ;
    8644                                predicates:creator ?creator ;
    87                                 ' . $uid . $title . $description . $creator . $questions . '
     45                                ' . $this->createArguments($arguments) . '
    8846            }';
    8947       
     
    11270    private function getQuestions($uid)
    11371    {
    114         //Create the querystring. Note that the UID is the only argument in this case.
    115         $querystring = '
    116             PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    117             PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    118             SELECT DISTINCT ?has_question
    119             WHERE
    120             {
    121                  _survey        predicates:resource_type        resources:survey ;
    122                 predicates:has_question ?has_question ;
    123                 predicates:uid \'' . $uid . '\'
    124             }';
    125 
    126         $results = $this->model->sparqlQuery($querystring);
    127         $questions = array();
    128         //For every question id, get the corresponding question
    129         if(!empty($results))
    130         {
    131             foreach($results as $questionId)
    132             {
    133                 $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label));
    134                 $questions[] = $resultQ[0];
    135             }
    136         }
    137         return $questions;
     72                $result = $this->model->findRegex("[(".$uid.")]", "[(has_question)]", null);
     73                $iterator = $result->getStatementIterator();
     74                $questions = array();
     75                while($iterator->hasNext())
     76                {
     77                        $element = $iterator->next();
     78                        $result = $this->db->get("question", array("code" => $element->getLabelObject()));
     79                        $questions[] = $result[0];
     80                }
     81                return $questions;
    13882    }
    13983   
Note: See TracChangeset for help on using the changeset viewer.