Call for help: making functions work with viewtopic

Support for ANY phpBB3 related issue.
- MOD Support requests should be asked in the specific MOD topic -
Any version of phpBB3 is supported.
Forum rules
READ: StarTrekGuide.com Board Rules

Users are requested to fill out the Support Request Template and include it in every support request.

As a general rule, Support for a Specific MOD should be requested in the specific MOD topic, as it would alert the MOD Author of the support request.
Please specify if you have any MODs installed on your board when asking for support.

phpBB support requests allowed
MOD Requests and Generic discussions not allowed

Call for help: making functions work with viewtopic

Postby Pace » 29 May 2007, 13:12

Hi all.

You may know that I'm working on a karma MOD that will eventually be two-sided, with an aspect of mod/admin-controlled karma and with an aspect of user-given karma not unlike a "thanks" MOD.

But I've hit a wall I can't seem to avoid nor tear down: I have a "functions_karpost.php" file, and I have code changes in "viewtopic.php", but they do not seem to work together.
I'm sure it's a stupid part of the functions file that's missing, but at the same time, I've based this code on the custom profile fields code (/include/functions_profile_fields.php and viewtopic.php)? So why it doesn't work is beyond me.

Here's the code I'm talking about:

Code: Select all
<?php

class karma
{
    function 
generate_karma_template($mode, &$row$user_id 0)
    {
        global 
$db$user;

        if (
$mode == 'grab')
        {
            
error_reporting(E_ALL);
            
$sql 'SELECT *
                FROM ' 
USERS_TABLE "
                WHERE user_id = $user_id"
;
            
$result $db->sql_query($sql);
            
$karma_data = array();
            while (
$ka_row $db->sql_fetchrow($result))
            {
                
$karma_data[$ka_row['karma']] = $ka_row;
            }
            
$db->sql_freeresult($result);

            return 
$ka_row['karma'];
        }
        else if (
$mode == 'show')
        {
            
$tpl_karma = array();
            
$tpl_karma['row'] = $tpl_karma['blockrow'] = array();

            
$karma_value $this->get_karma($ka_row);

            
$tpl_karma['row'] = array(
                
'karma'    => $karma_value,
                
'KARMA_NAME'    => $user->lang['KARMA'],
            );

            
$tpl_karma['blockrow'] = array(
                
'KARMA_FIELD_VALUE'    => $karma_value,
                
'KARMA_NAME'    => $user->lang['KARMA'],
            );

            return 
$tpl_karma;
        }
        else
        {
            
trigger_error('Wrong mode for karma'E_USER_ERROR);
        }
    }

