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
← All changes | inc/Models/Question/QuestionPostModel.php +288 -276 4.3.9.1 → 4.4.10 View file →
@@ -1,276 +1,288 @@
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 ClassPress\Helpers\Debug;
15 -use Exception;
16 -use LearnPress\Databases\QuestionAnswersDB;
17 -use LearnPress\Filters\QuestionAnswersFilter;
18 -use LearnPress\Models\PostModel;
19 -use LP_Cache;
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 - 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 - * @param $question_type
166 - * @return $this
167 - */
168 - public function set_type($question_type)
169 - {
170 - $this->question_type = $question_type;
171 -
172 - return $this;
173 - }
174 -
175 - /**
176 - * Check type question is valid
177 - *
178 - * @param string $type
179 - *
180 - * @return void
181 - */
182 - public static function check_type_valid( string $type ): bool {
183 - $types = self::get_types();
184 -
185 - if ( ! array_key_exists( $type, $types ) ) {
186 - return false;
187 - }
188 -
189 - return true;
190 - }
191 -
192 - /**
193 - * Get type label by type
194 - *
195 - * @return string
196 - */
197 - public function get_type_label(): string {
198 - $type = $this->get_type();
199 - $types = self::get_types();
200 -
201 - if ( ! array_key_exists( $type, $types ) ) {
202 - return '';
203 - }
204 -
205 - return $types[ $type ];
206 - }
207 -
208 - /**
209 - * @return mixed
210 - */
211 - public function get_hint() {
212 - return $this->get_meta_value_by_key( self::META_KEY_HINT, '' );
213 - }
214 -
215 - /**
216 - * @return mixed
217 - */
218 - public function get_explanation() {
219 - return $this->get_meta_value_by_key( self::META_KEY_EXPLANATION, '' );
220 - }
221 -
222 - /**
223 - * @return mixed
224 - */
225 - public function get_mark() {
226 - return $this->get_meta_value_by_key( self::META_KEY_MARK, 1 );
227 - }
228 -
229 - /**
230 - * Create default answers for question
231 - * For case question does not have answers yet.
232 - *
233 - * @return array
234 - * @throws Exception
235 - */
236 - public function create_default_answers(): array {
237 - $answers = $this->get_default_answers();
238 -
239 - foreach ( $answers as $index => $answer ) {
240 - $answer = array(
241 - 'question_id' => $this->get_id(),
242 - 'title' => $answer['title'],
243 - 'value' => $answer['value'] ?? '',
244 - 'is_true' => $answer['is_true'] ?? '',
245 - 'order' => $index + 1,
246 - );
247 -
248 - $questionAnswerModel = new QuestionAnswerModel( $answer );
249 - $questionAnswerModel->save();
250 - $answers[ $index ]['question_answer_id'] = $questionAnswerModel->question_answer_id;
251 - }
252 -
253 - return $answers;
254 - }
255 -
256 - /**
257 - * Get default question list answers.
258 - *
259 - * @return array
260 - * @move from class LP_Question old
261 - */
262 - public function get_default_answers(): array {
263 - return [];
264 - }
265 -
266 - /**
267 - * Generate random value
268 - *
269 - * @param int $length
270 - *
271 - * @return string
272 - */
273 - public function random_value( int $length = 10 ): string {
274 - return substr( md5( uniqid( mt_rand(), true ) ), 0, $length );
275 - }
276 -}
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 +}