Skip to content

Commit

Permalink
Adding recalculate feature see #6422
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Jul 15, 2013
1 parent 1af8062 commit 033cd8f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 55 deletions.
46 changes: 24 additions & 22 deletions main/exercice/exercise.class.php
Expand Up @@ -2866,7 +2866,8 @@ public function manageAnswers(
$saved_results = true,
$from_database = false,
$show_result = true,
$hotspot_delineation_result = array()
$hotspot_delineation_result = array(),
$updateResults = false
) {
global $feedback_type, $debug;
global $learnpath_id, $learnpath_item_id; //needed in order to use in the exercise_attempt() for the time
Expand Down Expand Up @@ -3014,7 +3015,6 @@ public function manageAnswers(
$answerComment = $objAnswerTmp->selectComment($answerId);
$answerCorrect = $objAnswerTmp->isCorrect($answerId);

//$answerIdFromList = $objAnswerTmp->getAnswerIdFromList($answerId);
$answerWeighting = (float)$objAnswerTmp->selectWeighting($answerId);

//$numAnswer = $objAnswerTmp->selectAutoId($answerId);
Expand Down Expand Up @@ -4288,24 +4288,24 @@ public function manageAnswers(
if ($final_answer == 0) {
$questionScore = 0;
}
saveQuestionAttempt($questionScore, 1, $quesId, $exeId, 0); // we always insert the answer_id 1 = delineation
saveQuestionAttempt($questionScore, 1, $quesId, $exeId, 0, null, $updateResults); // we always insert the answer_id 1 = delineation
//in delineation mode, get the answer from $hotspot_delineation_result[1]
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList(1), $hotspot_delineation_result[1], $exerciseResultCoordinates[$quesId]);
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList(1), $hotspot_delineation_result[1], $exerciseResultCoordinates[$quesId], null, $updateResults);
} else {
if ($final_answer == 0) {
$questionScore = 0;
$answer = 0;
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, null, $updateResults);
if (is_array($exerciseResultCoordinates[$quesId])) {
foreach ($exerciseResultCoordinates[$quesId] as $idx => $val) {
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), 0, $val);
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), 0, $val, $updateResults);
}
}
} else {
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, null, $updateResults);
if (is_array($exerciseResultCoordinates[$quesId])) {
foreach ($exerciseResultCoordinates[$quesId] as $idx => $val) {
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), $choice[$idx], $val);
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), $choice[$idx], $val, $updateResults);
}
}
}
Expand Down Expand Up @@ -4365,13 +4365,13 @@ public function manageAnswers(
$reply = array_keys($choice);
for ($i = 0; $i < sizeof($reply); $i++) {
$ans = $reply[$i];
saveQuestionAttempt($questionScore, $ans.':'.$choice[$ans], $quesId, $exeId, $i, $this->id);
saveQuestionAttempt($questionScore, $ans.':'.$choice[$ans], $quesId, $exeId, $i, $this->id, $updateResults);
if ($debug) {
error_log('result =>'.$questionScore.' '.$ans.':'.$choice[$ans]);
}
}
} else {
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id, $updateResults);
}
} elseif ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
if ($choice != 0) {
Expand All @@ -4382,46 +4382,46 @@ public function manageAnswers(
}
for ($i = 0; $i < sizeof($reply); $i++) {
$ans = $reply[$i];
saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id);
saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id, $updateResults);
}
} else {
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id, $updateResults);
}
} elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
if ($choice != 0) {
$reply = array_keys($choice);
for ($i = 0; $i < sizeof($reply); $i++) {
$ans = $reply[$i];
saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id);
saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id, $updateResults);
}
} else {
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id, $updateResults);
}
} elseif ($answerType == MATCHING || $answerType == DRAGGABLE) {
if (isset($matching)) {
foreach ($matching as $j => $val) {
saveQuestionAttempt($questionScore, $val, $quesId, $exeId, $j, $this->id);
saveQuestionAttempt($questionScore, $val, $quesId, $exeId, $j, $this->id, $updateResults);
}
}
} elseif ($answerType == FREE_ANSWER) {
$answer = $choice;
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $updateResults);
} elseif ($answerType == ORAL_EXPRESSION) {
$answer = $choice;
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $nano);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $updateResults, $nano);
} elseif ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_IMAGE || $answerType == UNIQUE_ANSWER_NO_OPTION) {
$answer = $choice;
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $updateResults);
// } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
} elseif ($answerType == HOT_SPOT) {
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $updateResults);
if (isset($exerciseResultCoordinates[$questionId]) && !empty($exerciseResultCoordinates[$questionId])) {
foreach ($exerciseResultCoordinates[$questionId] as $idx => $val) {
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), $choice[$idx], $val, $this->id);
saveExerciseAttemptHotspot($exeId, $quesId, $objAnswerTmp->getRealAnswerIdFromList($idx), $choice[$idx], $val, $updateResults, $this->id);
}
}
} else {
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id, $updateResults);
}
}

