Skip to content

Commit

Permalink
Fixed issue #12052: Overlapping messages
Browse files Browse the repository at this point in the history
Dev: Implemented the new notification System for updates
  • Loading branch information
lacrioque committed Jan 13, 2017
1 parent a544b4f commit 4b298a4
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
28 changes: 27 additions & 1 deletion application/core/Survey_Common_Action.php
Expand Up @@ -384,10 +384,36 @@ function _updatenotification()
{
$updateModel = new UpdateForm();
$updateNotification = $updateModel->updateNotification;
$urlUpdate = Yii::app()->createUrl("admin/update");
$urlUpdateNotificationState = Yii::app()->createUrl("admin/update/sa/notificationstate");
$currentVersion = Yii::app()->getConfig("buildnumber");
$superadmins = User::model()->getSuperAdmins();

if($updateNotification->result)
{
return $this->getController()->renderPartial("/admin/update/_update_notification", array('security_update_available'=>$updateNotification->security_update));
if($updateNotification->security_update)
{
UniqueNotification::broadcast(array(
'title' => gT('Security update!')." (".gT("Current version: ").$currentVersion.")",
'message' => gT('A security update is available.')." <a href=".$urlUpdate.">".gT('Click here to use ComfortUpdate.')."</a>"
), $superadmins);
}
else if(Yii::app()->session['unstable_update'] )
{
UniqueNotification::broadcast(array(
'title' => gT('New UNSTABLE update available')." (".gT("Current version: ").$currentVersion.")",
'markAsNew' => false,
'message' => gT('A security update is available.')."<a href=".$urlUpdate.">".gT('Click here to use ComfortUpdate.')."</a>"
), $superadmins);
}

This comment has been minimized.

Copy link
@LouisGac
else
{
UniqueNotification::broadcast(array(
'title' => gT('New update available')." (".gT("Current version: ").$currentVersion.")",
'markAsNew' => false,
'message' => gT('A security update is available.')."<a href=".$urlUpdate.">".gT('Click here to use ComfortUpdate.')."</a>"
), $superadmins);
}
}
}
}
Expand Down
101 changes: 101 additions & 0 deletions application/models/UniqueNotification.php
@@ -0,0 +1,101 @@
<?php

/**
* Subclass of Notification, but with unique constraint.
* If a new message is created exactly like another one,
* it will be marked as unread.
*/
class UniqueNotification extends Notification
{

/**
* Whether or not this message should be marked as unread ('new')
* second time it's saved.
* @var boolean
*/
protected $markAsNew = true;

/**
* Wheather or not the importance should be set to normal
* second time it's saved.
* @var boolean
*/
protected $setNormalImportance = true;

/**
* As parent constructor but with markAsUndread
* @return UniqueNotification
*/
public function __construct($options = null)
{
parent::__construct($options);

if (isset($options['markAsNew']))
{
$this->markAsNew = $options['markAsNew'];
}

if (isset($options['setNormalImportance']))
{
$this->setNormalImportance = $options['setNormalImportance'];
}
}

/**
* Check for already existing notification and
* update that. Importance will be set to normal.
* @param boolean $runValidation Yii
* @param ? $attributes Yii
* @return void
*/
public function save($runValidation = true, $attributes = null)
{
$toHash = $this->entity . $this->entity_id . $this->title . $this->message;
$this->hash = hash('sha256', $toHash);

$duplicate = self::model()->findByAttributes(array(
'hash' => $this->hash
));

if (empty($duplicate))
{
parent::save($runValidation, $attributes);
}
else
{

if ($this->markAsNew)
{
$duplicate->status = 'new';
}

if ($this->setNormalImportance)
{
$duplicate->importance = self::NORMAL_IMPORTANCE;
}

$duplicate->update();
}

}
/**
* Broadcast a unique message to all users
* See example usage at manual page: https://manual.limesurvey.org/Notifications#Examples
* @param array $options
* @param array $users
*/
public static function broadcast(array $options, array $users = null)
{
// Get all users if no $users were given
if ($users === null)
{
$users = User::model()->findAll();
}

foreach ($users as $user) {
$options['user_id'] = $user->uid;
$not = new UniqueNotification($options);
$not->save();
}
}
}

1 comment on commit 4b298a4

@tammoterhark
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man, you guys are fast! Great. Will test soon.

Please sign in to comment.