Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log comments #179

Merged
merged 3 commits into from Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/Http/Controllers/ChapterController.php
Expand Up @@ -3,14 +3,13 @@
namespace App\Http\Controllers;

use App\Chapter;
use Illuminate\Http\Request;
use Symfony\Component\Yaml\Yaml;

class ChapterController extends Controller
{
public function index()
{
$chapters = Chapter::with('exercises')->get()->groupBy('parent_id');
$chapters = Chapter::with('exercises')->get()->groupBy('parent_id');

return view('chapter.index', compact('chapters'));
}

Expand All @@ -20,7 +19,8 @@ public function show(Chapter $chapter)
'parent',
'children',
'users',
'exercises'
'exercises',
'comments'
]);
return view('chapter.show', compact('chapter'));
}
Expand Down
27 changes: 16 additions & 11 deletions app/Http/Controllers/CommentController.php
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Comment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class CommentController extends Controller
Expand All @@ -17,13 +18,24 @@ public function store(Request $request)
'content' => 'required|string|min:1|max:500'
]
);
$commentableModel = $request->get('commentable_type');
$commentableId = $request->get('commentable_id');
/** @var Model $commentable */
$commentable = $commentableModel::findOrFail($commentableId);

$user = auth()->user();

$comment = $user->comments()->save(
Comment::make($request->all())
);

return $this->redirectBackToComment($comment);
activity()
->performedOn($commentable)
->causedBy($user)
->withProperties(['comment' => $comment, 'url' => $this->getCommentUrl($comment)])
->log('commented');

return redirect($this->getCommentUrl($comment));
}

public function update(Request $request, Comment $comment)
Expand All @@ -34,7 +46,7 @@ public function update(Request $request, Comment $comment)
$content = $request->get('content', $comment->content);
$comment->update(['content' => $content]);

return $this->redirectBackToComment($comment);
return redirect($this->getCommentUrl($comment));
}

public function destroy(Comment $comment)
Expand All @@ -43,15 +55,8 @@ public function destroy(Comment $comment)

return back();
}

/**
* @param Comment $comment
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
private function redirectBackToComment(Comment $comment)
private function getCommentUrl(Comment $comment)
{
return redirect(
url()->previous() . '#comment-' . $comment->id
);
return url()->previous() . '#comment-' . $comment->id;
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/UserChapterController.php
Expand Up @@ -4,8 +4,6 @@

use App\Http\Requests\User\SaveChapterRequest;
use App\User;
use Auth;
use Spatie\Activitylog\Models\Activity;

class UserChapterController extends Controller
{
Expand All @@ -21,8 +19,10 @@ public function store(SaveChapterRequest $request, User $user)
if ($log) {
activity()
->performedOn($user)
->causedBy(auth()->user())
->withProperties(['chapters' => $chapters])
->causedBy($user)
->withProperties(
['chapters' => $chapters, 'count' => count($chapters), 'url' => route('users.show', $user)]
)
->log($log);
}

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/WelcomeController.php
Expand Up @@ -2,14 +2,13 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Activitylog\Models\Activity;

class WelcomeController extends Controller
{
public function index()
{
$logItems = Activity::orderBy('created_at', 'DESC')->limit(10)->get();
$logItems = Activity::latest()->with('causer')->limit(10)->get();
return view('welcome', compact('logItems'));
}
}
1 change: 1 addition & 0 deletions resources/lang/en/activitylog.php
Expand Up @@ -7,4 +7,5 @@
'user' => 'User',
'action_added' => 'Added read chapters:',
'action_removed' => 'Removed read chapters:',
'action_commented' => 'Has left comment'
];
1 change: 1 addition & 0 deletions resources/lang/ru/activitylog.php
Expand Up @@ -7,4 +7,5 @@
'user' => 'Пользователь',
'action_added' => 'Добавил глав: ',
'action_removed' => 'Удалил глав из прочитанных: ',
'action_commented' => 'Оставил комментарий'
];
33 changes: 22 additions & 11 deletions resources/views/welcome.blade.php
Expand Up @@ -18,27 +18,38 @@
<a href="{{ route('users.show', $logItem->causer->id) }}">{{ $logItem->causer->name }}</a>
@endif
</strong>
<a href="#">{{ $logItem->created_at }}</a>
<a href="{{ $logItem->getExtraProperty('url') ?? '#' }}">{{ $logItem->created_at }}</a>
</div>
@if($logItem->description === 'commented')
<a href="{{ $logItem->getExtraProperty('url') }}">
{{ __('activitylog.action_'.$logItem->description) }}
</a>
<span>
{{ $logItem->getExtraProperty('comment.content') }}
</span>
@else
<div class="d-block">
@if($logItem->getExtraProperty('chapters'))
<a data-toggle="collapse" href="#collapseExp{{ $logItem->id }}" aria-expanded="false" aria-controls="collapseExp{{ $logItem->id }}">
<a data-toggle="collapse"
href="#collapseExp{{ $logItem->id }}"
aria-expanded="false"
aria-controls="collapseExp{{ $logItem->id }}">
{{ __('activitylog.action_'.$logItem->description) }}
{{ count($logItem->getExtraProperty('chapters')) }}</a>
{{ $logItem->getExtraProperty('count') ?? count($logItem->getExtraProperty('chapters')) }}
</a>
<div class="collapse" id="collapseExp{{ $logItem->id }}">
<ul>
@foreach($logItem->getExtraProperty('chapters') as $chapter)
<li>{{ $chapter }} {{ getChapterName($chapter) }}</li>
@if($logItem->getExtraProperty('chapters'))
<ul>
@foreach(($logItem->getExtraProperty('chapters')) as $chapter)
<li>{{ $chapter }} {{ getChapterName($chapter) }}</li>
@endforeach
</ul>
</ul>
@endif
</div>
@endif
</div>

@endif
</div>
</div>
@endforeach

</div>
<div class="col-md-4">
<h2 class="my-3">{{ __('welcome.what_is_here') }}</h2>
Expand Down