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) »
  • Email notification when Goal Amount reached.
Pages: [1]

Author Topic: Email notification when Goal Amount reached.  (Read 2023 times)

alextronic

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 2
  • I do what I do
Email notification when Goal Amount reached.
December 14, 2009, 05:22:42 pm
Hi there,

How can I (admin) get an email notification from the system when a contribution page's Goal Amount has been reached? It's a very important moment and we'd like to know right away.

Thanks again

Alejandro

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Email notification when Goal Amount reached.
December 14, 2009, 07:26:28 pm

your best bet would be to implement the civicrm post hook on the contribution object.

you can compute the total amount for a contribution page and then send email if needed

http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

alextronic

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 2
  • I do what I do
Re: Email notification when Goal Amount reached.
December 15, 2009, 02:01:02 pm
What's the problem with this? Doesn't seem to work (using dummy processor); the confirmation message/email comes to me, but I don't get the notifying email from the module...

Code: [Select]
<?php
// $Id: civinotify.module,v 1.0 2009/12/15 14:35:00 quicksketch Exp $

/**
 * Implementation of hook_civicrm_post()
 */

function civinotify_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if ( $objectName == 'Contribution' && $op == 'create' ) {

$pageID = $objectId;

$query = "
SELECT p.goal_amount as goal, sum( c.total_amount ) as total
FROM civicrm_contribution_page p,
     civicrm_contribution      c
WHERE p.id = c.contribution_page_id
     AND p.id = %1
     AND c.cancel_date is null
GROUP BY p.id
"
;

$config =& CRM_Core_Config::singleton( );
$params = array( 1 => array( $pageID, 'Integer' ) );
$dao =& CRM_Core_DAO::executeQuery( $query, $params );

if ( $dao->fetch( ) ) {
$goal = $dao->goal;
$total = $dao->total;

if ($total >= $goal) {
drupal_set_message("congrats!! your contribution helped finish this project!! etc etc");
$text = $objectRef;
$emailadd = "email@email.org>";
mail("my_email@gmail.com", "Someone has Fully Funded a contribPage!", $text, "From: ".$emailadd."\r\nContent-type: text/html\r\nContent-type: text/html\r\n");
}

}
}

}

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Email notification when Goal Amount reached.
December 15, 2009, 04:35:51 pm

did u enable the civinotify module in drupal?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

alextronic

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 2
  • I do what I do
Re: Email notification when Goal Amount reached.
December 15, 2009, 08:18:55 pm
Thank you Lobo.

Yes, I did enable it; but instead, the problem (well, the first problem), was the condition if ( $dao->fetch( ) ) {, I don't know how important that was, but I removed it and then I could go forward. Don't really understand very well why, in fact I still don't know if reverting to it...

Now, the (now minor) problem seems to be in how to use the values of the object resulting from the query. It seems they can't be directly used to work with them. This is my new code, note I added more columns in SELECT clause:

Code: [Select]
<?php
// $Id: civinotify.module,v 1.0 2009/12/15 14:35:00 quicksketch Exp $

/**
 * Implementation of hook_civicrm_post()
 */

function civinotify_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if ( 
$objectName == 'Contribution' && $op == 'create' ) {

$pageID = $objectId;

$query = "
SELECT p.goal_amount as goal, sum( c.total_amount ) as total, c.total_amount as total_amount, c.source as source
FROM civicrm_contribution_page p,
     civicrm_contribution      c
WHERE p.id = c.contribution_page_id
     AND p.id = %1
     AND c.cancel_date is null
GROUP BY p.id
"
;

$config =& CRM_Core_Config::singleton( );
$params = array( 1 => array( $pageID, 'Integer' ) );
$dao =& CRM_Core_DAO::executeQuery( $query, $params );

$goal = $dao->goal;
$total = $dao->total;
$source = $dao->source;
$amount = $dao->total_amount;

if ( ($total >= $goal) && ($total-$amount < $goal)  ) {
drupal_set_message("Congratulations! Your donation has contributed to fully fund this project! Thank you!");
$subject = "A donor has completed '".$source."' at XXXXX.org";
$text = "A contribution of $".$amount." has been made to the following contribution page: '".$source."'. This donation has closed the project by reaching its Goal Amount of $".$goal.". Automatic message sent by XXXXX.org";
$emailadd = "XXXXX.org <noreply@xxxxxxx.org>";
mail("my_email@gmail.com", $subject, $text, "From: ".$emailadd."\r\nContent-type: text/html\r\nContent-type: text/html\r\n");
}

}

}

