Skip to content

Commit

Permalink
= 4.2.7.6 =
Browse files Browse the repository at this point in the history
~ Refactor code handle Quiz.
  • Loading branch information
tungnxt89 committed Dec 23, 2024
1 parent 7fafee6 commit 66d6757
Show file tree
Hide file tree
Showing 15 changed files with 544 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Buttons extends Component {

if ( 'no' === confirm( __( 'Are you sure you want to retake the quiz?', 'learnpress' ), this.startQuiz ) ) {
! isOpen() && btn && btn.removeAttribute( 'disabled' );
btn.classList.remove( 'loading' );
return;
}
}
Expand Down
10 changes: 6 additions & 4 deletions assets/src/scss/learnpress.scss
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ ul.learn-press-breadcrumb {
/**
* Forms
*/
.retake-course,
.enroll-course,
.purchase-course {
display: inline-block;
form.retake-course,
form.enroll-course,
form.purchase-course {
display: inline-flex;
flex-direction: column;
gap: 10px;
}

/* */
Expand Down
3 changes: 1 addition & 2 deletions inc/Databases/class-lp-post-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function get_posts( LP_Post_Type_Filter $filter, int &$total_rows = 0 ) {

// Find ID
if ( isset( $filter->ID ) ) {
$filter->where[] = $this->wpdb->prepare( "AND $ca.ID = %d", $filter->ID );
$filter->where[] = $this->wpdb->prepare( "AND $ca.ID = %d", $filter->ID );
}

// Status
Expand Down Expand Up @@ -101,4 +101,3 @@ public function get_posts( LP_Post_Type_Filter $filter, int &$total_rows = 0 ) {
return $this->execute( $filter, $total_rows );
}
}

25 changes: 25 additions & 0 deletions inc/Filters/class-lp-quiz-filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Class LP_Quiz_Filter
*
* @author ThimPress
* @package LearnPress/Classes/Filters
* @since 4.2.7.6
* @version 1.0.1
*/

/**
* Prevent loading this file directly
*/
defined( 'ABSPATH' ) || exit();

if ( class_exists( 'LP_Quiz_Filter' ) ) {
return;
}

class LP_Quiz_Filter extends LP_Post_Type_Filter {
/**
* @var string
*/
public $post_type = LP_QUIZ_CPT;
}
16 changes: 3 additions & 13 deletions inc/Models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,26 +1059,16 @@ public function can_purchase( $user ) {
* If exists, return PostModel.
*
* @param LP_Course_JSON_Filter $filter
* @param bool $check_cache
*
* @return CourseModel|false|static
* @since 4.2.6.9
* @version 1.0.2
*/
public static function get_item_model_from_db( LP_Course_JSON_Filter $filter, bool $check_cache = false ) {
public static function get_item_model_from_db( LP_Course_JSON_Filter $filter ) {
$course_model = false;

try {
$filter->only_fields = [ 'json', 'post_content' ];
// Load cache
if ( $check_cache ) {

$key_cache = "course-model/{$filter->ID}/" . md5( json_encode( $filter ) );
$lp_course_cache = new LP_Course_Cache();
$course_model = $lp_course_cache->get_cache( $key_cache );

if ( $course_model instanceof CourseModel ) {
return $course_model;
}
}

$course_rs = self::get_course_from_db( $filter );
if ( $course_rs instanceof stdClass && isset( $course_rs->json ) ) {
Expand Down
6 changes: 4 additions & 2 deletions inc/Models/PostModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public function __construct( $data = null ) {
* Get user model
*
* @return false|UserModel
* @since 4.2.6.9
* @version 1.0.1
*/
public function get_author_model() {
if ( ! empty( $this->post_author ) ) {
Expand Down Expand Up @@ -145,11 +147,11 @@ public function map_to_object( $data ): PostModel {
* If exists, return PostModel.
*
* @param LP_Course_Filter $filter
* @param bool $check_cache
*
* @return PostModel|false|static
* @version 1.0.1
*/
public static function get_item_model_from_db( LP_Post_Type_Filter $filter, bool $check_cache = false ) {
public static function get_item_model_from_db( LP_Post_Type_Filter $filter ) {
$lp_post_db = LP_Post_DB::getInstance();
$post_model = false;

Expand Down
219 changes: 219 additions & 0 deletions inc/Models/QuizPostModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php

/**
* Class Quiz Post Model
*
* @package LearnPress/Classes
* @version 1.0.0
* @since 4.2.7.6
*/

namespace LearnPress\Models;

use LP_Cache;
use LP_Question;
use LP_Question_DB;
use LP_Question_Filter;
use LP_Quiz;
use LP_Quiz_Filter;
use Throwable;


class QuizPostModel extends PostModel {
/**
* @var string Post Type
*/
public $post_type = LP_QUIZ_CPT;

/**
* Const meta key
*/
const META_KEY_PASSING_GRADE = '_lp_passing_grade';
const META_KEY_RETAKE_COUNT = '_lp_retake_count';
const META_KEY_DURATION = '_lp_duration';
const META_KEY_PAGINATION = '_lp_pagination';
const META_KEY_REVIEW = '_lp_review';
const META_KEY_SHOW_CORRECT_REVIEW = '_lp_show_correct_review';
const META_KEY_INSTANT_CHECK = '_lp_instant_check';
const META_KEY_NEGATIVE_MARKING = '_lp_negative_marking';
const META_KEY_MINUS_SKIP_QUESTIONS = '_lp_minus_skip_questions';

/**
* Get post assignment by ID
*
* @param int $post_id
* @param bool $check_cache
*
* @return false|static
*/
public static function find( int $post_id, bool $check_cache = false ) {
$filter_post = new LP_Quiz_Filter();
$filter_post->ID = $post_id;

$key_cache = "quizPostModel/find/{$post_id}";
$lpQuizCache = new LP_Cache();

// Check cache
if ( $check_cache ) {
$quizPostModel = $lpQuizCache->get_cache( $key_cache );
if ( $quizPostModel instanceof QuizPostModel ) {
return $quizPostModel;
}
}

$quizPostModel = self::get_item_model_from_db( $filter_post );
// Set cache
if ( $quizPostModel instanceof QuizPostModel ) {
$lpQuizCache->set_cache( $key_cache, $quizPostModel );
}

return $quizPostModel;
}

/**
* Get max mark of assignment
*
* @return string
*/
public function get_duration(): string {
return $this->get_meta_value_by_key( self::META_KEY_DURATION, '0 minute' );
}

/**
* Get max mark of assignment
*
* @return float
*/
public function get_passing_grade(): float {
return (float) $this->get_meta_value_by_key( self::META_KEY_PASSING_GRADE, 80 );
}

/**
* Get retake count option.
*
* @return int
*/
public function get_retake_count(): int {
return (int) $this->get_meta_value_by_key( self::META_KEY_RETAKE_COUNT, 0 );
}

/**
* Get all question's ids of the quiz.
*
* @param array $statuses
*
* @return int[]
* @version 1.0.0
* @since 4.2.7.6
*/
public function get_question_ids( array $statuses = [ 'publish' ] ): array {
$lp_question_db = LP_Question_DB::getInstance();
$question_ids = [];

try {
$filter = new LP_Question_Filter();
$filter->ID = $this->get_id();
$filter->post_status = $statuses;
$question_ids = $lp_question_db->get_list_question_ids_of_quiz( $filter );

// Hook old
if ( has_filter( 'learn-press/quiz/get-question-ids' ) ) {
$quiz_old = new LP_Quiz( $this->get_id() );
$course_id_of_quiz_old = $quiz_old->get_course_id();

$question_ids = apply_filters(
'learn-press/quiz/get-question-ids',
$question_ids,
$this->get_id(),
$course_id_of_quiz_old
);
}

$question_ids = apply_filters(
'learn-press/quiz/question-ids',
$question_ids,
$this,
$statuses
);

if ( ! is_array( $question_ids ) ) {
$question_ids = array();
}
} catch ( Throwable $e ) {
error_log( $e->getMessage() );
}

return $question_ids;
}

/**
* Get number questions in quiz.
*
* @return int
*/
public function count_questions(): int {
$size = 0;
$questions = $this->get_question_ids();

if ( $questions ) {
$size = sizeof( $questions );
}

return (int) apply_filters( 'learn-press/quiz/count-questions', $size, $this->get_id() );
}

/**
* Get Mark of the quiz (total mark questions).
*
* @return mixed|null
*/
public function get_mark() {
$questions = $this->get_question_ids();
$mark = 0;

foreach ( $questions as $question_id ) {
$question = LP_Question::get_question( $question_id );
if ( $question ) {
$mark += $question->get_mark();
}
}

return apply_filters( 'learn-press/quiz-mark', $mark, $this->get_id() );
}

/**
* Check option instant check has enabled.
*
* @return bool
*/
public function has_instant_check(): bool {
return $this->get_meta_value_by_key( self::META_KEY_INSTANT_CHECK, 'no' ) === 'yes';
}

/**
* Check option negative marking has enabled.
*
* @return bool
*/
public function has_negative_marking(): bool {
return $this->get_meta_value_by_key( self::META_KEY_NEGATIVE_MARKING, 'no' ) === 'yes';
}

/**
* Check option minus skip questions has enabled.
*
* @return bool
*/
public function has_minus_skip_questions(): bool {
return $this->get_meta_value_by_key( self::META_KEY_MINUS_SKIP_QUESTIONS, 'no' ) === 'yes';
}

/**
* Check option minus skip questions has enabled.
*
* @return bool
*/
public function has_show_correct_review(): bool {
return $this->get_meta_value_by_key( self::META_KEY_SHOW_CORRECT_REVIEW, 'yes' ) === 'yes';
}
}
Loading

0 comments on commit 66d6757

Please sign in to comment.