Changeset 149 for Dev/trunk/classes


Ignore:
Timestamp:
11/07/11 16:55:36 (13 years ago)
Author:
jkraaijeveld
Message:

Added missing classes for the database connection

Location:
Dev/trunk/classes
Files:
7 added
8 edited

Legend:

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

    r144 r149  
    1111 * @author jkraaijeveld
    1212 */
    13 class ApplicationConnector implements IConnector {
    14 
     13class ApplicationConnector implements IConnector{
     14   
    1515    protected $model;
    1616    protected $fileName = 'data/applications/applications.rdf';
     
    1919     * Constructor for ApplicationConnector.
    2020     */
    21     public function __construct() {
     21    public function __construct()
     22    {
    2223        //Ensure the required folder for this connector exists
    23         if (!is_dir('data/applications/')) {
    24             mkdir('data/applications/', null, true);
    25         }
     24        if (!is_dir('data/applications/'))
     25            mkdir('data/applications/');       
    2626    }
    27 
     27   
    2828    /**
    2929     * function load()
     
    3333        //Get the Memory Model from the ModelFactory
    3434        $this->model = ModelFactory::getDefaultModel();
    35 
     35       
    3636        //Ensure the required file exists before loading
    37         if (file_exists($this->fileName))
     37        if(file_exists($this->fileName))
    3838            $this->model->load($this->fileName);
    3939    }
    40 
     40   
    4141    /**
    4242     * function save()
     
    4444     */
    4545    public function save() {
    46         $this->model->saveAs($this->fileName, 'rdf');
     46        $this->model->saveAs($this->fileName,'rdf');
    4747    }
    48 
     48   
    4949    /**
    5050     * function get($arguments)
     
    5858        $keys = array_keys($arguments);
    5959        //Set default values for arguments
    60         $uid = "?uid";
    61         $title = "?title";
    62         $description = "?description";
    63         $style = "?style";
     60        $uid = "?uid"; $title = "?title"; $description = "?description"; $style = "?style";
    6461        //Set the arguments if they are supplied
    65         if (in_array("uid", $keys))
    66             $uid = "\"" . $arguments["uid"] . "\"";
    67         if (in_array("title", $keys))
    68             $title = '\'' . $arguments["title"] . '\'';
    69         if (in_array("description", $keys))
    70             $description = "\"" . $arguments["description"] . "\"";
    71         if (in_array("style", $keys))
    72             $style = "\"" . $arguments["style"] . "\"";
     62        if(in_array("uid", $keys))
     63            $uid = "\"".$arguments["uid"]."\"";
     64        if(in_array("title", $keys))
     65            $title = '\''.$arguments["title"].'\'';
     66        if(in_array("description", $keys))
     67            $description = "\"".$arguments["description"]."\"";
     68        if(in_array("style", $keys))
     69            $style = "\"".$arguments["style"]."\"";   
    7370
    7471        //Create the querystring
     
    9390        $results = $this->model->sparqlQuery($querystring);
    9491        $applications = array();
    95         if (!empty($results)) {
     92        if(!empty($results))
     93        {
    9694            //Run over all results and create appropriate Application objets
    97             foreach ($results as $result) {
    98                 $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label);
     95            foreach($results as $result)
     96            {
     97                    $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label);
    9998            }
    10099        }
    101100        return $applications;
    102101    }
    103 
     102   
    104103    /**
    105104     * function set()
     
    108107     * @param type $rToolObject: The ResearchToolObject to be saved.
    109108     */
    110     public function set($rToolObject) {
     109    public function set($rToolObject)
     110    {
    111111        $this->load();
    112         $resourceApplication = new Resource(APPLICATION . '/' . $rToolObject->uid);
     112        $resourceApplication = new Resource(APPLICATION.'/'.$rToolObject->uid);
    113113        //Remove the old value stored with the given id
    114114        $this->model->subtract($this->model->find($resourceApplication, null, null));
     
    117117        $resourceApplicationType = new Resource(APPLICATION);
    118118        $predicateRType = new Resource(RTYPE);
    119         $this->model->add(new Statement($resourceApplication, $predicateRType, $resourceApplicationType));
    120 
     119        $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType));
     120               
    121121        $literalApplicationID = new Literal($rToolObject->uid);
    122122        $predicateUniqueID = new Resource(UID);
    123         $this->model->add(new Statement($resourceApplication, $predicateUniqueID, $literalApplicationID));
     123        $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID));
    124124
    125125        $applicationTitle = new Literal($rToolObject->title);
    126         $predicateTitle = new Resource(TITLE);
    127         $this->model->add(new Statement($resourceApplication, $predicateTitle, $applicationTitle));
     126        $predicateTitle = new Resource(TITLE); 
     127        $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle));
    128128
    129129        $applicationDescription = new Literal($rToolObject->description);
    130130        $predicateDescription = new Resource(DESCRIPTION);
    131         $this->model->add(new Statement($resourceApplication, $predicateDescription, $applicationDescription));
     131        $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription));
    132132
    133133        $applicationStyle = new Literal($rToolObject->style);
    134134        $predicateStyle = new Resource(STYLE);
    135         $this->model->add(new Statement($resourceApplication, $predicateStyle, $applicationStyle));
    136 
    137         $this->save();
     135        $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle));
     136               
     137        $this->save();
    138138    }
    139 
    140139}
    141140
  • Dev/trunk/classes/DatabaseInterface.php

    r139 r149  
    2121    private $userConnector;
    2222    private $surveyConnector;
     23    private $respondentConnector;
     24        private $answerConnector;
     25        private $answerSetConnector;
     26        private $sessionConnector;
    2327   
    2428    /**
     
    3135        $this->userConnector = new UserConnector();
    3236        $this->surveyConnector = new SurveyConnector();
     37        $this->respondentConnector = new RespondentConnector();
     38                $this->answerConnector = new AnswerConnector();
     39                $this->answerSetConnector = new AnswerSetConnector();
     40                $this->sessionConnector = new SessionConnector();
    3341    }
    3442   
     
    3947     * @return type
    4048     */
    41     public function get($type, $arguments)
     49    public function get($type, $arguments = array())
    4250    {
    4351        switch(strtolower($type))
     
    5462            case "survey":
    5563                return $this->surveyConnector->get($arguments);
     64                break;
     65            case "respondent":
     66                return $this->respondentConnector->get($arguments);
     67                break;
     68            case "answer":
     69                return $this->answerConnector->get($arguments);
     70                                break;
     71                        case "answerset":
     72                                return $this->answerSetConnector->get($arguments);
     73                                break;
     74                        case "session":
     75                                return $this->sessionConnector->get($arguments);
     76                                break;
    5677        }
    5778    }
     
    7596                break;
    7697            case "Survey":
    77                 $this->surveyConnector->set($rToolObject);
     98                                $this->surveyConnector->set($rToolObject);
     99                                break;
     100            case "Respondent":
     101                                $this->respondentConnector->set($rToolObject);
     102                                break;
     103            case "Answer":
     104                                $this->answerConnector->set($rToolObject);
     105                                break;
     106                        case "AnswerSet":
     107                                $this->answerSetConnector->set($rToolObject);
     108                                break;
     109                        case "Session":
     110                                $this->sessionConnector->set($rToolObject);
     111                                break;
    78112        }
    79113    }
  • Dev/trunk/classes/Question.php

    r131 r149  
    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/trunk/classes/QuestionConnector.php

    r131 r149  
    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 = '
    75          PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     82            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    7683            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    7784            SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category
     
    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
    94102        $results = $this->model->sparqlQuery($querystring);
     103
    95104       
    96105        $questions = array();
  • Dev/trunk/classes/Session.php

    r69 r149  
    11<?php
    2 
    32/**
    43 * Description of Session
    54 *
    6  * @author fpvanagthoven
     5 * @author jkraaijeveld
    76 */
    8 class Session {
    97
    10     public $title;
    11     public $description;
    12     public $pipeline;
    13     public $count;
     8class Session extends ResearchToolObject
     9{
     10        public $title;
     11        public $datetime;
     12        public $pipeline;
     13        public $answersets;
    1414
    15     public function __construct($title, $description) {
    16         $this->title = $title;
    17         $this->description = $description;
    18         $this->count = 0;
    19        
    20         $this->pipeline = array();
    21     }
    22    
    23     public static function getSession($info)
    24     {
    25         if (!empty($info))
    26         {
    27             $title = $info['sessionTitle'];
    28             $description = $info['sessionDescription'];
    29             $session = new Session($title, $description);
    30             $session->count = $info['pipelineCount'];
    31            
    32             $pipelineCount = 1;
    33             for ($i = 1; $i < $session->count + 1; $i++)
    34             {
    35                 if (isset($info[$i . 's']))
    36                 {
    37                     $session->pipeline[$i . 's'] = $info[$i . 's'];
    38                 }
    39                 else if (isset($info[$i . 'd']))
    40                 {
    41                     $session->pipeline[$i . 'd'] = $info[$i . 'd'];
    42                 }
    43                 else if (isset($info[$i . 'a']))
    44                 {
    45                     $session->pipeline[$i . 'a'] = $info[$i . 'a'];
    46                 }
     15       
     16        /**
     17         * Constructor for a Session object
     18         * @param type $uid
     19         * @param type $title
     20         * @param type $datetime
     21         * @param type $pipeline
     22         */
     23        public function __construct($uid = null, $title = null, $datetime = null, $pipeline = null, $answersets = null)
     24        {
     25                if(!isset($uid))
     26                {
     27                        $uid = md5(uniqid(rand(), true));
     28                }
     29                $this->uid = $uid;
     30                $this->title = $title;
     31                $this->datetime = $datetime;
     32                $this->pipeline = $pipeline;
     33                $this->answersets = $answersets;
     34        }
     35}       
    4736
    48                 $pipelineCount++;
    49             }
    50            
    51             return $session;
    52         }
    53         else
    54             return null;
    55     }
    5637
    57 }
    5838?>
  • Dev/trunk/classes/Survey.php

    r139 r149  
    1212    public $questions;
    1313   
    14     public function __construct($uid = null, $title = null, $description = null, $questions = null) {
     14    public function __construct($uid = null, $title = null, $description = null, $creator = null, $questions = null) {
    1515        if(!isset($uid))
    1616        {
     
    1919        $this->uid = $uid;
    2020        $this->title = $title;
    21         $this->description = $description;
     21                $this->description = $description;
     22                $this->creator = $creator;
    2223        $this->questions = $questions;
    2324    }
  • Dev/trunk/classes/SurveyConnector.php

    r139 r149  
    5555        $keys = array_keys($arguments);
    5656        //Set default values for arguments
    57         $uid = "?uid"; $title = "?title"; $description = "?description"; $questions = "";
     57        $uid = "?uid"; $title = "?title"; $description = "?description"; $creator = "?creator"; $questions = "";
    5858        if(in_array("uid", $keys))
    5959            $uid = '\''.$arguments["uid"].'\'';
     
    6161            $title = '\''.$arguments["title"].'\'';
    6262        if(in_array("description", $keys))
    63             $description = "\"".$arguments["description"]."\"";
     63                        $description = "\"".$arguments["description"]."\"";
     64                if(in_array("creator", $keys))
     65                        $creator = "\"".$arguments["creator"]."\"";
    6466        if(in_array("questions", $keys))
    6567        {
     
    7577            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    7678            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    77             SELECT DISTINCT ?uid, ?title, ?description
     79            SELECT DISTINCT ?uid, ?title, ?description, ?creator
    7880            WHERE
    7981            {
     
    8486                predicates:uid ' . $uid . '
    8587                predicates:title ' . $title . '
    86                 predicates:description ' . $description . '
     88                                predicates:description ' . $description . '
     89                                predicates:creator ' . $creator . '
    8790                ' . $questions . '
    8891            }';
     
    98101            foreach($results as $result)
    99102            {
    100                 $questions = $this->getQuestions($result['?uid']->label);
    101                 $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $questions);
     103                                $questions = $this->getQuestions($result['?uid']->label);
     104                                $creator = $this->db->get("user", array("uid" => $result['?creator']->label));
     105                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions);
    102106            }
    103107        }
     
    165169        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
    166170       
     171                $predicateCreator = new Resource(CREATOR);
     172                $surveyCreator = new Literal($rToolObject->creator->uid);
     173                $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
     174
    167175        if(isset($rToolObject->questions))
    168176        {
  • Dev/trunk/classes/UserConnector.php

    r131 r149  
    1616   
    1717    /**
    18      * Constructor for ApplicationConnector.
     18     * Constructor for RespondentConnector.
    1919     */
    2020    public function __construct()
     
    5050     * Gets the array of User objects belonging to arguments supplied.
    5151     * @param type $arguments : An array containing zero or more of the following keys:
    52      *                          'uid', 'name', 'password', 'style'
     52     *                          'uid', 'name', 'password'
    5353     */
    5454    public function get($arguments) {
     
    8181                                        predicates:password ' . $password . '
    8282            }';
    83         echo $querystring;
    8483        //Query the model
    8584        $results = $this->model->sparqlQuery($querystring);
Note: See TracChangeset for help on using the changeset viewer.