....but when I make a contribution not even the drupal_set_message pops up... Ouch!

NOTE: The line if ( ($total >= $goal) && ($total-$amount < $goal)  ) { allows to know if that contribution is the one that reached the goal amount since people might still keep donating after it has been reached.

So I feel I'm about to give the last step, I'm learning very interesting stuff today (as a n00b).

Thanks again

Alejandro
« Last Edit: December 15, 2009, 08:57:29 pm by alextronic »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Email notification when Goal Amount reached.
December 15, 2009, 09:27:49 pm

hey alex:

1. you do need the $dao->fetch( ) call

2. there is an error in your sql. the objectId is the contribution record ID not the page ID

Code: [Select]
$query = "
SELECT p.goal_amount as goal, sum( c.total_amount ) as total, c.total_amount as total_amount, c.source as source
FROM    civicrm_contribution_page p,
            civicrm_contribution      c
WHERE  p.id = c.contribution_page_id
     AND  c.id = %1
     AND  c.cancel_date is null
GROUP BY p.id
";

$config =& CRM_Core_Config::singleton( );
$params = array( 1 => array( $objectId, 'Integer' ) );

$dao =& CRM_Core_DAO::executeQuery( $query, $params );

        if ( $dao->fetch( ) ) {
   $goal = $dao->goal;
           ....



you might want to check the query in your mysql query log and ensure it does have the right values / ids etc

lobo

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

alextronic

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 2
  • I do what I do
Re: Email notification when Goal Amount reached.
December 16, 2009, 09:37:27 am
This is AWESOME Donald, thanks to you I was able to create my first mini-module for CiviCRM, 'civinotify'. Now I'm thinking of making it bigger (more notifications for more events), but the basics are already there for me.

Here's my code in case anyone is interested in getting notifications when contributions' goal amounts are reached (later on an admin page can be created to customize the emails), great!!!

Code: [Select]
<?php
// $Id: civinotify.module,v 1.1 2009/12/15 14:35:00 quicksketch Exp $
// By alextronic

/**
 * Implementation of hook_civicrm_post()
 */

function civinotify_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if ( 
$objectName == 'Contribution' && $op == 'create' ) {

$sql2="SELECT contribution_page_id FROM wideawake_civicrm.civicrm_contribution WHERE id = ".$objectId;
$result2 = db_query($sql2);
$res = array();
while (
$fila2 = db_fetch_array($result2)) $res = $fila2[contribution_page_id];

$pageID = $res;

$query = "
SELECT p.goal_amount as goal, sum( c.total_amount ) as total, c.total_amount as total_amount, p.title as source
FROM civicrm_contribution_page p,
     civicrm_contribution      c
WHERE p.id = c.contribution_page_id
     AND p.id = %1
     AND c.cancel_date is null
GROUP BY p.id
"
;

$config =& CRM_Core_Config::singleton( );
$params = array( 1 => array( $pageID, 'Integer' ) );
$dao =& CRM_Core_DAO::executeQuery( $query, $params );

if ( $dao->fetch( ) ) {
$goal = $dao->goal;
$total = $dao->total;
$fund = $dao->source;
$amount = $dao->total_amount;
if ( ($total >= $goal) && ($total-$amount < $goal)  ) {
drupal_set_message("Congratulations! Your donation has contributed to fully fund this project! Thank you!");
$subject = "A donor has completed '".$fund."' at website.org";
$text = "A contribution of $".$amount." has been made to the following contribution page: '".$fund."'. This donation has closed the project by reaching its Goal Amount of $".$goal.". Automatic message sent by website.org";
$emailadd = "website.org <noreply@website.org>";
mail("website@website.org", $subject, $text, "From: ".$emailadd."\r\nContent-type: text/html\r\nContent-type: text/html\r\n");
}

}

}
}

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Email notification when Goal Amount reached.

This forum was archived on 2017-11-26.