Simple Karma Challenge

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: Karma.php problem?

Postby Highway of Life » 13 Feb 2007, 12:32

Pace, are you testing this on your localhost, or on the server itself?

You might have a much more successful time testing it on your localhost... it will show you the errors.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10


Re: Karma.php problem?

Postby Pace » 13 Feb 2007, 12:44

Until I last posted, it was only on the localhost, and that's where I got the blank page. So I decided to try on the server, and that's where I got error messages. Finally Tongue

But now I'm stuck with the error message I posted, the one concerning functions.php
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Handyman » 13 Feb 2007, 12:49

Post the function in functions.php that runs o line 3318 (make sure you post the entire function.
it's an easy fix
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Pace » 13 Feb 2007, 13:44

Code: Select all
            if (empty($user->data))
            {
                
$user->session_begin();
            }


("$user->session_begin();" is line 3319)
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Handyman » 13 Feb 2007, 13:50

Yes, post then entire function? it has to be added to the global for it to work.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Pace » 13 Feb 2007, 13:53

This, you mean?
Spoiler:
Code: Select all
function msg_handler($errno$msg_text$errfile$errline)
{
    global 
$cache$db$auth$template$config$user;
    global 
$phpEx$phpbb_root_path$msg_title$msg_long_text;

    
// Message handler is stripping text. In case we need it, we are possible to define long text...
    
if (isset($msg_long_text) && $msg_long_text && !$msg_text)
    {
        
$msg_text $msg_long_text;
    }

    switch (
$errno)
    {
        case 
E_NOTICE:
        case 
E_WARNING:

            
// Check the error reporting level and return if the error level does not match
            // Additionally do not display notices if we suppress them via @
            // If DEBUG is defined the default level is E_ALL
            
if (($errno & ((defined('DEBUG') && error_reporting()) ? E_ALL error_reporting())) == 0)
            {
                return;
            }

            if (
strpos($errfile'cache') === false && strpos($errfile'template.') === false)
            {
                
// remove complete path to installation, with the risk of changing backslashes meant to be there
                
$errfile str_replace(array(phpbb_realpath($phpbb_root_path), '\\'), array('''/'), $errfile);
                
$msg_text str_replace(array(phpbb_realpath($phpbb_root_path), '\\'), array('''/'), $msg_text);

                echo 
'<b>[phpBB Debug] PHP Notice</b>: in file <b>' $errfile '</b> on line <b>' $errline '</b>: <b>' $msg_text '</b><br />' "\n";
            }

        break;

        case 
E_USER_ERROR:

            
garbage_collection();

            echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
            echo 
'<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">';
            echo 
'<head>';
            echo 
'<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
            echo 
'<title>' $msg_title '</title>';
            echo 
'<link href="' $phpbb_root_path 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />';
            echo 
'</head>';
            echo 
'<body id="errorpage">';
            echo 
'<div id="wrap">';
            echo 
'    <div id="page-header">';
            echo 
'        <a href="' $phpbb_root_path '">Return to forum index</a>';
            echo 
'    </div>';
            echo 
'    <div id="page-body">';
            echo 
'        <div class="panel">';
            echo 
'            <span class="corners-top"><span></span></span>';
            echo 
'            <div id="content">';
            echo 
'                <h1>General Error</h1>';
            
            echo 
'                <h2>' $msg_text '</h2>';
            
            if (!empty(
$config['board_contact']))
            {
                echo 
'                <p>Please notify the board administrator or webmaster: <a href="mailto:' $config['board_contact'] . '">' $config['board_contact'] . '</a></p>';
            }
            
            echo 
'            </div>';
            echo 
'            <span class="corners-bottom"><span></span></span>';
            echo 
'        </div>';
            echo 
'    </div>';
            echo 
'    <div id="page-footer">';
            echo 
'        Powered by phpBB &copy; ' date('Y') . ' <a href="http://www.phpbb.com/">phpBB Group</a>';
            echo 
'    </div>';
            echo 
'</div>';
            echo 
'</body>';
            echo 
'</html>';
            
            exit;
        break;

        case 
E_USER_WARNING:
        case 
E_USER_NOTICE:

            
define('IN_ERROR_HANDLER'true);

            if (empty(
$user->data))
            {
                
$user->session_begin();
            }

            
// We re-init the auth array to get correct results on login/logout
            
$auth->acl($user->data);

            if (empty(
$user->lang))
            {
                
$user->setup();
            }

            
$msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text;
            
$msg_title = (!isset($msg_title)) ? $user->lang['INFORMATION'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title);

            if (!
defined('HEADER_INC'))
            {
                if (
defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
                {
                    
adm_page_header($msg_title);
                }
                else
                {
                    
page_header($msg_title);
                }
            }

            
$template->set_filenames(array(
                
'body' => 'message_body.html')
            );

            
$template->assign_vars(array(
                
'MESSAGE_TITLE'        => $msg_title,
                
'MESSAGE_TEXT'        => $msg_text,
                
'S_USER_WARNING'    => ($errno == E_USER_WARNING) ? true false,
                
'S_USER_NOTICE'        => ($errno == E_USER_NOTICE) ? true false)
            );

            
// We do not want the cron script to be called on error messages
            
define('IN_CRON'true);
            
            if (
defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
            {
                
adm_page_footer();
            }
            else
            {
                
page_footer();
            }

            exit;
        break;
    }
}
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Handyman » 13 Feb 2007, 14:40

Yeah, that is what I meant.
Very odd, $user is in the globals and it's giving you that error.
Though I guess you said you got past that? at least I hope.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Pace » 15 Feb 2007, 12:45

Okay, Handyman inspired me, and after a few more hours of trying out new stuff, getting past one error and then past the next, I finally have a working karma.php
I now have one remaining problem: I can't get karma to show when someone looks at a user's profile.

I've been modifying memberlist.php, but it never seems to work.
What I need to put in there are the following bits of code:

Spoiler:
Code: Select all
        // Advanced KARMA Mod
        
$sql 'SELECT karma FROM ' USERS_TABLE " WHERE user_id= '$user_id' ";
        
$result $db->sql_query($sql);
        
$karma $db-> sql_fetchfield('karma');
        
$db->sql_freeresult($result);
        
$applaud_img '';
        
$applaud_alt '';
        
$applaud_url '';
        
$smite_img '';
        
$smite_alt '';
        
$smite_url '';
        
$user_karma '';

        switch (
$karma)
        {
            case -
8:
            
$karma_text "Banned";
            break;
            case -
7:
            
$karma_text "Galactic Scourge";
            break;
            case -
6:
            
$karma_text "Public Enemy";
            break;
            case -
5:
            
$karma_text "Most Wanted";
            break;
            case -
4:
            
$karma_text "Fugitive";
            break;
            case -
3:
            
$karma_text "Felon";
            break;
            case -
2:
            
$karma_text "Criminal";
            break;
            case -
1:
            
$karma_text "Offender";
            break;
            case 
0:
            
$karma_text "Clean";
            break;
            case 
1:
            
$karma_text "Decent Individual";
            break;
            case 
2:
            
$karma_text "Good Egg";
            break;
            case 
3:
            
$karma_text "Upstanding Citizen";
            break;
            case 
4:
            
$karma_text "Role Model";
            break;
            case 
5:
            
$karma_text "Pillar of Society";
            break;
            case 
6:
            case 
7:
            
$karma_text "Honoured Leader";
            break;
            case 
8:
            
$karma_text "Divine";
            break;
        }

        if (
$user_id == 2)
        {
            
$user_karma $user->lang['KARMA'] . ': Divine (n<sup>n</sup>)';
        }
        else
        {
            if(!
$auth->acl_gets('a_''m_') && !$auth->acl_getf_global('m_'))
            {
                
$user_karma $user->lang['KARMA'] . ': ' $karma_text " ($karma)";
            }
            else
            {
            
$applaud_alt $user->lang['APPLAUD'];
            
$applaud_url append_sid("{$phpbb_root_path}karma.$phpEx?" "&amp;u=$user_id&x=applaud");
            
$applaud_img '<img src="styles/Galarpian/imageset/icon_applaud.gif" width="11" height="11" border="0" />';
            
            
$user_karma $user->lang['KARMA'] . ': ' $karma_text " ($karma)";

            
$smite_alt $user->lang['SMITE'];
            
$smite_url append_sid("{$phpbb_root_path}karma.$phpEx?" "&amp;u=$user_id&x=smite");
            
$smite_img '<img src="styles/Galarpian/imageset/icon_smite.gif" width="11" height="11" border="0" />';
            }
        }
        
        
// Advanced KARMA Mod

When posted in the function "show_profile($data)
{" area, this gives me an error: "undefined variable: db".
Now, why does it do that? The "$db->" is used in plenty of other places.
If, on the other hand, I post it in the "case 'viewprofile':" section, well, it doesn't work either, because the page then tells me the "$user_karma" variable is not defined.

After that, I have to put the following:
Code: Select all
'USER_KARMA' => $user_karma,

Now, it seems I'm supposed to put that in the
Code: Select all
    // Dump it out to the template
    
return array(

part, but I'm not sure, due to all the errors I get?

So, what's the problem now?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Highway of Life » 15 Feb 2007, 14:49

OH boy... lots of changes here...
Let me see if I can quickly address the biggest issues...
  1. first NEVER hardcode language into your php public file.
    Hmm... I shall repeat that...

    NEVER put language variables outside of your language directory.
  2. Second, all the data from the users table is already being called, so we don?t need ANOTHER query...
  3. Third, you would probably want to control the karma levels from the language file, see code opimization below.
  4. Fourth, Empty defined variables can be defined in one line of code, i.e.
    Code: Select all
    $variable $another_var $yet_another $still_more $im_empty_too '';
  5. Fifth, you?ll want to include this code in a seperate file, then call it through a function, see code optimization below.
  6. Sixth, allow for greater values than -8 to +8, as it?s currently only a span of 16 clicks, so it would only take a handful of posts to reach those numbers.
    Idealy, you would want to manage this from the ACP, but for a very simple, generic karma system such as this, you can control it through the language.
  7. Seventh, Don?t use a static User ID for the founder, use a user type instead... see code optimization below.
  8. Eighth, See example of how to return the variables for use in the template, again, if it?s more than a small handful of lines of code, you?ll want to include a karma template file into the memberlist_view and memberlist_body files.
I?ll stop there since that is quite a lot, give this a shot, and go from here.

First, to the memberlist.php file...
Code: Select all
*** /phpBB3/memberlist.php   Wed Feb  7 20:47:30 2007
--- /phpBB3/memberlist_new.php      Thu Feb 15 14:37:03 2007
***************
*** 
488,493 ****
--- 
488,496 ----
                        unset(
$module);
                }
  
+               include_once(
$phpbb_root_path 'includes/functions_karma.' $phpEx);
+               
grab_karma_rank($member['user_karma'], $member['user_type']);

                
$template->assign_vars(array(
                        
'POSTS_DAY'                     => sprintf($user->lang['POST_DAY'], $posts_per_day),
                        
'POSTS_PCT'                     => sprintf($user->lang['POST_PCT'], $percentage),

Then, in the functions_karma.php file...

Code: Select all
<?php
function grab_karma_rank($user_karma$user_level)
{
    global 
$user$template;
    
// when we expand this mod for ACP and database control,
    // we would probably use regular expressions to determine the values,
    // but for now, we can be somewhat static.
    
$i $user_karma;
    switch (
$i)
    {
        case (
$i <= 0):
            
$karma $user->lang['karma_negative'][0]; // this would be clean.
        
break;
        case (
$i <= -1):
            
$karma $user->lang['karma_negative'][1];
        break;
        case (
$i <= -3):
            
$karma $user->lang['karma_negative'][2];
        break;
        
// ... repeat until you reach something close to like -20
        // which would be "banned"
        // then start on the positive case...
        
case ($i => 1):
            
$karma $user->lang['karma_positive'][0];
        break;
        case (
$i => 5):
            
$karma $user->lang['karma_positive'][1];
        break;
        case (
$i => 12):
            
$karma $user->lang['karma_positive'][2];
        break;
        
// etc... and repeat until you reach something like oh... 100 or so
        // which would be Supreme Ruler, or some such thing
    
}
    
    if (
$user_level == USER_FOUNDER)
    {
        
$karma $user->lang['karma_positive'][20]; // assuming 20 is "Divine"
    
}
    
    
$template->assign_vars(array(
        
'KARMA_LEVEL'    => $karma,
    ));
    
    return;
}

?>


Your language file would contain something like:
Code: Select all
<?php
// ... header info
$lang array_merge($lang, array(
    
'karma_negative' => array(
        
'Clean',
        
'Offender',
        
'Criminal',
        
'Felon'// etc...
    
),
    
    
'karma_positive' => array(
        
'Decent Individual',
        
'Good Egg',
        
'Upstanding Citizen',
        
'Role Model'// etc...
    
),
?>


Remember, these are just examples... change to fit..
And I?m thinking there must be a better way to handle the level switch...

Scratch

Ah, like the Ranks, but those are database controlled, still might glean an idea off of that system.

Moved to Starfleet Academy Intermediate challenges for phpBB3.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: Simple Karma Challenge

Postby Pace » 15 Feb 2007, 16:06

Heh? Okay, I'll look at that tomorrow. Thanks for explaining that. I was really only porting the old karma hack.

Anyway, once I've got everything working, I'll put that together as a MOD, and post the code of my karma.php file here as well.

Thanks again!
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

PreviousNext

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 1 guest