function JQ_editQuiz($id, $option, $page, $course_id)
 {
     global $JLMS_DB, $my, $JLMS_CONFIG;
     $AND_ST = "";
     if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
         $AND_ST = " AND IF(is_time_related, (show_period < '" . $enroll_period . "' ), 1) ";
     }
     $row = new mos_JoomQuiz_Quiz($JLMS_DB);
     $row->addCond($AND_ST);
     $row->load($id);
     if ($id) {
     } elseif (mosGetParam($_REQUEST, 'flag')) {
         $row->bind($_POST);
         $row->flag = mosGetParam($_REQUEST, 'c_pool_type_gqp');
     } else {
         $row->c_user_id = $my->id;
         $JLMS_DB->SetQuery("Select name from #__users WHERE id = '" . $my->id . "'");
         $row->c_author = $JLMS_DB->LoadResult();
         $row->c_slide = 1;
         $row->c_gradebook = 1;
         $row->c_max_numb_attempts = 0;
         $row->c_resume = 1;
     }
     if (!isset($row->flag)) {
         $row->flag = 0;
     }
     /*----------------------------------------------------*/
     $count_array = array();
     $new_val_array = array();
     if ($JLMS_CONFIG->get('global_quest_pool')) {
         if ($id) {
             $query = "SELECT count(*) FROM #__lms_quiz_t_quiz_gqp WHERE quiz_id = {$id} AND qcat_id <> 0";
             $JLMS_DB->setQuery($query);
             $pool_is_cat_mode_gqp = intval($JLMS_DB->loadResult());
             $query = "SELECT count(*) FROM #__lms_quiz_t_quiz_gqp WHERE quiz_id = {$id} AND qcat_id = 0";
             $JLMS_DB->setQuery($query);
             $pool_is_quest_mode_gqp = intval($JLMS_DB->loadResult());
             if ($pool_is_cat_mode_gqp) {
                 $query = "SELECT * FROM #__lms_quiz_t_quiz_gqp WHERE quiz_id = {$id} AND qcat_id <> 0 ORDER BY orderin";
                 $JLMS_DB->setQuery($query);
                 $db_cats = $JLMS_DB->loadObjectList();
                 $row->flag = 2;
                 for ($i = 0; $i < count($db_cats); $i++) {
                     $_REQUEST['cat_id_gqp'][] = $db_cats[$i]->qcat_id;
                     $_REQUEST['pool_cat_number_gqp'][] = $db_cats[$i]->items_number;
                 }
             }
             //			$lists['pool_quest_mode_gqp'] = $pool_is_quest_mode_gqp ? false : true;
             //			if (!$lists['pool_quest_mode_gqp']) {
             //				$lists['pool_quest_num_gqp'] = 0;
             //			}
         }
     }
     $levels = array();
     $javascript = 'onchange="document.adminForm.page.value=\'edit_quiz\';document.adminForm.submit();"';
     if ($row->flag == 2) {
         $cat_id = mosGetParam($_REQUEST, 'cat_id_gqp');
         $count_array = mosGetParam($_REQUEST, 'pool_cat_number_gqp');
         if (isset($cat_id) && count($cat_id)) {
             foreach ($cat_id as $k => $v) {
                 if ($cat_id[$k] == 0) {
                     unset($cat_id[$k]);
                 }
                 unset($_REQUEST['cat_id_gqp'][$k]);
             }
             if (!isset($cat_id[0])) {
                 unset($cat_id);
             }
             $lists = array();
             $level = 0;
             if (isset($cat_id) && count($cat_id)) {
                 foreach ($cat_id as $k => $v) {
                     $query = "SELECT parent" . "\n FROM #__lms_gqp_cats" . "\n WHERE id = '" . $cat_id[$k] . "'";
                     $JLMS_DB->setQuery($query);
                     $parent = intval($JLMS_DB->loadResult());
                     if (!$parent) {
                         $parent = 0;
                     }
                     /*-----------------category-----------------------*/
                     $query = "SELECT id AS value, c_category AS text" . "\n FROM #__lms_gqp_cats" . "\n WHERE parent = '" . $parent . "'" . "\n ORDER BY c_category";
                     $JLMS_DB->setQuery($query);
                     $categories[] = mosHTML::makeOption('0', 'Select Category');
                     $categories = array_merge($categories, $JLMS_DB->loadObjectList());
                     $level = JLMS_quiz_admin_class::check_level($cat_id[$k]);
                     $categories = JLMS_quiz_admin_class::count_questions_in_category($categories, $level);
                     $category = mosHTML::selectList($categories, 'cat_id_gqp[]', 'class="text_area" size="1" style="width:100%;" ' . $javascript, 'value', 'text', $cat_id[$k]);
                     //echo JLMS_quiz_admin_class::check_level($cat_id[$k]); die;
                     $levels[] = $level;
                     $lists['category'][] = $category;
                     unset($category);
                     unset($categories);
                     /*----------------subcategory---------------------*/
                     if (JLMS_quiz_admin_class::select_sub($k, $cat_id)) {
                         $this_level = JLMS_quiz_admin_class::check_level($cat_id[$k]);
                         $level1 = $this_level;
                         for ($j = $this_level; $j > -1; $j--) {
                             for ($n = $k; $n > -1; $n--) {
                                 if (isset($cat_id[$n]) && isset($level1) && JLMS_quiz_admin_class::check_level($cat_id[$n]) == $level1 && JLMS_quiz_admin_class::this_level_on_stek($cat_id, $n)) {
                                     $query = "SELECT id AS value, c_category AS text" . "\n FROM #__lms_gqp_cats" . "\n WHERE parent = '" . $cat_id[$n] . "'" . "\n ORDER BY c_category";
                                     $JLMS_DB->setQuery($query);
                                     if ($JLMS_DB->loadObjectList()) {
                                         $categories[] = mosHTML::makeOption('0', 'Select SubCategory');
                                         $categories = array_merge($categories, $JLMS_DB->loadObjectList());
                                         $categories = JLMS_quiz_admin_class::count_questions_in_category($categories, $this_level);
                                         $category = mosHTML::selectList($categories, 'cat_id_gqp[]', 'class="text_area" size="1" style="width:100%;" ' . $javascript, 'value', 'text', '');
                                         $levels[] = $level1 + 1;
                                         $lists['category'][] = $category;
                                         unset($category);
                                         unset($categories);
                                         $new_val_array[] = $cat_id[$k];
                                     }
                                     $level1--;
                                 }
                             }
                         }
                     }
                     /*------------------------------------------------*/
                 }
             }
         }
         $query = "SELECT id AS value, c_category AS text" . "\n FROM #__lms_gqp_cats" . "\n WHERE parent = 0" . "\n ORDER BY c_category";
         $JLMS_DB->setQuery($query);
         $categories[] = mosHTML::makeOption('0', 'Select Category');
         $categories = array_merge($categories, $JLMS_DB->loadObjectList());
         $categories = JLMS_quiz_admin_class::count_questions_in_category($categories, 0);
         $category = mosHTML::selectList($categories, 'cat_id_gqp[]', 'class="text_area" size="1" style="width:100%;"' . $javascript, 'value', 'text', '');
         $lists['new_category'] = $category;
         unset($category);
         unset($categories);
     }
     /*---------------------------------------------------*/
     if (is_array($row->params)) {
         $row->params = implode("\n", $row->params);
     }
     $params = new JLMSParameters($row->params);
     $params->def('disable_quest_feedback', 0);
     $params->def('sh_user_answer', 0);
     $params->def('sh_correct_answer', 1);
     $params->def('sh_explanation', 0);
     $params->def('sh_final_page', 1);
     $params->def('sh_self_verification', 0);
     $params->def('sh_final_page_text', 1);
     $params->def('sh_final_page_grafic', 0);
     $params->def('sh_final_page_fdbck', 1);
     $params->def('sh_skip_quest', 0);
     if (!$row->c_user_id) {
         $row->c_user_id = $my->id;
     }
     $query = "SELECT a.*, b.items_number FROM #__lms_quiz_t_category as a LEFT JOIN #__lms_quiz_t_quiz_pool as b" . "\n ON a.c_id = b.qcat_id AND b.quiz_id = {$id}" . "\n WHERE a.course_id = '" . $course_id . "' AND a.is_quiz_cat = 0 order by a.c_category";
     $JLMS_DB->setQuery($query);
     $pool_cats = $JLMS_DB->loadObjectList();
     $lists['jq_pool_categories'] = $pool_cats;
     if ($JLMS_CONFIG->get('global_quest_pool')) {
         $query = "SELECT a.*, b.items_number FROM #__lms_gqp_cats as a LEFT JOIN #__lms_quiz_t_quiz_gqp as b" . "\n ON a.id = b.qcat_id AND b.quiz_id = {$id}" . "\n order by a.c_category";
         $JLMS_DB->setQuery($query);
         $pool_cats = $JLMS_DB->loadObjectList();
         $lists['jq_pool_categories_gqp'] = $pool_cats;
     }
     $query = "SELECT items_number FROM #__lms_quiz_t_quiz_pool WHERE quiz_id = {$id} AND qcat_id = 0";
     $JLMS_DB->setQuery($query);
     $pool_quest_num = intval($JLMS_DB->loadResult());
     $lists['pool_quest_num'] = $pool_quest_num;
     if ($JLMS_CONFIG->get('global_quest_pool')) {
         $query = "SELECT items_number FROM #__lms_quiz_t_quiz_gqp WHERE quiz_id = {$id} AND qcat_id = 0";
         $JLMS_DB->setQuery($query);
         $pool_quest_num_gqp = intval($JLMS_DB->loadResult());
         $lists['pool_quest_num_gqp'] = $pool_quest_num_gqp;
     }
     $query = "SELECT count(*) FROM #__lms_quiz_t_quiz_pool WHERE quiz_id = {$id} AND qcat_id <> 0";
     $JLMS_DB->setQuery($query);
     $pool_is_quest_mode = intval($JLMS_DB->loadResult());
     $lists['pool_quest_mode'] = $pool_is_quest_mode ? false : true;
     if (!$lists['pool_quest_mode']) {
         $lists['pool_quest_num'] = 0;
     }
     $query = "SELECT * FROM #__lms_quiz_t_category WHERE course_id = '" . $course_id . "' AND is_quiz_cat = 1 order by c_category";
     $JLMS_DB->setQuery($query);
     $jq_cats = $JLMS_DB->loadObjectList();
     $lists['jq_categories'] = mosHTML::selectList($jq_cats, 'c_category_id', 'class="inputbox" size="1"', 'c_id', 'c_category', $row->c_category_id);
     $jq_temps = array();
     /*$query = "SELECT * FROM #__lms_quiz_templates order by id";
     	$JLMS_DB->setQuery( $query );
     	$jq_temps = $JLMS_DB->loadObjectList();
     	$lists['jq_templates'] = mosHTML::selectList( $jq_temps, 'c_skin', 'class="inputbox" size="1"', 'id', 'template_name', $row->c_skin );
     	$jq_langs = array();*/
     /*	$query = "SELECT * FROM #__lms_quiz_languages order by id";
     	$JLMS_DB->setQuery( $query );
     	$jq_langs = $JLMS_DB->loadObjectList();
     	$lists['jq_languages'] = mosHTML::selectList( $jq_langs, 'c_language', 'class="inputbox" size="1"', 'id', 'lang_file', $row->c_language );*/
     $jq_certs = array();
     $query = "SELECT id as value, crtf_text as text, crtf_name FROM #__lms_certificates WHERE course_id = '" . $course_id . "' AND crtf_type = 2 AND parent_id = 0 ORDER BY id";
     $JLMS_DB->setQuery($query);
     $jq_certs[] = mosHTML::makeOption(0, _JLMS_SB_NO_CERTIFICATE);
     $jq_certs2 = $JLMS_DB->loadObjectList();
     $i = 0;
     while ($i < count($jq_certs2)) {
         $jq_certs2[$i]->text = $jq_certs2[$i]->crtf_name ? $jq_certs2[$i]->crtf_name : (strlen($jq_certs2[$i]->text) > 50 ? substr($jq_certs2[$i]->text, 0, 50) . '...' : $jq_certs2[$i]->text);
         $i++;
     }
     $jq_certs = array_merge($jq_certs, $jq_certs2);
     $lists['jq_certificates'] = mosHTML::selectList($jq_certs, 'c_certificate', 'class="inputbox" size="1" ' . ($params->get('sh_self_verification') ? "disabled='disabled'" : '') . '', 'value', 'text', $params->get('sh_self_verification') == 1 ? 0 : $row->c_certificate);
     $user_emailto = array();
     $user_emailto[] = mosHTML::makeOption(0, _JLMS_DISABLE_OPTION);
     $user_emailto[] = mosHTML::makeOption(1, _JLMS_QUIZ_EMAIL_TO_AUTHOR);
     $user_emailto[] = mosHTML::makeOption(2, _JLMS_QUIZ_EMAIL_TO_LEARNER);
     $lists['user_email_to'] = mosHTML::selectList($user_emailto, 'c_email_to', 'class="inputbox" size="1"', 'value', 'text', $row->c_email_to);
     $quiz_explanation = array();
     $quiz_explanation[] = mosHTML::makeOption(0, _JLMS_DISABLE_OPTION);
     $quiz_explanation[] = mosHTML::makeOption(1, _JLMS_QUIZ_REVIEW_FOR_ALL);
     $quiz_explanation[] = mosHTML::makeOption(2, _JLMS_QUIZ_REVIEW_FOR_PASSED);
     $quiz_explanation[] = mosHTML::makeOption(3, _JLMS_QUIZ_REVIEW_FOR_FAILED);
     $lists['quiz_explanation'] = mosHTML::selectList($quiz_explanation, 'params[sh_explanation]', 'class="inputbox" size="1"', 'value', 'text', $params->get('sh_explanation'));
     $lists['published'] = mosHTML::yesnoradioList('published', '', $row->published);
     $lists['sh_final_page_text'] = mosHTML::yesnoradioList('params[sh_final_page_text]', 'class="inputbox"', $params->get('sh_final_page_text', 1));
     $lists['sh_final_page_grafic'] = mosHTML::yesnoradioList('params[sh_final_page_grafic]', 'class="inputbox"', $params->get('sh_final_page_grafic', 0));
     $lists['sh_final_page_fdbck'] = mosHTML::yesnoradioList('params[sh_final_page_fdbck]', 'class="inputbox"', $params->get('sh_final_page_fdbck', 1));
     $property = $params->get('sh_self_verification') == 1 ? "disabled" : "";
     $property_1 = $property . ' onclick="javascript: this.form[\'c_enable_review\'].value = this.value;"';
     $lists['c_enable_review_chk'] = mosHTML::yesnoradioList('c_enable_review_chk', $property_1, $row->c_enable_review);
     $lists['sh_correct_answer'] = mosHTML::yesnoradioList('params[sh_correct_answer]', $property, $params->get('sh_correct_answer', 1));
     $lists['sh_user_answer'] = mosHTML::yesnoradioList('params[sh_user_answer]', $property, $params->get('sh_user_answer', 0));
     $lists['c_resume'] = mosHTML::yesnoradioList('c_resume', '', $row->c_resume);
     if (!mosGetParam($_REQUEST, 'c_title') && !isset($row->c_title)) {
         if (!isset($cat_id)) {
             $cat_id = array();
         }
         $count_array = JLMS_quiz_admin_class::insert_null($new_val_array, $count_array, $cat_id);
     }
     JLMS_quiz_admin_html_class::JQ_editQuiz($row, $lists, $option, $page, $course_id, $params, $levels, $count_array);
 }
function JLMS_showLinks($option)
{
    $JLMS_CONFIG =& JLMSFactory::getCOnfig();
    $Itemid = $JLMS_CONFIG->get('Itemid');
    $db =& JFactory::getDbo();
    $user = JLMSFactory::getUser();
    $my_id = $user->get('id');
    $JLMS_ACL =& JLMSFactory::getACL();
    $id = $JLMS_CONFIG->get('course_id');
    $AND_ST = "";
    if (false !== ($enroll_period = JLMS_getEnrolPeriod($my_id, $id))) {
        $AND_ST = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
    }
    if ($id && $JLMS_ACL->CheckPermissions('links', 'view')) {
        $query = "SELECT a.*, b.name as author_name" . "\n FROM #__lms_links as a LEFT JOIN #__users as b ON a.owner_id = b.id" . "\n WHERE a.course_id = '" . $id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('links', 'view_all') ? '' : "\n AND a.published = 1") . "\n ORDER BY a.ordering, a.link_name";
        $db->SetQuery($query);
        $rows = $db->LoadObjectList();
        $lms_titles_cache =& JLMSFactory::getTitles();
        $lms_titles_cache->setArray('links', $rows, 'id', 'link_name');
        JLMS_course_links_html::showCourseLinks($id, $option, $rows);
    } elseif ($id) {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=details_course&id={$id}"));
    } else {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}"));
    }
}
function editTopic($course_id, $topic_id = 0)
{
    global $JLMS_DB, $Itemid, $JLMS_CONFIG, $my;
    $usertype = $JLMS_CONFIG->get('current_usertype', 0);
    if ($course_id == $JLMS_CONFIG->get('course_id') && $usertype == 1) {
        $AND_ST = "";
        if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
            $AND_ST = " AND IF(is_time_related, (show_period < '" . $enroll_period . "' ), 1) ";
        }
        $row = new JLMS_Topic($JLMS_DB);
        $row->addCond($AND_ST);
        if ($topic_id) {
            $row->load($topic_id);
        }
        $lists['publishing'] = mosHTML::yesnoRadioList('published', 'class="inputbox" ', $topic_id != 0 ? $row->published : 0);
        $query = "SELECT name, ordering FROM #__lms_topics WHERE course_id={$course_id} ORDER BY ordering";
        $JLMS_DB->setQuery($query);
        $topics = $JLMS_DB->loadObjectList();
        array_unshift($topics, mosHTML::makeOption(0, _JLMS_SB_FIRST_ITEM, 'ordering', 'name'));
        array_push($topics, mosHTML::makeOption(9999, _JLMS_SB_LAST_ITEM, 'ordering', 'name'));
        $lists['ordering'] = mosHTML::selectList($topics, 'ordering', '', 'ordering', 'name', $row->ordering);
        unset($tmp);
        $lists['names'] = '';
        for ($i = 1; $i <= 10; $i++) {
            $tmp[] = mosHTML::makeOption($i, $i);
            $lists['names'] .= '<div id="name_' . $i . '" style="display:none;"><input class="inputbox" size="40" type="text" name="name_' . $i . '" /></div>' . "\n";
        }
        $javascript = 'onclick="jlms_Change_weekly();"';
        $lists['number'] = mosHTML::selectList($tmp, 'number', $javascript, 'value', 'text', 1);
        JLMS_topic_html::editTopic($course_id, $topic_id, $row, $lists);
    } else {
        $msg = '';
        //_JLMS_TOPIC_HACK;
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=details_course&id={$course_id}#topic_{$topic_id}"), $msg);
    }
}
 function JLMS_Course_HomePage($course_id, $add_elements_page = false)
 {
     global $option, $my, $JLMS_DB, $Itemid, $JLMS_CONFIG, $JLMS_SESSION;
     //TODO: replace global option declaration
     $usertype = $JLMS_CONFIG->get('current_usertype', 0);
     $JLMS_ACL =& JLMSFactory::getACL();
     $is_teacher = $JLMS_ACL->isTeacher();
     $AND_ST = "";
     if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
         $AND_ST = " AND IF(is_time_related, (show_period < '" . $enroll_period . "' ), 1) ";
     }
     $this->course_id = $course_id;
     $curr_date = date("Y-m-d");
     $is_curtopic = intval(mosgetparam($_REQUEST, 't_id', 0));
     //create list of topics
     if ($usertype == 1) {
         $published = '1';
     } else {
         $published = "published=1 AND (publish_start=0 OR start_date<='{$curr_date}') AND (publish_end=0 OR end_date>='{$curr_date}')";
     }
     $query = "SELECT * FROM #__lms_topics WHERE course_id={$course_id} " . $AND_ST . " AND {$published} " . ($is_curtopic ? " AND id=" . $is_curtopic : "") . " ORDER BY ordering";
     $JLMS_DB->setQuery($query);
     $this->topics = $JLMS_DB->loadObjectList('id');
     //get items of the course
     //2 - documents
     if ($add_elements_page) {
         $rows = array();
         $possibilities = new stdClass();
         JLMSDocs::FillList($course_id, $rows, $possibilities, false, false);
         $this->elements[_DOCUMENT_ID] = JLMSDocs::GetItemsbyPermission($rows, 'manage');
     } else {
         $AND_ST_D = "";
         if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
             $AND_ST_D = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
         }
         $query = "SELECT a.*, b.file_name FROM #__lms_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 WHERE a.course_id={$course_id}" . $AND_ST_D;
         $JLMS_DB->setQuery($query);
         $this->elements[_DOCUMENT_ID] = $JLMS_DB->loadObjectList('id');
         foreach ($this->elements[_DOCUMENT_ID] as $elem) {
             if ($elem->folder_flag == 3) {
                 $query = "SELECT a.*, b.file_name" . "\n FROM #__lms_outer_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 " . "\n WHERE a.folder_flag = 0 AND a.id = " . $elem->file_id;
                 $JLMS_DB->SetQuery($query);
                 $out_row = $JLMS_DB->LoadObjectList();
                 if (count($out_row) && isset($out_row[0]->allow_link) && $out_row[0]->allow_link == 1) {
                     $this->elements[_DOCUMENT_ID][$elem->id]->doc_name = $out_row[0]->doc_name;
                     $this->elements[_DOCUMENT_ID][$elem->id]->doc_description = $out_row[0]->doc_description;
                     $this->elements[_DOCUMENT_ID][$elem->id]->file_id = $out_row[0]->file_id;
                     $this->elements[_DOCUMENT_ID][$elem->id]->file_name = $out_row[0]->file_name;
                 } else {
                     unset($this->elements[_DOCUMENT_ID][$elem->id]);
                 }
             }
         }
         $this->elements[_DOCUMENT_ID] = AppendFileIcons_toList($this->elements[_DOCUMENT_ID]);
     }
     //get max tree level
     $this->max_lvl = getDirNesting($this->elements[_DOCUMENT_ID], 0);
     global $max_lvl;
     $max_lvl = $this->max_lvl;
     //3 - links
     $query = "SELECT * FROM #__lms_links WHERE course_id={$course_id}" . $AND_ST;
     $JLMS_DB->setQuery($query);
     $this->elements[_LINK_ID] = $JLMS_DB->loadObjectList('id');
     //4 - contents ... er... TODO
     //5 - quizs
     $query = "SELECT *, c_id AS id FROM #__lms_quiz_t_quiz WHERE course_id={$course_id}" . $AND_ST;
     $JLMS_DB->setQuery($query);
     $this->elements[_QUIZ_ID] = $JLMS_DB->loadObjectList('c_id');
     $quizzes_i = array();
     foreach ($this->elements[_QUIZ_ID] as $row) {
         $quizzes_i[] = $row->c_id;
     }
     //add results for student
     if ($usertype == 2 && count($this->elements[_QUIZ_ID])) {
         $quizzes_r = array();
         foreach ($this->elements[_QUIZ_ID] as $row) {
             $quizzes_r[] = $row->c_id;
         }
         $q_items_num = array();
         $extra_info_cells = array();
         if (!empty($quizzes_r)) {
             $quizzes_r_cid = implode(',', $quizzes_r);
             $query = "SELECT * FROM #__lms_quiz_results WHERE quiz_id IN ({$quizzes_r_cid}) AND course_id={$course_id} AND user_id={$my->id} GROUP BY quiz_id";
             $JLMS_DB->SetQuery($query);
             $extra_info_cells = $JLMS_DB->loadObjectList();
         }
         foreach ($this->elements[_QUIZ_ID] as $i => $value) {
             $this->elements[_QUIZ_ID][$i]->start_date = '-';
             $this->elements[_QUIZ_ID][$i]->end_date = '-';
             $this->elements[_QUIZ_ID][$i]->status = -1;
             $this->elements[_QUIZ_ID][$i]->user_passed = -1;
             $this->elements[_QUIZ_ID][$i]->points = -1;
             $this->elements[_QUIZ_ID][$i]->user_score = 0;
             $this->elements[_QUIZ_ID][$i]->quiz_max_score = 0;
             foreach ($extra_info_cells as $cell) {
                 if ($cell->quiz_id == $this->elements[_QUIZ_ID][$i]->c_id) {
                     $this->elements[_QUIZ_ID][$i]->start_date = JLMS_dateToDisplay($cell->quiz_date, false, $JLMS_CONFIG->get('offset') * 60 * 60 - $cell->user_time, '\\<\\b\\r \\/>H:i:s');
                     $this->elements[_QUIZ_ID][$i]->end_date = JLMS_dateToDisplay($cell->quiz_date, false, $JLMS_CONFIG->get('offset') * 60 * 60, '\\<\\b\\r \\/>H:i:s');
                     $this->elements[_QUIZ_ID][$i]->points = $cell->user_score;
                     $this->elements[_QUIZ_ID][$i]->status = $cell->user_passed;
                     $this->elements[_QUIZ_ID][$i]->user_passed = $cell->user_passed;
                     $this->elements[_QUIZ_ID][$i]->user_score = $cell->user_score;
                     $this->elements[_QUIZ_ID][$i]->quiz_max_score = $cell->quiz_max_score;
                 }
             }
         }
         unset($extra_info_cells);
         require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "lms_certificates.php";
         $arr = array();
         JLMS_Certificates::JLMS_GB_getUserCertificates($course_id, $my->id, $arr);
         $arr1 = isset($arr['user_quiz_certificates']) ? $arr['user_quiz_certificates'] : array();
         foreach ($this->elements[_QUIZ_ID] as $i => $value) {
             for ($j = 0; $j < count($arr1); $j++) {
                 if ($arr1[$j]->c_quiz_id == $this->elements[_QUIZ_ID][$i]->c_id) {
                     $this->elements[_QUIZ_ID][$i]->link_certificate = "<a target = \"_blank\" href = \"" . $JLMS_CONFIG->get('live_site') . "/index.php?tmpl=component&option=" . $option . "&amp;no_html=1&amp;task=print_quiz_cert&amp;course_id=" . $course_id . "&amp;stu_quiz_id=" . $arr1[$j]->stu_quiz_id . "&amp;user_unique_id=" . $arr1[$j]->user_unique_id . "\"><img src = \"" . $JLMS_CONFIG->get('live_site') . "/components/com_joomla_lms/lms_images/toolbar/btn_certificate.png\" border = \"0\" align=\"top\" alt=\"certificate\"/></a>";
                 }
             }
         }
     }
     //6 - scorm ... er... TODO
     //7 - learning paths
     $lpaths = array();
     if ($course_id && $usertype) {
         //$JLMS_SESSION->clear('redirect_to_learnpath');
         if ($usertype == 1) {
             $query = "SELECT * FROM #__lms_learn_paths WHERE course_id = '" . $course_id . "'" . $AND_ST . "\n ORDER BY ordering";
         } elseif ($usertype == 2) {
             //$query = "SELECT a.*, b.user_status as r_status, b.start_time as r_start, b.end_time as r_end"
             //. "\n FROM #__lms_learn_paths as a LEFT JOIN #__lms_learn_path_results as b ON a.id = b.lpath_id AND b.course_id = '".$course_id."' AND b.user_id = '".$my->id."'"
             $query = "SELECT a.*, '' as r_status, '' as r_start, '' as r_end" . "\n FROM #__lms_learn_paths as a" . "\n WHERE a.course_id = '" . $course_id . "'" . "\n AND a.published = 1" . "\n ORDER BY a.ordering";
         }
         $JLMS_DB->SetQuery($query);
         $lpaths = $JLMS_DB->LoadObjectList();
         if ($usertype == 2) {
             $user_ids = array();
             $user_ids[] = $my->id;
             require_once _JOOMLMS_FRONT_HOME . "/includes/lms_grades.lib.php";
             JLMS_LP_populate_results($course_id, $lpaths, $user_ids);
             // 13 August 2007 (DEN) Check for prerequisites.
             // 1. get the list of lpath_ids.
             $lpath_ids = array();
             foreach ($lpaths as $lpath) {
                 $lpath_ids[] = $lpath->id;
             }
             if (!empty($lpath_ids)) {
                 $lpath_ids_str = implode(',', $lpath_ids);
                 // 2. get the list of prerequisites
                 // SELECT from two tables (+ #__lms_learn_paths) - because the prereq lpath could be deleted...
                 $query = "SELECT a.* FROM #__lms_learn_path_prerequisites as a, #__lms_learn_paths as b" . "\n WHERE a.lpath_id IN ({$lpath_ids_str}) AND a.req_id = b.id";
                 $JLMS_DB->SetQuery($query);
                 $prereqs = $JLMS_DB->LoadObjectList();
                 if (!empty($prereqs)) {
                     // 3. compare lists of prereqs to the lists of lpaths.
                     $i = 0;
                     while ($i < count($lpaths)) {
                         $is_hidden = false;
                         $o = 0;
                         while ($o < count($prereqs)) {
                             if ($prereqs[$o]->lpath_id == $lpaths[$i]->id) {
                                 $j = 0;
                                 while ($j < count($lpaths)) {
                                     if ($lpaths[$j]->id == $prereqs[$o]->req_id) {
                                         if (!$lpaths[$j]->item_id) {
                                             if (empty($lpaths[$j]->r_status)) {
                                                 $is_hidden = true;
                                                 break;
                                             } else {
                                                 $end_time = strtotime($lpaths[$j]->r_end);
                                                 $current_time = strtotime(date("Y-m-d H:i:s"));
                                                 if ($current_time > $end_time && $current_time - $end_time < $prereqs[$o]->time_minutes * 60) {
                                                     $is_hidden = true;
                                                     break;
                                                 }
                                             }
                                         } else {
                                             if (empty($lpaths[$j]->s_status)) {
                                                 $is_hidden = true;
                                                 break;
                                             } else {
                                                 $end_time = strtotime($lpaths[$j]->r_end);
                                                 $current_time = strtotime(date("Y-m-d H:i:s"));
                                                 if ($current_time > $end_time && $current_time - $end_time < $prereqs[$o]->time_minutes * 60) {
                                                     $is_hidden = true;
                                                     break;
                                                 }
                                             }
                                         }
                                     }
                                     $j++;
                                 }
                             }
                             $o++;
                         }
                         $lpaths[$i]->is_hidden = $is_hidden;
                         $i++;
                     }
                 }
             }
         }
     }
     foreach ($lpaths as $lpath) {
         $this->elements[_LPATH_ID][$lpath->id] = $lpath;
     }
     //populate topics with links to their items
     $query = "SELECT * FROM #__lms_topic_items WHERE course_id={$course_id} ORDER BY ordering";
     $JLMS_DB->setQuery($query);
     $items = $JLMS_DB->loadObjectList();
     for ($i = 0; $i < count($items); $i++) {
         if ($items[$i]->item_type == 7) {
             $query = "SELECT lp_type FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'" . $AND_ST;
             $JLMS_DB->SetQuery($query);
             $lp_type = $JLMS_DB->LoadResult();
             if ($lp_type == 2) {
                 $query = "SELECT item_id FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $learn_path_id = $JLMS_DB->LoadResult();
                 $query = "SELECT scorm_package, params as scorm_params, width as scorm_width, height as scorm_height FROM #__lms_n_scorm WHERE id = '" . $learn_path_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $outer_doc = null;
                 $scorm_info = null;
                 $scorm_info = $JLMS_DB->LoadObject();
                 if (is_object($scorm_info)) {
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_params = $scorm_info->scorm_params;
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_width = $scorm_info->scorm_width;
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_height = $scorm_info->scorm_height;
                     $scorm_package = $scorm_info->scorm_package;
                     $query = "SELECT id FROM #__lms_n_scorm WHERE scorm_package = '" . $scorm_package . "' AND course_id = 0";
                     $JLMS_DB->SetQuery($query);
                     $scorm_lib_id = $JLMS_DB->LoadResult();
                     $query = "SELECT outdoc_share, owner_id, allow_link FROM #__lms_outer_documents WHERE file_id = '" . $scorm_lib_id . "' AND folder_flag = 3";
                     $JLMS_DB->SetQuery($query);
                     $outer_doc = $JLMS_DB->LoadObject();
                 }
                 if (is_object($outer_doc) && isset($outer_doc->allow_link) && $outer_doc->allow_link == 1) {
                     // 01May2009: new library policy: if 'allow_link' is enabled - we can view already added resource !
                 } else {
                     unset($items[$i]);
                 }
             } elseif ($lp_type == 1) {
                 $query = "SELECT item_id FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $learn_path_id = $JLMS_DB->LoadResult();
                 if ($learn_path_id) {
                     $query = "SELECT params as scorm_params, width as scorm_width, height as scorm_height FROM #__lms_n_scorm WHERE id = '" . $learn_path_id . "'";
                     $JLMS_DB->SetQuery($query);
                     $scorm_info = null;
                     $scorm_info = $JLMS_DB->LoadObject();
                     if (is_object($scorm_info)) {
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_params = $scorm_info->scorm_params;
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_width = $scorm_info->scorm_width;
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_height = $scorm_info->scorm_height;
                     }
                 }
             }
         }
     }
     if (!$is_teacher) {
         $this->_filterItemsByShowPeriod($items, $course_id);
     }
     $mas = array();
     foreach ($items as $k => $v) {
         $mas[] = $items[$k];
     }
     unset($lpath);
     $items = $mas;
     $links = array();
     foreach ($items as $item) {
         $item_tmp->id = $item->item_id;
         $item_tmp->type = $item->item_type;
         $item_tmp->ordering = $item->ordering;
         $links[$item->topic_id][] = $item;
     }
     $this->links = $links;
 }
