function save_module_data()
    {
        global $wpdb, $last_inserted_unit_id, $save_elements;
        if (isset($_POST['module_type']) && $save_elements == true) {
            foreach (array_keys($_POST['module_type']) as $module_type => $module_value) {
                if ($module_value == $this->name) {
                    $data = new stdClass();
                    $data->ID = '';
                    $data->unit_id = '';
                    $data->title = '';
                    $data->excerpt = '';
                    $data->content = '';
                    $data->metas = array();
                    $data->metas['module_type'] = $this->name;
                    $data->post_type = 'module';
                    if (isset($_POST[$this->name . '_id'])) {
                        foreach ($_POST[$this->name . '_id'] as $key => $value) {
                            $data->ID = $_POST[$this->name . '_id'][$key];
                            $data->unit_id = (isset($_POST['unit_id']) and isset($_POST['unit']) && $_POST['unit'] != '') ? $_POST['unit_id'] : $last_inserted_unit_id;
                            $data->title = $_POST[$this->name . '_title'][$key];
                            $data->content = $_POST[$this->name . '_content'][$key];
                            $data->metas['module_order'] = $_POST[$this->name . '_module_order'][$key];
                            $data->metas['module_page'] = $_POST[$this->name . '_module_page'][$key];
                            $data->metas['limit_attempts_value'] = $_POST[$this->name . '_limit_attempts_value'][$key];
                            $data->metas['minimum_grade_required'] = $_POST[$this->name . '_minimum_grade_required'][$key];
                            // if ( isset($_POST[$this->name . '_show_title_on_front'][$key]) ) {
                            //     $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_on_front'][$key];
                            // } else {
                            //     $data->metas['show_title_on_front'] = 'no';
                            // }
                            $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_field'][$key];
                            // if ( isset($_POST[$this->name . '_mandatory_answer'][$key]) ) {
                            //     $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer'][$key];
                            // } else {
                            //     $data->metas['mandatory_answer'] = 'no';
                            // }
                            $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer_field'][$key];
                            // if ( isset($_POST[$this->name . '_gradable_answer'][$key]) ) {
                            //     $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer'][$key];
                            // } else {
                            //     $data->metas['gradable_answer'] = 'no';
                            // }
                            $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer_field'][$key];
                            // if ( isset($_POST[$this->name . '_limit_attempts'][$key]) ) {
                            //     $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts'][$key];
                            // } else {
                            //     $data->metas['limit_attempts'] = 'no';
                            // }
                            $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts_field'][$key];
                            $data->metas['time_estimation'] = $_POST[$this->name . '_time_estimation'][$key];
                            parent::update_module($data);
                        }
                    }
                }
            }
        }
        if (isset($_POST['submit_modules_data_save']) || isset($_POST['submit_modules_data_done']) || isset($_POST['save_student_progress_indication'])) {
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    $response_id = intval(str_replace($this->name . '_front_', '', $file));
                    if (!function_exists('wp_handle_upload')) {
                        require_once ABSPATH . 'wp-includes/pluggable.php';
                        require_once ABSPATH . 'wp-admin/includes/file.php';
                    }
                    $uploadedfile = $_FILES[$file];
                    $upload_overrides = array('test_form' => false);
                    $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
                    if ($movefile) {
                        //var_dump( $movefile );
                        if (!isset($movefile['error'])) {
                            $filename = $movefile['file'];
                            $wp_upload_dir = wp_upload_dir();
                            $attachment = array('guid' => $movefile['url'], 'post_mime_type' => $movefile['type'], 'post_title' => basename($movefile['url']), 'post_content' => '', 'post_status' => 'inherit');
                            $attach_id = wp_insert_attachment($attachment, $filename, $response_id);
                            $post_ancestors = get_post_ancestors($response_id);
                            $unit_id = $post_ancestors[0];
                            $course_id = get_post_meta($unit_id, 'course_id', true);
                            update_post_meta($attach_id, 'user_ID', get_current_user_ID());
                            update_post_meta($attach_id, 'course_id', $course_id);
                            // Record mandatory question answered
                            $mandatory_answer = get_post_meta($response_id, 'mandatory_answer', true);
                            if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                                Student_Completion::record_mandatory_answer(get_current_user_id(), $course_id, $unit_id, $response_id);
                            }
                        } else {
                            ?>
							<p class="form-info-red"><?php 
                            echo $movefile['error'];
                            ?>
</p>
						<?php 
                        }
                    } else {
                    }
                }
            }
        }
    }
 function save_module_data()
 {
     global $wpdb, $last_inserted_unit_id, $save_elements;
     if (isset($_POST['module_type']) && $save_elements == true) {
         $answers = array();
         if (isset($_POST[$this->name . '_radio_answers'])) {
             foreach ($_POST[$this->name . '_radio_answers'] as $post_answers) {
                 $answers[] = $post_answers;
             }
             foreach (array_keys($_POST['module_type']) as $module_type => $module_value) {
                 if ($module_value == $this->name) {
                     $data = new stdClass();
                     $data->ID = '';
                     $data->unit_id = '';
                     $data->title = '';
                     $data->excerpt = '';
                     $data->content = '';
                     $data->metas = array();
                     $data->metas['module_type'] = $this->name;
                     $data->post_type = 'module';
                     foreach ($_POST[$this->name . '_id'] as $key => $value) {
                         //cp_write_log( $key );
                         $data->ID = $_POST[$this->name . '_id'][$key];
                         $data->unit_id = (isset($_POST['unit_id']) and isset($_POST['unit']) && $_POST['unit'] != '') ? $_POST['unit_id'] : $last_inserted_unit_id;
                         $data->title = $_POST[$this->name . '_title'][$key];
                         $data->content = $_POST[$this->name . '_content'][$key];
                         $data->metas['module_order'] = $_POST[$this->name . '_module_order'][$key];
                         $data->metas['module_page'] = $_POST[$this->name . '_module_page'][$key];
                         $data->metas['checked_answer'] = $_POST[$this->name . '_checked_index'][$key];
                         $data->metas['answers'] = $answers[$key];
                         $data->metas['time_estimation'] = $_POST[$this->name . '_time_estimation'][$key];
                         $data->metas['limit_attempts_value'] = $_POST[$this->name . '_limit_attempts_value'][$key];
                         $data->metas['minimum_grade_required'] = $_POST[$this->name . '_minimum_grade_required'][$key];
                         // if ( isset($_POST[$this->name . '_show_title_on_front'][$key]) ) {
                         //     $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_on_front'][$key];
                         // } else {
                         //     $data->metas['show_title_on_front'] = 'no';
                         // }
                         $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_field'][$key];
                         // if ( isset($_POST[$this->name . '_mandatory_answer'][$key]) ) {
                         //     $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer'][$key];
                         // } else {
                         //     $data->metas['mandatory_answer'] = 'no';
                         // }
                         $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer_field'][$key];
                         // if ( isset($_POST[$this->name . '_gradable_answer'][$key]) ) {
                         //     $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer'][$key];
                         // } else {
                         //     $data->metas['gradable_answer'] = 'no';
                         // }
                         $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer_field'][$key];
                         // if ( isset($_POST[$this->name . '_limit_attempts'][$key]) ) {
                         //     $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts'][$key];
                         // } else {
                         //     $data->metas['limit_attempts'] = 'no';
                         // }
                         $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts_field'][$key];
                         parent::update_module($data);
                     }
                 }
             }
         }
     }
     if (isset($_POST['submit_modules_data_save']) || isset($_POST['submit_modules_data_done']) || isset($_POST['save_student_progress_indication'])) {
         foreach ($_POST as $response_name => $response_value) {
             if (preg_match('/' . $this->name . '_front_/', $response_name)) {
                 $response_id = intval(str_replace($this->name . '_front_', '', $response_name));
                 if ($response_value != '') {
                     $data = new stdClass();
                     $data->ID = '';
                     $data->title = '';
                     $data->excerpt = '';
                     $data->content = '';
                     $data->metas = array();
                     $data->metas['user_ID'] = get_current_user_id();
                     $data->post_type = 'module_response';
                     $data->response_id = $response_id;
                     $data->title = '';
                     //__( 'Response to '.$response_id.' module ( Unit '.$_POST['unit_id'].' )' );
                     $response_value = stripslashes($response_value);
                     $response_value = strip_tags($response_value);
                     $response_value = htmlentities($response_value);
                     $data->content = $response_value;
                     /* CHECK AND SET THE GRADE AUTOMATICALLY */
                     $checked_value = get_post_meta($response_id, 'checked_answer', true);
                     $checked_value = stripslashes($checked_value);
                     $checked_value = strip_tags($checked_value);
                     $checked_value = htmlentities($checked_value);
                     if ($data->content == $checked_value) {
                         $response_grade = 100;
                     } else {
                         $response_grade = 0;
                     }
                     $data->auto_grade = $response_grade;
                     // Record mandatory question answered
                     $mandatory_answer = get_post_meta($response_id, 'mandatory_answer', true);
                     $unit_id = (int) $_POST['unit_id'];
                     if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                         $course_id = get_post_meta($unit_id, 'course_id', true);
                         Student_Completion::record_mandatory_answer(get_current_user_id(), $course_id, $unit_id, $response_id);
                     }
                     $data->module_id = $response_id;
                     parent::update_module_response($data);
                 }
             }
         }
     }
 }
 public static function save_response_grade($response_id = '', $response_grade = '', $user_id = false, $course_id = false, $unit_id = false, $module_id = false)
 {
     if ((isset($_POST['response_id']) || $response_id !== '') && (isset($_POST['response_grade']) || $response_grade !== '')) {
         $grade_data = array('grade' => $response_grade !== '' && is_numeric($response_grade) ? $response_grade : $_POST['response_grade'], 'instructor' => get_current_user_ID(), 'time' => current_time('timestamp'));
         update_post_meta($response_id !== '' && is_numeric($response_id) ? $response_id : $_POST['response_id'], 'response_grade', $grade_data);
         if (!$user_id) {
             $user_id = isset($_POST['student_id']) ? (int) $_POST['student_id'] : false;
             $course_id = isset($_POST['course_id']) ? (int) $_POST['course_id'] : false;
             $unit_id = isset($_POST['unit_id']) ? (int) $_POST['unit_id'] : false;
             $module_id = isset($_POST['module_id']) ? (int) $_POST['module_id'] : false;
         }
         if (!empty($user_id) && !empty($course_id) && !empty($unit_id) && !empty($module_id)) {
             $mandatory_answer = get_post_meta($module_id, 'mandatory_answer', true);
             if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                 Student_Completion::record_mandatory_answer($user_id, $course_id, $unit_id, $module_id);
             }
             Student_Completion::record_gradable_result($user_id, $course_id, $unit_id, $module_id, floatval($grade_data['grade']));
         }
         return true;
     } else {
         return false;
     }
 }