source: Dev/trunk/classes/RespondentConnector.php @ 151

Last change on this file since 151 was 149, checked in by jkraaijeveld, 13 years ago

Added missing classes for the database connection

File size: 4.4 KB
Line 
1<?php
2// Survey database interface class as intermediate for storing data from the site to the RDF database
3require_once 'rdfConstants.php';
4// Include RAP Library to write RDF files
5include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
6
7/**
8 * Description of RespondentConnector
9 *
10 * @author jkraaijeveld
11 */
12class RespondentConnector implements IConnector{
13   
14    protected $model;
15    protected $fileName = 'data/users/respondents.rdf';
16   
17    /**
18     * Constructor for RespondentConnector.
19     */
20    public function __construct()
21    {
22        //Ensure the required folder for this connector exists
23        if (!is_dir('data/users/'))
24            mkdir('data/users/');       
25    }
26   
27    /**
28     * function load()
29     * Loads the file into the standard MemModel.
30     */
31    public function load() {
32        //Get the Memory Model from the ModelFactory
33        $this->model = ModelFactory::getDefaultModel();
34       
35        //Ensure the required file exists before loading
36        if(file_exists($this->fileName))
37            $this->model->load($this->fileName);
38    }
39   
40    /**
41     * function save()
42     * Saves the MemModel into the given file.
43     */
44    public function save() {
45        $this->model->saveAs($this->fileName,'rdf');
46    }
47   
48    /**
49     * function get($arguments)
50     * Gets the array of Respondent objects belonging to arguments supplied.
51     * @param type $arguments : An array containing zero or more of the following keys:
52     *                          'uid', 'name', 'password'
53     */
54    public function get($arguments) {
55        $this->load();
56        //Determine which arguments are supplied
57        $keys = array_keys($arguments);
58        //Set default values for arguments
59        $uid = "?uid"; $name = "?name"; $password = "?password";
60        //Set the arguments if they are supplied
61        if(in_array("uid", $keys))
62            $uid = "\"".$arguments["uid"]."\"";
63        if(in_array("name", $keys))
64            $name = '\''.$arguments["name"].'\'';
65        if(in_array("password", $keys))
66            $password = "\"".$arguments["password"]."\"";
67       
68        //Create the querystring
69        $querystring = '
70            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
71            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
72            SELECT ?uid, ?name, ?password
73            WHERE       
74            {
75                    _respondent predicates:resource_type        resources:respondent ;
76                                        predicates:uid ?uid ;
77                                        predicates:name ?name ;
78                                        predicates:password ?password ;
79                                predicates:uid ' . $uid . '
80                                        predicates:name ' . $name . '
81                                        predicates:password ' . $password . '
82            }';
83        //Query the model
84        $results = $this->model->sparqlQuery($querystring);
85                $respondents = array();
86        if(!empty($results))
87        {
88            //Run over all results and create appropriate Respondent objets
89            foreach($results as $result)
90            {
91                    $respondents[] = new Respondent($result['?uid']->label, $result['?name']->label, $result['?password']->label);
92            }
93        }
94        return $respondents;
95    }
96   
97    /**
98     * Save the given User object in the file.
99     * @param type $rToolObject
100     */
101    public function set($rToolObject)
102    {
103        $this->load();
104        $resourceRespondent = new Resource(RESPONDENT . '/' . $rToolObject->uid);
105        //Remove the old value stored with the given id
106        $this->model->subtract($this->model->find($resourceRespondent, null, null));
107
108                //Set the new values
109        $resourceRespondentType = new Resource(RESPONDENT);
110        $predicateRType = new Resource(RTYPE);
111        $this->model->add(new Statement($resourceRespondent,$predicateRType,$resourceRespondentType));
112
113        $literalRespondentID = new Literal($rToolObject->uid);
114        $predicateUniqueID = new Resource(UID);
115        $this->model->add(new Statement($resourceRespondent,$predicateUniqueID,$literalRespondentID));
116       
117        $literalRespondentName = new Literal($rToolObject->name);
118        $predicateName = new Resource(NAME);
119        $this->model->add(new Statement($resourceRespondent,$predicateName,$literalRespondentName));                   
120
121        $literalPassword = new Literal($rToolObject->password);
122        $predicatePassword = new Resource(PASSWORD);
123        $this->model->add(new Statement($resourceRespondent,$predicatePassword,$literalPassword));
124        $this->save();
125    }
126}
127
128?>
Note: See TracBrowser for help on using the repository browser.