Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Messing with activities

AviramAviram
edited December 2013 in Development

Hello guys, it's again me with another research going on.

I'm trying to understand how to create an activity, but the documentation is empty on this one and the vanilla source code is getting me confused.

At first I saw this usage in commentmodel:

</p> <pre><code> $ActivityModel = new ActivityModel(); $Activity = array( 'ActivityType' => 'Comment', 'ActivityUserID' => $Fields['InsertUserID'], 'HeadlineFormat' => $HeadlineFormat, 'RecordType' => 'Comment', 'RecordID' => $CommentID, 'Route' => "/discussion/comment/$CommentID#Comment_$CommentID", 'Data' => array( 'Name' => $Discussion->Name, 'Category' => GetValue('Name', $Category) ) ); $ActivityModel->Queue($Activity, 'DiscussionComment');


OK so from this I understand that there are the following fields:

ActivityType= Probably one of the activities from the ActivityType table in the database, it finds it based on ActivityName? Do I have to add an activity type?

ActivityUserID = Who "initiated" the activity.

Headlineformat = What message to display.

RecordType = Unsure about this.. is this like the type of the record that calls the activity, as if I were to notify about something in a discussion I would put Discussion?

RecordID= the ID of where activity took place.

Route= link to the thing we notify about

Data= ??? Used for formatting the message?

So now I read the discussionmodel usage:
</p> <pre><code> $Activity = array( 'ActivityType' => 'Discussion', 'ActivityUserID' => $Fields['InsertUserID'], 'HeadlineFormat' => $HeadlineFormat, 'RecordType' => 'Discussion', 'RecordID' => $DiscussionID, 'Route' => DiscussionUrl($Fields), 'Data' => array( 'Name' => $DiscussionName, 'Category' => GetValue('Name', $Category) ) ); ................... $Activity['HeadlineFormat'] = T('HeadlineFormat.Mention', '{ActivityUserID,user} mentioned you in <a href="{Url,html}">{Data.Name,text}</a>'); $Activity['NotifyUserID'] = GetValue('UserID', $User); $ActivityModel->Queue($Activity, 'Mention');


OK Now we see the ActivityType is Discussion, but when it queues it puts in second parameter Mention?

I go to the ActivityType Table, no activitytype called Mention? Only DiscussionMention and CommentMention

but ActivityType in the array is 'Discussion'? so neither options I had in mind that are used to identify ActivityType are invalid in this case..

Also what's the RouteCode in ActivityType table?

Thanks in advance! and sorry if I got you an headache

