| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models\Quiz; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Databases\QuizQuestionsDB; |
| 7 |
use LearnPress\Filters\QuizQuestionsFilter; |
| 8 |
use LearnPress\Models\Question\QuestionPostModel; |
| 9 |
use LearnPress\Models\QuizPostModel; |
| 10 |
use LP_Cache; |
| 11 |
use stdClass; |
| 12 |
use Throwable; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class QuizQuestionModel |
| 16 |
* |
| 17 |
* Handle all method about quiz question. |
| 18 |
* |
| 19 |
* @package LearnPress/Classes |
| 20 |
* @version 1.0.1 |
| 21 |
* @since 4.2.9 |
| 22 |
*/ |
| 23 |
class QuizQuestionModel { |
| 24 |
/** |
| 25 |
* Auto increment, Primary key |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
public $quiz_question_id = 0; |
| 30 |
/** |
| 31 |
* Title of the section |
| 32 |
* |
| 33 |
* @var int |
| 34 |
*/ |
| 35 |
public $quiz_id = 0; |
| 36 |
/** |
| 37 |
* Foreign key, Course ID |
| 38 |
* |
| 39 |
* @var int |
| 40 |
*/ |
| 41 |
public $question_id = 0; |
| 42 |
/** |
| 43 |
* Order of the section |
| 44 |
* |
| 45 |
* @var int |
| 46 |
*/ |
| 47 |
public $question_order = 0; |
| 48 |
|
| 49 |
/** |
| 50 |
* If data get from database, map to object. |
| 51 |
* Else create new object to save data to database. |
| 52 |
* |
| 53 |
* @param array|object|mixed $data |
| 54 |
*/ |
| 55 |
public function __construct( $data = null ) { |
| 56 |
if ( $data ) { |
| 57 |
$this->map_to_object( $data ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Map array, object data to QuizQuestionModel. |
| 63 |
* Use for data get from database. |
| 64 |
* |
| 65 |
* @param array|object|mixed $data |
| 66 |
* |
| 67 |
* @return QuizQuestionModel |
| 68 |
*/ |
| 69 |
public function map_to_object( $data ): QuizQuestionModel { |
| 70 |
foreach ( $data as $key => $value ) { |
| 71 |
if ( property_exists( $this, $key ) ) { |
| 72 |
$this->{$key} = $value; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get section id |
| 81 |
* |
| 82 |
* @return int |
| 83 |
*/ |
| 84 |
public function get_quiz_question_id(): int { |
| 85 |
return $this->quiz_question_id; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get course model |
| 90 |
* |
| 91 |
* @return false|QuizPostModel |
| 92 |
*/ |
| 93 |
public function get_quiz_post_model() { |
| 94 |
return QuizPostModel::find( $this->quiz_id, true ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get course model |
| 99 |
* |
| 100 |
* @return false|QuestionPostModel |
| 101 |
*/ |
| 102 |
public function get_question_post_model() { |
| 103 |
return QuestionPostModel::find( $this->question_id, true ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get quizQuestionsModel by quiz_id and question_id. |
| 108 |
* |
| 109 |
* @return false|QuizQuestionModel |
| 110 |
*/ |
| 111 |
public static function find( int $quiz_id, int $question_id, $check_cache = true ) { |
| 112 |
$filter = new QuizQuestionsFilter(); |
| 113 |
$filter->quiz_id = $quiz_id; |
| 114 |
$filter->question_id = $question_id; |
| 115 |
$key_cache = "quizQuestion/find/{$quiz_id}/{$question_id}"; |
| 116 |
$cache = new LP_Cache(); |
| 117 |
|
| 118 |
// Check cache |
| 119 |
if ( $check_cache ) { |
| 120 |
$model = $cache->get_cache( $key_cache ); |
| 121 |
if ( $model instanceof QuizQuestionModel ) { |
| 122 |
return $model; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$model = static::get_item_model_from_db( $filter ); |
| 127 |
|
| 128 |
// Set cache |
| 129 |
if ( $model instanceof QuizQuestionModel ) { |
| 130 |
$cache->set_cache( $key_cache, $model ); |
| 131 |
} |
| 132 |
|
| 133 |
return $model; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get post from database. |
| 138 |
* If not exists, return false. |
| 139 |
* If exists, return QuizQuestionModel. |
| 140 |
* |
| 141 |
* @param QuizQuestionsFilter $filter |
| 142 |
* |
| 143 |
* @return QuizQuestionModel|false|static |
| 144 |
* @version 1.0.0 |
| 145 |
*/ |
| 146 |
public static function get_item_model_from_db( QuizQuestionsFilter $filter ) { |
| 147 |
$db = QuizQuestionsDB::getInstance(); |
| 148 |
$model = false; |
| 149 |
|
| 150 |
try { |
| 151 |
$db->get_query_single_row( $filter ); |
| 152 |
$query_single_row = $db->get_quiz_questions( $filter ); |
| 153 |
$rs = $db->wpdb->get_row( $query_single_row ); |
| 154 |
|
| 155 |
if ( $rs instanceof stdClass ) { |
| 156 |
$model = new static( $rs ); |
| 157 |
} |
| 158 |
} catch ( Throwable $e ) { |
| 159 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 160 |
} |
| 161 |
|
| 162 |
return $model; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Save data to table quiz_questions. |
| 167 |
* |
| 168 |
* @throws Exception |
| 169 |
* @since 4.2.8.6 |
| 170 |
* @version 1.0.1 |
| 171 |
*/ |
| 172 |
public function save(): QuizQuestionModel { |
| 173 |
$quizPostModel = $this->get_quiz_post_model(); |
| 174 |
if ( ! $quizPostModel ) { |
| 175 |
throw new Exception( __( 'Quiz not found', 'learnpress' ) ); |
| 176 |
} |
| 177 |
|
| 178 |
$quizPostModel->check_capabilities_update_item_course(); |
| 179 |
|
| 180 |
$db = QuizQuestionsDB::getInstance(); |
| 181 |
|
| 182 |
$data = []; |
| 183 |
foreach ( get_object_vars( $this ) as $property => $value ) { |
| 184 |
$data[ $property ] = $value; |
| 185 |
} |
| 186 |
|
| 187 |
if ( $data['quiz_question_id'] === 0 ) { // Insert data. |
| 188 |
$quiz_question_id = $db->insert_data( $data ); |
| 189 |
$this->quiz_question_id = $quiz_question_id; |
| 190 |
} else { // Update data. |
| 191 |
$db->update_data( $data ); |
| 192 |
} |
| 193 |
|
| 194 |
// Clear cache |
| 195 |
$this->clean_caches(); |
| 196 |
|
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Delete row |
| 202 |
* |
| 203 |
* @throws Exception |
| 204 |
* @since 4.2.8.6 |
| 205 |
* @version 1.0.1 |
| 206 |
*/ |
| 207 |
public function delete() { |
| 208 |
// Check permission |
| 209 |
$quizPostModel = $this->get_quiz_post_model(); |
| 210 |
if ( ! $quizPostModel ) { |
| 211 |
throw new Exception( __( 'Quiz not found', 'learnpress' ) ); |
| 212 |
} |
| 213 |
|
| 214 |
$quizPostModel->check_capabilities_update_item_course(); |
| 215 |
|
| 216 |
$db = QuizQuestionsDB::getInstance(); |
| 217 |
$filter = new QuizQuestionsFilter(); |
| 218 |
$filter->where[] = $db->wpdb->prepare( 'AND quiz_question_id = %d', $this->quiz_question_id ); |
| 219 |
$filter->collection = $db->tb_lp_quiz_questions; |
| 220 |
$db->delete_execute( $filter ); |
| 221 |
|
| 222 |
// Clear cache |
| 223 |
$this->clean_caches(); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Clean caches |
| 228 |
* |
| 229 |
* @return void |
| 230 |
* @throws Exception |
| 231 |
* @since 4.2.9 |
| 232 |
* @version 1.0.0 |
| 233 |
*/ |
| 234 |
public function clean_caches() { |
| 235 |
$key_cache = "quizQuestion/find/{$this->quiz_id}/{$this->question_id}"; |
| 236 |
$lp_course_cache = new LP_Cache(); |
| 237 |
$lp_course_cache->clear( $key_cache ); |
| 238 |
} |
| 239 |
} |
| 240 |
|