CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Modifying the personal campaign pages to use for membership drives
Pages: [1]

Author Topic: Modifying the personal campaign pages to use for membership drives  (Read 571 times)

herb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 2
Modifying the personal campaign pages to use for membership drives
September 21, 2011, 10:11:12 am
A client wanted to create a membership drive where current members would have personal pages which the could use to recruit new members. It sounded a lot like the Personal Campaign Pages in CiviCRM (which they were already using).  We were able to make some small hacks to the PCP so that they could use it for their specific purposes.

Let's call the module Member Drive. The Client didn't need to show an individual "thermometer" or "goal amount" since there was no monetary goal for the campaign or individual. Instead the focus was on individuals competing for the top three recruiter spots, with chances to winning prizes. Instead the module shows a table of the number of recruits on the PCP. And it creates a couple blocks that display the top 3 recruiters and a block that shows all the recruiters. The data is just being shown in simple tables but could be adapted to be shown using Drupal's table functions as well.

In memberdrive.module:

Code: [Select]
<?php

/**
 * @file
 * Contains functions for Customizing the personal campaign pages for Member Drive.
 */

/*
 * Implements hook_help()
 */
function memberdrive_help( ) {
    switch (
$section) {
    case 
'admin/modules#description':
        return 
t('Customizes CiviCRM personal campaign pages for Membership drive.');
    default :
        return;
    }
}

/**
 * Implementation of hook_menu().
 */
function memberdrive_menu() {
  
$items = array();
  
 
$items['admin/settings/memberdrive'] = array(
    
'title' => 'Member Drive',
    
'page callback' => 'drupal_get_form',
    
'page arguments' => array('memberdrive_admin_settings'),
    
'access callback' => 'user_access',
    
'access arguments' => array('administer site configuration'),
'weight' => -3,
    
'description' => 'Settings for Member Drive.',
    
'type' => MENU_NORMAL_ITEM,
  );

  return 
$items;
}

/**
 * Menu callback for admin/settings/memberdrive.
 * Admin settings for setting which contribution form is active for Member Drive.
 */
function memberdrive_admin_settings() {

  
$form['memberdrive_contrib_id']  = array(
    
'#type' => 'textfield',
    
'#title' => t('Member Drive contribution Id'),
    
'#default_value' => variable_get('memberdrive_contrib_id', NULL),
    
'#description' => t('The CiviCRM contribution page id for Member Drive. Can be retrieved by going to civicrm/admin/pcp&reset=1 and hovering over the contribution page and noting the id= at end of URL'),
  );

$form = system_settings_form($form);

return $form;
}

/*
 * Add blocks for the Top 3 Recruiters and All Recruiters
 */
function memberdrive_block($op = 'list',$delta = '',$edit = array()) {
  switch (
$op) {

    case 
'list':
      
$blocks = array();
      
$blocks['top-five']['info'] = t('Top 3');
      
$blocks['list']['info'] = t('All Recruiters');
      return 
$blocks;
    case 
'view':
      switch (
$delta) {
       case 
'top-five':
        
$block['subject'] = t('Top 3 Recruiters');
        
$block['content'] = block_content_refer_top_three();
        break;
       case 
'list':
        
$block['subject'] = t('All Recruiters');
        
$block['content'] = block_content_refer();
        break;
      }
      return 
$block;
  }
}

/*
 * Creates content for Top 3 Recruiters block
 */
function block_content_refer_top_three() {
  
ob_start();
  
$content = ob_get_clean();
  
$content = '<div class="top-five">';
      
  
$rar_contrib_id = variable_get('memberdrive_contrib_id', NULL);
  
$refer_list = refer_list($rar_contrib_id);
  
$total_raised = total_raised_contribpage($rar_contrib_id);
      
  
$content .= '<table><tr><th>Rank</th><th>Name</th><th>Members Recruited</th></tr>';
  foreach (
$refer_list as $t => $o) {
    if (
$o['no'] < 4) { // only select the top 3
      
$content .= '<tr><td>';
      
$content .= '#' . $o['no'];
      
$content .= '</td><td>';
      
$content .= $o['display_name'];
      
$content .= '</td><td>';
      
$content .= $o['count'];
      
$content .= '</td></tr>';
    }
  }
  
$content .= '</table>';
  
$content .= '<h3>Total Money Raised: $' . $total_raised . '</h3></div>';
  return 
$content;
}

