News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Need help regarding Online time on Profile summary

Started by Rumpa, November 30, 2024, 02:06:19 AM

Previous topic - Next topic

Rumpa

Need help regarding Online time on Profile summary


here are the code i am trying
// Add the online time display in the profile areas
function add_online_time_to_profile(&$profile_areas)
{
    global $txt;

    // Load the custom language file for the current language
    loadLanguage('OnlineTime');

    // Add the custom callback to the summary area
    if (isset($profile_areas['info']['areas']['summary']['enabled']))
    {
        $profile_areas['info']['areas']['summary']['custom_callback'] = 'display_online_time_in_summary';
    }
}


// Custom callback to fetch and display online time
function display_online_time_in_summary($memID)
{
    global $context, $smcFunc, $txt;

    // Query to fetch total online time from the `members` table
    $request = $smcFunc['db_query']('', '
        SELECT total_time_logged_in
        FROM {db_prefix}members
        WHERE id_member = {int:id_member}',
        [
            'id_member' => $memID,
        ]
    );

    // Fetch the result
    list($total_time_logged_in) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    // Convert total time (in seconds) to hours and minutes
    $hours = floor($total_time_logged_in / 3600);
    $minutes = floor(($total_time_logged_in % 3600) / 60);

    // Add the formatted time to the profile context
    $context['member']['online_time'] = sprintf($txt['online_time'], $hours, $minutes);
}


tried to triggered hook with this
<hook hook="integrate_pre_include" function="integrate_pre_include" file="$sourcedir/OnlineTimeProfile.php" />
<hook hook="integrate_load_theme" function="integrate_load_theme" file="$sourcedir/OnlineTimeProfile.php" />


but not working

Diego Andrés

Quote from: Rumpa on November 30, 2024, 02:06:19 AM<hook hook="integrate_pre_include" function="integrate_pre_include" file="$sourcedir/OnlineTimeProfile.php" />
<hook hook="integrate_load_theme" function="integrate_load_theme" file="$sourcedir/OnlineTimeProfile.php" />


???
You have an obvious issue here, it will probably even show up in the error log.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Rumpa

Quote from: Diego Andrés on November 30, 2024, 11:39:11 AM
Quote from: Rumpa on November 30, 2024, 02:06:19 AM<hook hook="integrate_pre_include" function="integrate_pre_include" file="$sourcedir/OnlineTimeProfile.php" />
<hook hook="integrate_load_theme" function="integrate_load_theme" file="$sourcedir/OnlineTimeProfile.php" />


???
You have an obvious issue here, it will probably even show up in the error log.
How to resolved it can you tell me please

Bugo

First of all, in the function attribute, you need to specify the name of the function that is located in the file you mentioned in the file attribute.

Secondly, it's better to use a more specialized hook, like integrate_profile_areas.

<hook hook="integrate_pre_include" file="$sourcedir/OnlineTimeProfile.php" />
<hook hook="integrate_profile_areas" function="add_online_time_to_profile" file="$sourcedir/OnlineTimeProfile.php" />

Thirdly, instead of making an unnecessary database query, you can use the following code:

function display_online_time_in_summary($memID)
{
   global $user_profile, $context, $txt;

   $timeDays = floor($user_profile[$memID]['total_time_logged_in'] / 86400);
   $timeHours = floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);
   $context['time_logged_in'] = ($timeDays > 0 ? $timeDays . $txt['total_time_logged_days'] : '') .
   ($timeHours > 0 ? $timeHours . $txt['total_time_logged_hours'] : '') . floor(($user_profile[$memID]
   ['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_minutes'];

   var_dump($context['time_logged_in']);
}

Rumpa

Thanks for reply @Bugo

Here i tried with this code but nothing to display in profile summary

// Register the hook during load
function integrate_pre_include(&$files)
{
    $files[] = dirname(__FILE__) . '/OnlineTimeInprofile.php';
}

// Add the online time display in the profile
function add_online_time_to_profile(&$profile_areas)
{
    global $profile_areas;

    // Add it under the 'summary' section
    if (isset($profile_areas['info']['areas']['summary']['enabled']))
    {
        $profile_areas['info']['areas']['summary']['custom_callback'] = 'display_online_time_in_summary';
    }
}

// Custom callback to fetch and display online time
function display_online_time_in_summary($memID)
{
  global $user_profile, $context, $txt;

  $timeDays = floor($user_profile[$memID]['total_time_logged_in'] / 86400);
  $timeHours = floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);
  $context['time_logged_in'] = ($timeDays > 0 ? $timeDays . $txt['total_time_logged_days'] : '') .
  ($timeHours > 0 ? $timeHours . $txt['total_time_logged_hours'] : '') . floor(($user_profile[$memID]
  ['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_minutes'];

  var_dump($context['time_logged_in']);
}


is there need any separate lanuage file or anything elese

Bugo

It seems like you're pulling some code out of thin air and then scratching your head when it doesn't work! How about sharing the materials you learned from? I suggest starting with studying other people's mods—not copying, but really diving into how they work.

Now, regarding your task, you've got two options: you can implement the output through a separate section like you planned, but that will require creating a template file:

package-info.xml, the install section:

<hook hook="integrate_pre_include" file="$sourcedir/OnlineTimeProfile.php" />
<hook hook="integrate_profile_areas" function="OnlineTimeProfile::profileAreas#" file="$sourcedir/OnlineTimeProfile.php" />
<require-file name="OnlineTimeProfile.php" destination="$sourcedir"/>
<require-file name="MyCustom.template.php" destination="$themedir"/>

OnlineTimeProfile.php:

<?php

if (!defined('SMF'))
    die(
'No direct access...');

class 
OnlineTimeProfile
{
    public function 
profileAreas(array &$profile_areas)
    {
        
$profile_areas['info']['areas']['custom_area'] = [
            
'label' => 'My Title',
            
'function' => __CLASS__ '::displayOnlineTimeInSummary#',
            
'permission' => [
                
'own' => 'is_not_guest',
                
'any' => 'profile_view',
            ],
        ];
    }

    public function 
displayOnlineTimeInSummary($memID)
    {
        global 
$user_profile$context$txt;

        
loadTemplate('MyCustom');

        
$context['sub_template'] = 'profile';

        
$timeDays floor($user_profile[$memID]['total_time_logged_in'] / 86400);
        
$timeHours floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);

        
$context['time_logged_in'] = ($timeDays $timeDays $txt['total_time_logged_days'] : '') .
        (
$timeHours $timeHours $txt['total_time_logged_hours'] : '') . floor(($user_profile[$memID]
        [
'total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_minutes'];
    }
}


MyCustom.template.php:

<?php

function template_profile()
{
    global 
$context;

    
var_dump($context['time_logged_in']);
}






=======================================================================

Or, you could use the integrate_load_custom_profile_fields hook to create a custom field that only shows up on the profile page:

    /**
    * @hook integrate_load_custom_profile_fields
    */
    public function loadCustomProfileFields($memID, $area)
    {
        global $context, $user_profile, $txt;

        if (empty($context['current_action']) || $context['current_action'] != 'profile' || $area != 'summary')
            return;

        $timeDays = floor($user_profile[$memID]['total_time_logged_in'] / 86400);
        $timeHours = floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);

        $time_logged_in = ($timeDays > 0 ? $timeDays . $txt['total_time_logged_days'] : '') .
        ($timeHours > 0 ? $timeHours . $txt['total_time_logged_hours'] : '') . floor(($user_profile[$memID]
        ['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_minutes'];

        $context['custom_fields'][] = array(
            'name'        => 'Online time',
            'type'        => 'text',
            'output_html' => $time_logged_in,
            'placement'  => 0,
            'colname'    => 'time_logged_in'
        );
    }



Happy coding!

Rumpa


Diego Andrés

@Bugo Are you okay with the user submitting your code? And if so, are you okay with him adding you as additional author?

SMF Tricks - Free & Premium Responsive Themes for SMF.

Bugo


Rumpa


Advertisement: