source: Dev/trunk/classes/SurveyAnswerRDFWriter.php @ 83

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

start with answer saving

File size: 2.5 KB
Line 
1<?php
2
3class SurveyAnswerRDFWriter
4{
5    protected $surveyID;
6    protected $respondentID;
7    protected $path;
8
9    public function __construct($surveyID,$sessionID,$respondentID)
10    {
11        // Create empty MemModel
12        $factory = new ModelFactory();
13        $this->model = $factory->getDefaultModel();
14       
15        $basePath = 'data/sessions/';
16        $sessionPath = $basePath . $sessionID . '/';
17        $this->path  = $sessionPath .'survey_'.$surveyID . '/';
18        $this->surveyID = $surveyID;
19        $this->respondentID = $respondentID;
20       
21        if (!is_dir($basePath))
22            mkdir($basePath);
23        if (!is_dir($sessionPath))
24             mkdir($sessionPath);
25        if (!is_dir($this->path))
26              mkdir($this->path);
27    }
28
29    public function saveSurveyData()
30    {           
31        $this->model->saveAs($this->path.'/answer_'.$this->respondentID.'.rdf','rdf');
32    }
33
34    public function setRespondentData($name)
35    {                           
36        $resourceUser = new Resource(USER . '/' . $this->respondentID);         
37
38        $resourceUserType = new Resource(USER);
39        $predicateRType = new Resource(RTYPE);
40        $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType));
41
42        $literalUserName = new Literal($name);
43        $predicateName = new Resource(NAME);
44        $this->model->add(new Statement($resourceUser,$predicateName,$literalUserName));               
45
46        $literalUserID = new Literal($this->respondentID);
47        $predicateUniqueID = new Resource(UID);
48        $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID));
49
50        $resourceSurvey = new Resource(SURVEY.'/'.$this->surveyID);
51        $predicateRespondent = new Resource(RESPONDENT);
52        $this->model->add(new Statement($resourceSurvey,$predicateRespondent,$resourceUser));
53       
54        $this->saveSurveyData();
55    }
56
57    public function setAnswers($answers)
58    {
59        foreach($answers as $questionID => $answerValue)
60        {
61            $resourceQuestion = new Resource(QUESTION.'/'.$questionID);
62
63            $resourceQuestionType = new Resource(QUESTION);
64            $predicateRType = new Resource(RTYPE);
65            $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
66           
67            $predicateAnswered = new Resource(ANSWERED);       
68            $answer = new Literal($answerValue);
69            $this->model->add(new Statement($resourceQuestion,$predicateAnswered,$answer));
70        }
71    }
72}
73
74?>
Note: See TracBrowser for help on using the repository browser.