1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitQuizResult extends UBItem {
19: 20: 21:
22: const SUBTYPE = "ClipitQuizResult";
23: const REL_QUIZRESULT_CLIPITUSER = "ClipitQuizResult-ClipitUser";
24: const REL_QUIZRESULT_QUIZQUESTION = "ClipitQuizResult-ClipitQuizQuestion";
25: 26: 27:
28: public $quiz_question = 0;
29:
30:
31: public $answer;
32: 33: 34:
35: public $correct = false;
36:
37: 38: 39: 40: 41:
42: protected function copy_from_elgg($elgg_entity) {
43: parent::copy_from_elgg($elgg_entity);
44: $this->answer = $elgg_entity->get("answer");
45: $this->correct = (bool)$elgg_entity->get("correct");
46: $this->quiz_question = (int)static::get_quiz_question($this->id);
47: }
48:
49: 50: 51: 52: 53:
54: protected function copy_to_elgg($elgg_entity) {
55: parent::copy_to_elgg($elgg_entity);
56: $elgg_entity->set("answer", $this->answer);
57: $elgg_entity->set("correct", (bool)$this->correct);
58: }
59:
60: 61: 62: 63: 64:
65: protected function save($double_save=false) {
66:
67: if(empty($this->quiz_question)) {
68: return parent::save($double_save);
69: }
70:
71: $prev_result = static::get_from_question_user($this->quiz_question, elgg_get_logged_in_user_guid());
72:
73: if(empty($prev_result)){
74: parent::save($double_save);
75: ClipitQuizQuestion::add_quiz_results($this->quiz_question, array($this->id));
76: return $this->id;
77: }
78:
79:
80: if(!empty($prev_result) && $prev_result->id !== $this->id){
81: $this->id = $prev_result->id;
82: }
83:
84:
85: return parent::save($double_save);
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96:
97: static function set_properties($id, $prop_value_array) {
98: $new_prop_value_array = array();
99: foreach($prop_value_array as $prop => $value) {
100: if($prop == "correct") {
101: if(strtolower($value) == "true") {
102: $new_prop_value_array["correct"] = true;
103: } elseif(strtolower($value) == "false") {
104: $new_prop_value_array["correct"] = false;
105: } else {
106: $new_prop_value_array["correct"] = (bool)$value;
107: }
108: } if(($prop == "answer") && (!is_array($value))){
109: $new_prop_value_array["answer"] = json_decode($value);
110: } else {
111: $new_prop_value_array[$prop] = $value;
112: }
113: }
114: return parent::set_properties($id, $new_prop_value_array);
115: }
116:
117: static function evaluate_result($result_id){
118: if(empty($result_id)){
119: return null;
120: }
121: $result_properties = static::get_properties($result_id, array("answer", "quiz_question"));
122: $answer = $result_properties["answer"];
123: $quiz_question = $result_properties["quiz_question"];
124:
125: $question_properties = ClipitQuizQuestion::get_properties($quiz_question, array("option_type", "validation_array"));
126: $option_type = (string)$question_properties["option_type"];
127: $validation_array = $question_properties["validation_array"];
128:
129: switch($option_type){
130: case ClipitQuizQuestion::TYPE_SELECT_ONE:
131: case ClipitQuizQuestion::TYPE_SELECT_MULTI:
132: case ClipitQuizQuestion::TYPE_TRUE_FALSE:
133:
134: $correct = true;
135: if(!is_array($answer) || count($answer) != count($validation_array)){
136: $correct = false;
137: } else{
138: $pos = 0;
139: while($pos < count($answer)){
140: if((bool)$answer[$pos] != (bool)$validation_array[$pos]){
141: $correct = false;
142: break;
143: } else{
144: $pos++;
145: }
146: }
147: }
148: return static::set_properties($result_id, array("correct" => $correct));
149: case ClipitQuizQuestion::TYPE_NUMBER:
150: if((float)$answer == (float)$validation_array[0]){
151: $correct = true;
152: } else{
153: $correct = false;
154: }
155: return static::set_properties($result_id, array("correct" => $correct));
156: }
157: return null;
158: }
159:
160: static function get_quiz_question($id) {
161: $rel_array = get_entity_relationships($id, true);
162: foreach($rel_array as $rel) {
163: if($rel->relationship == ClipitQuizQuestion::REL_QUIZQUESTION_QUIZRESULT) {
164: return $question_id = $rel->guid_one;
165: }
166: }
167: return 0;
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: static function get_by_quiz_question($quiz_question_array) {
178: $quiz_result_array = array();
179: foreach($quiz_question_array as $quiz_question_id) {
180: $result_array = ClipitQuizQuestion::get_quiz_results($quiz_question_id);
181: $quiz_result_array[$quiz_question_id] = static::get_by_id($result_array);
182: }
183: return $quiz_result_array;
184: }
185:
186: 187: 188: 189: 190: 191: 192:
193: static function get_from_question_user($quiz_question_id, $user_id){
194: $result_array = static::get_by_owner(array($user_id));
195: if(!empty($result_array)&& !empty($result_array[$user_id])) {
196: foreach ($result_array[$user_id] as $quiz_result) {
197: if ($quiz_result->quiz_question === $quiz_question_id) {
198: return $quiz_result;
199: }
200: }
201: }
202: return 0;
203: }
204: }