<?php
require_once('crm_database.class.php');

class Course
{
    public static function getCourseByUrl($url)
    {
        $query = "SELECT * FROM `" . JMLADATBAZIS . "`.`aleducation_courses` WHERE url = ? AND lang = 'de'";
        Database::getInstance()->query($query, array('s', $url));

        return Database::getInstance()->fetchNext();
    }

    public static function getCourseApplication($courseId, $userId)
    {
        $query = "SELECT * FROM `" . JMLADATBAZIS . "`.`aleducation_applications` WHERE user_id = ? AND course_id = ?";
        Database::getInstance()->query($query, array('ii', $userId, $courseId));

        return Database::getInstance()->fetchNext();
    }

    public static function getApplicationPercentage($courseId, $userId)
    {
        $query = "SELECT * FROM www_archline_hu.aleducation_applications WHERE user_id = {$userId} AND course_id = {$courseId}";
        Database::getInstance()->query($query, array('ii', $userId, $courseId));

        $result = Database::getInstance()->fetchNext();

        return empty($result) ? 0 : $result['percentage'];
    }

    public static function get_string_between($string, $start, $end)
    {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
}
