2015-08-26 16:18:58 +09:30
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Core\Discussions;
|
2015-02-24 20:33:18 +10:30
|
|
|
|
2015-07-18 22:59:47 +09:30
|
|
|
use Flarum\Events\DiscussionWasRead;
|
2015-07-04 12:24:48 +09:30
|
|
|
use Flarum\Core\Model;
|
2015-07-01 16:47:07 +09:30
|
|
|
use Flarum\Core\Support\EventGenerator;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-02-24 20:33:18 +10:30
|
|
|
|
2015-07-01 16:47:07 +09:30
|
|
|
/**
|
|
|
|
* Models a discussion-user state record in the database.
|
|
|
|
*
|
|
|
|
* Stores information about how much of a discussion a user has read. Can also
|
|
|
|
* be used to store other information, if the appropriate columns are added to
|
|
|
|
* the database, like a user's subscription status for a discussion.
|
2015-07-07 19:20:18 +09:30
|
|
|
*
|
|
|
|
* @todo document database columns with @property
|
2015-07-01 16:47:07 +09:30
|
|
|
*/
|
2015-02-24 20:33:18 +10:30
|
|
|
class DiscussionState extends Model
|
|
|
|
{
|
2015-07-01 16:47:07 +09:30
|
|
|
use EventGenerator;
|
|
|
|
|
2015-02-24 20:33:18 +10:30
|
|
|
/**
|
2015-07-04 12:24:48 +09:30
|
|
|
* {@inheritdoc}
|
2015-02-24 20:33:18 +10:30
|
|
|
*/
|
|
|
|
protected $table = 'users_discussions';
|
|
|
|
|
|
|
|
/**
|
2015-07-04 12:24:48 +09:30
|
|
|
* {@inheritdoc}
|
2015-02-24 20:33:18 +10:30
|
|
|
*/
|
2015-07-18 22:59:47 +09:30
|
|
|
protected $dates = ['read_time'];
|
2015-02-24 20:33:18 +10:30
|
|
|
|
|
|
|
/**
|
2015-07-01 16:47:07 +09:30
|
|
|
* Mark the discussion as being read up to a certain point. Raises the
|
|
|
|
* DiscussionWasRead event.
|
2015-02-24 20:33:18 +10:30
|
|
|
*
|
2015-07-01 16:47:07 +09:30
|
|
|
* @param int $number
|
2015-02-24 20:33:18 +10:30
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function read($number)
|
|
|
|
{
|
|
|
|
if ($number > $this->read_number) {
|
|
|
|
$this->read_number = $number;
|
|
|
|
$this->read_time = time();
|
|
|
|
|
|
|
|
$this->raise(new DiscussionWasRead($this));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the relationship with the discussion that this state is for.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function discussion()
|
|
|
|
{
|
2015-07-04 12:24:48 +09:30
|
|
|
return $this->belongsTo('Flarum\Core\Discussions\Discussion', 'discussion_id');
|
2015-02-24 20:33:18 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the relationship with the user that this state is for.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2015-07-04 12:24:48 +09:30
|
|
|
return $this->belongsTo('Flarum\Core\Users\User', 'user_id');
|
2015-02-24 20:33:18 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the keys for a save update query.
|
|
|
|
*
|
2015-07-04 12:24:48 +09:30
|
|
|
* @param Builder $query
|
|
|
|
* @return Builder
|
2015-02-24 20:33:18 +10:30
|
|
|
*/
|
2015-07-01 16:47:07 +09:30
|
|
|
protected function setKeysForSaveQuery(Builder $query)
|
2015-02-24 20:33:18 +10:30
|
|
|
{
|
|
|
|
$query->where('discussion_id', $this->discussion_id)
|
|
|
|
->where('user_id', $this->user_id);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|