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