public function index()
{
$books = $this->bookRepo->getAllPaginated(10);
- $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
- return view('books/index', ['books' => $books, 'recents' => $recents]);
+ $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
+ $popular = $this->bookRepo->getPopular(4, 0);
+ return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
}
/**
return Views::getUserRecentlyViewed($count, $page, $this->book);
}
+ public function getPopular($count = 10, $page = 0)
+ {
+ return Views::getPopular($count, $page, $this->book);
+ }
+
/**
* Get a book by slug
* @param $slug
return 1;
}
+
+ /**
+ * Get the entities with the most views.
+ * @param int $count
+ * @param int $page
+ * @param bool|false $filterModel
+ */
+ public function getPopular($count = 10, $page = 0, $filterModel = false)
+ {
+ $skipCount = $count * $page;
+ $query = $this->view->select('id', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
+ ->groupBy('viewable_id', 'viewable_type')
+ ->orderBy('view_count', 'desc');
+
+ if($filterModel) $query->where('viewable_type', '=', get_class($filterModel));
+
+ $views = $query->with('viewable')->skip($skipCount)->take($count)->get();
+ $viewedEntities = $views->map(function ($item) {
+ return $item->viewable()->getResults();
+ });
+ return $viewedEntities;
+ }
+
/**
* Get all recently viewed entities for the current user.
* @param int $count
@endif
</div>
<div class="col-sm-4 col-sm-offset-1">
+ <div id="recents">
+ @if($recents)
+ <div class="margin-top large"> </div>
+ <h3>Recently Viewed</h3>
+ @include('partials/entity-list', ['entities' => $recents])
+ @endif
+ </div>
<div class="margin-top large"> </div>
- @if($recents)
- <h3>Recently Viewed</h3>
- @include('partials/entity-list', ['entities' => $recents])
- @endif
+ <div id="popular">
+ <h3>Popular Books</h3>
+ @if(count($popular) > 0)
+ @include('partials/entity-list', ['entities' => $popular])
+ @else
+ <p class="text-muted">The most popular books will appear here.</p>
+ @endif
+ </div>
</div>
</div>
</div>
@if(count($entities) > 0)
- @foreach($entities as $entity)
+ @foreach($entities as $index => $entity)
@if($entity->isA('page'))
@include('pages/list-item', ['page' => $entity])
@elseif($entity->isA('book'))
@elseif($entity->isA('chapter'))
@include('chapters/list-item', ['chapter' => $entity, 'hidePages' => true])
@endif
- <hr>
+
+ @if($index !== count($entities) - 1)
+ <hr>
+ @endif
+
@endforeach
@else
<p class="text-muted">
--- /dev/null
+<?php
+
+use Illuminate\Foundation\Testing\WithoutMiddleware;
+use Illuminate\Foundation\Testing\DatabaseMigrations;
+use Illuminate\Foundation\Testing\DatabaseTransactions;
+
+class ActivityTrackingTest extends TestCase
+{
+
+ public function testRecentlyViewedBooks()
+ {
+ $books = \BookStack\Book::all()->take(10);
+
+ $this->asAdmin()->visit('/books')
+ ->dontSeeInElement('#recents', $books[0]->name)
+ ->dontSeeInElement('#recents', $books[1]->name)
+ ->visit($books[0]->getUrl())
+ ->visit($books[1]->getUrl())
+ ->visit('/books')
+ ->seeInElement('#recents', $books[0]->name)
+ ->seeInElement('#recents', $books[1]->name);
+ }
+
+ public function testPopularBooks()
+ {
+ $books = \BookStack\Book::all()->take(10);
+
+ $this->asAdmin()->visit('/books')
+ ->dontSeeInElement('#popular', $books[0]->name)
+ ->dontSeeInElement('#popular', $books[1]->name)
+ ->visit($books[0]->getUrl())
+ ->visit($books[1]->getUrl())
+ ->visit($books[0]->getUrl())
+ ->visit('/books')
+ ->seeInNthElement('#popular .book', 0, $books[0]->name)
+ ->seeInNthElement('#popular .book', 1, $books[1]->name);
+ }
+}
$settings->put($key, $value);
}
}
+
+ /**
+ * Assert that a given string is seen inside an element.
+ *
+ * @param bool|string|null $element
+ * @param integer $position
+ * @param string $text
+ * @param bool $negate
+ * @return $this
+ */
+ protected function seeInNthElement($element, $position, $text, $negate = false)
+ {
+ $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
+
+ $rawPattern = preg_quote($text, '/');
+
+ $escapedPattern = preg_quote(e($text), '/');
+
+ $content = $this->crawler->filter($element)->eq($position)->html();
+
+ $pattern = $rawPattern == $escapedPattern
+ ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
+
+ $this->$method("/$pattern/i", $content);
+
+ return $this;
+ }
}