source: Dev/trunk/classes/SurveyRDFWriter.php @ 26

Last change on this file since 26 was 26, checked in by basvannuland, 14 years ago

made some changes to way rdf database is constructed.
Made an interface class to write to db and read from db.
interface returns array with survey information in the same way it gets it from the surveyCreationTool.
TODO, override existing survey when it is modified.

File size: 3.5 KB
Line 
1<?php
2
3class SurveyRDFWriter
4{
5        var $model;
6               
7        var $resourceSurvey;
8        var $surveyUID;
9       
10    public function __construct($surveyUID)
11    {
12        // Create empty MemModel
13                $factory = new ModelFactory();
14                $this->model = $factory->getDefaultModel();
15               
16                $this->surveyUID = $surveyUID;
17               
18    }
19       
20        public function createSurvey($sTitle, $sDescription)
21        {               
22                $this->resourceSurvey = new Resource(SURVEY.'/'.$this->surveyUID);
23               
24                $resourceSurveyType = new Resource(SURVEY);
25                $predicateRType = new Resource(RTYPE);
26                $this->model->add(new Statement($this->resourceSurvey,$predicateRType,$resourceSurveyType));
27               
28                $literalSurveyID = new Literal($this->surveyUID);
29                $predicateUniqueID = new Resource(UID);
30                $this->model->add(new Statement($this->resourceSurvey,$predicateUniqueID,$literalSurveyID));
31               
32                $predicateTitle = new Resource(TITLE);         
33                $surveyTitle = new Literal($sTitle);
34                $this->model->add(new Statement($this->resourceSurvey,$predicateTitle,$surveyTitle));           
35               
36                $predicateDescription = new Resource(DESCRIPTION);
37                $surveyDescription = new Literal($sDescription);
38                $this->model->add(new Statement($this->resourceSurvey,$predicateDescription,$surveyDescription));
39               
40        }
41       
42        public function addQuestion($qTitle,$qDescription,$qType,$qAnswers)
43        {
44                $questionID = md5( uniqid(rand(), true) );
45                $resourceQuestion = new Resource(QUESTION.'/'.$questionID);
46                               
47                $resourceQuestionType = new Resource(QUESTION);
48                $predicateRType = new Resource(RTYPE);
49                $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
50                               
51                $predicateUniqueID = new Resource(UID);
52                $questionUID = new Literal($questionID);
53                $this->model->add(new Statement($resourceQuestion,$predicateUniqueID,$questionUID));
54                               
55                $predicateTitle = new Resource(TITLE);         
56                $questionTitle = new Literal($qTitle);
57                $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));     
58               
59                $predicateDescription = new Resource(DESCRIPTION);
60                $questionDescription = new Literal($qDescription);
61                $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
62               
63                $predicateQType = new Resource(QTYPE);
64                $resourceQuestionType = new Literal($qType);
65                $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
66               
67                foreach($qAnswers as $answer)
68                {               
69                        $answerID = md5( uniqid(rand(), true) );       
70                        $resourceAnswer = new Resource(ANSWER.'/'.$answerID);
71                               
72                        $resourceAnswerType = new Resource(ANSWER);
73                        $predicateRType = new Resource(RTYPE);
74                        $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); 
75
76                        $predicateUniqueID = new Resource(UID);
77                        $answerUID = new Literal($answerID);
78                        $this->model->add(new Statement($resourceAnswer,$predicateUniqueID,$answerUID));                       
79                                               
80                        $answerTitle = new Literal($answer['Title']);
81                        $this->model->add(new Statement($resourceAnswer,$predicateTitle,$answerTitle));
82                       
83                        $answerDescription = new Literal($answer['Description']);
84                        $this->model->add(new Statement($resourceAnswer,$predicateDescription,$answerDescription));
85                       
86                        $predicateAnswer = new Resource(HAS_ANSWER);   
87                        $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$resourceAnswer));
88                }
89               
90                $predicateQuestion = new Resource(HAS_QUESTION);
91                $this->model->add(new Statement($this->resourceSurvey,$predicateQuestion,$resourceQuestion));
92               
93        }
94       
95        public function saveSurvey()
96        {       
97                $this->model->saveAs('surveys/'.$this->surveyUID.'.rdf','rdf');
98        }
99
100}
101
102?>
Note: See TracBrowser for help on using the repository browser.