Adding notification feature see #2189 (personal option to issue mail when something new happens)

skala
Julio Montoya 14 years ago
parent 40707715c4
commit a6078eb79c
  1. 26
      main/auth/profile.php
  2. 5
      main/cron/notification.php
  3. 7
      main/inc/lib/database.lib.php
  4. 32
      main/inc/lib/message.lib.php
  5. 25
      main/inc/lib/model.lib.php
  6. 168
      main/inc/lib/notification.lib.php
  7. 8
      main/inc/lib/social.lib.php
  8. 41
      main/install/db_main.sql
  9. 14
      main/install/migrate-db-1.8.7-1.8.8-pre.sql
  10. 44
      main/install/update-db-1.8.7-1.8.8.inc.php
  11. 1
      main/messages/new_message.php

@ -97,16 +97,12 @@ EOF;
}
/*
-----------------------------------------------------------
Configuration file
-----------------------------------------------------------
*/
require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
/*
-----------------------------------------------------------
Libraries
-----------------------------------------------------------
*/
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
@ -115,13 +111,10 @@ require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
require_once api_get_path(LIBRARY_PATH).'social.lib.php';
$tool_name = is_profile_editable() ? get_lang('ModifProfile') : get_lang('ViewProfile');
$table_user = Database :: get_main_table(TABLE_MAIN_USER);
/*
-----------------------------------------------------------
Form
-----------------------------------------------------------
*/
/*
@ -329,15 +322,21 @@ foreach ($extra as $id => $field_details) {
if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
break;
case USER_FIELD_TYPE_SELECT:
$options = array();
if (in_array($field_details[1], array('mail_notify_invitation', 'mail_notify_message', 'mail_notify_group_message'))) {
$field_details[3] = get_lang($field_details[3]);
}
$options = array();
foreach ($field_details[9] as $option_id => $option_details) {
if (in_array($field_details[1], array('mail_notify_invitation', 'mail_notify_message', 'mail_notify_group_message'))) {
$option_details[2] = get_lang($option_details[2]);
}
$options[$option_details[1]] = $option_details[2];
}
$form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, '');
if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
break;
case USER_FIELD_TYPE_SELECT_MULTIPLE:
$options = array();
$options = array();
foreach ($field_details[9] as $option_id => $option_details) {
$options[$option_details[1]] = $option_details[2];
}
@ -476,16 +475,9 @@ $user_data = array_merge($user_data, $extra_data);
$form->setDefaults($user_data);
/*
==============================================================================
FUNCTIONS
==============================================================================
*/
/*
-----------------------------------------------------------
LOGIC FUNCTIONS
-----------------------------------------------------------
*/
/**
* Can a user edit his/her profile?
@ -497,9 +489,7 @@ function is_profile_editable() {
}
/*
-----------------------------------------------------------
PRODUCTIONS FUNCTIONS
-----------------------------------------------------------
*/
/**

@ -0,0 +1,5 @@
<?php
require_once '../inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH).'notification.lib.php';
$notify = new Notification();
$notify->send();

@ -276,7 +276,7 @@ define('TABLE_THEMATIC','thematic');
define('TABLE_THEMATIC_PLAN', 'thematic_plan');
define('TABLE_THEMATIC_ADVANCE','thematic_advance');
// Careers, promotions, Usergroups
define('TABLE_CAREER', 'career');
define('TABLE_PROMOTION', 'promotion');
@ -285,7 +285,8 @@ define('TABLE_USERGROUP_REL_USER', 'usergroup_rel_user');
define('TABLE_USERGROUP_REL_COURSE', 'usergroup_rel_course');
define('TABLE_USERGROUP_REL_SESSION', 'usergroup_rel_session');
// Mail notifications
define('TABLE_NOTIFICATION', 'notification');
/* DATABASE CLASS
The class and its methods
@ -1327,7 +1328,7 @@ class Database {
$params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table
$values = array_values($filtred_attributes);
if (!empty($params) && !empty($values)) {
$sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')';
echo $sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')';
$result = self::query($sql);
return self::get_last_insert_id();
}

@ -202,7 +202,8 @@ class MessageManager
}
/**
* Save message for social network
* Sends a message to a user/group
*
* @param int receiver user id
* @param string subject
* @param string content
@ -216,7 +217,7 @@ class MessageManager
*/
public static function send_message ($receiver_user_id, $subject, $content, $file_attachments = array(), $file_comments = array(), $group_id = 0, $parent_id = 0, $edit_message_id = 0, $sent_email = false) {
global $charset;
$table_message = Database::get_main_table(TABLE_MESSAGE);
$table_message = Database::get_main_table(TABLE_MESSAGE);
$group_id = intval($group_id);
$receiver_user_id = intval($receiver_user_id);
$parent_id = intval($parent_id);
@ -254,10 +255,9 @@ class MessageManager
" VALUES ('$user_sender_id', '$receiver_user_id', '1', '".api_get_utc_datetime()."','$subject','$content','$group_id','$parent_id', '".api_get_utc_datetime()."')";
$result = Database::query($query);
$inbox_last_id = Database::insert_id();
}
}
// save attachment file for inbox messages
// Save attachment file for inbox messages
if (is_array($file_attachments)) {
$i = 0;
foreach ($file_attachments as $file_attach) {
@ -270,7 +270,7 @@ class MessageManager
if (empty($group_id)) {
//message in outbox for user friend or group
$sql = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
$sql = "INSERT INTO $table_message (user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
" VALUES ('$user_sender_id', '$receiver_user_id', '4', '".api_get_utc_datetime()."','$subject','$content', '$group_id', '$parent_id', '".api_get_utc_datetime()."')";
$rs = Database::query($sql);
$outbox_last_id = Database::insert_id();
@ -286,11 +286,21 @@ class MessageManager
}
}
}
//@todo sent email process
if ($sent_email) {
}
//Load user settings
require_once api_get_path(LIBRARY_PATH).'notification.lib.php';
$notification = new Notification();
if (empty($group_id)) {
$user_id = $receiver_user_id;
$notification->save_message_notifications(array($user_id), $subject,$content);
} else {
$user_list = GroupPortalManager::get_users_by_group($group_id);
$new_user_list = array();
foreach($user_list as $user_data) {
$new_user_list []= $user_data['user_id'];
}
$notification->save_group_notifications($new_user_list, $subject,$content);
}
return $result;
} else {
return get_lang('UserDoesNotExist');

@ -11,14 +11,23 @@ class Model {
var $table;
var $columns;
// var $pk; some day this will be implemented
public function __construct() {
}
/**
* Useful finder
* Useful finder - experimental akelos like only use in notification.lib.php send function
*/
public function find() {
public function find($type, $options = null) {
switch($type) {
case 'all':
return self::get_all($options);
break;
case (is_numeric($type)) :
return self::get($type);
break;
}
}
/**
@ -63,8 +72,8 @@ class Model {
return $result;
}
public function get_all() {
return Database::select('*',$this->table);
public function get_all($options = null) {
return Database::select('*',$this->table, $options);
}
/**
@ -112,17 +121,17 @@ class Model {
*/
public function update($params) {
$params = $this->clean_parameters($params);
//If the class have the updated at we update the date
//If the class has the updated_at field we update the date
if (in_array('updated_at', $this->columns)) {
$params['updated_at'] = api_get_utc_datetime();
}
//If the class have the created at we remove it
//If the class has the created_at field then we remove it
if (in_array('created_at', $this->columns)) {
unset($params['created_at']);
}
if (!empty($params)) {
$id = $params['id'];
if (!empty($params) && !empty($params['id'])) {
$id = intval($params['id']);
unset($params['id']); //To not overwrite the id
if (is_numeric($id)) {
$result = Database::update($this->table, $params, array('id = ?'=>$id));

@ -0,0 +1,168 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This class provides methods for the Notification management.
* Include/require it in your code to use its features.
* @package chamilo.library
*/
require_once 'model.lib.php';
require_once 'usermanager.lib.php';
//default values
//mail_notify_message ("At once", "Daily", "No")
define('NOTIFY_MESSAGE_AT_ONCE', '1');
define('NOTIFY_MESSAGE_DAILY', '8');
define('NOTIFY_MESSAGE_NO', '0');
//mail_notify_invitation ("At once", "Daily", "No")
define('NOTIFY_INVITATION_AT_ONCE', '1');
define('NOTIFY_INVITATION_DAILY', '8');
define('NOTIFY_INVITATION_NO', '0');
// mail_notify_group_message ("At once", "Daily", "No")
define('NOTIFY_GROUP_AT_ONCE', '1');
define('NOTIFY_GROUP_DAILY', '8');
define('NOTIFY_GROUP_NO', '0');
class Notification extends Model {
var $table;
var $columns = array('id','dest_user_id','dest_mail','title','content','send_freq','created_at','sent_at');
var $max_content_length = 255;
var $debug = true;
public function __construct() {
$this->table = Database::get_main_table(TABLE_NOTIFICATION);
}
public function send($frec = NOTIFY_MESSAGE_DAILY) {
$notifications = $this->find('all',array('where'=>array('sent_at IS NULL AND send_freq = ?'=>$frec)));
if (!empty($notifications)) {
foreach($notifications as $item_to_send) {
//Sending email
api_send_mail($item_to_send['dest_mail'], $item_to_send['title'], $item_to_send['content']);
if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); }
//Updating
$item_to_send['sent_at'] = api_get_utc_datetime();
$this->update($item_to_send);
//if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); }
}
}
}
/**
* Save message notification
* @param array user list of ids
* @param string title
* @param string content of the message
*
*/
public function save_message_notifications($user_list, $title, $content) {
if (!empty($user_list)) {
foreach($user_list as $user_id) {
$extra_data = UserManager::get_extra_user_data($user_id);
$params = array();
switch ($extra_data['mail_notify_message']) {
case NOTIFY_MESSAGE_NO:
break;
case NOTIFY_MESSAGE_AT_ONCE:
$user_info = api_get_user_info($user_id);
if (!empty($user_info['mail'])) {
api_send_mail($user_info['mail'], $title, cut($content, $this->max_content_length));
}
$params['sent_at'] = api_get_utc_datetime();
default:
$extra_data = UserManager::get_extra_user_data($user_id);
$user_info = api_get_user_info($user_id);
$params['dest_user_id'] = $user_id;
$params['dest_mail'] = $user_info['mail'];
$params['title'] = $title;
$params['content'] = cut($content, $this->max_content_length);
$params['send_freq'] = $extra_data['mail_notify_message'];
$this->save($params);
break;
}
}
}
}
/**
* Save invitation notification
* @param array user list of ids
* @param string title
* @param string content of the message
*
*/
public function save_invitation_notifications($user_list, $title, $content) {
if (!empty($user_list)) {
foreach($user_list as $user_id) {
$extra_data = UserManager::get_extra_user_data($user_id);
$params = array();
switch ($extra_data['mail_notify_invitation']) {
case NOTIFY_INVITATION_NO:
break;
case NOTIFY_INVITATION_AT_ONCE:
$user_info = api_get_user_info($user_id);
if (!empty($user_info['mail'])) {
api_send_mail($user_info['mail'], $title, cut($content, $this->max_content_length));
}
$params['sent_at'] = api_get_utc_datetime();
default:
$extra_data = UserManager::get_extra_user_data($user_id);
$user_info = api_get_user_info($user_id);
$params['dest_user_id'] = $user_id;
$params['dest_mail'] = $user_info['mail'];
$params['title'] = $title;
$params['content'] = cut($content, $this->max_content_length);
$params['send_freq'] = $extra_data['mail_notify_invitation'];
$this->save($params);
break;
}
}
}
}
/**
* Save group notifications
* @param array user list of ids
* @param string title
* @param string content of the message
*
*/
public function save_group_notifications($user_list, $title, $content) {
if (!empty($user_list)) {
foreach($user_list as $user_id) {
//Avoiding sending a message to myself
if ($user_id == api_get_user_id()) {
continue;
}
$extra_data = UserManager::get_extra_user_data($user_id);
$params = array();
switch ($extra_data['mail_notify_group_message']) {
case NOTIFY_GROUP_NO:
break;
case NOTIFY_GROUP_AT_ONCE:
$user_info = api_get_user_info($user_id);
if (!empty($user_info['mail'])) {
api_send_mail($user_info['mail'], $subject, cut($content,150));
}
$params['sent_at'] = api_get_utc_datetime();
default:
$extra_data = UserManager::get_extra_user_data($user_id);
$user_info = api_get_user_info($user_id);
$params['dest_user_id'] = $user_id;
$params['dest_mail'] = $user_info['mail'];
$params['title'] = $subject;
$params['content'] = cut($content,150);
$params['send_freq'] = $extra_data['mail_notify_group_message'];
$this->save($params);
break;
}
}
}
}
}

@ -181,8 +181,14 @@ class SocialManager extends UserManager {
$row_exist=Database::fetch_array($res_exist,'ASSOC');
if ($row_exist['count']==0) {
$sql='INSERT INTO '.$tbl_message.'(user_sender_id,user_receiver_id,msg_status,send_date,title,content) VALUES('.$user_id.','.$friend_id.','.MESSAGE_STATUS_INVITATION_PENDING.',"'.$current_date.'","'.$message_title.'","'.$message_content.'")';
Database::query($sql);
Database::query($sql);
require_once api_get_path(LIBRARY_PATH).'notification.lib.php';
$notification = new Notification();
$notification->save_invitation_notifications(array($friend_id), $message_title, $message_content);
return true;
} else {
//invitation already exist

@ -2549,6 +2549,10 @@ CREATE TABLE course_request (
UNIQUE KEY code (code)
);
--
-- Structure for Careers, Promotions and Usergroups
--
CREATE TABLE career (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL ,
@ -2592,3 +2596,40 @@ CREATE TABLE usergroup_rel_session (
usergroup_id INT NOT NULL,
session_id INT NOT NULL
);
--
-- Structure for Mail notifications
--
CREATE TABLE notification (
id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
dest_user_id INT NOT NULL,
dest_mail CHAR(255),
title CHAR(255),
content CHAR(255),
send_freq SMALLINT DEFAULT 1,
created_at DATETIME NOT NULL,
sent_at DATETIME NULL
);
ALTER TABLE notification ADD index mail_notify_sent_index (sent_at);
ALTER TABLE notification ADD index mail_notify_freq_index (sent_at, send_freq, created_at);
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_invitation', 'MailNotifyInvitation',1,1,'1');
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_message', 'MailNotifyMessage',1,1,'1');
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_group_message','MailNotifyGroup_message',1,1,'1');
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (6, '1', 'AtOnce',1) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (6, '8', 'Daily',2) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (6, '0', 'No',3) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (7, '1', 'AtOnce',1) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (7, '8', 'Daily',2) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (7, '0', 'No',3) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (8, '1', 'AtOnce',1) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (8, '8', 'Daily',2) ";
INSERT INTO user_field_options (field_id, option_value, option_display_text, option_order) values (8, '0', 'No',3) ";

@ -166,10 +166,24 @@ INSERT INTO settings_current (variable, subkey, type, category, selected_value,
INSERT INTO settings_options (variable, value, display_text) VALUES ('accessibility_font_resize', 'true', 'Yes');
INSERT INTO settings_options (variable, value, display_text) VALUES ('accessibility_font_resize', 'false', 'No');
CREATE TABLE notification ( id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT, dest_user_id INT NOT NULL, dest_mail CHAR(255), title CHAR(255), content CHAR(255), send_freq SMALLINT DEFAULT 1, created_at DATETIME NOT NULL, sent_at DATETIME NULL);
ALTER TABLE notification ADD index mail_notify_sent_index (sent_at);
ALTER TABLE notification ADD index mail_notify_freq_index (sent_at, send_freq, created_at);
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_invitation', 'MailNotifyInvitation',1,1,'1');
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_message', 'MailNotifyMessage',1,1,'1');
INSERT INTO user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_group_message','MailNotifyGroup_message',1,1,'1');
UPDATE settings_current SET selected_value = '1.8.8.13709' WHERE variable = 'chamilo_database_version';
-- xxSTATSxx
ALTER TABLE track_e_exercices ADD COLUMN orig_lp_item_view_id INT NOT NULL DEFAULT 0;
-- xxUSERxx
ALTER TABLE personal_agenda ADD INDEX idx_personal_agenda_user (user);
ALTER TABLE personal_agenda ADD INDEX idx_personal_agenda_parent (parent_event_id);

@ -249,8 +249,52 @@ if (defined('SYSTEM_INSTALLATION')) {
}
}
//Adding notifications options
$sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_invitation', 'mail_notify_invitation',1,1,'at_once') ";
$result = Database::query($sql);
$id = Database::insert_id();
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'at_once', 'at_once',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'daily', 'daily',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'no', 'no',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_message', 'mail_notify_message',1,1,'at_once')";
$result = Database::query($sql);
$id = Database::insert_id();
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'at_once', 'at_once',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'daily', 'daily',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'no', 'no',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_group_message','mail_notify_group_message',1,1,'at_once') ";
$result = Database::query($sql);
$id = Database::insert_id();
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'at_once', 'at_once',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'daily', 'daily',1) ";
$result = Database::query($sql);
$sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, 'no', 'no',1) ";
$result = Database::query($sql);
}
}
/* // This fragment of code is not necessary so far.
// Get the courses databases queries list (c_q_list)

@ -4,7 +4,6 @@
* @package chamilo.messages
*/
/**
* This script shows a compose area (wysiwyg editor if supported, otherwise
* a simple textarea) where the user can type a message.

Loading…
Cancel
Save