User Tools

Site Tools


userpoints:developer

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
userpoints:developer [2024/01/10 17:52] adminuserpoints:developer [2025/03/07 14:28] (current) admin
Line 1: Line 1:
-====== UserPoints API documentation ======+====== UserPoints Integrations and API ======
  
  
-Introduction UserPoints - API integration in a Third party component (advanced)+This is an introduction to UserPoints integrations with Joomla or third party extensions. It is dedicated to developers and anyone with a sufficient knowledge of the PHP language and Joomla 4/5 extension development. There is a UserPoints API which has recently been simplified.
  
-This documentation is dedicated to developers and anyone with a sufficient knowledge of the PHP language and Joomla 4/5 component development. 
 Licence: UserPoints is released under license GNU/GPL License. Author: Bernard Gilly – Adrien Roussel - Martin Brampton. This project started as AlphaUserPoints back in 2008. Licence: UserPoints is released under license GNU/GPL License. Author: Bernard Gilly – Adrien Roussel - Martin Brampton. This project started as AlphaUserPoints back in 2008.
  
-===== Plugin/Rule creation =====+===== Basic Operation of UserPoints =====
  
-A plugin creation (new rule creation for a 3rd party component) is done in 2 steps+UserPoints provides a way to award users points for specific actions on Joomla based web site. Points can also be "spent" in order to permit other actions. The actions involved are specific to whatever applications are appropriate for the site. It is possible, but not necessary, to equate points to monetary value.
  
 +The handling of points is determined by what are called **rules** in UserPoints. A rule is triggered by something happening as a result of user action. The rule determines what happens in the way of awarding or deducting points. UserPoints automatically installs some plugins and rules that support actions triggered by standard Joomla extensions. These include actions to do with content (articles) and to do with user registration.
  
-=== Step1 - API insertion in component ===+Unfortunately, for historic reasons, rules are identified by plugin name. It is often not necessary for there to be an actual plugin corresponding to the name. The term plugin name should be understood as being the rule name.
  
-Just insert the following API in the component code where needed. The best way is to make it follow user action that could give the users some points or take someFor example : in a comment system component or in a forum, just add the API after the comment or topic INSERT query in the database.+===== Ways to Connect Software to UserPoints ===== 
 + 
 +The approach is different, depending on whether the author of the software to be connected is implementing code to help the integration. If this is the case, then any rules needed for the integration can be specified by an XML file in the root of the component involved. The software then uses the API described below to interact with UserPoints. No new plugin is needed for this kind of integration. 
 + 
 +Alternatively, it may be possible to achieve integration without modifying the relevant software. If it provides plugin hooks, then plugin can be written that will be triggered by events in the third party softwareThe plugin can use the API. The administrator UI of UserPoints provides the ability to create and modify rules to manage the integration.
  
 === Basic API === === Basic API ===
  
 +The UserPoints code has been fully namespaced and will autoload when used correctly. In addition, for Joomla 3.x, UserPoints automatically installs and enables a plugin that will establish the namespacing. This is not necessary with Joomla 4+ which takes account of the namespace in the UserPoints packaging XML.
 +
 +Given the above, use of the API becomes in its simplest form:
    
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php'+    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP)+    if (class_exists(UserPoints::class) { 
-    +        UserPoints::newpoints('rule_name');
-        require_once $api_UP; UserPoints::newpoints( 'function_name' );+
     }     }
 </code> </code>
  
-function_name is the name of the rule that will be used to attribute points to the current user (if logged in). For each UserPoints integrated rule (system rules), names syntax is as follows:+rule_name is the name of the rule that will be used to attribute points to the current user (if logged in). For each UserPoints integrated rule (system rules), names syntax is as follows:
  
 example: sysplgbup_newregistered for new users points attribution example: sysplgbup_newregistered for new users points attribution
  
-It is convenient to keep the same name syntax for third party components plugin as follows: +You should select names for rules that are unlikely to conflict with other integrations.
- +
-plgbup_functionname +
- +
-Example: plgbup_newcomment or plgbup_newtopic for a comment system or forum API integration. To name a rule that would give points when adding a new topic in Kunena: plgbup_kunena_topic_create.+
  
 Keep in mind that this method only gives points to the current user logged in. This is the Basic API. Keep in mind that this method only gives points to the current user logged in. This is the Basic API.
Line 42: Line 44:
 === Attribute points to another user than the current user === === Attribute points to another user than the current user ===
  
-To give points to anothe user than the one connected, only the user id is required. To get his UserPoints (BUPID) Identity, we just need to use the getAnyUserReferreID(). This method is the one used to give points to an article author when the article is being read by someone on the site.+To give points to another user than the one connected, only the rule and user ID is required. To get his UserPoints (BUPID) Identity, we just need to use the getAnyUserReferreID(). This method is the one used to give points to an article author when the article is being read by someone on the site.
  
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php';  +    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP)+    if (class_exists(UserPoints::class) { 
-    {  +        $upid = UserPoints::getAnyUserReferreID( $userID ); 
-        require_once $api_UP; +        if ( $bupid ) UserPoints::newpoints( 'rule_name', $upid );
-        $bupid = UserPoints::getAnyUserReferreID( $userID ); +
-        if ( $bupid ) UserPoints::newpoints( 'function_name', $bupid ); +
     }     }
 </code> </code>
