Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CoursePress] - Override Module Submit. Overrides Module Submit action. Those are triggered when the Next/Previous buttons are clicked in the front end
<?php
/**
* Plugin Name: [CoursePress] - Override Module Submit
* Plugin URI: https://premium.wpmudev.org/
* Description: Overrides Module Submit action. Those are triggered when the Next/Previous buttons are clicked in the fron end
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Override_Module_Submit' ) ) {
class WPMUDEV_Override_Module_Submit {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Override_Module_Submit();
}
return self::$_instance;
}
private function __construct() {
add_action( 'init', array( $this, 'remove_default_action' ), 1 );
add_action( 'init', array( __CLASS__, 'process_submission' ), 20 );
}
public function remove_default_action() {
remove_action( 'init', array( 'CoursePress_Module', 'process_submission' ) );
}
public static function process_submission() {
if ( CoursePress_Module::is_valid() ) {
self::submit( $_REQUEST );
}
}
public static function submit( $input ) {
if ( ! ini_get( 'safe_mode' ) ) {
set_time_limit( 0 );
}
$course_id = (int) $input['course_id'];
$unit_id = (int) $input['unit_id'];
$module = isset( $input['module'] ) ? (array) $input['module'] : false;
$module_id = 0;
$page = ! empty( $input['page'] ) ? (int) $input['page'] : 1;
$student_id = (int) $input['student_id'];
$has_error = false;
$course_mode = get_post_meta( $course_id, 'cp_course_view', true );
$is_focus = 'focus' == $course_mode;
$excluded_modules = array(
'input-textarea',
'input-text',
'input-upload',
'input-form',
);
$validate = isset( $input['save_progress_and_exit'] )? false : true;
// Validate the course
$error = CoursePress_Data_Course::can_access( $course_id, $unit_id );
$can_update_course = CoursePress_Data_Capabilities::can_update_course( $course_id, $student_id );
if ( ! empty( $error ) ) {
$has_error = true;
CoursePress_Module::$error_message = $error;
} elseif ( true === CoursePress_Module::validate_course( $input ) ) {
$has_error = true;
} else {
if ( ! empty( $input['module_id'] ) ) {
$module_ids = (array) $input['module_id'];
foreach ( $module_ids as $module_id ) {
$attributes = CoursePress_Data_Module::attributes( $module_id );
$module_type = $attributes['module_type'];
$is_mandatory = ! empty( $attributes['mandatory'] );
$is_assessable = ! empty( $attributes['assessable'] ) || ! empty( $attributes['require_instructor_assessment'] );
$is_answerable = preg_match( '%input-%', $module_type );
if ( 'input-upload' == $module_type ) {
if ( false == $can_update_course && empty( $_FILES ) ) {
if ( $validate ) {
CoursePress_Module::$error_message = __( 'You need to complete the required module!', 'coursepress' );
$has_error = true;
}
}
continue; // Upload validation is at the bottom
}
if ( 'discussion' == $module_type ) {
if ( empty( $input['comment'] ) ) {
if ( $is_mandatory && false == $can_update_course ) {
// Check if current student previously commented.
if ( CoursePress_Data_Discussion::have_comments( $student_id, $module_id ) ) {
continue;
} else {
$has_error = true;
CoursePress_Module::$error_message = __( 'Your participation to the discussion is required!', 'coursepress' );
continue;
}
}
} else {
$comments = array(
'comment_content' => $input['comment'],
'comment_post_ID' => $module_id,
'user_ID' => $student_id,
);
// Check for parent comment
if ( ! empty( $input['comment_parent'] ) ) {
$comments['comment_parent'] = $input['comment_parent'];
}
CoursePress_Module::add_comment( $comments, $student_id );
// Update subscribers list
$field_name = CoursePress_Helper_Discussion::get_field_name();
$value = isset( $input[ $field_name ] ) ? $input[ $field_name ] : CoursePress_Helper_Discussion::get_default_key();
CoursePress_Data_Discussion::update_user_subscription( $student_id, $module_id, $value );
continue;
}
}
if ( $is_answerable ) {
if ( ! isset( $module[ $module_id ] ) || '' === ( $module[ $module_id ] ) ) {
// Check if module is mandatory
if ( $is_mandatory && false == $can_update_course ) {
if ( $validate ) {
CoursePress_Module::$error_message = __( 'You need to complete the required module!', 'coursepress' );
$has_error = true;
}
}
continue;
} else {
$response = $module[ $module_id ];
if ( 'input-quiz' == $module_type ) {
foreach ( $attributes['questions'] as $qi => $question ) {
$answers = array_keys( $question['options']['answers'] );
if ( isset( $response[ $qi ] ) && '' != $response[ $qi ] ) {
$qi_response = $response[ $qi ];
$values = array();
foreach ( $answers as $a_key ) {
$values[ $a_key ] = '';
if ( 'multiple' == $question['type'] ) {
$values[ $a_key ] = in_array( $a_key, $qi_response );
} else {
$values[ $a_key ] = $qi_response == $a_key;
}
}
$response[ $qi ] = $values;
}
}
}
// Record submission only if student actually submitted the form.
if ( empty( $input['is_module_hidden'][ $module_id ] ) ) {
// Attempt to record the submission.
CoursePress_Data_Student::module_response( $student_id, $course_id, $unit_id, $module_id, $response );
}
// override $is_assessable if module type 'input-form', regardless if enabled in admin dashboard or not
// logic from CoursePress_Data_Module::get_form_results() is that Form will have a grade of 100 if not required, otherwise check if empty for all submodules
if ( $module_type == 'input-form' ) {
$is_assessable = true;
}
// Check if the grade acquired pass
if ( true === $is_assessable && ! in_array( $module_type, $excluded_modules ) ) {
$minimum_grade = $attributes['minimum_grade'];
$grades = CoursePress_Data_Student::get_grade( $student_id, $course_id, $unit_id, $module_id );
$grade = CoursePress_Helper_Utility::get_array_val( $grades, 'grade' );
$pass = (int) $grade >= (int) $minimum_grade;
// Added the $validate check, so if the action is `save_progress_and_exit` ( on "Save Progress & Exit" click ),
// it should not return an error message, so js would allow to redirect
// to course page as expected
if ( false === $pass && false == $can_update_course && $validate ) {
$has_error = true;
CoursePress_Module::$error_message = ( $module_type == 'input-form' ) ?
__( 'You did not complete the form!', 'coursepress' )
: __( 'You did not pass the required minimum grade Bro!', 'coursepress' );
}
}
}
}
}
}
// Check for upload submission
if ( ! empty( $_FILES['module'] ) ) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$upload_overrides = array(
'test_form' => false,
'mimes' => CoursePress_Helper_Utility::allowed_student_mimes(),
);
$files = $_FILES['module'];
foreach ( $files['name'] as $_module_id => $filename ) {
$attributes = CoursePress_Data_Module::attributes( $_module_id );
$response = CoursePress_Data_Student::get_response( $student_id, $course_id, $unit_id, $_module_id );
$required = ! empty( $attributes['mandatory'] );
if ( true === $required && false == $can_update_course ) {
if ( empty( $filename ) ) {
if ( empty( $response ) ) {
if ( $validate ) {
CoursePress_Module::$error_message = __( 'You need to complete the required module!', 'coursepress' );
$has_error = true;
}
continue;
} else {
// There's an old submission, exclude!
continue;
}
}
} else {
// If it is not required and no submission, break
if ( empty( $filename ) ) {
continue;
}
}
$file = array(
'name' => $filename,
'size' => $files['size'][ $_module_id ],
'error' => $files['error'][ $_module_id ],
'type' => $files['type'][ $_module_id ],
'tmp_name' => $files['tmp_name'][ $_module_id ],
);
$response = wp_handle_upload( $file, $upload_overrides );
$response['size'] = $file['size'];
if ( ! empty( $response['error'] ) ) {
$has_error = true;
CoursePress_Module::$error_message = $response['error'];
} else {
CoursePress_Data_Student::module_response( $student_id, $course_id, $unit_id, $_module_id, $response );
}
}
}
}
$via_ajax = ! empty( $input['is_cp_ajax'] );
if ( $has_error ) {
if ( $via_ajax ) {
if ( $is_focus ) {
$html = '[coursepress_focus_item course="%s" unit="%s" type="%s" item_id="%s"]';
$html = do_shortcode( sprintf( $html, $course_id, $unit_id, 'module', $module_id ) );
} else {
$html = CoursePress_Template_Unit::unit_with_modules( $course_id, $unit_id, $page, $student_id );
}
$json_data = array(
'success' => false,
'data'=> array(
'error' => true,
'error_message' => CoursePress_Module::$error_message,
'html' => $html,
'is_reload' => false,
)
);
header('Content-type: text/plain');
echo json_encode( $json_data );
die();
} else {
add_action( 'coursepress_before_unit_modules', array( __CLASS__, 'show_error_message' ) );
}
} else {
if ( isset( $input['page'] ) ) {
$page = intval( $input['page'] );
}
// Update student progress
//CoursePress_Data_Student::get_calculated_completion_data( $student_id, $course_id );
$next = CoursePress_Data_Course::get_next_accessible_module( $course_id, $unit_id, $page, $module_id );
if ( $via_ajax ) {
$item_id = $next['id'];
$reload = false;
if ( 'section' == $next['type'] ) {
$reload = $unit_id != $next['unit'];
$unit_id = $next['unit'];
$item_id = $next['id'];
} else {
$item_id = $next['id'];
}
if ( $is_focus ) {
$html = '[coursepress_focus_item course="%s" unit="%s" type="%s" item_id="%s"]';
$html = do_shortcode( sprintf( $html, $course_id, $unit_id, $next['type'], $item_id ) );
} else {
$html = '';
if ( 'completion_page' != $next['id'] ) {
$html = CoursePress_Template_Unit::unit_with_modules( $course_id, $unit_id, $item_id, $student_id );
}
}
$type = 'completion_page' == $next['id'] ? 'completion' : $next['type'];
/**
* Save last seen unit and page.
*/
if ( isset( $input['save_progress_and_exit'] ) ) {
$meta_key = CoursePress_Data_Course::get_last_seen_unit_meta_key( $course_id );
$meta_value = array(
'unit_id' => isset( $input['unit_id'] )? $input['unit_id'] : false,
'page' => isset( $input['page'] )? $input['page'] : 1,
);
update_user_meta( $student_id, $meta_key, $meta_value );
$next['url'] = CoursePress_Data_Course::get_course_url( $course_id );
}
$json_data = array(
'success' => true,
'html' => $html,
'url' => ! empty( $next['url'] ) ? $next['url'] : false,
'type' => $type,
'is_reload' => $reload,
);
$json_data = array( 'success' => true, 'data' => $json_data );
header('Content-type: text/plain');
echo json_encode( $json_data );
die();
//wp_send_json_success( $json_data );
} else {
$next_url = $next['url'];
wp_safe_redirect( $next_url ); exit;
}
}
}
}
if ( ! function_exists( 'wpmudev_override_module_submit' ) ) {
function wpmudev_override_module_submit(){
return WPMUDEV_Override_Module_Submit::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_override_module_submit', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.