source: Dev/branches/rest-dojo-ui/server/classes/models/ResultSet.php @ 256

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

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.6 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                        return false;
75                //Ensure the required folder exists.
76                if(!is_dir('data/results/'))
77                        mkdir('data/results/');
78
79                $model = ResearchToolObject::load(ResultSet::$filename);
80
81                $resourceResultSet = new Resource(RESULTSET . '/' . $this->uid);
82                //Remove the old value stored with the given id
83                $model->subtract($model->find($resourceResultSet, null, null));
84
85                //Set new values
86                $resourceResultSetType = new Resource(RESULTSET);
87                $predicateRType = new Resource(RTYPE);
88                $model->add(new Statement($resourceResultSet, $predicateRType, $resourceResultSetType));
89
90                $resultSetId = new Literal($this->uid);
91                $predicateId = new Resource(UID);
92                $model->add(new Statement($resourceResultSet, $predicateId, $resultSetId));
93
94                if(isset($this->answersets))
95                {
96                        foreach($this->answersets as $answerset)
97                        {
98                                $answerSetId = new Literal($answerset->uid);
99                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
100                                $model->add(new Statement($resourceResultSet, $predicateAnswerSet, $answerSetId));
101                        }
102                }
103
104                //TODO: store other sets when they are implemented.
105
106                $model->saveAs(ResultSet::$filename, 'rdf');
107                return true;
108        }
109
110        /**
111         * function get()
112         * @param type $arguments : An array having one or more of the following elements:
113         * 'uid', 'answersets', 'playerresults', 'groupresults', 'periodicresults'
114         */
115        public static function get($arguments)
116        {
117                $model = ResearchToolObject::load(ResultSet::$filename);
118                //Build the query string
119                $querystring = '
120                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
121                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
122                        SELECT DISTINCT ?uid
123                        WHERE
124                        {
125                                _resultset predicates:resource_type resources:resultset ;
126                                predicates:uid ?uid ;
127                                ' . ResearchToolObject::createArguments($arguments) . '
128                        }';
129                //Query the model
130                $results = $model->sparqlQuery($querystring);
131                $resultSets = array();
132                if(!empty($results))
133                {
134                        foreach($results as $result)
135                        {
136                                $answerSets = ResultSet::getAnswerSets($model, $result['?uid']->label);
137                                $resultSets[] = new ResultSet($result['?uid']->label, $answerSets, array(), array(), array());
138                        }
139                }
140                return $resultSets;
141        }
142
143        /**
144         * function getAnswerSets()
145         * @param type $uid : The ResultSet uid for which the answerSets should be retrieved.
146         */
147        private static function getAnswerSets($model, $uid)
148        {
149                $result = $model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
150                $iterator = $result->getStatementIterator();
151                $answersets = array();
152                while($iterator->hasNext())
153                {
154                        $element = $iterator->next();
155                        $answersets[] = $element->getLabelObject();
156                }
157                return $answersets;
158        }
159}
Note: See TracBrowser for help on using the repository browser.