Changeset 171 for Dev/trunk/classes/QuestionConnector.php
- Timestamp:
- 12/02/11 15:26:01 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/classes/QuestionConnector.php
r158 r171 12 12 * @author jkraaijeveld 13 13 */ 14 class QuestionConnector implements IConnector {14 class QuestionConnector extends Connector { 15 15 16 protected $model;17 protected $fileName = 'data/questions/questions.rdf';18 16 19 17 /** … … 22 20 public function __construct() 23 21 { 22 $this->fileName = 'data/questions/questions.rdf'; 24 23 //Ensure the required folder for this connector exists 25 24 if (!is_dir('data/questions/')) … … 28 27 29 28 /** 30 * function load()31 * Loads the file into the standard MemModel.32 */33 public function load() {34 //Get the Memory Model from the ModelFactory35 $this->model = ModelFactory::getDefaultModel();36 37 //Ensure the required file exists before loading38 if(file_exists($this->fileName))39 $this->model->load($this->fileName);40 }41 42 /**43 * function save()44 * Saves the MemModel into the given file.45 */46 public function save() {47 $this->model->saveAs($this->fileName,'rdf');48 }49 50 /**51 29 * Get the questions corresponding to the arguments. 52 30 * @param type $arguments : An array having one ore more of the following elements: 53 * ' uid', 'title', 'type', 'description', 'category', 'has_answer'.31 * 'code', 'title', 'type', 'description', 'category', 'definedanswers'. 54 32 */ 55 33 public function get($arguments) 56 34 { 57 35 $this->load(); 58 59 $keys = array_keys($arguments);60 //Set default values for arguments61 $uid = ""; $title = ""; $type = ""; $description = ""; $category = ""; $has_answer = "";62 //Set the arguments if they are supplied63 if(in_array("uid", $keys))64 $uid = 'predicates:question_code \''.$arguments["uid"].'\'';65 if(in_array("title", $keys))66 $title = 'predicates:title \''.$arguments["title"].'\'';67 if(in_array("type", $keys))68 $type = 'predicates:type \''.$arguments["type"].'\'';69 if(in_array("description", $keys))70 $description = "predicates:description \"".$arguments["description"]."\"";71 if(in_array("category", $keys))72 $style = "predicates:category \"".$arguments["category"]."\"";73 if(in_array("answers", $keys))74 {75 foreach($arguments["answers"] as $answer)76 {77 $has_answer = $has_answer . 'predicates:has_answer \'' . $answer . '\' ';78 }79 }80 36 81 37 $querystring = ' … … 91 47 predicates:description ?description ; 92 48 predicates:question_category ?category ; 93 '. $uid . $title . $type . $description . $category . $has_answer . ' 94 }'; 49 '. $this->createArguments($arguments) . ' 50 }'; 51 52 95 53 //Create the querystring 96 54 $results = $this->model->sparqlQuery($querystring); … … 115 73 private function getAnswers($uid) 116 74 { 117 $querystring = ' 118 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 119 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 120 SELECT DISTINCT ?has_answer 121 WHERE 122 { 123 _question predicates:resource_type resources:question ; 124 predicates:has_answer ?has_answer ; 125 predicates:question_code \'' . $uid . '\' 126 }'; 75 $result = $this->model->findRegex("[(".$uid.")]", "[(has_answer)]", null); 76 $iterator = $result->getStatementIterator(); 77 $answers = array(); 78 while($iterator->hasNext()) 79 { 80 $element = $iterator->next(); 81 $answers[] = $element->getLabelObject(); 82 } 83 return $answers; 127 84 128 $answers = $this->model->sparqlQuery($querystring); 129 $returnArray = array(); 130 if(!empty($answers)) 131 { 132 foreach($answers as $answer) 133 { 134 $returnArray[] = $answer['?has_answer']->label; 135 } 136 } 137 return $returnArray; 138 } 139 85 } 140 86 141 87 /**
Note: See TracChangeset
for help on using the changeset viewer.