Refactor options for configuration: exercise_finished_email_settings

See BT#17786
pull/3559/head
Julio Montoya 5 years ago
parent 7a64c4a4c5
commit 66f16b95d3
  1. 59
      main/exercise/exercise_result.php
  2. 59
      main/inc/lib/exercise.lib.php

@ -22,13 +22,7 @@ require_once __DIR__.'/../inc/global.inc.php';
$this_section = SECTION_COURSES;
/* ACCESS RIGHTS */
api_protect_course_script(true);
if ($debug) {
error_log('Entering exercise_result.php: '.print_r($_POST, 1));
}
$origin = api_get_origin();
/** @var Exercise $objExercise */
@ -89,6 +83,7 @@ $pageActions = '';
$pageTop = '';
$pageBottom = '';
$pageContent = '';
$courseInfo = api_get_course_info();
if (!in_array($origin, ['learnpath', 'embeddable', 'mobileapp'])) {
// So we are not in learnpath tool
@ -156,14 +151,14 @@ if ($origin !== 'embeddable') {
}
// We check if the user attempts before sending to the exercise_result.php
$attempt_count = Event::get_attempt_count(
api_get_user_id(),
$objExercise->id,
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
if ($objExercise->selectAttempts() > 0) {
$attempt_count = Event::get_attempt_count(
api_get_user_id(),
$objExercise->id,
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
if ($attempt_count >= $objExercise->selectAttempts()) {
Display::addFlash(
Display::return_message(
@ -213,7 +208,7 @@ $feedbackType = $objExercise->getFeedbackType();
ob_start();
// Display and save questions
ExerciseLib::displayQuestionListByAttempt(
$stats = ExerciseLib::displayQuestionListByAttempt(
$objExercise,
$exe_id,
$saveResults,
@ -230,18 +225,44 @@ if (!empty($learnpath_id) && $saveResults) {
$emailSettings = api_get_configuration_value('exercise_finished_email_settings');
if (!empty($emailSettings)) {
$subject = get_lang('ExerciseFinished');
$attemptCountToSend = $attempt_count++;
$subject = sprintf(get_lang('WrongAttemptXInCourseX'), $attemptCountToSend, $courseInfo['title']);
$wrongAnswersCount = $stats['failed_answers_count'];
if (0 === $wrongAnswersCount) {
$subject = sprintf(get_lang('ExerciseValidationInCourseX'), $courseInfo['title']);
}
$totalScore = ExerciseLib::show_score($total_score, $max_score, false, true);
$exerciseExtraFieldValue = new ExtraFieldValue('exercise');
$data = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable($objExercise->iId, 'MailAttemptX');
$content = '';
$userInfo = api_get_user_info();
if ($data && isset($data['value'])) {
$content = sprintf($data['value'], $attemptCountToSend);
$content = str_replace('((exercise_error_count))', $wrongAnswersCount, $content);
$content = AnnouncementManager::parseContent(
api_get_user_id(),
$content,
api_get_course_id(),
api_get_session_id()
);
if (0 !== $wrongAnswersCount) {
$content .= $stats['failed_answers_html'];
} else {
$content .= 'Exercise ok!';
}
}
if (isset($emailSettings['send_by_status']) && !empty($emailSettings['send_by_status'])) {
foreach ($emailSettings['send_by_status'] as $item) {
$type = $item['type'];
switch ($item['type']) {
case 'only_score':
$content = get_lang('YourScore')." $totalScore ";
//$content = get_lang('YourScore')." $totalScore ";
break;
case 'complete':
$content = $pageContent;
//$content = $pageContent;
break;
}
@ -256,14 +277,14 @@ if (!empty($emailSettings)) {
if (isset($emailSettings['send_by_email']) && !empty($emailSettings['send_by_email'])) {
foreach ($emailSettings['send_by_email'] as $item) {
$type = $item['type'];
switch ($item['type']) {
/*switch ($item['type']) {
case 'only_score':
$content = get_lang('YourScore')." $totalScore ";
break;
case 'complete':
$content = $pageContent;
break;
}
}*/
api_mail_html('', $item['email'], $subject, $content);
}
}

@ -4516,6 +4516,7 @@ EOT;
$showTotalScoreAndUserChoicesInLastAttempt = true;
$showTotalScore = true;
$showQuestionScore = true;
$attemptResult = [];
if (in_array(
$objExercise->results_disabled,
@ -4724,27 +4725,29 @@ EOT;
}
}
$scorePassed = $my_total_score >= $my_total_weight;
if (function_exists('bccomp')) {
$compareResult = bccomp($my_total_score, $my_total_weight, 3);
$scorePassed = $compareResult === 1 || $compareResult === 0;
}
$calculatedScore = [
'result' => self::show_score(
$my_total_score,
$my_total_weight,
false
),
'pass' => $scorePassed,
'score' => $my_total_score,
'weight' => $my_total_weight,
'comments' => $comnt,
'user_answered' => $result['user_answered'],
];
$score = [];
if ($show_results) {
$scorePassed = $my_total_score >= $my_total_weight;
if (function_exists('bccomp')) {
$compareResult = bccomp($my_total_score, $my_total_weight, 3);
$scorePassed = $compareResult === 1 || $compareResult === 0;
}
$score = [
'result' => self::show_score(
$my_total_score,
$my_total_weight,
false
),
'pass' => $scorePassed,
'score' => $my_total_score,
'weight' => $my_total_weight,
'comments' => $comnt,
'user_answered' => $result['user_answered'],
];
$score = $calculatedScore;
}
if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
$reviewScore = [
'score' => $my_total_score,
@ -4776,6 +4779,10 @@ EOT;
if ($show_results) {
$question_content .= '</div>';
}
$calculatedScore['question_content'] = $question_content;
$attemptResult[] = $calculatedScore;
if ($objExercise->showExpectedChoice()) {
$exercise_content .= Display::div(
Display::panel($question_content),
@ -4947,6 +4954,22 @@ EOT;
if (!empty($remainingMessage)) {
echo Display::return_message($remainingMessage, 'normal', false);
}
$failedAnswersCount = 0;
$wrongQuestionHtml = '';
foreach ($attemptResult as $item) {
if (false === $item['pass']) {
$failedAnswersCount++;
$wrongQuestionHtml .= $item['question_content'].'<br />';
}
}
return [
'attempts_result' => $attemptResult,
'total_answers_count' => count($attemptResult),
'failed_answers_count' => $failedAnswersCount,
'failed_answers_html' => $wrongQuestionHtml,
];
}
/**

Loading…
Cancel
Save