1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13: 
 14: 
 15:  16:  17:  18:  19: 
 20: class ClipitQuizQuestion extends UBItem {
 21:      22:  23: 
 24:     const SUBTYPE = "ClipitQuizQuestion";
 25:     const REL_QUIZQUESTION_TAG = "ClipitQuizQuestion-ClipitTag";
 26:     const REL_QUIZQUESTION_QUIZRESULT = "ClipitQuizQuestion-ClipitQuizResult";
 27:     const REL_QUIZQUESTION_IMAGE = "ClipitQuizQuestion-ClipitFile";
 28:     const TYPE_TRUE_FALSE = "true_false";
 29:     const TYPE_SELECT_ONE = "select_one";
 30:     const TYPE_SELECT_MULTI = "select_multi";
 31:     const TYPE_NUMBER = "number";
 32: 
 33:      34:  35: 
 36:     public $option_array = array();
 37:      38:  39: 
 40:     public $validation_array = array();
 41:      42:  43: 
 44:     public $option_type = "";
 45:      46:  47: 
 48:     public $tag_array = array();
 49:      50:  51: 
 52:     public $quiz_result_array = array();
 53:      54:  55: 
 56:     public $video = "";
 57:      58:  59: 
 60:     public $image = 0;
 61:      62:  63: 
 64:     public $difficulty = 0;
 65:      66:  67: 
 68:     public $order = 0;
 69: 
 70:      71:  72:  73:  74: 
 75:     protected function copy_from_elgg($elgg_entity) {
 76:         parent::copy_from_elgg($elgg_entity);
 77:         $this->tag_array = static::get_tags($this->id);
 78:         $this->quiz_result_array = static::get_quiz_results($this->id);
 79:         $this->option_array = (array)$elgg_entity->get("option_array");
 80:         $this->validation_array = (array)$elgg_entity->get("validation_array");
 81:         $this->option_type = (string)$elgg_entity->get("option_type");
 82:         $this->video = (string)$elgg_entity->get("video");
 83:         $this->image = static::get_image((int)$this->id);
 84:         $this->difficulty = (int)$elgg_entity->get("difficulty");
 85:         $this->order = (int)$elgg_entity->get("order");
 86:     }
 87: 
 88:      89:  90:  91:  92: 
 93:     protected function copy_to_elgg($elgg_entity) {
 94:         parent::copy_to_elgg($elgg_entity);
 95:         $elgg_entity->set("option_array", (array)$this->option_array);
 96:         $elgg_entity->set("validation_array", (array)$this->validation_array);
 97:         $elgg_entity->set("option_type", (string)$this->option_type);
 98:         $video_metadata = ClipitVideo::video_url_parser($this->video);
 99:         $elgg_entity->set("video", (string)$video_metadata["url"]);
100:         $elgg_entity->set("difficulty", (int)$this->difficulty);
101:         $elgg_entity->set("order", (int)$this->order);
102:     }
103: 
104:     105: 106: 107: 108: 109: 
110:     protected function save($double_save=false) {
111:         parent::save($double_save);
112:         static::set_tags($this->id, $this->tag_array);
113:         static::set_quiz_results($this->id, $this->quiz_result_array);
114:         static::set_image($this->id, $this->image);
115:         return $this->id;
116:     }
117: 
118:     119: 120: 121: 122: 123: 
124:     static function get_from_quiz($quiz_id){
125:         $quiz_question_ids = ClipitQuiz::get_quiz_questions($quiz_id);
126:         return static::get_by_id($quiz_question_ids);
127:     }
128: 
129:     130: 131: 132: 133: 134: 
135:     static function get_image($id){
136:         $temp = UBCollection::get_items($id, static::REL_QUIZQUESTION_IMAGE);
137:         if(!empty($temp)){
138:             return (int)array_pop($temp);
139:         }
140:         return null;
141:     }
142: 
143:     144: 145: 146: 147: 148: 149: 
150:     static function set_image($id, $image_id){
151:         return UBCollection::set_items($id, array($image_id), static::REL_QUIZQUESTION_IMAGE);
152:     }
153: 
154:     155: 156: 157: 158: 159: 
160:     static function get_from_video($video_url){
161:         $base64_decode = base64_decode($video_url, true);
162:         if($base64_decode != false){
163:             $video_url = $base64_decode;
164:         }
165:         $video_metadata = ClipitVideo::video_url_parser($video_url);
166:         if($video_metadata == false){
167:             return array();
168:         }
169:         $video_url = $video_metadata["url"];
170:         $return_quiz_question_array = array();
171:         $quiz_array = ClipitQuiz::get_all(0, 0, "", true, true);
172:         foreach($quiz_array as $quiz_id){
173:             $quiz_question_array = static::get_from_quiz($quiz_id);
174:             foreach($quiz_question_array as $quiz_question){
175:                 if(strcmp($video_url, $quiz_question->video) === 0){
176:                     $return_quiz_question_array[] = $quiz_question;
177:                 }
178:             }
179:         }
180:         return $return_quiz_question_array;
181:     }
182: 
183:     static function evaluate_results($id){
184:         $quiz_results = static::get_quiz_results($id);
185:         foreach($quiz_results as $quiz_result){
186:             ClipitQuizResult::evaluate_result($quiz_result);
187:         }
188:         return true;
189:     }
190: 
191:     192: 193: 194: 195: 196: 
197:     static function add_quiz_results($id, $result_array) {
198:         return UBCollection::add_items($id, $result_array, static::REL_QUIZQUESTION_QUIZRESULT, true);
199:     }
200: 
201:     202: 203: 204: 205: 206: 
207:     static function set_quiz_results($id, $result_array) {
208:         return UBCollection::set_items($id, $result_array, static::REL_QUIZQUESTION_QUIZRESULT, true);
209:     }
210: 
211:     212: 213: 214: 215: 216: 
217:     static function remove_quiz_results($id, $result_array) {
218:         return UBCollection::remove_items($id, $result_array, static::REL_QUIZQUESTION_QUIZRESULT);
219:     }
220: 
221:     222: 223: 224: 225: 226: 227: 
228:     static function get_quiz_results($id) {
229:         return UBCollection::get_items($id, static::REL_QUIZQUESTION_QUIZRESULT);
230:     }
231: 
232:     233: 234: 235: 236: 237: 238: 239: 
240:     static function add_tags($id, $tag_array) {
241:         return UBCollection::add_items($id, $tag_array, static::REL_QUIZQUESTION_TAG);
242:     }
243: 
244:     245: 246: 247: 248: 249: 250: 251: 
252:     static function set_tags($id, $tag_array) {
253:         return UBCollection::set_items($id, $tag_array, static::REL_QUIZQUESTION_TAG);
254:     }
255: 
256:     257: 258: 259: 260: 261: 262: 263: 
264:     static function remove_tags($id, $tag_array) {
265:         return UBCollection::remove_items($id, $tag_array, static::REL_QUIZQUESTION_TAG);
266:     }
267: 
268:     269: 270: 271: 272: 273: 274: 
275:     static function get_tags($id) {
276:         return UBCollection::get_items($id, static::REL_QUIZQUESTION_TAG);
277:     }
278: 
279:     static function set_type_true_false($id) {
280:         $prop_value_array = array();
281:         $prop_value_array["option_type"] = static::TYPE_TRUE_FALSE;
282:         return static::set_properties($id, $prop_value_array);
283:     }
284: 
285:     static function set_type_select_one($id) {
286:         $prop_value_array = array();
287:         $prop_value_array["option_type"] = static::TYPE_SELECT_ONE;
288:         return static::set_properties($id, $prop_value_array);
289:     }
290: 
291:     static function set_type_select_multi($id) {
292:         $prop_value_array = array();
293:         $prop_value_array["option_type"] = static::TYPE_SELECT_MULTI;
294:         return static::set_properties($id, $prop_value_array);
295:     }
296: 
297:     static function set_type_number($id) {
298:         $prop_value_array = array();
299:         $prop_value_array["option_type"] = static::TYPE_NUMBER;
300:         return static::set_properties($id, $prop_value_array);
301:     }
302: }
303: