uo_tincanny_reporting_auto_calculate_scaled_score
Filters whether to automatically calculate scaled scores for Tincanny reports, allowing custom logic before the default calculation.
add_filter( 'uo_tincanny_reporting_auto_calculate_scaled_score', $callback, 10, 1 );
Description
Filters whether to automatically calculate a scaled score when the Statement lacks one. Returning `true` allows Uncanny Tincan to attempt calculation. Useful for ensuring score reporting consistency, especially with older xAPI content.
Usage
add_filter( 'uo_tincanny_reporting_auto_calculate_scaled_score', 'your_function_name', 10, 1 );
Parameters
-
$activity(mixed) - This parameter determines whether the system should attempt to automatically calculate a scaled score when one is not present.
Return Value
The filtered value.
Examples
/**
* Example of how to use the 'uo_tincanny_reporting_auto_calculate_scaled_score' filter.
*
* This example demonstrates how to intercept the filter and manually calculate a scaled score
* based on the activity's success/completion status if the scaled score is not already set.
*
* @param bool $auto_calculate The default value, typically false. If true, the plugin will attempt auto-calculation.
* @param array $context An array containing contextual data, including activity, group_id, course_id, and lesson_id.
* @return bool Whether to proceed with auto-calculation or not.
*/
add_filter( 'uo_tincanny_reporting_auto_calculate_scaled_score', function( $auto_calculate, $context ) {
// Only proceed if the default is to NOT auto-calculate and the scaled score is null.
// The original code checks for `true === apply_filters(...)`, so we need to return `true` to enable auto-calculation.
if ( $auto_calculate === false && is_null( $context['activity']->getScore()->getScaled() ) ) {
// Attempt to get the score and completion status from the activity.
$score = $context['activity']->getScore()->getRaw(); // Assuming getRaw() returns a numerical score
$completion = $context['activity']->isCompleted(); // Assuming isCompleted() returns a boolean
// If completion is true and a raw score is available, we can calculate a scaled score.
if ( $completion && is_numeric( $score ) ) {
// Simple example: Assume a completion means a scaled score of 1.0,
// otherwise, if there's a score but not completed, maybe 0.5.
// In a real-world scenario, you'd have more complex logic based on specific requirements.
$new_scaled_score = ( $score > 0 ) ? 1.0 : 0.5; // Example: score > 0 and completed = 1.0, else 0.5
// Update the activity's score object directly if possible, or return true to let the plugin handle it.
// For demonstration, we'll return true to allow the plugin to use this context.
// A more advanced implementation might directly set $this->TC_Result->getScore()->setScaled($new_scaled_score);
// but that's not directly accessible within this filter context without more complex object passing.
// The filter is designed to signal *if* auto-calculation should happen.
// The plugin itself would then use the context to perform the calculation.
// For this filter, we signal *that* we want to allow auto-calculation.
return true;
}
}
// If we don't meet the criteria for our custom calculation, return the original value.
return $auto_calculate;
}, 10, 2 ); // Priority 10, 2 arguments expected ( $auto_calculate, $context )
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/uncanny-tincan/classes/TinCanRequest.php:294
$scaled_score = false;
$duration = 0;
if ( $this->TC_Result->getScore() ) {
// Check if we should try to auto calculate null scaled scores.
if ( is_null( $this->TC_Result->getScore()->getScaled() ) ) {
if ( true === apply_filters(
'uo_tincanny_reporting_auto_calculate_scaled_score',
false,
array(
'activity' => $activity,
'group_id' => $group_id,
'course_id' => $course_id,
'lesson_id' => $lesson_id,