ulgm_rest_api_get_assignments_data
Filters assignments data before it's returned in the REST API response for get_assignments.
add_filter( 'ulgm_rest_api_get_assignments_data', $callback, 10, 2 );
Description
Filters the assignment data before it's returned by the REST API for group assignments. Developers can modify the assignment table or the POST data to alter the results or add custom validation. This hook fires after initial data retrieval and before the response is sent.
Usage
add_filter( 'ulgm_rest_api_get_assignments_data', 'your_function_name', 10, 2 );
Parameters
-
$assignments_table(mixed) - This parameter contains the data for assignments, likely structured as a table or an array of assignment objects.
-
$_POST(mixed) - This parameter contains the data representing assignments, likely in a table format, that is being filtered.
Return Value
The filtered value.
Examples
add_filter( 'ulgm_rest_api_get_assignments_data', 'my_custom_assignments_data_filter', 10, 2 );
/**
* Modifies the assignments data returned by the REST API for group assignments.
*
* This example demonstrates how to:
* 1. Check for specific POST data to conditionally alter the results.
* 2. Add a custom field to each assignment if certain conditions are met.
* 3. Filter out assignments based on a custom status.
*
* @param array $_assignments_table The original array of assignment data.
* @param array $_POST The raw $_POST data sent with the request.
* @return array The modified array of assignment data.
*/
function my_custom_assignments_data_filter( $_assignments_table, $_POST ) {
// Ensure we have the necessary data from the POST request
if ( ! isset( $_POST['user_role'] ) || $_POST['user_role'] !== 'administrator' ) {
return $_assignments_table; // Only administrators can modify this data
}
// Example: Add a custom 'completion_percentage' field if not already present
// This might be useful for a custom report view.
if ( isset( $_POST['show_completion'] ) && $_POST['show_completion'] === 'true' ) {
foreach ( $_assignments_table as &$assignment ) {
// Simulate calculating completion percentage (replace with actual logic if available)
$assignment['completion_percentage'] = rand( 0, 100 );
}
unset( $assignment ); // Unset the reference to avoid unexpected side effects
}
// Example: Filter out assignments with a specific 'pending_approval' status
// This might be used if a certain user role shouldn't see assignments awaiting approval.
if ( isset( $_POST['filter_pending'] ) && $_POST['filter_pending'] === 'true' ) {
$_assignments_table = array_filter( $_assignments_table, function( $assignment ) {
return $assignment['status'] !== 'pending_approval';
} );
}
// Re-index the array after filtering
$_assignments_table = array_values( $_assignments_table );
return $_assignments_table;
}
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/classes/reports/group-assignments.php:929
public function get_assignments_data() {
$data = $_POST;
// validate inputs
$lesson_ID = absint( $data['lessonId'] );
$course_ID = absint( $data['courseId'] );
$group_ID = absint( $data['groupId'] );
$status = $data['status'];
$assignments_table = $this->assignments_table( $lesson_ID, $course_ID, $group_ID, $status );
$assignments_table = apply_filters( 'ulgm_rest_api_get_assignments_data', $assignments_table, $_POST );
return $assignments_table;
}