Line 59: Line 59:
  
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php'+    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP)+    if (class_exists(UserPoints::class) { 
-    { +        $keyreference = UserPoints::buildKeyreference('rule_name', 'item id' );   
-        require_once $api_UP;   +        UserPoints::newpoints( 'rule_name', '', $keyreference);
-        $keyreference = UserPoints::buildKeyreference('function_name', 'item id' );   +
-        UserPoints::newpoints( 'function_name', '', $keyreference);  +
     }     }
 </code> </code>
  
-=== Adding information datas ===+=== Adding information data ===
  
-To add information datas to be displayed in the action details, just add a new parameter as follows:+To add information data to be displayed in the action details, just add a new parameter as follows:
  
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php'+    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP))  +    if (class_exists(UserPoints::class) { 
-    { +        $keyreference = UserPoints::buildKeyreference('rule_name', 'item id' );   
-        require_once $api_UP;   +        UserPoints::newpoints( rule_name','', $keyreference,'information_data');
-        $keyreference = UserPoints::buildKeyreference('function_name', 'item id' );   +
-        UserPoints::newpoints( function_name','', $keyreference,'information_datas');  +
     }     }
 </code> </code>
Line 84: Line 80:
 === Using different amounts of points on the same rule === === Using different amounts of points on the same rule ===
  
-A component might also need to add or to take points for different amounts. For example, when buying goods with points. Products have different prices, a fixed amount in the rule would'nt make it. The API $randompoints parameter comes instead of the amount of points set in the rule. It can be negative in case of purchases or penalities.+A component might also need to add or to take points for different amounts. For example, when buying goods with points. Products have different prices, a fixed amount in the rule would not work. The API $randompoints parameter comes instead of the amount of points set in the rule. It can be negative in case of purchases or penalities.
  
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php'+    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP)+    if (class_exists(UserPoints::class) { 
-    { +        $keyreference = UserPoints::buildKeyreference('rule_name', 'item id' );   
-        require_once $api_UP;   +        UserPoints::newpoints( 'rule_name','', $keyreference,'',-1450);
-        $keyreference = UserPoints::buildKeyreference('function_name', 'item id' );   +
-        UserPoints::newpoints( 'function_name','', $keyreference,'',-1450);  +
     }     }
 </code> </code>
Line 98: Line 92:
 === Get the result from a successful operation === === Get the result from a successful operation ===
  
-In more advanced code, if the component routine needs to know if the operation has been successful or not, (enough amount of points for a purchase in a user account), we can add a 'feedback' parameter. It has a Boolean type value. Code example:+In more advanced code, if the new software needs to know if the operation has been successful or not, (enough amount of points for a purchase in a user account), we can add a 'feedback' parameter. It has a Boolean type value. Code example:
  
 <code php> <code php>
