Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/addons/h5p/src/Utils.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Tutor H5P Addon Utils class
4 + *
5 + * @package TutorPro\Addons
6 + * @subpackage H5P
7 + * @author Themeum <support@themeum.com>
8 + * @link https://themeum.com
9 + * @since 3.0.0
10 + */
11 +
12 + namespace TutorPro\H5P;
13 +
14 + use Tutor\Helpers\QueryHelper;
15 + use TUTOR\Input;
16 +
17 + if ( ! defined( 'ABSPATH' ) ) {
18 + exit;
19 + }
20 +
21 + /**
22 + * Utils class for Tutor H5P addon
23 + */
24 + class Utils {
25 +
26 + /**
27 + * Provide the list of user created H5P content from H5P plugin.
28 + *
29 + * @since 3.0.0
30 + *
31 + * @param string $order_by sorting order.
32 + * @param string $search_filter search for H5P content by title.
33 + * @return array
34 + */
35 + public static function get_h5p_contents( $order_by = null, $search_filter = null ) {
36 + global $wpdb;
37 +
38 + $order_field = null;
39 + $reverse = null;
40 + $filter = null;
41 +
42 + if ( isset( $order_by ) ) {
43 + $order_field = 'updated_at';
44 + $reverse = 'ASC' === $order_by ? true : false;
45 + }
46 +
47 + if ( isset( $search_filter ) ) {
48 +
49 + $search_filter = '%' . $wpdb->esc_like( $search_filter ) . '%';
50 +
51 + $filter = array(
52 + array(
53 + 'title',
54 + $search_filter,
55 + 'LIKE',
56 + ),
57 + array(
58 + 'content_type',
59 + $search_filter,
60 + 'LIKE',
61 + ),
62 + );
63 +
64 + }
65 +
66 + if ( ! isset( $filter ) ) {
67 + $filter = array(
68 + array(
69 + 'user_id',
70 + get_current_user_id(),
71 + '=',
72 + ),
73 + );
74 + } else {
75 + array_push(
76 + $filter,
77 + array(
78 + 'user_id',
79 + get_current_user_id(),
80 + '=',
81 + )
82 + );
83 + }
84 +
85 + $fields = array( 'title', 'content_type', 'user_name', 'tags', 'updated_at', 'id', 'user_id' );
86 + $h5p_contents = new \H5PContentQuery( $fields, null, null, $order_field, $reverse, $filter );
87 + $results = $h5p_contents->get_rows();
88 +
89 + return $results;
90 + }
91 +
92 + /**
93 + * Get a single H5P content for a given content id.
94 + *
95 + * @since 3.0.0
96 + *
97 + * @param object $plugin_instance instance of h5p plugin.
98 + * @param int $content_id the content id to get the H5P content.
99 + * @return string
100 + */
101 + public static function get_h5p_content( $plugin_instance, $content_id ) {
102 + $content = $plugin_instance->get_content( $content_id );
103 + return $plugin_instance->add_assets( $content );
104 + }
105 +
106 + /**
107 + * Provide the correct translated display text based on current locale.
108 + *
109 + * @since 3.0.0
110 + *
111 + * @param object $display_language the object with the display text.
112 + * @return string
113 + */
114 + public static function get_xpi_locale_property( $display_language ) {
115 + $default_locale = 'en-US';
116 + $current_locale = str_replace( '_', '-', get_locale() );
117 +
118 + if ( property_exists( $display_language, $current_locale ) ) {
119 + return $display_language->{ $current_locale };
120 + } else {
121 + return $display_language->{ $default_locale };
122 + }
123 + }
124 +
125 +
126 + /**
127 + * Get single tutor H5P quiz result.
128 + *
129 + * @since 3.0.0
130 + *
131 + * @param int $question_id the question id.
132 + * @param int $user_id the user id.
133 + * @param integer $attempt_id the attempt id.
134 + * @param integer $quiz_id the quiz id.
135 + * @param integer $content_id the content id.
136 + * @return array
137 + */
138 + public static function get_h5p_quiz_result( $question_id, $user_id, $attempt_id = 0, $quiz_id = 0, $content_id = 0 ) {
139 + global $wpdb;
140 +
141 + $question_id = (int) $question_id;
142 + $user_id = (int) $user_id;
143 + $content_id = (int) $content_id;
144 + $attempt_id = (int) $attempt_id;
145 + $quiz_id = (int) $quiz_id;
146 +
147 + $where_clause = $wpdb->prepare( ' AND question_id = %d AND user_id = %d', $question_id, $user_id );
148 +
149 + if ( 0 !== $content_id ) {
150 + $where_clause .= $wpdb->prepare( ' AND content_id = %d', $content_id );
151 + }
152 +
153 + if ( 0 !== $attempt_id ) {
154 + $where_clause .= $wpdb->prepare( ' AND attempt_id = %d', $attempt_id );
155 + }
156 +
157 + if ( 0 !== $quiz_id ) {
158 + $where_clause .= $wpdb->prepare( ' AND quiz_id = %d', $quiz_id );
159 + }
160 +
161 + $quiz_results = $wpdb->get_results(
162 + // phpcs:disable
163 + "SELECT * FROM {$wpdb->prefix}tutor_h5p_quiz_result
164 + WHERE 1=1 {$where_clause}
165 + ORDER BY finished DESC LIMIT 1"
166 + // phpcs:enable
167 + );
168 +
169 + return $quiz_results;
170 + }
171 +
172 + /**
173 + * Get quiz result list for tutor H5P quiz.
174 + *
175 + * @since 3.0.0
176 + *
177 + * @param integer $question_id the question id.
178 + * @param integer $user_id the user id.
179 + * @param integer $attempt_id the attempt id.
180 + * @param integer $quiz_id the quiz id.
181 + * @param integer $content_id the content id.
182 + * @return mixed
183 + */
184 + public static function get_h5p_quiz_results( $question_id = 0, $user_id = 0, $attempt_id = 0, $quiz_id = 0, $content_id = 0 ) {
185 +
186 + global $wpdb;
187 +
188 + $question_id = (int) $question_id;
189 + $user_id = (int) $user_id;
190 + $content_id = (int) $content_id;
191 + $attempt_id = (int) $attempt_id;
192 + $quiz_id = (int) $quiz_id;
193 +
194 + $where_clause = '';
195 +
196 + if ( 0 !== $content_id ) {
197 + $where_clause .= $wpdb->prepare( ' AND content_id = %d', $content_id );
198 + }
199 +
200 + if ( 0 !== $attempt_id ) {
201 + $where_clause .= $wpdb->prepare( ' AND attempt_id = %d', $attempt_id );
202 + }
203 +
204 + if ( 0 !== $quiz_id ) {
205 + $where_clause .= $wpdb->prepare( ' AND quiz_id = %d', $quiz_id );
206 + }
207 +
208 + if ( 0 !== $question_id ) {
209 + $where_clause .= $wpdb->prepare( ' AND question_id = %d', $question_id );
210 + }
211 +
212 + if ( 0 !== $user_id ) {
213 + $where_clause .= $wpdb->prepare( ' AND user_id = %d', $user_id );
214 + }
215 +
216 + $quiz_results = $wpdb->get_results(
217 + // phpcs:disable
218 + "SELECT * FROM {$wpdb->prefix}tutor_h5p_quiz_result
219 + WHERE 1=1 {$where_clause}"
220 + // phpcs:enable
221 + );
222 +
223 + return $quiz_results;
224 + }
225 +
226 + /**
227 + * Convert xAPI result duration from iso8601 to date string.
228 + *
229 + * @since 3.0.0
230 + *
231 + * @param string $duration the iso8601 result duration.
232 + * @return string
233 + */
234 + public static function convert_iso8601_to_string( $duration ) {
235 + $hours = null;
236 + $minutes = null;
237 + $seconds = null;
238 +
239 + if ( isset( $duration ) ) {
240 + preg_match( '/\\d{1,2}.\d{1,2}[H]/', $duration, $hours );
241 + preg_match( '/\d{1,2}.\d{1,2}[M]/', $duration, $minutes );
242 + preg_match( '/\d{1,2}.\d{1,2}[S]/', $duration, $seconds );
243 + }
244 +
245 + $duration = array(
246 + 'hours' => $hours && is_array( $hours ) ? $hours[0] : 0,
247 + 'minutes' => $minutes && is_array( $minutes ) ? $minutes[0] : 0,
248 + 'seconds' => $seconds && is_array( $seconds ) ? $seconds[0] : 0,
249 + );
250 +
251 + $hours = substr( $duration['hours'], 0, -1 );
252 + $minutes = substr( $duration['minutes'], 0, -1 );
253 + $seconds = substr( $duration['seconds'], 0, -1 );
254 +
255 + $final_duration = '';
256 + $hours ? $final_duration .= $hours . ' hours ' : '';
257 + $minutes ? $final_duration .= $minutes . ' minutes ' : '';
258 + $seconds ? $final_duration .= $seconds . ' seconds' : '';
259 +
260 + return $final_duration;
261 + }
262 +
263 + /**
264 + * Save H5P statements in database
265 + *
266 + * @since 3.0.0
267 + *
268 + * @param array $statement the statement data to insert.
269 + *
270 + * @return int|false
271 + */
272 + public static function save_tutor_h5p_statement( $statement ) {
273 +
274 + global $wpdb;
275 +
276 + $is_inserted = $wpdb->insert( "{$wpdb->prefix}tutor_h5p_statement", $statement );
277 +
278 + return $is_inserted;
279 + }
280 +
281 +
282 + /**
283 + * Get last ten H5P statements sorted by saved date.
284 + *
285 + * @since 3.0.0
286 + *
287 + * @param string $search the search query string.
288 + *
289 + * @return array
290 + */
291 + public static function get_last_ten_statements( $search = '' ) {
292 + global $wpdb;
293 +
294 + // phpcs:disable
295 + $total_statements = $wpdb->get_results(
296 + $wpdb->prepare(
297 + "SELECT * FROM {$wpdb->prefix}tutor_h5p_statement
298 + WHERE 1=1 AND instructor_id = %d
299 + AND (verb = %s OR activity_name = %s OR user_id = %s)
300 + ORDER BY created_at DESC LIMIT 10 ",
301 + get_current_user_id(),
302 + $search,
303 + $search,
304 + $search
305 + )
306 + );
307 + // phpcs:enable
308 +
309 + return $total_statements;
310 + }
311 +
312 + /**
313 + * Obtain H5P content id from shortcode.
314 + *
315 + * @since 3.0.0
316 + *
317 + * @param string $short_code get content id from short code.
318 + * @return int
319 + */
320 + public static function get_h5p_content_id( $short_code ) {
321 +
322 + $content_id = array();
323 + preg_match( '/\d+/', explode( ' ', $short_code )[1], $content_id );
324 +
325 + return count( $content_id ) ? (int) $content_id[0] : 0;
326 + }
327 +
328 + /**
329 + * Get all H5P shortcodes from lesson description
330 + *
331 + * @since 3.0.0
332 + *
333 + * @param string $input_string the lesson content string.
334 + * @return array
335 + */
336 + public static function get_h5p_shortcodes( $input_string ) {
337 +
338 + $matches = array();
339 + $match_count = preg_match_all( '/\[h5p id="\d+"\]/', trim( $input_string ), $matches );
340 + $short_codes = array();
341 +
342 + if ( $match_count > 0 ) {
343 + $short_codes = $matches[0];
344 + }
345 + return $short_codes;
346 + }
347 +
348 + /**
349 + * Provide chart data to show line chart.
350 + *
351 + * @since 3.0.0
352 + *
353 + * @return mixed
354 + */
355 + public static function chart_data() {
356 + $analytics = array();
357 + if ( isset( $_GET['page'] ) && 'tutor_h5p' === $_GET['page'] ) {
358 + $time_period = isset( $_GET['period'] ) ? sanitize_text_field( $_GET['period'] ) : '';
359 + $start_date = isset( $_GET['start_date'] ) ? sanitize_text_field( $_GET['start_date'] ) : '';
360 + $end_date = isset( $_GET['end_date'] ) ? sanitize_text_field( $_GET['end_date'] ) : '';
361 + if ( '' !== $start_date ) {
362 + $start_date = tutor_get_formated_date( 'Y-m-d', $start_date );
363 + }
364 + if ( '' !== $end_date ) {
365 + $end_date = tutor_get_formated_date( 'Y-m-d', $end_date );
366 + }
367 +
368 + $data = Analytics::get_h5p_statements_count( $time_period, $start_date, $end_date )['statements'];
369 +
370 + $analytics = array(
371 + array(
372 + 'id' => 'ta_total_statements',
373 + 'label' => __( 'Total Statements', 'tutor-pro' ),
374 + 'data' => $data,
375 + ),
376 + );
377 + }
378 +
379 + return $analytics;
380 + }
381 +
382 + /**
383 + * Provide config for H5P addon
384 + *
385 + * @since 3.0.0
386 + *
387 + * @return object
388 + */
389 + public static function addon_config() {
390 + $info = array(
391 + 'path' => plugin_dir_path( TUTOR_H5P_FILE ),
392 + 'url' => plugin_dir_url( TUTOR_H5P_FILE ),
393 + 'basename' => plugin_basename( TUTOR_H5P_FILE ),
394 + 'version' => TUTOR_H5P_VERSION,
395 + 'nonce_action' => 'tutor_nonce_action',
396 + 'nonce' => '_wpnonce',
397 + 'h5p_plugin' => class_exists( 'H5P_Plugin' ) ? \H5P_Plugin::get_instance() : null,
398 + 'h5p_admin_plugin' => class_exists( 'H5P_Plugin_Admin' ) ? \H5P_Plugin_Admin::get_instance() : null,
399 + );
400 +
401 + return (object) $info;
402 + }
403 + }
404 +