source: Dev/branches/rest-dojo-ui/server/classes/models/ResultSet.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: 4.7 KB
Line 
1<?php
2// Survey database interface class as intermediate for storing data from the site to the RDF database
3require_once 'rdfConstants.php';
4// Include RAP Library to write RDF files
5include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
6
7/**
8 * Description of ResultSet
9 */
10class ResultSet extends ResearchToolObject{
11
12        private static $filename = 'data/results/resultsets.rdf';
13
14        public $answersets;
15        public $playerresults;
16        public $groupresults;
17        public $periodicresults;
18
19        /**
20         * Constructor for an ResultSet object.
21         * @param type $uid
22         * @param type $answersets;
23         * @param type $playerresults;
24         * @param type $groupresults;
25         * @param type $periodicresults;
26         */
27        public function __construct($uid=null, $answersets = null, $playerresults = null, $groupresults = null, $periodicresults = null)
28        {
29                if(!isset($uid))
30                {
31                        $uid = md5(uniqid(rand(), true));
32                }
33               
34                $this->uid = $uid;
35                $this->answersets = $answersets;
36                $this->playerresults = $playerresults;
37                $this->groupresults = $groupresults;
38                $this->periodicresults = $periodicresults;
39        }
40
41        /**
42         * funtion evaluate()
43         * evaluates the references of all results
44         */
45        public function evaluate()
46        {
47                if(!empty($this->answersets) && is_string($this->answersets[0]))
48                {
49                        $newAnswerSets = array();
50                        foreach($this->answersets as $answerSet)
51                        {
52                                $result = AnswerSet::get(array("uid" => $answerSet));
53                                if(!isset($result[0]))
54                                        return false;
55                                $newAnswerSets[] = $result[0];
56                        }                       
57                        $this->answersets = $newAnswerSets;
58                }
59                //TODO: Evaluate the other sets when they are implemented.
60
61                return true;
62        }
63
64        /**
65         * function save()
66         * Saves the current object into the database.
67         */
68        public function save()
69        {
70                //If evaluation fails, some references are incorrect.
71                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
72                //TODO: Decide how to fix invalid references graciously.
73                if(!$this->evaluate())
74                        throw new Exception('Evaluation failed.');
75
76        //Ensure the required folder exists.
77                if(!is_dir('data/results/'))
78                        mkdir('data/results/');
79
80                $model = ResearchToolObject::load(ResultSet::$filename);
81
82                $resourceResultSet = new Resource(RESULTSET . '/' . $this->uid);
83                //Remove the old value stored with the given id
84                $model->subtract($model->find($resourceResultSet, null, null));
85
86                //Set new values
87                $resourceResultSetType = new Resource(RESULTSET);
88                $predicateRType = new Resource(RTYPE);
89                $model->add(new Statement($resourceResultSet, $predicateRType, $resourceResultSetType));
90
91                $resultSetId = new Literal($this->uid);
92                $predicateId = new Resource(UID);
93                $model->add(new Statement($resourceResultSet, $predicateId, $resultSetId));
94
95                if(isset($this->answersets))
96                {
97                        foreach($this->answersets as $answerset)
98                        {
99                                $answerSetId = new Literal($answerset->uid);
100                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
101                                $model->add(new Statement($resourceResultSet, $predicateAnswerSet, $answerSetId));
102                        }
103                }
104
105                //TODO: store other sets when they are implemented.
106
107                $model->saveAs(ResultSet::$filename, 'rdf');
108                return true;
109        }
110
111        /**
112         * function get()
113         * @param type $arguments : An array having one or more of the following elements:
114         * 'uid', 'answersets', 'playerresults', 'groupresults', 'periodicresults'
115         */
116        public static function get($arguments)
117        {
118                $model = ResearchToolObject::load(ResultSet::$filename);
119                //Build the query string
120                $querystring = '
121                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
122                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
123                        SELECT DISTINCT ?uid
124                        WHERE
125                        {
126                                _resultset predicates:resource_type resources:resultset ;
127                                predicates:uid ?uid ;
128                                ' . ResearchToolObject::createArguments($arguments) . '
129                        }';
130                //Query the model
131                $results = $model->sparqlQuery($querystring);
132                $resultSets = array();
133                if(!empty($results))
134                {
135                        foreach($results as $result)
136                        {
137                                $answerSets = ResultSet::getAnswerSets($model, $result['?uid']->label);
138                                $resultSets[] = new ResultSet($result['?uid']->label, $answerSets, array(), array(), array());
139                        }
140                }
141                return $resultSets;
142        }
143
144        /**
145         * function getAnswerSets()
146         * @param type $uid : The ResultSet uid for which the answerSets should be retrieved.
147         */
148        private static function getAnswerSets($model, $uid)
149        {
150                $result = $model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
151                $iterator = $result->getStatementIterator();
152                $answersets = array();
153                while($iterator->hasNext())
154                {
155                        $element = $iterator->next();
156                        $answersets[] = $element->getLabelObject();
157                }
158                return $answersets;
159        }
160}
Note: See TracBrowser for help on using the repository browser.