Skip to content

Commit

Permalink
Exercises: Add new Ranking mode #2788
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Feb 7, 2019
1 parent 50a6f89 commit 8cbcfe9
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
8 changes: 8 additions & 0 deletions main/exercise/exercise.class.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1963,6 +1963,14 @@ public function createForm($form, $type = 'full')
'5', '5',
['id' => 'result_disabled_5', 'onclick' => 'check_results_disabled()'] ['id' => 'result_disabled_5', 'onclick' => 'check_results_disabled()']
); );
$radios_results_disabled[] = $form->createElement(
'radio',
'results_disabled',
null,
get_lang('ExerciseRankingMode'),
'6',
['id' => 'result_disabled_6']
);


$form->addGroup( $form->addGroup(
$radios_results_disabled, $radios_results_disabled,
Expand Down
22 changes: 20 additions & 2 deletions main/exercise/exercise.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1118,7 +1118,16 @@
// Exam is ready to be taken // Exam is ready to be taken
if ($is_actived_time) { if ($is_actived_time) {
// Show results 697 $attempt_text = get_lang('LatestAttempt').' : '; // Show results 697 $attempt_text = get_lang('LatestAttempt').' : ';
if ($my_result_disabled == 0 || $my_result_disabled == 2) { if (
in_array(
$my_result_disabled,
[
RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
RESULT_DISABLE_SHOW_SCORE_ONLY,
RESULT_DISABLE_RANKING,
]
)
) {
//More than one attempt //More than one attempt
if ($num > 0) { if ($num > 0) {
$row_track = Database :: fetch_array($qryres); $row_track = Database :: fetch_array($qryres);
Expand Down Expand Up @@ -1173,7 +1182,16 @@
} else { } else {
// Normal behaviour. // Normal behaviour.
// Show results. // Show results.
if ($my_result_disabled == 0 || $my_result_disabled == 2) { if (
in_array(
$my_result_disabled,
[
RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
RESULT_DISABLE_SHOW_SCORE_ONLY,
RESULT_DISABLE_RANKING,
]
)
) {
if ($num > 0) { if ($num > 0) {
$row_track = Database :: fetch_array($qryres); $row_track = Database :: fetch_array($qryres);
$attempt_text = get_lang('LatestAttempt').' : '; $attempt_text = get_lang('LatestAttempt').' : ';
Expand Down
3 changes: 3 additions & 0 deletions main/exercise/overview.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES, RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT, RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK, RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
RESULT_DISABLE_RANKING,
] ]
)) { )) {
$row['result'] = $score; $row['result'] = $score;
Expand All @@ -267,6 +268,7 @@
RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES, RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT, RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK, RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
RESULT_DISABLE_RANKING,
] ]
) || ( ) || (
$objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY && $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
Expand Down Expand Up @@ -324,6 +326,7 @@
break; break;
case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS: case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES: case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
case RESULT_DISABLE_RANKING:
$header_names = [ $header_names = [
get_lang('Attempt'), get_lang('Attempt'),
get_lang('StartDate'), get_lang('StartDate'),
Expand Down
1 change: 1 addition & 0 deletions main/inc/lib/api.lib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@
define('RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES', 3); //Show final score only with categories define('RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES', 3); //Show final score only with categories
define('RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT', 4); define('RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT', 4);
define('RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK', 5); define('RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK', 5);
define('RESULT_DISABLE_RANKING', 6);
// 4: Show final score only with categories and show expected answers only on the last attempt // 4: Show final score only with categories and show expected answers only on the last attempt


define('EXERCISE_MAX_NAME_SIZE', 80); define('EXERCISE_MAX_NAME_SIZE', 80);
Expand Down
128 changes: 128 additions & 0 deletions main/inc/lib/exercise.lib.php
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */


use Chamilo\CoreBundle\Entity\TrackEExercises;
use ChamiloSession as Session; use ChamiloSession as Session;


/** /**
Expand Down Expand Up @@ -4440,6 +4441,7 @@ public static function displayQuestionListByAttempt(
[ [
RESULT_DISABLE_SHOW_SCORE_ONLY, RESULT_DISABLE_SHOW_SCORE_ONLY,
RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES, RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
RESULT_DISABLE_RANKING,
] ]
) )
) { ) {
Expand Down Expand Up @@ -4835,6 +4837,132 @@ public static function displayQuestionListByAttempt(
); );
} }
} }

if (RESULT_DISABLE_RANKING == $objExercise->selectResultsDisabled()) {
echo Display::page_header(get_lang('Ranking'), null, 'h4');
echo self::displayResultsInRanking(
$objExercise->iId,
api_get_user_id(),
api_get_course_int_id(),
api_get_session_id()
);
}
}

/**
* Display the ranking of results in a exercise
*
* @param int $exerciseId
* @param int $currentUserId
* @param int $courseId
* @param int $sessionId
*
* @return string
*/
public static function displayResultsInRanking($exerciseId, $currentUserId, $courseId, $sessionId = 0)
{
$data = self::exerciseResultsInRanking($exerciseId, $courseId, $sessionId);

$table = new HTML_Table(['class' => 'table table-hover table-bordered']);
$table->setHeaderContents(0, 0, get_lang('Position'), ['class' => 'text-right']);
$table->setHeaderContents(0, 1, get_lang('Username'));
$table->setHeaderContents(0, 2, get_lang('Score'), ['class' => 'text-right']);
$table->setHeaderContents(0, 3, get_lang('Date'), ['class' => 'text-center']);

foreach ($data as $r => $item) {
$selected = $item[1]->getId() == $currentUserId;

foreach ($item as $c => $value) {
$table->setCellContents($r + 1, $c, $value);

$attrClass = '';

if (in_array($c, [0, 2])) {
$attrClass = 'text-right';
} elseif (3 == $c) {
$attrClass = 'text-center';
}

if ($selected) {
$attrClass .= ' warning';
}

$table->setCellAttributes($r + 1, $c, ['class' => $attrClass]);
}
}

return $table->toHtml();
}

/**
* Get the ranking for results in a exercise.
* Function used internally by ExerciseLib::displayResultsInRanking
*
* @param int $exerciseId
* @param int $courseId
* @param int $sessionId
*
* @return array
*/
public static function exerciseResultsInRanking($exerciseId, $courseId, $sessionId = 0)
{
$em = Database::getManager();

$dql = 'SELECT DISTINCT te.exeUserId FROM ChamiloCoreBundle:TrackEExercises te WHERE te.exeExoId = :id AND te.cId = :cId';
$dql .= api_get_session_condition($sessionId, true, false, 'te.sessionId');

$result = $em
->createQuery($dql)
->setParameters(['id' => $exerciseId, 'cId' => $courseId])
->getScalarResult();

$data = [];

/** @var TrackEExercises $item */
foreach ($result as $item) {
$bestAttemp = self::get_best_attempt_by_user($item['exeUserId'], $exerciseId, $courseId, $sessionId = 0);

$data[] = $bestAttemp;
}

usort(
$data,
function ($a, $b) {
if ($a['exe_result'] != $b['exe_result']) {
return $a['exe_result'] > $b['exe_result'] ? -1 : 1;
}

if ($a['exe_date'] != $b['exe_date']) {
return $a['exe_date'] > $b['exe_date'] ? -1 : 1;
}

return 0;
}
);

// flags to display the same position in case of tie
$lastScore = $data[0]['exe_result'];
$position = 1;

$data = array_map(
function ($item) use (&$lastScore, &$position) {
if ($item['exe_result'] < $lastScore) {
++$position;
}

$lastScore = $item['exe_result'];

return [
$position,
api_get_user_entity($item['exe_user_id']),
self::show_score($item['exe_result'], $item['exe_weighting'], true, true, true),
api_convert_and_format_date($item['exe_date'], DATE_TIME_FORMAT_SHORT),
];
},
$data
);

return $data;
} }


/** /**
Expand Down

0 comments on commit 8cbcfe9

Please sign in to comment.