Add certificate notification see BT#12982

- Add sms CERTIFICATE_NOTIFICATION constant
- Add option "direct_message" to send a message directly to the
SMS plugin.
- Remove unused code in clocworksms
pull/2487/head
jmontoyaa 7 years ago
parent ea31f874fa
commit ff650dfe65
  1. 188
      main/gradebook/gradebook_display_certificate.php
  2. 1
      main/inc/lib/SmsPlugin.php
  3. 74
      plugin/clockworksms/lib/clockworksms.lib.php
  4. 1
      plugin/clockworksms/sms_templates/certificate_notification.tpl

@ -25,6 +25,7 @@ function confirmation() {
}
}
</script>";
api_block_anonymous_users();
if (!api_is_allowed_to_edit() && !api_is_student_boss()) {
@ -36,7 +37,130 @@ $action = isset($_GET['action']) && $_GET['action'] ? $_GET['action'] : null;
$filterOfficialCode = isset($_POST['filter']) ? Security::remove_XSS($_POST['filter']) : null;
$filterOfficialCodeGet = isset($_GET['filter']) ? Security::remove_XSS($_GET['filter']) : null;
$url = api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id.'&filter='.$filterOfficialCode;
$courseInfo = api_get_course_info();
$filter = api_get_setting('certificate_filter_by_official_code');
$userList = array();
$filterForm = null;
$certificate_list = array();
if ($filter === 'true') {
$options = UserManager::getOfficialCodeGrouped();
$options = array_merge(array('all' => get_lang('All')), $options);
$form = new FormValidator(
'official_code_filter',
'POST',
api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id
);
$form->addElement('select', 'filter', get_lang('OfficialCode'), $options);
$form->addButton('submit', get_lang('Submit'));
$filterForm = '<br />'.$form->returnForm();
if ($form->validate()) {
$officialCode = $form->getSubmitValue('filter');
if ($officialCode == 'all') {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
} else {
$userList = UserManager::getUsersByOfficialCode($officialCode);
if (!empty($userList)) {
$certificate_list = GradebookUtils::get_list_users_certificates(
$cat_id,
$userList
);
}
}
} else {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
}
} else {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
}
$content = '';
$tags = [
'((course_title))',
'((user_first_name))',
'((user_last_name))',
'((author_first_name))',
'((author_last_name))',
'((score))',
'((portal_name))'
];
switch ($action) {
case 'send_notifications':
$currentUserInfo = api_get_user_info();
$originalMessage = isset($_POST['message']) ? $_POST['message'] : '';
if (!empty($originalMessage)) {
foreach ($certificate_list as $index => $value) {
$userInfo = api_get_user_info($value['user_id']);
if (empty($userInfo)) {
continue;
}
$list = GradebookUtils::get_list_gradebook_certificates_by_user_id(
$value['user_id'],
$cat_id
);
$subject = get_lang('NotificationCertificateSubject');
foreach ($list as $valueCertificate) {
$replace = [
$courseInfo['title'],
$userInfo['firstname'],
$userInfo['lastname'],
$currentUserInfo['firstname'],
$currentUserInfo['lastname'],
$valueCertificate['score_certificate'],
api_get_setting('Institution')
];
$message = str_replace($tags, $replace, $originalMessage);
MessageManager::send_message(
$userInfo['id'],
$subject,
$message,
[],
[],
0,
0,
0,
0,
$currentUserInfo['id']
);
$plugin = new AppPlugin();
$smsPlugin = $plugin->getSMSPluginLibrary();
if ($smsPlugin) {
$additionalParameters = array(
'smsType' => SmsPlugin::CERTIFICATE_NOTIFICATION,
'userId' => $userInfo['id'],
'direct_message' => $message
);
$smsPlugin->send($additionalParameters);
}
}
}
Display::addFlash(Display::return_message(get_lang('Sent')));
}
header('Location: '.$url);
exit;
break;
case 'show_notification_form':
$form = new FormValidator('notification', 'post', $url.'&action=send_notifications');
$form->addHeader(get_lang('SendNotification'));
$form->addHtmlEditor('message', get_lang('Message'));
$form->addLabel(
get_lang('Tags'),
Display::return_message(implode('<br />', $tags), 'normal', false)
);
$form->addButtonSend(get_lang('Send'));
$form->setDefaults(
['message' => nl2br(get_lang('NotificationCertificateTemplate'))]
);
$content = $form->returnForm();
break;
case 'export_all_certificates':
if (api_is_student_boss()) {
$userGroup = new UserGroup();
@ -70,8 +194,6 @@ switch ($action) {
break;
}
$course_code = api_get_course_id();
$interbreadcrumb[] = array(
'url' => Security::remove_XSS($_SESSION['gradebook_dest']).'?',
'name' => get_lang('Gradebook'),
@ -98,6 +220,10 @@ if (isset($_GET['action']) && $_GET['action'] == 'delete') {
$token = Security::get_token();
echo Display::page_header(get_lang('GradebookListOfStudentsCertificates'));
if (!empty($content)) {
echo $content;
}
//@todo replace all this code with something like get_total_weight()
$cats = Category::load($cat_id, null, null, null, null, null, false);
@ -123,7 +249,7 @@ if (!empty($cats)) {
$total_resource_weight = 0;
if (!empty($datagen)) {
$data_array = $datagen->get_data(
GradebookDataGenerator :: GDG_SORT_NAME,
0,
0,
null,
true
@ -149,45 +275,6 @@ if (!empty($cats)) {
}
}
$filter = api_get_setting('certificate_filter_by_official_code');
$userList = array();
$filterForm = null;
$certificate_list = array();
if ($filter === 'true') {
echo '<br />';
$options = UserManager::getOfficialCodeGrouped();
$options = array_merge(array('all' => get_lang('All')), $options);
$form = new FormValidator(
'official_code_filter',
'POST',
api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id
);
$form->addElement('select', 'filter', get_lang('OfficialCode'), $options);
$form->addButton('submit', get_lang('Submit'));
$filterForm = '<br />'.$form->returnForm();
if ($form->validate()) {
$officialCode = $form->getSubmitValue('filter');
if ($officialCode == 'all') {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
} else {
$userList = UserManager::getUsersByOfficialCode($officialCode);
if (!empty($userList)) {
$certificate_list = GradebookUtils::get_list_users_certificates(
$cat_id,
$userList
);
}
}
} else {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
}
} else {
$certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
}
$url = api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id.'&filter='.$filterOfficialCode;
echo '<div class="btn-group">';
echo Display::url(
@ -209,7 +296,14 @@ if (count($certificate_list) > 0 && $hideCertificateExport !== 'true') {
$url.'&action=export_all_certificates',
array('class' => 'btn btn-default')
);
echo Display::url(
get_lang('SendCertificateNotifications'),
$url.'&action=show_notification_form',
array('class' => 'btn btn-default')
);
}
echo '</div>';
echo $filterForm;
@ -229,19 +323,19 @@ if (count($certificate_list) == 0) {
$value['user_id'],
$cat_id
);
foreach ($list as $value_certificate) {
foreach ($list as $valueCertificate) {
echo '<tr>';
echo '<td width="50%">'.get_lang('Score').' : '.$value_certificate['score_certificate'].'</td>';
echo '<td width="30%">'.get_lang('Date').' : '.api_convert_and_format_date($value_certificate['created_at']).'</td>';
echo '<td width="50%">'.get_lang('Score').' : '.$valueCertificate['score_certificate'].'</td>';
echo '<td width="30%">'.get_lang('Date').' : '.api_convert_and_format_date($valueCertificate['created_at']).'</td>';
echo '<td width="20%">';
$url = api_get_path(WEB_PATH).'certificates/index.php?id='.$value_certificate['id'];
$url = api_get_path(WEB_PATH).'certificates/index.php?id='.$valueCertificate['id'];
$certificates = Display::url(
get_lang('Certificate'),
$url,
array('target' => '_blank', 'class' => 'btn btn-default')
);
echo $certificates;
echo '<a onclick="return confirmation();" href="gradebook_display_certificate.php?sec_token='.$token.'&'.api_get_cidreq().'&action=delete&cat_id='.$cat_id.'&certificate_id='.$value_certificate['id'].'">
echo '<a onclick="return confirmation();" href="gradebook_display_certificate.php?sec_token='.$token.'&'.api_get_cidreq().'&action=delete&cat_id='.$cat_id.'&certificate_id='.$valueCertificate['id'].'">
'.Display::return_icon('delete.png', get_lang('Delete')).'
</a>';
echo '</td></tr>';

@ -52,6 +52,7 @@ class SmsPlugin extends Plugin
const BEEN_INVITED_COMPLETE_SURVEY_COURSE = 40;
const REMINDER_ASSIGNMENT_COURSE_DUE = 41;
const USER_DETAILS_MODIFIED = 42;
const CERTIFICATE_NOTIFICATION = 43;
public $isCoursePlugin = true;
public $isMailPlugin = true;

@ -96,61 +96,46 @@ class Clockworksms implements SmsPluginLibraryInterface
{
$trimmedKey = trim(CONFIG_SECURITY_API_KEY);
if (!empty($trimmedKey)) {
$phoneExists = array_key_exists("mobilePhoneNumber", $additionalParameters);
$phoneExists = array_key_exists('mobilePhoneNumber', $additionalParameters);
$to = $phoneExists ? $additionalParameters['mobilePhoneNumber'] : $this->getMobilePhoneNumberById($additionalParameters['userId']);
$message = array(
"to" => $to,
"message" => $this->getSms($additionalParameters)
'to' => $to,
'message' => $this->getSms($additionalParameters)
);
if (!empty($message['message'])) {
$result = $this->api->send($message);
// Commented for future message logging / tracking purposes
/*if ($result["success"]) {
echo "Message sent - ID: " . $result["id"];
} else {
echo "Message failed - Error: " . $result["error_message"];
}*/
$this->api->send($message);
}
}
}
/**
* buildSms (builds an SMS from a template and data)
* @param object ClockworksmsPlugin object
* @param object Template object
* @param string Template file name
* @param string Text key from lang file
* @param array Data to fill message variables (if any)
* @return object Template object with message property updated
* @param ClockworksmsPlugin $plugin
* @param Template $tpl
* @param string Template file name
* @param string $messageKey Text key from lang file
* @param array $parameters Data to fill message variables (if any)
* @return string
*/
public function buildSms($plugin, $tpl, $templateName, $messageKey, $parameters = null)
{
$result = Database::select(
'selected_value',
'settings_current',
array(
'where'=> array('variable = ?' => array('clockworksms_message'.$messageKey))
)
);
//if (empty($result)) {
if (0) {
$tpl->assign('message', '');
} else {
$templatePath = 'clockworksms/sms_templates/';
$content = $tpl->fetch($templatePath.$templateName);
$message = $plugin->get_lang($messageKey);
if ($parameters !== null) {
$message = vsprintf($message, $parameters);
}
$tpl->assign('message', $message);
public function buildSms(
$plugin,
$tpl,
$templateName,
$messageKey,
$parameters = []
) {
// Send direct message with out using plugin get_lang
if (isset($parameters['direct_message'])) {
return $parameters['direct_message'];
}
return $tpl->params['message'];
$message = $plugin->get_lang($messageKey);
if ($parameters !== null) {
$message = vsprintf($message, $parameters);
}
return $message;
}
/**
@ -321,6 +306,15 @@ class Clockworksms implements SmsPluginLibraryInterface
)
);
break;
case SmsPlugin::CERTIFICATE_NOTIFICATION:
return $this->buildSms(
$plugin,
$tpl,
'certificate_notification.tpl',
'',
$additionalParameters
);
break;
// Message types to be implemented. Fill the array parameter with arguments.
/*case SmsPlugin::ACCOUNT_CREATED_UPDATED_LOGIN_PASSWORD:
return $this->buildSms(

Loading…
Cancel
Save