source: Dev/branches/jos-branch/classes/UserConnector.php @ 148

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

Finished DatabaseInterface? v0.1? I hate version numbering.

File size: 4.2 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 UserConnector
9 *
10 * @author jkraaijeveld
11 */
12class UserConnector implements IConnector{
13   
14    protected $model;
15    protected $fileName = 'data/users/users.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 User 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                    _user       predicates:resource_type        resources:user ;
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        $users = array();
86        if(!empty($results))
87        {
88            //Run over all results and create appropriate Application objets
89            foreach($results as $result)
90            {
91                    $users[] = new User($result['?uid']->label, $result['?name']->label, $result['?password']->label);
92            }
93        }
94        return $users;
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        $resourceUser = new Resource(USER . '/' . $rToolObject->uid);
105        //Remove the old value stored with the given id
106        $this->model->subtract($this->model->find($resourceUser, null, null));
107
108        $resourceUserType = new Resource(USER);
109        $predicateRType = new Resource(RTYPE);
110        $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType));
111
112        $literalUserID = new Literal($rToolObject->uid);
113        $predicateUniqueID = new Resource(UID);
114        $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID));
115       
116        $literalUserName = new Literal($rToolObject->name);
117        $predicateName = new Resource(NAME);
118        $this->model->add(new Statement($resourceUser,$predicateName,$literalUserName));                       
119
120        $literalPassword = new Literal($rToolObject->password);
121        $predicatePassword = new Resource(PASSWORD);
122        $this->model->add(new Statement($resourceUser,$predicatePassword,$literalPassword));
123        $this->save();
124    }
125}
126
127?>
Note: See TracBrowser for help on using the repository browser.