Attention Controls
Attention Controls
Introduction
Attention Controls (ACs) are used in the BKF app to ensure that truck drivers are actively engaged while watching training videos. There are two types of ACs:
- One-Timecode ACs: These ACs require the driver to enter a code that appears on the screen during the video. The code is only valid for a limited amount of time, typically around 50 seconds.
- Two-Timecode ACs: These ACs require the driver to remember for example a color that appears on the screen during the video. After some time, a request with three different colors comes up, and the driver has to confirm the correct color within 30 seconds.
ACs are implemented as classes in the App\AttentionControls namespace. Each AC class must extend the BaseAttentionControl abstract class and implement the AttentionControlInterface interface.
Handling control failures
When a driver fails to pass an attention control, the system handles the failure by jumping the video back to the latest point where an attention control was passed. If no attention control has been passed yet, the system jumps the video back to the start. This ensures that the driver can review the content leading up to the point of failure and provides an opportunity for them to regain their focus and successfully pass the attention control.
Backend
AttentionControlInterface
The AttentionControlInterface interface defines the methods that each AC class must implement:
interface AttentionControlInterface
{
public function getPriority();
public function getTimecodeType();
public function setTimecodes(array $timecodes);
public function getTimecodes();
public function getControlComponent();
public function setModel(AttentionControl $model);
public function loadConfig(array $config);
public function getConfig(): array;
public function initializeControl();
}
getPriority()returns the priority of the AC compared to other ACs in the same video. So we can see how many percent is passed. (deprecated)getTimecodeType()returns the type of AC, either AttentionControlTypes::ONE_TIMECODE or AttentionControlTypes::TWO_TIMECODES.setTimecodes(array $timecodes)sets the timecodes for the AC.getTimecodes()returns the timecodes for the AC.getControlComponent()returns the Livewire component for the AC.setModel(AttentionControl $model)sets the AttentionControl model for the AC.loadConfig(array $config)loads the configuration for the AC.getConfig(): arrayreturns the configuration for the AC.initializeControl()is called once the AC is created and is used to create initial configurations.
BaseAttentionControl
The BaseAttentionControl abstract class provides a base implementation for the AttentionControlInterface methods. It also provides methods for loading and setting configuration options.
abstract class BaseAttentionControl implements AttentionControlInterface
{
protected $timecodes;
protected $model;
public function __construct(array $config = [])
{
$this->loadConfig($config);
}
public function loadConfig(array $config)
{
foreach ($config as $key => $item) {
$this->$key = $item;
}
}
public function getPriority()
{
return 10;
}
public function setTimecodes(array $timecodes)
{
$this->timecodes = $timecodes;
}
public function getTimecodes()
{
return $this->timecodes;
}
public function setModel(AttentionControl $model)
{
$this->model = $model;
}
public function initializeControl()
{
return null;
}
}
Each AC class should extend the BaseAttentionControl class and implement the AttentionControlInterface methods specific to the AC.
Overall, Attention Controls are a critical feature of the BKF app, ensuring that truck drivers are actively engaged and paying attention during training courses. By customizing and extending the ACs, the BKF app can meet the specific needs of different courses and videos and provide a better learning experience for drivers.
Generation of ACs
Attention Controlls are generated through a function in the HasAttentionControls trait. The function generates attention controlls with the AttentionControlManager every time a user starts a video for the first time. There is a safe space in the beginning and end of every video of 10 minutes.
The AttentionControlManager is responsible for generating and scheduling attention controls during video playback. It loads configuration parameters from a config file and calculates the timecodes for each control based on the video length and control frequency (configurable in app config). It also keeps track of the available attention controls and randomly selects one to be generated and scheduled at each timecode.
The manager initializes each attention control by calling its initializeControl() method and assigns timecodes to the control. The control is then added to the list of planned attention controls.
After all attention controls have been generated and scheduled, the manager sorts the list by timecode and returns it as an array.
Frontend
The frontend of the Attention Control system is built with Alpine.js and Livewire. It provides the user interface for the system, allowing the user to interact with the Attention Control components. Every attention control is represented by an alpine component.
Functions used in Attention Controls
You can extract functions into attention controls, to have the base of your control.
timedAttentionControl(seconds, secondarySeconds = null)
This function returns an object that includes a timer for a timed attention control. The seconds parameter sets the primary countdown time for the control, and the optional secondarySeconds parameter sets a secondary countdown time. The object includes methods for resetting and starting the timers.
baseControl(timecode, secondaryTimecode = null)
This function returns an object that includes methods and properties for a basic attention control. The timecode parameter specifies the timecode when the control should become visible, and the optional secondaryTimecode parameter specifies a secondary timecode. The object includes methods for handling control start and control pass events, and for checking the timecode.
withAnimations()
This function returns an object that includes methods for triggering success and error animations in attention controls. These methods dispatch custom events that can be listened for by the frontend.
Generate a new attention control
Adding a new attention control requires the following steps:
Create a new Blade view for the attention control. The view should include an
x-dataattribute, which specifies the JavaScript component to use for the control.Create a new JavaScript component for the attention control. This component should include any necessary functionality and should be defined in a separate
.jsfile. Use the functions described before, to embet the basic fuctionallity of an attention control.Modify the
AttentionControlclass to include the new attention control. In particular, you'll need to add a new method to the class that returns the name of the Blade view to use for the control.If the new attention control requires any configuration options, you'll need to add a form to the
create.blade.phpview that allows users to specify these options when creating a new attention control.Add it to the
AttentionControlManagerClass here in the array:
private $availableAttentionControls = [
ColorAttentionControl::class,
SmsAttentionControl::class,
];
Creating a Blade View
To create a new Blade view for an attention control, follow these steps:
Create a new Blade file in the
resources/views/livewire/attention-controlsdirectory. The filename should match the name of the attention control (e.g.,color-attention-control.blade.php).Add an
x-dataattribute to the root element of the view. This attribute should specify the name of the JavaScript component to use for the attention control.Write the HTML and Livewire components necessary for the attention control. Make sure to use Livewire directives (e.g.,
wire:click) to bind JavaScript functionality to the HTML.
Creating a JavaScript Component
To create a new JavaScript component for an attention control, follow these steps:
Create a new JavaScript file in the
resources/js/attention-controlsdirectory. The filename should match the name of the attention control (e.g.,color-attention-control.js). Link it inside the index.js in the folder.Define the JavaScript component in the new file. The component should export a single object that defines the component's behavior and properties.
If the attention control requires any additional functionality (e.g., timers), define this functionality in the JavaScript file.
Modifying the AttentionControl Class
To add the new attention control to the AttentionControl class, follow these steps:
Add a new method to the class that returns the name of the Blade view to use for the attention control. The method name should match the name of the attention control (e.g.,
colorAttentionControl).In the
createmethod of theAttentionControlclass, add a newswitchstatement that handles the creation of the new attention control.If the new attention control requires any configuration options, modify the
create.blade.phpview to include a form that allows users to specify these options.