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

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

Added new support for Answer, AnswerSet?, Question and Survey

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