PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 All 142 releases
learnpress / inc / Models / Question / QuestionPostModel.php

QuestionPostModel.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.10, at inc/Models/Question/QuestionPostModel.php

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