Example #1
0
 /**
  * Track video viewing progress
  *
  * @return     void
  */
 public function trackingTask()
 {
     // Include need media tracking library
     require_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'tables' . DS . 'mediatracking.php';
     require_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'tables' . DS . 'mediatrackingdetailed.php';
     // Instantiate objects
     $database = \App::get('db');
     $session = \App::get('session');
     // Get request vars
     $time = Request::getVar('time', 0);
     $duration = Request::getVar('duration', 0);
     $event = Request::getVar('event', 'update');
     $resourceid = Request::getVar('resourceid', 0);
     $detailedId = Request::getVar('detailedTrackingId', 0);
     $ipAddress = $_SERVER['REMOTE_ADDR'];
     // Check for asset id
     if (!$resourceid) {
         echo 'Unable to find resource identifier.';
         return;
     }
     // Instantiate new media tracking object
     $mediaTracking = new MediaTracking($database);
     $mediaTrackingDetailed = new MediaTrackingDetailed($database);
     // Load tracking information for user for this resource
     $trackingInformation = $mediaTracking->getTrackingInformationForUserAndResource(User::get('id'), $resourceid, 'course');
     $trackingInformationDetailed = $mediaTrackingDetailed->loadByDetailId($detailedId);
     // Are we creating a new tracking record
     if (!is_object($trackingInformation)) {
         $trackingInformation = new stdClass();
         $trackingInformation->user_id = User::get('id');
         $trackingInformation->session_id = $session->getId();
         $trackingInformation->ip_address = $ipAddress;
         $trackingInformation->object_id = $resourceid;
         $trackingInformation->object_type = 'course';
         $trackingInformation->object_duration = $duration;
         $trackingInformation->current_position = $time;
         $trackingInformation->farthest_position = $time;
         $trackingInformation->current_position_timestamp = Date::toSql();
         $trackingInformation->farthest_position_timestamp = Date::toSql();
         $trackingInformation->completed = 0;
         $trackingInformation->total_views = 1;
         $trackingInformation->total_viewing_time = 0;
     } else {
         // Get the amount of video watched from last tracking event
         $time_viewed = (int) $time - (int) $trackingInformation->current_position;
         // If we have a positive value and its less then our ten second threshold,
         // add viewing time to total watched time
         if ($time_viewed < 10 && $time_viewed > 0) {
             $trackingInformation->total_viewing_time += $time_viewed;
         }
         // Set the new current position
         $trackingInformation->current_position = $time;
         $trackingInformation->current_position_timestamp = Date::toSql();
         // Set the object duration
         if ($duration > 0) {
             $trackingInformation->object_duration = $duration;
         }
         // Check to see if we need to set a new farthest position
         if ($trackingInformation->current_position > $trackingInformation->farthest_position) {
             $trackingInformation->farthest_position = $time;
             $trackingInformation->farthest_position_timestamp = Date::toSql();
         }
         // If event type is start, means we need to increment view count
         if ($event == 'start' || $event == 'replay') {
             $trackingInformation->total_views++;
         }
         // If event type is end, we need to increment completed count
         if ($event == 'ended') {
             $trackingInformation->completed++;
         }
     }
     // Save detailed tracking info
     if ($event == 'start' || !$trackingInformationDetailed) {
         $trackingInformationDetailed = new stdClass();
         $trackingInformationDetailed->user_id = User::get('id');
         $trackingInformationDetailed->session_id = $session->getId();
         $trackingInformationDetailed->ip_address = $ipAddress;
         $trackingInformationDetailed->object_id = $resourceid;
         $trackingInformationDetailed->object_type = 'course';
         $trackingInformationDetailed->object_duration = $duration;
         $trackingInformationDetailed->current_position = $time;
         $trackingInformationDetailed->farthest_position = $time;
         $trackingInformationDetailed->current_position_timestamp = Date::toSql();
         $trackingInformationDetailed->farthest_position_timestamp = Date::toSql();
         $trackingInformationDetailed->completed = 0;
     } else {
         // Set the new current position
         $trackingInformationDetailed->current_position = $time;
         $trackingInformationDetailed->current_position_timestamp = Date::toSql();
         // Set the object duration
         if ($duration > 0) {
             $trackingInformationDetailed->object_duration = $duration;
         }
         // Check to see if we need to set a new farthest position
         if (isset($trackingInformationDetailed->farthest_position) && $trackingInformationDetailed->current_position > $trackingInformationDetailed->farthest_position) {
             $trackingInformationDetailed->farthest_position = $time;
             $trackingInformationDetailed->farthest_position_timestamp = Date::toSql();
         }
         // If event type is end, we need to increment completed count
         if ($event == 'ended') {
             $trackingInformationDetailed->completed++;
         }
     }
     // Save detailed
     $mediaTrackingDetailed->save($trackingInformationDetailed);
     // Save tracking information
     if ($mediaTracking->save($trackingInformation)) {
         if (!isset($trackingInformation->id)) {
             $trackingInformation->id = $mediaTracking->id;
         }
         $trackingInformation->detailedId = $mediaTrackingDetailed->id;
         echo json_encode($trackingInformation);
     }
 }