Ejemplo n.º 1
0
 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);
                 }
             }
         }
     }
 }