diff --git a/main/inc/ajax/lp.ajax.php b/main/inc/ajax/lp.ajax.php index d94bdd7511..a9bb93d8eb 100755 --- a/main/inc/ajax/lp.ajax.php +++ b/main/inc/ajax/lp.ajax.php @@ -11,6 +11,7 @@ $action = $_REQUEST['a']; $course_id = api_get_course_int_id(); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); +$sessionId = api_get_session_id(); switch ($action) { case 'get_documents'; @@ -170,6 +171,23 @@ switch ($action) { } } + break; + case 'update_gamification': + $lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null; + + $jsonGamification = [ + 'stars' => 0, + 'score' => 0 + ]; + + if ($lp) { + $score = $lp->getCalculateScore($sessionId); + + $jsonGamification['stars'] = $lp->getCalculateStars($sessionId); + $jsonGamification['score'] = sprintf(get_lang('XPoints'), $score); + } + + echo json_encode($jsonGamification); break; default: echo ''; diff --git a/main/newscorm/learnpath.class.php b/main/newscorm/learnpath.class.php index ebd2516c8e..cf5022e2f0 100755 --- a/main/newscorm/learnpath.class.php +++ b/main/newscorm/learnpath.class.php @@ -10178,7 +10178,7 @@ EOD; $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId); - if ($progress > 50) { + if ($progress >= 50) { $stars++; } diff --git a/main/newscorm/lp_ajax_switch_item.php b/main/newscorm/lp_ajax_switch_item.php index 477f3c7dd7..ecd19d870b 100755 --- a/main/newscorm/lp_ajax_switch_item.php +++ b/main/newscorm/lp_ajax_switch_item.php @@ -214,6 +214,7 @@ function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_it "update_toc('highlight','".$new_item_id."');". "update_toc('$mylesson_status','".$new_item_id."');". "update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');"; + $return .= 'updateGamificationValues(); '; $mylp->set_error_msg(''); $mylp->prerequisites_match(); // Check the prerequisites are all complete. diff --git a/main/newscorm/scorm_api.php b/main/newscorm/scorm_api.php index 99cc98f164..c43dfe3fc6 100755 --- a/main/newscorm/scorm_api.php +++ b/main/newscorm/scorm_api.php @@ -1360,6 +1360,26 @@ function update_progress_bar(nbr_complete, nbr_total, mode) { return true; } +/** + * Update the gamification values (number of stars and score) + */ +function updateGamificationValues() { + var fetchValues = $.ajax('lp.ajax.php', { + dataType: 'json', + data: { + a: 'update_gamification' + } + }); + + $.when(fetchValues).done(function (values) { + if (values.stars > 0) { + $('#scorm-gamification .fa-star:nth-child(' + values.stars + ')').addClass('level'); + } + + $('#scorm-gamification .col-xs-4').text(values.score); + }); +} + /** * Analyses the variables that have been modified through this SCO's life and * put them into an array for later shipping to lp_ajax_save_item.php