Skip to content

Commit

Permalink
Dev: Update notification database field names
Browse files Browse the repository at this point in the history
Dev: 'read' will be 'first_read'
Dev: 'type' will be 'importance' and an integer
Dev: 'modal_class' will be 'display_class'
  • Loading branch information
olleharstedt committed Aug 4, 2016
1 parent f551858 commit cc07b9b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 44 deletions.
4 changes: 1 addition & 3 deletions application/controllers/admin/NotificationController.php
Expand Up @@ -54,9 +54,7 @@ public function notificationRead($notId)
try
{
$not = Notification::model()->findByPk($notId);
$not->read = date('Y-m-d H:i:s', time());
$not->status = 'read';
$result = $not->update();
$result = $not->markAsRead();
echo json_encode(array('result' => $result));
}
catch (Exception $ex)
Expand Down
6 changes: 3 additions & 3 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -1425,10 +1425,10 @@ function db_upgrade_all($iOldDBVersion, $bSilent=false) {
'title' => 'string not null', // varchar(255) in postgres
'message' => 'text not null',
'status' => 'string default \'new\'',
'type' => 'string default \'log\'',
'modal_class' => 'string default \'default\'',
'importance' => 'int default 1',
'display_class' => 'string default \'default\'',
'created' => 'datetime not null',
'read' => 'datetime default null'
'first_read' => 'datetime default null'
));
$oDB->createCommand()->createIndex('notif_index', '{{notifications}}', 'entity, entity_id, status', false);
$oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>259),"stg_name='DBVersion'");
Expand Down
66 changes: 41 additions & 25 deletions application/models/Notification.php
Expand Up @@ -9,14 +9,19 @@
* @property string $entity_id survey id or user id
* @property string $title
* @property string $message
* @property string $type success, warning, danger
* @property integer $importance 1 or 3. 3 will show popup on page load, 2 is reserved for future bell animation.
* @property string $display_class warning, danger, success
* @property string $status new, read
* @property DateTime $created When the notification was created
* @property DateTime $read When the notification was read
* @property DateTime $first_read When the notification was read
*/
class Notification extends LSActiveRecord
{

const NORMAL_IMPORTANCE = 1; // Just notification in admin menu
const BELL_IMPORTANCE = 2; // TODO: Bell animation
const HIGH_IMPORTANCE = 3; // Popup on page load

/**
* See example usage at manual page: https://manual.limesurvey.org/Notifications#Examples
* @param array<string, mixed>|string|null $options If string then scenario
Expand Down Expand Up @@ -50,34 +55,34 @@ public function __construct($options = null)
throw new InvalidArgumentException('Invalid entity: ' . $options['entity']);
}

// Default to 'default' modal class
if (!isset($options['modal_class']))
// Default to 'default' display class
if (!isset($options['display_class']))
{
$options['modal_class'] = 'default';
$options['display_class'] = 'default';
}

// Default to 'log' notification type
if (!isset($options['type']))
// Default to 'log' notification importance
if (!isset($options['importance']))
{
$options['type'] = 'log';
$options['importance'] = self::NORMAL_IMPORTANCE;
}

// Type must be 'log' or 'important'
if ($options['type'] != 'log' && $options['type'] != 'important')
// importance must be between 1 and 3
if ($options['importance'] < 1 && $options['importance'] > 3)
{
throw new InvalidArgumentException('Invalid type: ' . $options['type']);
throw new InvalidArgumentException('Invalid importance: ' . $options['importance']);
}

// Set everything up
$this->entity = $options['entity'];
$this->entity_id = $options['entity_id'];
$this->title = $options['title'];
$this->message = $options['message'];
$this->modal_class = $options['modal_class'];
$this->type = $options['type'];
$this->display_class = $options['display_class'];
$this->importance = $options['importance'];
$this->status = 'new';
$this->created = date('Y-m-d H:i:s', time());
$this->read = null;
$this->first_read = null;
}

/**
Expand Down Expand Up @@ -142,12 +147,11 @@ public function rules()
return array(
array('entity_id', 'numerical', 'integerOnly'=>true),
array('entity', 'length', 'max'=>64),
array('type, status', 'length', 'max'=>63),
array('title', 'length', 'max'=>255),
array('message, created, read', 'safe'),
array('message, created, first_read', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, entity, entity_id, message, type, created, read, status, title', 'safe', 'on'=>'search'),
array('id, entity, entity_id, message, importance, created, first_read, status, title', 'safe', 'on'=>'search'),
);
}

Expand All @@ -172,9 +176,9 @@ public function attributeLabels()
'entity' => 'Entity',
'entity_id' => 'Entity',
'message' => 'Message',
'type' => 'Type',
'importance' => 'Importance',
'created' => 'Created',
'read' => 'Read',
'first_read' => 'Read',
'status' => 'Status',
'title' => 'Title',
);
Expand Down Expand Up @@ -202,9 +206,9 @@ public function search()
$criteria->compare('entity',$this->entity,true);
$criteria->compare('entity_id',$this->entity_id);
$criteria->compare('message',$this->message,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('importance',$this->importance);
$criteria->compare('created',$this->created,true);
$criteria->compare('read',$this->read,true);
$criteria->compare('first_read',$this->first_read);
$criteria->compare('status',$this->status,true);
$criteria->compare('title',$this->title,true);

Expand Down Expand Up @@ -242,6 +246,18 @@ public function getReadUrl()
);
}

/**
* Mark notification as read NOW()
* @return boolean Result of update
*/
public function markAsRead()
{
$this->first_read = date('Y-m-d H:i:s', time());
$this->status = 'read';
$result = $this->update();
return $result;
}

/**
* Url to fetch the complete notification menu widget
* @param int|null $surveyId
Expand Down Expand Up @@ -280,15 +296,15 @@ public static function getNotifications($surveyId)
}

/**
* Get notifications of type 'important'
* Get notifications of importance HIGH_IMPORTANCE
* @param int|null $surveyId
* @return Notification[]
*/
public static function getImportantNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$criteria2 = new CDbCriteria();
$criteria2->addCondition('type = \'important\'');
$criteria2->addCondition('importance = ' . self::HIGH_IMPORTANCE);
$criteria->mergeWith($criteria2, 'AND');

return self::model()->findAll($criteria);
Expand Down Expand Up @@ -316,7 +332,7 @@ public static function countNewNotifications($surveyId)
$criteria = self::getCriteria($surveyId);

$criteria2 = new CDbCriteria();
$criteria2->addCondition('status = \'new\''); // TODO: read = null
$criteria2->addCondition('status = \'new\''); // TODO: Check first_read = null instead?
$criteria->mergeWith($criteria2, 'AND');

$nr = self::model()->count($criteria);
Expand All @@ -332,7 +348,7 @@ public static function countImportantNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$criteria2 = new CDbCriteria();
$criteria2->addCondition('type = \'important\'');
$criteria2->addCondition('importance = ' . self::HIGH_IMPORTANCE);
$criteria->mergeWith($criteria2, 'AND');

return self::model()->count($criteria);
Expand Down
2 changes: 1 addition & 1 deletion application/views/admin/super/admin_notifications.php
Expand Up @@ -47,7 +47,7 @@ class='admin-notification-link'
data-url='<?php echo $not->ajaxUrl; ?>'
data-read-url='<?php echo $not->readUrl; ?>'
data-update-url='<?php echo $updateUrl; ?>'
data-type='<?php echo $not->type; ?>'
data-importance='<?php echo $not->importance; ?>'
data-status='<?php echo $not->status; ?>'
href='#'
>
Expand Down
6 changes: 3 additions & 3 deletions installer/sql/create-mssql.sql
Expand Up @@ -599,10 +599,10 @@ CREATE TABLE prefix_notifications (
[title] nvarchar(255) NOT NULL,
[message] nvarchar(max) NOT NULL,
[status] nvarchar(63) NOT NULL DEFAULT 'new',
[type] nvarchar(63) NOT NULL DEFAULT 'log',
[modal_class] nvarchar(63) DEFAULT 'default',
[importance] int NOT NULL DEFAULT 1,
[display_class] nvarchar(63) DEFAULT 'default',
[created] datetime NOT NULL,
[read] datetime DEFAULT NULL,
[first_read] datetime DEFAULT NULL,
PRIMARY KEY ([id])
);
CREATE INDEX [notif_index] ON [prefix_notifications] ([entity_id],[entity],[status]);
Expand Down
6 changes: 3 additions & 3 deletions installer/sql/create-mysql.sql
Expand Up @@ -605,10 +605,10 @@ CREATE TABLE IF NOT EXISTS `prefix_notifications` (
`title` VARCHAR(255) NOT NULL,
`message` TEXT NOT NULL,
`status` VARCHAR(63) NOT NULL DEFAULT 'new' COMMENT 'new or read',
`type` VARCHAR(63) NOT NULL DEFAULT 'log' COMMENT 'log or important',
`modal_class` VARCHAR(63) DEFAULT 'default' COMMENT 'Bootstrap class, like warning, info, success',
`importance` INT(11) NOT NULL DEFAULT 1,
`display_class` VARCHAR(63) DEFAULT 'default' COMMENT 'Bootstrap class, like warning, info, success',
`created` DATETIME NOT NULL,
`read` DATETIME DEFAULT NULL,
`first_read` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX(`entity`, `entity_id`, `status`)
) ENGINE=MYISAM CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Expand Down
6 changes: 3 additions & 3 deletions installer/sql/create-pgsql.sql
Expand Up @@ -608,10 +608,10 @@ CREATE TABLE prefix_notifications (
"title" character varying(255) NOT NULL,
"message" TEXT NOT NULL,
"status" character varying(63) NOT NULL DEFAULT 'new',
"type" character varying(63) NOT NULL DEFAULT 'log',
"modal_class" character varying(63) DEFAULT 'default',
"importance" integer NOT NULL DEFAULT 1,
"display_class" character varying(63) DEFAULT 'default',
"created" timestamp NOT NULL,
"read" timestamp DEFAULT NULL,
"first_read" timestamp DEFAULT NULL,
CONSTRAINT prefix_notifications_pkey PRIMARY KEY (id)
);
CREATE INDEX prefix_index ON prefix_notifications USING btree (entity, entity_id, status);
Expand Down
6 changes: 3 additions & 3 deletions scripts/admin/notifications.js
Expand Up @@ -91,7 +91,7 @@ $(document).ready(function() {

$('#admin-notification-modal .modal-title').html(not.title);
$('#admin-notification-modal .modal-body-text').html(not.message);
$('#admin-notification-modal .modal-content').addClass('panel-' + not.modal_class);
$('#admin-notification-modal .modal-content').addClass('panel-' + not.display_class);
$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16));
$('#admin-notification-modal').modal();

Expand All @@ -114,11 +114,11 @@ $(document).ready(function() {
log('nr', nr);

var url = $(that).data('url');
var type = $(that).data('type');
var importance = $(that).data('importance');
var status = $(that).data('status');

// Important notifications are shown as pop-up on load
if (type == 'important' && status == 'new') {
if (importance == 3 && status == 'new') {
showNotificationModal(that, url);
log('stoploop');
return false; // Stop loop
Expand Down

0 comments on commit cc07b9b

Please sign in to comment.