function JLMS_edit_event($option, $id)
{
    global $JLMS_DB, $Itemid, $my, $JLMS_CONFIG;
    $id = $JLMS_CONFIG->get('course_id');
    $agenda_id = intval(mosGetParam($_REQUEST, 'agenda_id', 0));
    $JLMS_ACL =& JLMSFactory::getACL();
    $assigned_groups_only = $JLMS_ACL->CheckPermissions('advanced', 'assigned_groups_only');
    $AND_ST = "";
    if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $id))) {
        $AND_ST = " AND IF(is_time_related, (show_period < '" . $enroll_period . "' ), 1) ";
    }
    $do_redirect = false;
    $query = "SELECT is_limited, owner_id FROM `#__lms_agenda` WHERE agenda_id = {$agenda_id} AND course_id = {$id}" . $AND_ST;
    $JLMS_DB->setQuery($query);
    $row = $JLMS_DB->loadObject();
    if (is_object($row) && isset($row->is_limited)) {
        $flag = 0;
        if ($row->is_limited == 0 && $row->owner_id != $my->id) {
            $flag = 1;
        }
    } else {
        $do_redirect = true;
    }
    if ($id && $JLMS_ACL->CheckPermissions('announce', 'manage') && !$flag) {
        $where = '';
        if ($assigned_groups_only && $row->is_limited) {
            $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)) {
                $where .= "\n AND is_limited = 1 AND ( `groups` LIKE '%|{$groups_where_admin_manager['0']}|%'";
                for ($i = 1; $i < count($groups_where_admin_manager); $i++) {
                    $where .= "\n OR `groups` like '%|{$groups_where_admin_manager[$i]}|%'";
                }
                $where .= "\n OR owner_id = '" . $my->id . "')";
            } else {
                $where .= "\n AND (owner_id = '" . $my->id . "' OR agenda_id = 0) AND groups = ''";
            }
        }
        $query = "SELECT * FROM `#__lms_agenda` WHERE agenda_id = {$agenda_id} AND course_id = {$id}" . $where;
        $JLMS_DB->setQuery($query);
        $agenda_item = $JLMS_DB->loadObject();
        if (is_object($agenda_item) && isset($agenda_item->agenda_id)) {
            if ($JLMS_ACL->CheckPermissions('announce', 'only_own') && $agenda_item->owner_id != $my->id) {
                $do_redirect = true;
            } elseif ($JLMS_ACL->CheckPermissions('announce', 'only_own_role') && $JLMS_ACL->GetRole() != $JLMS_ACL->UserSystemRole($JLMS_DB, $agenda_item->owner_id)) {
                $do_redirect = true;
            } else {
                $groups_arr = array();
                if ($agenda_item->groups) {
                    $groups = substr($agenda_item->groups, 1, strlen($agenda_item->groups) - 2);
                    $groups_arr = explode('|', $groups);
                }
                if ($assigned_groups_only) {
                    $lists['is_limited'] = mosHTML::yesnoRadioList('is_limited', 'disabled="disabled" class="inputbox" ', $agenda_item->is_limited);
                } else {
                    $lists['is_limited'] = mosHTML::yesnoRadioList('is_limited', 'class="inputbox" ', $agenda_item->is_limited);
                }
                $lists['groups'] = JLMSmultiselect($groups_arr, true, $id);
                JLMS_agenda_html::show_add_event($id, $option, $agenda_item, $lists);
            }
        } else {
            $do_redirect = true;
        }
    } else {
        $do_redirect = true;
    }
    if ($do_redirect) {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=agenda&id={$id}"));
    }
}
 function FillList($course_id, &$docs_rows, &$docs_possibilities, $folders = false, $show_unavailable = true)
 {
     global $JLMS_DB, $my;
     $JLMS_ACL =& JLMSFactory::getACL();
     $AND_ST = "";
     if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
         $AND_ST = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
     }
     $query = "SELECT a.*, b.file_name, c.name, c.username, c.name as author_name, dp.*, dp.doc_id as doc_perms_db, dp.doc_id as doc_perms, dp.p_manage as p_create, dp.p_publish as p_publish_childs" . "\n FROM #__lms_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 LEFT JOIN #__users as c ON a.owner_id = c.id" . "\n LEFT JOIN #__lms_documents_perms as dp ON  a.id = dp.doc_id AND dp.role_id = " . $JLMS_ACL->GetRole() . "\n WHERE a.course_id = '" . $course_id . "'" . $AND_ST . ($folders ? "\n AND a.folder_flag = 1" : "") . "\n ORDER BY a.parent_id, a.ordering, a.doc_name, c.username";
     $JLMS_DB->SetQuery($query);
     $rows = $JLMS_DB->LoadObjectList();
     if (!$folders) {
         // not only folders are present in 'rows' - we need to filter/remove unnecessary items
         $bad_in = array();
         $rows_n = array();
         for ($j = 0; $j < count($rows); $j++) {
             // TODO: redevelop this part of the code, DO NOT USE database queries within the for/while/foreach loops
             if ($rows[$j]->folder_flag == 3) {
                 // this is a file-library link
                 $query = "SELECT a.*, b.file_name" . "\n FROM #__lms_outer_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 " . "\n WHERE a.folder_flag = 0 AND a.id = " . $rows[$j]->file_id;
                 $JLMS_DB->SetQuery($query);
                 $out_row = $JLMS_DB->LoadObjectList();
                 if (count($out_row) && $out_row[0]->allow_link == 1) {
                     // resource is found in the Library
                     $rows[$j]->doc_name = $out_row[0]->doc_name;
                     $rows[$j]->doc_description = $out_row[0]->doc_description;
                     $rows[$j]->file_id = $out_row[0]->file_id;
                     $rows[$j]->file_name = $out_row[0]->file_name;
                 } else {
                     // there is no link in the Library (e.g. file-library file was removed or its permissions were changed)
                     if ($JLMS_ACL->CheckPermissions('docs', 'manage') && $show_unavailable) {
                         // show 'Resource is not available' message instead of a file
                         $rows[$j]->doc_name = _JLMS_LP_RESOURSE_ISUNAV;
                         $rows[$j]->is_link = 1;
                         $rows[$j]->author_name = '';
                     } else {
                         // remove item from the array
                         $g = 0;
                         $rows_n = array();
                         // What the f**k?....
                         for ($z = 0; $z < count($rows); $z++) {
                             if ($z != $j && !in_array($z, $bad_in)) {
                                 $g++;
                             } else {
                                 // populate array of the items which need to be removed from the list of files
                                 $bad_in[] = $j;
                             }
                         }
                     }
                 }
             }
         }
         if (count($bad_in)) {
             // remove 'not available' items from the list of files
             $rows_n = array();
             $g = 0;
             for ($z = 0; $z < count($rows); $z++) {
                 if (in_array($z, $bad_in)) {
                 } else {
                     $rows_n[$g] = $rows[$z];
                     $g++;
                 }
             }
             $rows = $rows_n;
         }
     }
     $rows = JLMS_GetTreeStructure($rows);
     $rows = AppendFileIcons_toList($rows);
     $possibilities = new stdClass();
     if (true) {
         //proceed in any case, even if we have 'ignore_permissions'
         // check notes about custom permissions at the top of the 'documents' source file (after $task section)
         $bad_in = array();
         $permissions = array();
         $permissions[0] = new stdClass();
         $permissions[0]->active = 1;
         // not used any more ????
         $permissions[0]->p_view = $possibilities->view = $JLMS_ACL->CheckPermissions('docs', 'view') ? 1 : 0;
         $permissions[0]->p_viewall = $possibilities->viewall = $JLMS_ACL->CheckPermissions('docs', 'view_all') ? 1 : 0;
         $permissions[0]->p_order = $possibilities->order = $JLMS_ACL->CheckPermissions('docs', 'order') ? 1 : 0;
         $permissions[0]->p_publish = $possibilities->publish = $JLMS_ACL->CheckPermissions('docs', 'publish') ? 1 : 0;
         $permissions[0]->p_publish_childs = $possibilities->publish_childs = $JLMS_ACL->CheckPermissions('docs', 'publish') ? 1 : 0;
         $permissions[0]->p_manage = $possibilities->manage = $JLMS_ACL->CheckPermissions('docs', 'manage') ? 1 : 0;
         $permissions[0]->p_create = $possibilities->create = $JLMS_ACL->CheckPermissions('docs', 'manage') ? 1 : 0;
         $folder_id = 0;
         $j = 0;
         $n = count($rows);
         while ($j < $n) {
             if ($rows[$j]->folder_flag == 1 && isset($rows[$j]->doc_perms_db) && $rows[$j]->doc_perms_db) {
                 // folder with configured custom permissions
                 $old_folder_id = $rows[$j]->parent_id;
                 //$folder_id;
                 // process permissions (add them into array from which they will be associated with child items)
                 $folder_id = $rows[$j]->id;
                 $parent_id = $rows[$j]->parent_id;
                 if ($JLMS_ACL->CheckPermissions('docs', 'ignore_permissions')) {
                     $permissions[$folder_id] = new stdClass();
                     $permissions[$folder_id] = $permissions[0];
                     $rows[$j]->p_view = $permissions[0]->p_view;
                     $rows[$j]->p_viewall = $permissions[0]->p_viewall;
                     $rows[$j]->p_order = $permissions[0]->p_order;
                     $rows[$j]->p_publish = $permissions[0]->p_publish;
                     $rows[$j]->p_manage = $permissions[0]->p_manage;
                     $rows[$j]->p_create = $permissions[0]->p_create;
                     $rows[$j]->p_publish_childs = $permissions[0]->p_publish_childs;
                 } else {
                     $permissions[$folder_id] = new stdClass();
                     $permissions[$folder_id]->active = 1;
                     // not used any more ????
                     $permissions[$folder_id]->p_view = $rows[$j]->p_view;
                     $permissions[$folder_id]->p_viewall = $permissions[$folder_id]->p_view ? $rows[$j]->p_viewall : 0;
                     $permissions[$folder_id]->p_order = $permissions[$folder_id]->p_view ? $rows[$j]->p_order : 0;
                     $permissions[$folder_id]->p_publish = $permissions[$folder_id]->p_view ? $rows[$j]->p_publish : 0;
                     $permissions[$folder_id]->p_publish_childs = $permissions[$folder_id]->p_view ? $rows[$j]->p_publish_childs : 0;
                     $permissions[$folder_id]->p_manage = $permissions[$folder_id]->p_view ? $rows[$j]->p_manage : 0;
                     // "no view - no manage" // 'no martiny - no party ;)'
                     $permissions[$folder_id]->p_create = $permissions[$folder_id]->p_view ? $rows[$j]->p_create : 0;
                     // set parent's permissions for this folder
                     $rows[$j]->doc_perms = 1;
                     $rows[$j]->p_view = $permissions[$old_folder_id]->p_view ? $rows[$j]->p_view : 0;
                     $rows[$j]->p_viewall = $permissions[$old_folder_id]->p_viewall ? $rows[$j]->p_viewall : 0;
                     $rows[$j]->p_order = $permissions[$old_folder_id]->p_order ? $rows[$j]->p_order : 0;
                     $rows[$j]->p_publish = $permissions[$old_folder_id]->p_publish ? $rows[$j]->p_publish : 0;
                     $rows[$j]->p_manage = $permissions[$old_folder_id]->p_manage ? $rows[$j]->p_manage : 0;
                     $rows[$j]->p_create = $old_folder_id ? $permissions[$old_folder_id]->p_create : $rows[$j]->p_create;
                     $rows[$j]->p_publish_childs = $old_folder_id ? $permissions[$old_folder_id]->p_publish_childs : $rows[$j]->p_publish_childs;
                 }
                 // change 'view' permission regarding to the 'published' status of the item
                 if (!$rows[$j]->p_viewall) {
                     if ($rows[$j]->published && $permissions[$old_folder_id]->p_view && ($rows[$j]->publish_start && strtotime($rows[$j]->start_date) <= strtotime(date('Y-m-d')) || !$rows[$j]->publish_start) && ($rows[$j]->publish_end && strtotime($rows[$j]->end_date) >= strtotime(date('Y-m-d')) || !$rows[$j]->publish_end)) {
                         // user can view this item
                     } elseif ($rows[$j]->owner_id == $my->id && $rows[$j]->p_manage) {
                         // this item is unpublished, but user is its owner - he can view it if he has 'manage' rights
                     } else {
                         $rows[$j]->p_view = 0;
                         $rows[$j]->p_viewall = 0;
                         $rows[$j]->p_order = 0;
                         $rows[$j]->p_publish = 0;
                         $rows[$j]->p_manage = 0;
                         $rows[$j]->p_create = 0;
                         $rows[$j]->p_publish_childs = 0;
                         $permissions[$folder_id]->p_view = 0;
                         $permissions[$folder_id]->p_viewall = 0;
                         $permissions[$folder_id]->p_order = 0;
                         $permissions[$folder_id]->p_publish = 0;
                         $permissions[$folder_id]->p_manage = 0;
                         $permissions[$folder_id]->p_create = 0;
                         $permissions[$folder_id]->p_publish_childs = 0;
                     }
                 }
                 $possibilities->create = $possibilities->create ? $possibilities->create : $rows[$j]->p_create;
                 // we can create docs at least in this folder
                 $possibilities->publish_childs = $possibilities->publish_childs ? $possibilities->publish_childs : $rows[$j]->p_publish_childs;
             } else {
                 // any other item: file,  file-library link or folder without custom permissions
                 $folder_id = $rows[$j]->parent_id;
                 if (isset($permissions[$folder_id]) && isset($permissions[$folder_id]->active) && $permissions[$folder_id]->active) {
                     // set parent's permissions for this item
                     $rows[$j]->doc_perms = 1;
                     $rows[$j]->p_view = $permissions[$folder_id]->p_view;
                     $rows[$j]->p_viewall = $permissions[$folder_id]->p_viewall;
                     $rows[$j]->p_order = $permissions[$folder_id]->p_order;
                     $rows[$j]->p_publish = $permissions[$folder_id]->p_publish;
                     $rows[$j]->p_manage = $permissions[$folder_id]->p_manage;
                     $rows[$j]->p_create = 0;
                     //$permissions[$folder_id]->p_create;
                     $rows[$j]->p_publish_childs = 0;
                     //$permissions[$folder_id]->p_publish_childs;
                     // change 'view' permission regarding to the 'published' status of the item
                     if (!$rows[$j]->p_viewall) {
                         if ($rows[$j]->published && $permissions[$folder_id]->p_view && ($rows[$j]->publish_start && strtotime($rows[$j]->start_date) <= strtotime(date('Y-m-d')) || !$rows[$j]->publish_start) && ($rows[$j]->publish_end && strtotime($rows[$j]->end_date) >= strtotime(date('Y-m-d')) || !$rows[$j]->publish_end)) {
                             // user can view this item
                         } elseif ($rows[$j]->owner_id == $my->id && $rows[$j]->p_manage) {
                             // this item is unpublished, but user is its owner - he can view it if he has 'manage' rights
                         } else {
                             $rows[$j]->p_view = 0;
                             $rows[$j]->p_viewall = 0;
                             $rows[$j]->p_order = 0;
                             $rows[$j]->p_publish = 0;
                             $rows[$j]->p_manage = 0;
                             $rows[$j]->p_create = 0;
                             $rows[$j]->p_publish_childs = 0;
                             if ($rows[$j]->folder_flag == 1) {
                                 // just a folder (without custom permissions)
                                 $folder_id_current = $rows[$j]->id;
                                 $permissions[$folder_id_current]->p_view = 0;
                                 $permissions[$folder_id_current]->p_viewall = 0;
                                 $permissions[$folder_id_current]->p_order = 0;
                                 $permissions[$folder_id_current]->p_publish = 0;
                                 $permissions[$folder_id_current]->p_manage = 0;
                                 $permissions[$folder_id_current]->p_create = 0;
                                 $permissions[$folder_id_current]->p_publish_childs = 0;
                             }
                         }
                     }
                     $possibilities->view = $possibilities->view ? $possibilities->view : $rows[$j]->p_view;
                     $possibilities->viewall = $possibilities->view ? $possibilities->viewall : $rows[$j]->p_viewall;
                     $possibilities->order = $possibilities->order ? $possibilities->order : $rows[$j]->p_order;
                     $possibilities->publish = $possibilities->publish ? $possibilities->publish : $rows[$j]->p_publish;
                     $possibilities->manage = $possibilities->manage ? $possibilities->manage : $rows[$j]->p_manage;
                     //$possibilities->create = $possibilities->create ? $possibilities->create : $rows[$j]->p_create;
                     //$possibilities->publish_childs = $possibilities->publish_childs ? $possibilities->publish_childs : $rows[$j]->p_publish_childs;
                 }
                 if ($rows[$j]->folder_flag == 1) {
                     // just a folder (without custom permissions)
                     $rows[$j]->p_create = $permissions[$folder_id]->p_create;
                     $rows[$j]->p_publish_childs = $permissions[$folder_id]->p_publish_childs;
                     $possibilities->create = $possibilities->create ? $possibilities->create : $rows[$j]->p_create;
                     $possibilities->publish_childs = $possibilities->publish_childs ? $possibilities->publish_childs : $rows[$j]->p_publish_childs;
                     // HERE: inherit permissions from the parent folder
                     $folder_id = $rows[$j]->id;
                     $parent_id = $rows[$j]->parent_id;
                     if (isset($permissions[$parent_id]) && isset($permissions[$parent_id]->active) && $permissions[$parent_id]->active) {
                         $permissions[$folder_id]->active = 1;
                         $permissions[$folder_id]->p_view = isset($permissions[$folder_id]->p_view) && $permissions[$folder_id]->p_view || !isset($permissions[$folder_id]->p_view) ? $permissions[$parent_id]->p_view : $permissions[$folder_id]->p_view;
                         $permissions[$folder_id]->p_viewall = isset($permissions[$folder_id]->p_viewall) && $permissions[$folder_id]->p_viewall || !isset($permissions[$folder_id]->p_viewall) ? $permissions[$parent_id]->p_viewall : $permissions[$folder_id]->p_viewall;
                         $permissions[$folder_id]->p_order = isset($permissions[$folder_id]->p_order) && $permissions[$folder_id]->p_order || !isset($permissions[$folder_id]->p_order) ? $permissions[$parent_id]->p_order : $permissions[$folder_id]->p_order;
                         $permissions[$folder_id]->p_publish = isset($permissions[$folder_id]->p_publish) && $permissions[$folder_id]->p_publish || !isset($permissions[$folder_id]->p_publish) ? $permissions[$parent_id]->p_publish : $permissions[$folder_id]->p_publish;
                         $permissions[$folder_id]->p_manage = isset($permissions[$folder_id]->p_manage) && $permissions[$folder_id]->p_manage || !isset($permissions[$folder_id]->p_manage) ? $permissions[$parent_id]->p_manage : $permissions[$folder_id]->p_manage;
                         $permissions[$folder_id]->p_create = isset($permissions[$folder_id]->p_create) && $permissions[$folder_id]->p_create || !isset($permissions[$folder_id]->p_create) ? $permissions[$parent_id]->p_create : $permissions[$folder_id]->p_create;
                         $permissions[$folder_id]->p_publish_childs = isset($permissions[$folder_id]->p_publish_childs) && $permissions[$folder_id]->p_publish_childs || !isset($permissions[$folder_id]->p_publish_childs) ? $permissions[$parent_id]->p_publish_childs : $permissions[$folder_id]->p_publish_childs;
                         if (!$permissions[$folder_id]->p_view) {
                             // "no view - no manage" // 'no martiny - no party ;)'
                             $permissions[$folder_id]->p_viewall = 0;
                             $permissions[$folder_id]->p_order = 0;
                             $permissions[$folder_id]->p_publish = 0;
                             $permissions[$folder_id]->p_manage = 0;
                             $permissions[$folder_id]->p_create = 0;
                             $permissions[$folder_id]->p_publish_childs = 0;
                         }
                     }
                 }
             }
             $j++;
         }
     }
     $docs_rows = $rows;
     $docs_possibilities = $possibilities;
 }
