1 | <?php
|
---|
2 | require_once 'rdfConstants.php';
|
---|
3 | //Include RAP Library to write to RDF files
|
---|
4 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Description of ApplicationInstance
|
---|
8 | *
|
---|
9 | * @author jkraaijeveld
|
---|
10 | */
|
---|
11 |
|
---|
12 | class ApplicationInstance extends ResearchToolObject{
|
---|
13 | public static $filename = 'data/applications/applicationinstances.rdf';
|
---|
14 |
|
---|
15 | public $application;
|
---|
16 | public $starttime;
|
---|
17 | public $endtime;
|
---|
18 | public $open;
|
---|
19 | public $playerresults;
|
---|
20 | public $groupresults;
|
---|
21 | public $periodicresults;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructor for an ApplicationInstance object.
|
---|
25 | * @param type $uid: The UID of the application instance object.
|
---|
26 | * @param type $application: The related application.
|
---|
27 | * @param type $starttime: The starttime of the application round.
|
---|
28 | * @param type $endtime: The endtime of the application round.
|
---|
29 | * @param type $open: A boolean to specify if people can start this application.
|
---|
30 | * @param type $playerresults: Individual player results from playing.
|
---|
31 | * @param type $groupresults: Group-based results from playing.
|
---|
32 | * @param type $periodicresults: Periodical results from playing.
|
---|
33 | */
|
---|
34 | public function __construct($uid = null, $application = null, $starttime = null, $endtime = null, $open = 0, $playerresults = null, $groupresults = null, $periodicresults = null)
|
---|
35 | {
|
---|
36 | if(!isset($uid))
|
---|
37 | {
|
---|
38 | $uid = md5(uniqid(rand(), true));
|
---|
39 | }
|
---|
40 | $this->uid = $uid;
|
---|
41 | $this->application = $application;
|
---|
42 | $this->starttime = $starttime;
|
---|
43 | $this->endtime = $endtime;
|
---|
44 | $this->open = $open;
|
---|
45 | $this->playerresults = $playerresults;
|
---|
46 | $this->groupresults = $groupresults;
|
---|
47 | $this->periodicresults = $periodicresults;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * function evaluate()
|
---|
52 | * Evaluates the references in this object
|
---|
53 | */
|
---|
54 | public function evaluate()
|
---|
55 | {
|
---|
56 | if(is_string($this->application))
|
---|
57 | {
|
---|
58 | $app = ResearchToolObject::stripUri($this->application);
|
---|
59 | $result = Application::get(array("uid" => $app["uid"]));
|
---|
60 | if(!isset($result[0]))
|
---|
61 | return false;
|
---|
62 | $this->application = $result[0];
|
---|
63 | }
|
---|
64 |
|
---|
65 | //TODO: Evaluate game results.
|
---|
66 |
|
---|
67 |
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * function save()
|
---|
73 | * Saves the current object into the database.
|
---|
74 | */
|
---|
75 | public function save()
|
---|
76 | {
|
---|
77 | //Do note save if there are invalid references in this object.
|
---|
78 | if(!$this->evaluate())
|
---|
79 | throw new Exception('Evaluation failed.');
|
---|
80 |
|
---|
81 | //Ensure the required folder exists.
|
---|
82 | if(!is_dir('data/applications/'))
|
---|
83 | mkdir('data/applications/');
|
---|
84 |
|
---|
85 | $model = ResearchToolObject::load(ApplicationInstance::$filename);
|
---|
86 | $resourceAI = new Resource(APPLICATIONINSTANCE.'/'.$this->uid);
|
---|
87 | //Remove the old value stored with the given id.
|
---|
88 | $model->subtract($model->find($resourceAI, null, null));
|
---|
89 |
|
---|
90 | //Add the new statements to the model
|
---|
91 | $resourceAIType = new Resource(APPLICATIONINSTANCE);
|
---|
92 | $predicateRType = new Resource(RTYPE);
|
---|
93 | $model->add(new Statement($resourceAI, $predicateRType, $resourceAIType));
|
---|
94 |
|
---|
95 | $AIUid = new Literal($this->uid);
|
---|
96 | $predicateUid = new Resource(UID);
|
---|
97 | $model->add(new Statement($resourceAI, $predicateUid, $AIUid));
|
---|
98 |
|
---|
99 | $AIApplication = new Resource(APPLICATION . '/' . $this->application->uid);
|
---|
100 | $predicateApplication = new Resource(OF_APPLICATION);
|
---|
101 | $model->add(new Statement($resourceAI, $predicateApplication, $AIApplication));
|
---|
102 |
|
---|
103 | $AIStartTime = new Literal($this->starttime->getTimestamp());
|
---|
104 | $predicateStartTime = new Resource(STARTTIME);
|
---|
105 | $model->add(new Statement($resourceAI, $predicateStartTime, $AIStartTime));
|
---|
106 |
|
---|
107 | $AIEndTime = new Literal($this->endtime->getTimestamp());
|
---|
108 | $predicateEndTime = new Resource(ENDTIME);
|
---|
109 | $model->add(new Statement($resourceAI, $predicateEndTime, $AIEndTime));
|
---|
110 |
|
---|
111 | $AIOpen = new Literal((bool)$this->open);
|
---|
112 | $predicateOpen = new Resource(OPEN);
|
---|
113 | $model->add(new Statement($resourceAI, $predicateOpen, $AIOpen));
|
---|
114 |
|
---|
115 | $model->saveAs(ApplicationInstance::$filename, 'rdf');
|
---|
116 | return true;
|
---|
117 | //TODO: Game results.
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * function get($arguments)
|
---|
122 | * Gets the array of ApplicationInstance objects belonging to the arguments supplied.
|
---|
123 | * @param type $arguments : An array containing zero or more of the following keys:
|
---|
124 | * 'uid', 'of_application', 'starttime', 'endtime', 'open'
|
---|
125 | * TODO: Support game results.
|
---|
126 | */
|
---|
127 | public static function get($arguments)
|
---|
128 | {
|
---|
129 | $model = ResearchToolObject::load(ApplicationInstance::$filename);
|
---|
130 | //Build the query string
|
---|
131 | $querystring = '
|
---|
132 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
133 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
134 | SELECT DISTINCT ?uid, ?of_application, ?starttime, ?endtime, ?open
|
---|
135 | WHERE
|
---|
136 | {
|
---|
137 | ?applicationinstance predicates:resource_type resources:applicationinstance ;
|
---|
138 | predicates:uid ?uid ;
|
---|
139 | predicates:of_application ?of_application ;
|
---|
140 | predicates:starttime ?starttime ;
|
---|
141 | predicates:endtime ?endtime ;
|
---|
142 | predicates:open ?open ;
|
---|
143 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
144 | }';
|
---|
145 | //Query the model
|
---|
146 | $results = $model->sparqlQuery($querystring);
|
---|
147 | //Create the corresponding ApplicationInstance objects
|
---|
148 | $aInstances = array();
|
---|
149 | if(!empty($results))
|
---|
150 | {
|
---|
151 | foreach($results as $result)
|
---|
152 | {
|
---|
153 | $startTime = new DateTime();
|
---|
154 | $startTime->setTimestamp(intval($result['?starttime']->label));
|
---|
155 | $endTime = new DateTime();
|
---|
156 | $endTime->setTimestamp(intval($result['?endtime']->label));
|
---|
157 | $open = (bool) $result['?open'];
|
---|
158 | $aInstances[] = new ApplicationInstance($result['?uid']->label, $result['?of_application']->uri, $startTime, $endTime, $open, null, null, null);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return $aInstances;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static function create($obj) {
|
---|
165 | return new ApplicationInstance($obj->uid, $obj->application,
|
---|
166 | $obj->starttime, $obj->endtime, $obj->open, $obj->playerresults,
|
---|
167 | $obj->groupresults, $obj->periodicresults);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | ?>
|
---|