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