public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/pages/page-course-dashboard.php
/**
 * Delete a course, its modules and disassociating of the units that it contains.
 *
 * @since 1.0.0
 *
 * @param object $courseDetails The details of the course to delete.
 * @param string $deleteMethod The deletion method. Either 'complete' or 'course_and_module'.
 */
Delete a course, its modules and disassociating of the units that it contains.

@param object $courseDetails The details of the course to delete.
@param string $deleteMethod The deletion method. Either 'complete' or 'course_and_module'.
Delete a course its modules and disassociating of the units that it contains . @param object $courseDetails The details of the course to delete . @param string $deleteMethod The deletion method . Either ' complete ' or ' course_and_module ' .
function WPCW_modules_deleteCourse( $courseDetails, $deleteMethod ) {
	if ( ! $courseDetails ) {
		return;
	}
	global $wpdb, $wpcwdb;
	$wpdb->show_errors();
	$unitList = $wpdb->get_col( $wpdb->prepare( "
    	SELECT unit_id
    	FROM $wpcwdb->units_meta
    	WHERE parent_course_id = %d
    ", $courseDetails->course_id ) );
	switch ( $deleteMethod ) {
		case 'course_and_module':
			if ( $unitList ) {
				foreach ( $unitList as $unitID ) {
					$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
					", $unitID );
					$wpdb->query( $SQL );
					update_post_meta( $unitID, 'wpcw_associated_module', 0 );
					$SQL = $wpdb->prepare( "
						DELETE FROM $wpcwdb->user_progress
						WHERE unit_id = %d
					", $unitID );
					$wpdb->query( $SQL );
					$SQL = $wpdb->prepare( "
						DELETE FROM $wpcwdb->user_progress_quiz
						WHERE unit_id = %d
					", $unitID );
					$wpdb->query( $SQL );
					$SQL = $wpdb->prepare( "UPDATE $wpcwdb->quiz SET parent_unit_id = 0, parent_course_id = 0 WHERE parent_unit_id = %d", $unitID );
					$wpdb->query( $SQL );
				}
			}
			$SQL = $wpdb->prepare( "
				UPDATE $wpcwdb->quiz
				   SET parent_course_id = 0
				WHERE parent_course_id = %d
			", $courseDetails->course_id );
			$wpdb->query( $SQL );
			break;
		default:
			$quizList = $wpdb->get_col( $wpdb->prepare( "
		    	SELECT quiz_id
		    	FROM $wpcwdb->quiz
		    	WHERE parent_course_id = %d
		    ", $courseDetails->course_id ) );
			if ( $quizList ) {
				$quizIDList = implode( ",", $quizList );
				$questionIDList = $wpdb->get_col( "
					SELECT question_id
					FROM $wpcwdb->quiz_qs_mapping
					WHERE parent_quiz_id IN ($quizIDList)
					GROUP BY question_id
				" );
				$wpdb->query( "
					DELETE FROM $wpcwdb->quiz_qs_mapping
					WHERE parent_quiz_id IN ($quizIDList)
				" );
				if ( ! empty( $questionIDList ) ) {
					foreach ( $questionIDList as $questionID ) {
						WPCW_questions_updateUsageCount( $questionID );
					}
				}
			}
			$SQL = $wpdb->prepare( "DELETE FROM $wpcwdb->quiz WHERE parent_course_id = %d", $courseDetails->course_id );
			$wpdb->query( $SQL );
			if ( $unitList ) {
				$unitIDList = implode( ",", $unitList );
				$wpdb->query( "
					DELETE FROM $wpcwdb->units_meta
					WHERE unit_id IN ($unitIDList)
				" );
				$wpdb->query( "
					DELETE FROM $wpcwdb->user_progress
					WHERE unit_id IN ($unitIDList)
				" );
				$wpdb->query( "
					DELETE FROM $wpcwdb->user_progress_quiz
					WHERE unit_id IN ($unitIDList)
				" );
				foreach ( $unitList as $unitID ) {
					wp_delete_post( $unitID, true );
				}
			} 
			break;
	}
	$wpdb->query( $SQL = $wpdb->prepare( "
				DELETE FROM $wpcwdb->modules
				WHERE parent_course_id = %d
			", $courseDetails->course_id ) );
	$wpdb->query( $SQL = $wpdb->prepare( "
				DELETE FROM $wpcwdb->certificates
				WHERE cert_course_id = %d
			", $courseDetails->course_id ) );
	$wpdb->query( $SQL = $wpdb->prepare( "
				DELETE FROM $wpcwdb->courses
				WHERE course_id = %d
			", $courseDetails->course_id ) );
	$wpdb->query( $SQL = $wpdb->prepare( "
				DELETE FROM $wpcwdb->user_courses
				WHERE course_id = %d
			", $courseDetails->course_id ) );
	do_action( 'wpcw_course_deleted', $courseDetails );
	return true;
}
function WPCW_modules_deleteCourse ( $ courseDetails , $ deleteMethod ) { if ( ! $ courseDetails ) { return ; } global $ wpdb , $ wpcwdb ; $ wpdb -> show_errors ( ) ; $ unitList = $ wpdb -> get_col ( $ wpdb -> prepare ( " 
    	SELECT unit_id
    	FROM  $ wpcwdb -> units_meta 
    	WHERE parent_course_id = %d
     " , $ courseDetails -> course_id ) ) ; switch ( $ deleteMethod ) { case 'course_and_module' : if ( $ unitList ) { foreach ( $ unitList as $ unitID ) { $ 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
					 " , $ unitID ) ; $ wpdb -> query ( $ SQL ) ; update_post_meta ( $ unitID , 'wpcw_associated_module' , 0 ) ; $ SQL = $ wpdb -> prepare ( " 
						DELETE FROM  $ wpcwdb -> user_progress 
						WHERE unit_id = %d
					 " , $ unitID ) ; $ wpdb -> query ( $ SQL ) ; $ SQL = $ wpdb -> prepare ( " 
						DELETE FROM  $ wpcwdb -> user_progress_quiz 
						WHERE unit_id = %d
					 " , $ unitID ) ; $ wpdb -> query ( $ SQL ) ; $ SQL = $ wpdb -> prepare ( " UPDATE  $ wpcwdb -> quiz  SET parent_unit_id = 0, parent_course_id = 0 WHERE parent_unit_id = %d " , $ unitID ) ; $ wpdb -> query ( $ SQL ) ; } } $ SQL = $ wpdb -> prepare ( " 
				UPDATE  $ wpcwdb -> quiz 
				   SET parent_course_id = 0
				WHERE parent_course_id = %d
			 " , $ courseDetails -> course_id ) ; $ wpdb -> query ( $ SQL ) ; break ; default : $ quizList = $ wpdb -> get_col ( $ wpdb -> prepare ( " 
		    	SELECT quiz_id
		    	FROM  $ wpcwdb -> quiz 
		    	WHERE parent_course_id = %d
		     " , $ courseDetails -> course_id ) ) ; if ( $ quizList ) { $ quizIDList = implode ( " , " , $ quizList ) ; $ questionIDList = $ wpdb -> get_col ( " 
					SELECT question_id
					FROM  $ wpcwdb -> quiz_qs_mapping 
					WHERE parent_quiz_id IN ( $ quizIDList )
					GROUP BY question_id
				 " ) ; $ wpdb -> query ( " 
					DELETE FROM  $ wpcwdb -> quiz_qs_mapping 
					WHERE parent_quiz_id IN ( $ quizIDList )
				 " ) ; if ( ! empty ( $ questionIDList ) ) { foreach ( $ questionIDList as $ questionID ) { WPCW_questions_updateUsageCount ( $ questionID ) ; } } } $ SQL = $ wpdb -> prepare ( " DELETE FROM  $ wpcwdb -> quiz  WHERE parent_course_id = %d " , $ courseDetails -> course_id ) ; $ wpdb -> query ( $ SQL ) ; if ( $ unitList ) { $ unitIDList = implode ( " , " , $ unitList ) ; $ wpdb -> query ( " 
					DELETE FROM  $ wpcwdb -> units_meta 
					WHERE unit_id IN ( $ unitIDList )
				 " ) ; $ wpdb -> query ( " 
					DELETE FROM  $ wpcwdb -> user_progress 
					WHERE unit_id IN ( $ unitIDList )
				 " ) ; $ wpdb -> query ( " 
					DELETE FROM  $ wpcwdb -> user_progress_quiz 
					WHERE unit_id IN ( $ unitIDList )
				 " ) ; foreach ( $ unitList as $ unitID ) { wp_delete_post ( $ unitID , true ) ; } } break ; } $ wpdb -> query ( $ SQL = $ wpdb -> prepare ( " 
				DELETE FROM  $ wpcwdb -> modules 
				WHERE parent_course_id = %d
			 " , $ courseDetails -> course_id ) ) ; $ wpdb -> query ( $ SQL = $ wpdb -> prepare ( " 
				DELETE FROM  $ wpcwdb -> certificates 
				WHERE cert_course_id = %d
			 " , $ courseDetails -> course_id ) ) ; $ wpdb -> query ( $ SQL = $ wpdb -> prepare ( " 
				DELETE FROM  $ wpcwdb -> courses 
				WHERE course_id = %d
			 " , $ courseDetails -> course_id ) ) ; $ wpdb -> query ( $ SQL = $ wpdb -> prepare ( " 
				DELETE FROM  $ wpcwdb -> user_courses 
				WHERE course_id = %d
			 " , $ courseDetails -> course_id ) ) ; do_action ( 'wpcw_course_deleted' , $ courseDetails ) ; return true ; }
Delete a course, its modules and disassociating of the units that it contains.
Delete a course its modules and disassociating of the units that it contains .
// Get a list of units for this course. // Disassociate quizzes, units, etc. // Unit Meta // Remove course info. Update database with new association and ordering. // Unit Post Meta // Update post meta to remove associated module detail // User Progress // Remove progress for this unit, since unit is now unassociated. // User Quiz Progress // Progress is linked to a course, hence wanting to remove it. // Quiz - Unconnect it from the quiz that it's associated with. // Quiz Association // Remove course info for all quizzes // Complete delete - delete absolutely everything... // Quiz Deletion - need a list of quiz IDs to delete the // question mappings // Reduce SQL queries - do an ARRAY search on the WHERE. // Get a list of question IDs first // Remove the mappings between Quiz and their Questions // Update usage count for questions // Quizzes themselves // Reduce SQL queries - do an ARRAY search on the WHERE. // Unit Meta // User Progress // User Quiz Progress // Use WordPress delete for deleting the unit. // end unit check // Module deletion here. // Certificate deletion for this course // Perform course deletion here. // Course progress summary for each user needs to be removed. // Trigger event that course has been deleted