Commit 139c67fe authored by Rebecca Blundell's avatar Rebecca Blundell Committed by Cecilia Vela Gurovic

Bug 1866148: Add function first view in a collection code

behatnotneeded
Change-Id: I349f76b7808f29191c4b28249281decfe49f14ec
parent 98c089e3
......@@ -17,19 +17,18 @@ require_once(get_config('libroot') . 'collection.php');
$id = param_integer('id');
$collection = new \Collection($id);
$viewIds = $collection->get_viewids();
if (empty($viewIds)) {
$firstview = $collection->first_view();
if ($firstview === null) {
json_reply(true, array(
'message' => get_string('noviewsincollection', 'collection'),
));
}
$firstViewId = $viewIds[0];
if (!can_view_view($firstViewId)) {
if (!can_view_view($firstview)) {
json_reply('local', get_string('accessdenied', 'error'));
}
$firstview = new View($firstViewId);
$collection = $firstview->get('collection');
if ($firstview->uses_new_layout()) {
......@@ -37,7 +36,7 @@ if ($firstview->uses_new_layout()) {
$newlayout = true;
}
else {
$blocks = $view->build_rows();
$blocks = $firstview->build_rows();
$newlayout = false;
}
......
......@@ -27,7 +27,7 @@ if ($firstview->uses_new_layout()) {
$newlayout = true;
}
else {
$blocks = $view->build_rows();
$blocks = $firstview->build_rows();
$newlayout = false;
}
......
......@@ -673,6 +673,30 @@ class Collection {
return $this->views;
}
/**
* Returns first view in the current collection
*
* @return View the first view of the collection, null if the collection is empty
*/
public function first_view() {
$viewid = get_field('collection_view', 'view', 'collection', $this->get('id'), 'displayorder', '0');
$viewid = get_field_sql("SELECT cv.view
FROM {collection_view} cv
WHERE cv.collection = ?
AND cv.displayorder = (
SELECT MIN(displayorder)
FROM {collection_view} cv2
WHERE cv2.collection = ?)",
array($this->get('id'), $this->get('id')));
if ($viewid) {
require_once('view.php');
$view = new View($viewid);
return $view;
}
return null;
}
/**
* Check that a collection can have a framework
*
......
......@@ -57,8 +57,7 @@ $owner = $collection->get('owner');
$views = $collection->get('views');
// Get the first view from the collection
$firstview = $views['views'][0];
$view = new View($firstview->id);
$view = $collection->first_view();
if (!can_view_view($view->get('id'))) {
throw new AccessDeniedException();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment