function jlms_booking_save($course_id, $option)
{
    global $my, $JLMS_DB, $Itemid, $JLMS_SESSION, $JLMS_CONFIG;
    $cid = intval(mosgetparam($_POST, 'p_id', 0));
    //$usertype = $JLMS_CONFIG->get('current_usertype', 0);
    $sel_option = intval(mosgetparam($_POST, "sel_option", 0));
    $from_time = mosgetparam($_POST, 'from_time', 0);
    $to_time = mosgetparam($_POST, 'to_time', 0);
    $from_minutes = mosgetparam($_POST, 'from_min', 0);
    $to_minutes = mosgetparam($_POST, 'to_min', 0);
    $teacher_id = intval(mosgetparam($_POST, 'teacher_id', 0));
    $c_public = intval(mosgetparam($_POST, 'c_public', 0));
    $p_name = strval(mosgetparam($_POST, 'p_name', ''));
    $p_description = strval(mosgetparam($_POST, 'p_description', ''));
    $JLMS_ACL =& JLMSFactory::getACL();
    if ($course_id && $JLMS_ACL->CheckPermissions('conference', 'manage')) {
        //	if ( $course_id && ($usertype == 1)) {
        if (!defined('_JLMS_CONF_BOOK_EXISTS')) {
            define('_JLMS_CONF_BOOK_EXISTS', 'Conference with the same time period is already exists');
        }
        if ($cid) {
            $save_date = JLMS_dateToDB(mosGetParam($_REQUEST, 'start_date', date('Y-m-d')));
            $daytime_from = mktime($from_time, $from_minutes, 0, substr($save_date, 5, 2), substr($save_date, 8, 2), substr($save_date, 0, 4));
            $daytime_to = mktime($to_time, $to_minutes, 0, substr($save_date, 5, 2), substr($save_date, 8, 2), substr($save_date, 0, 4));
            if ($teacher_id) {
                $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . ")) AND p_id <> {$cid}";
            } else {
                $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE course_id='" . $course_id . "' AND teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . ")) AND p_id <> {$cid}";
            }
            $JLMS_DB->setQuery($query);
            if ($JLMS_DB->LoadResult()) {
                echo "<script> alert('" . _JLMS_CONF_BOOK_EXISTS . "'); window.history.go(-1); </script>\n";
                exit;
            }
            if ($c_public) {
                $query = "SELECT count(*) FROM #__lms_conference_usr WHERE p_id = {$cid}";
                $JLMS_DB->setQuery($query);
                $is_subscribers = $JLMS_DB->LoadResult();
                if ($is_subscribers) {
                    $c_public = 0;
                }
            }
            $query = "UPDATE #__lms_conference_period SET teacher_id = {$teacher_id}, from_time = '" . $daytime_from . "',to_time= '" . $daytime_to . "',public = '" . $c_public . "',p_name = " . $JLMS_DB->Quote($p_name) . ", p_description = " . $JLMS_DB->Quote($p_description);
            $query .= "\n WHERE p_id = {$cid}";
            $JLMS_DB->setQuery($query);
            $JLMS_DB->query();
        } else {
            if ($sel_option) {
                $all_dates = array();
                if (count($_POST['weekday']) && count($_POST['monthday'])) {
                    foreach ($_POST['monthday'] as $month_year) {
                        $month = substr($month_year, 0, 2);
                        $year = substr($month_year, 3, 4);
                        $all_dates = Daysforweeks($_POST['weekday'], $month, $year);
                        if (count($all_dates)) {
                            foreach ($all_dates as $adata) {
                                $daytime_from = mktime($from_time, $from_minutes, 0, substr($adata, 5, 2), substr($adata, 8, 2), substr($adata, 0, 4));
                                $daytime_to = mktime($to_time, $to_minutes, 0, substr($adata, 5, 2), substr($adata, 8, 2), substr($adata, 0, 4));
                                //$query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE course_id='".$course_id."' AND teacher_id = $teacher_id AND ((from_time < ".$daytime_from." AND to_time > ".$daytime_from.") OR (from_time < ".$daytime_to." AND to_time > ".$daytime_to."))";
                                if ($teacher_id) {
                                    $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . "))";
                                } else {
                                    $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE course_id='" . $course_id . "' AND teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . "))";
                                }
                                $JLMS_DB->setQuery($query);
                                if ($JLMS_DB->LoadResult()) {
                                    echo "<script> alert('" . _JLMS_CONF_BOOK_EXISTS . "'); window.history.go(-1); </script>\n";
                                    exit;
                                }
                                $query = "INSERT INTO #__lms_conference_period(course_id, teacher_id, from_time, to_time,public, p_name, p_description)";
                                $query .= "\n VALUES ('" . $course_id . "', {$teacher_id}, '" . $daytime_from . "','" . $daytime_to . "', {$c_public}, " . $JLMS_DB->Quote($p_name) . ", " . $JLMS_DB->Quote($p_description) . ")";
                                $JLMS_DB->setQuery($query);
                                $JLMS_DB->query();
                            }
                        }
                    }
                }
            } else {
                $save_date = JLMS_dateToDB(mosGetParam($_REQUEST, 'start_date', date('Y-m-d')));
                $daytime_from = mktime($from_time, $from_minutes, 0, substr($save_date, 5, 2), substr($save_date, 8, 2), substr($save_date, 0, 4));
                $daytime_to = mktime($to_time, $to_minutes, 0, substr($save_date, 5, 2), substr($save_date, 8, 2), substr($save_date, 0, 4));
                //$query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE course_id='".$course_id."' AND teacher_id = $teacher_id AND ((from_time < ".$daytime_from." AND to_time > ".$daytime_from.") OR (from_time < ".$daytime_to." AND to_time > ".$daytime_to."))";
                if ($teacher_id) {
                    $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . "))";
                } else {
                    $query = "SELECT COUNT(*) FROM #__lms_conference_period WHERE course_id='" . $course_id . "' AND teacher_id = {$teacher_id} AND ((from_time > " . $daytime_from . " AND from_time < " . $daytime_to . ") OR (to_time > " . $daytime_from . " AND to_time < " . $daytime_to . ") OR (from_time < " . $daytime_from . " AND to_time > " . $daytime_from . ") OR (from_time < " . $daytime_to . " AND to_time > " . $daytime_to . ") OR (from_time = " . $daytime_from . " AND to_time = " . $daytime_to . "))";
                }
                $JLMS_DB->setQuery($query);
                if ($JLMS_DB->LoadResult()) {
                    echo "<script> alert('" . _JLMS_CONF_BOOK_EXISTS . "'); window.history.go(-1); </script>\n";
                    exit;
                }
                $query = "INSERT INTO #__lms_conference_period(course_id, teacher_id, from_time,to_time,public,p_name,p_description)";
                $query .= "\n VALUES ('" . $course_id . "', {$teacher_id}, '" . $daytime_from . "','" . $daytime_to . "', {$c_public}, " . $JLMS_DB->Quote($p_name) . ", " . $JLMS_DB->Quote($p_description) . ")";
                $JLMS_DB->setQuery($query);
                $JLMS_DB->query();
            }
        }
    }
    JLMSRedirect(sefRelToAbs('index.php?option=' . $option . '&amp;task=conference&amp;mode=booking&amp;id=' . $course_id));
}
function saveTopic($course_id)
{
    global $option, $Itemid, $JLMS_DB, $JLMS_CONFIG;
    $usertype = $JLMS_CONFIG->get('current_usertype', 0);
    if ($course_id == $JLMS_CONFIG->get('course_id') && $usertype == 1) {
        $topic_description = strval(JLMS_getParam_LowFilter($_POST, 'description', ''));
        $topic_description = JLMS_ProcessText_LowFilter($topic_description);
        $topic_name_post = isset($_REQUEST['name']) ? strval($_REQUEST['name']) : '';
        $topic_name_post = get_magic_quotes_gpc() ? stripslashes($topic_name_post) : $topic_name_post;
        $topic_name_post = ampReplace(strip_tags($topic_name_post));
        $row = new JLMS_Topic($JLMS_DB);
        $row->bind($_POST);
        $row->name = $topic_name_post;
        $row->description = $topic_description;
        $row->start_date = mosGetParam($_REQUEST, 'start_date', '0000-00-00');
        $row->start_date = JLMS_dateToDB($row->start_date);
        $row->end_date = mosGetParam($_REQUEST, 'end_date', '0000-00-00');
        $row->end_date = JLMS_dateToDB($row->end_date);
        $days = intval(mosGetParam($_POST, 'days', ''));
        $hours = intval(mosGetParam($_POST, 'hours', ''));
        $mins = intval(mosGetParam($_POST, 'mins', ''));
        if ($row->is_time_related) {
            $row->show_period = JLMS_HTML::_('showperiod.getminsvalue', $days, $hours, $mins);
        }
        //----> ordering implementation
        if (mosGetParam($_POST, 'weekly', 0)) {
            //----> 06.12.2007 - DEN - 14.12.2007 - Replaced by TPETb
            $number = intval(mosGetParam($_POST, 'number', 0));
            if ($number > 50) {
                $number = 50;
            }
            //<----
            if ($number <= 0) {
                $number = 1;
            }
            //fool-check
        } else {
            $number = 1;
            //fool-check
        }
        $ordering = $row->ordering;
        //moveup topics with higher ordering
        $query = "UPDATE #__lms_topics SET ordering=ordering+{$number} WHERE course_id={$course_id} AND ordering>={$ordering}";
        $JLMS_DB->setQuery($query);
        $JLMS_DB->query();
        //<----
        if (mosGetParam($_POST, 'weekly', 0)) {
            $fix = 2 - $JLMS_CONFIG->get('date_format_fdow', 1);
            $first_date = strtotime($row->start_date);
            $first_day = date("w", $first_date);
            $next_date = date("Y-m-d", mktime(0, 0, 0, date("m", $first_date), date("d", $first_date) + 7 - 1 - $first_day + $fix, date("Y", $first_date)));
            $row->publish_end = 1;
            $row->end_date = $next_date;
            if (empty($_POST['name'])) {
                $date_1 = intval(strftime("%d", $first_date)) . ' ' . month_lang(strftime("%m", $first_date), 0, 2);
                $date_2 = intval(strftime("%d", strtotime($next_date))) . ' ' . month_lang(strftime("%m", strtotime($next_date)), 0, 2);
                $name = $date_1 . ' - ' . $date_2;
                $row->name = $name;
            } else {
                $row->name = $topic_name_post;
            }
        }
        $row->store();
        if (mosGetParam($_POST, 'weekly', 0)) {
            for ($i = 2; $i <= $number; $i++) {
                $row = new JLMS_Topic($JLMS_DB);
                $first_date = strtotime($next_date);
                $row->start_date = date("Y-m-d", mktime(0, 0, 0, date("m", $first_date), date("d", $first_date) + 1, date("Y", $first_date)));
                $next_date = date("Y-m-d", mktime(0, 0, 0, date("m", $first_date), date("d", $first_date) + 7, date("Y", $first_date)));
                $row->course_id = $course_id;
                $row->published = 1;
                $row->publish_start = 1;
                $row->publish_end = 1;
                $row->end_date = $next_date;
                if (empty($_POST['name_' . $i])) {
                    $date_1 = intval(strftime("%d", strtotime($row->start_date))) . ' ' . month_lang(strftime("%B", strtotime($row->start_date)), 0, 2);
                    $date_2 = intval(strftime("%d", strtotime($row->end_date))) . ' ' . month_lang(strftime("%B", strtotime($row->end_date)), 0, 2);
                    $name = $date_1 . ' - ' . $date_2;
                    $row->name = $name;
                } else {
                    $row->name = isset($_POST['name_' . $i]) ? strval($_POST['name_' . $i]) : '';
                    $row->name = get_magic_quotes_gpc() ? stripslashes($row->name) : $row->name;
                    $row->name = ampReplace(strip_tags($row->name));
                }
                $row->description = $topic_description;
                $ordering++;
                $row->ordering = $ordering;
                $row->store();
            }
        }
        fixTopicOrder($course_id);
        if ($_POST['id']) {
            $msg = _JLMS_TOPIC_T_EDITED;
        } else {
            $msg = _JLMS_TOPIC_T_CREATED;
        }
        if (mosGetParam($_POST, 'weekly', 0)) {
            $msg = _JLMS_TOPIC_T_SERIES_CREATED;
        }
    } else {
        $msg = '';
        //_JLMS_TOPIC_HACK;
    }
    JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=details_course&id={$course_id}#topic_{$row->id}"), $msg);
}
function JLMS_sreportAccess($option, $is_full)
{
    global $JLMS_DB, $JLMS_CONFIG, $JLMS_SESSION, $my, $Itemid;
    $JLMS_ACL =& JLMSFactory::getACL();
    $view = mosGetParam($_REQUEST, 'view', '');
    if ($view == 'csv' || $view == 'xls') {
        $is_full = 1;
    }
    $start_date = mosGetParam($_REQUEST, 'start_date', "");
    $end_date = mosGetParam($_REQUEST, 'end_date', "");
    $limit = intval(mosGetParam($_GET, 'limit', $JLMS_SESSION->get('list_limit', $JLMS_CONFIG->get('list_limit'))));
    $JLMS_SESSION->set('list_limit', $limit);
    $limitstart = intval(mosGetParam($_GET, 'limitstart', 0));
    $filt_group = intval(mosGetParam($_REQUEST, 'filt_group', 0));
    $lists = array();
    $reporting_header = array();
    $filt_cat = intval(mosGetParam($_REQUEST, 'filt_cat', 0));
    //FLMS multicat
    $levels = array();
    if ($JLMS_CONFIG->get('multicat_use', 0)) {
        $query = "SELECT * FROM #__lms_course_cats_config ORDER BY id";
        $JLMS_DB->setQuery($query);
        $levels = $JLMS_DB->loadObjectList();
        if (count($levels) == 0) {
            for ($i = 0; $i < 5; $i++) {
                if ($i > 0) {
                    $levels[$i]->cat_name = _JLMS_COURSES_COURSES_GROUPS;
                } else {
                    $levels[$i]->cat_name = _JLMS_COURSES_COURSES_GROUPS;
                }
            }
        }
        $level_id = array();
        for ($i = 0; $i < count($levels); $i++) {
            if ($i == 0) {
                $level_id[$i] = intval(mosGetParam($_REQUEST, 'filter_id_' . $i . '', $JLMS_SESSION->get('FLMS_filter_id_' . $i . '', 0)));
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
            } else {
                $level_id[$i] = intval(mosGetParam($_REQUEST, 'filter_id_' . $i . '', $JLMS_SESSION->get('FLMS_filter_id_' . $i . '', 0)));
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
            }
            if ($i == 0) {
                $parent_id[$i] = 0;
            } else {
                $parent_id[$i] = $level_id[$i - 1];
            }
            if ($i == 0 || $parent_id[$i]) {
                //(Max): extra requests
                $query = "SELECT count(id) FROM `#__lms_course_cats` WHERE parent = '" . $parent_id[$i] . "' ORDER BY c_category";
                $JLMS_DB->setQuery($query);
                $groups = $JLMS_DB->loadResult();
                if ($groups == 0) {
                    $level_id[$i] = 0;
                    $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                }
            }
        }
        for ($i = 0; $i < count($levels); $i++) {
            if ($i > 0 && $level_id[$i - 1] == 0) {
                $level_id[$i] = 0;
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                $parent_id[$i] = 0;
            } elseif ($i == 0 && $level_id[$i] == 0) {
                $level_id[$i] = 0;
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                $parent_id[$i] = 0;
            }
        }
        $javascript = 'onclick="javascript:read_filter();" onchange="javascript:write_filter();submitFormView(\'\');"';
        $query1 = "SELECT group_id FROM `#__lms_users_in_global_groups` WHERE user_id = '" . $my->id . "'";
        $JLMS_DB->setQuery($query1);
        $user_group_ids = $JLMS_DB->loadResultArray();
        $categories_reporting = array();
        $name_categories_reporting = array();
        for ($i = 0; $i < count($levels); $i++) {
            if ($i == 0 || $parent_id[$i]) {
                //(Max): extra requests
                if ($parent_id[$i] == 0 && !$JLMS_ACL->CheckPermissions('lms', 'create_course')) {
                    $query = "SELECT * FROM `#__lms_course_cats` WHERE `parent` = '0'";
                    $query .= "\n AND (";
                    if (count($user_group_ids)) {
                        $query .= "( `restricted` = 1 AND ( `groups` LIKE '%|{$user_group_ids['0']}|%'";
                        for ($i1 = 1; $i1 < count($user_group_ids); $i1++) {
                            $query .= "\n OR `groups` like '%|{$user_group_ids[$i1]}|%'";
                        }
                        $query .= "\n ) ) \n OR ";
                    }
                    $query .= "(`restricted` = 0 )) ";
                    $query .= "\n ORDER BY `c_category`";
                } else {
                    $query = "SELECT * FROM `#__lms_course_cats` WHERE parent = '" . $parent_id[$i] . "' ORDER BY c_category";
                }
                $JLMS_DB->setQuery($query);
                $groups = $JLMS_DB->loadObjectList();
                if ($parent_id[$i] && $i > 0 && count($groups)) {
                    $type_level[$i][] = mosHTML::makeOption(0, ' &nbsp; ');
                    foreach ($groups as $group) {
                        $type_level[$i][] = mosHTML::makeOption($group->id, $group->c_category);
                        if ($group->id == $level_id[$i]) {
                            $name_categories_reporting[] = $levels[$i]->cat_name;
                            $categories_reporting[] = $group->c_category;
                        }
                    }
                    $lists['filter_' . $i . ''] = mosHTML::selectList($type_level[$i], 'filter_id_' . $i . '', 'class="inputbox" size="1" style="width: 100%;" ' . $javascript, 'value', 'text', $level_id[$i]);
                    //onchange="document.location.href=\''. $link_multi .'\';"
                } elseif ($i == 0) {
                    $type_level[$i][] = mosHTML::makeOption(0, ' &nbsp; ');
                    foreach ($groups as $group) {
                        $type_level[$i][] = mosHTML::makeOption($group->id, $group->c_category);
                        if ($group->id == $level_id[$i]) {
                            $name_categories_reporting[] = $levels[$i]->cat_name;
                            $categories_reporting[] = $group->c_category;
                        }
                    }
                    $lists['filter_' . $i . ''] = mosHTML::selectList($type_level[$i], 'filter_id_' . $i . '', 'class="inputbox" size="1" style="width: 100%;" ' . $javascript, 'value', 'text', $level_id[$i]);
                    //onchange="document.location.href=\''. $link_multi .'\';"
                }
            }
        }
        $reporting_header['name_categories'] = $name_categories_reporting;
        $reporting_header['categories'] = $categories_reporting;
    }
    //FLMS multicat
    $s_date_db = '';
    $start_date = $start_date == "-" ? "" : $start_date;
    $end_date = $end_date == "-" ? "" : $end_date;
    if ($start_date) {
        $start_date = JLMS_dateToDB($start_date);
        $s_date = explode('-', $start_date);
        $s_date_db = date("Y-m-d H:i:s", mktime(0, 0, 0, $s_date[1], $s_date[2], $s_date[0]));
    }
    $e_date_db = '';
    if ($end_date) {
        $end_date = JLMS_dateToDB($end_date);
        $e_date = explode('-', $end_date);
        $e_date_db = date("Y-m-d H:i:s", mktime(23, 59, 0, $e_date[1], $e_date[2], $e_date[0]));
    }
    $teacher_in_courses = $JLMS_CONFIG->get('teacher_in_courses', array());
    $parent_in_courses = array();
    $parent_in_courses = $JLMS_CONFIG->get('parent_in_courses', array());
    $courses = array_merge($teacher_in_courses, $parent_in_courses);
    //var_dump($courses);
    if (count($courses)) {
        $courses_str = implode(',', $courses);
        $JLMS_DB->setQuery('SELECT id FROM #__lms_courses WHERE id IN(' . $courses_str . ')');
        $courses = $JLMS_DB->loadResultArray();
        $g_items = array();
        $g_items[] = mosHTML::makeOption(0, _JLMS_ATT_FILTER_ALL_GROUPS);
        if ($JLMS_CONFIG->get('use_global_groups', 1)) {
            if (!count($courses)) {
                $courses = array(0);
            }
            $query = "SELECT user_id FROM #__lms_users_in_groups WHERE course_id IN (" . implode(',', $courses) . ")";
            $JLMS_DB->setQuery($query);
            $cid = $JLMS_DB->loadResultArray();
            if (!$cid) {
                $cid = array(-1);
            }
            $query = "SELECT group_id FROM #__lms_users_in_global_groups WHERE user_id IN (" . implode(',', $cid) . ")";
            $JLMS_DB->setQuery($query);
            $gid = $JLMS_DB->loadResultArray();
            if (!$gid) {
                $gid = array(-1);
            }
            $query = "SELECT distinct id AS value, ug_name AS text FROM #__lms_usergroups WHERE id IN (" . implode(',', $gid) . ") AND course_id = 0 ORDER BY text";
            //course id check just in case))
            $JLMS_DB->setQuery($query);
            $groups = $JLMS_DB->loadObjectList();
        } else {
            if (!count($courses)) {
                $courses = array(0);
            }
            $query = "SELECT distinct a.id as value, a.ug_name as text FROM #__lms_usergroups as a, #__lms_users_in_groups as b" . "\n WHERE a.course_id IN (" . implode(',', $courses) . ") AND b.group_id = a.id ORDER BY a.ug_name";
            $JLMS_DB->SetQuery($query);
            $groups = $JLMS_DB->LoadObjectList();
        }
        $g_items = array_merge($g_items, $groups);
        $link = "javascript:document.adminForm.submit();";
        $lists['filter'] = mosHTML::selectList($g_items, 'filt_group', 'class="inputbox" size="1" style="width: 100%;" onchange="' . $link . '"', 'value', 'text', $filt_group);
        $groups_reporting = array();
        foreach ($groups as $grp) {
            if ($filt_group && $grp->value == $filt_group) {
                $groups_reporting[] = $grp->text;
            }
        }
        $name_groups_reporting[] = 'Usergroup';
        $reporting_header['name_groups'] = $name_groups_reporting;
        $reporting_header['groups'] = $groups_reporting;
        //---
        $where = '';
        if ($JLMS_CONFIG->get('multicat_use', 0)) {
            //NEW MUSLTICATS
            //			$tmp_level = array();
            $last_catid = 0;
            $tmp_cats_filter = JLMS_getFilterMulticategories($last_catid);
            $catids = implode(",", $tmp_cats_filter);
            if ($last_catid && count($tmp_cats_filter)) {
                $where .= "\n AND ( cat_id IN (" . $catids . ")";
                if ($JLMS_CONFIG->get('sec_cat_use', 0)) {
                    foreach ($tmp_cats_filter as $tmp_cats_filter_one) {
                        $where .= "\n OR sec_cat LIKE '%|" . $tmp_cats_filter_one . "|%'";
                    }
                }
                $where .= "\n )";
            }
            //NEW MUSLTICATS
        }
        if (!$courses_str) {
            $courses_str = '0';
        }
        $query = "SELECT id FROM #__lms_courses WHERE id IN (" . $courses_str . ")" . $where . " ORDER BY course_name, id";
        $JLMS_DB->setQuery($query);
        $courses2 = $JLMS_DB->loadResultArray();
        $courses = $courses2;
        //---
        if ($JLMS_CONFIG->get('use_global_groups', 1) && $filt_group) {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_global_groups as c" . "\n WHERE a.id = c.user_id " . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '');
        } else {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_groups as c" . "\n WHERE a.id = c.user_id AND c.course_id IN (" . $courses_str . ")" . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '');
        }
        $JLMS_DB->SetQuery($query);
        $users2 = $JLMS_DB->LoadResultArray();
        require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "classes" . DS . "lms.pagination.php";
        $pageNav = new JLMSPageNav(count($users2), $limitstart, $limit);
        if ($JLMS_CONFIG->get('use_global_groups', 1) && $filt_group) {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_global_groups as c" . "\n WHERE a.id = c.user_id " . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '') . "\n ORDER BY a.name " . (!$is_full ? "LIMIT {$pageNav->limitstart}, {$pageNav->limit}" : "");
        } else {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_groups as c" . "\n WHERE a.id = c.user_id AND c.course_id IN (" . $courses_str . ")" . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '') . "\n ORDER BY a.name " . (!$is_full ? "LIMIT {$pageNav->limitstart}, {$pageNav->limit}" : "");
        }
        $JLMS_DB->SetQuery($query);
        $users = $JLMS_DB->LoadResultArray();
        //var_dump($users);
        if (count($users)) {
            // 21 May 2007
            // prepare tracking images
            // 18 August 2007 - changes (DEN) - added check for GD and FreeType support
            if ($JLMS_CONFIG->get('show_reports_images', 0)) {
                $generate_images = true;
            } else {
                $generate_images = false;
            }
            $msg = '';
            if (!function_exists('imageftbbox') || !function_exists('imagecreatetruecolor')) {
                $generate_images = false;
                $sec = false;
                if (!function_exists('imagecreatetruecolor')) {
                    $msg = 'This function requires GD 2.0.1 or later (2.0.28 or later is recommended).';
                    $sec = true;
                }
                if (!function_exists('imageftbbox')) {
                    $msg .= ($sec ? '<br />' : '') . 'This function is only available if PHP is compiled with freetype support.';
                }
            }
            // end of GD and FreeType support check
            $users_str = implode(',', $users);
            $new_image_ym_stats = new stdClass();
            if ($JLMS_CONFIG->get('temp_folder', '') && $generate_images) {
                // temp folder setup is ready.
                $img_gl_width = $JLMS_CONFIG->get('visual_set_tracking_image_base_width', 400);
                //parameter added 09.06.2007
                $img_gl_height = $JLMS_CONFIG->get('visual_set_tracking_image_base_height', 260);
                //parameter added 09.06.2007
                require_once _JOOMLMS_FRONT_HOME . "/includes/libchart/libchart.php";
                JLMS_cleanLibChartCache();
                // Year/Month statistic
                $chart = new PieChart($img_gl_width * 2, $img_gl_height + 40, false, 'no_title');
                //(700, 70 + count($img_names)*30);
                $query = "SELECT COUNT(*) as hits,c.course_name FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . implode(',', $users2) . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "  group by h.course_id ORDER BY hits desc LIMIT 10";
                $JLMS_DB->SetQuery($query);
                $img_names = $JLMS_DB->LoadObjectLIST();
                //var_dump($img_names);
                for ($i = 0, $n = count($img_names); $i < $n; $i++) {
                    $chart->addPoint(new Point($img_names[$i]->course_name, $img_names[$i]->hits));
                }
                $title = "Top 10 courses";
                //$title = JLMS_TR_temp_fix($title);
                $mas[0] = $title;
                $title = '';
                $chart->setTitle($title);
                $filename = time() . '_' . md5(uniqid(rand(), true)) . ".png";
                $new_image_ym_stats->filename = $filename;
                $new_image_ym_stats->width = $img_gl_width;
                $new_image_ym_stats->height = $img_gl_height;
                $new_image_ym_stats->alt = $title;
                $new_image_ym_stats->title = $mas[0];
                $chart->render($JLMS_CONFIG->get('absolute_path') . "/" . $JLMS_CONFIG->get('temp_folder', '') . "/{$filename}");
            }
            $query = "SELECT COUNT(*) as hits,h.course_id as c_id FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . implode(',', $users2) . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "   group by h.course_id ";
            $JLMS_DB->SetQuery($query);
            $tot_hits = $JLMS_DB->LoadObjectLIST();
            $query = "SELECT COUNT(*) as hits,h.user_id as usr_id,h.course_id as c_id FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . $users_str . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "   group by h.course_id,h.user_id ORDER BY h.course_id,h.user_id";
            $JLMS_DB->SetQuery($query);
            $hits = $JLMS_DB->LoadObjectLIST();
            //var_dump($hits);
            if ($view == 'csv') {
                JLMS_REP_exportCsv($hits, $tot_hits, $users, $courses, $pageNav, $lists, $levels, $filt_cat, $filt_group, $option, 1);
            } else {
                if ($view == 'xls') {
                    JLMS_REP_exportXLS($hits, $tot_hits, $users, $courses, $reporting_header);
                } else {
                    JLMS_reports_html::JLMS_sreportAccess($tot_hits, $new_image_ym_stats, $hits, $users, $courses, $pageNav, $start_date, $end_date, $lists, $levels, $filt_cat, $filt_group, $option, $is_full);
                }
            }
        }
    }
}
function JLMS_save_event($option)
{
    $JLMS_CONFIG =& JLMSFactory::getConfig();
    $Itemid = $JLMS_CONFIG->get('Itemid');
    global $my, $JLMS_DB;
    //$course_id     = intval(mosGetParam($_REQUEST, 'id', 0));
    $course_id = $JLMS_CONFIG->get('course_id');
    $start_date = JLMS_dateToDB(mosGetParam($_REQUEST, 'start_date', date('Y-m-d')));
    $end_date = JLMS_dateToDB(mosGetParam($_REQUEST, 'end_date', date('Y-m-d')));
    $agenda_detail = isset($_REQUEST['jlms_agenda_detail']) ? strval($_REQUEST['jlms_agenda_detail']) : '';
    $agenda_detail = get_magic_quotes_gpc() ? stripslashes($agenda_detail) : $agenda_detail;
    $agenda_detail = JLMS_ProcessText_HardFilter($agenda_detail);
    $edit = mosGetParam($_REQUEST, 'edit', '');
    $agenda_id = intval(mosGetParam($_REQUEST, 'agenda_id', 0));
    $agenda_title = isset($_REQUEST['jlms_agenda_title']) ? strval($_REQUEST['jlms_agenda_title']) : '';
    $agenda_title = get_magic_quotes_gpc() ? stripslashes($agenda_title) : $agenda_title;
    $agenda_title = ampReplace(strip_tags($agenda_title));
    $groups = mosGetParam($_REQUEST, 'groups', array(0));
    $is_limited = intval(mosGetParam($_REQUEST, 'is_limited', 0));
    $is_time_related = intval(mosGetParam($_POST, 'is_time_related', ''));
    $days = intval(mosGetParam($_POST, 'days', ''));
    $hours = intval(mosGetParam($_POST, 'hours', ''));
    $mins = intval(mosGetParam($_POST, 'mins', ''));
    if ($is_time_related) {
        $show_period = JLMS_HTML::_('showperiod.getminsvalue', $days, $hours, $mins);
    }
    $JLMS_ACL =& JLMSFactory::getACL();
    $assigned_groups_only = $JLMS_ACL->CheckPermissions('advanced', 'assigned_groups_only');
    if ($assigned_groups_only) {
        $is_limited = 1;
        $groups_where_admin_manager = JLMS_ACL_HELPER::GetAssignedGroups($my->id, $course_id);
        for ($i = 0; $i < count($groups); $i++) {
            if (!in_array($groups[$i], $groups_where_admin_manager)) {
                unset($groups[$i]);
            }
        }
        sort($groups);
    }
    $groups_in_db_arr = array();
    $query = "SELECT groups FROM #__lms_agenda WHERE agenda_id = {$agenda_id} AND course_id = {$course_id}";
    $JLMS_DB->setQuery($query);
    $groups_in_db = $JLMS_DB->LoadResult();
    if ($groups_in_db) {
        $groups_in_db = substr($groups_in_db, 1, strlen($groups_in_db) - 2);
        $groups_in_db_arr = explode('|', $groups_in_db);
        if (count($groups_where_admin_manager)) {
            $groups_in_db_arr = array_diff($groups_in_db_arr, $groups_where_admin_manager);
        }
    }
    $groups_str = '';
    if ($is_limited && (count($groups) && $groups[0] != 0 || count($groups_in_db_arr))) {
        $groups = array_merge($groups, $groups_in_db_arr);
        $razd = '|';
        for ($i = 0; $i < count($groups); $i++) {
            $groups_str .= $razd . $groups[$i];
        }
        $groups_str .= '|';
    } else {
        $groups_str = '';
    }
    //echo $groups_str; die;
    $ag_id = 0;
    if ($course_id && $JLMS_ACL->CheckPermissions('announce', 'manage')) {
        //proverka na korrektnost' end_date
        if (strtotime($end_date) < strtotime($start_date)) {
            $end_date = $start_date;
        }
        if (isset($edit) && $edit == 'yes' && $agenda_id) {
            $query = "SELECT owner_id FROM #__lms_agenda WHERE agenda_id = {$agenda_id} AND course_id = {$course_id}";
            $JLMS_DB->setQuery($query);
            $agenda_owner = $JLMS_DB->LoadResult();
            $proceed_with_edit = true;
            if ($agenda_owner) {
                if ($JLMS_ACL->CheckPermissions('announce', 'only_own') && $agenda_owner != $my->id) {
                    $proceed_with_edit = false;
                } elseif ($JLMS_ACL->CheckPermissions('announce', 'only_own_role') && $JLMS_ACL->GetRole() != $JLMS_ACL->UserSystemRole($JLMS_DB, $agenda_owner)) {
                    $proceed_with_edit = false;
                }
            }
            if ($proceed_with_edit) {
                $set = '';
                if ($is_time_related) {
                    $set = ",  show_period = '" . $show_period . "'";
                }
                $query = "UPDATE `#__lms_agenda` " . " SET title = " . $JLMS_DB->Quote($agenda_title) . ", is_limited = '" . $is_limited . "', groups = '" . $groups_str . "', content = " . $JLMS_DB->Quote($agenda_detail) . ", start_date = '" . $start_date . "', end_date = '" . $end_date . "', is_time_related = '" . $is_time_related . "'" . $set . " WHERE agenda_id = '" . $agenda_id . "' AND course_id = '" . $course_id . "' ";
                $JLMS_DB->setQuery($query);
                $JLMS_DB->query();
                $ag_id = $agenda_id;
            }
        } else {
            $row = new StdClass();
            #$row->id = 0;
            $row->course_id = $course_id;
            $row->owner_id = $my->id;
            $row->is_limited = $is_limited;
            $row->title = $agenda_title;
            $row->groups = $groups_str;
            $row->content = $agenda_detail;
            $row->start_date = $start_date;
            $row->end_date = $end_date;
            $row->is_time_related = $is_time_related;
            $row->show_period = $show_period;
            $JLMS_DB->insertobject('#__lms_agenda', $row);
            $insert_id = $JLMS_DB->insertid();
            $ag_id = $insert_id;
        }
    }
    if ($ag_id) {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&amp;Itemid={$Itemid}&amp;task=agenda&amp;id={$course_id}&amp;agenda_id=" . $ag_id));
    } else {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&amp;Itemid={$Itemid}&amp;task=agenda&amp;id={$course_id}"));
    }
}
function JLMS_saveHW($option)
{
    global $my, $JLMS_DB, $Itemid;
    $course_id = intval(mosGetParam($_REQUEST, 'course_id', 0));
    $id = intval(mosGetParam($_REQUEST, 'id', 0));
    $groups = mosGetParam($_REQUEST, 'groups', array(0));
    $is_limited = intval(mosGetParam($_REQUEST, 'is_limited', 0));
    $JLMS_ACL =& JLMSFactory::getACL();
    $assigned_groups_only = $JLMS_ACL->CheckPermissions('advanced', 'assigned_groups_only');
    //	if ( (JLMS_GetUserType($my->id, $course_id) == 1) && ( ($id && (JLMS_GetHWCourse($id) == $course_id)) || !$id ) ) {
    if ($JLMS_ACL->CheckPermissions('homework', 'manage') && ($id && JLMS_GetHWCourse($id) == $course_id || !$id)) {
        if ($id) {
            $AND_ST = "";
            $oldH = new mos_Joomla_LMS_HomeWork($JLMS_DB);
            if ($assigned_groups_only) {
                $groups_where_admin_manager = JLMS_ACL_HELPER::GetAssignedGroups($my->id, $id);
                $groups_where_isset_user = JLMS_ACL_HELPER::GetUserGlobalGroup($my->id, $id);
                $groups_where_admin_manager = array_merge($groups_where_admin_manager, $groups_where_isset_user);
                if (count($groups_where_admin_manager)) {
                    $AND_ST .= "\n AND (is_limited = 0 OR groups LIKE '%|{$groups_where_admin_manager['0']}|%'";
                    for ($i = 1; $i < count($groups_where_admin_manager); $i++) {
                        $AND_ST .= "\n OR groups like '%|{$groups_where_admin_manager[$i]}|%'";
                    }
                    $AND_ST .= "\n OR owner_id = '" . $my->id . "')";
                } else {
                    $AND_ST .= "\n AND (is_limited = 0 OR owner_id = '" . $my->id . "' OR id = 0) AND groups = ''";
                }
            }
            $oldH->addCond($AND_ST);
            $oldH->load($id);
            if (!$oldH->id) {
                JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=homework&id={$course_id}"));
            }
        }
        $row = new mos_Joomla_LMS_HomeWork($JLMS_DB);
        if (!$row->bind($_POST)) {
            echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
            exit;
        }
        $row->post_date = JLMS_dateToDB($row->post_date);
        $row->end_date = JLMS_dateToDB($row->end_date);
        $hw_name = isset($_REQUEST['hw_name']) ? strval($_REQUEST['hw_name']) : 'homework';
        $hw_name = get_magic_quotes_gpc() ? stripslashes($hw_name) : $hw_name;
        $row->hw_name = ampReplace(strip_tags($hw_name));
        $days = intval(mosGetParam($_POST, 'days', ''));
        $hours = intval(mosGetParam($_POST, 'hours', ''));
        $mins = intval(mosGetParam($_POST, 'mins', ''));
        if ($row->is_time_related) {
            $row->show_period = JLMS_HTML::_('showperiod.getminsvalue', $days, $hours, $mins);
        }
        $row->hw_description = strval(JLMS_getParam_LowFilter($_POST, 'hw_description', ''));
        $row->hw_description = JLMS_ProcessText_LowFilter($row->hw_description);
        $row->hw_shortdescription = strval(JLMS_getParam_LowFilter($_POST, 'hw_shortdescription', ''));
        $row->hw_shortdescription = JLMS_ProcessText_HardFilter($row->hw_shortdescription);
        if ($assigned_groups_only) {
            $row->is_limited = 1;
            $groups_where_admin_manager = JLMS_ACL_HELPER::GetAssignedGroups($my->id, $course_id);
            for ($i = 0; $i < count($groups); $i++) {
                if (!in_array($groups[$i], $groups_where_admin_manager)) {
                    unset($groups[$i]);
                }
            }
            sort($groups);
        }
        $groups_in_db_arr = array();
        if ($row->id) {
            $query = "SELECT groups FROM #__lms_homework WHERE id = '" . $row->id . "' AND course_id = '" . $course_id . "'";
            $JLMS_DB->setQuery($query);
            $groups_in_db = $JLMS_DB->LoadResult();
            if ($groups_in_db) {
                $groups_in_db = substr($groups_in_db, 1, strlen($groups_in_db) - 2);
                $groups_in_db_arr = explode('|', $groups_in_db);
                if (isset($groups_where_admin_manager) && count($groups_where_admin_manager)) {
                    $groups_in_db_arr = array_diff($groups_in_db_arr, $groups_where_admin_manager);
                }
                $groups_in_db_arr = array_unique($groups_in_db_arr);
            }
        }
        $groups_str = '';
        if ($row->is_limited && (count($groups) && $groups[0] != 0 || count($groups_in_db_arr))) {
            //$groups = array_merge($groups, $groups_in_db_arr);
            $groups = array_unique($groups);
            $razd = '|';
            for ($i = 0; $i < count($groups); $i++) {
                $groups_str .= $razd . $groups[$i];
            }
            $groups_str .= '|';
        } else {
            $groups_str = '';
        }
        $row->groups = $groups_str;
        if (!$row->id) {
            $row->owner_id = $my->id;
        }
        if (!$row->check()) {
            echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
            exit;
        }
        if (!$row->store()) {
            echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
            exit;
        }
    }
    JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=homework&id={$course_id}"));
}