Changeset 26 for Dev


Ignore:
Timestamp:
07/20/11 18:05:04 (14 years ago)
Author:
basvannuland
Message:

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.

Location:
Dev/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/classes/SurveyRDFReader.php

    r16 r26  
    44{
    55        var $model;
    6                
    7         public function SurveyRDFReader()
    8         {
     6       
     7        var $surveyUID;
     8       
     9    public function __construct($surveyUID)
     10    {
    911        // Create empty MemModel
    1012                $factory = new ModelFactory();
    11                 $this->model = $factory->getDefaultModel();     
     13                $this->model = $factory->getDefaultModel();
     14               
     15                $this->surveyUID = $surveyUID;
    1216        }
    1317       
    14         public function loadSurvey($sTitle)
     18        public function loadSurvey()
    1519        {
    16                 $this->model->load('surveys/'.$sTitle.'.rdf');
     20                $this->model->load('surveys/'.$this->surveyUID.'.rdf');
    1721        }
    1822       
    19         public function readSurveyTitle()
     23        public function getSurveyInfo()
     24        {               
     25                SurveyRDFReader::loadSurvey();
     26       
     27                $result = array();
     28                $result[] = $this->readSurveyInfo();                           
     29                $result[] = $this->readSurveyQuestions();
     30               
     31                return $result;
     32        }
     33       
     34        public function readSurveyInfo()
    2035        {
    2136                $querystring = '
    22                         PREFIX  predicates: <' .SURVEYTOOL_PREDICATES_NAMESPACE . '>
     37                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    2338                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    24                         SELECT  ?title
    25                         WHERE   { resources:survey predicates:title ?title }';
     39                        SELECT  ?title ?description
     40                        WHERE   
     41                        {
     42                                _survey         predicates:resource_type        resources:survey ;
     43                                                        predicates:title                        ?title ;
     44                                                        predicates:description          ?description
     45                        }';
     46                       
     47                $result = $this->model->sparqlQuery($querystring);
     48                               
     49                return $result;
     50        }
     51       
     52        public function readSurveyQuestions()
     53        {
     54                $result = array();
     55                $result[] = $this->readSurveyQuestionsTitle();                         
     56                $result[] = $this->readSurveyQuestionsDescription();
     57                $result[] = $this->readSurveyQuestionsType();
     58                $result[] = $this->readSurveyQuestionsID();             
     59                               
     60                return $result;
     61        }
     62       
     63        public function readSurveyQuestionsTitle()
     64        {
     65                $querystring = '
     66                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     67                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     68                        SELECT  ?questionTitle
     69                        WHERE
     70                        {       
     71                                _question       predicates:resource_type        resources:question ;
     72                                                        predicates:title                        ?questionTitle         
     73                        }';
    2674                       
    2775                $result = $this->model->sparqlQuery($querystring);
     
    3078        }
    3179       
    32         public function readSurveyDescription()
     80        public function readSurveyQuestionsDescription()
    3381        {
    3482                $querystring = '
    35                         PREFIX  predicates: <' .SURVEYTOOL_PREDICATES_NAMESPACE . '>
     83                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    3684                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    37                         SELECT  ?description
    38                         WHERE   { resources:survey predicates:description ?description }';
     85                        SELECT  ?questionDescription
     86                        WHERE
     87                        {       
     88                                _question       predicates:resource_type        resources:question ;
     89                                                        predicates:description  ?questionDescription           
     90                        }';
     91                       
     92                $result = $this->model->sparqlQuery($querystring);
     93               
     94                return $result;
     95        }
     96       
     97        public function readSurveyQuestionsType()
     98        {
     99                $querystring = '
     100                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     101                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     102                        SELECT  ?questionType
     103                        WHERE
     104                        {       
     105                                _question       predicates:resource_type        resources:question ;
     106                                                        predicates:question_type        ?questionType                           
     107                        }';
     108                       
     109                $result = $this->model->sparqlQuery($querystring);
     110               
     111                return $result;
     112        }
     113       
     114        public function readSurveyQuestionsID()
     115        {
     116                $querystring = '
     117                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     118                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     119                        SELECT  ?questionID
     120                        WHERE
     121                        {       
     122                                _question       predicates:resource_type        resources:question ;
     123                                                        predicates:uid                          ?questionID                             
     124                        }';
     125                       
     126                $result = $this->model->sparqlQuery($querystring);
     127               
     128                return $result;
     129        }
     130       
     131        public function readSurveyAnswers($qID)
     132        {
     133                $querystring = '
     134                        PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
     135                        PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
     136                        SELECT  ?answerTitle
     137                        WHERE
     138                        {
     139                                _question               predicates:resource_type        resources:question ;
     140                                                                predicates:uid                          "' . $qID . '"  ;
     141                                                                predicates:has_answer           _answer .
     142                                _answer                 predicates:title                        ?answerTitle                                                           
     143                        }';
    39144                       
    40145                $result = $this->model->sparqlQuery($querystring);
  • Dev/trunk/classes/SurveyRDFWriter.php

    r16 r26  
    66               
    77        var $resourceSurvey;
    8     /**
    9     * Use SurveyRDFWriter::getInstance() instead of this
    10     * constructor.
    11     */
    12     public function __construct()
     8        var $surveyUID;
     9       
     10    public function __construct($surveyUID)
    1311    {
    1412        // Create empty MemModel
     
    1614                $this->model = $factory->getDefaultModel();
    1715               
    18                 $this->initResources();
     16                $this->surveyUID = $surveyUID;
     17               
    1918    }
    20 
    21         function initResources()
    22         {
    23                
    24                 $this->resourceSurvey = new Resource(SURVEY);
    25         }
    2619       
    2720        public function createSurvey($sTitle, $sDescription)
    2821        {               
     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));
    2931               
    3032                $predicateTitle = new Resource(TITLE);         
     
    3436                $predicateDescription = new Resource(DESCRIPTION);
    3537                $surveyDescription = new Literal($sDescription);
    36                 $this->model->add(new Statement($this->resourceSurvey,$predicateDescription,$surveyDescription));               
     38                $this->model->add(new Statement($this->resourceSurvey,$predicateDescription,$surveyDescription));
     39               
    3740        }
    3841       
    3942        public function addQuestion($qTitle,$qDescription,$qType,$qAnswers)
    4043        {
    41                 $resourceQuestion = new Resource(QUESTION);
    42                 $predicateQuestion = new Resource(HAS_QUESTION);
    43                 $this->model->add(new Statement($this->resourceSurvey,$predicateQuestion,$resourceQuestion));
    44                
     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                               
    4555                $predicateTitle = new Resource(TITLE);         
    4656                $questionTitle = new Literal($qTitle);
     
    5161                $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));         
    5262               
    53                 $predicateType = new Resource(TYPE);
    54                 $resourceQuestionType = new Resource(QUESTION_TYPE);
    55                 $this->model->add(new Statement($resourceQuestion,$predicateType,$resourceQuestionType));
     63                $predicateQType = new Resource(QTYPE);
     64                $resourceQuestionType = new Literal($qType);
     65                $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
    5666               
    5767                foreach($qAnswers as $answer)
    58                 {
    59                         $resourceAnswer = new Resource(ANSWER);
    60                         $predicateAnswer = new Resource(HAS_ANSWER);   
    61                         $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$resourceAnswer));
    62                        
    63                         $predicateTitle = new Resource(TITLE);
     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                                               
    6480                        $answerTitle = new Literal($answer['Title']);
    6581                        $this->model->add(new Statement($resourceAnswer,$predicateTitle,$answerTitle));
    6682                       
    67                         $predicateDescription = new Resource(DESCRIPTION);
    6883                        $answerDescription = new Literal($answer['Description']);
    6984                        $this->model->add(new Statement($resourceAnswer,$predicateDescription,$answerDescription));
    7085                       
     86                        $predicateAnswer = new Resource(HAS_ANSWER);   
     87                        $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$resourceAnswer));
    7188                }
    7289               
     90                $predicateQuestion = new Resource(HAS_QUESTION);
     91                $this->model->add(new Statement($this->resourceSurvey,$predicateQuestion,$resourceQuestion));
    7392               
    7493        }
    7594       
    76         public function saveSurvey($sTitle)
    77         {
    78                 $this->model->saveAs('surveys/'.$sTitle.'.rdf','rdf');
    79                
    80                 return true;
     95        public function saveSurvey()
     96        {       
     97                $this->model->saveAs('surveys/'.$this->surveyUID.'.rdf','rdf');
    8198        }
    8299
  • Dev/trunk/rdfConstants.php

    r16 r26  
    1616
    1717// Predicates used for RDF triples
     18define('UID',SURVEYTOOL_PREDICATES_NAMESPACE . 'uid');
    1819define('CREATOR',SURVEYTOOL_PREDICATES_NAMESPACE . 'creator');
    1920define('TITLE',SURVEYTOOL_PREDICATES_NAMESPACE . 'title');
    2021define('DESCRIPTION',SURVEYTOOL_PREDICATES_NAMESPACE . 'description');
    21 define('TYPE',SURVEYTOOL_PREDICATES_NAMESPACE . 'type');
     22define('RTYPE',SURVEYTOOL_PREDICATES_NAMESPACE . 'resource_type');
     23define('QTYPE',SURVEYTOOL_PREDICATES_NAMESPACE . 'question_type');
    2224define('HAS_QUESTION',SURVEYTOOL_PREDICATES_NAMESPACE . 'has_question');
    2325define('HAS_ANSWER',SURVEYTOOL_PREDICATES_NAMESPACE . 'has_answer');
  • Dev/trunk/submitsurvey.php

    r17 r26  
    1111
    1212$sTitle = $_POST['surveyTitle'];
     13$sDesription = $_POST['surveyDescription'];
    1314
    14 $surveyRDFWriter->createSurvey($sTitle,$_POST['surveyDescription']);
     15$surveyRDFWriter->createSurvey($sTitle,$sDesription);
    1516
    16 $done = $surveyRDFWriter->saveSurvey($sTitle);
     17$qNumber = 1;
     18do
     19{
     20        $qTitle = $_POST['questionTitle'.$qNumber];
     21        $qDescription = $_POST['questionDescription'.$qNumber];
     22        $qType = $_POST['questionType'.$qNumber];
     23
     24        if ($qTitle != null)
     25        {
     26                $qaArray = array();
     27                $aNumber = 1;
     28                do
     29                {
     30                        $aTitle = $_POST['q'.$qNumber.'ans'.$aNumber];
     31                        if ($aTitle != null)
     32                        {
     33                                $aArray = array();
     34                                $aArray['Title'] = $aTitle;
     35                                $aArray['Description'] = 'answerDescription'.$aNumber;
     36                                $qaArray[] = $aArray;
     37                        }
     38                        $aNumber++;
     39                } while ($aTitle != null);
     40                               
     41                $qNumber++;
     42                $surveyRDFWriter->addQuestion($qTitle,$qDescription,$qType,$qaArray);
     43        }
     44} while($qTitle != null);
     45
     46
     47$surveyRDFWriter->saveSurvey($sTitle);
    1748
    1849echo 'Done<br/><br/>';
     
    2758  $value = $line['?title'];
    2859    if($value != "")
    29       echo $value->toString()."<br/>";
     60      echo $value->toString().'<br/>';
    3061    else
    31       echo "undbound<br/>";
     62      echo 'undbound<br/>';
    3263}
    3364
     
    3768  $value = $line['?description'];
    3869    if($value != "")
    39       echo $value->toString()."<br/>";
     70      echo $value->toString().'<br/>';
    4071    else
    41       echo "undbound<br/>";
     72      echo 'undbound<br/>';
    4273}
    4374
     75$result3 = $surveyRDFReader->readSurveyQuestions($sTitle);
     76
     77if($result3 != null)
     78{
     79        var_dump($result3);
     80         
     81        foreach($result3 as $line){
     82          $value = $line['?questionTitle'];
     83                if($value != "")
     84                  echo '<br/>'.'<br/>'.$value->toString().'<br/>';
     85                else
     86                  echo 'undbound<br/>';
     87        }
     88} else
     89{
     90        echo '<"Literal("'.$sTitle.'")"> empty result <br/>';
     91}
     92echo '<br/>';
    4493var_dump($_POST);
    4594
  • Dev/trunk/surveycreation.php

    r24 r26  
    22
    33if (isset($_POST['timeStamp']))
     4{
    45    $timeStamp = $_POST['timeStamp'];
     6       
     7        var_dump($_POST);
     8        echo '<br/><br/>';
     9        $surveyDBI = SurveyDatabaseInterface::getInstance();
     10        $surveyDBI->setSurveyInfo($_POST);
     11        $info = $surveyDBI->getSurveyInfo();
     12        echo '<br/><br/>';
     13        var_dump($info);
     14}
    515else {
    616    $timeStamp = null;
    717}
     18
    819
    920?>
Note: See TracChangeset for help on using the changeset viewer.