learn_mark_as_complete.php
7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Metabox' ) ) && ( ! class_exists( 'LearnDash_Settings_Metabox_Course_Users_Management' ) ) ) {
class LearnDash_Settings_Metabox_Course_Users_Management extends LearnDash_Settings_Metabox {
public function __construct() {
// What screen ID are we showing on.
$this->settings_screen_id = 'sfwd-courses';
// Used within the Settings API to uniquely identify this section.
$this->settings_metabox_key = 'learndash-course-users-management';
// Section label/header.
$this->settings_section_label = sprintf(
// translators: placeholder: Course.
esc_html_x( '%s Complete Management', 'placeholder: management', 'learndash' ),
learndash_get_custom_label( 'course' )
);
parent::__construct();
}
protected function show_settings_metabox_fields( $metabox = null ) {
if ( ( is_object( $metabox ) ) && ( is_a( $metabox, 'LearnDash_Settings_Metabox' ) ) && ( $metabox->settings_metabox_key === $this->settings_metabox_key ) ) {
if ( ( isset( $metabox->post ) ) && ( is_a( $metabox->post, 'WP_Post ' ) ) ) {
$course_id = $metabox->post->ID;
} else {
$course_id = get_the_ID();
}
if ( ( ! empty( $course_id ) ) && ( get_post_type( $course_id ) === learndash_get_post_type_slug( 'course' ) ) ) {
$users = learndash_get_course_users_access_from_meta( $course_id );
echo '<table id="markcomplete" width="100%"> <thead><tr><th>Enrolment Date</th><th>Email</th><th>First Name</th><th>Last Name</th><th>Online</th><th>Mark Complete</th></tr></thead>';
foreach($users as $user){
$user_info = get_userdata($user);
$course_status = learndash_course_status( $course_id ,$user);
if( $course_status != "Completed" ){
$course_enrolled_since = ld_course_access_from( $course_id, $user );
$course_enrolled_since = learndash_adjust_date_time_display( $course_enrolled_since, 'Y-m-d H:i:s' );
$online = "Offline";
if(is_user_online( $user )){
$online = "Online";
}
echo '<tr><td>'. $course_enrolled_since .'</td><td>'. $user_info->user_email .'</td><td>'.$user_info->first_name.'</td><td>'.$user_info->last_name.'</td><td>'. $online .'</td><td><button class="mark_as_complete components-button is-primary" data-user-id="'.$user.'" data-course-id="'.$course_id.'" >Mark Complete</button></td></tr>';
}
}
echo "</table>";
echo"";
}
}
}
public function save_post_meta_box( $post_id = 0, $saved_post = null, $update = null, $settings_field_updates = null ) {
if ( ( isset( $_POST['learndash_course_users_nonce'] ) ) && ( wp_verify_nonce( $_POST['learndash_course_users_nonce'], 'learndash_course_users_nonce_' . $post_id ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ( isset( $_POST['learndash_course_users'] ) ) && ( isset( $_POST['learndash_course_users'][ $post_id ] ) ) && ( ! empty( $_POST['learndash_course_users'][ $post_id ] ) ) && isset( $_POST[ 'learndash_course_users-' . $post_id . '-changed' ] ) && ( ! empty( $_POST[ 'learndash_course_users-' . $post_id . '-changed' ] ) ) ) {
$course_users = (array) json_decode( stripslashes( $_POST['learndash_course_users'][ $post_id ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
learndash_set_users_for_course( $post_id, $course_users );
}
}
}
}
add_filter(
'learndash_post_settings_metaboxes_init_' . learndash_get_post_type_slug( 'course' ),
function( $metaboxes = array() ) {
if ( ( ! isset( $metaboxes['LearnDash_Settings_Metabox_Course_Users_Management'] ) ) && ( class_exists( 'LearnDash_Settings_Metabox_Course_Users_Management' ) ) ) {
$metaboxes['LearnDash_Settings_Metabox_Course_Users_Management'] = LearnDash_Settings_Metabox_Course_Users_Management::add_metabox_instance();
}
return $metaboxes;
},
50,
1
);
}
add_filter(
'learndash_header_data',
function( $header_data, $menu_tab_key, $admin_tab_sets ) {
$header_data['tabs'] = array_merge(
$header_data['tabs'],
array(
array(
'id' => 'course-complete-settings',
'name' => esc_html__( 'Mark as Completed', 'learndash' ),
'metaboxes' => [ 'learndash-course-users-management' ],
'showDocumentSidebar' => 'false',
),
)
);
return $header_data;
},
30,
3
);
add_action( 'wp_ajax_mark_as_complete', 'mark_as_complete_callback' );
function mark_as_complete_callback() {
$selected_user_id = $_POST['user_id'];
$selected_course_id = $_POST['course_id'];
learndash_user_course_complete_all_steps( $selected_user_id, $selected_course_id );
wp_send_json( array( 'success' => true ) );
}
add_action( 'admin_print_scripts-post-new.php', 'course_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'course_admin_script', 11 );
function course_admin_script() {
global $post_type;
if( 'sfwd-courses' == $post_type )
wp_enqueue_style( 'course-dataTables-styles', '//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css' );
wp_enqueue_script( 'course-dataTables-script', '//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js' );
wp_enqueue_script( 'course-admin-script', get_stylesheet_directory_uri() . '/js/course-admin.js' );
wp_enqueue_style( 'course-admin-styles', get_stylesheet_directory_uri() . '/css/course-admin.css' );
}
//Update user online status
add_action('init', 'users_status_init');
add_action('admin_init', 'users_status_init');
function users_status_init(){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
$user = wp_get_current_user(); //Get the current user's data
//Update the user if they are not on the list, or if they have not been online in the last 900 seconds (15 minutes)
if ( !isset($logged_in_users[$user->ID]['last']) || $logged_in_users[$user->ID]['last'] <= time()-900 ){
$logged_in_users[$user->ID] = array(
'id' => $user->ID,
'username' => $user->user_login,
'last' => time(),
);
set_transient('users_status', $logged_in_users, 900); //Set this transient to expire 15 minutes after it is created.
}
}
//Check if a user has been online in the last 15 minutes
function is_user_online($id){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
return isset($logged_in_users[$id]['last']) && $logged_in_users[$id]['last'] > time()-900; //Return boolean if the user has been online in the last 900 seconds (15 minutes).
}
//Check when a user was last online.
function user_last_online($id){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
//Determine if the user has ever been logged in (and return their last active date if so).
if ( isset($logged_in_users[$id]['last']) ){
return $logged_in_users[$id]['last'];
} else {
return false;
}
}