Expand Down Expand Up @@ -5506,7 +5506,9 @@ public function returnEndButtonHTML()
$html = Display::url(get_lang('ReturnToExerciseList'), api_get_path(WEB_CODE_PATH).'exercice/exercice.php?'.api_get_cidreq(), array('class' => 'btn btn-large'));
break;
case '2':
$html = Display::url(get_lang('Logout'), api_get_path(WEB_PUBLIC_PATH).'logout', array('class' => 'btn btn-large'));
global $app;
$url = $app['url_generator']->generate('admin_logout');
$html = Display::url(get_lang('Logout'), $url, array('class' => 'btn btn-large'));
break;
}
return $html;
Expand Down
3 changes: 1 addition & 2 deletions main/exercice/exercise_report.php
Expand Up @@ -28,8 +28,6 @@
// Access control
api_protect_course_script(true, false, true);



// including additional libraries
require_once 'exercise.class.php';
require_once 'question.class.php';
Expand Down Expand Up @@ -266,6 +264,7 @@
$actions .= '<a href="stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('statistics.png',get_lang('ReportByQuestion'), '', ICON_SIZE_MEDIUM).'</a>';
$actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&hotpotato_name='.$path.'&exerciseId='.intval($_GET['exerciseId']).'" >'.
Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>';
$actions .= '<a href="recalculate_scores.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('history.png',get_lang('RecalculateResults'), '', ICON_SIZE_MEDIUM).'</a>';
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions main/exercice/recalculate_scores.php
@@ -0,0 +1,30 @@
<?php

$exerciseId = $app['request']->get('exerciseId');

$exercise = new Exercise();
$exercise->read($exerciseId);
$examResults = get_all_exercise_results($exerciseId, api_get_course_int_id(), api_get_session_id(), true);

foreach ($examResults as $exerciseAttempt) {
$exeId = $exerciseAttempt['exe_id'];
$questionList = $exerciseAttempt['question_list'];

// Cleaning total result in order to recalculate:
$stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
$sql = 'UPDATE '.$stat_table.' SET exe_result = 0, exe_weighting = 0 WHERE exe_id = '.$exeId;
Database::query($sql);

$totalWeight = 0;
foreach ($questionList as $questionId => $questionAttempt) {
$result = $exercise->manageAnswers($exeId, $questionId, $questionAttempt['answer'], 'exercise_show', array(), true, true, false, array(), true);
$totalWeight += floatval($result['weight']);
}

$sql = 'UPDATE '.$stat_table.' SET exe_weighting = '.$totalWeight.' WHERE exe_id = '.$exeId;
Database::query($sql);
}

$urlMainExercise = api_get_path(WEB_CODE_PATH).'exercice/';
header('Location: '.$urlMainExercise.'exercise_report.php?exerciseId='.$exerciseId.'&'.api_get_cidreq());
exit;
112 changes: 81 additions & 31 deletions main/inc/lib/events.lib.inc.php
Expand Up @@ -431,6 +431,7 @@ function createEventExercise($exo_id)
* @param integer Question ID
* @param integer Exercise attempt ID a.k.a exe_id (from track_e_exercise)
* @param integer Position
* @param bool update results?
* @param integer Exercise ID
* @param string Filename (for audio answers - using nanogong)
* @param integer User ID The user who's going to get this score. Default value of null means "get from context".
Expand All @@ -447,6 +448,7 @@ function saveQuestionAttempt(
$exe_id,
$position,
$exercise_id = 0,
$updateResults = false,
$nano = null,
$user_id = null,
$course_id = null,
Expand Down Expand Up @@ -539,35 +541,63 @@ function saveQuestionAttempt(
if ($debug) {
error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
}
//The attempt already exist do not update use update_event_exercise() instead
return false;
if ($updateResults == false) {
// The attempt already exist do not update use update_event_exercise() instead
return false;
}
} else {
$attempt['exe_id'] = $exe_id;
}

if ($debug) {
error_log("updateResults : $updateResults");
error_log("Saving question attempt: ");
error_log($sql);
}

$attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);
$recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);