/*
 * Creates content for All Recruiters
 */
function block_content_refer() {
  
ob_start();
  
$content = ob_get_clean();
  
$content = '<div class="top-five">';
      
  
$rar_contrib_id = variable_get('memberdrive_contrib_id', NULL);
  
$refer_list = refer_list($rar_contrib_id);
  
$total_raised = total_raised_contribpage($rar_contrib_id);
      
  
$content .= '<table><tr><th>Rank</th><th>Name</th><th>Members Recruited</th><th>Total</th><th>Join Page</th></tr>';
  foreach (
$refer_list as $t => $o) {
      
$content .= '<tr><td>';
      
$content .= '#' . $o['no'];
      
$content .= '</td><td>';
      
$content .= l($o['display_name'], 'civicrm/contact/view', array('query' => array('cid' => $o['contact_id'], 'reset' => '1')));
      
$content .= '</td><td>';
      
$content .= $o['count'];
      
$content .= '</td><td>';
      
$content .= $o['total_amount'];
      
$content .= '</td><td>';
      
$content .= ' ' . l('view', 'civicrm/contribute/pcp/info', array('query' => array('reset' => '1', 'id' => $o['pcp_id'])));
      
$content .= '</td></tr>';
  }
  
$content .= '</table>';
  
$content .= '<h3>Total Money Raised: $' . $total_raised . '</h3></div>';
  return 
$content;
}

/**
 * Implements hook_user()
 * Adds link to Member drive pages on the Account page
 */
function memberdrive_user($op, &$edit, &$account, $category = NULL) {
  global 
$user;
  switch (
$op) {
    case 
'view':
      
$rar_pages = '';
      
$pc_pages = pc_pages($account->uid);
      foreach (
$pc_pages as $r => $a) {
        
$rar_pages .= '<a href="/civicrm/contribute/pcp/info?reset=1&id=' . $a['id'] . '">&raquo; ' . $a['title'] . '</a><br/>';
      }
      if (
$pc_pages) {
        
$account->content['memberdrive'] = array(
          
'#type' => 'user_profile_category',
          
'#weight' => -5,
          
'#title' => t('Member Drive'),
          
'link' => array(
            
'#type' => 'user_profile_item',
            
'#value' => $rar_pages,
          ),
        );
      }

      
// hide Summary if not admin
      
if (!user_access('administer users')) {
        unset(
$account->content['summary']);         
      }

      break;
  }
}

/**
 * Implements of hook_civicrm_buildForm()
 * Disable some fields when member creates a PCP page.
 */
function memberdrive_civicrm_buildForm($fname, &$form) {

  require_once 
"CRM/Core/Error.php";

  
$display_forms = array(
      
'CRM_Contribute_Form_PCP_Campaign',
  );
  if (
in_array($fname, $display_forms)) {

      
drupal_add_js(drupal_get_path('module','memberdrive') . '/memberdrive.js');

      
// set defaults which won't be used b/c the goal is to be
      // one of the top recruiters, not raise money
      
if (empty($defaults['goal_amount'])) { $defaults['goal_amount'] = "1.00"; }
      
$defaults['is_thermometer'] = "0";
      
$defaults['is_honor_roll'] = "1";
      
$defaults['donate_link_text'] = "Join!";
      
      
$form->setDefaults($defaults);
      
      
// freeze first name, last name and grade
    
$elementList = array( 'goal_amount', 'is_thermometer', 'is_honor_roll', 'donate_link_text' );
    
$form->freeze( $elementList );

    return;
  }  


}

/*
 * Implementation of hook_civicrm_pagerun()
 * 
 * Adds Member Drive data to personal campaign pages
 */
