HTML Logo by World Wide Web Consortium ( Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS. ocPortal 9 is superseded by Composr 10.

Head over to for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.

ocPortal Tutorial: Making an addon (part 3)

Written by Chris Graham, ocProducts
Welcome to the third of our series of addon making tutorials. If you haven't yet read the prior tutorials then it's advisable that you do so before reading this one.

Showing an expanded users-online display in OCF

A similar feature to this was added to ocPortal 4, so this example is no longer usable. But it is still a good tool for learning.

Today we're going to make a modification to how OCF shows the online users at the bottom of the forum-view.
We're going to colour-code each user there against usergroup, and give usergroup colour-keys beneath.

This example is much more involved than the ones given in the previous tutorial. The previous addons were straight-forward as we were just adding new modular functionality into the ocPortal framework – now we are extending and changing existing behaviour. Right now I don't know how I'll do this, and my explanations will follow my train of logic. I've intentionally done this to help you to learn to think like a programmer like me thinks.

In order to get an idea of what to do we need to consider what we're influencing. To find this out I'll need to go and find the ocPortal code that handles the user-online functionality that we're changing. Right now I've just opened up my web browser to our forums and scrolled to the bottom. I see we have the text 'Users online:' there, near the list that we need to change. This doesn't help us find the code because this is a language string and thus not stored where the actual code is stored. If I can find some unique HTML I'll be able to find the template and then use the name of that template to find the PHP code (there are other ways to do it, but this way is what I am most comfortable doing). I'm opening up the HTML page source and searching for that 'Users online:' phrase, and I've found '<td class="ocf_stats_usersonline_2">'.

Next I do a file contents search in Windows Explorer, in the themes/default/templates directory for the string I found. The search gives me 'OCF_STATS.tpl'. I open up this in my editor as I'll probably need it. Now I want to know where this template is used in the PHP code, so I do another file search, this time in the ocPortal root directory, for all PHP files containing do_template('OCF_STATS'. This search reveals a match in sources/ocf_general.php, so I open that up in my editor and I use the editor search feature to go to the exact line, which turns out to be line 418.

We now have PHP code and a template, and these are probably all we need to be modifying. Because this is an addon, we should be overriding files rather than editing them directly, otherwise we wouldn't be able to revert, and upgrading ocPortal would be awkward because we wouldn't know what we had changed (everything old and new would be lumped together). Therefore I copy themes/default/templates/OCF_STATS.tpl to themes/default/templates_custom/OCF_STATS.tpl, and I copy the PHP function in sources/ocf_general.php that uses that template ('ocf_wrapper') into a new PHP file, sources_custom/ocf_general.php, between new <?php and ?> markers. Our sources_custom/ocf_general.php uses function-level overriding, which is much more effective because it reduces the chance of bug fixes to the original copy being non-effective. This is because less is overridden, and thus less verbatim-ocProducts-code is masked.

I remove the old non-overridden files from my editor, and open up my copies instead.

Now all we need to do is to read over ocPortal's 'ocf_wrapper' function and 'OCF_STATS' template to get an understanding of how they work. I can't explain all that here – you need to have built up your own code understanding so you can figure it out yourself. In fact, after reading the code I've found I'm also going to need to override the OCF_USER_MEMBER.tpl template, so I do for that what I did for OCF_STATS.tpl.

Knowing how to program, I now have written my code, with my modified files saved as following:



{+START,IF_ADJACENT,OCF_USER_MEMBER}, {+END}{+START,IF_PASSED,AT}<a {+START,IF_PASSED,COLOUR}style="color: {COLOUR}" {+END}title="{AT#}" href="{PROFILE_URL*}">{USERNAME*}</a>{+END}{+START,IF_NON_PASSED,AT}<a {+START,IF_PASSED,COLOUR}style="color: {COLOUR}" {+END}title="{!MEMBER}" href="{PROFILE_URL*}">{USERNAME*}</a>{+END}<span class="accessibility_hidden">, </span>



<br />

<div class="box guid_{_GUID}">
   <div class="box_inner">

      <div class="ocf_stats_1">
         <table summary="{!MAP_TABLE}" cellpadding="0" cellspacing="0" class="map_table ocf_stats_2">
            <colgroup span="2">
               <col class="ocf_bottom_bar_left_column" />
               <col class="ocf_bottom_bar_right_column" />

               <th class="de_th ocf_column1 ocf_stats_usersonline_1">
                  <span class="field_name">{!USERS_ONLINE}:</span><br />
                  <span class="associated_link">[ <a href="{USERS_ONLINE_URL*}">{!DETAILS}</a> ]</span>
               <td class="ocf_stats_usersonline_2">

                     {+START,LOOP,GROUPS}{+START,IF,{$NOT,{$GET,doing_first_group}}}, {+END}<a style="color: {GCOLOUR*}" href="{$PAGE_LINK*,_SEARCH:groups:view:{GID}}">{GTITLE*}</a>{$SET,doing_first_group,0}{+END}
               <th class="de_th ocf_column1 ocf_stats_main_1">
               <td class="ocf_stats_main_2">

<br />




 * Do the wrapper that fits around OCF module output.
 * @param  tempcode   The title for the module output that we are wrapping.
 * @param  tempcode   The module output that we are wrapping.
 * @param  boolean    Whether to include the personal bar in the wrap.
 * @param  boolean    Whether to include statistics in the wrap.
 * @param  ?AUTO_LINK The forum to make the search link search under (NULL: Users own PT forum/unknown).
 * @return tempcode   The wrapped output.
function ocf_wrapper($title,$content,$show_personal_bar=true,$show_stats=true,$forum_id=NULL)

   global $ZONE;
   $wide=(($ZONE['zone_wide']==1) || (get_param_integer('wide',get_param_integer('wide_high',0))==1));
   if (!$wide)

   // Notifications
   if ((get_member()!=$GLOBALS['OCF_DRIVER']->get_guest_id()) && ((get_page_name()=='forumview') || (get_page_name()=='topicview')))
      if (get_option('is_on_block_cache')=='1') $_notifications=get_cache_entry('_new_pp',$cache_identifier);
      if (is_null($_notifications))
         $notifications=new ocp_tempcode();
         foreach ($unread_pps as $unread_pp)
            if (is_null($by)) $by=do_lang('UNKNOWN');
            if (is_null($unread_pp['t_forum_id']))
            } else
               if ($unread_pp['p_title']!='') $u_title=$unread_pp['p_title'];
            if ($description!='') $description=' ('.$description.')';

      } else
   } else
      $notifications=new ocp_tempcode();

   if ($show_personal_bar)
      if (get_member()!=$GLOBALS['OCF_DRIVER']->get_guest_id()) // Logged in user



         $_new_topics=$GLOBALS['FORUM_DB']->query('SELECT COUNT(*) AS mycnt FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_topics WHERE t_forum_id IS NOT NULL AND t_cache_first_time>'.(string)intval($member_info['last_visit_time']));
         $_new_posts=$GLOBALS['FORUM_DB']->query('SELECT COUNT(*) AS mycnt FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_posts WHERE p_cache_forum_id IS NOT NULL AND p_time>'.(string)intval($member_info['last_visit_time']));

         // Any unread PT-PPs?



      } else // Guest
   } else $head=new ocp_tempcode();

   if ($show_stats)

      // Colours for various usergroup IDs -- we'll do 16, but we'll repeat 4 times in case we have up to 64 usergroups

      // Users online
      $users_online=new ocp_tempcode();
      foreach ($members as $member=>$username)
         if ($member==$GLOBALS['OCF_DRIVER']->get_guest_id())
         if (is_null($username)) continue;
      if ($guests!=0)
         if (!$users_online->is_blank()) $users_online->attach(', ');

      // Birthdays
      $birthdays=new ocp_tempcode();
      foreach ($_birthdays as $_birthday)
         if (array_key_exists('age',$_birthday)) $birthday->attach(' ('.$_birthday['age'].')');
      if (!$birthdays->is_blank()) $birthdays=do_template('OCF_BIRTHDAYS',array('_GUID'=>'03da2c0d46e76407d63bff22aac354bd','BIRTHDAYS'=>$birthdays));

      // Usergroup keys
      foreach ($all_groups as $gid=>$gtitle)
         if ($gid==db_get_first_id()) continue; // Throw out the first, guest usergroup

   } else $foot=new ocp_tempcode();


   return $wrap;


