| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Question Post Model |
| 5 |
* To replace class LP_Question old |
| 6 |
* |
| 7 |
* @package LearnPress/Classes |
| 8 |
* @version 1.0.0 |
| 9 |
* @since 4.2.9 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace LearnPress\Models\Question; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use LearnPress\Databases\QuestionAnswersDB; |
| 16 |
use LearnPress\Filters\QuestionAnswersFilter; |
| 17 |
use LearnPress\Models\PostModel; |
| 18 |
use LP_Cache; |
| 19 |
use LP_Debug; |
| 20 |
use LP_Question_Filter; |
| 21 |
|
| 22 |
class QuestionPostModel extends PostModel { |
| 23 |
/** |
| 24 |
* @var string Post Type |
| 25 |
*/ |
| 26 |
public $post_type = LP_QUESTION_CPT; |
| 27 |
|
| 28 |
protected $answer_options = []; |
| 29 |
|
| 30 |
private $question_type = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Const meta key |
| 34 |
*/ |
| 35 |
const META_KEY_TYPE = '_lp_type'; |
| 36 |
const META_KEY_MARK = '_lp_mark'; |
| 37 |
const META_KEY_HINT = '_lp_hint'; |
| 38 |
const META_KEY_EXPLANATION = '_lp_explanation'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Get post question by ID |
| 42 |
* |
| 43 |
* @param int $post_id |
| 44 |
* @param bool $check_cache |
| 45 |
* |
| 46 |
* @return false|static |
| 47 |
*/ |
| 48 |
public static function find( int $post_id, bool $check_cache = false ) { |
| 49 |
$filter_post = new LP_Question_Filter(); |
| 50 |
$filter_post->ID = $post_id; |
| 51 |
|
| 52 |
$key_cache = "questionPostModel/find/{$post_id}"; |
| 53 |
$lpQuizCache = new LP_Cache(); |
| 54 |
|
| 55 |
// Check cache |
| 56 |
if ( $check_cache ) { |
| 57 |
$questionPostModel = $lpQuizCache->get_cache( $key_cache ); |
| 58 |
if ( $questionPostModel instanceof QuestionPostModel ) { |
| 59 |
return $questionPostModel; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$questionPostModel = self::get_item_model_from_db( $filter_post ); |
| 64 |
// Set cache |
| 65 |
if ( $questionPostModel instanceof QuestionPostModel ) { |
| 66 |
$lpQuizCache->set_cache( $key_cache, $questionPostModel ); |
| 67 |
} |
| 68 |
|
| 69 |
return $questionPostModel; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get answer options for question |
| 74 |
*/ |
| 75 |
public function get_answer_option() { |
| 76 |
try { |
| 77 |
if ( empty( $this->answer_options ) ) { |
| 78 |
$db = QuestionAnswersDB::getInstance(); |
| 79 |
$filter = new QuestionAnswersFilter(); |
| 80 |
$filter->question_id = $this->get_id(); |
| 81 |
$filter->limit = -1; // Get all answers for question |
| 82 |
$answers_rs = $db->get_question_answers( $filter ); |
| 83 |
$answer_options = []; |
| 84 |
|
| 85 |
foreach ( $answers_rs as $answer ) { |
| 86 |
$questionAnswerModel = new QuestionAnswerModel( $answer ); |
| 87 |
$questionAnswerModel->get_all_metadata(); |
| 88 |
$answer_options[] = $questionAnswerModel; |
| 89 |
} |
| 90 |
|
| 91 |
$this->answer_options = $answer_options; |
| 92 |
} |
| 93 |
} catch ( Exception $e ) { |
| 94 |
LP_Debug::error_log( $e ); |
| 95 |
} |
| 96 |
|
| 97 |
return $this->answer_options; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get all types of question |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public static function get_types(): array { |
| 106 |
$types = [ |
| 107 |
'true_or_false' => esc_html__( 'True Or False', 'learnpress' ), |
| 108 |
'multi_choice' => esc_html__( 'Multi Choice', 'learnpress' ), |
| 109 |
'single_choice' => esc_html__( 'Single Choice', 'learnpress' ), |
| 110 |
'fill_in_blanks' => esc_html__( 'Fill In Blanks', 'learnpress' ), |
| 111 |
]; |
| 112 |
|
| 113 |
return apply_filters( 'learn-press/question-types', $types ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get question object by type |
| 118 |
* |
| 119 |
* @param string $type |
| 120 |
* |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public static function get_question_obj_by_type( string $type = '' ): string { |
| 124 |
$types = self::get_types(); |
| 125 |
|
| 126 |
if ( ! array_key_exists( $type, $types ) ) { |
| 127 |
return ''; |
| 128 |
} |
| 129 |
|
| 130 |
// For addon sorting choice old <= v4.0.1 |
| 131 |
if ( class_exists( 'LP_Addon_Sorting_Choice_Preload' ) && $type === 'sorting_choice' ) { |
| 132 |
if ( version_compare( LP_ADDON_SORTING_CHOICE_VER, '4.0.1', '<=' ) ) { |
| 133 |
return QuestionSortingChoiceModel::class; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
switch ( $type ) { |
| 138 |
case 'true_or_false': |
| 139 |
return QuestionPostTrueFalseModel::class; |
| 140 |
case 'multi_choice': |
| 141 |
return QuestionPostMultipleChoiceModel::class; |
| 142 |
case 'single_choice': |
| 143 |
return QuestionPostSingleChoiceModel::class; |
| 144 |
case 'fill_in_blanks': |
| 145 |
return QuestionPostFIBModel::class; |
| 146 |
default: |
| 147 |
return apply_filters( 'learn-press/question-object-by-type', '', $type ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get type question |
| 153 |
* |
| 154 |
* @return string|float |
| 155 |
*/ |
| 156 |
public function get_type() { |
| 157 |
if ( empty( $this->question_type ) ) { |
| 158 |
$this->question_type = $this->get_meta_value_by_key( self::META_KEY_TYPE, '' ); |
| 159 |
} |
| 160 |
|
| 161 |
return $this->question_type; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Check type question is valid |
| 166 |
* |
| 167 |
* @param string $type |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public static function check_type_valid( string $type ): bool { |
| 172 |
$types = self::get_types(); |
| 173 |
|
| 174 |
if ( ! array_key_exists( $type, $types ) ) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get type label by type |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function get_type_label(): string { |
| 187 |
$type = $this->get_type(); |
| 188 |
$types = self::get_types(); |
| 189 |
|
| 190 |
if ( ! array_key_exists( $type, $types ) ) { |
| 191 |
return ''; |
| 192 |
} |
| 193 |
|
| 194 |
return $types[ $type ]; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @return mixed |
| 199 |
*/ |
| 200 |
public function get_hint() { |
| 201 |
return $this->get_meta_value_by_key( self::META_KEY_HINT, '' ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* @return mixed |
| 206 |
*/ |
| 207 |
public function get_explanation() { |
| 208 |
return $this->get_meta_value_by_key( self::META_KEY_EXPLANATION, '' ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @return mixed |
| 213 |
*/ |
| 214 |
public function get_mark() { |
| 215 |
return $this->get_meta_value_by_key( self::META_KEY_MARK, 1 ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Create default answers for question |
| 220 |
* For case question does not have answers yet. |
| 221 |
* |
| 222 |
* @return array |
| 223 |
* @throws Exception |
| 224 |
*/ |
| 225 |
public function create_default_answers(): array { |
| 226 |
$answers = $this->get_default_answers(); |
| 227 |
|
| 228 |
foreach ( $answers as $index => $answer ) { |
| 229 |
$answer = array( |
| 230 |
'question_id' => $this->get_id(), |
| 231 |
'title' => $answer['title'], |
| 232 |
'value' => $answer['value'] ?? '', |
| 233 |
'is_true' => $answer['is_true'] ?? '', |
| 234 |
'order' => $index + 1, |
| 235 |
); |
| 236 |
|
| 237 |
$questionAnswerModel = new QuestionAnswerModel( $answer ); |
| 238 |
$questionAnswerModel->save(); |
| 239 |
$answers[ $index ]['question_answer_id'] = $questionAnswerModel->question_answer_id; |
| 240 |
} |
| 241 |
|
| 242 |
return $answers; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get default question list answers. |
| 247 |
* |
| 248 |
* @return array |
| 249 |
* @move from class LP_Question old |
| 250 |
*/ |
| 251 |
public function get_default_answers(): array { |
| 252 |
return []; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Generate random value |
| 257 |
* |
| 258 |
* @param int $length |
| 259 |
* |
| 260 |
* @return string |
| 261 |
*/ |
| 262 |
public function random_value( int $length = 10 ): string { |
| 263 |
return substr( md5( uniqid( mt_rand(), true ) ), 0, $length ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Check capabilities create question |
| 268 |
* |
| 269 |
* @return bool |
| 270 |
*/ |
| 271 |
public function check_capabilities_create(): bool { |
| 272 |
return current_user_can( 'edit_' . LP_LESSON_CPT . 's' ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Check capabilities update question |
| 277 |
* |
| 278 |
* @return bool |
| 279 |
*/ |
| 280 |
public function check_capabilities_update(): bool { |
| 281 |
return current_user_can( 'edit_' . LP_LESSON_CPT, $this->ID ); |
| 282 |
} |
| 283 |
} |
| 284 |
|