-    $api_UP = JPATH_SITE.'/components/com_userpoints/helper.php'+    use BlackSheepResearch\Component\UserPoints\Site\UserPoints
-    if ( file_exists($api_UP)+    if (class_exists(UserPoints::class) { 
-    { +        $keyreference=UserPoints::buildKeyreference('up_purchasewithvirtuemart', $transactionID ); 
-        require_once $api_UP; +        if (UserPoints::newpoints( 'up_purchasewithvirtuemart', '', $keyreference, 'Product reference: AM-5245', -1290, true))
-        $keyreference=UserPoints::buildKeyreference('plgbup_purchasewithvirtuemart', $transactionID ); +
-        if (UserPoints::newpoints( 'plgbup_purchasewithvirtuemart', '', $keyreference, 'Product reference: AM-5245', -1290, true))+
         {         {
-            [... code continued ...]+            [... code for successful operation ...]
         }         }
     }     }
Line 117: Line 109:
 In a customized code component, you can force and remove the constraint on a rule to the user level by adding the parameter _force = 1_. The existing rule will be available now for _guest, registered_ and _special_. In a customized code component, you can force and remove the constraint on a rule to the user level by adding the parameter _force = 1_. The existing rule will be available now for _guest, registered_ and _special_.
  
-=== Display an additional system message on frontend ===+=== Display an additional system message to the user ===
  
-you can display a specific message on frontend by adding the parameter frontmessage=”You custom message”. API full implementation+you can display a specific message to the user by adding the parameter frontmessage=”You custom message”. API full implementation
  
 <code php> <code php>
-    UserPoints::newpoints( +    use BlackSheepResearch\Component\UserPoints\Site\UserPoints; 
-        string$pluginfunction, +    if (class_exists(UserPoints::class) { 
-        [string$BUPIDotheruser = ''], +      UserPoints::newpoints( 
-        [string$keyreference = ''], +        string $pluginfunction, 
-        [string$data = ''], +        [string $BUPIDotheruser = ''], 
-        [integer$randompoints = 0], +        [string $keyreference = ''], 
-        [boolean$feedback = false], +        [string $data = ''], 
-        [integer$force=0], +        [integer $randompoints = 0], 
-        [string$frontmessage=''+        [boolean $feedback = false], 
-    );+        [integer $force=0], 
 +        [string $frontmessage=''
 +      ); 
 +    }
 </code> </code>
  
-Note: If the operation is a points substraction, the account has to have at least the same amount of points. If not, a notice warns the user that he doe'snt have enough points to complete the action (by default). You can set up general parameter in the configuration of UserPoints, in backend administrator to allows your users to have a negative account.+Note: If the operation is a points substraction, the account has to have at least the same amount of points. If not, a notice warns the user that he does not have enough points to complete the action (by default). You can set up an option in the configuration of UserPoints, as administrator to allow your users to have a negative account.
  
 ==== Step 2 - XML file creation ==== ==== Step 2 - XML file creation ====
  
-Then developers have to create an xml file to make easier the installation process in the UserPoints component. This xml file has to be utf-8 encoded (required). All developers of third party extensions for Joomla! can add directly at the root of their frontend component a unique xml file containing all the rules for a single componentDeveoper has to respect the ordering and tags: structure example: [betauserpoints_rule.xml](images/stories/documentation/betauserpoints/betauserpoints_rule.xml) Tag “component” is the name of the third component like “com_kunena” or other. As it is the same component, it is worth repeating for each rule in the tag “rule”. +To complete the integration of an application with UserPoints, create an XML file to specify rule(s). The XML file should be added at the top level of the extension that is being integrated with UserPointsIn the case of component, it should be on the user sideThe XML file must be called userpoints_rule.xml. If the extension being integrated with UserPoints has been installed including a valid userpoints_rule.xml file, then the site administrator can invoke "Auto-detect new rules" from the UserPoints control panelUserPoints will then look for any XML rule files and incorporate them into the database of rules
-Administrator of the website which install a third component with this xml file can autodetect directly from the button “auto-detect plugins” in control panel of UserPoints. + 
-This xml file has to be utf-8 encoded (required) but not be zipped! Just put this file at the root of frontend component folder or plugin or module and include this file in your installer extension. This file must be named exactly as follows: betauserpoints_rule.xml+An example of userpoints_rule.xml is the file for Remository which reads: 
 + 
 +<code> 
 +<?xml version="1.0" encoding="UTF-8"?> 
 +<userpoints> 
 +    <component>com_remository</component> 
 +    <rules> 
 +        <rule> 
 +            <name>Upload</name> 
 +            <description>Give points when a new file is uploaded</description> 
 +            <plugin_function>up_remository_up</plugin_function> 
 +            <fixed_points>true</fixed_points> 
 +            <category>ot</category> 
 +            <display_message>1</display_message> 
 +            <email_notification>1</email_notification> 
 +        </rule> 
 +        <rule> 
 +            <name>Download</name> 
 +            <description>Take points when a file is downloaded</description> 
 +            <plugin_function>up_remository_down</plugin_function> 
 +            <fixed_points>false</fixed_points> 
 +            <category>ot</category> 
 +            <display_message>1</display_message> 
 +            <email_notification>1</email_notification> 
 +        </rule> 
 +    </rules> 
 +</userpoints> 
 +</code> 
  
 === Code list for categories : === === Code list for categories : ===
Line 163: Line 186:
 </code> </code>
  
-NOTE Optionally, you can avoid this step manually by filling all the fields in the creation of new rule directly in the rule manager (button 'New' in toolbar).+=== Code list for methods=== 
 + 
 +You can add a <method> tag into the XML for a rule, with the following options: 
 + 
 +<code> 
 +1 -> Only once 
 +2 -> Once a day per user 
 +3 -> Once a day for a single user on all users 
 +4 -> Whenever 
 +5 -> Once per week 
 +6 -> Once per month 
 +7 -> Once per year 
 +</code> 
  
-==== Plugin/Rule installation ====+NOTE : It is possible to create new rules manually by filling all the fields in the creation of a new rule directly in the rule manager (button 'New' in toolbar). But for the integration of other extensions with UserPoints the use of a userpoints_rule.xml file is much better.
  
-- auto-detect xml file at the root of frontend component: 
-Click on the button “Auto-detect plugins” in the control panel of UserPoints after installation of third component, plugin or module. Check regularly or periodically by clicking on this button. 
  
-==== Using UserPoints information in a third party component ====+===== Using UserPoints information in a third party component =====
  
 You can use easily the profil informations of a user directly in a third component. You can use easily the profil informations of a user directly in a third component.
Line 433: Line 467:
 * Created on 15 January 2016. * Created on 15 January 2016.
  
-* Last updated on 05 April 2023.+* Last updated on 25 November 2024.
  
  
  
  
userpoints/developer.1704909150.txt.gz · Last modified: 2024/01/10 17:52 by admin