    function 
get_karma(&$ka_row)
    {
        global 
$user;
        
$karma_value $ka_row['karma'];

        
$positive_karmas = array
        (
            
=> 0,
            
=> 0,
            
=> 0,
            
=> 0,
            
=> 0,
            
=> 1,
            
=> 1,
            
=> 1,
            
=> 1,
            
10 => 1,
            
11 => 1,
            
12 => 1,
            
13 => 2,
            
14 => 2,
            
15 => 2,
            
16 => 2,
            
17 => 2,
            
18 => 2,
            
19 => 2,
            
20 => 2,
            
21 => 2,
            
22 => 3,
            
23 => 3,
            
24 => 3,
            
25 => 3,
            
26 => 3,
            
27 => 3,
            
28 => 3,
            
29 => 3,
            
30 => 3,
            
31 => 3,
            
32 => 3,
            
33 => 4,
            
34 => 4,
            
35 => 4,
            
36 => 4,
            
37 => 4,
            
38 => 4,
            
39 => 4,
            
40 => 4,
            
41 => 4,
            
42 => 4,
            
43 => 4,
            
44 => 4,
            
45 => 4,
            
46 => 5,
            
47 => 5,
            
48 => 5,
            
49 => 5,
            
50 => 5,
            
51 => 6,
            
52 => 6,
            
53 => 6,
            
54 => 6,
            
55 => 6,
            
56 => 6,
            
57 => 6,
            
58 => 6,
            
59 => 6,
            
60 => 6,
            
61 => 6,
            
62 => 6,
            
63 => 6,
            
64 => 6,
            
65 => 6,
            
66 => 6,
            
67 => 6,
            
68 => 7,
            
69 => 7,
            
70 => 7,
            
71 => 7,
            
72 => 7,
            
73 => 7,
            
74 => 7,
            
75 => 7,
            
76 => 7,
            
77 => 7,
            
78 => 7,
            
79 => 7,
            
80 => 7,
            
81 => 7,
            
82 => 7,
            
83 => 7,
            
84 => 7,
            
85 => 7,
            
86 => 7,
            
87 => 8,
            
88 => 8,
            
89 => 8,
            
90 => 8,
            
91 => 8,
            
92 => 8,
            
93 => 8,
            
94 => 8,
            
95 => 8,
            
96 => 8,
            
97 => 8,
            
98 => 8,
            
99 => 8,
            
100 => 9
        
);
        
        
$negative_karmas = array
        (
            
=> 1,
            
=> 2,
            
=> 3,
            
=> 4,
            
=> 5,
            
=> 6,
            
=> 7,
            
10 => 8
        
);
        
        if (isset(
$positive_karmas[$karma_value]))
        {
            
$poster_karma $user->lang['karma_positive'][$karma_value];
        }
        elseif (isset(
$negative_karmas[-$karma_value]))
        {
            
$poster_karma $user->lang['karma_negative'][-$karma_value];
        }
        else
        {
            if (
$karma_value 0)
                
$poster_karma $user->lang['karma_negative'][8];
            else
                
$poster_karma $user->lang['karma_positive'][9];
            
$poster_karma $user->lang['karma_negative'][0];
        }
    return 
$poster_karma;
    }

}
?>


Viewtopic changes:
Code: Select all
 // Load custom profile fields
if ($config['load_cpf_viewtopic'])
{
    include(
$phpbb_root_path 'includes/functions_profile_fields.' $phpEx);
    
$cp = new custom_profile();

    
// Grab all profile fields from users in id cache for later use - similar to the poster cache
    
$profile_fields_cache $cp->generate_profile_fields_template('grab'$id_cache);
}

// Load karma field
    
include($phpbb_root_path 'includes/functions_karpost.' $phpEx);
    
$ka = new karma();

    
// Grab karma field from users in id cache for later use - similar to the poster cache
    
$karma_field_cache $ka->generate_karma_template('grab'$id_cache);

[
?]

    if (
$config['load_cpf_viewtopic'])
    {
        
$cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show'false$profile_fields_cache[$poster_id]) : array();
    }

    
$ka_row = array();
    
$ka_row = (isset($karma_field_cache[$poster_id])) ? $ka->generate_karma_template('show'$row$karma_field_cache[$poster_id]) : array();

[
?]

        
'S_CUSTOM_FIELDS'    => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true false,

        
'S_KARMA'        => (isset($ka_row['row']) && sizeof($ka_row['row'])) ? true false,

[
?]

    if (isset(
$cp_row['row']) && sizeof($cp_row['row']))
    {
        
$postrow array_merge($postrow$cp_row['row']);
    }

    
$postrow array_merge($postrow$tpl_karma['row']);

    
// Dump vars into template
    
$template->assign_block_vars('postrow'$postrow);

    if (!empty(
$cp_row['blockrow']))
    {
        foreach (
$cp_row['blockrow'] as $field_data)
        {
            
$template->assign_block_vars('postrow.custom_fields'$field_data);
        }
    }

    if (!empty(
$tpl_karma['blockrow']))
    {
        foreach (
$tpl_karma['blockrow'] as $karma_data)
        {
            
$template->assign_block_vars('postrow', array(
                
'KARMA_VALUE'    => '100')  // this part was intended as a test to see if it actually worked? which it didn't
            
);
        }
    }

 

And the problem is that viewtopic tells me that "tpl_karma" is an undefined variable. And on top of that, when I was using code closer to the custom profile fields code, it told me the same thing for $ka_row
So what's the problem exactly? Anyone with any ideas?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male


