PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
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 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / course / class-lp-course-no-required-enroll.php

class-lp-course-no-required-enroll.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.2, at inc/course/class-lp-course-no-required-enroll.php

205 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Course_No_Required_Enroll.
5 *
6 * @author ThimPress
7 * @package LearnPress/Classes
8 * @version 4.0.9
9 */
10 defined( 'ABSPATH' ) || exit();
11 if ( ! class_exists( 'LP_Course' ) ) {
12 class LP_Course_No_Required_Enroll {
13 public $_course;
14
15 /**
16 * LP_Course_No_Required_Enroll constructor.
17 *
18 * @editor tungnx
19 * @modify 4.1.4.1
20 */
21 public function __construct( LP_Course $course ) {
22 $this->_course = $course;
23 }
24
25 /**
26 * Get result do quiz
27 *
28 * @param LP_Quiz $quiz
29 * @param array $answered
30 *
31 * @since 4.0.0
32 * @version 1.0.1
33 * @return array
34 */
35 public function get_result_quiz( LP_Quiz $quiz, array $answered = [] ): array {
36 $result = array(
37 'questions' => array(),
38 'mark' => $quiz->get_mark(),
39 'user_mark' => 0,
40 'question_count' => 0,
41 'question_empty' => 0,
42 'question_answered' => 0,
43 'question_wrong' => 0,
44 'question_correct' => 0,
45 'status' => LP_ITEM_COMPLETED,
46 'result' => 0,
47 'time_spend' => 0,
48 'passing_grade' => $quiz->get_passing_grade(),
49 'answered' => array(),
50 'pass' => 0,
51 );
52
53 if ( empty( $answered ) ) {
54 return $result;
55 }
56
57 $question_ids = array_keys( $answered );
58 $result['question_count'] = count( $question_ids );
59 $questions = learn_press_rest_prepare_user_questions( $question_ids );
60
61 foreach ( $questions as $key => $question_info ) {
62 $question_id = $question_info['id'];
63 //$question = learn_press_get_question( $question_id );
64 $question = $question_info['object'];
65
66 if ( ! $question ) {
67 continue;
68 }
69
70 $point = floatval( $question->get_mark() );
71
72 $result['questions'][ $question_id ] = $question_info;
73 $result['answered'][ $question_id ] = [];
74 $result['answered'][ $question_id ]['answered'] = $answered[ $question_id ] ?? '';
75
76 if ( isset( $answered[ $question_id ] ) ) { // User's answer
77 $result['question_answered']++;
78
79 $check = $question->check( $answered[ $question_id ] );
80 if ( $check['correct'] ) {
81 $result['question_correct']++;
82 $result['user_mark'] += $point;
83
84 $result['answered'][ $question_id ]['correct'] = true;
85 $result['answered'][ $question_id ]['mark'] = $point;
86 } else {
87 if ( $quiz->get_negative_marking() ) {
88 $result['user_mark'] -= $point;
89 }
90 $result['question_wrong']++;
91
92 $result['answered'][ $question_id ]['correct'] = false;
93 $result['answered'][ $question_id ]['mark'] = 0;
94 }
95 } elseif ( ! array_key_exists( 'instant_check', $answered ) ) { // User skip question
96 if ( $quiz->get_negative_marking() && $quiz->get_minus_skip_questions() ) {
97 $result['user_mark'] -= $point;
98 }
99 $result['question_empty']++;
100
101 $result['answered'][ $question_id ]['correct'] = false;
102 $result['answered'][ $question_id ]['mark'] = 0;
103 }
104
105 $can_review_quiz = get_post_meta( $quiz->get_id(), '_lp_review', true ) === 'yes';
106 if ( $can_review_quiz && ! array_key_exists( 'instant_check', $answered ) ) {
107 $result['questions'][ $question_id ]['explanation'] = $question->get_explanation();
108 $result['questions'][ $question_id ]['options'] = learn_press_get_question_options_for_js(
109 $question,
110 array(
111 'include_is_true' => get_post_meta( $quiz->get_id(), '_lp_show_correct_review', true ) === 'yes',
112 'answer' => $answered[ $question_id ] ?? '',
113 )
114 );
115 }
116 }
117
118 if ( $result['user_mark'] < 0 ) {
119 $result['user_mark'] = 0;
120 }
121
122 if ( $result['user_mark'] > 0 && $result['mark'] > 0 ) {
123 $result['result'] = round( $result['user_mark'] * 100 / $result['mark'], 2, PHP_ROUND_HALF_DOWN );
124 }
125
126 $passing_grade = $quiz->get_data( 'passing_grade', 0 );
127 if ( $result['result'] >= $passing_grade ) {
128 $result['pass'] = 1;
129 } else {
130 $result['pass'] = 0;
131 }
132
133 return $result;
134 }
135
136 /**
137 * @param $question_id
138 * @param null $answered
139 *
140 * @return array|false
141 */
142 public function guest_check_question( $question_id, $answered = null ) {
143 $checked = false;
144 if ( isset( $question_id ) && isset( $answered ) ) {
145 $question = learn_press_get_question( $question_id );
146 $checked = $question->check( $answered );
147 $checked['answered'] = $answered;
148 }
149 return $checked;
150 }
151
152 /**
153 * Start quiz
154 *
155 * @param LP_Quiz $quiz
156 *
157 * @return array
158 * @throws Exception
159 * @editor tungnx
160 * @modify 4.1.4.1
161 */
162 public function guest_start_quiz( LP_Quiz $quiz ): array {
163 $response = array(
164 'status' => 'error',
165 'message' => '',
166 );
167
168 $show_check = $quiz->get_instant_check();
169 $duration = $quiz->get_duration();
170 $show_correct_review = $quiz->get_show_correct_review();
171 $question_ids = $quiz->get_question_ids();
172 $status = LP_ITEM_STARTED;
173 $checked_questions = [];
174 $hinted_questions = [];
175 $time_remaining = $duration->get();
176
177 $questions = learn_press_rest_prepare_user_questions(
178 $question_ids,
179 array(
180 'instant_check' => $show_check,
181 'quiz_status' => $status,
182 'checked_questions' => $checked_questions,
183 'hinted_questions' => $hinted_questions,
184 'answered' => [],
185 'show_correct_review' => $show_correct_review,
186 )
187 );
188
189 $response['status'] = 'success';
190 $results['question_ids'] = $question_ids;
191 $results['questions'] = $questions;
192 $results['total_time'] = $time_remaining;
193 $results['duration'] = $duration->get();
194 $results['status'] = $status; // Must be started
195 $results['retaken'] = 0;
196 $results['attempts'] = [];
197 $results['user_item_id'] = 0;
198 $results['answered'] = [];
199 $response['results'] = $results;
200
201 return $response;
202 }
203 }
204 }
205