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

Last change on this file since 289 was 289, checked in by hendrikvanantwerpen, 13 years ago

[merge] svn merge ../jos-branch -r 284:287 .

File size: 5.8 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 = 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                $model->writeAsHTMLTable();
131
132                //Build the query string
133                $querystring = '
134                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
135                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
136                        SELECT DISTINCT ?uid, ?of_application, ?starttime, ?endtime, ?open
137                        WHERE
138                        {
139                                ?applicationinstance    predicates:resource_type        resources:applicationinstance ;
140                                predicates:uid ?uid ;
141                                predicates:of_application ?of_application ;
142                                predicates:starttime ?starttime ;
143                                predicates:endtime ?endtime ;
144                                predicates:open ?open ;
145                        ' . ResearchToolObject::createArguments($arguments) . '
146        }';
147                //Query the model
148                $results = $model->sparqlQuery($querystring);
149                //Create the corresponding ApplicationInstance objects
150                $aInstances = array();
151                if(!empty($results))
152                {
153                        foreach($results as $result)
154                        {
155                                $startTime = new DateTime();
156                                $startTime->setTimestamp(intval($result['?starttime']->label));
157                                $endTime = new DateTime();
158                                $endTime->setTimestamp(intval($result['?endtime']->label));
159                                $open = (bool) $result['?open'];
160                                $aInstances[] = new ApplicationInstance($result['?uid']->label, $result['?of_application']->uri, $startTime, $endTime, $open, null, null, null);
161                        }
162                }
163                return $aInstances;
164
165
166        }
167
168    public static function create($obj) {
169        return new ApplicationInstance($obj->uid, $obj->application,
170                $obj->starttime, $obj->endtime, $obj->open, $obj->playerresults,
171                $obj->groupresults, $obj->periodicresults);
172    }
173}
174?>
Note: See TracBrowser for help on using the repository browser.