<?php
defined('_JEXEC') or die;

jimport('joomla.user.helper');

class ModCourseListHelper
{
	public static function getCourses()
	{
		$lang = self::getLang();

		$db = JFactory::getDbo();

		$query = $db->getQuery(true);

		$query->select(array('c.*'));
		$query->from($db->quoteName('www_archline_hu.aleducation_courses', 'c'));
		$query->where($db->quoteName('c.lang') . ' = ' . $db->quote($lang));
		$query->where($db->quoteName('c.published') . ' = ' . $db->quote('Y'));

		// Reset the query using our newly populated query object.
		$db->setQuery($query);

		return $db->loadObjectList();
	}

	public static function getApplicationPercentage($courseId, $userId)
	{
		$db = JFactory::getDbo();

		$query = $db->getQuery(true);

		$query->select(array('e.*'));
		$query->from($db->quoteName('www_archline_hu.aleducation_applications', 'e'));
		$query->where($db->quoteName('e.user_id') . ' = ' . $db->quote($userId));
		$query->where($db->quoteName('e.course_id') . ' = ' . $db->quote($courseId));

		$db->setQuery($query);

		$result = $db->loadObject();

		return empty($result) ? 0 : $result->percentage;
	}

	public static function getApplications($userId)
	{
		$db = JFactory::getDbo();

		$query = $db->getQuery(true);

		$query->select(array('e.*'));
		$query->from($db->quoteName('www_archline_hu.aleducation_applications', 'e'));
		$query->where($db->quoteName('e.user_id') . ' = ' . $db->quote($userId));

		$db->setQuery($query);

		return $db->loadObjectList();
	}

	public static function getColor()
	{
		return self::getLang() == "hun" ? "#e67e22" : "#3897d3";
	}

	public static function getLang()
	{
		$lang = JFactory::getLanguage()->getTag();

		if ($lang == 'de-DE')
			return 'de';

		if ($lang == 'hu-HU')
			return 'hun';

		return 'eng';
	}
}
