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

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

Refactored queries / cleaned up some code.

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