function memberdrive_civicrm_pageRun( &$page ) {
  
// You can assign variables to the template using:
  // $page->assign( 'varName', $varValue );
  // in your template, {$varName} will output the contents of $varValue
  // you should customize your template if doing so

  //$page->assign( 'varName', 'This is a variable assigned by the hook' );
  // Not going to use this since we'll just add the text to an existing variable
    
  
civicrm_initialize( true );
  require_once 
"CRM/Core/Error.php";
  
  
$pagename = $page->getVar('_name'); 
  
  if (
$pagename == "CRM_Contribute_Page_PCPInfo") {
    
//CRM_Core_Error::debug( 'page', $page );
    
    // get the personal campaign page id so we can find out the rank,
    // money raised and members recruited by the owner of the page
    
$pcpid = $page->getVar('_id');
    
$recruited = members_recruited($pcpid);
    
$rank = rank($pcpid);
    
$total_raised = total_raised_pcp($pcpid);
    
//CRM_Core_Error::debug( 'rank', $rank );
    
    // get the details from smarty
    
$smarty  =& CRM_Core_Smarty::singleton( );
    
$details =& $smarty->get_template_vars( 'pcp' );
    
    
$details['page_text'] .= '<div class="rank"><table><tr><td><strong>My Rank:</strong></td><td>#';
    
$details['page_text'] .= $rank;
    
$details['page_text'] .= '</td></tr><tr><td><strong>Number of members recruited:</strong></td><td>';
    
$details['page_text'] .= $recruited;
    
$details['page_text'] .= '</td></tr><tr><td><strong>Money raised so far:</strong></td><td>';
    
$details['page_text'] .= '$' . $total_raised;
    
$details['page_text'] .= '</td></tr></table><a href="/refer-rider">See Refer-a-Rider campaign</a>';
    
$details['page_text'] .= ' | <a href="/contact?edit[subject]=Report%20Refer%20a%20Rider%20page&edit[message]=Reporting%20this%20page%3A%0D';
    
$details['page_text'] .= $current_url . '%0D%0D&' . $destination;
    
$details['page_text'] .= '">Report this page</a></div>';

    
// CRM_Core_Error::debug( 'POST', $details );
    
$smarty->assign_by_ref( 'pcp', $details );
  }

}

/*
 * Provides number of members recruited through the personal campaign page. 
 */
function members_recruited($pcpId) {
  
  require_once 
'CRM/Core/DAO.php';
  
  
$query = "SELECT count(cc.id)
            FROM civicrm_contribution cc 
                 LEFT JOIN civicrm_contribution_soft cs ON cc.id = cs.contribution_id
            WHERE cs.pcp_id = %1 
                  AND contribution_status_id = 1 
                  AND is_test = 0"
;
        
        
$params = array( 1 => array( $pcpId, 'Integer' ) );
        return 
CRM_Core_DAO::singleValueQuery( $query, $params );
}

/*
 * Show the list of campaigners, recruitments and ranks
 * 
 * @ $contribId
 * return $list
 */
function refer_list($contribId) {
  
  
civicrm_initialize( true );
  require_once 
'CRM/Core/DAO.php';
 
  
//then get a list of all contributions by pcp for that contribution page

  
$query = "SELECT count(cc.id) AS count, SUM(cc.total_amount) as total_amount, cs.pcp_id, co.display_name, co.id
            FROM civicrm_contribution cc 
                 LEFT JOIN civicrm_contribution_soft cs ON cc.id = cs.contribution_id
                 LEFT JOIN civicrm_pcp cp ON cs.pcp_id = cp.id
                 LEFT JOIN civicrm_contact co ON cp.contact_id = co.id
            WHERE cp.contribution_page_id = %1
                  AND contribution_status_id = 1 
                  AND is_test = 0
                  GROUP BY cs.pcp_id
                  ORDER BY count DESC, total_amount DESC"
;
  
$params = array( 1 => array( $contribId, 'Integer' ) );        
  
$dao = CRM_Core_DAO::executeQuery( $query, $params );
  
$count = 0;
  while( 
$dao->fetch() ) {
    
$count++;
    
$list[$dao->pcp_id]['pcp_id'] = $dao->pcp_id;
    
$list[$dao->pcp_id]['contact_id'] = $dao->id;
    
$list[$dao->pcp_id]['no'] = $count;
    
$list[$dao->pcp_id]['count']     = $dao->count;
    
$list[$dao->pcp_id]['total_amount']     = $dao->total_amount;
    
$list[$dao->pcp_id]['display_name'] = $dao->display_name;
  }
  return 
$list;
}

/*
 * Provides individual rank
 * 
 * @ $pcpId
 * return $rank
 */
