1 | <?php |
---|
2 | // Survey database interface class as intermediate for storing data from the site to the RDF database |
---|
3 | require_once 'rdfConstants.php'; |
---|
4 | // Include RAP Library to write RDF files |
---|
5 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
6 | |
---|
7 | /** |
---|
8 | * Description of UserConnector |
---|
9 | * |
---|
10 | * @author jkraaijeveld |
---|
11 | */ |
---|
12 | class 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 | //Ensure the required file exists before loading |
---|
35 | if(file_exists($this->fileName)) |
---|
36 | $this->model->load($this->fileName); |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * function save() |
---|
41 | * Saves the MemModel into the given file. |
---|
42 | */ |
---|
43 | public function save() { |
---|
44 | $this->model->saveAs($this->fileName,'rdf'); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * function get($arguments) |
---|
49 | * Gets the array of User objects belonging to arguments supplied. |
---|
50 | * @param type $arguments : An array containing zero or more of the following keys: |
---|
51 | * 'uid', 'name', 'password' |
---|
52 | */ |
---|
53 | public function get($arguments) { |
---|
54 | $this->load(); |
---|
55 | //Determine which arguments are supplied |
---|
56 | $keys = array_keys($arguments); |
---|
57 | //Set default values for arguments |
---|
58 | $uid = ""; $name = ""; $password = ""; |
---|
59 | //Set the arguments if they are supplied |
---|
60 | if(in_array("uid", $keys)) |
---|
61 | $uid = "predicates:uid \"".$arguments["uid"]."\""; |
---|
62 | if(in_array("name", $keys)) |
---|
63 | $name = 'predicates:name \''.$arguments["name"].'\''; |
---|
64 | if(in_array("password", $keys)) |
---|
65 | $password = "predicates:password \"".$arguments["password"]."\""; |
---|
66 | |
---|
67 | //Create the querystring |
---|
68 | $querystring = ' |
---|
69 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
70 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
71 | SELECT ?uid, ?name, ?password |
---|
72 | WHERE |
---|
73 | { |
---|
74 | _user predicates:resource_type resources:user ; |
---|
75 | predicates:uid ?uid ; |
---|
76 | predicates:name ?name ; |
---|
77 | predicates:password ?password ; |
---|
78 | ' . $uid . $name . $password . ' |
---|
79 | }'; |
---|
80 | //Query the model |
---|
81 | $results = $this->model->sparqlQuery($querystring); |
---|
82 | $users = array(); |
---|
83 | if(!empty($results)) |
---|
84 | { |
---|
85 | //Run over all results and create appropriate Application objets |
---|
86 | foreach($results as $result) |
---|
87 | { |
---|
88 | $users[] = new User($result['?uid']->label, $result['?name']->label, $result['?password']->label); |
---|
89 | } |
---|
90 | } |
---|
91 | return $users; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Save the given User object in the file. |
---|
96 | * @param type $rToolObject |
---|
97 | */ |
---|
98 | public function set($rToolObject) |
---|
99 | { |
---|
100 | $this->load(); |
---|
101 | $resourceUser = new Resource(USER . '/' . $rToolObject->uid); |
---|
102 | //Remove the old value stored with the given id |
---|
103 | $this->model->subtract($this->model->find($resourceUser, null, null)); |
---|
104 | |
---|
105 | $resourceUserType = new Resource(USER); |
---|
106 | $predicateRType = new Resource(RTYPE); |
---|
107 | $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType)); |
---|
108 | |
---|
109 | $literalUserID = new Literal($rToolObject->uid); |
---|
110 | $predicateUniqueID = new Resource(UID); |
---|
111 | $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID)); |
---|
112 | |
---|
113 | $literalUserName = new Literal($rToolObject->name); |
---|
114 | $predicateName = new Resource(NAME); |
---|
115 | $this->model->add(new Statement($resourceUser,$predicateName,$literalUserName)); |
---|
116 | |
---|
117 | $literalPassword = new Literal($rToolObject->password); |
---|
118 | $predicatePassword = new Resource(PASSWORD); |
---|
119 | $this->model->add(new Statement($resourceUser,$predicatePassword,$literalPassword)); |
---|
120 | $this->save(); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | ?> |
---|