Thumbnail: Our functionality modification in action.

Our functionality modification in action.

As usual I won't explain all the code, but I will mention a few things:
  • The main PHP code doesn't have to do much more than define some colours and pass in a usergroup list. Almost all the code in my file is just code from the original ocf_wrapper function
  • I passed a new parameter into OCF_USER_MEMBER.tpl and used that to indicate the colour to display a member link with; however, I wrapped a Tempcode 'IF_PASSED' directive around the use of the parameter because the template is reused and won't always be given a colour in that way
  • I passed in an array into OCF_STATS.tpl, containing all the usergroup names and colours. I then processed the array using a Tempcode 'LOOP' directive that spat out comma-separated links. I used a common GET/SET trick so I could show commas between each usergroup without also suffering a leading/trailing comma
Don't worry that I keep pulling out clever things from a proverbial hat. There are a finite number of standard ocPortal programming 'tricks' so once you know them all you can stop learning.


Gambling hook (line and sinker)

Create a new Point Store product based around gambling. Members pay a certain set price to do a gamble (e.g. 3 points), and then they receive a random number of points within a range (e.g. -20 to 20). Therefore the gambling odds are fair, but there's an 'administration fee' for it.

Point-store hooks are just files in sources/hooks/modules/pointstore. Use flagrant.php as an example, but yours should be much simpler. There are actually only three functions (methods, technically) that need to be in a Point Store hook:
  • 'init', which performs common initialisation for all steps (you can probably leave this empty)
  • 'info', which contains Tempcoded HTML which describes the product and provides a link
  • a function that has the same name as whatever 'type' parameter you used in the aforementioned link; this either is the first in a chain of input and confirmation and delivery, or for a simple case like this, you can just do the actual gamble delivery in this single function.

Be imaginative

Make a feature change of your own design, and release it as an addon.

Points challenge

125 points will be given to any user that releases a working ocPortal addon (which may be based off pre-existing Open Source code, made by anybody) so long as the addon contains at least 1000 lines of PHP code.

To claim your prize, post in the Addons forum and 'report post' with the phrase '125 points please' in your report.

See also