function rank($pcpId) {
  
  require_once 
'CRM/Core/DAO.php';
  
  
//get the contribution page
  
$query = "SELECT `contribution_page_id` FROM `civicrm_pcp` WHERE id = %1";
  
$params = array( 1 => array( $pcpId, 'Integer' ) );
  
$contrib_page_id = CRM_Core_DAO::singleValueQuery( $query, $params );
 
  
//then get a list of all contributions by pcp for that contribution page

  
$query = "SELECT count(cc.id) AS count, cs.pcp_id, SUM(cc.total_amount) as total_amount,
            FROM civicrm_contribution cc 
                 LEFT JOIN civicrm_contribution_soft cs ON cc.id = cs.contribution_id
                 LEFT JOIN civicrm_pcp cp ON cs.pcp_id = cp.id
                 LEFT JOIN civicrm_contact co ON cp.contact_id = co.id
            WHERE cp.contribution_page_id = %1
                  AND contribution_status_id = 1 
                  AND is_test = 0
                  GROUP BY cs.pcp_id
                  ORDER BY count DESC, total_amount DESC"
;
  
$params = array( 1 => array( $contrib_page_id, 'Integer' ) );        
  
$dao = CRM_Core_DAO::executeQuery( $query, $params );
  
$count = 0;
  while( 
$dao->fetch() ) {
    
$count++;
    if (
$dao->pcp_id == $pcpId) {
      
$rank = $count;
    }
  }
  return 
$rank;
}

/*
 * Total money raised for personal campaign page
 * 
 * @ $pcpId
 * return money raised for pcp
 */
function total_raised_pcp($pcpId) {
  
  require_once 
'CRM/Core/DAO.php';
  
  
$query = "SELECT SUM(cc.total_amount) as total
            FROM civicrm_pcp pcp 
              LEFT JOIN civicrm_contribution_soft cs ON ( pcp.id = cs.pcp_id ) 
              LEFT JOIN civicrm_contribution cc ON ( cs.contribution_id = cc.id)
            WHERE pcp.id = %1 
              AND cc.contribution_status_id =1 
              AND cc.is_test = 0"
;
        
  
$params = array( 1 => array( $pcpId, 'Integer' ) );
  return 
CRM_Core_DAO::singleValueQuery( $query, $params );
}

/*
 * Total money raised by contribution page
 * 
 * @ $contribId
 * return money raised for contrib page
 */
function total_raised_contribpage($contribId) {
  
  require_once 
'CRM/Core/DAO.php';

  
  
$query = "SELECT SUM(cc.total_amount) as total
            FROM civicrm_pcp pcp 
              LEFT JOIN civicrm_contribution_soft cs ON ( pcp.id = cs.pcp_id ) 
              LEFT JOIN civicrm_contribution cc ON ( cs.contribution_id = cc.id)
            WHERE pcp.contribution_page_id = %1 
              AND cc.contribution_status_id =1 
              AND cc.is_test = 0"
;
        
  
$params = array( 1 => array( $contribId, 'Integer' ) );
  return 
CRM_Core_DAO::singleValueQuery( $query, $params );
}

/*
 * All pcp pages for a user
 * 
 * @ $ufId
 */
function pc_pages($ufId) {
  
  require_once 
'CRM/Core/DAO.php';

  
$query = "SELECT pcp.id, pcp.title, pcp.status_id, pcp.contribution_page_id
            FROM civicrm_pcp pcp 
              LEFT JOIN civicrm_uf_match cu ON ( pcp.contact_id = cu.contact_id ) 
            WHERE pcp.is_active = 1
            AND pcp.status_id = 2
            AND cu.uf_id = %1"
;

        
  
$params = array( 1 => array( $ufId, 'Integer' ) );        
  
$dao = CRM_Core_DAO::executeQuery( $query, $params );

  while( 
$dao->fetch() ) {
    
$list[$dao->id]['id'] = $dao->id;
    
$list[$dao->id]['title']     = $dao->title;
    
$list[$dao->id]['status_id'] = $dao->status_id;
  }
  return 
$list;
}

In memberdrive.js
Code: [Select]
$(document).ready(function(){

  $('.crm-contribution-form-block-is_thermometer').hide();
  $('.crm-contribution-form-block-donate_link_text').hide();
  $('.crm-contribution-form-block-goal_amount').hide();

});

In memberdrive.install
Code: [Select]
<?php

/**
 * @file
 * Contains install and update functions for Member Drive.
 */


/**
 * Implementation of hook_install()
 */
function memberdrive_install() {
}

/**
 * Implementation of hook_uninstall()
 */
function memberdrive_uninstall() {
variable_del('memberdrive_contrib_id');
}
Web Developer
Freeform Solutions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Modifying the personal campaign pages to use for membership drives

This forum was archived on 2017-11-26.