function JLMS_showHomeWork($id, $option)
{
    global $my, $JLMS_DB, $Itemid, $JLMS_SESSION, $JLMS_CONFIG;
    $course_id = JRequest::getInt('course_id');
    $id = $course_id ? $course_id : $id;
    $usertype = JLMS_GetUserType($my->id, $id);
    $JLMS_ACL =& JLMSFactory::getACL();
    $assigned_groups_only = $JLMS_ACL->CheckPermissions('advanced', 'assigned_groups_only');
    $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));
    $where = '';
    //	if ($id && ($usertype == 1 || $usertype == 6)) {
    //	if ($id && ($JLMS_ACL->CheckPermissions('homework', 'manage') || $usertype == 6)) {
    if ($id && ($JLMS_ACL->CheckPermissions('homework', 'manage') || $JLMS_ACL->CheckPermissions('homework', 'view_stats'))) {
        $members = "'0'";
        $AND_ST = "";
        $fields = ',0 AS checkedout';
        if ($JLMS_ACL->_role_type == 2 || $JLMS_ACL->_role_type == 3 || $JLMS_ACL->_role_type == 4) {
            if ($assigned_groups_only) {
                $fields = ",IF(a.owner_id != '" . $my->id . "',1 ,0) AS checkedout";
                $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)) {
                    $where .= "\n AND (a.is_limited = 0 OR a.groups LIKE '%|{$groups_where_admin_manager['0']}|%'";
                    for ($i = 1; $i < count($groups_where_admin_manager); $i++) {
                        $where .= "\n OR a.groups like '%|{$groups_where_admin_manager[$i]}|%'";
                    }
                    $where .= "\n OR a.owner_id = '" . $my->id . "')";
                } else {
                    $where .= "\n AND (a.is_limited = 0 OR a.owner_id = '" . $my->id . "' OR a.id = 0) AND a.groups = ''";
                }
            }
        } else {
            if ($JLMS_ACL->_role_type < 2) {
                $query = "select a.group_id FROM #__lms_users_in_global_groups as a WHERE a.user_id = '" . $my->id . "' AND a.subgroup1_id = 0 AND a.group_id > 0";
                $JLMS_DB->setQuery($query);
                $temp1 = $JLMS_DB->loadResultArray();
                $query = "select subgroup1_id FROM #__lms_users_in_global_groups WHERE user_id = '" . $my->id . "' AND subgroup1_id > 0";
                $JLMS_DB->setQuery($query);
                $temp2 = $JLMS_DB->loadResultArray();
                $group_where_isset_user = array_merge($temp1, $temp2);
                if (count($group_where_isset_user)) {
                    $where .= "\n AND (( a.groups <> '' AND a.groups IS NOT NULL AND (groups LIKE '%|{$group_where_isset_user['0']}|%'";
                    for ($i = 1; $i < count($group_where_isset_user); $i++) {
                        $where .= "\n OR groups like '%|{$group_where_isset_user[$i]}|%'";
                    }
                    $where .= "\n )) OR (a.is_limited = 0 AND (a.groups = '' OR a.groups IS NULL)))";
                }
                if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $id))) {
                    $AND_ST = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
                }
            }
        }
        $query = "SELECT count(*) FROM #__lms_homework AS a" . "\n WHERE a.course_id = '" . $id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('homework', 'view_all') ? '' : "\n AND a.published = 1") . $where;
        $JLMS_DB->SetQuery($query);
        $total = $JLMS_DB->LoadResult();
        require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "classes" . DS . "lms.pagination.php";
        $pageNav = new JLMSPageNav($total, $limitstart, $limit);
        $query = "SELECT a.*" . $fields . "\n FROM #__lms_homework as a" . "\n WHERE a.course_id = '" . $id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('homework', 'view_all') ? '' : "\n AND a.published = 1") . $where . "\n ORDER BY a.post_date DESC, a.hw_name" . "\n LIMIT {$pageNav->limitstart}, {$pageNav->limit}";
        $JLMS_DB->SetQuery($query);
        $rows = $JLMS_DB->LoadObjectList();
        $lms_titles_cache =& JLMSFactory::getTitles();
        $lms_titles_cache->setArray('homework', $rows, 'id', 'hw_name');
        JLMS_homework_html::showHomeWorks($id, $option, $rows, $pageNav, $usertype);
    } elseif ($id && $JLMS_ACL->CheckPermissions('homework', 'view')) {
        $filt_hw = intval(mosGetParam($_GET, 'filt_hw', $JLMS_SESSION->get('filt_hw', 0)));
        $JLMS_SESSION->set('filt_hw', $filt_hw);
        $query = "select a.group_id FROM #__lms_users_in_global_groups as a WHERE a.user_id = '" . $my->id . "' AND a.subgroup1_id = 0 AND a.group_id > 0";
        $JLMS_DB->setQuery($query);
        $temp1 = $JLMS_DB->loadResultArray();
        $query = "select subgroup1_id FROM #__lms_users_in_global_groups WHERE user_id = '" . $my->id . "' AND subgroup1_id > 0";
        $JLMS_DB->setQuery($query);
        $temp2 = $JLMS_DB->loadResultArray();
        $group_where_isset_user = array_merge($temp1, $temp2);
        $AND_ST = "";
        if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $id))) {
            $AND_ST = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
        }
        $query = "SELECT count(*) FROM #__lms_homework as a" . ($filt_hw ? "\n LEFT JOIN #__lms_homework_results as b ON a.id = b.hw_id AND b.user_id = '" . $my->id . "' AND b.course_id = '" . $id . "'" : '') . "\n WHERE a.course_id = '" . $id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('homework', 'view_all') ? '' : "\n AND a.published = 1") . ($filt_hw ? $filt_hw == 2 ? "\n AND b.hw_status = 1" : ($filt_hw == 1 ? " AND (b.hw_status IS NULL OR b.hw_status <> 1)" : "") : '');
        $query .= "\n AND a.is_limited = 0";
        if (isset($group_where_isset_user) && count($group_where_isset_user)) {
            $query .= "\n OR (a.is_limited = 1" . "\n AND (a.groups LIKE '%|{$group_where_isset_user['0']}|%'";
            for ($i = 1; $i < count($group_where_isset_user); $i++) {
                $query .= "\n OR a.groups like '%|{$group_where_isset_user[$i]}|%'";
            }
            $query .= "\n ))";
        }
        $JLMS_DB->SetQuery($query);
        $total = $JLMS_DB->LoadResult();
        require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "classes" . DS . "lms.pagination.php";
        $pageNav = new JLMSPageNav($total, $limitstart, $limit);
        $query = "SELECT a.*, b.id as result_id, b.hw_status, b.hw_date, b.grade" . "\n FROM #__lms_homework as a LEFT JOIN #__lms_homework_results as b ON a.id = b.hw_id AND b.user_id = '" . $my->id . "' AND b.course_id = '" . $id . "'" . "\n WHERE a.course_id = '" . $id . "'" . $AND_ST . ($filt_hw ? $filt_hw == 2 ? "\n AND b.hw_status = 1" : ($filt_hw == 1 ? " AND (b.hw_status IS NULL OR b.hw_status <> 1)" : "") : '') . ($JLMS_ACL->CheckPermissions('homework', 'view_all') ? '' : "\n AND a.published = 1");
        $query .= "\n AND (a.is_limited = 0";
        if (isset($group_where_isset_user) && count($group_where_isset_user)) {
            $query .= "\n OR (a.is_limited = 1" . "\n AND (a.groups LIKE '%|{$group_where_isset_user['0']}|%'";
            for ($i = 1; $i < count($group_where_isset_user); $i++) {
                $query .= "\n OR a.groups like '%|{$group_where_isset_user[$i]}|%'";
            }
            $query .= "\n )))";
        } else {
            $query .= "\n )";
        }
        $query .= "\n ORDER BY a.post_date DESC, a.hw_name" . "\n LIMIT {$pageNav->limitstart}, {$pageNav->limit}";
        $JLMS_DB->SetQuery($query);
        $rows = $JLMS_DB->LoadObjectList();
        $lists = array();
        $f_items = array();
        $f_items[] = mosHTML::makeOption(0, _JLMS_HW_FILTER_ALL_RESULTS);
        $f_items[] = mosHTML::makeOption(2, _JLMS_HW_STATUS_COMPLETED);
        $f_items[] = mosHTML::makeOption(1, _JLMS_HW_STATUS_INCOMPLETE);
        $link = "index.php?option={$option}&amp;Itemid={$Itemid}&task=homework&id={$id}";
        $link = $link . "&amp;filt_hw='+this.options[selectedIndex].value+'";
        $link = sefRelToAbs($link);
        $link = str_replace('%5C%27', "'", $link);
        $link = str_replace('%5B', "[", $link);
        $link = str_replace('%5D', "]", $link);
        $link = str_replace('%20', "+", $link);
        $link = str_replace("\\\\\\", "", $link);
        $link = str_replace('%27', "'", $link);
        $lists['filter'] = mosHTML::selectList($f_items, 'filt_hw', 'class="inputbox" size="1" onchange="document.location.href=\'' . $link . '\';"', 'value', 'text', $filt_hw);
        $lists['used_filter'] = $filt_hw;
        $lms_titles_cache =& JLMSFactory::getTitles();
        $lms_titles_cache->setArray('homework', $rows, 'id', 'hw_name');
        JLMS_homework_html::showHomeWorks_stu($id, $option, $rows, $pageNav, $lists);
    } else {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}"));
    }
}