source: Dev/branches/jos-branch/server/classes/models/Survey.php @ 298

Last change on this file since 298 was 298, checked in by jkraaijeveld, 13 years ago

[Server] Made the models compliant with the Visio design in terms of ApplicationInstance?, SurveyInstance?, SessionInstance? and all of their fields.
[Server] Now stores references as RDF Resources for all models.
[Server] Changed some possible get() arguments to be more consistent.

File size: 5.7 KB
Line 
1<?php
2
3/**
4 * Description of Survey
5 *
6 * @author jkraaijeveld
7 */
8class Survey extends ResearchToolObject{
9        private static $filename = 'data/surveys/surveys.rdf';
10
11    public $title;
12    public $description;
13    public $creator;
14    public $questions;
15   
16    public function __construct($uid = null, $title = null, $description = null, $creator = null, $questions = null) {
17        if(!isset($uid))
18        {
19            $uid = md5(uniqid(rand(), true));
20        }
21        $this->uid = $uid;
22        $this->title = $title;
23                $this->description = $description;
24                $this->creator = $creator;
25        $this->questions = $questions;
26        }
27
28        /**
29         * function evaluate()
30         * Evaluates the references
31         */
32        public function evaluate()
33        {
34
35                if(is_string($this->creator))
36                {
37                        $crea = ResearchToolObject::stripUri($this->creator);
38                        $result = User::get(array("uid" => $crea["uid"]));     
39                        if(!isset($result[0]))
40                                return false;
41                        $this->creator = $result[0];
42                }
43
44                if(!empty($this->questions) && is_string($this->questions[0]))
45                {
46                        $newQuestions = array();
47                        foreach($this->questions as $question)
48                        {
49                                $ques = ResearchToolObject::stripUri($question);
50                                $result = Question::get(array("uid" => $ques["uid"]));
51                                if(!isset($result[0]))
52                                        return false;
53                                $newQuestions[] = $result[0];                   
54                        }
55                        $this->questions = $newQuestions;
56                }
57                return true;
58        }
59
60        /**
61         * function save()
62         * Saves the current object into the database.
63         */
64    public function save()
65    {
66                //If evaluation fails, some references are incorrect.
67                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
68                //TODO: Decide how to fix invalid references graciously.
69                if(!$this->evaluate())
70                        throw new Exception('Evaluation failed.');
71
72                if(!is_dir('data/surveys/'))
73                        mkdir('data/surveys/');
74
75                $model = ResearchToolObject::load(Survey::$filename);
76
77        $resourceSurvey = new Resource(SURVEY.'/'.$this->uid);
78        //Remove the old value stored with the given id
79        $model->subtract($model->find($resourceSurvey, null, null));
80       
81        $resourceSurveyType = new Resource(SURVEY);
82        $predicateRType = new Resource(RTYPE);
83        $model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
84
85        $predicateUniqueID = new Resource(UID);
86        $literalSurveyID = new Literal($this->uid);
87        $model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
88
89        $predicateTitle = new Resource(TITLE);         
90        $surveyTitle = new Literal($this->title);
91        $model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));               
92
93        $predicateDescription = new Resource(DESCRIPTION);
94        $surveyDescription = new Literal($this->description);
95        $model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
96       
97                $predicateCreator = new Resource(CREATOR);
98                $surveyCreator = new Resource(USER . '/' . $this->creator->uid);
99                $model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
100
101        if(isset($this->questions))
102        {
103            foreach($this->questions as $question)
104            {
105                                $questionUid = new Resource(QUESTION . '/' . $question->uid);
106                $predicateQuid = new Resource(HAS_QUESTION);
107                $model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
108            }
109                }
110                $model->saveAs(Survey::$filename, 'rdf');
111                return true;
112        }
113
114    /**
115     * function get($arguments)
116     * Gets the array of Survey objects belonging to arguments supplied.
117     * @param type $arguments : An array containing zero or more of the following keys:
118     *                          'uid', 'title', 'description', 'questions'
119     * Note: questions has to be an array of question IDs (codes) for this filter to work.
120     */
121    public static function get($arguments)
122    {
123                $model = ResearchToolObject::load(Survey::$filename);
124       
125        //Build the query string
126        $querystring = '
127            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
128            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
129            SELECT DISTINCT ?uid, ?title, ?description, ?creator
130            WHERE
131            {
132                _survey     predicates:resource_type    resources:survey ;
133                predicates:uid ?uid ;
134                predicates:title ?title ;
135                                predicates:description ?description ;
136                                predicates:creator ?creator ;
137                                ' . ResearchToolObject::createArguments($arguments) . '
138            }';
139       
140        //Query the model
141        $results = $model->sparqlQuery($querystring);
142       
143        //Create the corresponding Survey objects
144        $surveys = array();
145        if(!empty($results))
146        {
147            foreach($results as $result)
148            {
149                                $questions = Survey::getQuestions($model, $result['?uid']->label);
150                                $creator = $result['?creator']->uri;
151                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator, $questions);
152            }
153        }
154        return $surveys;
155    }
156   
157    /**
158     * Gets the questions corresponding to the survey
159     * @param type $uid : The uid for which the questions should be gotten.
160     */
161    private static function getQuestions($model, $uid)
162    {
163                $result = $model->findRegex("[(".$uid.")]", "[(has_question)]", null);
164                $iterator = $result->getStatementIterator();
165                $questions = array();
166                while($iterator->hasNext())
167                {
168                        $element = $iterator->next();
169                        $questions[] = $element->getLabelObject();
170                }
171                return $questions;
172    }
173
174    public static function create($obj) {
175        return new Survey($obj->uid, $obj->title, $obj->description,
176                $obj->creator, $obj->questions);
177    }
178
179}
180
181?>
Note: See TracBrowser for help on using the repository browser.