Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • ClipitActivity
  • ClipitChat
  • ClipitComment
  • ClipitEvent
  • ClipitExample
  • ClipitFile
  • ClipitGroup
  • ClipitLA
  • ClipitLabel
  • ClipitPerformanceItem
  • ClipitPerformanceRating
  • ClipitPost
  • ClipitQuiz
  • ClipitQuizQuestion
  • ClipitQuizResult
  • ClipitRating
  • ClipitRemoteTrickyTopic
  • ClipitRemoteVideo
  • ClipitResource
  • ClipitSite
  • ClipitStoryboard
  • ClipitTag
  • ClipitTagRating
  • ClipitTask
  • ClipitTrickyTopic
  • ClipitUser
  • ClipitVideo
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * ClipIt - JuxtaLearn Web Space
  4:  * PHP version:     >= 5.2
  5:  * Creation date:   2013-10-10
  6:  * Last update:     $Date$
  7:  * @author          Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
  8:  * @version         $Version$
  9:  * @link            http://www.juxtalearn.eu
 10:  * @license         GNU Affero General Public License v3
 11:  * @package         ClipIt
 12:  * @subpackage      clipit_api
 13:  */
 14: 
 15: /**
 16:  * A Student Result for a Quiz Question, with a link to it, and a boolean value to show whether it is Correct or not.
 17:  */
 18: class ClipitQuizResult extends UBItem {
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 21:      */
 22:     const SUBTYPE = "ClipitQuizResult";
 23:     const REL_QUIZRESULT_CLIPITUSER = "ClipitQuizResult-ClipitUser";
 24:     const REL_QUIZRESULT_QUIZQUESTION = "ClipitQuizResult-ClipitQuizQuestion";
 25:     /**
 26:      * @var int Id of ClipitQuizQuestion this ClipitQuizResult is related to
 27:      */
 28:     public $quiz_question = 0;
 29: 
 30:     // can be different types, depending on the question type
 31:     public $answer;
 32:     /**
 33:      * @var bool Determines if this Result is correct (true) or incorrect (false) - computed by "evaluate_result"
 34:      */
 35:     public $correct = false;
 36: 
 37:     /**
 38:      * Loads object parameters stored in Elgg
 39:      *
 40:      * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
 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:      * Copy $this object parameters into an Elgg entity.
 51:      *
 52:      * @param ElggEntity $elgg_entity Elgg object instance to save $this to
 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:      * Saves this instance to the system.
 62:      * @param  bool $double_save if $double_save is true, this object is saved twice to ensure that all properties are updated properly. E.g. the time created property can only beset on ElggObjects during an update. Defaults to false!
 63:      * @return bool|int Returns the Id of the saved instance, or false if error
 64:      */
 65:     protected function save($double_save=false) {
 66:         // no quiz question specified: just save object and exit.
 67:         if(empty($this->quiz_question)) {
 68:             return parent::save($double_save);
 69:         }
 70:         // get possible previous result for this user and for target question.
 71:         $prev_result = static::get_from_question_user($this->quiz_question, elgg_get_logged_in_user_guid());
 72:         // if no previous result, save new object, add relationship and exit.
 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:         // if there is a previous result with different id than this->id (which may be empty yet),
 79:         // we take the existing result->id as this->id, and save the current properties onto that id.
 80:         if(!empty($prev_result) && $prev_result->id !== $this->id){
 81:             $this->id = $prev_result->id;
 82:         }
 83:         // else if a previous result exists but has the same id as $this (older version of us),
 84:         // then update with current properties and exit.
 85:         return parent::save($double_save);
 86:     }
 87: 
 88:     /**
 89:      * Sets values to specified properties of an Item
 90:      *
 91:      * @param int   $id Id of Item to set property values
 92:      * @param array $prop_value_array Array of property=>value pairs to set into the Item
 93:      *
 94:      * @return int|bool Returns Id of Item if correct, or false if error
 95:      * @throws InvalidParameterException
 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:                 // answer is correct until proven wrong
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:      * Get Quiz Results by Quiz Questions
172:      *
173:      * @param array $quiz_question_array Array of Quiz Question IDs to get Results form
174:      *
175:      * @return array|bool Array of Quiz Results nested per Quiz Question IDs, or false if error
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:      * Returns the ClipitQuizResult ID for a Quiz Question submitted by a User
188:      *
189:      * @param int $quiz_question_id
190:      * @param int $user_id
191:      * @return static The Quiz Result, or 0 if none found.
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: }
API documentation generated by ApiGen 2.8.0