| @@ -1,146 +1,146 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | - exit; // Exit if accessed directly | |
| 5 | -} | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * Class LP_Lesson_DB | |
| 9 | - * | |
| 10 | - * @since 3.2.8 | |
| 11 | - * @version 1.0.3 | |
| 12 | - */ | |
| 13 | -class LP_Question_DB extends LP_Post_DB { | |
| 14 | - private static $_instance; | |
| 15 | - | |
| 16 | - protected function __construct() { | |
| 17 | - parent::__construct(); | |
| 18 | - } | |
| 19 | - | |
| 20 | - public static function getInstance() { | |
| 21 | - if ( is_null( self::$_instance ) ) { | |
| 22 | - self::$_instance = new self(); | |
| 23 | - } | |
| 24 | - | |
| 25 | - return self::$_instance; | |
| 26 | - } | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Get questions | |
| 30 | - * | |
| 31 | - * @return array|null|int|string | |
| 32 | - * @throws Exception | |
| 33 | - * @since 4.1.6 | |
| 34 | - * @version 1.0.0 | |
| 35 | - */ | |
| 36 | - public function get_questions( LP_Question_Filter $filter, &$total_rows = 0 ) { | |
| 37 | - if ( empty( $filter->collection_alias ) ) { | |
| 38 | - $filter->collection_alias = 'q'; | |
| 39 | - } | |
| 40 | - | |
| 41 | - return $this->get_posts( $filter, $total_rows ); | |
| 42 | - } | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * Get all questions are unassigned to any quiz. | |
| 46 | - * | |
| 47 | - * @param LP_Question_Filter|null $filter | |
| 48 | - * | |
| 49 | - * @return array|null|int|string | |
| 50 | - * @throws Exception | |
| 51 | - * @since 4.1.6 | |
| 52 | - * @version 1.0.1 | |
| 53 | - */ | |
| 54 | - public function get_questions_not_assign_quiz( $filter = null ) { | |
| 55 | - $lp_qq_filter = new LP_Quiz_Questions_Filter(); | |
| 56 | - $lp_qq_filter->return_string_query = true; | |
| 57 | - $lp_qq_filter->only_fields = array( 'question_id' ); | |
| 58 | - $query_question_ids_assigned = LP_Quiz_Questions_DB::getInstance()->get_quiz_questions( $lp_qq_filter ); | |
| 59 | - | |
| 60 | - if ( is_null( $filter ) ) { | |
| 61 | - $filter = new LP_Question_Filter(); | |
| 62 | - } | |
| 63 | - $filter->only_fields = array( 'q.ID' ); | |
| 64 | - $filter->where[] = "AND ID NOT IN($query_question_ids_assigned)"; | |
| 65 | - $filter->where[] = $this->wpdb->prepare( 'AND q.post_status not IN(%s, %s)', 'trash', 'auto-draft' ); | |
| 66 | - | |
| 67 | - return $this->get_questions( $filter ); | |
| 68 | - } | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * Count all questions are unassigned to any quiz. | |
| 72 | - * | |
| 73 | - * @param LP_Question_Filter|null $filter | |
| 74 | - * | |
| 75 | - * @return int | |
| 76 | - * @throws Exception | |
| 77 | - * @since 3.0.0 | |
| 78 | - * @version 1.0.1 | |
| 79 | - */ | |
| 80 | - function get_total_question_unassigned( $filter = null ): int { | |
| 81 | - if ( is_null( $filter ) ) { | |
| 82 | - $filter = new LP_Question_Filter(); | |
| 83 | - } | |
| 84 | - | |
| 85 | - $filter->query_count = true; | |
| 86 | - $filter->post_status = []; | |
| 87 | - $filter->field_count = 'q.ID'; | |
| 88 | - | |
| 89 | - return $this->get_questions_not_assign_quiz( $filter ); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Get list Question ids of Quiz | |
| 94 | - * | |
| 95 | - * @param LP_Question_Filter|null $filter | |
| 96 | - * | |
| 97 | - * Clear cache when save quiz same id | |
| 98 | - * | |
| 99 | - * @return array | |
| 100 | - * @throws Exception | |
| 101 | - * @see LP_Quiz_Post_Type::save | |
| 102 | - * @version 1.0.1 | |
| 103 | - */ | |
| 104 | - public function get_list_question_ids_of_quiz( $filter = null ): array { | |
| 105 | - $key_cache = "$filter->ID/question_ids"; | |
| 106 | - | |
| 107 | - // Get cache | |
| 108 | - $lp_quiz_cache = LP_Quiz_Cache::instance(); | |
| 109 | - $quiz_ids = $lp_quiz_cache->get_cache( $key_cache ); | |
| 110 | - | |
| 111 | - if ( $quiz_ids ) { | |
| 112 | - return $quiz_ids; | |
| 113 | - } | |
| 114 | - | |
| 115 | - $statues = array( 'publish' ); | |
| 116 | - if ( ! empty( $filter->post_status ) ) { | |
| 117 | - $statues = $filter->post_status; | |
| 118 | - } | |
| 119 | - | |
| 120 | - $format = LP_Helper::format_query_IN( $statues ); | |
| 121 | - $args = array_merge( array( LP_QUESTION_CPT, $filter->ID ), $statues ); | |
| 122 | - | |
| 123 | - $query = $this->wpdb->prepare( | |
| 124 | - " | |
| 125 | - SELECT question_id | |
| 126 | - FROM $this->tb_lp_quiz_questions AS quiz_q | |
| 127 | - INNER JOIN $this->tb_posts as p | |
| 128 | - ON p.ID = quiz_q.question_id | |
| 129 | - AND p.post_type = %s | |
| 130 | - AND quiz_q.quiz_id = %d | |
| 131 | - AND p.post_status IN(" . $format . ') | |
| 132 | - ORDER BY question_order | |
| 133 | - ', | |
| 134 | - $args | |
| 135 | - ); | |
| 136 | - | |
| 137 | - $ids = $this->wpdb->get_col( $query ); | |
| 138 | - | |
| 139 | - $this->check_execute_has_error(); | |
| 140 | - | |
| 141 | - // Set cache | |
| 142 | - $lp_quiz_cache->set_cache( $key_cache, $ids, DAY_IN_SECONDS ); | |
| 143 | - | |
| 144 | - return $ids; | |
| 145 | - } | |
| 146 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit; // Exit if accessed directly | |
| 5 | +} | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Class LP_Lesson_DB | |
| 9 | + * | |
| 10 | + * @since 3.2.8 | |
| 11 | + * @version 1.0.3 | |
| 12 | + */ | |
| 13 | +class LP_Question_DB extends LP_Post_DB { | |
| 14 | + private static $_instance; | |
| 15 | + | |
| 16 | + protected function __construct() { | |
| 17 | + parent::__construct(); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public static function getInstance() { | |
| 21 | + if ( is_null( self::$_instance ) ) { | |
| 22 | + self::$_instance = new self(); | |
| 23 | + } | |
| 24 | + | |
| 25 | + return self::$_instance; | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Get questions | |
| 30 | + * | |
| 31 | + * @return array|null|int|string | |
| 32 | + * @throws Exception | |
| 33 | + * @since 4.1.6 | |
| 34 | + * @version 1.0.0 | |
| 35 | + */ | |
| 36 | + public function get_questions( LP_Question_Filter $filter, &$total_rows = 0 ) { | |
| 37 | + if ( empty( $filter->collection_alias ) ) { | |
| 38 | + $filter->collection_alias = 'q'; | |
| 39 | + } | |
| 40 | + | |
| 41 | + return $this->get_posts( $filter, $total_rows ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Get all questions are unassigned to any quiz. | |
| 46 | + * | |
| 47 | + * @param LP_Question_Filter|null $filter | |
| 48 | + * | |
| 49 | + * @return array|null|int|string | |
| 50 | + * @throws Exception | |
| 51 | + * @since 4.1.6 | |
| 52 | + * @version 1.0.1 | |
| 53 | + */ | |
| 54 | + public function get_questions_not_assign_quiz( $filter = null ) { | |
| 55 | + $lp_qq_filter = new LP_Quiz_Questions_Filter(); | |
| 56 | + $lp_qq_filter->return_string_query = true; | |
| 57 | + $lp_qq_filter->only_fields = array( 'question_id' ); | |
| 58 | + $query_question_ids_assigned = LP_Quiz_Questions_DB::getInstance()->get_quiz_questions( $lp_qq_filter ); | |
| 59 | + | |
| 60 | + if ( is_null( $filter ) ) { | |
| 61 | + $filter = new LP_Question_Filter(); | |
| 62 | + } | |
| 63 | + $filter->only_fields = array( 'q.ID' ); | |
| 64 | + $filter->where[] = "AND ID NOT IN($query_question_ids_assigned)"; | |
| 65 | + $filter->where[] = $this->wpdb->prepare( 'AND q.post_status not IN(%s, %s)', 'trash', 'auto-draft' ); | |
| 66 | + | |
| 67 | + return $this->get_questions( $filter ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Count all questions are unassigned to any quiz. | |
| 72 | + * | |
| 73 | + * @param LP_Question_Filter|null $filter | |
| 74 | + * | |
| 75 | + * @return int | |
| 76 | + * @throws Exception | |
| 77 | + * @since 3.0.0 | |
| 78 | + * @version 1.0.1 | |
| 79 | + */ | |
| 80 | + function get_total_question_unassigned( $filter = null ): int { | |
| 81 | + if ( is_null( $filter ) ) { | |
| 82 | + $filter = new LP_Question_Filter(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + $filter->query_count = true; | |
| 86 | + $filter->post_status = []; | |
| 87 | + $filter->field_count = 'q.ID'; | |
| 88 | + | |
| 89 | + return $this->get_questions_not_assign_quiz( $filter ); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Get list Question ids of Quiz | |
| 94 | + * | |
| 95 | + * @param LP_Question_Filter|null $filter | |
| 96 | + * | |
| 97 | + * Clear cache when save quiz same id | |
| 98 | + * | |
| 99 | + * @return array | |
| 100 | + * @throws Exception | |
| 101 | + * @see LP_Quiz_Post_Type::save | |
| 102 | + * @version 1.0.1 | |
| 103 | + */ | |
| 104 | + public function get_list_question_ids_of_quiz( $filter = null ): array { | |
| 105 | + $key_cache = "$filter->ID/question_ids"; | |
| 106 | + | |
| 107 | + // Get cache | |
| 108 | + $lp_quiz_cache = LP_Quiz_Cache::instance(); | |
| 109 | + $quiz_ids = $lp_quiz_cache->get_cache( $key_cache ); | |
| 110 | + | |
| 111 | + if ( $quiz_ids ) { | |
| 112 | + return $quiz_ids; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $statues = array( 'publish' ); | |
| 116 | + if ( ! empty( $filter->post_status ) ) { | |
| 117 | + $statues = $filter->post_status; | |
| 118 | + } | |
| 119 | + | |
| 120 | + $format = LP_Helper::format_query_IN( $statues ); | |
| 121 | + $args = array_merge( array( LP_QUESTION_CPT, $filter->ID ), $statues ); | |
| 122 | + | |
| 123 | + $query = $this->wpdb->prepare( | |
| 124 | + " | |
| 125 | + SELECT question_id | |
| 126 | + FROM $this->tb_lp_quiz_questions AS quiz_q | |
| 127 | + INNER JOIN $this->tb_posts as p | |
| 128 | + ON p.ID = quiz_q.question_id | |
| 129 | + AND p.post_type = %s | |
| 130 | + AND quiz_q.quiz_id = %d | |
| 131 | + AND p.post_status IN(" . $format . ') | |
| 132 | + ORDER BY question_order | |
| 133 | + ', | |
| 134 | + $args | |
| 135 | + ); | |
| 136 | + | |
| 137 | + $ids = $this->wpdb->get_col( $query ); | |
| 138 | + | |
| 139 | + $this->check_execute_has_error(); | |
| 140 | + | |
| 141 | + // Set cache | |
| 142 | + $lp_quiz_cache->set_cache( $key_cache, $ids, DAY_IN_SECONDS ); | |
| 143 | + | |
| 144 | + return $ids; | |
| 145 | + } | |
| 146 | +} | |