source: Dev/branches/jos-branch/server/classes/models/User.php @ 308

Last change on this file since 308 was 308, checked in by jkraaijeveld, 13 years ago
File size: 3.9 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 User
9 *
10 * @author jkraaijeveld
11 */
12
13class User extends ResearchToolObject {
14
15        //Static Database-related information.
16        private static $filename = 'data/users/users.rdf';
17   
18    public $email;
19    public $passwordHash;
20    public $passwordSalt;
21   
22    /**
23     * Constructor of User
24     * If the user does not yet exist in the database, call with null as first parameter
25     */
26    public function __construct($uid = null, $email = null, $passwordHash = null, $passwordSalt = null) {
27        if(!isset($uid))
28        {
29             $uid = md5(uniqid(rand(), true));
30        }
31        $this->uid = $uid;
32                $this->email = $email;
33        $this->passwordHash = $passwordHash;
34        $this->passwordSalt = $passwordSalt;
35        }
36
37        /**
38         * function save()
39         * Saves the current object into the database.
40         */
41        public function save()
42        {
43                //Ensure the required folder exists.
44                if(!is_dir('data/users/'))
45                        mkdir('data/users/');
46
47                $model = ResearchToolObject::load(User::$filename);
48        $resourceUser = new Resource(USER . '/' . $this->uid);
49        //Remove the old value stored with the given id
50        $model->subtract($model->find($resourceUser, null, null));
51
52        $resourceUserType = new Resource(USER);
53        $predicateRType = new Resource(RTYPE);
54        $model->add(new Statement($resourceUser,$predicateRType,$resourceUserType));
55
56        $literalUserID = new Literal($this->uid);
57        $predicateUniqueID = new Resource(UID);
58        $model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID));
59       
60        $literalUserName = new Literal($this->email);
61        $predicateName = new Resource(EMAIL);
62        $model->add(new Statement($resourceUser,$predicateName,$literalUserName));                     
63
64        $literalPasswordHash = new Literal($this->passwordHash);
65        $predicatePasswordHash = new Resource(PASSWORDHASH);
66                $model->add(new Statement($resourceUser,$predicatePasswordHash,$literalPasswordHash));
67
68        $literalPasswordSalt = new Literal($this->passwordSalt);
69        $predicatePasswordSalt = new Resource(PASSWORDSALT);
70                $model->add(new Statement($resourceUser,$predicatePasswordSalt,$literalPasswordSalt));
71
72                $model->saveAs(User::$filename, 'rdf');
73                return true;
74        }
75
76
77        /**
78         * static function get($arguments)
79         * Gets the array of User objects belonging to arguments supplied.
80         * @param type $arguments : An array containing zero or more of the following keys:
81         *                                                      'uid', 'email', 'passwordHash', 'passwordSalt'
82         */
83        public static function get($arguments)
84        {
85            $model = ResearchToolObject::load(User::$filename);
86        //Create the querystring
87        $querystring = '
88            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
89            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
90            SELECT ?uid, ?email, ?passwordHash, ?passwordSalt
91            WHERE       
92            {
93                    _user       predicates:resource_type        resources:user ;
94                                        predicates:uid ?uid ;
95                                        predicates:email ?email ;
96                                        predicates:passwordHash ?passwordHash ;
97                                        predicates:passwordSalt ?passwordSalt ;
98                                        ' . ResearchToolObject::createArguments($arguments) . '
99            }';
100        //Query the model
101                $results = $model->sparqlQuery($querystring);
102        $users = array();
103        if(!empty($results))
104        {
105            //Run over all results and create appropriate User objects
106            foreach($results as $result)
107            {
108                                $users[] = new User($result['?uid']->label, $result['?email']->label, $result['?passwordHash']->label, $result['?passwordSalt']->label);
109            }
110        }
111        return $users;
112        }
113
114    public static function create($obj) {
115        return new User($obj->uid, $obj->email,$obj->passwordHash,$obj->passwordSalt);
116    }
117
118}
119
120?>
Note: See TracBrowser for help on using the repository browser.