<?php

/*------------------------------------------------------------------------
# com_guru
# ------------------------------------------------------------------------
# author    iJoomla
# copyright Copyright (C) 2013 ijoomla.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.ijoomla.com
# Technical Support:  Forum - http://www.ijoomla.com.com/forum/index/
-------------------------------------------------------------------------*/

defined('_JEXEC') or die('Restricted access');
jimport("joomla.aplication.component.model");

class guruModelguruScorm extends JModelList
{
	protected $_context = 'com_guru.guruscorm';
	var $_total;
	var $_pagination;

	protected function populateState($ordering = null, $direction = null)
	{
		$app = JFactory::getApplication();
		$config = JFactory::getConfig();

		$limit = $app->getUserStateFromRequest('com_guru.guruscorm.limit', 'limit', $config->get('list_limit', 20));
		$this->setState('list.limit', $limit);

		$limitstart = $app->getUserStateFromRequest('com_guru.guruscorm.limitstart', 'limitstart', '0', 'int');
		$this->setState('list.start', trim($limitstart));

		$filter_course_id = $app->getUserStateFromRequest('com_guru.guruscorm.filter_course_id', 'filter_course_id', '0', 'int');
		$this->setState('filter_course_id', trim($filter_course_id));

		$filter_keyword = $app->getUserStateFromRequest('com_guru.guruscorm.filter_keyword', 'filter_keyword', '', 'string');
		$this->setState('filter_keyword', trim($filter_keyword));
	}

	function getListQuery()
	{
		$db = JFactory::getDbo();

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

		$query->select('a.*,b.name as course_name');
		$query->from($db->quoteName('#__guru_scorm_items', 'a'));
		$query->join('INNER', $db->quoteName('#__guru_program', 'b') . ' ON (' . $db->quoteName('a.course_id') . ' = ' . $db->quoteName('b.id') . ')');


		$user = JFactory::getUser();
		$query->where($db->quoteName('a.author_id') . " = " . $db->quote($user->id));

		$course_id = $this->getState('filter_course_id');
		if ($course_id) {
			$query->where($db->quoteName('a.course_id') . " = " . $db->quote($course_id));
		}

		$filter_keyword = $this->getState('filter_keyword');
		if ($filter_keyword) {
			$query->where($db->quoteName('a.title') . " LIKE " . $db->quote('%' . $filter_keyword . '%'));
		}

		$query->order('id DESC');

		return $query;
	}

	function getMyScorm($courses, $filter = '')
	{
		if (empty($courses))
			return false;

		$db = JFactory::getDbo();

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

		$query->select('a.*,b.name as course_name');
		$query->from($db->quoteName('#__guru_scorm_items', 'a'));
		$query->join('INNER', $db->quoteName('#__guru_program', 'b') . ' ON (' . $db->quoteName('a.course_id') . ' = ' . $db->quoteName('b.id') . ')');
		$query->where($db->quoteName('a.course_id') . " IN (" . $courses . ")");

		// filter bu course
		if (!empty($filter['course_id'])) {
			$query->where($db->quoteName('a.course_id') . " = " . $db->quote($filter['course_id']));
		}
		// filter by keyword
		if (!empty($filter['keyword'])) {
			$query->where($db->quoteName('a.title') . " LIKE " . $db->quote('%' . $filter['keyword'] . '%'));
		}

		// show scorm active only
		$date = JFactory::getDate();
		$query->where($db->quoteName('start') . ' <= ' . $db->Quote($date));
		$query->where($db->quoteName('end') . ' >= ' . $db->Quote($date));

		$query->order('end ASC');

		$db->setQuery($query);
		$results = $db->loadObjectList();

		$this->_total = $filter['limit']; //$db->loadResult();

		return $results;
	}
}