Comments

  • hgtonighthgtonight ∞ · New Moderator

    The route is the URL the activity should link to. In this case it is the discussions URL.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

    Aviram
  • @hgtonight:
    I asked about the RouteCode column in the table.

  • LincLinc Director of Development Detroit Vanilla Staff

    You need a better IDE for code inspection, methinks. Go to the 'Queue' method in the ActivityModel to learn about the second parameter ('Mention').

    Your inferences about RecordType and Data are correct.

    'Discussion' and 'Comment' are special ActivityTypes used for the Advanced Notifications, I believe.

    UnderDog
  • hgtonighthgtonight ∞ · New Moderator

    @Lincoln, mind sharing some insight on the differences between notifications and advanced notifications?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • businessdadbusinessdad Stealth contributor MVP

    @hgtonight said:
    Lincoln, mind sharing some insight on the differences between notifications and advanced notifications?

    My finding, with Vanilla 2.0.x was that "advanced notifications" allows users to receive a notifications when a new discussion is posted in a forum. It would be interesting to see if they can do more than that.

  • LincLinc Director of Development Detroit Vanilla Staff

    Yes, Garden.AdvancedNotifications.View is the permission governing the ability to receive per-category notifications of any new discussions and/or comment. They operate differently than the normal notifications under Edit Preferences and appear under the sub-heading "Category Notifications".

    As an aside, Advanced Notifications does not scale. It's for admins & mods only.

  • @Lincoln,

    Thanks for your comment. I'll try the things out.

    About IDE, do you have any recommendation ? until now I used gedit .. I tried some of the PHP IDES but they were pretty useless in other development enviroments (not vanilla)

  • peregrineperegrine MVP
    edited December 2013

    @Aviram said:
    Lincoln,

    Thanks for your comment. I'll try the things out.

    About IDE, do you have any recommendation ? until now I used gedit .. I tried some of the PHP IDES but they were pretty useless in other development enviroments (not vanilla)

    try netbeans, easier to set up than eclipse for php, it seems. I use grep mostly though.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • hgtonighthgtonight ∞ · New Moderator

    @Aviram said:
    About IDE, do you have any recommendation ?

    I am really liking netbeans 7.4. So there is another vote for it.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • LincLinc Director of Development Detroit Vanilla Staff

    @Aviram Several Vanilla devs use NetBeans. I find PhpStorm to be the best.

    UnderDogShadowdare
  • If I skip adding an activity to the database and I use the comment and discussion activity types, would that cause issues later on?

  • NVM my last comment, I just added tables and used them.
    I cannot find tho how to add a preference so I can use it when calling Save .. so right now I'm using DiscussionComment in order to make it notify users

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @hgtonight said:

    Did you want each user to be able to configure their notifications? I see you have it listed as a todo at top. If you want to add in a notification preference, use something like below:

    public function ProfileController_AfterPreferencesDefined_Handler($Sender) {
      $Sender->Preferences['Notifications']['Popup.ProfileVisitors'] = T('Notify me when people visit my profile.');
      // Doesn't seem like you would want an email notification, but this is how to add it anyway
      // $Sender->Preferences['Notifications']['Email.ProfileVisitors'] = T('Notify me when people visit my profile.');
    }
    

    Not quite sure what you are talking about, but maybe that piece of code could help you. Do you want to create a custom Activity? I've tried that recently and gave it up, but here's what I've found out (you would have to change "ProfileVisitors" in the above by "YourActivity" name).

    First you have to create a new Activity. There seems to be no function for that, so you have to use SQL. I've no clue what each of the columns do but I add my guesses

    // check if there is already an activity type for YourActivity
    if ($SQL->GetWhere('ActivityType', array('Name' => 'YourActivity'))->NumRows() == 0)
       // if not, create it.
       $SQL->Insert('ActivityType', // table to insert into
          array('Name' => 'YourActivity', 
             'AllowComments' => '0', // allow users to comment below activities
             'FullHeadline' => 'FullHeadline: %1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s.', // the message to show. See below for the meaning of the variables. Not sure when Full- and when ProfileHeadline is shown
             'ProfileHeadline' => 'ProfileHeadline: %1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s.',
             'RouteCode' => 'profile', // I guess it's a link to the controller?
             'Notify' => '1', // maybe the default behaviour?
             'Public' => '0' // 1 will make it a wall post that anyone can see
          ));
    

    Here are the variables

    • %1 = ActivityName
    • %2 = ActivityName Possessive: Username
    • %3 = RegardingName
    • %4 = RegardingName Possessive: Username, his, her, your
    • %5 = Link to RegardingName's Wall
    • %6 = his/her
    • %7 = he/she
    • %8 = RouteCode & Route

    Then you have the function AddActivity in functions.general.php that you can use

    if (!function_exists('AddActivity')) {
       /**
    * A convenience function that allows adding to the activity table with a single line.
    */
       function AddActivity($ActivityUserID, $ActivityType, $Story = '', $RegardingUserID = '', $Route = '', $SendEmail = '') {
          $ActivityModel = new ActivityModel();
          return $ActivityModel->Add($ActivityUserID, $ActivityType, $Story, $RegardingUserID, '', $Route, $SendEmail);
       }
    }
    

    But I do not know if there is anything else to take into consideration

    Aviram
  • Hey @R_J !

    Thanks for your comment. I've successfully added an activity and used it but I can't figure how to add it's own preferences, so when I call the Save method on ActivityModel it'd notify the user or not notify, depending on his specific settings for that type of activity. for now I'm using the DiscussionComment perference and my own activity.

    I guess I would want to change it if it's possible so it will have seperate notification preference.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Have you seen the first snippet of code? Add this to your plugin and a user can set notifications preferences for your activity in his profile:

    public function ProfileController_AfterPreferencesDefined_Handler($Sender) {
       $Sender->Preferences['Notifications']['Popup.YourActivity'] = T('Whatever it is good for');
       $Sender->Preferences['Notifications']['Email.YourActivity'] = T('Whatever it is good for');
    }
    
  • Oh I didn't notice! I'll try that out.

    How do I set the default preferences to be notify, or I don't need to do that?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Please tell me, if you know how ;)

    But from looking at the code of the activitymodel it could be that you have to call SaveToConfig('Preferences.Email.YourActivity', TRUE); and SaveToConfig('Preferences.Popup.YourActivity', TRUE); in your Setup() and that's it.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Got it working :-)

    <?php if (!defined('APPLICATION')) exit();
    
    $PluginInfo['ActivityTest'] = array(
       'Name' => 'Activity Test',
       'Description' => 'Test Activity and Notification Feature',
       'Version' => '0.1',
       'Author' => 'Robin'
    );
    
    class ActivityTestPlugin extends Gdn_Plugin {
       public function Setup() {
          $Sql = Gdn::SQL();
          // check if there is already an activity type for YourActivity
          if ($Sql->GetWhere('ActivityType', array('Name' => 'ActivityTest'))->NumRows() == 0) {
             // if not, create it.
             $Sql->Insert('ActivityType', // table to insert into
                array('Name' => 'ActivityTest',
                   'AllowComments' => '0', // allow users to comment below activities
                   'FullHeadline' => '%1$s looked at a %8$s.', // the message to show. See below for the meaning of the variables. Not sure when Full- and when ProfileHeadline is shown
                   'ProfileHeadline' => 'ProfileHeadline: %1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s.',
                   'RouteCode' => 'Discussion', // Name of the link (Route) that could be passed to the activity
                   'Notify' => '1', // maybe the default behaviour?
                   'Public' => '1' // 1 will make it a wall post that anyone can see
                ));
          }
          SaveToConfig('Preferences.Popup.ActivityTest', TRUE);
          SaveToConfig('Preferences.Email.ActivityTest', TRUE);
       }
       public function ProfileController_AfterPreferencesDefined_Handler($Sender) {
          $Sender->Preferences['Notifications']['Popup.ActivityTest'] = T('Notify me when test event happens');
          $Sender->Preferences['Notifications']['Email.ActivityTest'] = T('Notify me when test event happens');
       }
    
       public function DiscussionController_Render_Before($DiscussionController) {
          $DiscussionID = $DiscussionController->DiscussionID;
          $ActivityUserID = Gdn::Session()->UserID; // current user
    
          $ActivityType = 'ActivityTest';
          $Story = 'Some additional text to display under the Headline'; // use default Story
          $RegardingUserID = 1; // in this case the admin
          $CommentActivityID = '';
          $Route = '/discussion/'.$DiscussionID;
          $SendEmail = ''; // use default setting
    
          $ActivityModel = new ActivityModel();
          $ActivityID = $ActivityModel->Add(
             $ActivityUserID,
             $ActivityType,
             $Story,
             $RegardingUserID,
             $CommentActivityID,
             $Route,
             $SendEmail
          );
          $ActivityModel->SendNotification($ActivityID, $Story);
       }
    }
    
    UnderDogSkullduggery
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    I have to add: it is working somehow, but I do not know how exactly

    UnderDogAviram
  • hgtonighthgtonight ∞ · New Moderator

    I recently worked with activities on Yaga. This is the code I use to notify people of badge awards:

    // Record some activity
    $ActivityModel = new ActivityModel();
    
    $Activity = array(
      'ActivityType' => 'BadgeAward',
      'ActivityUserID' => Gdn::Session()->UserID,
      'RegardingUserID' => $UserID,
      'Photo' => '/uploads/' . $Badge->Photo,
      'RecordType' => 'Badge',
      'RecordID' => $BadgeID,
      'Route' => '/badges/detail/' . $Badge->BadgeID . '/' . Gdn_Format::Url($Badge->Name),
      'HeadlineFormat' => T('Yaga.HeadlineFormat.BadgeEarned'),
      'Data' => array(
        'Name' => $Badge->Name
      ),
      'Story' => $Badge->Description
    );
    
    $ActivityModel->Queue($Activity);
    
    // Notify the user of the award
    $Activity['NotifyUserID'] = $UserID;
    $Activity['Emailed'] = ActivityModel::SENT_PENDING;
    $ActivityModel->Queue($Activity, 'Badges', array('Force' => TRUE));
    

    As far as I can tell, this forces the activity to abide by the user's notifications preferences.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

    AviramSkullduggery
  • @hgtonight

    I think your way is a "hack" way, and it seems like @R_J is the correct usage, but then again. if it works why break it? :)

    R_J Thanks for the example, I will use it!

  • Hey guys, I've noticed that in other activities that if they stack up for same Regard it just puts them all in one Activity rather than each activity on it's on, I wonder how is that possible?

  • ShadowdareShadowdare π Moderator
    edited January 2014

    @Aviram said:
    About IDE, do you have any recommendation ?

    When I simply want to edit files and don't need the features of an IDE, I use Notepad++ and Sublime Text.

    I used NetBeans before and it provides a lot of features most people look for in an IDE. I like NetBeans because it's free and has a lot of features, but the interface feels clunky and slow, so I started looking into other IDEs recently. A lot of other IDEs that support PHP are also built with Java and feel clunky.

    I tried Komodo IDE, which isn't built with Java, and found that has a very clean interface and feels snappy, but it's missing some features that I find useful in other IDEs and it doesn't do some things as well as NetBeans.

    For example, the code completion feature isn't as streamlined as the other IDEs and adding a PHP include for code completion scanning can't be done per-project, unless you create symbolic links or copy the includes into the project folder. Also, there is no built-in PHP formatting feature for cleaning up code, so you would have to use an external script which is tedious to configure.

    I have started using PHPStorm recently and found that it has a lot of nice features. While it isn't as light as Komodo IDE, it's less clunky and more modern looking compared to NetBeans. I agree with @Lincoln; PHPStorm is fantastic!

    Add Pages to Vanilla with the Basic Pages app | Publish articles with the Articles app

    hgtonight
  • Old thread resurrection. First off, this thread is awesome. I was looking all over trying trying to find how to do this.

    I'm placing the following lines into a plugin and having some issues, I'll skip the setup() portion and go right to the part that's failing. I'm trying to do 2 notifications, and only the first one is succeeding, I assume it's because of some of the variables I'm using for the second one are not getting what they need.

    Here's the portion that's working:

          // notify the player that they have been drafted   
          $ActivityUserID = Gdn::Session()->UserID; // current user
          $ActivityType = 'PlayerDrafted';
          $Story = 'You have been drafted';
          $RegardingUserID = $PlayerID; // The player drafted
          $CommentActivityID = '';
          $Route = '';
          $SendEmail = '1'; // Send email
          $ActivityModel = new ActivityModel();
          $ActivityID = $ActivityModel->Add(
             $ActivityUserID,
             $ActivityType,
             $Story,
             $RegardingUserID,
             $CommentActivityID,
             $Route,
             $SendEmail
          );
          //  $ActivityModel->SendNotification($ActivityID, $Story);
          // lets try Queue instead
          $ActivityModel->Queue($ActivityID, $Story);
    

    So that works with using either SendNotification() or Queue()

    Here's the portion that's not working:

          // notify the owner that the have drafted a player
          $td_ActivityUserID = Gdn::Session()->UserID; // already did this
          $td_ActivityType = 'TeamDrafted';
          $PlayerGT = $Sender->User->Gamertag;  // this does not appear to be working
          $td_Story = 'The ' . $OwnerTeam . ' have drafted ' . $PlayerGT;  // or perhaps this isnt working...
          $td_RegardingUserID = $OwnerID; // The owner
          $td_CommentActivityID = ''; // already did this
          $td_Route = '';  // already did this
          $td_SendEmail = '1'; // Send email
          $td_ActivityModel = new ActivityModel();
          $td_ActivityID = $td_ActivityModel->Add(
             $td_ActivityUserID,
             $td_ActivityType,
             $td_Story,
             $td_RegardingUserID,
             $td_CommentActivityID,
             $td_Route,
             $td_SendEmail
          );
          $td_ActivityModel->Queue($td_ActivityID, $td_Story);
    

    What's the best way to test whether $PlayerGT, or $td_Story are getting the right information? Just echoing them?

  • hgtonighthgtonight ∞ · New Moderator

    var_dump($PlayerGT); die(); is quick and easy.

    You can also run a PHP debugger and set a breakpoint.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

    Skullduggery
  • Thanks, I'll try that.

    Further to write things to the activity table... It doesn't seem like using new ActivityModel() will make things show up publicly on the Activity page. Was that the case for you guys? For my one notification that works, it doesnt show up on the activity page. Perhaps I'm still doing it wrong.

    Since this is an older thread, I'm wondering if there is any newer information on how to send notifications/have them show on the activity page. Thanks

Sign In or Register to comment.