PluginProbe
WP Courses LMS – Online Courses Builder, eLearning Courses, Courses Solution, Education Courses / trunk
WP Courses LMS – Online Courses Builder, eLearning Courses, Courses Solution, Education Courses vtrunk
3.2.30 trunk 3.1.40 3.1.41 3.1.42 3.1.43 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19 3.2.2 3.2.20 3.2.21 3.2.22 3.2.23 3.2.24 3.2.25 All 36 releases
wp-courses / functions / tracking.php

tracking.php in WP Courses LMS – Online Courses Builder, eLearning Courses, Courses Solution, Education Courses trunk, at functions/tracking.php

327 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function wpc_track_lesson() {
4 $post_type = get_post_type();
5 $logged_in = is_user_logged_in();
6 if($logged_in && $post_type == 'lesson' || $logged_in && $post_type == 'wpc-quiz') {
7 global $wpdb;
8
9 $post_id = get_the_ID();
10 $user_id = get_current_user_id();
11
12 $time_now = time();
13
14 if($post_type == 'lesson'){
15 $course_id = isset($_GET['course_id']) ? (int) $_GET['course_id'] : wpc_get_first_connected_course($post_id);
16 } else {
17 $course_id = isset($_GET['course_id']) ? (int) $_GET['course_id'] : wpc_get_first_connected_course($post_id, 'quiz-to-course');
18 }
19
20 wpc_push_viewed($post_id, $course_id, $user_id);
21 }
22 }
23 add_action("wp_head", "wpc_track_lesson", 10);
24
25 function wpc_push_viewed($post_id, $course_id, $user_id) {
26 global $wpdb;
27 $table_name = $wpdb->prefix . 'wpc_tracking';
28 $sql = "SELECT post_id from $table_name WHERE post_id = $post_id And user_id = $user_id";
29 $results = $wpdb->get_results($sql);
30
31 $time_now = time();
32
33 if($wpdb->num_rows <= 0) {
34 $wpdb->insert(
35 $table_name, array(
36 "user_id" => $user_id,
37 "post_id" => $post_id,
38 "course_id" => $course_id,
39 "completed" => 0,
40 "viewed_timestamp" => $time_now,
41 "completed_timestamp" => 0
42 ),
43 array("%d", "%d", "%d", "%d", "%d")
44 );
45 } else {
46 $wpdb->query($wpdb->prepare("UPDATE $table_name SET viewed_timestamp = %d WHERE user_id = $user_id AND post_id = $post_id AND course_id = $course_id", $time_now));
47 }
48 update_user_meta($user_id, 'wpc-last-viewed-course', $course_id);
49 }
50
51 function wpc_push_completed($user_id, $post_id, $status){
52 global $wpdb;
53 $table_name = $wpdb->prefix . "wpc_tracking";
54 $sql = "UPDATE $table_name SET completed_timestamp = %d, completed = %d WHERE user_id = $user_id AND post_id = $post_id";
55 $wpdb->query($wpdb->prepare($sql, time(), $status));
56 }
57
58 function wpc_get_lesson_status($user_id, $post_id) {
59 global $wpdb;
60 $table_name = $wpdb->prefix . "wpc_tracking";
61 $sql = "SELECT completed FROM $table_name WHERE $post_id = post_id AND user_id = $user_id LIMIT 1";
62 $result = $wpdb->get_results($sql);
63 if(!empty($result)) {
64 $status = array(
65 'viewed' => 1,
66 'completed' => (int) $result[0]->completed
67 );
68 } else {
69 $status = array(
70 'viewed' => 0,
71 'completed' => 0
72 );
73 }
74 return $status;
75 }
76
77 function wpc_get_last_tracked_lesson($user_id, $view = 0) {
78 global $wpdb;
79 $table_name = $wpdb->prefix . "wpc_tracking";
80 if($view === 0){
81 $sql = "SELECT post_id FROM $table_name ORDER BY viewed_timestamp DESC LIMIT 1";
82 } else {
83 $sql = "SELECT post_id FROM $table_name WHERE completed = 1 ORDER BY completed_timestamp DESC LIMIT 1";
84 }
85 $result = $wpdb->get_results($sql);
86 return $result = !empty($result) ? $result[0]->post_id : null;
87 }
88
89 /**
90 *
91 * @param int $user_id User ID
92 * @param bool $view 0 for viewed, 1 for completed
93 * @param int $page Page you'd like to retrieve results from
94 * @param int $posts_per_page The number of rows you'd like to retrieve
95 * @param bool $include_quiz Set to true to include tracked quizzes in return
96 * @return array Completed and viewed lessons and quizzes (if $include_quiz === true)
97 */
98
99 function wpc_get_tracked_lessons_by_user($user_id, $view = 0, $page = 1, $posts_per_page = 1000000, $include_quiz = true) {
100 global $wpdb;
101 $offset = ($page - 1) * $posts_per_page;
102 $table_name = $wpdb->prefix . "wpc_tracking";
103 $table_name_2 = $wpdb->prefix . 'posts';
104
105 if($view === 0) {
106 if($include_quiz === true) {
107 $sql = "SELECT {$table_name}.user_id, {$table_name}.course_id, {$table_name}.viewed_timestamp, {$table_name}.completed_timestamp, {$table_name}.completed, {$table_name}.post_id, {$table_name_2}.post_status, {$table_name_2}.post_type FROM {$table_name} INNER JOIN $table_name_2 ON {$table_name}.post_id={$table_name_2}.ID WHERE {$table_name}.user_id = {$user_id} AND {$table_name_2}.post_status = 'publish' ORDER BY viewed_timestamp DESC LIMIT $posts_per_page OFFSET $offset";
108 } else {
109 $sql = "SELECT {$table_name}.user_id, {$table_name}.course_id, {$table_name}.viewed_timestamp, {$table_name}.completed_timestamp, {$table_name}.completed, {$table_name}.post_id, {$table_name_2}.post_status, {$table_name_2}.post_type FROM {$table_name} INNER JOIN $table_name_2 ON {$table_name}.post_id={$table_name_2}.ID WHERE {$table_name}.user_id = {$user_id} AND {$table_name_2}.post_status = 'publish' AND {$table_name_2}.post_type != 'wpc-quiz' ORDER BY viewed_timestamp DESC LIMIT $posts_per_page OFFSET $offset";
110 }
111 } else {
112 if($include_quiz === true) {
113 $sql = "SELECT {$table_name}.user_id, {$table_name}.course_id, {$table_name}.viewed_timestamp, {$table_name}.completed_timestamp, {$table_name}.completed, {$table_name}.post_id, {$table_name_2}.post_status, {$table_name_2}.post_type FROM {$table_name} INNER JOIN $table_name_2 ON {$table_name}.post_id={$table_name_2}.ID WHERE {$table_name}.user_id = {$user_id} AND {$table_name_2}.post_status = 'publish' AND {$table_name}.completed = 1 ORDER BY viewed_timestamp DESC LIMIT $posts_per_page OFFSET $offset";
114 } else {
115 $sql = "SELECT {$table_name}.user_id, {$table_name}.course_id, {$table_name}.viewed_timestamp, {$table_name}.completed_timestamp, {$table_name}.completed, {$table_name}.post_id, {$table_name_2}.post_status, {$table_name_2}.post_type FROM {$table_name} INNER JOIN $table_name_2 ON {$table_name}.post_id={$table_name_2}.ID WHERE {$table_name}.user_id = {$user_id} AND {$table_name_2}.post_status = 'publish' AND {$table_name}.completed = 1 AND {$table_name_2}.post_type != 'wpc-quiz' ORDER BY viewed_timestamp DESC LIMIT $posts_per_page OFFSET $offset";
116 }
117 }
118 $results = $wpdb->get_results($sql);
119 return $results;
120 }
121
122 function wpc_get_viewed_lessons_per_day($days = 90, $view = 0) {
123 global $wpdb;
124 $table_name = $wpdb->prefix . "wpc_tracking";
125 $view = $view === 0 ? 0 : 1;
126 $view_column_timestamp = $view === 0 ? 'viewed_timestamp' : 'completed_timestamp';
127 $sql = "SELECT COUNT(FROM_UNIXTIME($view_column_timestamp, '%Y-%m-%d')) as y, FROM_UNIXTIME($view_column_timestamp, '%Y-%m-%d') as x FROM $table_name WHERE $view_column_timestamp > 0 AND completed = {$view} GROUP BY FROM_UNIXTIME($view_column_timestamp, '%Y-%m-%d') ORDER BY $view_column_timestamp DESC LIMIT {$days}";
128 $results = $wpdb->get_results($sql);
129 $results = array_reverse($results);
130 return $results;
131 }
132
133 function wpc_get_active_users($num = 25, $view = 0, $time_length = 604800) {
134 global $wpdb;
135 $table_name = $wpdb->prefix . "wpc_tracking";
136 $table_name_2 = $wpdb->prefix . "users";
137
138 $time_now = time();
139 $time_start = $time_now - $time_length;
140
141 // get viewed
142 if($view === 0) {
143 $sql = "SELECT COUNT({$table_name}.post_id) as y, {$table_name_2}.user_login as x FROM $table_name INNER JOIN $table_name_2 ON {$table_name}.user_id={$table_name_2}.ID WHERE viewed_timestamp >= $time_start GROUP BY {$table_name}.user_id ORDER BY y DESC LIMIT $num";
144 } else {
145 // get completed
146 $sql = "SELECT COUNT({$table_name}.post_id) as y, {$table_name_2}.user_login as x FROM $table_name INNER JOIN $table_name_2 ON {$table_name}.user_id={$table_name_2}.ID WHERE completed = {$view} AND completed_timestamp >= $time_start GROUP BY {$table_name}.user_id ORDER BY y DESC LIMIT $num";
147 }
148
149 $results = $wpdb->get_results($sql);
150 return $results;
151 }
152
153 /**
154 * checks whether or not someone has viewed of completed a lesson
155 * @param int $post_id The lesson or quiz ID you'd like to check for being viewed or completed
156 * @param array $tracking The user tracking data from wpc_get_tracked_lessons_by_user( (int) $user_id, (int) $view (int) $page, (int) $posts_per_page)
157 * @param bool $view 0 for viewed lessons, 1 for completed lessons
158 * @return bool If the lesson or quiz has been completed or viewed depending upon the passed $view parameter
159 */
160
161 function wpc_has_done($post_id, $tracking, $view = 0) {
162 $done = false;
163 foreach($tracking as $tracked){
164 if($view === 1) {
165 if($tracked->post_id == $post_id && $tracked->completed == $view){
166 $done = true;
167 break;
168 } else {
169 $done = false;
170 }
171 } else {
172 if($tracked->post_id == $post_id){
173 $done = true;
174 break;
175 } else {
176 $done = false;
177 }
178 }
179
180 }
181 return $done;
182 }
183
184 /**
185 * @param int $user_id User ID
186 * @param bool $view 0 for viewed lessons, 1 for completed lessons
187 * @param bool $include_quiz true to include viewed and completed quizzes in result
188 * @return int Total count of how many lessons have been viewed or completed
189 */
190
191 function wpc_get_user_tracking_count($user_id, $view = 0, $include_quiz = true){
192 $query = wpc_get_tracked_lessons_by_user($user_id, $view, 1, 1000000, $include_quiz);
193 return count($query);
194 }
195
196 /**
197 * @param int $course_id The course ID for the percentage complete you'd like to retrieve
198 * @param int $user_id
199 * @param bool $view 0 for viewed lessons, 1 for completed lessons
200 * @return int Percentage complete for the passed course ID
201 */
202
203 function wpc_get_percent_done($course_id, $user_id = null, $view = 0){
204
205 if($course_id == -1) {
206 return 0;
207 }
208
209 if($user_id == null){
210 $user_id = get_current_user_id();
211 }
212
213 $args = array(
214 'post_to' => $course_id,
215 'connection_type' => array('lesson-to-course', 'quiz-to-course'),
216 'join' => false,
217 );
218
219 $lessons = wpc_get_connected($args);
220
221 $all_lessons_count = 0;
222
223 if(!empty($lessons)){
224 foreach($lessons as $lesson) {
225 if(get_post_status($lesson->post_from) == 'publish'){
226 $all_lessons_count++;
227 }
228 }
229 }
230
231 $count = 0;
232
233 if($all_lessons_count === 0) {
234 return 0;
235 }
236
237 foreach($lessons as $lesson) {
238 $status = wpc_get_lesson_status($user_id, $lesson->post_from);
239 $status = $view === 0 ? (int) $status['viewed'] : (int) $status['completed'];
240 $status === 1 && get_post_status($lesson->post_from) == 'publish' ? $count++ : '';
241 }
242
243 $percent_done = $count > 0 ? ($count / $all_lessons_count) * 100 : 0;
244 return (int) $percent_done;
245 }
246
247 /**
248 * @param int $module_id The module ID for the percentage complete you'd like to retrieve
249 * @param int $user_id
250 * @param bool $view 0 for viewed lessons, 1 for completed lessons
251 * @return int Percentage complete for the passed module ID
252 */
253
254 function wpc_get_module_percent_done($module_id, $user_id, $view = 0) {
255 global $wpdb;
256 $table_name = $wpdb->prefix . "wpc_connections";
257 $sql = "SELECT post_from FROM $table_name WHERE connection_type = 'lesson-to-module' AND post_to = $module_id";
258 $results = $wpdb->get_results($sql, ARRAY_N);
259 $all_lesson_ids_count = $wpdb->num_rows;
260 $lesson_ids = array_map('end', $results);
261 $lesson_ids = "'" . implode("', '", $lesson_ids) . "'";
262
263 $table_name = $wpdb->prefix . "wpc_tracking";
264 if($view === 0) {
265 $sql = "SELECT post_id FROM $table_name WHERE post_id IN ( $lesson_ids ) AND user_id = $user_id";
266 } else {
267 $sql = "SELECT post_id FROM $table_name WHERE post_id IN ( $lesson_ids ) AND user_id = $user_id AND completed = $view";
268 }
269
270 $results = $wpdb->get_results($sql);
271 $tracked_lesson_ids = $wpdb->num_rows;
272
273 return $tracked_lesson_ids === 0 || $all_lesson_ids_count === 0 ? 0 : ( $tracked_lesson_ids / $all_lesson_ids_count ) * 100;
274 }
275
276 function wpc_get_average_percent( $view = 0, $count = 5, $time_length = 7889229 ) {
277 $time_now = time();
278 $time_start = $time_now - $time_length;
279
280 global $wpdb;
281 $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
282 $table_name = $wpdb->prefix . "wpc_connections";
283 $tracking_table = $wpdb->prefix . "wpc_tracking";
284
285 $course_titles = array();
286 $all_percent = array();
287
288 $course_args = array(
289 'post_type' => 'course',
290 'nopaging' => true,
291 'post_status' => 'publish',
292 'posts_per_page' => -1,
293 );
294
295 $course_query = new WP_Query($course_args);
296
297 while($course_query->have_posts()) {
298 $course_query->the_post();
299 $id = get_the_ID();
300 $course_titles[] = get_the_title();
301
302 $sql = "SELECT post_from FROM $table_name WHERE connection_type = 'lesson-to-course' AND post_to = $id";
303 $wpdb->get_results($sql);
304 $total_connected_count = $wpdb->num_rows;
305
306 $sql = $view === 0 ? "SELECT user_id FROM $tracking_table WHERE course_id = $id" : "SELECT user_id FROM $tracking_table WHERE course_id = $id AND completed = 1";
307 $wpdb->get_results($sql);
308 $total_tracked = $wpdb->num_rows;
309
310 if($total_connected_count !== 0 && $total_tracked !== 0) {
311 $avg_lessons_viewed = $total_tracked / $user_count;
312 $avg_percent = $avg_lessons_viewed / $total_connected_count;
313 } else {
314 $avg_percent = 0;
315 }
316
317 $all_percent[] = $avg_percent * 100;
318 }
319
320 array_multisort($all_percent, SORT_DESC, SORT_NUMERIC, $course_titles);
321 // limit number of courses
322 $all_percent = array_slice($all_percent, 0, 5);
323 $course_titles = array_slice($course_titles, 0, 5);
324 return array($course_titles, $all_percent);
325 }
326
327 ?>