source: Dev/branches/rest-dojo-ui/server/classes/models/Respondent.php @ 302

Last change on this file since 302 was 274, checked in by hendrikvanantwerpen, 13 years ago
  • [Client] Moved pages in subtree of rft/, allowing controllers next to them.
  • [Client] Created questions page, gives overview and allows adding.
  • [Client] Page controllers inherit from _Page, because the previous mechanism w

asn't working.

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