if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
$recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
if ($debug) {
error_log("Saving e attempt recording ");
}
$attempt_recording = array(
'exe_id' => $attempt_id,
'question_id' => $question_id,
'marks' => $score,
'insert_date' => $now,
'author' => '',
'session_id' => $session_id,
);
Database::insert($recording_table, $attempt_recording);
if ($updateResults == false) {
$attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);

if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {

if ($debug) {
error_log("Saving e attempt recording ");
}
$attempt_recording = array(
'exe_id' => $attempt_id,
'question_id' => $question_id,
'marks' => $score,
'insert_date' => $now,
'author' => '',
'session_id' => $session_id,
);
Database::insert($recording_table, $attempt_recording);
}
} else {
Database::update($TBL_TRACK_ATTEMPT, $attempt,
array('exe_id = ? AND question_id = ? AND user_id = ? ' => array($exe_id, $question_id, $user_id)));

if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {

$attempt_recording = array(
'exe_id' => $exe_id,
'question_id' => $question_id,
'marks' => $score,
'insert_date' => $now,
'author' => '',
'session_id' => $session_id,
);

Database::update($recording_table, $attempt_recording,
array('exe_id = ? AND question_id = ? AND session_id = ? ' => array($exe_id, $question_id, $session_id))
);
}
$attempt_id = $exe_id;
}


return $attempt_id;
} else {
return false;
Expand All @@ -581,28 +611,48 @@ function saveQuestionAttempt(
* @param int Answer ID
* @param int Whether this answer is correct (1) or not (0)
* @param string Coordinates of this point (e.g. 123;324)
* @param bool update results?
* @return boolean Result of the insert query
* @uses Course code and user_id from global scope $_cid and $_user
*/
function saveExerciseAttemptHotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0)
function saveExerciseAttemptHotspot($exe_id, $question_id, $answer_id, $correct, $coords, $updateResults = false, $exerciseId = 0)
{
global $safe_lp_id, $safe_lp_item_id;

//Validation in case of fraud with actived control time
if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
$correct = 0;
if ($updateResults == false) {
// Validation in case of fraud with activated control time
if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
$correct = 0;
}
}

$tbl_track_e_hotspot = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
$sql = "INSERT INTO $tbl_track_e_hotspot (hotspot_user_id, c_id, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".
" VALUES ('".api_get_user_id()."',".
" '".api_get_course_int_id()."', ".
" '".Database :: escape_string($exe_id)."', ".
" '".Database :: escape_string($question_id)."',".
" '".Database :: escape_string($answer_id)."',".
" '".Database :: escape_string($correct)."',".
" '".Database :: escape_string($coords)."')";
return $result = Database::query($sql);
if ($updateResults) {
$params = array(
'hotspot_correct' => $correct,
'hotspot_coordinate' => $coords
);
Database::update($tbl_track_e_hotspot, $params,
array('hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => array(
api_get_user_id(),
$exe_id,
$question_id,
$answer_id,
$answer_id

)));

} else {

$sql = "INSERT INTO $tbl_track_e_hotspot (hotspot_user_id, c_id, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".
" VALUES ('".api_get_user_id()."',".
" '".api_get_course_int_id()."', ".
" '".Database :: escape_string($exe_id)."', ".
" '".Database :: escape_string($question_id)."',".
" '".Database :: escape_string($answer_id)."',".
" '".Database :: escape_string($correct)."',".
" '".Database :: escape_string($coords)."')";
return $result = Database::query($sql);
}
}

/**
Expand Down

0 comments on commit 033cd8f

Please sign in to comment.