The Joomla! Community Magazine™

JomSocial & K2 integration (Joomla! 1.7) - Part 1

Written by | Thursday, 01 December 2011 00:00 | Published in 2011 December

Here is a step-by-step tutorial on how-to:

1. Make entry in the JomSocial feed on new/edited article.
2. Make entry in the JomSocial feed on new comment.
3. Add user points for new article.
4. Add user points for edited article.
5. Add user points for new comment.

The tutorial:

1. Make a backup. Whenever you change files, please make a backup first.
2. Open the file "administrator/components/com_k2/models/item.php"
In that file find:
case 'save':
default:

Right after it, place the following code:
// Hack to add new or edited articles in the JomSocial activity stream
// Check if JomSocial is installed
$jcheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'core.php';
// Check if the JomSocial library exist and if so, include it
if ( file_exists($jcheck)) {
require_once($jcheck);
}
// Get the current article title
$contentTitle = $row->title;
// Get the current article url
$link = 'index.php?option=com_k2&view=item&id='.$row->id;
// Add proper routing
$link = K2HelperRoute::getItemRoute($row->id.':'.urlencode($row->alias),$row->catid.':'.urlencode($row->category->alias));

// Post to JomSocial activity stream
$item->link=$link;
$act = new stdClass();
$act->cmd = 'wall.write';
$act->actor = $user->id;
$act->target = 0; // no target
if($isNew) {
$act->title = JText::_('K2_JOMSOCIAL_ARTICLE').' <a href="'.$link.'">'.$contentTitle.'</a>';
// Add user points for new article
if ( file_exists($jcheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.article.new');
}
}
else {
// Add user points for edited article
$act->title = JText::_('K2_JOMSOCIAL_EDIT').' <a href="'.$link.'">'.$contentTitle.'</a>';
if ( file_exists($jcheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.article.edit');
}
}
$act->content = '';
$act->app = 'wall';
$act->cid = 0;

CFactory::load('libraries', 'activities');
// Add support for like and comments in JomSocial activity stream
$act->comment_type  = 'k2.article';
$act->comment_id    = CActivities::COMMENT_SELF;
$act->like_type     = 'k2.article';
$act->like_id     = CActivities::LIKE_SELF;
CActivityStream::add($act);
// End JomSocial activity stream hack

Note: The code above does everything necessary for Item adding and editing in the frontend. If you want to fully cover the backend adding and editing articles, add the same code after:
case 'apply':

and after
case 'saveAndNew':

3. Open the file "components/com_k2/models/item.php"

Find in it:
if ($params->get('commentsPublishing')) {

And add right after it the following code:
// check if JomSocial is installed
$jscheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'core.php';

//activity stream  - added a comment
if ( file_exists($jscheck)) {
require_once($jscheck);
}

$contentTitle = $item->title;
$link = K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias),$item->catid.':'.urlencode($item->category->alias));

//$item->link=urldecode(JRoute::_($link));
$item->link=$link;

$act = new stdClass();
$act->cmd    = 'wall.write';
$act->actor    = $user->id;
$act->target    = 0; // no target
$act->title    = JText::_('K2_JOMSOCIAL_COMMENT').' <a href="'.$item->link.'">'.$contentTitle.'</a>';
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.comment.add');
$act->content    = '';
$act->app    = 'wall';
$act->cid    = 0;
      
CFactory::load('libraries', 'activities');
$act->comment_type  = 'k2.comment';
$act->comment_id    = CActivities::COMMENT_SELF;
$act->like_type     = 'k2.comment';
$act->like_id     = CActivities::LIKE_SELF;
CActivityStream::add($act);

4. Create a new file named "jomsocial_rule.xml" with the content bellow and upload it to "components/com_k2/"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE jomsocial>
<jomsocial>
<component>com_k2</component>
<rules>
<rule>
<name>Add Article</name>
<description>Add New Blog Post.</description>
<action_string>com_k2.article.new</action_string>
<publish>true</publish>
<points>10</points>
<access_level>1</access_level>
</rule>
<rule>
<name>Edit Article</name>
<description>Edit New Blog Post.</description>
<action_string>com_k2.article.edit</action_string>
<publish>true</publish>
<points>1</points>
<access_level>1</access_level>
</rule>
<rule>
<name>Post Comment</name>
<description>Post New Blog Comment.</description>
<action_string>com_k2.comment.add</action_string>
<publish>true</publish>
<points>1</points>
<access_level>1</access_level>
</rule>
</rules>
</jomsocial>

5. Add the following lines at the bottom of "language/en-GB/en-GB.com_k2.ini" (or in your respective language):
;JomSocial
K2_JOMSOCIAL_COMMENT="{actor} commented on the article"
K2_JOMSOCIAL_ARTICLE="{actor} added the new article"
K2_JOMSOCIAL_EDIT="{actor} edited the article"


6. Go to JomSocial in the backend, there go to User Points, click on "Rule Scan" and it should find the three new rules (add article, edit article, add comment). Click on refresh. By default it gives 10 points for new article, 1 for edited article and 1 for comment. You can edit the points after refresh and clicking on the name of the rule.

That's it. Now when your users add or edit content or add comments, they will get points and this will appear in their stream in JomSocial.
We will try to write a tutorial on how to display JomSocial data in K2 items.

P.S. If You have any troubles or questions please feel free to ask a question at our forum post

Read 51477 times
Tagged under Administrators