| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models\Question; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Databases\QuestionAnswersDB; |
| 7 |
use LearnPress\Filters\QuestionAnswersFilter; |
| 8 |
use LP_Cache; |
| 9 |
use stdClass; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class QuestionAnswerModel |
| 13 |
* Answers for questions in LearnPress. |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
* @since 4.2.9 |
| 17 |
*/ |
| 18 |
class QuestionAnswerModel { |
| 19 |
public $question_answer_id; |
| 20 |
public $question_id; |
| 21 |
public $title; |
| 22 |
public $value; |
| 23 |
public $order; |
| 24 |
public $is_true; |
| 25 |
|
| 26 |
/********** Fields not on table **********/ |
| 27 |
public $meta_data; |
| 28 |
public $is_get_all_metadata = false; |
| 29 |
|
| 30 |
/** Constant */ |
| 31 |
const META_KEY_BLANKS = '_blanks'; |
| 32 |
|
| 33 |
/** |
| 34 |
* If data get from database, map to object. |
| 35 |
* Else create new object to save data to database. |
| 36 |
* |
| 37 |
* @param array|object|mixed $data |
| 38 |
*/ |
| 39 |
public function __construct( $data = null ) { |
| 40 |
if ( $data ) { |
| 41 |
$this->map_to_object( $data ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( is_null( $this->meta_data ) ) { |
| 45 |
$this->meta_data = new stdClass(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Map array, object data to QuestionAnswerModel. |
| 51 |
* Use for data get from database. |
| 52 |
* |
| 53 |
* @param array|object|mixed $data |
| 54 |
* |
| 55 |
* @return QuestionAnswerModel |
| 56 |
*/ |
| 57 |
public function map_to_object( $data ): QuestionAnswerModel { |
| 58 |
foreach ( $data as $key => $value ) { |
| 59 |
if ( property_exists( $this, $key ) ) { |
| 60 |
$this->{$key} = $value; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get question post model. |
| 69 |
* |
| 70 |
* @return QuestionPostModel|false |
| 71 |
*/ |
| 72 |
public function get_question_post_model() { |
| 73 |
return QuestionPostModel::find( $this->question_id, true ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Find question answer by question_answer_id. |
| 78 |
* |
| 79 |
* @param int $question_answer_id |
| 80 |
* @param bool $check_cache |
| 81 |
* |
| 82 |
* @return QuestionAnswerModel|false |
| 83 |
* @throws Exception |
| 84 |
*/ |
| 85 |
public static function find( int $question_answer_id, bool $check_cache = false ) { |
| 86 |
$model = null; |
| 87 |
$filter = new QuestionAnswersFilter(); |
| 88 |
$filter->question_answer_id = $question_answer_id; |
| 89 |
|
| 90 |
$key_cache = "questionAnswerModel/find/{$question_answer_id}"; |
| 91 |
$cache = new LP_Cache(); |
| 92 |
|
| 93 |
// Check cache |
| 94 |
if ( $check_cache ) { |
| 95 |
$model = $cache->get_cache( $key_cache ); |
| 96 |
if ( $model instanceof QuestionAnswerModel ) { |
| 97 |
return $model; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$db = QuestionAnswersDB::getInstance(); |
| 102 |
$db->get_query_single_row( $filter ); |
| 103 |
$query_single_row = $db->get_question_answers( $filter ); |
| 104 |
$rs = $db->wpdb->get_row( $query_single_row ); |
| 105 |
|
| 106 |
if ( $rs instanceof stdClass ) { |
| 107 |
$model = new static( $rs ); |
| 108 |
} |
| 109 |
|
| 110 |
// Set cache |
| 111 |
if ( $model instanceof QuestionAnswerModel ) { |
| 112 |
$cache->set_cache( $key_cache, $model ); |
| 113 |
} |
| 114 |
|
| 115 |
return $model; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get all meta_data |
| 120 |
* |
| 121 |
* @return mixed|null |
| 122 |
*/ |
| 123 |
public function get_all_metadata() { |
| 124 |
if ( $this->is_get_all_metadata ) { |
| 125 |
return $this->meta_data; |
| 126 |
} |
| 127 |
|
| 128 |
$questionPostModel = $this->get_question_post_model(); |
| 129 |
if ( $questionPostModel && $questionPostModel->get_type() === 'fill_in_blanks' ) { |
| 130 |
$blanks = learn_press_get_question_answer_meta( $this->question_answer_id, '_blanks' ); |
| 131 |
$this->meta_data = $blanks; |
| 132 |
$this->is_get_all_metadata = true; |
| 133 |
} |
| 134 |
|
| 135 |
return $this->meta_data; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get meta value by key. |
| 140 |
* |
| 141 |
* @param string $key |
| 142 |
* @param mixed $value |
| 143 |
* |
| 144 |
* @return void |
| 145 |
* @since 4.2.9 |
| 146 |
* @version 1.0.0 |
| 147 |
*/ |
| 148 |
public function save_meta_value_by_key( string $key, $value ) { |
| 149 |
if ( ! $this->meta_data instanceof stdClass ) { |
| 150 |
$this->meta_data = new stdClass(); |
| 151 |
} |
| 152 |
|
| 153 |
$this->meta_data->{$key} = $value; |
| 154 |
update_metadata( 'learnpress_question_answer', $this->question_answer_id, $key, $value ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Check capabilities of current user to create question answer. |
| 159 |
* |
| 160 |
* @throws Exception |
| 161 |
*/ |
| 162 |
public function check_capabilities_create() { |
| 163 |
$user = wp_get_current_user(); |
| 164 |
if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT, $this->question_id ) ) { |
| 165 |
throw new Exception( __( 'You do not have permission to create answer.', 'learnpress' ) ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Check capabilities of current user to update question answer. |
| 171 |
* |
| 172 |
* @throws Exception |
| 173 |
*/ |
| 174 |
public function check_capabilities_update() { |
| 175 |
$user = wp_get_current_user(); |
| 176 |
if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT, $this->question_id ) ) { |
| 177 |
throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Update data to database. |
| 183 |
* |
| 184 |
* If question_answer_id is empty, insert new data, else update data. |
| 185 |
* |
| 186 |
* @throws Exception |
| 187 |
* @since 4.2.9 |
| 188 |
* @version 1.0.0 |
| 189 |
*/ |
| 190 |
public function save() { |
| 191 |
$db = QuestionAnswersDB::getInstance(); |
| 192 |
$data = []; |
| 193 |
foreach ( get_object_vars( $this ) as $property => $value ) { |
| 194 |
$data[ $property ] = $value; |
| 195 |
} |
| 196 |
|
| 197 |
$args = [ |
| 198 |
'data' => $data, |
| 199 |
'filter' => new QuestionAnswersFilter(), |
| 200 |
'table_name' => $db->tb_lp_question_answers, |
| 201 |
]; |
| 202 |
|
| 203 |
// Check if exists course id. |
| 204 |
if ( empty( $this->question_answer_id ) ) { // Insert data. |
| 205 |
$this->check_capabilities_create(); |
| 206 |
$args['key_auto_increment'] = 'question_answer_id'; |
| 207 |
$this->question_answer_id = $db->insert_data( $args ); |
| 208 |
} else { // Update data. |
| 209 |
$this->check_capabilities_update(); |
| 210 |
$args['where_key'] = 'question_answer_id'; |
| 211 |
$db->update_data( $args ); |
| 212 |
} |
| 213 |
|
| 214 |
$this->clean_caches(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @throws Exception |
| 219 |
* |
| 220 |
* @since 4.2.9 |
| 221 |
* @version 1.0.1 |
| 222 |
*/ |
| 223 |
public function check_valid_before_delete() { |
| 224 |
$questionPostModel = $this->get_question_post_model(); |
| 225 |
if ( ! $questionPostModel ) { |
| 226 |
throw new Exception( __( 'Question not found', 'learnpress' ) ); |
| 227 |
} |
| 228 |
|
| 229 |
$this->check_capabilities_update(); |
| 230 |
|
| 231 |
if ( $questionPostModel->get_type() === 'single_choice' || $questionPostModel->get_type() === 'multi_choice' ) { |
| 232 |
// For single choice and multiple choice, at least two answer is required. |
| 233 |
$filter = new QuestionAnswersFilter(); |
| 234 |
$filter->question_id = $this->question_id; |
| 235 |
$total_rows = 0; |
| 236 |
$filter->query_count = true; |
| 237 |
$answers = (int) QuestionAnswersDB::getInstance()->get_question_answers( $filter, $total_rows ); |
| 238 |
if ( $answers <= 2 ) { |
| 239 |
throw new Exception( __( 'At least two answer is required.', 'learnpress' ) ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Delete row |
| 246 |
* |
| 247 |
* @throws Exception |
| 248 |
*/ |
| 249 |
public function delete() { |
| 250 |
$this->check_valid_before_delete(); |
| 251 |
|
| 252 |
$db = QuestionAnswersDB::getInstance(); |
| 253 |
$filter = new QuestionAnswersFilter(); |
| 254 |
$filter->where[] = $db->wpdb->prepare( 'AND question_answer_id = %d', $this->question_answer_id ); |
| 255 |
$filter->collection = $db->tb_lp_question_answers; |
| 256 |
$db->delete_execute( $filter ); |
| 257 |
|
| 258 |
// Clear cache |
| 259 |
$this->clean_caches(); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Clean caches. |
| 264 |
*/ |
| 265 |
public function clean_caches() { |
| 266 |
$lpQuizCache = new LP_Cache(); |
| 267 |
$key_cache = "questionAnswerModel/find/{$this->question_answer_id}"; |
| 268 |
$lpQuizCache->clear( $key_cache ); |
| 269 |
} |
| 270 |
} |
| 271 |
|