[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 Respondent
|
---|
| 9 | *
|
---|
| 10 | * @author jkraaijeveld
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | class Respondent extends ResearchToolObject {
|
---|
| 14 | //Static Database-related information.
|
---|
| 15 | private static $filename = 'data/users/respondents.rdf';
|
---|
| 16 |
|
---|
| 17 | public $name;
|
---|
| 18 | public $password;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * Constructor for a Respondent object
|
---|
| 22 | * @param type $uid
|
---|
| 23 | * @param type $name
|
---|
| 24 | * @param type password
|
---|
| 25 | */
|
---|
| 26 | public function __construct($uid = null, $name = null, $password = null)
|
---|
| 27 | {
|
---|
| 28 | if(!isset($uid))
|
---|
| 29 | {
|
---|
| 30 | $uid = md5(uniqid(rand(), true));
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | $this->uid = $uid;
|
---|
| 34 | $this->name = $name;
|
---|
| 35 | $this->password = $password;
|
---|
| 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(Respondent::$filename);
|
---|
| 48 |
|
---|
| 49 | $resourceRespondent = new Resource(RESPONDENT . '/' . $this->uid);
|
---|
| 50 | //Remove the old value stored with the given id
|
---|
| 51 | $model->subtract($model->find($resourceRespondent, null, null));
|
---|
| 52 |
|
---|
| 53 | //Set the new values
|
---|
| 54 | $resourceRespondentType = new Resource(RESPONDENT);
|
---|
| 55 | $predicateRType = new Resource(RTYPE);
|
---|
| 56 | $model->add(new Statement($resourceRespondent,$predicateRType,$resourceRespondentType));
|
---|
| 57 |
|
---|
| 58 | $literalRespondentID = new Literal($this->uid);
|
---|
| 59 | $predicateUniqueID = new Resource(UID);
|
---|
| 60 | $model->add(new Statement($resourceRespondent,$predicateUniqueID,$literalRespondentID));
|
---|
| 61 |
|
---|
| 62 | $literalRespondentName = new Literal($this->name);
|
---|
| 63 | $predicateName = new Resource(NAME);
|
---|
| 64 | $model->add(new Statement($resourceRespondent,$predicateName,$literalRespondentName));
|
---|
| 65 |
|
---|
| 66 | $literalPassword = new Literal($this->password);
|
---|
| 67 | $predicatePassword = new Resource(PASSWORD);
|
---|
| 68 | $model->add(new Statement($resourceRespondent,$predicatePassword,$literalPassword));
|
---|
| 69 | $model->saveAs(Respondent::$filename, 'rdf');
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * static function get($arguments)
|
---|
| 76 | * Gets the array of Respondent objects belonging to arguments supplied.
|
---|
| 77 | * @param type $arguments : An array containing zero or more of the following keys:
|
---|
| 78 | 'uid', 'name', 'password'
|
---|
| 79 | */
|
---|
| 80 | public static function get($arguments) {
|
---|
| 81 | $model = ResearchToolObject::load(Respondent::$filename);
|
---|
| 82 |
|
---|
| 83 | //Create the querystring
|
---|
| 84 | $querystring = '
|
---|
| 85 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
| 86 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
| 87 | SELECT ?uid, ?name, ?password
|
---|
| 88 | WHERE
|
---|
| 89 | {
|
---|
| 90 | _respondent predicates:resource_type resources:respondent ;
|
---|
| 91 | predicates:uid ?uid ;
|
---|
| 92 | predicates:name ?name ;
|
---|
| 93 | predicates:password ?password ;
|
---|
| 94 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
| 95 | }';
|
---|
| 96 | //Query the model
|
---|
| 97 | $results = $model->sparqlQuery($querystring);
|
---|
| 98 | $respondents = array();
|
---|
| 99 | if(!empty($results))
|
---|
| 100 | {
|
---|
| 101 | //Run over all results and create appropriate Respondent objets
|
---|
| 102 | foreach($results as $result)
|
---|
| 103 | {
|
---|
| 104 | $respondents[] = new Respondent($result['?uid']->label, $result['?name']->label, $result['?password']->label);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | return $respondents;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 |
|
---|