Solved!

Postby Pace » 30 May 2007, 01:12

:yahoo: Well, I might as well say that young developers are often smarter :P

No offence meant to Handyman and Highway of Life, but iGuru at phpbb.com found a much more simple way of getting it to work, and while it involves putting more code in the template files, it's a lot easier:

Code: Select all
      <dd><strong>{L_KARMA}:</strong> <!-- IF postrow.POSTER_KARMA >= 0 && postrow.POSTER_KARMA <= 4 -->{L_KARMA1}<!-- ELSEIF postrow.POSTER_KARMA >= 5 && postrow.POSTER_KARMA <= 10 -->{L_KARMA2}<!-- ELSEIF postrow.POSTER_KARMA >= 11 && postrow.POSTER_KARMA <= 18 -->{L_KARMA3}<!-- ELSEIF postrow.POSTER_KARMA >= 19 && postrow.POSTER_KARMA <= 28 -->{L_KARMA4}<!-- ELSEIF postrow.POSTER_KARMA >= 29 && postrow.POSTER_KARMA <= 41 -->{L_KARMA5}<!-- ELSEIF postrow.POSTER_KARMA >= 42 && postrow.POSTER_KARMA <= 54 -->{L_KARMA6}<!-- ELSEIF postrow.POSTER_KARMA >= 55 && postrow.POSTER_KARMA <= 70 -->{L_KARMA7}<!-- ELSEIF postrow.POSTER_KARMA >= 71 && postrow.POSTER_KARMA <= 88 -->{L_KARMA8}<!-- ELSEIF postrow.POSTER_KARMA >= 89 && postrow.POSTER_KARMA <= 99 -->{L_KARMA9}<!-- ELSEIF postrow.POSTER_KARMA >= 100 -->{L_KARMA10}<!-- ELSEIF postrow.POSTER_KARMA >= -1 && postrow.POSTER_KARMA <= -2 -->{L_KARMA-1}<!-- ELSEIF postrow.POSTER_KARMA >= -3 && postrow.POSTER_KARMA <= -5 -->{L_KARMA-2}<!-- ELSEIF postrow.POSTER_KARMA >= -6 && postrow.POSTER_KARMA <= -9 -->{L_KARMA-3}<!-- ELSEIF postrow.POSTER_KARMA >= -10 -->{L_KARMA-4}<!-- ENDIF --> ({postrow.POSTER_KARMA})</dd>


And therefore in viewtopic I have the following:
Code: Select all
// in the "Guest" part:
                'jabber'            => '',
                'search'            => '',
                'age'                => '',

                'karma'                => '',

// in the "Member" part:

                'search'        => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", 'search_author=' . urlencode($row['username']) .'&amp;showresults=posts') : '',
                'karma'            => $row['karma'],

// in $postrow = array(

        'POSTER_WARNINGS'    => $user_cache[$poster_id]['warnings'],
        'POSTER_AGE'        => $user_cache[$poster_id]['age'],

        'POSTER_KARMA'        => $user_cache[$poster_id]['karma'],

 


You'll probably say it's more like a hack now, but frankly, it doesn't matter to me :P

Anyway, I'm now off to study some more for exams, but afterwards, finally back to work on this karma MOD to get the whole thing working, with both its "karma given from admin&mod" and "karma by users as means of thanks for a post" aspects.

Edit: code has been updated, and I've got it working in the "view profile" section just as nicely & easily.
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Solved!

Postby geoffreak » 30 May 2007, 21:56

