function JLMS_deleteDropBox($course_id, $option)
{
    global $my, $JLMS_DB, $Itemid;
    $usertype = JLMS_GetUserType($my->id, $course_id);
    if ($course_id && $usertype == 1) {
        $cid = mosGetParam($_REQUEST, 'cid', array(0));
        if (is_array($cid) && count($cid) > 0) {
            $cids = implode(',', $cid);
            $query = "SELECT distinct file_id FROM #__lms_dropbox WHERE id IN ({$cids}) AND course_id = '" . $course_id . "'";
            $JLMS_DB->SetQuery($query);
            $files = $JLMS_DB->LoadResultArray();
            if (count($files)) {
                $query = "DELETE FROM #__lms_dropbox WHERE id IN ({$cids}) AND course_id = '" . $course_id . "'";
                $JLMS_DB->SetQuery($query);
                $JLMS_DB->query();
                $files_del = JLMS_checkFiles($course_id, $files);
                if (count($files_del)) {
                    JLMS_deleteFiles($files_del);
                }
            }
        }
    } elseif ($course_id && $usertype == 2) {
        $cid = mosGetParam($_REQUEST, 'cid', array(0));
        if (is_array($cid) && count($cid) > 0) {
            $cids = implode(',', $cid);
            $query = "SELECT distinct file_id FROM #__lms_dropbox WHERE id IN ({$cids}) AND course_id = '" . $course_id . "' AND owner_id = '" . $my->id . "'";
            $JLMS_DB->SetQuery($query);
            $files = $JLMS_DB->LoadResultArray();
            if (count($files)) {
                $query = "DELETE FROM #__lms_dropbox WHERE id IN ({$cids}) AND course_id = '" . $course_id . "' AND owner_id = '" . $my->id . "'";
                $JLMS_DB->SetQuery($query);
                $JLMS_DB->query();
                $files_del = JLMS_checkFiles($course_id, $files);
                if (count($files_del)) {
                    JLMS_deleteFiles($files_del);
                }
            }
        }
    }
    JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=dropbox&id={$course_id}"));
}
示例#2
0
 function JQ_NextPreview()
 {
     global $JLMS_DB, $my;
     $ret_str = '';
     $quiz_id = intval(mosGetParam($_REQUEST, 'quiz', 0));
     $quest_id = intval(mosGetParam($_REQUEST, 'quest_id', 0));
     $preview_id = '111';
     //strval( mosGetParam( $_REQUEST, 'preview_id', '' ) );
     $id = intval(mosGetParam($_REQUEST, 'id', 0));
     $usertype = JLMS_GetUserType($my->id, $id);
     $query = "SELECT * FROM #__lms_quiz_t_quiz WHERE c_id = '" . $quiz_id . "' AND course_id = '" . $id . "'";
     $JLMS_DB->SetQuery($query);
     $quiz = $JLMS_DB->LoadObjectList();
     if (count($quiz)) {
         $quiz = $quiz[0];
     } else {
         $quiz = new stdClass();
         $quiz->c_wrong_message = '';
         $quiz->c_right_message = '';
     }
     //$query = "SELECT c_par_value FROM #__lms_quiz_setup WHERE c_par_name = 'admin_preview'";
     //$JLMS_DB->SetQuery( $query );
     $preview_code = '111';
     //$JLMS_DB->LoadResult();
     $query = "SELECT c_quiz_id FROM #__lms_quiz_t_question WHERE c_id = '" . $quest_id . "'";
     $JLMS_DB->SetQuery($query);
     $q_quiz = $JLMS_DB->LoadResult();
     if ($quiz_id == $q_quiz && $usertype == 1 && $preview_id == $preview_code && $quest_id) {
         $answer = strval(isset($_REQUEST['answer']) ? $_REQUEST['answer'] : '');
         $answer = get_magic_quotes_gpc() ? stripslashes($answer) : $answer;
         $quest_id_pre = $quest_id;
         $query = "SELECT c_type from #__lms_quiz_t_question WHERE c_id = '" . $quest_id . "'";
         $JLMS_DB->SetQuery($query);
         $qtype = $JLMS_DB->LoadResult();
         $query = "SELECT params from #__lms_quiz_t_question WHERE c_id = '" . $quest_id . "'";
         $JLMS_DB->SetQuery($query);
         $qparams = $JLMS_DB->LoadResult();
         $quest_params = new JLMSParameters($qparams);
         if ($qtype == 20) {
             $query = "SELECT c_pool from #__lms_quiz_t_question WHERE c_id = '" . $quest_id . "'";
             $JLMS_DB->SetQuery($query);
             $qpool = $JLMS_DB->LoadResult();
             if ($qpool) {
                 $query = "SELECT a.* FROM #__lms_quiz_t_question as a" . "\n WHERE a.c_id = " . $qpool;
                 $JLMS_DB->setQuery($query);
                 $pool_quest = $JLMS_DB->loadObject();
                 if (is_object($pool_quest)) {
                     $qtype = $pool_quest->c_type;
                     $quest_id = $pool_quest->c_id;
                     $quest_params = new JLMSParameters($pool_quest->params);
                 }
             }
         }
         if ($qtype == 21) {
             $query = "SELECT c_pool_gqp from #__lms_quiz_t_question WHERE c_id = '" . $quest_id . "'";
             $JLMS_DB->SetQuery($query);
             $qpool_gqp = $JLMS_DB->LoadResult();
             if ($qpool_gqp) {
                 $query = "SELECT a.* FROM #__lms_quiz_t_question as a" . "\n WHERE a.c_id = " . $qpool_gqp;
                 $JLMS_DB->setQuery($query);
                 $pool_quest_gqp = $JLMS_DB->loadObject();
                 if (is_object($pool_quest_gqp)) {
                     $qtype = $pool_quest_gqp->c_type;
                     $quest_id = $pool_quest_gqp->c_id;
                     $quest_params = new JLMSParameters($pool_quest_gqp->params);
                 }
             }
         }
         $is_correct = 0;
         switch ($qtype) {
             case 1:
             case 3:
             case 12:
                 $query = "SELECT a.c_point, b.c_id, a.c_attempts FROM #__lms_quiz_t_question as a, #__lms_quiz_t_choice as b WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id and b.c_right = '1'";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 if (count($ddd)) {
                     if ($ddd[0]->c_id == $answer) {
                         $is_correct = 1;
                     }
                 }
                 break;
             case 2:
             case 13:
                 $query = "SELECT a.c_point, a.c_attempts FROM #__lms_quiz_t_question as a WHERE a.c_id = '" . $quest_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 $query = "SELECT b.c_id FROM #__lms_quiz_t_question as a, #__lms_quiz_t_choice as b WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id and b.c_right = '1'";
                 $JLMS_DB->SetQuery($query);
                 $ddd2 = $JLMS_DB->LoadObjectList();
                 $query = "SELECT b.c_id FROM #__lms_quiz_t_question as a, #__lms_quiz_t_choice as b WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id and b.c_right <> '1'";
                 $JLMS_DB->SetQuery($query);
                 $ddd3 = $JLMS_DB->LoadObjectList();
                 $ans_array = explode(',', $answer);
                 if (count($ddd2) && count($ddd)) {
                     $is_correct = 1;
                     foreach ($ddd2 as $right_row) {
                         if (!in_array($right_row->c_id, $ans_array)) {
                             $is_correct = 0;
                         }
                     }
                     foreach ($ddd3 as $not_right_row) {
                         if (in_array($not_right_row->c_id, $ans_array)) {
                             $is_correct = 0;
                         }
                     }
                 }
                 break;
             case 4:
             case 5:
             case 11:
                 $query = "SELECT a.c_point, a.c_attempts FROM #__lms_quiz_t_question as a WHERE a.c_id = '" . $quest_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 $query = "SELECT b.c_id, b.c_left_text, b.c_right_text FROM #__lms_quiz_t_question as a, #__lms_quiz_t_matching as b WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id ORDER BY b.ordering";
                 $JLMS_DB->SetQuery($query);
                 $ddd2 = $JLMS_DB->LoadObjectList();
                 $answer = urldecode($answer);
                 $ans_array = explode('```', $answer);
                 if (count($ddd2) && count($ddd)) {
                     $is_correct = 1;
                     $rr_num = 0;
                     for ($di = 0, $dn = count($ddd2); $di < $dn; $di++) {
                         $ddd2[$di]->c_right_text_md5 = md5($ddd2[$di]->c_right_text);
                     }
                     foreach ($ddd2 as $right_row) {
                         if ($right_row->c_right_text_md5 != $ans_array[$rr_num]) {
                             $is_correct = 0;
                         }
                         $rr_num++;
                     }
                 }
                 break;
             case 6:
                 $query = "SELECT a.c_point, a.c_attempts FROM #__lms_quiz_t_question as a WHERE a.c_id = '" . $quest_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 $query = "SELECT c.c_text FROM #__lms_quiz_t_question as a, #__lms_quiz_t_blank as b, #__lms_quiz_t_text as c WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id and c.c_blank_id = b.c_id";
                 $JLMS_DB->SetQuery($query);
                 $ddd2 = $JLMS_DB->LoadObjectList();
                 $answer = trim(urldecode($answer));
                 if (count($ddd2) && count($ddd)) {
                     /*foreach ($ddd2 as $right_row) {
                     			if ($right_row->c_text == $answer) { $is_correct = 1; }
                     		}*/
                     foreach ($ddd2 as $right_row) {
                         if ($quest_params->get('case_sensivity', 0)) {
                             if ($right_row->c_text === $answer) {
                                 $is_correct = 1;
                             }
                         } else {
                             if (strtolower($right_row->c_text) === strtolower($answer)) {
                                 $is_correct = 1;
                             }
                         }
                         if (!$is_correct) {
                             /**
                              * 01 November 2007 - DEN - bugfix - checking different character encodings
                              * I.e. if browser sent data in UTF, but DB collation is ISO
                              * or another case, if DB collation is UTF, but browser sent response in ISO.
                              * TODO: code is not tested fully. - need testing with ISO (danish, german), cp and UTF
                              */
                             if (function_exists('utf8_encode')) {
                                 $a_u = utf8_encode($right_row->c_text);
                                 $b_u = utf8_encode($answer);
                             } else {
                                 $a_u = $right_row->c_text;
                                 $b_u = $answer;
                             }
                             if ($quest_params->get('case_sensivity', 0)) {
                                 if ($a_u === $answer) {
                                     $is_correct = 1;
                                 } else {
                                     if ($right_row->c_text === $b_u) {
                                         $is_correct = 1;
                                     }
                                 }
                             } else {
                                 if (strtolower($a_u) === strtolower($answer)) {
                                     $is_correct = 1;
                                 } else {
                                     if (strtolower($right_row->c_text) === strtolower($b_u)) {
                                         $is_correct = 1;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 break;
             case 7:
                 $query = "SELECT a.c_point, a.c_attempts, b.c_start_x, b.c_start_y, b.c_width, b.c_height FROM #__lms_quiz_t_question as a, #__lms_quiz_t_hotspot as b WHERE a.c_id = '" . $quest_id . "' and b.c_question_id = a.c_id";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 if (count($ddd)) {
                     $ans_array = explode(',', $answer);
                     if (count($ans_array) == 2 && $ans_array[0] >= $ddd[0]->c_start_x && $ans_array[0] <= $ddd[0]->c_start_x + $ddd[0]->c_width && $ans_array[1] >= $ddd[0]->c_start_y && $ans_array[1] <= $ddd[0]->c_start_y + $ddd[0]->c_height) {
                         $is_correct = 1;
                     }
                 }
                 break;
             case 8:
                 $query = "SELECT a.c_point, a.c_attempts FROM #__lms_quiz_t_question as a WHERE a.c_id = '" . $quest_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $ddd = $JLMS_DB->LoadObjectList();
                 $answer = trim(urldecode($answer));
                 if (count($ddd)) {
                     if ($answer) {
                         $is_correct = 1;
                     }
                 }
                 break;
             case 9:
                 $is_correct = 1;
                 break;
             case 10:
                 $is_correct = 1;
                 break;
         }
         $quest_id = $quest_id_pre;
         $query = "SELECT * FROM #__lms_quiz_t_question WHERE c_quiz_id = '" . $quiz_id . "' AND c_id = '" . $quest_id . "'";
         $JLMS_DB->SetQuery($query);
         $q_data = $JLMS_DB->LoadObjectList();
         $query = "SELECT count(*) FROM #__lms_quiz_t_question WHERE c_quiz_id = '" . $quiz_id . "'";
         $JLMS_DB->SetQuery($query);
         $q_data_count = $JLMS_DB->LoadResult();
         //require( dirname(__FILE__) . "/language/english.php");
         // preloading QUIZ languge (28.02.2007 new method) (all quizzes messages now in global quiz language)
         global $JLMS_LANGUAGE, $JLMS_CONFIG;
         JLMS_require_lang($JLMS_LANGUAGE, 'quiz.lang', $JLMS_CONFIG->get('default_language'));
         //require(_JOOMLMS_FRONT_HOME . "/languages/".$JLMS_CONFIG->get('default_language').'/quiz.lang.php');
         require _JOOMLMS_FRONT_HOME . '/includes/quiz/quiz_language.php';
         global $jq_language;
         /*$query = "SELECT lang_file FROM #__lms_quiz_languages WHERE is_default = 1 and lang_file <> 'default'";
         		$JLMS_DB->SetQuery( $query );
         		$req_lang = $JLMS_DB->LoadResult();
         		if ($req_lang && file_exists( dirname(__FILE__) . "/language/".$req_lang.".php")) {
         			include( dirname(__FILE__) . "/language/".$req_lang.".php");
         		}*/
         /*if ( ($quiz->c_language) && ($quiz->c_language != 1) ) {
         			$query = "SELECT lang_file FROM #__lms_quiz_languages WHERE id = '".intval($quiz->c_language)."'";
         			$JLMS_DB->SetQuery( $query );
         			$req_lang = $JLMS_DB->LoadResult();
         			if ($req_lang && file_exists( dirname(__FILE__) . "/language/".$req_lang.".php")) {
         				include( dirname(__FILE__) . "/language/".$req_lang.".php");
         			}
         		}*/
         if ($quiz->c_wrong_message) {
             $jq_language['quiz_answer_incorrect'] = htmlspecialchars(nl2br($quiz->c_wrong_message));
         }
         if ($quiz->c_right_message) {
             $jq_language['quiz_answer_correct'] = htmlspecialchars(nl2br($quiz->c_right_message));
         }
         $query = "SELECT * FROM #__lms_quiz_t_question_fb WHERE quest_id = {$quest_id}";
         $JLMS_DB->SetQuery($query);
         $q_fbs = $JLMS_DB->LoadObjectList();
         foreach ($q_fbs as $qfb) {
             if ($qfb->choice_id == -1) {
                 if ($qfb->fb_text) {
                     $jq_language['quiz_answer_incorrect'] = $qfb->fb_text;
                 }
             } elseif (!$qfb->choice_id) {
                 if ($qfb->fb_text) {
                     $jq_language['quiz_answer_correct'] = $qfb->fb_text;
                 }
             }
         }
         // 12.03.2007
         #$query = "SELECT template_name FROM #__lms_quiz_templates WHERE id = '".$quiz->c_skin."'";
         #$JLMS_DB->SetQuery( $query );
         #$cur_tmpl = $JLMS_DB->LoadResult();
         $cur_tmpl = 'joomlaquiz_lms_template';
         if ($cur_tmpl) {
             require_once dirname(__FILE__) . '/templates/' . $cur_tmpl . '/jq_template.php';
             if (count($q_data) > 0) {
                 $toolbar = array();
                 $toolbar[] = array('btn_type' => 'prev', 'btn_js' => "javascript:JQ_previewQuest();void(0);");
                 #if ($quiz->c_slide) {
                 #	$toolbar[] = array('btn_type' => 'contents', 'btn_js' => "javascript:jq_ShowPanel();");
                 #}
                 $ret_str .= "\t" . '<task>preview_finish</task>' . "\n";
                 $ret_str .= "\t" . '<quiz_menu><![CDATA[' . JLMS_ShowToolbar($toolbar, false) . ']]></quiz_menu>' . "\n";
                 $ret_str .= "\t" . '<quiz_quest_num>X</quiz_quest_num>' . "\n";
                 $ret_str .= "\t" . '<quiz_prev_correct>' . $is_correct . '</quiz_prev_correct>' . "\n";
                 $msg_html = JoomlaQuiz_template_class::JQ_show_messagebox('', $is_correct ? $qtype == 8 ? $jq_language['quiz_answer_accepted'] : $jq_language['quiz_answer_correct'] : $jq_language['quiz_answer_incorrect'], $is_correct);
                 $ret_str .= "\t" . '<quiz_message_box><![CDATA[' . $msg_html . ']]></quiz_message_box>' . "\n";
             }
         }
     }
     return $ret_str;
 }
function JLMS_show_course_forum($id, $option)
{
    /*
    board_type = 1 - Public/Group course board
    board_type = 2 - LearningPath public course board
    board_type = 3 - Board for users from 'Global group'
    board_type = 4 - Private (teachers-only) boards
    board_type = 5 - Private (teachers-only) LearningPath boards
    */
    global $my, $JLMS_DB, $Itemid, $JLMS_CONFIG;
    $doc =& JFactory::getDocument();
    $usertype = JLMS_GetUserType($my->id, $id);
    if (!$usertype) {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=details_course&id={$id}"));
    }
    $JLMS_ACL =& JLMSFactory::getACL();
    $forum =& JLMS_SMF::getInstance();
    if (!$forum) {
        return false;
    }
    $id_cat = 0;
    $course_name = '';
    $owner_name = '';
    $private_forums_tobe_created = array();
    // list of private forums to be created
    $msg = '';
    //error message - prompt to login if not blank
    $query = "SELECT * FROM #__lms_forums WHERE published = 1";
    $JLMS_DB->setQuery($query);
    $lms_forum_types = $JLMS_DB->loadObjectList('id');
    //[TODO] - check all types of the forums and create/remove necessary forum boards in the SMF - i.e. if they wasn't be created/removed previously
    // Note, autocreation is necessary only for 'global groups' forums and private forums (`access` is 1)
    $give_course_forums = array();
    $need_to_create = false;
    $need_to_update = false;
    if (count($lms_forum_types)) {
        // check all forum types for access.
        $allowed_forum_types = array();
        // allowed forum types
        $lms_forum_types_ids = array();
        foreach ($lms_forum_types as $lft) {
            $do_proceeed = true;
            // firstly - check `access` and `permissions`
            switch (intval($lft->forum_access)) {
                case 0:
                    // any user
                    break;
                case 1:
                    // check user role
                    if ($lft->forum_permissions) {
                        $allowed_roles = explode(',', $lft->forum_permissions);
                        if ($JLMS_ACL->GetRole() && in_array($JLMS_ACL->GetRole(), $allowed_roles)) {
                            // allow access
                        } else {
                            // restrict access if this forum is not permissioned.
                            $do_proceeed = false;
                        }
                    } else {
                        // if this is a restricted area but there is no roles configured
                        $do_proceeed = false;
                    }
                    break;
            }
            if (!$do_proceeed) {
                continue;
            }
            // secondly - check `user_level` permissions
            switch (intval($lft->user_level)) {
                case 0:
                    // any user
                    break;
                case 1:
                    // local usergroup forum
                // local usergroup forum
                case 2:
                    // global usergroup forum
                    if ($lft->user_level == 1 && $JLMS_CONFIG->get('use_global_groups', 1)) {
                        $do_proceeed = false;
                        // restrict access to the local-group forum, if we are in the global mode
                    } elseif ($lft->user_level == 2 && !$JLMS_CONFIG->get('use_global_groups', 1)) {
                        $do_proceeed = false;
                        // restrict access to the global-group forum, if we are in the local mode
                    }
                    break;
            }
            if (!$do_proceeed) {
                continue;
            }
            $allowed_forum_types[$lft->id] = $lft;
            // we have a list of allowed forum types here.
            $lms_forum_types_ids[] = $lft->id;
        }
        if (count($lms_forum_types_ids)) {
            $query = "SELECT id, course_id, board_type, group_id, ID_GROUP AS id_group, ID_CAT AS id_cat, ID_BOARD AS id_board, is_active, need_update FROM #__lms_forum_details" . "\n WHERE course_id = {$id}" . "\n AND board_type IN (" . implode(',', $lms_forum_types_ids) . ")";
            $JLMS_DB->setQuery($query);
            $active_course_forums = $JLMS_DB->loadObjectList();
            for ($i = 0; $i < count($active_course_forums); $i++) {
                $active_course_forums[$i]->forum_level = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_level;
                $active_course_forums[$i]->user_level = $lms_forum_types[$active_course_forums[$i]->board_type]->user_level;
                $active_course_forums[$i]->forum_access = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_access;
                $active_course_forums[$i]->forum_permissions = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_permissions;
                $active_course_forums[$i]->parent_forum = $lms_forum_types[$active_course_forums[$i]->board_type]->parent_forum;
                $active_course_forums[$i]->forum_moderators = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_moderators;
                $active_course_forums[$i]->forum_name = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_name;
                $active_course_forums[$i]->forum_desc = $lms_forum_types[$active_course_forums[$i]->board_type]->forum_desc;
                $active_course_forums[$i]->moderated = $lms_forum_types[$active_course_forums[$i]->board_type]->moderated;
                // !!!! ATTENTION, `need_update` field was moved to the `lms_forum_details` table //$active_course_forums[$i]->need_update = $lms_forum_types[$active_course_forums[$i]->board_type]->need_update;
            }
            // [TODO] - inspect each forum type and populate list of necessary forums (to be created or already created)
            $give_course_forums = array();
            foreach ($allowed_forum_types as $aft) {
                switch (intval($aft->forum_level)) {
                    case 0:
                        switch (intval($aft->user_level)) {
                            case 0:
                                //one forum should be created for course.
                                $forum->populateCourseForums($id, $give_course_forums, $active_course_forums, $aft);
                                break;
                            case 1:
                                $forum->populateLgroupForums($id, $give_course_forums, $active_course_forums, $aft);
                                break;
                            case 2:
                                $forum->populateGgroupForums($id, $give_course_forums, $active_course_forums, $aft);
                                break;
                        }
                        break;
                    case 1:
                        if (intval($aft->user_level) == 0) {
                            $forum->populateLpathForums($id, $give_course_forums, $active_course_forums, $aft);
                        } else {
                            //ignore any other forums - currently there is no posibility to have such difficult structure
                        }
                        break;
                }
            }
            if (count($give_course_forums)) {
                $need_to_create = false;
                for ($i = 0; $i < count($give_course_forums); $i++) {
                    if ($give_course_forums[$i]->id == 0) {
                        $need_to_create = true;
                    }
                    if (isset($give_course_forums[$i]->need_update) && $give_course_forums[$i]->need_update) {
                        $need_to_update = true;
                    }
                    if ($need_to_create && $need_to_update) {
                        break;
                    }
                }
            }
        }
    }
    $was_created = array();
    $was_updated = array();
    $newcat_wascreated = false;
    if (count($give_course_forums)) {
        //echo '<pre>';var_dump($give_course_forums);die;
        if ($need_to_create || $need_to_update) {
            $query = "SELECT a.course_name, a.owner_id, b.username as owner_name FROM #__lms_courses as a LEFT JOIN #__users as b ON a.owner_id = b.id WHERE a.id = {$id}";
            $JLMS_DB->setQuery($query);
            $course_info = $JLMS_DB->loadObject();
            if (is_object($course_info)) {
                $course_name = $course_info->course_name;
                // - we need it to create boards
                $owner_name = $course_info->owner_name;
            }
        }
        $query = "SELECT ID_CAT AS id_cat FROM #__lms_forum_details WHERE course_id = {$id} LIMIT 0,1";
        $JLMS_DB->setQuery($query);
        $id_cat = $JLMS_DB->loadResult();
        // ID of the Course CATEGORY (in which all course boards are placed) - we need it to create boards
        // TODO:
        // check if SMF boards was removed and create new ones if necessary!
        $query = "SELECT distinct ID_GROUP AS id_group FROM `#__lms_forum_details` ";
        $JLMS_DB->setQuery($query);
        $all_lms_groups = $JLMS_DB->loadResultArray();
        $all_moderator_ids = array();
        $all_moderators = array();
        for ($i = 0; $i < count($give_course_forums); $i++) {
            if (($need_to_create && $give_course_forums[$i]->id == 0 || $give_course_forums[$i]->need_update) && $give_course_forums[$i]->forum_moderators) {
                $new_moderators = explode(',', $give_course_forums[$i]->forum_moderators);
                $all_moderator_ids = array_merge($all_moderator_ids, $new_moderators);
            }
        }
        if (count($all_moderator_ids)) {
            for ($i = 0; $i < count($all_moderator_ids); $i++) {
                $all_moderator_ids[$i] = intval($all_moderator_ids[$i]);
            }
            $all_moderator_ids = array_unique($all_moderator_ids);
            if (!empty($all_moderator_ids)) {
                $all_moderator_ids_str = implode(',', $all_moderator_ids);
                $query = "SELECT id, username FROM #__users WHERE id IN ({$all_moderator_ids_str})";
                $JLMS_DB->setQuery($query);
                $all_moderators = $JLMS_DB->LoadObjectList('id');
            }
        }
        if (!$id_cat) {
            // CATEGORY for this course doesn't exists - we need to create new one
            $storeData = array();
            $storeData['name'] = $course_name;
            $storeData['can_collapse'] = 1;
            $id_cat = $forum->storeCategory($storeData);
            $newcat_wascreated = true;
            // TODO:
            // if instead CAT_ID #__forum_details was populated with 0 - we need to update #__forum_details table and boards in SMF db
            // all updates to Joola db are 100 - 150 lines below
        }
        $user_exists = false;
        $is_forum_category = 0;
        $boardurl = $forum->getBoardURL();
        $link = $boardurl . '/index.php#' . $id_cat;
        $smf_user_details = $forum->loadMemberByName($my->username);
        if ($smf_user_details) {
            $user_exists = true;
            $all_current_smf_groups = array();
            // create all parent forums
            for ($i = 0; $i < count($give_course_forums); $i++) {
                if ($need_to_create && $give_course_forums[$i]->id == 0 && !$give_course_forums[$i]->parent_forum) {
                    if ($give_course_forums[$i]->forum_level == 1 && $give_course_forums[$i]->user_level == 0 || $give_course_forums[$i]->forum_level == 0 && $give_course_forums[$i]->user_level == 2 || $give_course_forums[$i]->forum_level == 0 && $give_course_forums[$i]->user_level == 0) {
                        $tmp = $forum->create($give_course_forums, $i, $course_name, $owner_name, $all_moderators, $id_cat);
                        $was_created[] = $tmp;
                        $all_current_smf_groups[] = $tmp->id_group;
                    }
                } elseif ($give_course_forums[$i]->id && $give_course_forums[$i]->need_update && !$give_course_forums[$i]->parent_forum) {
                    $tmp = $forum->update($give_course_forums, $i, $course_name, $owner_name, $all_moderators, $id_cat);
                    $was_updated[] = $tmp;
                    $all_current_smf_groups[] = $give_course_forums[$i]->id_group;
                } elseif ($give_course_forums[$i]->id && !$give_course_forums[$i]->need_update) {
                    $all_current_smf_groups[] = $give_course_forums[$i]->id_group;
                }
            }
            //crate all nested forums
            for ($i = 0; $i < count($give_course_forums); $i++) {
                if ($need_to_create && $give_course_forums[$i]->id == 0 && $give_course_forums[$i]->parent_forum) {
                    if ($give_course_forums[$i]->forum_level == 1 && $give_course_forums[$i]->user_level == 0 || $give_course_forums[$i]->forum_level == 0 && $give_course_forums[$i]->user_level == 2 || $give_course_forums[$i]->forum_level == 0 && $give_course_forums[$i]->user_level == 0) {
                        $tmp = $forum->create($give_course_forums, $i, $course_name, $owner_name, $all_moderators, $id_cat);
                        $was_created[] = $tmp;
                        $all_current_smf_groups[] = $tmp->id_group;
                    }
                } elseif ($give_course_forums[$i]->id && $give_course_forums[$i]->need_update && $give_course_forums[$i]->parent_forum) {
                    $tmp = $forum->update($give_course_forums, $i, $course_name, $owner_name, $all_moderators, $id_cat);
                    $was_updated[] = $tmp;
                    $all_current_smf_groups[] = $give_course_forums[$i]->id_group;
                }
            }
            $mem_id = $smf_user_details->id_member;
            $mem_real_name = $smf_user_details->real_name;
            $primary_group = $smf_user_details->id_group;
            $old_groups = explode(',', $smf_user_details->additional_groups);
            $old_groups_save = array();
            foreach ($old_groups as $group) {
                if (!in_array($group, $all_lms_groups)) {
                    $old_groups_save[] = $group;
                }
            }
            $forum_groups = array_unique(array_merge($all_current_smf_groups, $old_groups_save));
            $new_forum_groups = array();
            foreach ($forum_groups as $fg) {
                if ($fg) {
                    $new_forum_groups[] = $fg;
                }
            }
            $groups = implode(',', $new_forum_groups);
            if (!$mem_real_name && isset($my->name) && $my->name) {
                // update real_name of user, if it is missed
                $storeData = array();
                $storeData['id_member'] = $mem_id;
                $storeData['id_group'] = $primary_group;
                $storeData['additional_groups'] = $groups;
                $storeData['real_name'] = $my->name;
            } else {
                $storeData = array();
                $storeData['id_member'] = $mem_id;
                $storeData['id_group'] = $primary_group;
                $storeData['additional_groups'] = $groups;
            }
            $forum->storeMember($storeData);
            if (count($give_course_forums) == 1) {
                $is_forum_category = 0;
                $link = $boardurl . '/index.php?board=' . $give_course_forums[0]->id_board . '.0';
            } elseif (count($give_course_forums) > 1) {
                $is_forum_category = 1;
                $link = $boardurl . '/index.php#' . $id_cat;
            }
            $topic_id = JRequest::getVar('topic_id', 0);
            $message_id = JRequest::getVar('message_id', 0);
            if ($topic_id && $message_id) {
                $link = $boardurl . '/index.php';
                $link .= '?topic=' . $topic_id;
                $link .= '.msg' . $message_id;
                $link .= '#msg' . $message_id;
            }
            if (true) {
                //($is_forum_category) {
                $forum_tree = $forum->selectBoards();
                $user_cats = array();
                foreach ($forum_tree as $ft) {
                    $ar = $ft->member_groups;
                    $ar = explode(',', $ar);
                    $is_cat = false;
                    foreach ($new_forum_groups as $gia) {
                        if (in_array($gia, $ar)) {
                            $is_cat = true;
                            break;
                        }
                    }
                    if ($is_cat) {
                        $user_cats[] = $ft->id_cat;
                    }
                }
                $user_cats = array_unique($user_cats);
                if (count($user_cats)) {
                    $forum->deleteCollapsedCategories($mem_id, $user_cats);
                    $new_ar = array($id_cat);
                    $rrr = array_diff($user_cats, $new_ar);
                    $forum->insertCollapsedCategories($mem_id, $rrr);
                }
            }
        }
        $mem_id_cookies = 0;
        if ($user_exists) {
            $mem_id_cookies = JLMS_checkSMF_cookies(false);
        }
        if ($need_to_create) {
            if (count($was_created)) {
                $query = "INSERT INTO #__lms_forum_details (course_id, board_type, group_id, ID_GROUP, ID_CAT, ID_BOARD, is_active) VALUES";
                $first = 1;
                foreach ($was_created as $obj) {
                    $query .= $first ? '' : ',';
                    $query .= "\n({$id}, {$obj->board_type}, {$obj->group_id}, {$obj->id_group}, {$obj->id_cat}, {$obj->id_board}, 1)";
                    $first = 0;
                }
                $JLMS_DB->setQuery($query);
                $JLMS_DB->query();
            }
        }
        if (isset($was_updated) && count($was_updated)) {
            $was_updated = array_unique($was_updated);
            if (!empty($was_updated)) {
                $was_updated_str = implode(',', $was_updated);
                $query = "UPDATE #__lms_forum_details SET need_update = 0 WHERE id IN ({$was_updated_str}) AND need_update = 1";
                $JLMS_DB->setQuery($query);
                $JLMS_DB->query();
            }
        }
        if ($user_exists) {
            if ($mem_id && $mem_id_cookies && $mem_id_cookies == $mem_id) {
                $mbname = $forum->getMbname();
                $doc->setTitle($mbname);
                //[TODO] - what is it - $mbname and $boardurl - from the Settings.php?
            } else {
                $msg = JLMS_FORUM_NOT_MEMBER;
            }
        } else {
            $msg = JLMS_FORUM_NOT_MEMBER;
        }
    } else {
        JLMSRedirect(sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}&task=details_course&id={$id}"));
    }
    JLMS_course_forum_html::wrapper_course_forum($link, $option, $id, $msg);
    return true;
}
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}"));
    }
}