Skip to content

Commit

Permalink
Portfolio: Move code to function - refs BT#18201
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Jan 19, 2021
1 parent e010d3b commit b3bce06
Showing 1 changed file with 69 additions and 50 deletions.
119 changes: 69 additions & 50 deletions main/inc/lib/PortfolioController.php
Expand Up @@ -468,40 +468,11 @@ public function deleteItem(Portfolio $item)
exit;
}

public function index(HttpRequest $httpRequest)
{
$listByUser = false;

if ($httpRequest->query->has('user')) {
$this->owner = api_get_user_entity($httpRequest->query->getInt('user'));

if (empty($this->owner)) {
api_not_allowed(true);
}

$listByUser = true;
}

$currentUserId = api_get_user_id();

$actions = [];

if ($currentUserId == $this->owner->getId()) {
$actions[] = Display::url(
Display::return_icon('add.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
$this->baseUrl.'action=add_item'
);
$actions[] = Display::url(
Display::return_icon('folder.png', get_lang('AddCategory'), [], ICON_SIZE_MEDIUM),
$this->baseUrl.'action=add_category'
);
}

$frmStudentList = null;
$frmTagList = null;

$categories = [];

private function getItemsForIndex(
int $currentUserId,
bool $listByUser = false,
FormValidator $frmFilterList = null
) {
if ($this->course) {
$queryBuilder = $this->em->createQueryBuilder();
$queryBuilder
Expand All @@ -527,12 +498,8 @@ public function index(HttpRequest $httpRequest)
}
}

$frmStudentList = $this->createFormStudentFilter($listByUser);

$frmTagList = $this->createFormTagFilter();

if ($frmTagList->validate()) {
$values = $frmTagList->exportValues();
if ($frmFilterList && $frmFilterList->validate()) {
$values = $frmFilterList->exportValues();

if (!empty($values['tags'])) {
$queryBuilder
Expand Down Expand Up @@ -561,36 +528,88 @@ public function index(HttpRequest $httpRequest)

$items = $queryBuilder->getQuery()->getResult();
} else {
$categoriesCriteria = [];
$categoriesCriteria['user'] = $this->owner;

$itemsCriteria = [];
$itemsCriteria['category'] = null;
$itemsCriteria['user'] = $this->owner;

if ($currentUserId !== $this->owner->getId()) {
$categoriesCriteria['isVisible'] = true;
$itemsCriteria['isVisible'] = true;
}

$categories = $this->em
->getRepository(PortfolioCategory::class)
->findBy($categoriesCriteria);

$items = $this->em
->getRepository(Portfolio::class)
->findBy($itemsCriteria, ['creationDate' => 'DESC']);
}

return $items;
}

private function getCategoriesForIndex(int $currentUserId): array
{
$categoriesCriteria = [];
$categoriesCriteria['user'] = $this->owner;

if ($currentUserId !== $this->owner->getId()) {
$categoriesCriteria['isVisible'] = true;
}

return $this->em
->getRepository(PortfolioCategory::class)
->findBy($categoriesCriteria);
}

public function index(HttpRequest $httpRequest)
{
$listByUser = false;

if ($httpRequest->query->has('user')) {
$this->owner = api_get_user_entity($httpRequest->query->getInt('user'));

if (empty($this->owner)) {
api_not_allowed(true);
}

$listByUser = true;
}

$currentUserId = api_get_user_id();

$actions = [];

if ($currentUserId == $this->owner->getId()) {
$actions[] = Display::url(
Display::return_icon('add.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
$this->baseUrl.'action=add_item'
);
$actions[] = Display::url(
Display::return_icon('folder.png', get_lang('AddCategory'), [], ICON_SIZE_MEDIUM),
$this->baseUrl.'action=add_category'
);
}

$frmStudentList = null;
$frmTagList = null;

$categories = [];

if ($this->course) {
$frmTagList = $this->createFormTagFilter();
$frmStudentList = $this->createFormStudentFilter($listByUser);
} else {
$categories = $this->getCategoriesForIndex($currentUserId);
}

$items = $this->getItemsForIndex($currentUserId, $listByUser, $frmTagList);

$template = new Template(null, false, false, false, false, false, false);
$template->assign('list_by_user', $listByUser);
$template->assign('user', $this->owner);
$template->assign('course', $this->course);
$template->assign('session', $this->session);
$template->assign('portfolio', $categories);
$template->assign('uncategorized_items', $items);
$template->assign('frm_student_list', $frmStudentList ? $frmStudentList->returnForm() : '');
$template->assign('frm_tag_list', $frmTagList ? $frmTagList->returnForm() : '');
$template->assign('frm_student_list', $this->course ? $frmStudentList->returnForm() : '');
$template->assign('frm_tag_list', $this->course ? $frmTagList->returnForm() : '');

$layout = $template->get_template('portfolio/list.html.twig');
$content = $template->fetch($layout);
Expand Down

0 comments on commit b3bce06

Please sign in to comment.