<?php
/**
 * available variables:
 * taxonomy = list of taxonomy terms
 * quizzes = list of nodes with selected taxonomy or default taxonomy
 * selected_tid = current selected tid
 * quizzes_rank = quizzes ranking
 */
?>
<?php if (isset($taxonomy[$selected_tid])): ?>
	<div class="bigw-contest col-md-12">
		<div class="row init">
			<p class="browse-category"><?= t('Przeglądasz') ?>:</p>
			<div class="dropdown dropdown-nohover">
				<h1 class="dropwdown-toggle selected-category" data-toggle="dropdown" href="#"><?= $taxonomy[$selected_tid]->name; ?><span class="caret"></span></h1>
				<ul class="dropdown-menu choose-category" role="menu" aria-labelledby="dLabel">
					<?php foreach($taxonomy as $tid => $taxonomy_term):?>
						<li>
							<a tabindex="-1" href="/contest/<?= $tid;?>"><?= $taxonomy_term->name;?></a>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>
			<a class="link-back btn btn-default" href="/contest"><< <?= t("Powrót do kategorii") ?></a>
		</div>
		<div class="row">
			<?php
			if ($quizzes != NULL):
				$i = 1;
				foreach ($quizzes as $quiz_id => $quiz): ?>
					<?php
					$image = isset($quiz->field_image[LANGUAGE_NONE]) ? $quiz->field_image[LANGUAGE_NONE][0]["uri"] : '';
					?>
					<div class="col-5 col-sm-4 col-xs-12 no-padding row-<?= $i; ?>">
						<div class="contest-item">
							<div class="contest-image">
								<?php if (isset($image)) : ?>
									<img src="<?= image_style_url('contest', $image); ?>">
								<?php endif; ?>
							</div>
							<div class="contest-details">
								<h2 class="quiz-title">
									<?php
									$quiz_title = $quiz->title;
									if(strlen($quiz_title) > 45 ) {
										$quiz_title = substr($quiz_title, 0, 45) . "...";
									}
									print $quiz_title;
									;?></h2>
								<a
									class="btn btn-default quiz-start" href="<?= base_path() . drupal_get_path_alias('node/' . $quiz->nid); ?>"><?= t('rozpocznij'); ?>
								</a>
								<a class="quiz-more" data-toggle="modal" data-target="#quiz-info-<?= $quiz->nid; ?>"><?= t('Aktualny ranking'); ?></a>
							</div>
						</div>
					</div>
					<div id="quiz-info-<?= $quiz->nid ?>" class="modal fade" role="dialog">
						<div class="modal-dialog">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" class="close" data-dismiss="modal">&times;</button>
								</div>
								<div class="modal-body">
									<div class="contest-detail-image">
										<?php if (isset($image)) : ?>
											<img src="<?= image_style_url('contest', $image); ?>">
										<?php endif; ?>
									</div>
									<a
										class="btn btn-default quiz-detail-start" href="<?= base_path() . drupal_get_path_alias('node/' . $quiz->nid); ?>"><?= t('rozpocznij'); ?>
									</a>
									<h3 class="contest-detail-title"> <?= $quiz->title; ?></h3>
									<div class="quiz-summary">
										<?php if (isset($quiz->body[LANGUAGE_NONE])):?>
											<?= $quiz->body[LANGUAGE_NONE][0]['summary'];?>
										<?php endif; ?>
									</div>
									<div class="contest-rank">
										<?php
										if (isset($quizzes_rank[$quiz_id])):
											foreach ($quizzes_rank[$quiz_id] as $user_nid => $quiz_rank): ?>
												<?php if (isset($quiz_rank['user_data']) && $quiz_rank['user_data']) : ?>
													<div class="leader col-xs-12">
														<?php if (isset($quiz_rank['user_data']->field_user_image[LANGUAGE_NONE])) : ?>
															<div class="result-img">
																<img src="<?= image_style_url('profile_image', $quiz_rank['user_data']->field_user_image[LANGUAGE_NONE][0]['uri']); ?>">
															</div>
														<?php endif; ?>
														<?php if (!empty($quiz_rank['user_data'])): ?>
															<div class="result-user"><?= $quiz_rank['user_data']->name; ?></div>
														<?php endif; ?>
														<?php if($quiz_rank['points'] !== ''): ?>
															<div class="result-points"><?= $quiz_rank['points']; ?></div>
														<?php endif; ?>
													</div>
												<?php endif; ?>
												<?php
											endforeach;
										endif;
										?>
									</div>
								</div>
								<div class="modal-footer">
									<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
								</div>
							</div>
						</div>
					</div>
					<?php
					$i++;
				endforeach;
			else:
				?>
				<div class="row no-results">
					<h2><?= t('Brak nowych konkursów w tej kategorii'); ?></h2>
				</div>
			<?php endif; ?>
		</div>
	</div>
<?php else: ?>
	<div class="row no-results">
		<?= t('Brak nowych konkursów w tej kategorii'); ?>
		<a href="/contest"><?= t("Powrót do kategorii") ?></a>
	</div>
<?php endif; ?>
