Changeset 62 for Dev/trunk/classes
- Timestamp:
- 07/29/11 14:51:06 (14 years ago)
- Location:
- Dev/trunk/classes
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/classes/ApplicationDatabaseInterface.php
r51 r62 11 11 class ApplicationDatabaseInterface 12 12 { 13 protected $applicationRDFWriter; 14 protected $applicationRDFReader; 15 13 protected $applicationRDFWriter; 14 protected $applicationRDFReader; 16 15 17 public function __construct($surveyID) 18 { 19 if($surveyID == null) 20 $surveyUID = md5( uniqid(rand(), true) ); 21 else 22 $surveyUID = $surveyID; 23 24 $this->applicationRDFWriter = new ApplicationRDFWriter(); 25 $this->applicationRDFReader = new ApplicationRDFReader(); 16 17 public function __construct() 18 { 19 $this->applicationRDFWriter = new ApplicationRDFWriter(); 20 $this->applicationRDFReader = new ApplicationRDFReader(); 26 21 } 27 22 28 public function setApplicationInfo($application) 29 { 30 $aTitle = $application['applicationTitle']; 31 $aDescription = $application['applicationDescription']; 32 $aStyle = $application['applicationStyle']; 33 34 $this->applicationRDFWriter->setApplicationInfo($aTitle, $aDescription, $aStyle); 35 36 $this->applicationRDFWriter->saveApplications(); 37 } 38 39 public function getApplicationInfo() 40 { 41 $applicationInfo = array(); 42 43 $resultApplication = $this->applicationRDFReader->readAllApplications(); 44 45 for($aNumber = 0;$aNumber<=sizeof($resultApplication[0]);$aNumber++) 46 { 47 $applicationInfo['applicationID'] = substr($resultSurvey[0][$aNumber]['?uid'],9,strlen($resultSurvey[0][$aNumber]['?uid'])-11); 48 $applicationInfo['applicationTitle'] = substr($resultSurvey[1][$aNumber]['?title'],9,strlen($resultSurvey[0][$aNumber]['?title'])-11); 49 $applicationInfo['applicationDescription'] = substr($resultSurvey[2][$aNumber]['?description'],9,strlen($resultSurvey[0][$aNumber]['?description'])-11); 50 $applicationInfo['applicationStyle'] = substr($resultSurvey[3][$aNumber]['?style'],9,strlen($resultSurvey[0][$aNumber]['?style'])-11); 51 } 52 53 return $applicationInfo; 54 } 55 23 public function setApplicationInfo($applicationInfo) 24 { 25 $appTitle = $application['applicationTitle']; 26 $appDescription = $application['applicationDescription']; 27 $appStyle = $application['applicationStyle']; 28 29 $this->applicationRDFWriter->setApplicationInfo($appTitle, $appDescription, $appStyle); 30 31 $this->applicationRDFWriter->saveApplications(); 32 } 33 34 public function getApplicationInfo($applicationID) 35 { 36 $applicationInfo = array(); 37 38 $resultApplication = $this->applicationRDFReader->getApplicationInfo($applicationID); 39 40 $applicationInfo['applicationID'] = substr($resultApplication['?uid'],9,strlen($resultApplication['?uid'])-11); 41 $applicationInfo['applicationTitle'] = substr($resultApplication['?title'],9,strlen($resultApplication['?title'])-11); 42 $applicationInfo['applicationDescription'] = substr($resultApplication['?description'],9,strlen($resultApplication['?description'])-11); 43 $applicationInfo['applicationStyle'] = substr($resultApplication['?style'],9,strlen($resultApplication['?style'])-11); 44 45 return $applicationInfo; 46 } 47 48 public function getExistingApplications() 49 { 50 $applicationInfo = array(); 51 52 $resultApplication = $this->applicationRDFReader->readAllApplications(); 53 54 for($aNumber = 0;$aNumber<=sizeof($resultApplication[0]);$aNumber++) 55 { 56 $aID = substr($resultSurvey[0][$aNumber]['?uid'],9,strlen($resultSurvey[0][$aNumber]['?uid'])-11); 57 $aTitle = substr($resultSurvey[1][$aNumber]['?title'],9,strlen($resultSurvey[0][$aNumber]['?title'])-11); 58 $applicationInfo[$aID] = $aTitle; 59 } 60 61 return $applicationInfo; 62 } 56 63 } 57 64 ?> -
Dev/trunk/classes/ApplicationRDFReader.php
r45 r62 3 3 class ApplicationRDFReader 4 4 { 5 6 5 protected $model; 6 7 7 public function __construct() 8 8 { 9 10 11 12 13 14 15 16 9 // Create empty MemModel 10 $factory = new ModelFactory(); 11 $this->model = $factory->getDefaultModel(); 12 13 $fileName = 'data/applications/applications.rdf'; 14 15 if(file_exists($fileName)) 16 $this->model->load($fileName); 17 17 } 18 19 20 21 22 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>23 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>24 SELECT?title ?description ?style25 26 27 28 predicates:uid"' . $applicationID . '" ;29 predicates:title?title ;30 31 predicates:style?style32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>54 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>55 SELECT?uid56 57 58 59 predicates:uid?uid60 61 62 63 64 65 66 67 68 69 70 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>71 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>72 SELECT?title73 74 75 76 predicates:title?title77 78 79 80 81 82 83 84 85 86 87 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>88 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>89 SELECT?description90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>105 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>106 SELECT?style107 108 109 110 predicates:style?style111 112 113 114 115 116 18 19 public function readApplicationInfo($applicationID) 20 { 21 $querystring = ' 22 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 23 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 24 SELECT ?title ?description ?style 25 WHERE 26 { 27 _application predicates:resource_type resources:application ; 28 predicates:uid "' . $applicationID . '" ; 29 predicates:title ?title ; 30 prdeicates:description ?description ; 31 predicates:style ?style 32 }'; 33 34 $result = $this->model->sparqlQuery($querystring); 35 36 return $result; 37 } 38 39 public function readAllApplications() 40 { 41 $result = array(); 42 $result[] = $this->readApplicationIDs(); 43 $result[] = $this->readApplicationTitles(); 44 $result[] = $this->readApplicationDescriptions(); 45 $result[] = $this->readApplicationStyles(); 46 47 return $restult; 48 } 49 50 public function readApplicationIDs() 51 { 52 $querystring = ' 53 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 54 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 55 SELECT ?uid 56 WHERE 57 { 58 _application predicates:resource_type resources:application ; 59 predicates:uid ?uid 60 }'; 61 62 $result = $this->model->sparqlQuery($querystring); 63 64 return $result; 65 } 66 67 public function readApplicationTitles() 68 { 69 $querystring = ' 70 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 71 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 72 SELECT ?title 73 WHERE 74 { 75 _application predicates:resource_type resources:application ; 76 predicates:title ?title 77 }'; 78 79 $result = $this->model->sparqlQuery($querystring); 80 81 return $result; 82 } 83 84 public function readApplicationDescriptions() 85 { 86 $querystring = ' 87 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 88 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 89 SELECT ?description 90 WHERE 91 { 92 _application predicates:resource_type resources:application ; 93 prdeicates:description ?description 94 }'; 95 96 $result = $this->model->sparqlQuery($querystring); 97 98 return $result; 99 } 100 101 public function readApplicationStyles() 102 { 103 $querystring = ' 104 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 105 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 106 SELECT ?style 107 WHERE 108 { 109 _application predicates:resource_type resources:application ; 110 predicates:style ?style 111 }'; 112 113 $result = $this->model->sparqlQuery($querystring); 114 115 return $result; 116 } 117 117 } 118 118 -
Dev/trunk/classes/ApplicationRDFWriter.php
r45 r62 3 3 class ApplicationRDFWriter 4 4 { 5 6 5 protected $model; 6 protected $fileName = 'data/applications/applications.rdf'; 7 7 8 8 public function __construct($applicationID) 9 9 { 10 10 // Create empty MemModel 11 12 13 14 15 16 17 11 $factory = new ModelFactory(); 12 $this->model = $factory->getDefaultModel(); 13 14 $this->applicationID = $applicationID; 15 16 if(file_exists($this->fileName)) 17 $this->model->load($this->fileName); 18 18 } 19 20 21 22 $this->model->saveAs($fileName,'rdf'); 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle)); 50 19 20 public function saveApplications() 21 { 22 $this->model->saveAs($fileName,'rdf'); 23 } 24 25 public function setApplicationInfo($aTitle, $aDescription, $aStyle) 26 { 27 $applicationID = md5( uniqid(rand(), true) ); 28 29 $resourceApplication = new Resource(APPLICATION.'/'.$applicationID); 30 31 $resourceApplicationType = new Resource(APPLICATION); 32 $predicateRType = new Resource(RTYPE); 33 $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType)); 34 35 $literalApplicationID = new Literal($applicationID); 36 $predicateUniqueID = new Resource(UID); 37 $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID)); 38 39 $applicationTitle = new Literal($aTitle); 40 $predicateTitle = new Resource(TITLE); 41 $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle)); 42 43 $applicationDescription = new Literal($aDescription); 44 $predicateDescription = new Resource(DESCRIPTION); 45 $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription)); 46 47 $applicationStyle = new Literal($aStyle); 48 $predicateStyle = new Resource(STYLE); 49 $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle)); 50 } 51 51 } 52 52 -
Dev/trunk/classes/SurveyAnswerDatabaseInterface.php
r45 r62 1 1 <?php 2 2 3 // Survey class as intermediate for storing data from the site to the RDF database3 // Survey class as intermediate for storing answer data from the site to the RDF database 4 4 require_once 'rdfConstants.php'; 5 5 … … 15 15 16 16 17 public function __construct($surveyID,$ userID)17 public function __construct($surveyID,$applicationID) 18 18 { 19 $this->surveyAnswerRDFWriter = new SurveyAnswerRDFWriter($surveyUID,$ userID);20 $this->surveyAnswerRDFReader = new SurveyAnswerRDFReader($surveyUID );19 $this->surveyAnswerRDFWriter = new SurveyAnswerRDFWriter($surveyUID,$applicationID); 20 $this->surveyAnswerRDFReader = new SurveyAnswerRDFReader($surveyUID,$applicationID); 21 21 } 22 23 public function setRespondentInfo($respondentInfo) 24 { 25 $respondentName = $surveyInfo['respondentName']; 26 27 $this->surveyAnswerRDFWriter->setRespondentData($respondentName); 28 } 22 29 23 30 public function setSurveyAnswerInfo($surveyInfo) 24 31 { 25 26 } 32 33 $qAnswers = array(); 34 $qNumber = 1; 35 while (isset($surveyInfo['questionID'.$qNumber])) 36 { 37 if (isset($surveyInfo['q'.$qNumber.'answered'])) 38 { 39 $qAnswers[$surveyInfo['questionID'.$qNumber]] = $surveyInfo['q'.$qNumber.'answered']; 40 } 41 42 $qNumber++; 43 } 44 45 $this->surveyAnswerRDFWriter->setAnswers($qAnswers); 46 } 47 48 public function getSurveyAnswerInfo() 49 { 50 51 } 27 52 } 28 53 ?> -
Dev/trunk/classes/SurveyAnswerRDFReader.php
r51 r62 1 1 <?php 2 2 3 class SurveyAnswerRDFReader extends SurveyRDFReader3 class SurveyAnswerRDFReader 4 4 { 5 protected $tempModel;6 protected $answerPath;7 8 public function __construct($surveyUID )5 protected $model; 6 protected $filePath; 7 8 public function __construct($surveyUID,$sessionID) 9 9 { 10 parent::__construct($surveyUID);11 12 // Create empty MemModel13 $factory = new ModelFactory();14 $this->tempModel = $factory->getDefaultModel();15 $this->answerPath = 'data/surveys/answers_'.$this->surveyUID;16 }17 18 public function loadSurvey()19 {20 parent::loadSurvey();21 22 }23 24 public function getAnswerByQuestion($questionID)25 {26 $this->tempModel->loadModel($this->model);27 28 $answers = array();29 30 if($handle = opendir($this->answerPath))31 {32 while (false !== ($file = readdir($handle))) {33 if(strstr($file, 'answer_'))34 $answers[] = substr($file,0,strlen($file)-4);35 }36 }37 38 foreach($answers as $answer)39 {40 $this->tempModel->load($this->answerPath.'/'.$answer);41 }42 43 $this->tempModel->visualize();44 45 $querystring = '46 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>47 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>48 SELECT ?answered49 WHERE50 {51 _question predicates:resource_type resources:question ;52 predicates:uid "'.$questionID.'" ;53 predicates:answered ?answered54 }';55 56 $result = $this->tempModel->sparqlQuery($querystring);57 58 $this->tempModel->close();59 10 60 $this->tempModel->visualize(); 61 62 return $result; 63 } 64 65 public function getAnswerByRespondent($respondentID) 66 { 67 $this->tempModel->loadModel($this->model); 68 69 $this->tempModel->load($this->answerPath . '/$answer_' . $respondentID); 70 71 $this->tempModel->visualize(); 72 73 $querystring = ' 74 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 75 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 76 SELECT ?answered 77 WHERE 78 { 79 _question predicates:resource_type resources:question ; 80 predicates:answered ?answered 81 }'; 82 83 $result = $this->tempModel->sparqlQuery($querystring); 84 85 $this->tempModel->close(); 11 // Create empty MemModel 12 $factory = new ModelFactory(); 13 $this->model = $factory->getDefaultModel(); 14 $this->filePath = 'data/surveys/survey_'.$surveyUID . '.rdf'; 15 16 $this->model->load($this->filePath); 17 } 86 18 87 $this->tempModel->visualize(); 88 89 return $result; 90 } 91 92 public function getAnswerByRespondentAndQuestion($respondentID,$questionID) 93 { 94 $this->tempModel->loadModel($this->model); 95 96 $this->tempModel->load($this->answerPath . '/$answer_' . $respondentID); 97 98 $this->tempModel->visualize(); 99 100 $querystring = ' 101 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 102 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 103 SELECT ?answered 104 WHERE 105 { 106 _question predicates:resource_type resources:question ; 107 predicates:uid "'.$questionID.'" ; 108 predicates:answered ?answered 109 }'; 110 111 $result = $this->tempModel->sparqlQuery($querystring); 112 113 $this->tempModel->close(); 19 public function loadAnsers($userID) 20 { 21 $this->model->load($this->filePath.'/answer_'.$this->userUID.'.rdf'); 22 } 114 23 115 $this->tempModel->visualize(); 116 117 return $result; 118 } 24 public function getAnswers() 25 { 26 $querystring = ' 27 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 28 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 29 SELECT ?answered 30 WHERE 31 { 32 _answer predicates:answered ?answered 33 }'; 34 35 $result = $this->model->sparqlQuery($querystring); 36 37 return $result; 38 } 39 40 public function getAnswerByQuestionID($questionID) 41 { 42 $querystring = ' 43 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 44 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 45 SELECT ?answered 46 WHERE 47 { 48 _question predicates:resource_type resources:question ; 49 predicates:uid "'.$questionID.'" ; 50 predicates:answered ?answered 51 }'; 52 53 $result = $this->model->sparqlQuery($querystring); 54 55 return $result; 56 } 57 58 public function getAnswerByRespondent($respondentID) 59 { 60 $this->tempModel->loadModel($this->model); 61 62 $this->tempModel->load($this->answerPath . '/$answer_' . $respondentID); 63 64 $this->tempModel->visualize(); 65 66 $querystring = ' 67 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 68 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 69 SELECT ?answered 70 WHERE 71 { 72 _question predicates:resource_type resources:question ; 73 predicates:answered ?answered 74 }'; 75 76 $result = $this->tempModel->sparqlQuery($querystring); 77 78 $this->tempModel->close(); 79 80 $this->tempModel->visualize(); 81 82 return $result; 83 } 84 85 public function getAnswerByRespondentAndQuestion($respondentID,$questionID) 86 { 87 $this->tempModel->loadModel($this->model); 88 89 $this->tempModel->load($this->answerPath . '/$answer_' . $respondentID); 90 91 $this->tempModel->visualize(); 92 93 $querystring = ' 94 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 95 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 96 SELECT ?answered 97 WHERE 98 { 99 _question predicates:resource_type resources:question ; 100 predicates:uid "'.$questionID.'" ; 101 predicates:answered ?answered 102 }'; 103 104 $result = $this->tempModel->sparqlQuery($querystring); 105 106 $this->tempModel->close(); 107 108 $this->tempModel->visualize(); 109 110 return $result; 111 } 119 112 } 120 113 -
Dev/trunk/classes/SurveyAnswerRDFWriter.php
r51 r62 1 1 <?php 2 2 3 class SurveyAnswerRDFWriter extends SurveyRDFWriter3 class SurveyAnswerRDFWriter 4 4 { 5 6 protected $answerPath;5 protected $userUID; 6 protected $filePath; 7 7 8 public function __construct($surveyUID,$userUID)8 public function __construct($surveyUID,$sessionID,$userID) 9 9 { 10 parent::__construct($surveyUID); 11 12 $this->userUID = $userUID; 13 $this->answerPath = 'data/surveys/answers_'.$this->surveyUID; 10 // Create empty MemModel 11 $factory = new ModelFactory(); 12 $this->model = $factory->getDefaultModel(); 13 14 $this->filePath = 'data/sessions/' . $sessionID . '/survey_'.$surveyUID . '/'; 15 $this->userID = $userID; 16 17 if (!is_dir($this->filePath)) 18 mkdir($this->filePath); 14 19 } 15 16 public function saveSurvey() 17 { 18 if (!is_dir($this->answerPath)) 19 mkdir($this->answerPath); 20 $this->model->saveAs($this->answerPath.'/answer_'.$this->userUID.'.rdf','rdf'); 21 } 22 23 public function setRespondentData($name) 24 { 25 $resourceUser = new Resource(USER . '/' . $userID); 26 27 $resourceUserType = new Resource(USER); 28 $predicateRType = new Resource(RTYPE); 29 $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType)); 30 31 $LiteralUserName = new Literal($name); 32 $predicateName = new Resource(NAME); 33 $this->model->add(new Statement($resourceUser,$predicateUniqueID,$LiteralUserName)); 34 35 $literalUserID = new Literal($this->userUID); 36 $predicateUniqueID = new Resource(UID); 37 $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID)); 38 39 $predicateRespondent = new Resource(REPONDENT); 40 $this->model->add(new Statement($this->resourceSurvey,$predicateRespondent,$resourceUser)); 41 } 42 43 public function setAnswers($answers) 44 { 45 foreach($answers as $questionID => $answerValue) 46 { 47 $resourceQuestion = new Resource(QUESTION.'/'.$questionID); 48 49 $predicateAnswered = new Resource(ANSWERED); 50 $answer = new Literal($answerValue); 51 $this->model->add(new Statement($resourceQuestion,$predicateAnswered,$answer)); 52 } 53 } 20 21 public function saveSurvey() 22 { 23 $this->model->saveAs($this->filePath.'/answer_'.$this->userUID.'.rdf','rdf'); 24 } 25 26 public function setRespondentData($name) 27 { 28 $resourceUser = new Resource(USER . '/' . $this->userID); 29 30 $resourceUserType = new Resource(USER); 31 $predicateRType = new Resource(RTYPE); 32 $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType)); 33 34 $LiteralUserName = new Literal($name); 35 $predicateName = new Resource(NAME); 36 $this->model->add(new Statement($resourceUser,$predicateUniqueID,$LiteralUserName)); 37 38 $literalUserID = new Literal($this->userUID); 39 $predicateUniqueID = new Resource(UID); 40 $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID)); 41 42 $predicateRespondent = new Resource(REPONDENT); 43 $this->model->add(new Statement($this->resourceSurvey,$predicateRespondent,$resourceUser)); 44 } 45 46 public function setAnswers($answers) 47 { 48 foreach($answers as $questionID => $answerValue) 49 { 50 $resourceQuestion = new Resource(QUESTION.'/'.$questionID); 51 52 $predicateAnswered = new Resource(ANSWERED); 53 $answer = new Literal($answerValue); 54 $this->model->add(new Statement($resourceQuestion,$predicateAnswered,$answer)); 55 } 56 } 54 57 } 55 58 -
Dev/trunk/classes/SurveyDatabaseInterface.php
r45 r62 10 10 class SurveyDatabaseInterface 11 11 { 12 13 14 12 protected $surveyRDFWriter; 13 protected $surveyRDFReader; 14 15 15 16 16 public function __construct($surveyID) 17 17 { 18 19 20 21 22 23 24 18 if($surveyID == null) 19 $surveyUID = md5( uniqid(rand(), true) ); 20 else 21 $surveyUID = $surveyID; 22 23 $this->surveyRDFWriter = new SurveyRDFWriter($surveyUID); 24 $this->surveyRDFReader = new SurveyRDFReader($surveyUID); 25 25 } 26 27 public function setSurveyInfo($survey)28 {29 $sTitle = $survey['surveyTitle'];30 $sDescription = $survey['surveyDescription'];31 32 $this->surveyRDFWriter->createSurvey($sTitle,$sDescription);33 34 $qNumber = 1;35 while (isset($survey['questionTitle'.$qNumber]))36 {37 $qTitle = $survey['questionTitle'.$qNumber];38 $qDescription = $survey['questionDescription'.$qNumber];39 $qType = $survey['questionType'.$qNumber];40 if (isset($survey['questionID'.$qNumber]))41 $qID = $survey['questionID'.$qNumber];42 else43 $qID = md5( uniqid(rand(), true) );44 45 $qAnswers = array();46 $aNumber = 1;47 while (isset($survey['q'.$qNumber.'ans'.$aNumber]))48 {49 $aArray = array();50 $aArray['Title'] = $survey['q'.$qNumber.'ans'.$aNumber];51 $aArray['Description'] = 'answerDescription'.$aNumber;52 $qAnswers[] = $aArray;53 54 $aNumber++;55 }56 57 $this->surveyRDFWriter->addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers);58 59 $qNumber++;60 }61 26 62 $this->surveyRDFWriter->saveSurvey(); 63 } 64 65 public function getSurveyInfo() 66 { 67 $surveyInfo = array(); 68 69 $resultSurvey = $this->surveyRDFReader->getSurveyInfo(); 70 71 $surveyInfo['surveyID'] = substr($resultSurvey[0][0]['?uid'],9,strlen($resultSurvey[0][0]['?uid'])-11); 72 $surveyInfo['surveyTitle'] = substr($resultSurvey[0][0]['?title'],9,strlen($resultSurvey[0][0]['?title'])-11); 73 $surveyInfo['surveyDescription'] = substr($resultSurvey[0][0]['?description'],9,strlen($resultSurvey[0][0]['?description'])-11); 74 75 for($qNumber = 1;$qNumber<=sizeof($resultSurvey[1][0]);$qNumber++) 76 { 77 $surveyInfo['questionID'.$qNumber] = substr($resultSurvey[1][3][$qNumber-1]['?questionID'],9,strlen($resultSurvey[1][3][$qNumber-1]['?questionID'])-11); 78 $surveyInfo['questionTitle'.$qNumber] = substr($resultSurvey[1][0][$qNumber-1]['?questionTitle'],9,strlen($resultSurvey[1][0][$qNumber-1]['?questionTitle'])-11); 79 $surveyInfo['questionDescription'.$qNumber] = substr($resultSurvey[1][1][$qNumber-1]['?questionDescription'],9,strlen($resultSurvey[1][1][$qNumber-1]['?questionDescription'])-11); 80 $surveyInfo['questionType'.$qNumber] = substr($resultSurvey[1][2][$qNumber-1]['?questionType'],9,strlen($resultSurvey[1][2][$qNumber-1]['?questionType'])-11); 81 82 $resultAnswers = $this->surveyRDFReader->readSurveyAnswers($surveyInfo['questionID'.$qNumber]); 83 84 if ($resultAnswers != null) 85 { 86 for($aNumber = 1;$aNumber<=sizeof($resultAnswers);$aNumber++) 87 { 88 $surveyInfo['q'.$qNumber.'ans'.$aNumber] = substr($resultAnswers[$aNumber-1]['?answerTitle'],9,strlen($resultAnswers[$aNumber-1]['?answerTitle'])-11); 89 } 90 } 91 } 92 93 return $surveyInfo; 94 } 27 public function setSurveyInfo($survey) 28 { 29 $sTitle = $survey['surveyTitle']; 30 $sDescription = $survey['surveyDescription']; 95 31 96 public function getExistingSurveys() 97 { 98 $surveyIDs = array(); 99 $surveys = array(); 100 101 if($handle = opendir('data/surveys/')) 102 { 103 while (false !== ($file = readdir($handle))) { 104 if(strstr($file, 'survey_')) 105 $surveyIDs[] = substr($file,7,strlen($file)-11); 106 } 107 } 108 109 foreach($surveyIDs as $surveyID) 110 { 111 $surveyTitle = $this->surveyRDFReader->getSurveyTitleByID($surveyID); 112 $surveys[$surveyID] = substr($surveyTitle[0]['?title'],9,strlen($surveyTitle[0]['?title'])-11); 113 } 114 115 return $surveys; 116 } 117 32 $this->surveyRDFWriter->createSurvey($sTitle,$sDescription); 33 34 $sUserID = "Creator"; 35 36 $this->surveyRDFWriter->setUserData($sUserID); 37 38 $qNumber = 1; 39 while (isset($survey['questionTitle'.$qNumber])) 40 { 41 $qTitle = $survey['questionTitle'.$qNumber]; 42 $qDescription = $survey['questionDescription'.$qNumber]; 43 $qType = $survey['questionType'.$qNumber]; 44 if (isset($survey['questionID'.$qNumber])) 45 $qID = $survey['questionID'.$qNumber]; 46 else 47 $qID = md5( uniqid(rand(), true) ); 48 49 $qAnswers = array(); 50 $aNumber = 1; 51 while (isset($survey['q'.$qNumber.'ans'.$aNumber])) 52 { 53 $aArray = array(); 54 $aArray['Title'] = 'q'.$qNumber.'ans'.$aNumber; 55 $aArray['Description'] = $survey['q'.$qNumber.'ans'.$aNumber]; 56 $qAnswers[] = $aArray; 57 58 $aNumber++; 59 } 60 61 $this->surveyRDFWriter->addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers); 62 63 $qNumber++; 64 } 65 66 $this->surveyRDFWriter->saveSurvey(); 67 } 68 69 public function getSurveyInfo() 70 { 71 $surveyInfo = array(); 72 73 $resultSurvey = $this->surveyRDFReader->getSurveyInfo(); 74 75 $surveyInfo['surveyID'] = substr($resultSurvey[0][0]['?uid'],9,strlen($resultSurvey[0][0]['?uid'])-11); 76 $surveyInfo['surveyTitle'] = substr($resultSurvey[0][0]['?title'],9,strlen($resultSurvey[0][0]['?title'])-11); 77 $surveyInfo['surveyDescription'] = substr($resultSurvey[0][0]['?description'],9,strlen($resultSurvey[0][0]['?description'])-11); 78 $surveyInfo['surveyCreator'] = substr($resultSurvey[0][0]['?creator'],9,strlen($resultSurvey[0][0]['?creator'])-11); 79 80 for($qNumber = 1;$qNumber<=sizeof($resultSurvey[1][0]);$qNumber++) 81 { 82 $surveyInfo['questionID'.$qNumber] = substr($resultSurvey[1][3][$qNumber-1]['?questionID'],9,strlen($resultSurvey[1][3][$qNumber-1]['?questionID'])-11); 83 $surveyInfo['questionTitle'.$qNumber] = substr($resultSurvey[1][0][$qNumber-1]['?questionTitle'],9,strlen($resultSurvey[1][0][$qNumber-1]['?questionTitle'])-11); 84 $surveyInfo['questionDescription'.$qNumber] = substr($resultSurvey[1][1][$qNumber-1]['?questionDescription'],9,strlen($resultSurvey[1][1][$qNumber-1]['?questionDescription'])-11); 85 $surveyInfo['questionType'.$qNumber] = substr($resultSurvey[1][2][$qNumber-1]['?questionType'],9,strlen($resultSurvey[1][2][$qNumber-1]['?questionType'])-11); 86 87 $resultAnswers = $this->surveyRDFReader->readSurveyAnswers($surveyInfo['questionID'.$qNumber]); 88 89 if ($resultAnswers != null) 90 { 91 for($aNumber = 1;$aNumber<=sizeof($resultAnswers);$aNumber++) 92 { 93 $surveyInfo['q'.$qNumber.'ans'.$aNumber] = substr($resultAnswers[$aNumber-1]['?answerDescription'],9,strlen($resultAnswers[$aNumber-1]['?answerDescription'])-11); 94 } 95 } 96 } 97 98 return $surveyInfo; 99 } 100 101 public function getExistingSurveys() 102 { 103 $surveyIDs = array(); 104 $surveys = array(); 105 106 if($handle = opendir('data/surveys/')) 107 { 108 while (false !== ($file = readdir($handle))) { 109 if(strstr($file, 'survey_') && strstr($file, '.rdf')) 110 $surveyIDs[] = substr($file,7,strlen($file)-11); 111 } 112 } 113 114 foreach($surveyIDs as $surveyID) 115 { 116 $surveyTitle = $this->surveyRDFReader->getSurveyTitleByID($surveyID); 117 $surveys[$surveyID] = substr($surveyTitle[0]['?title'],9,strlen($surveyTitle[0]['?title'])-11); 118 } 119 120 return $surveys; 121 } 122 118 123 } 119 124 ?> -
Dev/trunk/classes/SurveyRDFReader.php
r45 r62 3 3 class SurveyRDFReader 4 4 { 5 6 7 8 9 5 protected $model; 6 7 protected $surveyUID; 8 protected $filePath; 9 10 10 public function __construct($surveyUID) 11 11 { 12 12 // Create empty MemModel 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>40 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>41 SELECT ?uid ?title ?description 42 43 44 _surveypredicates:resource_type resources:survey ;45 predicates:uid?uid ;46 predicates:title?title ;47 predicates:description ?description 48 }'; 49 50 $result = $this->model->sparqlQuery($querystring); 51 52 return $result; 53 } 54 55 public function readSurveyQuestions() 56 { 57 $result = array(); 58 $result[] = $this->readSurveyQuestionsTitle(); 59 $result[] = $this->readSurveyQuestionsDescription(); 60 $result[] = $this->readSurveyQuestionsType();61 $result[] = $this->readSurveyQuestionsID(); 62 63 return $result; 64 } 65 66 public function readSurveyQuestionsTitle() 67 { 68 $querystring = ' 69 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 70 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>71 SELECT ?questionTitle 72 WHERE 73 { 74 _question predicates:resource_type resources:question ; 75 predicates:title ?questionTitle 76 }'; 77 78 $result = $this->model->sparqlQuery($querystring); 79 80 return $result; 81 } 82 83 public function readSurveyQuestionsDescription() 84 { 85 $querystring = ' 86 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 87 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>88 SELECT ?questionDescription 89 WHERE 90 { 91 _question predicates:resource_type resources:question ; 92 predicates:description ?questionDescription 93 }'; 94 95 $result = $this->model->sparqlQuery($querystring); 96 97 return $result; 98 } 99 100 public function readSurveyQuestionsType() 101 { 102 $querystring = ' 103 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 104 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>105 SELECT ?questionType 106 WHERE 107 { 108 _question predicates:resource_type resources:question ; 109 predicates:question_type ?questionType 110 }'; 111 112 $result = $this->model->sparqlQuery($querystring); 113 114 return $result; 115 } 116 117 public function readSurveyQuestionsID() 118 { 119 $querystring = ' 120 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 121 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>122 SELECT ?questionID 123 WHERE 124 { 125 _question predicates:resource_type resources:question ; 126 predicates:uid ?questionID 127 }'; 128 129 $result = $this->model->sparqlQuery($querystring); 130 131 return $result; 132 } 133 134 public function readSurveyAnswers($questionID) 135 { 136 $querystring = ' 137 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 138 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>139 SELECT ?answerTitle 140 WHERE 141 { 142 _question predicates:resource_type resources:question ; 143 predicates:uid "' . $questionID . '";144 predicates:has_answer _answer . 145 _answer predicates:title ?answerTitle 146 }'; 147 148 $result = $this->model->sparqlQuery($querystring); 149 150 return $result; 151 } 152 153 public function getSurveyTitleByID($surveyID) 154 { 155 13 $factory = new ModelFactory(); 14 $this->model = $factory->getDefaultModel(); 15 16 $this->surveyUID = $surveyUID; 17 $this->filePath = 'data/surveys/survey_'.$this->surveyUID.'.rdf'; 18 } 19 20 public function loadSurvey() 21 { 22 $this->model->load($this->filePath); 23 } 24 25 public function getSurveyInfo() 26 { 27 SurveyRDFReader::loadSurvey(); 28 29 $result = array(); 30 $result[] = $this->readSurveyInfo(); 31 $result[] = $this->readSurveyQuestions(); 32 33 return $result; 34 } 35 36 public function readSurveyInfo() 37 { 38 $querystring = ' 39 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 40 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 41 SELECT ?uid ?title ?description ?creator 42 WHERE 43 { 44 _survey predicates:resource_type resources:survey ; 45 predicates:uid ?uid ; 46 predicates:title ?title ; 47 predicates:description ?description ; 48 predicates:creator ?creator 49 }'; 50 51 $result = $this->model->sparqlQuery($querystring); 52 53 return $result; 54 } 55 56 public function readSurveyQuestions() 57 { 58 $result = array(); 59 $result[] = $this->readSurveyQuestionsTitle(); 60 $result[] = $this->readSurveyQuestionsDescription(); 61 $result[] = $this->readSurveyQuestionsType(); 62 $result[] = $this->readSurveyQuestionsID(); 63 64 return $result; 65 } 66 67 public function readSurveyQuestionsTitle() 68 { 69 $querystring = ' 70 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 71 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 72 SELECT ?questionTitle 73 WHERE 74 { 75 _question predicates:resource_type resources:question ; 76 predicates:title ?questionTitle 77 }'; 78 79 $result = $this->model->sparqlQuery($querystring); 80 81 return $result; 82 } 83 84 public function readSurveyQuestionsDescription() 85 { 86 $querystring = ' 87 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 88 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 89 SELECT ?questionDescription 90 WHERE 91 { 92 _question predicates:resource_type resources:question ; 93 predicates:description ?questionDescription 94 }'; 95 96 $result = $this->model->sparqlQuery($querystring); 97 98 return $result; 99 } 100 101 public function readSurveyQuestionsType() 102 { 103 $querystring = ' 104 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 105 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 106 SELECT ?questionType 107 WHERE 108 { 109 _question predicates:resource_type resources:question ; 110 predicates:question_type ?questionType 111 }'; 112 113 $result = $this->model->sparqlQuery($querystring); 114 115 return $result; 116 } 117 118 public function readSurveyQuestionsID() 119 { 120 $querystring = ' 121 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 122 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 123 SELECT ?questionID 124 WHERE 125 { 126 _question predicates:resource_type resources:question ; 127 predicates:uid ?questionID 128 }'; 129 130 $result = $this->model->sparqlQuery($querystring); 131 132 return $result; 133 } 134 135 public function readSurveyAnswers($questionID) 136 { 137 $querystring = ' 138 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 139 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 140 SELECT ?answerDescription 141 WHERE 142 { 143 _question predicates:resource_type resources:question ; 144 predicates:uid "' . $questionID . '" ; 145 predicates:has_answer _answer . 146 _answer predicates:description ?answerDescription 147 }'; 148 149 $result = $this->model->sparqlQuery($querystring); 150 151 return $result; 152 } 153 154 public function getSurveyTitleByID($surveyID) 155 { 156 156 // Create empty MemModel 157 158 159 160 161 162 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>163 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>164 SELECT?title165 166 167 _surveypredicates:resource_type resources:survey ;168 predicates:title?title169 170 171 172 173 174 157 $factory = new ModelFactory(); 158 $tempmodel= $factory->getDefaultModel(); 159 $tempmodel->load('data/surveys/survey_'.$surveyID.'.rdf'); 160 161 $querystring = ' 162 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 163 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 164 SELECT ?title 165 WHERE 166 { 167 _survey predicates:resource_type resources:survey ; 168 predicates:title ?title 169 }'; 170 171 $result = $tempmodel->sparqlQuery($querystring); 172 173 return $result; 174 } 175 175 } 176 177 176 ?> -
Dev/trunk/classes/SurveyRDFWriter.php
r45 r62 3 3 class SurveyRDFWriter 4 4 { 5 protected $model; 6 7 protected $resourceSurvey; 8 protected $surveyUID; 9 protected $filePath; 10 5 protected $model; 6 7 protected $resourceSurvey; 8 protected $surveyUID; 9 10 protected $filePath; 11 11 12 public function __construct($surveyUID) 12 13 { 13 14 // Create empty MemModel 14 $factory = new ModelFactory(); 15 $this->model = $factory->getDefaultModel(); 16 17 $this->surveyUID = $surveyUID; 18 $this->filePath = 'data/surveys/survey_'.$this->surveyUID.'.rdf'; 19 15 $factory = new ModelFactory(); 16 $this->model = $factory->getDefaultModel(); 17 18 $this->surveyUID = $surveyUID; 19 $this->filePath = 'data/surveys/'; 20 if (!is_dir($this->filePath)) 21 mkdir($this->filePath); 20 22 } 21 22 public function saveSurvey()23 {24 $this->model->saveAs($this->filePath);25 }26 27 public function createSurvey($sTitle, $sDescription)28 {29 $this->resourceSurvey = new Resource(SURVEY.'/'.$this->surveyUID);30 31 $resourceSurveyType = new Resource(SURVEY);32 $predicateRType = new Resource(RTYPE);33 $this->model->add(new Statement($this->resourceSurvey,$predicateRType,$resourceSurveyType));34 35 $literalSurveyID = new Literal($this->surveyUID);36 $predicateUniqueID = new Resource(UID);37 $this->model->add(new Statement($this->resourceSurvey,$predicateUniqueID,$literalSurveyID));38 39 $predicateTitle = new Resource(TITLE);40 $surveyTitle = new Literal($sTitle);41 $this->model->add(new Statement($this->resourceSurvey,$predicateTitle,$surveyTitle));42 43 $predicateDescription = new Resource(DESCRIPTION);44 $surveyDescription = new Literal($sDescription);45 $this->model->add(new Statement($this->resourceSurvey,$predicateDescription,$surveyDescription));46 }47 48 public function setUserData($userID)49 {50 $resourceUser = new Resource(USER . '/' . $userID);51 52 $predicateCreator = new Resource(CREATOR);53 $this->model->add(new Statement($this->resourceSurvey,$predicateCreator,$resourceUser));54 }55 56 public function setApplicationData($applicationID)57 {58 $resourceApplication = new Resource(APPLICATION . '/' . $applicationID);59 60 $predicatePartOf = new Resource(PART_OF);61 $this->model->add(new Statement($this->resourceSurvey,$predicatePartOf,$resourceApplication));62 }63 64 public function addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers)65 {66 $questionID = $qID;67 $resourceQuestion = new Resource(QUESTION.'/'.$questionID);68 69 $resourceQuestionType = new Resource(QUESTION);70 $predicateRType = new Resource(RTYPE);71 $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));72 73 $predicateUniqueID = new Resource(UID);74 $questionUID = new Literal($questionID);75 $this->model->add(new Statement($resourceQuestion,$predicateUniqueID,$questionUID));76 77 $predicateTitle = new Resource(TITLE);78 $questionTitle = new Literal($qTitle);79 $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));80 81 $predicateDescription = new Resource(DESCRIPTION);82 $questionDescription = new Literal($qDescription);83 $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));84 85 $predicateQType = new Resource(QTYPE);86 $resourceQuestionType = new Literal($qType);87 $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));88 89 foreach($qAnswers as $answer)90 {91 $answerID = md5( uniqid(rand(), true) );92 $resourceAnswer = new Resource(ANSWER.'/'.$answerID);93 94 $resourceAnswerType = new Resource(ANSWER);95 $predicateRType = new Resource(RTYPE);96 $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));97 23 98 $predicateUniqueID = new Resource(UID); 99 $answerUID = new Literal($answerID); 100 $this->model->add(new Statement($resourceAnswer,$predicateUniqueID,$answerUID)); 101 102 $answerTitle = new Literal($answer['Title']); 103 $this->model->add(new Statement($resourceAnswer,$predicateTitle,$answerTitle)); 104 105 $answerDescription = new Literal($answer['Description']); 106 $this->model->add(new Statement($resourceAnswer,$predicateDescription,$answerDescription)); 107 108 $predicateAnswer = new Resource(HAS_ANSWER); 109 $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$resourceAnswer)); 110 } 111 112 $predicateQuestion = new Resource(HAS_QUESTION); 113 $this->model->add(new Statement($this->resourceSurvey,$predicateQuestion,$resourceQuestion)); 114 } 24 public function saveSurvey() 25 { 26 $this->model->saveAs($this->filePath.'survey_'.$this->surveyUID.'.rdf','rdf'); 27 } 28 29 public function createSurvey($sTitle, $sDescription) 30 { 31 $this->resourceSurvey = new Resource(SURVEY.'/'.$this->surveyUID); 32 33 $resourceSurveyType = new Resource(SURVEY); 34 $predicateRType = new Resource(RTYPE); 35 $this->model->add(new Statement($this->resourceSurvey,$predicateRType,$resourceSurveyType)); 36 37 $literalSurveyID = new Literal($this->surveyUID); 38 $predicateUniqueID = new Resource(UID); 39 $this->model->add(new Statement($this->resourceSurvey,$predicateUniqueID,$literalSurveyID)); 40 41 $predicateTitle = new Resource(TITLE); 42 $surveyTitle = new Literal($sTitle); 43 $this->model->add(new Statement($this->resourceSurvey,$predicateTitle,$surveyTitle)); 44 45 $predicateDescription = new Resource(DESCRIPTION); 46 $surveyDescription = new Literal($sDescription); 47 $this->model->add(new Statement($this->resourceSurvey,$predicateDescription,$surveyDescription)); 48 } 49 50 public function setUserData($userID) 51 { 52 $resourceUser = new Resource(USER . '/' . $userID); 53 54 $predicateCreator = new Resource(CREATOR); 55 $this->model->add(new Statement($this->resourceSurvey,$predicateCreator,$resourceUser)); 56 } 57 58 public function addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers) 59 { 60 $questionID = $qID; 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 $predicateUniqueID = new Resource(UID); 68 $questionUID = new Literal($questionID); 69 $this->model->add(new Statement($resourceQuestion,$predicateUniqueID,$questionUID)); 70 71 $predicateTitle = new Resource(TITLE); 72 $questionTitle = new Literal($qTitle); 73 $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle)); 74 75 $predicateDescription = new Resource(DESCRIPTION); 76 $questionDescription = new Literal($qDescription); 77 $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription)); 78 79 $predicateQType = new Resource(QTYPE); 80 $resourceQuestionType = new Literal($qType); 81 $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType)); 82 83 foreach($qAnswers as $answer) 84 { 85 $answerID = md5( uniqid(rand(), true) ); 86 $resourceAnswer = new Resource(ANSWER.'/'.$answerID); 87 88 $resourceAnswerType = new Resource(ANSWER); 89 $predicateRType = new Resource(RTYPE); 90 $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); 91 92 $predicateUniqueID = new Resource(UID); 93 $answerUID = new Literal($answerID); 94 $this->model->add(new Statement($resourceAnswer,$predicateUniqueID,$answerUID)); 95 96 $answerTitle = new Literal($answer['Title']); 97 $this->model->add(new Statement($resourceAnswer,$predicateTitle,$answerTitle)); 98 99 $answerDescription = new Literal($answer['Description']); 100 $this->model->add(new Statement($resourceAnswer,$predicateDescription,$answerDescription)); 101 102 $predicateAnswer = new Resource(HAS_ANSWER); 103 $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$resourceAnswer)); 104 } 105 106 $predicateQuestion = new Resource(HAS_QUESTION); 107 $this->model->add(new Statement($this->resourceSurvey,$predicateQuestion,$resourceQuestion)); 108 } 115 109 } 116 110 -
Dev/trunk/classes/UserDatabaseInterface.php
r44 r62 9 9 class UserDatabaseInterface 10 10 { 11 protected $key = 'CPSsecretKey'; 12 13 protected $userRDFWriter; 14 protected $userRDFReader; 15 16 public function __construct() 17 { 18 $this->userRDFWriter = new UserRDFWriter(); 19 $this->userRDFReader = new UserRDFReader(); 20 } 21 22 public function addNewUser($userInfo) 23 { 24 $userName = $userInfo['username']; 25 $userPassword = $this->RIJNDAEL_encrypt($userInfo['password']); 26 27 $this->userRDFWriter->addNewUser($userName, $userPassword); 28 $this->userRDFWriter->saveUsers(); 29 } 30 31 public function checkUserName($userName) 32 { 33 $result = false; 34 35 $resultUser = $this->userRDFReader->getUserNames(); 36 37 if ($resultUser != null) 38 { 39 foreach($resultUser as $user) 40 { 41 $name = substr($user['?name'],9,strlen($user['?name'])-11); 42 if(!strcmp($name ,$userName)) 43 { 44 $result = true; 45 } 46 } 47 } 48 49 return $result; 50 } 51 52 public function checkUserPassword($userInfo) 53 { 54 $result = false; 55 56 $userName = $userInfo['username']; 57 $userPassword = $userInfo['password']; 58 59 $encryptedPasswordLiteral = $this->userRDFReader->getUserPassword($userName); 60 $encryptedPassword = substr($encryptedPasswordLiteral[0]['?password'],9,strlen($encryptedPasswordLiteral[0]['?password'])-11); 61 62 if(!strcmp($this->RIJNDAEL_encrypt($userPassword),$encryptedPassword)) 63 $result = true; 64 65 return $result; 66 } 67 68 protected function RIJNDAEL_encrypt($text) 69 { 70 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 71 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 72 73 return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $text, MCRYPT_MODE_ECB, $iv)); 74 } 11 protected $key = 'CPSsecretKey'; 75 12 13 protected $userRDFWriter; 14 protected $userRDFReader; 15 16 public function __construct() 17 { 18 $this->userRDFWriter = new UserRDFWriter(); 19 $this->userRDFReader = new UserRDFReader(); 20 } 21 22 public function addNewUser($userInfo) 23 { 24 $userName = $userInfo['username']; 25 $userPassword = $this->RIJNDAEL_encrypt($userInfo['password']); 26 27 $this->userRDFWriter->addNewUser($userName, $userPassword); 28 $this->userRDFWriter->saveUsers(); 29 } 30 31 public function checkUserName($userName) 32 { 33 $result = false; 34 35 $resultUser = $this->userRDFReader->getUserNames(); 36 37 if ($resultUser != null) 38 { 39 foreach($resultUser as $user) 40 { 41 $name = substr($user['?name'],9,strlen($user['?name'])-11); 42 if(!strcmp($name ,$userName)) 43 { 44 $result = true; 45 } 46 } 47 } 48 49 return $result; 50 } 51 52 public function checkUserPassword($userInfo) 53 { 54 $result = false; 55 56 $userName = $userInfo['username']; 57 $userPassword = $userInfo['password']; 58 59 $encryptedPasswordLiteral = $this->userRDFReader->getUserPassword($userName); 60 $encryptedPassword = substr($encryptedPasswordLiteral[0]['?password'],9,strlen($encryptedPasswordLiteral[0]['?password'])-11); 61 62 if(!strcmp($this->RIJNDAEL_encrypt($userPassword),$encryptedPassword)) 63 $result = true; 64 65 return $result; 66 } 67 68 protected function RIJNDAEL_encrypt($text) 69 { 70 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 71 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 72 73 return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $text, MCRYPT_MODE_ECB, $iv)); 74 } 76 75 } 77 76 -
Dev/trunk/classes/UserRDFReader.php
r45 r62 3 3 class UserRDFReader 4 4 { 5 6 7 5 protected $model; 6 protected $fileName = 'data/users/users.rdf'; 7 8 8 public function __construct() 9 9 { 10 10 // Create empty MemModel 11 $factory = new ModelFactory(); 12 $this->model = $factory->getDefaultModel(); 13 14 15 if(file_exists($this->fileName)) 16 $this->model->load($this->fileName); 11 $factory = new ModelFactory(); 12 $this->model = $factory->getDefaultModel(); 13 14 if(file_exists($this->fileName)) 15 $this->model->load($this->fileName); 17 16 } 18 19 20 21 22 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>23 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>24 SELECT?name25 26 27 _userpredicates:resource_type resources:user ;28 predicates:name?name29 30 31 32 33 34 35 36 37 38 39 PREFIXpredicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>40 PREFIXresources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>41 SELECT?password42 43 44 _userpredicates:resource_type resources:user ;45 predicates:name"' . $userName . '" ;46 predicates:password?password47 48 49 50 51 52 17 18 public function getUserNames() 19 { 20 $querystring = ' 21 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 22 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 23 SELECT ?name 24 WHERE 25 { 26 _user predicates:resource_type resources:user ; 27 predicates:name ?name 28 }'; 29 30 $result = $this->model->sparqlQuery($querystring); 31 32 return $result; 33 } 34 35 public function getUserPassword($userName) 36 { 37 $querystring = ' 38 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 39 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 40 SELECT ?password 41 WHERE 42 { 43 _user predicates:resource_type resources:user ; 44 predicates:name "' . $userName . '" ; 45 predicates:password ?password 46 }'; 47 48 $result = $this->model->sparqlQuery($querystring); 49 50 return $result; 51 } 53 52 } 54 53 -
Dev/trunk/classes/UserRDFWriter.php
r45 r62 3 3 class UserRDFWriter 4 4 { 5 protected $model; 6 protected $fileName = 'data/users/users.rdf'; 5 7 6 protected $model;7 protected $fileName = 'data/users/users.rdf';8 9 8 public function __construct() 10 9 { 11 10 // Create empty MemModel 12 13 14 15 16 11 $factory = new ModelFactory(); 12 $this->model = $factory->getDefaultModel(); 13 14 if(file_exists($this->fileName)) 15 $this->model->load($this->fileName); 17 16 } 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 17 18 public function saveUsers() 19 { 20 $this->model->saveAs($this->fileName,'rdf'); 21 } 22 23 public function addNewUser($name,$password) 24 { 25 $userID = md5( uniqid(rand(), true) ); 26 27 $resourceUser = new Resource(USER . '/' . $userID); 28 29 $resourceUserType = new Resource(USER); 30 $predicateRType = new Resource(RTYPE); 31 $this->model->add(new Statement($resourceUser,$predicateRType,$resourceUserType)); 32 33 $literalUserName = new Literal($name); 34 $predicateName = new Resource(NAME); 35 $this->model->add(new Statement($resourceUser,$predicateName,$literalUserName)); 36 37 $literalUserID = new Literal($userID); 38 $predicateUniqueID = new Resource(UID); 39 $this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID)); 40 41 $literalPassword = new Literal($password); 42 $predicatePassword = new Resource(PASSWORD); 43 $this->model->add(new Statement($resourceUser,$predicatePassword,$literalPassword)); 44 } 46 45 47 46 }
Note: See TracChangeset
for help on using the changeset viewer.