source: Dev/branches/jos-branch/classes/models/Question.php @ 233

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