user2317337
user2317337

Reputation:

Trying to write a simple Joomla plugin

Please help, this is my first plugin I'm writing and I'm completely lost. I'm trying to write and update information in a table in a joomla database using my custom giveBadge() function. The functions receives two different variables, the first variable is the $userID and the second one is the digit 300 which I pass at the bottom of the class using giveBadge(300). At the same comparing the $userID in the Joomla database to ensure that the number 300 is given to the current user logged in the Joomla site.

Thanks in advance.

<?php


     defined('JPATH_BASE') or die;


      class plgUserBadge extends JPlugin
      {
           public function onUserLogin () {
                $user =& JFactory::getUser();
                $userID =& user->userID;

                return $userID;
           }

           public function giveBadge ($userID, &$badgeID) {
                // Get a db connection.
                $db = JFactory::getDbo();

                // Create a new query object.
                $query = $db->getQuery(true);

                // Fields to update.
                $fields = array(
                    'profile_value=\'Updating custom message for user 1001.\'',
                    'ordering=2');

                // Conditions for which records should be updated.
                $conditions = array(
                    'user_id='.$userID, 
                    'profile_key=\'custom.message\'');

                $query->update($db->quoteName('#__user_badges'))->set($fields)->where($conditions);

                $db->setQuery($query);

                try {
                    $result = $db->query(); 
                } catch (Exception $e) {
                    // Catch the error.
                }es = array(1001, $db->quote('custom.message'), $db->quote('Inserting a record using insert()'), 1);

            }   

        }

     giveBadge(300);  //attaches to $badgeID    

    ?>

Upvotes: 0

Views: 277

Answers (1)

Valentin Despa
Valentin Despa

Reputation: 42622

Here is not going well with your code:

  • You can drop the assign by reference in all your code (&) - you really don't need it, in 99% of the cases.
  • Use an IDE (for example Eclipse with PDT). At the top of your code you have & user->userID; Any IDE will spot your error and also other things in your code.
  • Study existing plugins to understand how they work. Here is also the documentation on plugins.
  • The method onUserLogin() will automatically be called by Joomla when the specific event is triggered (when your plugin is activated). Check with a die("My plugin was called") to see if your plugin is really called
  • inside onUserLogin() you do all your business logic. You are not supposed to return something, just return true. Right now your method does absolutely nothing. But you can call $this->giveBadge() to move the logic to another method.

Upvotes: 2

Related Questions