[215] | 1 | <?php |
---|
| 2 | require_once 'rdfConstants.php'; |
---|
| 3 | // Include RAP Library to write RDF files |
---|
| 4 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * Description of Question |
---|
| 8 | * |
---|
| 9 | * @author jkraaijeveld |
---|
| 10 | */ |
---|
| 11 | class Question extends ResearchToolObject{ |
---|
| 12 | private static $filename = 'data/questions/questions.rdf'; |
---|
| 13 | |
---|
| 14 | public $title; |
---|
| 15 | public $type; |
---|
| 16 | public $description; |
---|
| 17 | public $category; |
---|
| 18 | public $answers; // format answers['#'] |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Constructor for a Question. $uid equals the corresponding code. |
---|
| 22 | * @param type $uid |
---|
| 23 | * @param type $title |
---|
| 24 | * @param type $type |
---|
| 25 | * @param type $description |
---|
| 26 | * @param type $category |
---|
| 27 | * @param type $answers |
---|
| 28 | */ |
---|
| 29 | public function __construct($uid, $title = null, $type = null, $description = null, $category = null, $answers = null) |
---|
| 30 | { |
---|
| 31 | if(!isset($uid)) |
---|
| 32 | { |
---|
| 33 | $uid = md5(uniqid(rand(), true)); |
---|
| 34 | } |
---|
| 35 | $this->uid = $uid; |
---|
| 36 | $this->title = $title; |
---|
| 37 | $this->type = $type; |
---|
| 38 | $this->description = $description; |
---|
| 39 | $this->category = $category; |
---|
| 40 | $this->answers = $answers; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * function save() |
---|
| 45 | * Saves the current object into the database. |
---|
| 46 | */ |
---|
| 47 | public function save() |
---|
| 48 | { |
---|
| 49 | //Ensure the required folder exists |
---|
| 50 | if (!is_dir('data/questions/')) |
---|
| 51 | mkdir('data/questions/'); |
---|
| 52 | |
---|
| 53 | $model = ResearchToolObject::load(Question::$filename); |
---|
| 54 | |
---|
| 55 | $resourceQuestion = new Resource(QUESTION.'/'.$this->uid); |
---|
| 56 | //Remove the old value stored with the given id |
---|
| 57 | $model->subtract($model->find($resourceQuestion, null, null)); |
---|
| 58 | |
---|
| 59 | //Save all the new values |
---|
| 60 | $resourceQuestionType = new Resource(QUESTION); |
---|
| 61 | $predicateRType = new Resource(RTYPE); |
---|
| 62 | $model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType)); |
---|
| 63 | |
---|
| 64 | $questionQCode = new Literal($this->uid); |
---|
| 65 | $predicateQCode = new Resource(QCODE); |
---|
| 66 | $model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode)); |
---|
| 67 | |
---|
| 68 | $questionTitle = new Literal($this->title); |
---|
| 69 | $predicateTitle = new Resource(TITLE); |
---|
| 70 | $model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle)); |
---|
| 71 | |
---|
| 72 | $questionDescription = new Literal($this->description); |
---|
| 73 | $predicateDescription = new Resource(DESCRIPTION); |
---|
| 74 | $model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription)); |
---|
| 75 | |
---|
| 76 | $resourceQuestionType = new Literal($this->type); |
---|
| 77 | $predicateQType = new Resource(QTYPE); |
---|
| 78 | $model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType)); |
---|
| 79 | |
---|
| 80 | $questionQCategory = new Literal($this->category); |
---|
| 81 | $predicateQCategory = new Resource(QCATEGORY); |
---|
| 82 | $model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory)); |
---|
| 83 | |
---|
| 84 | if(isset($this->answers)) |
---|
| 85 | { |
---|
| 86 | foreach($this->answers as $answer) |
---|
| 87 | { |
---|
| 88 | $answerValue = new Literal($answer); |
---|
| 89 | $predicateAnswer = new Resource(HAS_ANSWER); |
---|
| 90 | $model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue)); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | $model->saveAs(Question::$filename, 'rdf'); |
---|
| 94 | return true; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Get the questions corresponding to the arguments. |
---|
| 99 | * @param type $arguments : An array having one ore more of the following elements: |
---|
| 100 | * 'code', 'title', 'type', 'description', 'category', 'definedanswers'. |
---|
| 101 | */ |
---|
| 102 | public static function get($arguments) |
---|
| 103 | { |
---|
| 104 | $model = ResearchToolObject::load(Question::$filename); |
---|
| 105 | |
---|
| 106 | $querystring = ' |
---|
| 107 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 108 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 109 | SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category |
---|
| 110 | WHERE |
---|
| 111 | { |
---|
| 112 | _question predicates:resource_type resources:question ; |
---|
| 113 | predicates:question_code ?uid ; |
---|
| 114 | predicates:title ?title ; |
---|
| 115 | predicates:question_type ?type ; |
---|
| 116 | predicates:description ?description ; |
---|
| 117 | predicates:question_category ?category ; |
---|
| 118 | '. ResearchToolObject::createArguments($arguments) . ' |
---|
| 119 | }'; |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | //Create the querystring |
---|
| 123 | $results = $model->sparqlQuery($querystring); |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | $questions = array(); |
---|
| 127 | if(!empty($results)) |
---|
| 128 | { |
---|
| 129 | foreach($results as $result) |
---|
| 130 | { |
---|
| 131 | $answers = Question::getAnswers($model, $result['?uid']->label); |
---|
| 132 | $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | return $questions; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Gets the answers belonging to the given uid. |
---|
| 140 | * @param type $uid : The uid of the Question in question (haha, pun). |
---|
| 141 | */ |
---|
| 142 | private static function getAnswers($model, $uid) |
---|
| 143 | { |
---|
| 144 | $result = $model->findRegex("[(".$uid.")]", "[(has_answer)]", null); |
---|
| 145 | $iterator = $result->getStatementIterator(); |
---|
| 146 | $answers = array(); |
---|
| 147 | while($iterator->hasNext()) |
---|
| 148 | { |
---|
| 149 | $element = $iterator->next(); |
---|
| 150 | $answers[] = $element->getLabelObject(); |
---|
| 151 | } |
---|
| 152 | return $answers; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | ?> |
---|