source: Dev/branches/rest-dojo-ui/server/classes/models/ApplicationInstance.php @ 263

Last change on this file since 263 was 263, checked in by hendrikvanantwerpen, 13 years ago
  • [Client] Finished page framework. See rft/ui/content.js or test files for details.
  • [Client] Allow login by pressing Enter.
  • [API] On exception include details in json response.
  • [Server Use Exceptions when save() fails, iso return values.
File size: 5.4 KB
Line 
1<?php
2require_once 'rdfConstants.php';
3//Include RAP Library to write to RDF files
4include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
5
6/**
7 * Description of ApplicationInstance
8 *
9 * @author jkraaijeveld
10 */
11
12class 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 = false, $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                        $result = Application::get(array("uid" => $this->application));
59                        if(!isset($result[0]))
60                                return false;
61                        $this->application = $result[0];
62                }
63
64                //TODO: Evaluate game results.
65
66
67                return true;
68        }
69
70        /**
71         * function save()
72         * Saves the current object into the database.
73         */
74        public function save()
75        {
76                //Do note save if there are invalid references in this object.
77                if(!$this->evaluate())
78                        throw new Exception('Evaluation failed.');
79
80                //Ensure the required folder exists.
81                if(!is_dir('data/applications/'))
82                        mkdir('data/applications/');
83
84                $model = ResearchToolObject::load(ApplicationInstance::$filename);
85                $resourceAI = new Resource(APPLICATIONINSTANCE.'/'.$this->uid);
86                //Remove the old value stored with the given id.
87                $model->subtract($model->find($resourceAI, null, null));
88
89                //Add the new statements to the model
90                $resourceAIType = new Resource(APPLICATIONINSTANCE);
91                $predicateRType = new Resource(RTYPE);
92                $model->add(new Statement($resourceAI, $predicateRType, $resourceAIType));
93
94                $AIUid = new Literal($this->uid);
95                $predicateUid = new Resource(UID);
96                $model->add(new Statement($resourceAI, $predicateUid, $AIUid));
97
98                $AIApplication = new Literal($this->application->uid);
99                $predicateApplication = new Resource(OF_APPLICATION);
100                $model->add(new Statement($resourceAI, $predicateApplication, $AIApplication));
101
102                $AIStartTime = new Literal($this->starttime->getTimestamp());
103                $predicateStartTime = new Resource(STARTTIME);
104                $model->add(new Statement($resourceAI, $predicateStartTime, $AIStartTime));
105
106                $AIEndTime = new Literal($this->endtime->getTimestamp());
107                $predicateEndTime = new Resource(ENDTIME);
108                $model->add(new Statement($resourceAI, $predicateEndTime, $AIEndTime));
109
110                $AIOpen = new Literal($this->open);
111                $predicateOpen = new Resource(OPEN);
112                $model->add(new Statement($resourceAI, $predicateOpen, $AIOpen));
113
114                $model->saveAs(ApplicationInstance::$filename, 'rdf');
115                return true;
116                //TODO: Game results.
117        }
118
119        /**
120         * function get($arguments)
121         * Gets the array of ApplicationInstance objects belonging to the arguments supplied.
122         * @param type $arguments : An array containing zero or more of the following keys:
123         *                                                      'uid', 'of_application', 'starttime', 'endtime', 'open'
124         * TODO: Support game results.
125         */
126        public static function get($arguments)
127        {
128                $model = ResearchToolObject::load(ApplicationInstance::$filename);
129
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
148                //Create the corresponding ApplicationInstance objects
149                $aInstances = array();
150                if(!empty($results))
151                {
152                        foreach($results as $result)
153                        {
154                                $startTime = new DateTime();
155                                $startTime->setTimestamp(intval($result['?starttime']->label));
156                                $endTime = new DateTime();
157                                $endTime->setTimestamp(intval($result['?endtime']->label));
158                                $open = false;
159                                if ($result['?open'] == 'true')
160                                        $open = true;
161
162                                $aInstances[] = new ApplicationInstance($result['?uid']->label, $result['?of_application']->label, $startTime, $endTime, $open, null, null, null);
163                        }
164                }
165                return $aInstances;
166
167
168        }
169}
170?>
Note: See TracBrowser for help on using the repository browser.