Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue #12052: Overlapping messages
Dev: Implemented the new notification System for updates
- Loading branch information
Showing
2 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
There was a problem hiding this comment.
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, use the original string from:
https://github.com/LimeSurvey/LimeSurvey/blob/master/application/views/admin/update/_update_notification.php