Skip to content

Commit

Permalink
Added Unit tests and documentation for the alerts feature
Browse files Browse the repository at this point in the history
Signed-off-by: Linda Kamau <kamaulynder@gmail.com>
  • Loading branch information
kamaulynder committed Nov 23, 2011
1 parent 017ef13 commit c6ef8ff
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 144 deletions.
20 changes: 8 additions & 12 deletions application/controllers/alerts.php
Expand Up @@ -15,9 +15,6 @@


class Alerts_Controller extends Main_Controller { class Alerts_Controller extends Main_Controller {


const MOBILE_ALERT = 1;
const EMAIL_ALERT = 2;

public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
Expand Down Expand Up @@ -109,25 +106,24 @@ public function index()
// If there is a post and $_POST is not empty // If there is a post and $_POST is not empty
if ($post = $this->input->post()) if ($post = $this->input->post())
{ {
$alerts = new Alert_Model(); $alert_orm = new Alert_Model();
if ($alerts->validate($post)) if ($alert_orm->validate($post))
{ {
// Yes! everything is valid // Yes! everything is valid
// Save alert and send out confirmation code // Save alert and send out confirmation code


if ( ! empty($post->alert_mobile)) if ( ! empty($post->alert_mobile))
{ {
alert::_send_mobile_alert($post); alert::_send_mobile_alert($post, $alert_orm);
$this->session->set('alert_mobile', $post->alert_mobile);
} }


if ( ! empty($post->alert_email)) if ( ! empty($post->alert_email))
{ {
alert::_send_email_alert($post); alert::_send_email_alert($post, $alert_orm);
$this->session->set('alert_email', $post->alert_email);
} }


$this->session->set('alert_mobile', $post->alert_mobile);
$this->session->set('alert_email', $post->alert_email);

url::redirect('alerts/confirm'); url::redirect('alerts/confirm');
} }
// No! We have validation errors, we need to show the form again, with the errors // No! We have validation errors, we need to show the form again, with the errors
Expand Down
158 changes: 91 additions & 67 deletions application/helpers/alert.php
Expand Up @@ -14,16 +14,21 @@ class alert_Core {
const MOBILE_ALERT = 1; const MOBILE_ALERT = 1;
const EMAIL_ALERT = 2; const EMAIL_ALERT = 2;


public static function _send_mobile_alert($post) /**
* Sends an alert to a mobile phone
*
* @param Validation_Core $post
* @param Alert_Model $alert
* @return bool
*/
public static function _send_mobile_alert($post, $alert)
{ {
// For Mobile Alerts, Confirmation Code if ( ! $post instanceof Validation_Core AND !$alert instanceof Alert_Model)
$alert_mobile = $post->alert_mobile; {
$alert_lon = $post->alert_lon; throw new Kohana_Exception('Invalid parameter types');
$alert_lat = $post->alert_lat; }
$alert_radius = $post->alert_radius;
$alert_confirmed = $post->alert_confirmed; // Should be 8 distinct characters

// Should be 6 distinct characters
$alert_code = text::random('distinct', 8); $alert_code = text::random('distinct', 8);


$settings = ORM::factory('settings', 1); $settings = ORM::factory('settings', 1);
Expand Down Expand Up @@ -52,16 +57,11 @@ public static function _send_mobile_alert($post)
$message = Kohana::lang('ui_admin.confirmation_code').$alert_code $message = Kohana::lang('ui_admin.confirmation_code').$alert_code
.'.'.Kohana::lang('ui_admin.not_case_sensitive'); .'.'.Kohana::lang('ui_admin.not_case_sensitive');


if (sms::send($alert_mobile, $sms_from, $message) === true) if (sms::send($post->alert_mobile, $sms_from, $message) === true)
{ {
$alert = ORM::factory('alert');
$alert->alert_type = self::MOBILE_ALERT; $alert->alert_type = self::MOBILE_ALERT;
$alert->alert_recipient = $alert_mobile; $alert->alert_recipient = $post->alert_mobile;
$alert->alert_code = $alert_code; $alert->alert_code = $alert_code;
$alert->alert_lon = $alert_lon;
$alert->alert_lat = $alert_lat;
$alert->alert_radius = $alert_radius;
$alert->alert_confirmed = $alert_confirmed;
if (isset($_SESSION['auth_user'])) if (isset($_SESSION['auth_user']))
{ {
$alert->user_id = $_SESSION['auth_user']->id; $alert->user_id = $_SESSION['auth_user']->id;
Expand All @@ -78,16 +78,20 @@ public static function _send_mobile_alert($post)


/** /**
* Sends an email alert * Sends an email alert
*
* @param Validation_Core $post
* @param Alert_Model $alert
* @return bool
*/ */
public static function _send_email_alert($post) public static function _send_email_alert($post, $alert)
{ {
if ( ! $post instanceof Validation_Core AND !$alert instanceof Alert_Model)
{
throw new Kohana_Exception('Invalid parameter types');
}

// Email Alerts, Confirmation Code // Email Alerts, Confirmation Code
$alert_email = $post->alert_email; $alert_email = $post->alert_email;
$alert_lon = $post->alert_lon;
$alert_lat = $post->alert_lat;
$alert_radius = $post->alert_radius;
$alert_confirmed = $post->alert_confirmed;

$alert_code = text::random('alnum', 20); $alert_code = text::random('alnum', 20);


$settings = kohana::config('settings'); $settings = kohana::config('settings');
Expand All @@ -105,14 +109,9 @@ public static function _send_email_alert($post)


if (email::send($to, $from, $subject, $message, TRUE) == 1) if (email::send($to, $from, $subject, $message, TRUE) == 1)
{ {
$alert = ORM::factory('alert');
$alert->alert_type = self::EMAIL_ALERT; $alert->alert_type = self::EMAIL_ALERT;
$alert->alert_recipient = $alert_email; $alert->alert_recipient = $alert_email;
$alert->alert_code = $alert_code; $alert->alert_code = $alert_code;
$alert->alert_lon = $alert_lon;
$alert->alert_lat = $alert_lat;
$alert->alert_radius = $alert_radius;
$alert->alert_confirmed = $alert_confirmed;
if (isset($_SESSION['auth_user'])) if (isset($_SESSION['auth_user']))
{ {
$alert->user_id = $_SESSION['auth_user']->id; $alert->user_id = $_SESSION['auth_user']->id;
Expand All @@ -130,50 +129,72 @@ public static function _send_email_alert($post)


/** /**
* This handles sms alerts subscription via phone * This handles sms alerts subscription via phone
* @params alert,location (city) - required *
* @params distance, category - optional * @param string $message_from Subscriber MSISDN (mobile phone number)
* @param string $message_description Message content
* @return bool
*/ */
public function mobile_alerts_register($message_from, $message_description) public static function mobile_alerts_register($message_from, $message_description)
{ {
// Preliminary validation
if (empty($message_from) OR empty($message_description))
{
// Log the error
Kohana::log('info', 'Insufficient data to proceed with subscription via mobile phone');

// Return
return FALSE;
}


/** //Get the message details (location, category, distance)
* Get the message details (location, category, distance) $message_details = explode(" ",$message_description);
*/ $message = $message_details[1].",".Kohana::config('settings.default_country');
$message_details = explode(" ",$message_description); $geocoder = map::geocode($message);
$message = $message_details[1].",".Kohana::config('settings.default_country');
$geocoder = map::geocode($message);


/** // Generate alert code
* Generate alert code $alert_code = text::random('distinct', 8);
*/
$alert_code = text::random('distinct', 8);


// POST variable with items to save
$post = array(
'alert_type'=> self::MOBILE_ALERT,
'alert_mobile'=>$message_from,
'alert_code'=>$alert_code,
'alert_lon'=>$geocoder['lon'],
'alert_lat'=>$geocoder['lat'],
'alert_radius'=>'20',
'alert_confirmed'=>'1'
);

// Create ORM object for the alert and validate
$alert_orm = new Alert_Model();
if ($alert_orm->validate($post))
{
return self::_send_mobile_alert($post, $alert_orm);
}


/* POST variable with items to save */ return FALSE;

$post = array(
'alert_type'=> self::MOBILE_ALERT,
'alert_mobile'=>$message_from,
'alert_code'=>$alert_code,
'alert_lon'=>$geocoder['lon'],
'alert_lat'=>$geocoder['lat'],
'alert_radius'=>'20',
'alert_confirmed'=>'1'
);

//convert the array to object
$p = (object) $post;

/**
* Save alert details
*/
self::_send_mobile_alert($p);


} }



/**
public function mobile_alerts_unsubscribe($message_from, $message_description) * This handles unsubscription from alerts via the mobile phone
*
* @param string $message_from Phone number of subscriber
* @param string $message_description Message content
* @return bool
*/
public static function mobile_alerts_unsubscribe($message_from, $message_description)
{ {
// Validate parameters

if (empty($message_from) OR empty($message_description))
{
// Log the error
Kohana::log('info', 'Cannot unsubscribe from alerts via the mobile phone - insufficient data');

// Return
return FALSE;
}


$settings = ORM::factory('settings', 1); $settings = ORM::factory('settings', 1);


Expand Down Expand Up @@ -204,7 +225,7 @@ public function mobile_alerts_unsubscribe($message_from, $message_description)
if (sms::send($message_from, $sms_from, $message) === true) if (sms::send($message_from, $sms_from, $message) === true)
{ {
// Fetch all alerts with the specified code // Fetch all alerts with the specified code
$alerts = ORM::factory('alert') $alerts = ORM::factory('alert')
->where('alert_recipient', $message_from) ->where('alert_recipient', $message_from)
->find_all(); ->find_all();


Expand All @@ -216,14 +237,19 @@ public function mobile_alerts_unsubscribe($message_from, $message_description)
->delete_all(); ->delete_all();


$alert->delete(); $alert->delete();

} }
} return TRUE;
}
return FALSE;
} }




/* This handles saving alert categories that a subscriber has subscribed for
*
* @param $Alert_Model $alert
*/


private function _add_categories(Alert_Model $alert, $post) private static function _add_categories(Alert_Model $alert, $post)
{ {
if (isset($post->alert_category)) if (isset($post->alert_category))
{ {
Expand All @@ -242,6 +268,4 @@ private function _add_categories(Alert_Model $alert, $post)
} }
} }




} }

0 comments on commit c6ef8ff

Please sign in to comment.