public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/pages/page-course-modify.php
/**
 * Run the changes for the course access change.
 *
 * @since 1.0.0
 *
 * @param object $page The current page object for messages.
 * @param string $action The action that's been requested.
 * @param object $userDetails The details of this course.
 */
Run the changes for the course access change.

@param object $page The current page object for messages.
@param string $action The action that's been requested.
@param object $userDetails The details of this course.
Run the changes for the course access change . @param object $page The current page object for messages . @param string $action The action that ' s been requested . @param object $userDetails The details of this course .
function WPCW_showPage_ModifyCourse_courseAccess_runAccessChanges( $page, $action, $courseDetails ) {
	$args = array();
	switch ( $action ) {
		case 'grant_access_users_all':
			$userType = false;
			break;
		case 'grant_access_users_all_subscribers':
			$args['role'] = 'subscriber';
			$userType     = __( 'subscriber', 'wp-courseware' );
			break;
		case 'grant_access_users_admins':
			$args['role'] = 'administrator';
			$userType     = __( 'admin', 'wp-courseware' );
			break;
		default:
			$page->showMessage( __( 'Unknown access change was requested.', 'wp-courseware' ), true );
			return;
			break;
	}
	WPCW_messages_showProgress( sprintf( __( 'Requesting a list of <b>all %s users</b> to update... (this make take a while)...', 'wp-courseware' ), $userType ), 0 );
	$userList = get_users( $args );
	if ( ! empty( $userList ) ) {
		$userCount = count( $userList );
		WPCW_messages_showProgress( sprintf( __( 'Found %d user(s), so now starting to add them to this course...', 'wp-courseware' ), $userCount ), 1 );
		global $wpdb, $wpcwdb;
		$count = 0;
		foreach ( $userList as $userDetails ) {
			WPCW_messages_showProgress( sprintf( __( 'Processing <b>%s</b>... ', 'wp-courseware' ), $userDetails->data->user_login, $userDetails->data->display_name ), 2 );
			$entryExists = $wpdb->get_row( $wpdb->prepare( "
				SELECT *
				 FROM $wpcwdb->user_courses
				WHERE user_id = %d
				  AND course_id = %d
				 ", $userDetails->ID, $courseDetails->course_id ) );
			if ( $entryExists ) {
				WPCW_messages_showProgress( __( 'User can already access this course. Skipping.', 'wp-courseware' ), 3 );
			} 
			else {
				$wpdb->query( $wpdb->prepare( "
				INSERT INTO $wpcwdb->user_courses
				(user_id, course_id, course_progress, course_final_grade_sent)
				VALUES(%d, %d, 0, '')
				 ", $userDetails->ID, $courseDetails->course_id ) );
				WPCW_messages_showProgress( __( 'Added.', 'wp-courseware' ), 3 );
			}
			$count ++;
			WPCW_messages_showProgress( sprintf( __( 'Done. %.1f%% complete.', 'wp-courseware' ), ( $count / $userCount ) * 100 ), 3 );
		}
	} else {
		WPCW_messages_showProgress( __( 'No users found. Nothing to do.', 'wp-courseware' ), 1 );
	}
	WPCW_messages_showProgress( '<b>' . __( 'All done.', 'wp-courseware' ) . '</b>', 0 );
}
function WPCW_showPage_ModifyCourse_courseAccess_runAccessChanges ( $ page , $ action , $ courseDetails ) { $ args = array ( ) ; switch ( $ action ) { case 'grant_access_users_all' : $ userType = false ; break ; case 'grant_access_users_all_subscribers' : $ args [ 'role' ] = 'subscriber' ; $ userType = __ ( 'subscriber' , 'wp-courseware' ) ; break ; case 'grant_access_users_admins' : $ args [ 'role' ] = 'administrator' ; $ userType = __ ( 'admin' , 'wp-courseware' ) ; break ; default : $ page -> showMessage ( __ ( 'Unknown access change was requested.' , 'wp-courseware' ) , true ) ; return ; break ; } WPCW_messages_showProgress ( sprintf ( __ ( 'Requesting a list of <b>all %s users</b> to update... (this make take a while)...' , 'wp-courseware' ) , $ userType ) , 0 ) ; $ userList = get_users ( $ args ) ; if ( ! empty ( $ userList ) ) { $ userCount = count ( $ userList ) ; WPCW_messages_showProgress ( sprintf ( __ ( 'Found %d user(s), so now starting to add them to this course...' , 'wp-courseware' ) , $ userCount ) , 1 ) ; global $ wpdb , $ wpcwdb ; $ count = 0 ; foreach ( $ userList as $ userDetails ) { WPCW_messages_showProgress ( sprintf ( __ ( 'Processing <b>%s</b>... ' , 'wp-courseware' ) , $ userDetails -> data -> user_login , $ userDetails -> data -> display_name ) , 2 ) ; $ entryExists = $ wpdb -> get_row ( $ wpdb -> prepare ( " 
				SELECT *
				 FROM  $ wpcwdb -> user_courses 
				WHERE user_id = %d
				  AND course_id = %d
				  " , $ userDetails -> ID , $ courseDetails -> course_id ) ) ; if ( $ entryExists ) { WPCW_messages_showProgress ( __ ( 'User can already access this course. Skipping.' , 'wp-courseware' ) , 3 ) ; } else { $ wpdb -> query ( $ wpdb -> prepare ( " 
				INSERT INTO  $ wpcwdb -> user_courses 
				(user_id, course_id, course_progress, course_final_grade_sent)
				VALUES(%d, %d, 0, '')
				  " , $ userDetails -> ID , $ courseDetails -> course_id ) ) ; WPCW_messages_showProgress ( __ ( 'Added.' , 'wp-courseware' ) , 3 ) ; } $ count ++ ; WPCW_messages_showProgress ( sprintf ( __ ( 'Done. %.1f%% complete.' , 'wp-courseware' ) , ( $ count / $ userCount ) * 100 ) , 3 ) ; } } else { WPCW_messages_showProgress ( __ ( 'No users found. Nothing to do.' , 'wp-courseware' ) , 1 ) ; } WPCW_messages_showProgress ( '<b>' . __ ( 'All done.' , 'wp-courseware' ) . '</b>' , 0 ) ; }
Run the changes for the course access change.
Run the changes for the course access change .
// No defaults actually, get_users() gets all users by default. // Kick of message to show we've started. // Report how many users we have to process. // Each user has 2 DB accesses to update, so this may take a while. // See if the user already exists for this course. // They already exist, nothing to do. // Adding the user