Pace wrote:
Code: Select all
      <dd><strong>{L_KARMA}:</strong> <!-- IF postrow.POSTER_KARMA >= 0 && postrow.POSTER_KARMA <= 4 -->{L_KARMA1}<!-- ELSEIF postrow.POSTER_KARMA >= 5 && postrow.POSTER_KARMA <= 10 -->{L_KARMA2}<!-- ELSEIF postrow.POSTER_KARMA >= 11 && postrow.POSTER_KARMA <= 18 -->{L_KARMA3}<!-- ELSEIF postrow.POSTER_KARMA >= 19 && postrow.POSTER_KARMA <= 28 -->{L_KARMA4}<!-- ELSEIF postrow.POSTER_KARMA >= 29 && postrow.POSTER_KARMA <= 41 -->{L_KARMA5}<!-- ELSEIF postrow.POSTER_KARMA >= 42 && postrow.POSTER_KARMA <= 54 -->{L_KARMA6}<!-- ELSEIF postrow.POSTER_KARMA >= 55 && postrow.POSTER_KARMA <= 70 -->{L_KARMA7}<!-- ELSEIF postrow.POSTER_KARMA >= 71 && postrow.POSTER_KARMA <= 88 -->{L_KARMA8}<!-- ELSEIF postrow.POSTER_KARMA >= 89 && postrow.POSTER_KARMA <= 99 -->{L_KARMA9}<!-- ELSEIF postrow.POSTER_KARMA >= 100 -->{L_KARMA10}<!-- ELSEIF postrow.POSTER_KARMA >= -1 && postrow.POSTER_KARMA <= -2 -->{L_KARMA-1}<!-- ELSEIF postrow.POSTER_KARMA >= -3 && postrow.POSTER_KARMA <= -5 -->{L_KARMA-2}<!-- ELSEIF postrow.POSTER_KARMA >= -6 && postrow.POSTER_KARMA <= -9 -->{L_KARMA-3}<!-- ELSEIF postrow.POSTER_KARMA >= -10 -->{L_KARMA-4}<!-- ENDIF --> ({postrow.POSTER_KARMA})</dd>


It doesn't matter how short it is!
DON'T PUT THAT MANY IF STATEMENTS IN A TEMPLATE!
This will fail the phpbb moddb requirements because code needs to be legible.

You need to place the if's in the php file by using something like this:
Code: Select all
if ($karma 90)
{
    do 
this
}
else if (
$karma 75)
{
    do 
this because  75 karma <= 90
}
.. 
Earthlings, we have been watching you for years, and now we know your primitive language, so listen: All your base are belong to us.
Anime Bite - Your future #1 source for all things anime and manga!My Blog
User avatar
geoffreak    
Supporter
Supporter
 
Posts: 663
Joined: 17 Mar 2007, 21:52
Location: The Internets
Favorite Team: phpBB
Gender: Male
phpBB Knowledge: 9

Postby Pace » 31 May 2007, 00:07

Thing is, given that I don't have a clue about how MODX works or can be used (MOD was sooo much easier? both for developing and for using), I'll probably hand it over to someone else to clean up and reformat. Simple reasons: when I had this kind of code in viewtopic.php & memberlist.php, HoL told me it was better to use a new functions file in /includes, but that didn't work. Basically, nothing seems to work that is considered good code. And nothing that works is considered good :D

So unless someone can prove to me that putting it in the templates doesn't work (which it does) or someone comes up with the exact code to put inside the php files, I won't be touching this again.
I know it sounds as if a sulking child were saying it. But when you've followed Handyman and Highway of Life's advice for hours on end, trying to get a simple thing to work in the most correct, "conform to standards" way, and failed, I think you understand my position and frustration at the whole thing.
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Call for help: making functions work with viewtopic

Postby geoffreak » 31 May 2007, 08:31

I had to completely rewrite my thank post mod after the first release.
It worked to begin with... :glare:

You can see how much work was required in the STG Academy ;)


Anyways, if I were you, I would check current mods to see how they work.
and if in doubt about modx, you can generate it using my MODX generator (accessible via the link in my signature) :D
Earthlings, we have been watching you for years, and now we know your primitive language, so listen: All your base are belong to us.
Anime Bite - Your future #1 source for all things anime and manga!My Blog
User avatar
geoffreak    
Supporter
Supporter
 
Posts: 663
Joined: 17 Mar 2007, 21:52
Location: The Internets
Favorite Team: phpBB
Gender: Male
phpBB Knowledge: 9


Return to phpBB3 Support

Who is online

Users browsing this forum: No registered users and 9 guests