Julio Montoya преди 11 години
родител
ревизия
44c603d945
променени са 3 файла, в които са добавени 189 реда и са изтрити 158 реда
  1. 0 130
      main/exercice/exercise_report.php
  2. 45 10
      main/exercice/exercise_show.php
  3. 144 18
      main/inc/ajax/exercise.ajax.php

+ 0 - 130
main/exercice/exercise_report.php

@@ -113,136 +113,6 @@ if (!empty($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') {
     }
 }
 
-//Send student email @todo move this code in a class, library
-if (isset($_REQUEST['comments']) && $_REQUEST['comments'] == 'update' && ($is_allowedToEdit || $is_tutor) && $_GET['exeid'] == strval(intval($_GET['exeid']))) {
-    $id = intval($_GET['exeid']); //filtered by post-condition
-    $track_exercise_info = ExerciseLib::get_exercise_track_exercise_info($id);
-    if (empty($track_exercise_info)) {
-        api_not_allowed();
-    }
-    $test = $track_exercise_info['title'];
-    $student_id = $track_exercise_info['exe_user_id'];
-    $session_id = $track_exercise_info['session_id'];
-    $lp_id = $track_exercise_info['orig_lp_id'];
-    //$lp_item_id        = $track_exercise_info['orig_lp_item_id'];
-    $lp_item_view_id = $track_exercise_info['orig_lp_item_view_id'];
-
-    $course_info = api_get_course_info();
-
-    // Teacher data
-    $teacher_info = api_get_user_info(api_get_user_id());
-    $from_name = api_get_person_name(
-        $teacher_info['firstname'],
-        $teacher_info['lastname'],
-        null,
-        PERSON_NAME_EMAIL_ADDRESS
-    );
-    $url = api_get_path(WEB_CODE_PATH).'exercice/result.php?id='.$track_exercise_info['exe_id'].'&'.api_get_cidreq(
-    ).'&show_headers=1&id_session='.$session_id;
-
-    $my_post_info = array();
-    $post_content_id = array();
-    $comments_exist = false;
-
-    foreach ($_POST as $key_index => $key_value) {
-        $my_post_info = explode('_', $key_index);
-        $post_content_id[] = $my_post_info[1];
-        if ($my_post_info[0] == 'comments') {
-            $comments_exist = true;
-        }
-    }
-
-    $loop_in_track = ($comments_exist === true) ? (count($_POST) / 2) : count($_POST);
-
-    $array_content_id_exe = array();
-
-    if ($comments_exist === true) {
-        $array_content_id_exe = array_slice($post_content_id, $loop_in_track);
-    } else {
-        $array_content_id_exe = $post_content_id;
-    }
-
-    for ($i = 0; $i < $loop_in_track; $i++) {
-        $my_marks = null;
-        if (isset($_POST['marks_'.$array_content_id_exe[$i]])) {
-            $my_marks = Database::escape_string($_POST['marks_'.$array_content_id_exe[$i]]);
-        }
-
-        $contain_comments = null;
-        if (isset($_POST['comments_'.$array_content_id_exe[$i]])) {
-            $contain_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
-        }
-
-        if (isset($contain_comments)) {
-            $my_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
-        } else {
-            $my_comments = '';
-        }
-        $my_questionid = intval($array_content_id_exe[$i]);
-        $sql = "SELECT question from $TBL_QUESTIONS WHERE c_id = $course_id AND iid = '$my_questionid'";
-
-        $result = Database::query($sql);
-        Database::result($result, 0, "question");
-
-        $query = "UPDATE $TBL_TRACK_ATTEMPT SET marks = '$my_marks', teacher_comment = '$my_comments'
-                  WHERE question_id = ".$my_questionid." AND exe_id=".$id;
-        Database::query($query);
-
-        //Saving results in the track recording table
-        $recording_changes = 'INSERT INTO '.$TBL_TRACK_ATTEMPT_RECORDING.' (exe_id, question_id, marks, insert_date, author, teacher_comment)
-                              VALUES ('."'$id','".$my_questionid."','$my_marks','".api_get_utc_datetime()."','".api_get_user_id()."'".',"'.$my_comments.'")';
-        Database::query($recording_changes);
-    }
-
-    $qry = 'SELECT DISTINCT question_id, marks FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$id.' GROUP BY question_id';
-    $res = Database::query($qry);
-    $tot = 0;
-    while ($row = Database :: fetch_array($res, 'ASSOC')) {
-        $tot += $row['marks'];
-    }
-
-    $totquery = "UPDATE $TBL_TRACK_EXERCICES SET exe_result = '".floatval($tot)."' WHERE exe_id = ".$id;
-    Database::query($totquery);
-
-    if (isset($_POST['send_notification'])) {
-        //@todo move this somewhere else
-        $subject = get_lang('ExamSheetVCC');
-
-        $message = '<p>'.get_lang('DearStudentEmailIntroduction').'</p><p>'.get_lang('AttemptVCC');
-        $message .= '<h3>'.get_lang('CourseName').'</h3><p>'.Security::remove_XSS($course_info['name']).'';
-        $message .= '<h3>'.get_lang('Exercise').'</h3><p>'.Security::remove_XSS($test);
-
-        //Only for exercises not in a LP
-        if ($lp_id == 0) {
-            $message .= '<p>'.get_lang('ClickLinkToViewComment').' <a href="#url#">#url#</a><br />';
-        }
-
-        $message .= '<p>'.get_lang('Regards').'</p>';
-        $message .= $from_name;
-        $message = str_replace("#test#", Security::remove_XSS($test), $message);
-        $message = str_replace("#url#", $url, $message);
-        MessageManager::send_message_simple($student_id, $subject, $message, api_get_user_id());
-    }
-
-    // Updating LP score here
-    if (in_array($origin, array('tracking_course', 'user_course', 'correct_exercise_in_lp'))) {
-        $sql_update_score = "UPDATE $TBL_LP_ITEM_VIEW SET score = '".floatval(
-            $tot
-        )."' WHERE c_id = ".$course_id." AND id = ".$lp_item_view_id;
-        Database::query($sql_update_score);
-        if ($origin == 'tracking_course') {
-            //Redirect to the course detail in lp
-            header('Location: '.$urlMainExercise.'exercice.php?course='.Security :: remove_XSS($_GET['course']));
-            exit;
-        } else {
-            // Redirect to the reporting
-            header(
-                'Location: '.api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?origin='.$origin.'&student='.$student_id.'&details=true&course='.$course_id.'&session_id='.$session_id
-            );
-            exit;
-        }
-    }
-}
 
 $actions = null;
 if (isset($origin) && $origin == 'learnpath') {

+ 45 - 10
main/exercice/exercise_show.php

@@ -134,10 +134,15 @@ function showfck(sid,marksid) {
 	document.getElementById(comment).style.display='none';
 }
 
-function getFCK(vals,marksid) {
+$(function() {
+    $("#myform").submit(function() {
+        $("#result_from_ajax").html('<?php echo Display::return_icon('loading1.gif'); ?>');
+
+        var vals = $("#vals").val();
+        var marksid = $("#marksid").val();
 	var f=document.getElementById('myform');
+        var m_id = marksid.split(',');
 
-	var m_id = marksid.split(',');
 	for(var i=0;i<m_id.length;i++){
 		var oHidn = document.createElement("input");
 		oHidn.type = "hidden";
@@ -152,11 +157,33 @@ function getFCK(vals,marksid) {
 		var oHidden = document.createElement("input");
 		oHidden.type = "hidden";
 		oHidden.name = "comments_"+ids[k];
-		oEditor = FCKeditorAPI.GetInstance(oHidden.name) ;
-		oHidden.value = oEditor.GetXHTML(true);
+            //oEditor = FCKeditorAPI.GetInstance(oHidden.name) ;
+            var valueEditor = CKEDITOR.instances[oHidden.name].getData();
+            //console.log(oHidden.name);
+            oHidden.value = valueEditor;
 		f.appendChild(oHidden);
 	}
+        var params = $("#myform").serialize();
+
+        $.ajax({
+            type : "post",
+            url: "<?php echo api_get_path(WEB_AJAX_PATH); ?>exercise.ajax.php?a=correct_exercise_result",
+            data: "<?php ?>"+params,
+            success: function(data) {
+                if (data == 0) {
+                    $('#result_from_ajax').html('<?php echo addslashes(Display::return_message(get_lang('Error'), 'warning'))?>');
+                } else {
+                    $('#result_from_ajax').html('<?php echo addslashes(Display::return_message(get_lang('Saved'), 'success'))?>');
+                    $('.question_row').hide();
+                    $('#myform').hide();
+                    $('#correct_again').hide();
 }
+            }
+        });
+        return false;
+
+    });
+});
 </script>
 <?php
 $show_results           = true;
@@ -700,26 +727,34 @@ if (is_array($arrid) && is_array($arrmarks)) {
 }
 
 if ($is_allowedToEdit && $locked == false && !api_is_drh()) {
+    echo '<form name="myform" id="myform">';
 	if (in_array($origin, array('tracking_course','user_course','correct_exercise_in_lp'))) {
-		echo '<form name="myform" id="myform" action="'.$urlMainExercise.'exercise_report.php?exerciseId='.$exercise_id.'&filter=2&comments=update&exeid='.$id.'&origin='.$origin.'&details=true&course='.Security::remove_XSS($_GET['cidReq']).$fromlink.'" method="post">';
+        //'.$urlMainExercise.'exercise_report.php?exerciseId='.$exercise_id.'&filter=2&comments=update&exeid='.$id.'&origin='.$origin.'&details=true&course='.Security::remove_XSS($_GET['cidReq']).$fromlink.'
 		echo '<input type = "hidden" name="lp_item_id"       value="'.$learnpath_id.'">';
 		echo '<input type = "hidden" name="lp_item_view_id"  value="'.$lp_item_view_id.'">';
 		echo '<input type = "hidden" name="student_id"       value="'.$student_id.'">';
 		echo '<input type = "hidden" name="total_score"      value="'.$totalScore.'"> ';
 		echo '<input type = "hidden" name="my_exe_exo_id"    value="'.$exercise_id.'"> ';
 	} else {
-		echo ' <form name="myform" id="myform" action="'.$urlMainExercise.'exercise_report.php?exerciseId='.$exercise_id.'&filter=1&comments=update&exeid='.$id.'" method="post">';
+        //action="'.$urlMainExercise.'exercise_report.php?exerciseId='.$exercise_id.'&filter=1&comments=update&exeid='.$id.'" method="post"
 	}
+    echo '<input type = "hidden" name="origin"       value="'.$origin.'">';
+    echo '<input type = "hidden" name="exeid"       value="'.$id.'">';
+    echo '<input type = "hidden" name="comments"       value="update">';
+
+    echo '<input id="vals" type = "hidden" name="vals"       value="'.$strids.'">';
+    echo '<input id="marksid" type = "hidden" name="marksid"       value="'.$marksid.'">';
 	if ($origin !='learnpath' && $origin!='student_progress') {
 
         echo '<label><input type= "checkbox" name="send_notification"> '.get_lang('SendEmail').'</label>';
 		?>
-		<button type="submit" class="btn btn-primary" value="<?php echo get_lang('Ok'); ?>" onclick="getFCK('<?php echo $strids; ?>','<?php echo $marksid; ?>');">
-            <?php echo get_lang('CorrectTest'); ?>
-        </button>
-		</form>
+        <input type="submit" class="btn btn-primary" value=" <?php echo get_lang('CorrectTest'); ?>">
 		<?php
 	}
+    echo '</form>
+        <div id="result_from_ajax">
+        </div>';
+    //echo '<div id="correct_again" style="display:none"><a href="'..'">'.get_lang('CorrectTest').'</div>';
 }
 
 //Came from lpstats in a lp

+ 144 - 18
main/inc/ajax/exercise.ajax.php

@@ -239,12 +239,13 @@ switch ($action) {
                 $h = floor($remaining/3600);
                 $m = floor(($remaining - ($h*3600))/60);
                 $s = ($remaining - ($h*3600) - ($m*60));
-                $array = array( $row['firstname'],
-                                $row['lastname'],
-                                api_format_date($row['start_date'], DATE_TIME_FORMAT_LONG).' ['.($h>0?$h.':':'').sprintf("%02d",$m).':'.sprintf("%02d",$s).']',
-                                $row['count_questions'],
-                                round($row['score']*100).'%'
-                               );
+                $array = array(
+                    $row['firstname'],
+                    $row['lastname'],
+                    api_format_date($row['start_date'], DATE_TIME_FORMAT_LONG).' ['.($h>0?$h.':':'').sprintf("%02d",$m).':'.sprintf("%02d",$s).']',
+                    $row['count_questions'],
+                    round($row['score']*100).'%'
+                );
                 $response->rows[$i]['cell'] = $array;
                 $i++;
             }
@@ -333,6 +334,7 @@ switch ($action) {
             }
 
             // Exercise information.
+            /** @var \Exercise $objExercise */
             $objExercise             = isset($_SESSION['objExercise']) ? $_SESSION['objExercise'] : null;
 
             // Question info.
@@ -355,15 +357,15 @@ switch ($action) {
             // First time here we create an attempt (getting the exe_id).
             if (empty($exercise_stat_info)) {
             } else {
-                //We know the user we get the exe_id
+                // We know the user we get the exe_id.
                 $exe_id        = $exercise_stat_info['exe_id'];
                 $total_score   = $exercise_stat_info['exe_result'];
 
-                //Getting the list of attempts
+                // Getting the list of attempts.
                 $attempt_list  = getAllExerciseEventByExeId($exe_id);
             }
 
-            // Updating Reminder algorythm.
+            // Updating Reminder algorythme.
             if ($objExercise->type == ONE_PER_PAGE) {
                 $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
 
@@ -399,7 +401,7 @@ switch ($action) {
                 }
             }
 
-            // No exe id? Can't save answer.
+            // No exe id? Can't save answer!
             if (empty($exe_id)) {
                 // Fires an error.
                 echo 'error';
@@ -433,11 +435,13 @@ switch ($action) {
                     continue;
                 }
 
-                if ($debug) error_log("Saving question_id = $my_question_id ");
 
                 $my_choice = isset($choice[$my_question_id]) ? $choice[$my_question_id] : null;
 
-                if ($debug) error_log("my_choice = ".print_r($my_choice, 1)."");
+                if ($debug) {
+                    error_log("Saving question_id = $my_question_id ");
+                    error_log("my_choice = ".print_r($my_choice, 1)."");
+                }
 
                // creates a temporary Question object
             	$objQuestionTmp = Question::read($my_question_id, $course_id);
@@ -451,14 +455,14 @@ switch ($action) {
                     $total_weight += $objQuestionTmp->selectWeighting();
                 }
 
-            	// This variable commes from exercise_submit_modal.php
+                // This variable came from exercise_submit_modal.php
                 $hotspot_delineation_result = null;
                 if (isset($_SESSION['hotspot_delineation_result']) && isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])) {
             	    $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
                 }
 
                 if ($type == 'simple') {
-                    //Getting old attempt in order to decrees the total score
+                    // Getting old attempt in order to decrees the total score.
                     $old_result = $objExercise->manageAnswers(
                         $exe_id,
                         $my_question_id,
@@ -470,13 +474,19 @@ switch ($action) {
                         false
                     );
 
-                    //Removing old score
+                    // Removing old score.
                     $total_score = $total_score - $old_result['score'];
+                    if ($debug) {
+                        error_log("old score = ".$old_result['score']);
+                        error_log("total_score = ".$total_score."");
+                    }
                 }
 
                 // Deleting old attempt
                 if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {
-                    if ($debug) error_log("delete_attempt  exe_id : $exe_id, my_question_id: $my_question_id");
+                    if ($debug) {
+                        error_log("delete_attempt  exe_id : $exe_id, my_question_id: $my_question_id");
+                    }
                     delete_attempt($exe_id, api_get_user_id(), $course_id, $session_id, $my_question_id);
                     if ($objQuestionTmp->type  == HOT_SPOT) {
             	        delete_attempt_hotspot($exe_id, api_get_user_id(), $course_id, $my_question_id);
@@ -503,8 +513,10 @@ switch ($action) {
                 //Adding the new score
                 $total_score += $result['score'];
 
-                if ($debug) error_log("total_score: $total_score ");
-                if ($debug) error_log("total_weight: $total_weight ");
+                if ($debug) {
+                    error_log("total_score: $total_score ");
+                    error_log("total_weight: $total_weight ");
+                }
 
                 $duration = 0;
                 $now = time();
@@ -587,7 +599,121 @@ switch ($action) {
         }
         echo 'ok';
         break;
+    case 'correct_exercise_result':
+
+        $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_drh();
+        $is_tutor = api_is_allowed_to_edit(true);
+
+        //Send student email @todo move this code in a class, library
+        if (isset($_POST['comments']) && $_POST['comments'] == 'update' && ($is_allowedToEdit || $is_tutor) && $_POST['exeid'] == strval(intval($_POST['exeid']))) {
+            $exeId = intval($_POST['exeid']);
+
+            $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
+            $TBL_TRACK_EXERCICES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
+            $TBL_TRACK_ATTEMPT = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
+            $TBL_TRACK_ATTEMPT_RECORDING = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
+            $TBL_LP_ITEM_VIEW = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
+
+            $track_exercise_info = ExerciseLib::get_exercise_track_exercise_info($exeId);
+
+            if (empty($track_exercise_info)) {
+                echo 0;
+            }
+
+            $test = $track_exercise_info['title'];
+            $student_id = $track_exercise_info['exe_user_id'];
+            $session_id = $track_exercise_info['session_id'];
+            $lp_id = $track_exercise_info['orig_lp_id'];
+            //$lp_item_id        = $track_exercise_info['orig_lp_item_id'];
+            $lp_item_view_id = $track_exercise_info['orig_lp_item_view_id'];
+
+            $course_info = api_get_course_info();
+
+            // Teacher data
+            $teacher_info = api_get_user_info(api_get_user_id());
+
+            $from_name = api_get_person_name(
+                $teacher_info['firstname'],
+                $teacher_info['lastname'],
+                null,
+                PERSON_NAME_EMAIL_ADDRESS
+            );
 
+            $url = api_get_path(WEB_CODE_PATH).'exercice/result.php?id='.$track_exercise_info['exe_id'].'&'.api_get_cidreq().'&show_headers=1&id_session='.$session_id;
+
+            $commentIds = array();
+            $marks = array();
+            $comments = array();
+
+            $result = array();
+            $values = explode(',', $_POST['vals']);
+            if (empty($values)) {
+                echo 0;
+                exit;
+            }
+
+            $countComments = count($commentIds);
+
+            foreach ($values as $questionId) {
+                $questionId = intval($questionId);
+                $comment = isset($_POST['comments_'.$questionId]) ? $_POST['comments_'.$questionId] : null;
+                $mark = isset($_POST['marks_'.$questionId]) ? $_POST['marks_'.$questionId] : null;
+
+                $mark = Database::escape_string($mark);
+                $comment = Database::escape_string($comment);
+
+                $query = "UPDATE $TBL_TRACK_ATTEMPT SET marks = '$mark', teacher_comment = '$comment'
+                          WHERE question_id = ".$questionId." AND exe_id=".$exeId;
+                Database::query($query);
+
+                //Saving results in the track recording table
+                $recording_changes = 'INSERT INTO '.$TBL_TRACK_ATTEMPT_RECORDING.' (exe_id, question_id, marks, insert_date, author, teacher_comment)
+                                      VALUES ('."'$exeId','".$questionId."','$mark','".api_get_utc_datetime()."','".api_get_user_id()."'".',"'.$comment.'")';
+                Database::query($recording_changes);
+            }
+
+            $qry = 'SELECT DISTINCT question_id, marks FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$exeId.' GROUP BY question_id';
+            $res = Database::query($qry);
+            $tot = 0;
+            while ($row = Database :: fetch_array($res, 'ASSOC')) {
+                $tot += $row['marks'];
+            }
+
+            $sql = "UPDATE $TBL_TRACK_EXERCICES SET exe_result = '".floatval($tot)."' WHERE exe_id = ".$exeId;
+            Database::query($sql);
+
+            if (isset($_POST['send_notification'])) {
+                //@todo move this somewhere else
+                $subject = get_lang('ExamSheetVCC');
+
+                $message = '<p>'.get_lang('DearStudentEmailIntroduction').'</p><p>'.get_lang('AttemptVCC');
+                $message .= '<h3>'.get_lang('CourseName').'</h3><p>'.Security::remove_XSS($course_info['name']).'';
+                $message .= '<h3>'.get_lang('Exercise').'</h3><p>'.Security::remove_XSS($test);
+
+                //Only for exercises not in a LP
+                if ($lp_id == 0) {
+                    $message .= '<p>'.get_lang('ClickLinkToViewComment').' <a href="#url#">#url#</a><br />';
+                }
+
+                $message .= '<p>'.get_lang('Regards').'</p>';
+                $message .= $from_name;
+                $message = str_replace("#test#", Security::remove_XSS($test), $message);
+                $message = str_replace("#url#", $url, $message);
+                MessageManager::send_message_simple($student_id, $subject, $message, api_get_user_id());
+            }
+
+            $origin = $_POST['origin'];
+
+            // Updating LP score here
+            if (isset($origin) && in_array($origin, array('tracking_course', 'user_course', 'correct_exercise_in_lp'))) {
+                $sql_update_score = "UPDATE $TBL_LP_ITEM_VIEW SET score = '".floatval($tot)."' WHERE c_id = ".$course_id." AND id = ".$lp_item_view_id;
+                Database::query($sql_update_score);
+            }
+            echo 1;
+            exit;
+        }
+        echo 0;
+        break;
     default:
         echo '';
 }