public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/ajax.php
/**
 * Handle Unit Ordering Saving.
 *
 * This function will save the order of the modules, units and any unassigned units.
 *
 * @since 1.0.0
 */
Handle Unit Ordering Saving.
This function will save the order of the modules, units and any unassigned units.


Handle Unit Ordering Saving . This function will save the order of the modules units and any unassigned units .
function WPCW_AJAX_handleUnitOrderingSaving() {
	if ( ! wp_verify_nonce( WPCW_arrays_getValue( $_POST, 'order_nonce' ), 'wpcw-order-nonce' ) ) {
		die ( __( 'Security check failed!', 'wp-courseware' ) );
	}
	$moduleList = WPCW_arrays_getValue( $_POST, 'moduleList' );
	if ( ! $moduleList || count( $moduleList ) < 1 ) {
		die();
	}
	global $wpdb, $wpcwdb;
	$wpdb->show_errors();
	$parentCourseID = 0;
	$moduleOrderCount = 0;
	$unitOrderCount = 0;
	foreach ( $moduleList as $moduleID ) {
		if ( preg_match( '/^wpcw_mod_(\d+)$/', $moduleID, $matches ) ) {
			$moduleDetails = WPCW_modules_getModuleDetails( $matches[1] );
			if ( $moduleDetails ) {
				$parentCourseID = $moduleDetails->parent_course_id;
				break;
			}
		}
	}
	if ( ! $parentCourseID ) {
		error_log( 'WPCW_AJAX_handleUnitOrderingSaving(). No associated parent course ID, so aborting.' );
		die();
	}
	$SQL = $wpdb->prepare( "
		UPDATE $wpcwdb->units_meta
		   SET unit_order = 0, parent_module_id = 0,
		   	   parent_course_id = 0, unit_number = 0
		WHERE parent_course_id = %d
	", $parentCourseID );
	$wpdb->query( $SQL );
	foreach ( $moduleList as $moduleID ) {
		if ( preg_match( '/^wpcw_mod_(\d+)$/', $moduleID, $matches ) ) {
			$moduleOrderCount++;
			$moduleIDClean = $matches[1];
			$SQL = $wpdb->prepare( "
				UPDATE $wpcwdb->modules
				   SET module_order = %d, module_number = %d
				WHERE module_id = %d
			", $moduleOrderCount, $moduleOrderCount, $moduleIDClean );
			$wpdb->query( $SQL );
			$unitList = WPCW_arrays_getValue( $_POST, $moduleID );
			if ( $unitList && count( $unitList ) > 0 ) {
				$unitNumber = 0;
				foreach ( $unitList as $unitID ) {
					$unitNumber++;
					if ( preg_match( '/^wpcw_unit_(\d+)$/', $unitID, $matches ) ) {
						$unitOrderCount += 10;
						$unitIDClean    = $matches[1];
						$SQL = $wpdb->prepare( "
							UPDATE $wpcwdb->units_meta
							   SET unit_order = %d, parent_module_id = %d,
							   	   parent_course_id = %d, unit_number = %d
							WHERE unit_id = %d
						", $unitOrderCount, $moduleIDClean,
							$parentCourseID, $unitNumber,
							$unitIDClean );
						$wpdb->query( $SQL );
						update_post_meta( $unitIDClean, 'wpcw_associated_module', $moduleIDClean );
					}
				}
			} 
		}
	}
	foreach ( $_POST as $key => $value ) {
		if ( preg_match( '/^wpcw_unit_(\d+)$/', $key, $matches ) ) {
			$unitIDClean = $matches[1];
			$quizIDRaw = false;
			if ( $value && is_array( $value ) ) {
				$quizIDRaw = $value[0];
			}
			if ( preg_match( '/^wpcw_quiz_(\d+)$/', $quizIDRaw, $matches ) ) {
				$quizIDClean = $matches[1];
				$parentData     = WPCW_units_getAssociatedParentData( $unitIDClean );
				$parentCourseID = $parentData->parent_course_id;
				$SQL = $wpdb->prepare( "
					UPDATE $wpcwdb->quiz
					   SET parent_unit_id = %d, parent_course_id = %d
					WHERE quiz_id = %d
				", $unitIDClean, $parentCourseID, $quizIDClean );
				$wpdb->query( $SQL );
				$SQL = $wpdb->prepare( "
					UPDATE $wpcwdb->user_progress_quiz
					   SET unit_id = %d
					WHERE quiz_id = %d
				", $unitIDClean, $quizIDClean );
				$wpdb->query( $SQL );
			}
		}
	}
	$unitList = WPCW_arrays_getValue( $_POST, 'unassunits' );
	if ( $unitList && count( $unitList ) > 0 ) {
		foreach ( $unitList as $unitID ) {
			if ( preg_match( '/^wpcw_unit_(\d+)$/', $unitID, $matches ) ) {
				$unitIDClean = $matches[1];
				WPCW_queue_dripfeed::updateQueueItems_unitRemovedFromCourse( $unitIDClean );
				$SQL = $wpdb->prepare( "
					UPDATE $wpcwdb->units_meta
					   SET unit_order = 0, parent_module_id = 0, parent_course_id = 0, unit_number = 0
					WHERE unit_id = %d
				", $unitIDClean );
				$wpdb->query( $SQL );
				update_post_meta( $unitIDClean, 'wpcw_associated_module', 0 );
				$SQL = $wpdb->prepare( "
					DELETE FROM $wpcwdb->user_progress
					WHERE unit_id = %d
				", $unitIDClean );
				$wpdb->query( $SQL );
			}
		}
	}
	$quizList = WPCW_arrays_getValue( $_POST, 'unassquizzes' );
	if ( $quizList && count( $quizList ) > 0 ) {
		foreach ( $quizList as $quizID ) {
			if ( preg_match( '/^wpcw_quiz_(\d+)$/', $quizID, $matches ) ) {
				$quizIDClean = $matches[1];
				$SQL = $wpdb->prepare( "
					UPDATE $wpcwdb->quiz
					   SET parent_unit_id = 0, parent_course_id = 0
					WHERE quiz_id = %d
				", $quizIDClean );
				$wpdb->query( $SQL );
				$SQL = $wpdb->prepare( "
					UPDATE $wpcwdb->user_progress_quiz
					   SET unit_id = 0
					WHERE quiz_id = %d
				", $quizIDClean );
				$wpdb->query( $SQL );
			}
		}  ($quizList as $quizID)
	}
	$courseDetails = WPCW_courses_getCourseDetails( $parentCourseID );
	if ( $courseDetails ) {
		do_action( 'wpcw_course_details_updated', $courseDetails );
	}
	die();
}
function WPCW_AJAX_handleUnitOrderingSaving ( ) { if ( ! wp_verify_nonce ( WPCW_arrays_getValue ( $ _POST , 'order_nonce' ) , 'wpcw-order-nonce' ) ) { die ( __ ( 'Security check failed!' , 'wp-courseware' ) ) ; } $ moduleList = WPCW_arrays_getValue ( $ _POST , 'moduleList' ) ; if ( ! $ moduleList || count ( $ moduleList ) < 1 ) { die ( ) ; } global $ wpdb , $ wpcwdb ; $ wpdb -> show_errors ( ) ; $ parentCourseID = 0 ; $ moduleOrderCount = 0 ; $ unitOrderCount = 0 ; foreach ( $ moduleList as $ moduleID ) { if ( preg_match ( '/^wpcw_mod_(\d+)$/' , $ moduleID , $ matches ) ) { $ moduleDetails = WPCW_modules_getModuleDetails ( $ matches [ 1 ] ) ; if ( $ moduleDetails ) { $ parentCourseID = $ moduleDetails -> parent_course_id ; break ; } } } if ( ! $ parentCourseID ) { error_log ( 'WPCW_AJAX_handleUnitOrderingSaving(). No associated parent course ID, so aborting.' ) ; die ( ) ; } $ SQL = $ wpdb -> prepare ( " 
		UPDATE  $ wpcwdb -> units_meta 
		   SET unit_order = 0, parent_module_id = 0,
		   	   parent_course_id = 0, unit_number = 0
		WHERE parent_course_id = %d
	 " , $ parentCourseID ) ; $ wpdb -> query ( $ SQL ) ; foreach ( $ moduleList as $ moduleID ) { if ( preg_match ( '/^wpcw_mod_(\d+)$/' , $ moduleID , $ matches ) ) { $ moduleOrderCount ++ ; $ moduleIDClean = $ matches [ 1 ] ; $ SQL = $ wpdb -> prepare ( " 
				UPDATE  $ wpcwdb -> modules 
				   SET module_order = %d, module_number = %d
				WHERE module_id = %d
			 " , $ moduleOrderCount , $ moduleOrderCount , $ moduleIDClean ) ; $ wpdb -> query ( $ SQL ) ; $ unitList = WPCW_arrays_getValue ( $ _POST , $ moduleID ) ; if ( $ unitList && count ( $ unitList ) > 0 ) { $ unitNumber = 0 ; foreach ( $ unitList as $ unitID ) { $ unitNumber ++ ; if ( preg_match ( '/^wpcw_unit_(\d+)$/' , $ unitID , $ matches ) ) { $ unitOrderCount += 10 ; $ unitIDClean = $ matches [ 1 ] ; $ SQL = $ wpdb -> prepare ( " 
							UPDATE  $ wpcwdb -> units_meta 
							   SET unit_order = %d, parent_module_id = %d,
							   	   parent_course_id = %d, unit_number = %d
							WHERE unit_id = %d
						 " , $ unitOrderCount , $ moduleIDClean , $ parentCourseID , $ unitNumber , $ unitIDClean ) ; $ wpdb -> query ( $ SQL ) ; update_post_meta ( $ unitIDClean , 'wpcw_associated_module' , $ moduleIDClean ) ; } } } } } foreach ( $ _POST as $ key => $ value ) { if ( preg_match ( '/^wpcw_unit_(\d+)$/' , $ key , $ matches ) ) { $ unitIDClean = $ matches [ 1 ] ; $ quizIDRaw = false ; if ( $ value && is_array ( $ value ) ) { $ quizIDRaw = $ value [ 0 ] ; } if ( preg_match ( '/^wpcw_quiz_(\d+)$/' , $ quizIDRaw , $ matches ) ) { $ quizIDClean = $ matches [ 1 ] ; $ parentData = WPCW_units_getAssociatedParentData ( $ unitIDClean ) ; $ parentCourseID = $ parentData -> parent_course_id ; $ SQL = $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> quiz 
					   SET parent_unit_id = %d, parent_course_id = %d
					WHERE quiz_id = %d
				 " , $ unitIDClean , $ parentCourseID , $ quizIDClean ) ; $ wpdb -> query ( $ SQL ) ; $ SQL = $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> user_progress_quiz 
					   SET unit_id = %d
					WHERE quiz_id = %d
				 " , $ unitIDClean , $ quizIDClean ) ; $ wpdb -> query ( $ SQL ) ; } } } $ unitList = WPCW_arrays_getValue ( $ _POST , 'unassunits' ) ; if ( $ unitList && count ( $ unitList ) > 0 ) { foreach ( $ unitList as $ unitID ) { if ( preg_match ( '/^wpcw_unit_(\d+)$/' , $ unitID , $ matches ) ) { $ unitIDClean = $ matches [ 1 ] ; WPCW_queue_dripfeed :: updateQueueItems_unitRemovedFromCourse ( $ unitIDClean ) ; $ SQL = $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> units_meta 
					   SET unit_order = 0, parent_module_id = 0, parent_course_id = 0, unit_number = 0
					WHERE unit_id = %d
				 " , $ unitIDClean ) ; $ wpdb -> query ( $ SQL ) ; update_post_meta ( $ unitIDClean , 'wpcw_associated_module' , 0 ) ; $ SQL = $ wpdb -> prepare ( " 
					DELETE FROM  $ wpcwdb -> user_progress 
					WHERE unit_id = %d
				 " , $ unitIDClean ) ; $ wpdb -> query ( $ SQL ) ; } } } $ quizList = WPCW_arrays_getValue ( $ _POST , 'unassquizzes' ) ; if ( $ quizList && count ( $ quizList ) > 0 ) { foreach ( $ quizList as $ quizID ) { if ( preg_match ( '/^wpcw_quiz_(\d+)$/' , $ quizID , $ matches ) ) { $ quizIDClean = $ matches [ 1 ] ; $ SQL = $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> quiz 
					   SET parent_unit_id = 0, parent_course_id = 0
					WHERE quiz_id = %d
				 " , $ quizIDClean ) ; $ wpdb -> query ( $ SQL ) ; $ SQL = $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> user_progress_quiz 
					   SET unit_id = 0
					WHERE quiz_id = %d
				 " , $ quizIDClean ) ; $ wpdb -> query ( $ SQL ) ; } } } $ courseDetails = WPCW_courses_getCourseDetails ( $ parentCourseID ) ; if ( $ courseDetails ) { do_action ( 'wpcw_course_details_updated' , $ courseDetails ) ; } die ( ) ; }
Handle Unit Ordering Saving.
Handle Unit Ordering Saving .
// Get list of modules to save, check IDs are what we expect, and abort if nothing to do. // Save new module ordering to database // Ordering of units is absolute to the whole course // Need a course ID for resetting the ordering. // Validate we have an actual module // Get course ID from module // If there's no associated parent course, there's an issue. // 2013-05-01 - Bug with orphan modules being left in the units_meta // Fix - Clean out existing units in this course, resetting them. // Then update the ordering using the loops below. // Check module name matches expected format. // Update module list with new ordering // Check units associated with this module // Check unit name matches expected format. // Update database with new association and ordering. // 2013-05-01 - Updated to use the module ID, rather than the module order. // end foreach // end of $unitList check // Check for any units that have associated quizzes // Check any post value that has a unit in it // Try to extract the unit ID // [wpcw_unit_71] => Array // ( //	[0] => wpcw_quiz_2 //) // Got a matching quiz ID // Grab parent course ID from unit. Can't assume all units are in same course. // Update database with new association and ordering. // Add new associated unit information to the user quiz progress, // keeping any existing quiz results. // Check for any unassigned units, and ensure they're de-associated from modules. // Check unit name matches expected format. // Remove notifications // Update database with new association and ordering. // Update post meta to remove associated module detail // Remove progress for this unit, as likely to be associated with something else. // Check for any unassigned quizzes, and ensure they're de-associated from units. // Check unit name matches expected format. // Update database with new association and ordering. // Remove the associated unit information from the user quiz progress. // But keep the quiz results for now. // end foreach ($quizList as $quizID) // Update course details