| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class ProfileQuizzesTemplate. |
| 4 |
* |
| 5 |
* @since 4.2.8.2 |
| 6 |
* @version 1.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\TemplateHooks\Profile; |
| 10 |
|
| 11 |
use LearnPress\Filters\UserItemsFilter; |
| 12 |
use LearnPress\Helpers\Singleton; |
| 13 |
use LearnPress\Helpers\Template; |
| 14 |
use LearnPress\Models\UserItems\UserItemModel; |
| 15 |
use LearnPress\Models\UserModel; |
| 16 |
use LearnPress\TemplateHooks\Course\ListCoursesTemplate; |
| 17 |
use LearnPress\TemplateHooks\Table\TableListTemplate; |
| 18 |
use LearnPress\TemplateHooks\TemplateAJAX; |
| 19 |
use LearnPress\Models\UserItems\UserQuizModel; |
| 20 |
use LP_Database; |
| 21 |
use LP_Profile; |
| 22 |
use LP_Datetime; |
| 23 |
use Exception; |
| 24 |
use LP_User_Items_Filter; |
| 25 |
use stdClass; |
| 26 |
use Throwable; |
| 27 |
|
| 28 |
class ProfileQuizzesTemplate { |
| 29 |
use Singleton; |
| 30 |
|
| 31 |
public function init() { |
| 32 |
add_filter( 'lp/rest/ajax/allow_callback', array( $this, 'allow_callback' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Set up the callback for the AJAX request. |
| 37 |
* |
| 38 |
* @param array $callbacks |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function allow_callback( $callbacks ) { |
| 43 |
$callbacks[] = get_class( $this ) . ':renderContent'; |
| 44 |
|
| 45 |
return $callbacks; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @throws Exception |
| 50 |
*/ |
| 51 |
public static function tab_content() { |
| 52 |
$html_wrapper = array( |
| 53 |
'<div class="learn-press-subtab-content">' => '</div>', |
| 54 |
); |
| 55 |
|
| 56 |
$profile = LP_Profile::instance(); |
| 57 |
if ( ! $profile->get_user() ) { |
| 58 |
throw new Exception( __( 'Invalid User Profile', 'learnpress' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
$user_id = $profile->get_user()->get_id(); |
| 62 |
if ( ! $user_id ) { |
| 63 |
throw new Exception( __( 'User is not exist', 'learnpress' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @uses ProfileQuizzesTemplate::renderContent() |
| 68 |
*/ |
| 69 |
$callback = array( |
| 70 |
'class' => self::class, |
| 71 |
'method' => 'renderContent', |
| 72 |
); |
| 73 |
$args = array( |
| 74 |
'id_url' => 'profile_quizzes', |
| 75 |
'user_id' => $user_id, |
| 76 |
'paged' => 1, |
| 77 |
'type' => 'all', |
| 78 |
); |
| 79 |
|
| 80 |
$content = TemplateAJAX::load_content_via_ajax( $args, $callback ); |
| 81 |
$html = Template::instance()->nest_elements( $html_wrapper, $content ); |
| 82 |
echo $html; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Render the content for the quiz tab. |
| 87 |
* |
| 88 |
* @param array $args |
| 89 |
* |
| 90 |
* @return stdClass |
| 91 |
*/ |
| 92 |
public static function renderContent( array $args ): stdClass { |
| 93 |
$content = new stdClass(); |
| 94 |
$html = ''; |
| 95 |
|
| 96 |
try { |
| 97 |
$userModel = UserModel::find( $args['user_id'], true ); |
| 98 |
if ( ! $userModel ) { |
| 99 |
throw new Exception( __( 'Invalid User', 'learnpress' ) ); |
| 100 |
} |
| 101 |
|
| 102 |
// Check permission, self user or admin can view |
| 103 |
$user_current_id = get_current_user_id(); |
| 104 |
if ( $user_current_id !== $userModel->get_id() |
| 105 |
&& ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { |
| 106 |
throw new Exception( __( 'You do not have permission to view this profile.', 'learnpress' ) ); |
| 107 |
} |
| 108 |
|
| 109 |
$total_rows = 0; |
| 110 |
$limit = apply_filters( |
| 111 |
'learnpress/profile/user-quizzes/limit', |
| 112 |
get_option( 'posts_per_page', 10 ) |
| 113 |
); |
| 114 |
|
| 115 |
$filter = new UserItemsFilter(); |
| 116 |
$filter->user_id = $userModel->get_id(); |
| 117 |
$filter->limit = $limit; |
| 118 |
$filter->page = $args['paged']; |
| 119 |
|
| 120 |
switch ( $args['type'] ) { |
| 121 |
case UserItemModel::STATUS_COMPLETED: |
| 122 |
$filter->status = LP_ITEM_COMPLETED; |
| 123 |
break; |
| 124 |
case UserItemModel::GRADUATION_PASSED: |
| 125 |
$filter->graduation = LP_GRADUATION_PASSED; |
| 126 |
break; |
| 127 |
case UserItemModel::GRADUATION_FAILED: |
| 128 |
$filter->graduation = LP_GRADUATION_FAILED; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
$user_quizzes = $userModel->get_quizzes_attend( $filter, $total_rows ); |
| 133 |
$total_pages = LP_Database::get_total_pages( $limit, $total_rows ); |
| 134 |
|
| 135 |
$header = [ |
| 136 |
[ |
| 137 |
'title' => __( 'Quiz', 'learnpress' ), |
| 138 |
], |
| 139 |
[ |
| 140 |
'title' => __( 'Result', 'learnpress' ), |
| 141 |
], |
| 142 |
[ |
| 143 |
'title' => __( 'Time spent', 'learnpress' ), |
| 144 |
], |
| 145 |
[ |
| 146 |
'title' => __( 'Started Date', 'learnpress' ), |
| 147 |
], |
| 148 |
]; |
| 149 |
$body = []; |
| 150 |
|
| 151 |
if ( ! empty( $user_quizzes ) ) { |
| 152 |
foreach ( $user_quizzes as $user_quiz ) { |
| 153 |
$userQuizModel = UserQuizModel::find_user_item( |
| 154 |
$user_quiz->user_id, |
| 155 |
$user_quiz->item_id, |
| 156 |
$user_quiz->item_type, |
| 157 |
$user_quiz->ref_id, |
| 158 |
$user_quiz->ref_type, |
| 159 |
true |
| 160 |
); |
| 161 |
if ( ! $userQuizModel ) { |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
$quizPostModel = $userQuizModel->get_quiz_post_model(); |
| 166 |
if ( ! $quizPostModel ) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
$courseModel = $userQuizModel->get_course_model(); |
| 171 |
|
| 172 |
$item_body = [ |
| 173 |
sprintf( |
| 174 |
'<a href="%s">%s</a>', |
| 175 |
esc_url( $courseModel->get_item_link( $quizPostModel->get_id() ) ), |
| 176 |
esc_html( $quizPostModel->get_the_title() ) |
| 177 |
), |
| 178 |
sprintf( |
| 179 |
'<span class="result-percent">%s%%</span><span class="lp-label label-%s"> %s</span>', |
| 180 |
esc_html( $userQuizModel->get_result()['result'] ), |
| 181 |
esc_attr( $user_quiz->status ), |
| 182 |
$userQuizModel->get_status_label( $user_quiz->graduation ) |
| 183 |
), |
| 184 |
$userQuizModel->get_time_spend(), |
| 185 |
( new LP_Datetime( $userQuizModel->get_start_time() ) )->format( LP_Datetime::I18N_FORMAT ), |
| 186 |
]; |
| 187 |
|
| 188 |
$body[] = $item_body; |
| 189 |
} |
| 190 |
|
| 191 |
$section_footer = [ |
| 192 |
'page_result' => TableListTemplate::instance()->html_page_result( |
| 193 |
[ |
| 194 |
'paged' => $args['paged'], |
| 195 |
'per_page' => $limit, |
| 196 |
'total_rows' => $total_rows, |
| 197 |
] |
| 198 |
), |
| 199 |
'pagination' => ListCoursesTemplate::instance()->html_pagination_number( |
| 200 |
[ |
| 201 |
'total_pages' => $total_pages, |
| 202 |
'paged' => $args['paged'], |
| 203 |
] |
| 204 |
), |
| 205 |
]; |
| 206 |
|
| 207 |
$table_args = array( |
| 208 |
'header' => $header, |
| 209 |
'body' => $body, |
| 210 |
'footer' => Template::combine_components( $section_footer ), |
| 211 |
'class_table' => 'profile-list-table profile-list-quizzes', |
| 212 |
); |
| 213 |
|
| 214 |
$html = TableListTemplate::instance()->html_table( $table_args ); |
| 215 |
} else { |
| 216 |
$html = Template::print_message( |
| 217 |
__( 'No quizzes found', 'learnpress' ), |
| 218 |
'info', |
| 219 |
false |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
$section = [ |
| 224 |
'header' => self::instance()->html_tabs_header( $args['type'] ), |
| 225 |
'content' => $html, |
| 226 |
]; |
| 227 |
|
| 228 |
$content->content = Template::combine_components( $section ); |
| 229 |
$content->total_pages = $total_pages; |
| 230 |
$content->paged = $args['paged']; |
| 231 |
|
| 232 |
} catch ( Throwable $e ) { |
| 233 |
$content->content = Template::print_message( $e->getMessage(), 'error', false ); |
| 234 |
} |
| 235 |
|
| 236 |
return $content; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Render the header for the quiz tab. |
| 241 |
* |
| 242 |
* @param string $tab_active The active tab. |
| 243 |
* |
| 244 |
* @return string. |
| 245 |
*/ |
| 246 |
public static function html_tabs_header( string $tab_active = 'all' ): string { |
| 247 |
$filter_types = apply_filters( |
| 248 |
'learnpress/profile/quiz-tab/header/filter-types', |
| 249 |
array( |
| 250 |
'all' => __( 'All', 'learnpress' ), |
| 251 |
'completed' => __( 'Finished', 'learnpress' ), |
| 252 |
'passed' => __( 'Passed', 'learnpress' ), |
| 253 |
'failed' => __( 'Failed', 'learnpress' ), |
| 254 |
) |
| 255 |
); |
| 256 |
|
| 257 |
$html_li = ''; |
| 258 |
foreach ( $filter_types as $type => $label ) { |
| 259 |
$html_li .= sprintf( |
| 260 |
'<li class="%s%s"> |
| 261 |
<span data-filter="%s">%s</span> |
| 262 |
</li>', |
| 263 |
$type, |
| 264 |
$type === $tab_active ? ' active' : '', |
| 265 |
esc_attr( $type ), |
| 266 |
esc_attr( $label ) |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
$section = [ |
| 271 |
'wrapper' => '<div class="learn-press-tabs">', |
| 272 |
'types' => '<div class="learn-press-filters quiz-filter-types">', |
| 273 |
'lis' => $html_li, |
| 274 |
'types_end' => '</div>', |
| 275 |
'wrapper_end' => '</div>', |
| 276 |
]; |
| 277 |
|
| 278 |
return Template::combine_components( $section ); |
| 279 |
} |
| 280 |
} |
| 281 |
|