source: Dev/branches/rest-dojo-ui/server/classes/models/Question.php @ 311

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

Merged changes from jos-branch:268,269 to rest-dojo-ui.

File size: 5.8 KB
Line 
1<?php
2require_once 'rdfConstants.php';
3// Include RAP Library to write RDF files
4include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
5
6/**
7 * Description of Question
8 *
9 * @author jkraaijeveld
10 */
11class Question extends ResearchToolObject{   
12
13        /**
14         * static function create()
15         * @param type $obj : The object which should be converted to a Question
16         */
17        public static function create($obj)
18        {
19                return new Question($obj->uid, $obj->code, $obj->title, $obj->type, $obj->description, $obj->category, $obj->answers); 
20        }
21
22
23        private static $filename = 'data/questions/questions.rdf';
24
25    public $code;
26    public $title;
27    public $type;
28    public $description;
29    public $category;
30    public $answers; // format answers['#']
31
32
33    /**
34     * Constructor for a Question. $uid equals the corresponding code.
35     * @param type $uid
36     * @param type $title
37     * @param type $type
38     * @param type $description
39     * @param type $category
40     * @param type $answers
41     */
42        public function __construct($uid = null, $code = null, $title = null, $type = null, $description = null, $category = null, $answers = null)
43    {
44                if(!isset($uid))
45                {
46                        $uid = md5(uniqid(rand(), true));
47                }
48        $this->code = $code;
49        $this->uid = $uid;
50        $this->title = $title;
51        $this->type = $type;
52        $this->description = $description;
53        $this->category =  $category;
54        $this->answers = $answers;
55        }
56
57        /**
58         * function save()
59         * Saves the current object into the database.
60         */
61        public function save()
62        {
63        //Ensure the required folder exists
64        if (!is_dir('data/questions/'))
65            mkdir('data/questions/');   
66
67                $model = ResearchToolObject::load(Question::$filename);
68               
69                $resourceQuestion = new Resource(QUESTION.'/'.$this->uid);
70                //Remove the old value stored with the given id
71        $model->subtract($model->find($resourceQuestion, null, null));
72       
73        //Save all the new values
74        $resourceQuestionType = new Resource(QUESTION);
75        $predicateRType = new Resource(RTYPE);
76        $model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
77       
78        $questionUid = new Literal($this->uid);
79        $predicateUid = new Resource(UID);
80        $model->add(new Statement($resourceQuestion,$predicateUid,$questionUid));
81       
82        $questionQCode = new Literal($this->code);
83        $predicateQCode = new Resource(QCODE);
84        $model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
85       
86        $questionTitle = new Literal($this->title);
87        $predicateTitle = new Resource(TITLE);         
88        $model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));   
89
90        $questionDescription = new Literal($this->description);
91        $predicateDescription = new Resource(DESCRIPTION);
92        $model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));               
93
94        $resourceQuestionType = new Literal($this->type);
95        $predicateQType = new Resource(QTYPE);
96        $model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
97       
98        $questionQCategory = new Literal($this->category);
99        $predicateQCategory = new Resource(QCATEGORY);
100        $model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
101
102        if(isset($this->answers))
103        {
104            foreach($this->answers as $answer)
105            {           
106                $answerValue = new Literal($answer);
107                $predicateAnswer = new Resource(HAS_ANSWER);   
108                $model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
109            }
110        }
111                $model->saveAs(Question::$filename, 'rdf');
112                return true;
113        }
114
115    /**
116     * Get the questions corresponding to the arguments.
117         * @param type $arguments : An array having one ore more of the following elements:
118         * 'uid', 'code', 'title', 'type', 'description', 'category', 'definedanswers'.
119     */
120    public static function get($arguments)
121    {
122                $model = ResearchToolObject::load(Question::$filename);
123           
124        $querystring = '
125            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
126            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
127            SELECT DISTINCT ?uid, ?code, ?title, ?type, ?description, ?category
128            WHERE       
129            {
130                _question       predicates:resource_type        resources:question ;               
131                predicates:uid ?uid ;
132                predicates:question_code ?code ;
133                predicates:title ?title ;
134                predicates:question_type ?type ;
135                predicates:description ?description ;
136                predicates:question_category ?category ;
137                                '. ResearchToolObject::createArguments($arguments) . '
138                        }';
139
140               
141        //Create the querystring
142        $results = $model->sparqlQuery($querystring);
143       
144        $questions = array();
145        if(!empty($results))
146        {
147            foreach($results as $result)
148            {
149                                $answers = Question::getAnswers($model, $result['?uid']->label);
150                $questions[] = new Question($result['?uid']->label, $result['?code']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
151            }
152        }
153        return $questions;
154    }
155   
156    /**
157     * Gets the preset answers belonging to the given uid.
158     * @param type $uid : The uid of the Question in question (haha, pun).
159     */
160    private static function getAnswers($model, $uid)
161    {
162                $result = $model->findRegex("[(".$uid.")]", "[(has_answer)]", null);
163                $iterator = $result->getStatementIterator();
164                $answers = array();
165                while($iterator->hasNext())
166                {
167                        $element = $iterator->next();
168                        $answers[] = $element->getLabelObject();
169                }
170                return $answers;
171        }
172
173}
174
175?>
Note: See TracBrowser for help on using the repository browser.