Esempio n. 1
0
function insCourse()
{
    require_once _base_ . '/lib/lib.upload.php';
    require_once _base_ . '/lib/lib.multimedia.php';
    require_once $GLOBALS['where_lms'] . '/lib/lib.course.php';
    require_once $GLOBALS['where_lms'] . '/lib/lib.manmenu.php';
    if ($_POST['course_type'] === 'classroom') {
        $url = 'index.php?r=alms/classroom/show&result=';
    } else {
        $url = 'index.php?modname=course&op=course_list&result=';
    }
    $array_lang = Docebo::langManager()->getAllLangCode();
    $array_lang[] = 'none';
    $acl_man =& Docebo::user()->getAclManager();
    $id_custom = importVar('selected_menu');
    // calc quota limit
    $quota = $_POST['course_quota'];
    if (isset($_POST['inherit_quota'])) {
        $quota = Get::sett('course_quota');
        $_POST['course_quota'] = COURSE_QUOTA_INHERIT;
    }
    $quota = $quota * 1024 * 1024;
    $path = Get::sett('pathcourse');
    $path = '/appLms/' . Get::sett('pathcourse') . (substr($path, -1) != '/' && substr($path, -1) != '\\' ? '/' : '');
    if ($_POST['course_name'] == '') {
        $_POST['course_name'] = Lang::t('_NO_NAME', 'course', 'lms');
    }
    // restriction on course status ------------------------------------------
    $user_status = 0;
    if (isset($_POST['user_status'])) {
        while (list($status) = each($_POST['user_status'])) {
            $user_status |= 1 << $status;
        }
    }
    // level that will be showed in the course --------------------------------
    $show_level = 0;
    if (isset($_POST['course_show_level'])) {
        while (list($lv) = each($_POST['course_show_level'])) {
            $show_level |= 1 << $lv;
        }
    }
    // save the file uploaded -------------------------------------------------
    $file_sponsor = '';
    $file_logo = '';
    $file_material = '';
    $file_othermaterial = '';
    $file_demo = '';
    $error = false;
    $quota_exceeded = false;
    $total_file_size = 0;
    if (is_array($_FILES) && !empty($_FILES)) {
        sl_open_fileoperations();
    }
    // load user material ---------------------------------------------------------------------------
    $arr_file = manageCourseFile('course_user_material', '', $path, $quota != 0 ? $quota - $total_file_size : false, false);
    $error |= $arr_file['error'];
    $quota_exceeded |= $arr_file['quota_exceeded'];
    $file_material = $arr_file['filename'];
    $total_file_size = $total_file_size + $arr_file['new_size'];
    // course otheruser material -------------------------------------------------------------------
    $arr_file = manageCourseFile('course_otheruser_material', '', $path, $quota != 0 ? $quota - $total_file_size : false, false);
    $error |= $arr_file['error'];
    $quota_exceeded |= $arr_file['quota_exceeded'];
    $file_othermaterial = $arr_file['filename'];
    $total_file_size = $total_file_size + $arr_file['new_size'];
    // course demo-----------------------------------------------------------------------------------
    $arr_file = manageCourseFile('course_demo', '', $path, $quota != 0 ? $quota - $total_file_size : false, false);
    $error |= $arr_file['error'];
    $quota_exceeded |= $arr_file['quota_exceeded'];
    $file_demo = $arr_file['filename'];
    $total_file_size = $total_file_size + $arr_file['new_size'];
    // course sponsor---------------------------------------------------------------------------------
    $arr_file = manageCourseFile('course_sponsor_logo', '', $path, $quota != 0 ? $quota - $total_file_size : false, false, true);
    $error |= $arr_file['error'];
    $quota_exceeded |= $arr_file['quota_exceeded'];
    $file_sponsor = $arr_file['filename'];
    $total_file_size = $total_file_size + $arr_file['new_size'];
    // course logo-----------------------------------------------------------------------------------
    $arr_file = manageCourseFile('course_logo', '', $path, $quota != 0 ? $quota - $total_file_size : false, false, true);
    $error |= $arr_file['error'];
    $quota_exceeded |= $arr_file['quota_exceeded'];
    $file_logo = $arr_file['filename'];
    $total_file_size = $total_file_size + $arr_file['new_size'];
    // ----------------------------------------------------------------------------------------------
    sl_close_fileoperations();
    if ($_POST["can_subscribe"] == "2") {
        $sub_start_date = Format::dateDb($_POST["sub_start_date"], "date");
        $sub_end_date = Format::dateDb($_POST["sub_end_date"], "date");
    }
    $date_begin = Format::dateDb($_POST['course_date_begin'], "date");
    $date_end = Format::dateDb($_POST['course_date_end'], "date");
    // insert the course in database -----------------------------------------------------------
    $hour_begin = '-1';
    $hour_end = '-1';
    if ($_POST['hour_begin']['hour'] != '-1') {
        $hour_begin = strlen($_POST['hour_begin']['hour']) == 1 ? '0' . $_POST['hour_begin']['hour'] : $_POST['hour_begin']['hour'];
        if ($_POST['hour_begin']['quarter'] == '-1') {
            $hour_begin .= ':00';
        } else {
            $hour_begin .= ':' . $_POST['hour_begin']['quarter'];
        }
    }
    if ($_POST['hour_end']['hour'] != '-1') {
        $hour_end = strlen($_POST['hour_end']['hour']) == 1 ? '0' . $_POST['hour_end']['hour'] : $_POST['hour_end']['hour'];
        if ($_POST['hour_end']['quarter'] == '-1') {
            $hour_end .= ':00';
        } else {
            $hour_end .= ':' . $_POST['hour_end']['quarter'];
        }
    }
    $query_course = "\r\n\tINSERT INTO " . $GLOBALS['prefix_lms'] . "_course\r\n\tSET idCategory \t\t\t= '" . (isset($_POST['idCategory']) ? $_POST['idCategory'] : 0) . "',\r\n\t\tcode \t\t\t\t= '" . $_POST['course_code'] . "',\r\n\t\tname \t\t\t\t= '" . $_POST['course_name'] . "',\r\n\t\tdescription \t\t= '" . $_POST['course_descr'] . "',\r\n\t\tlang_code \t\t\t= '" . $array_lang[$_POST['course_lang']] . "',\r\n\t\tstatus \t\t\t\t= '" . (int) $_POST['course_status'] . "',\r\n\t\tlevel_show_user \t= '" . $show_level . "',\r\n\t\tsubscribe_method \t= '" . (int) $_POST['course_subs'] . "',\r\n\r\n\t\tcreate_date\t\t\t= '" . date("Y-m-d H:i:s") . "',\r\n\r\n\t\tlinkSponsor \t\t= '" . $_POST['course_sponsor_link'] . "',\r\n\t\timgSponsor \t\t\t= '" . $file_sponsor . "',\r\n\t\timg_course \t\t\t= '" . $file_logo . "',\r\n\t\timg_material \t\t= '" . $file_material . "',\r\n\t\timg_othermaterial \t= '" . $file_othermaterial . "',\r\n\t\tcourse_demo \t\t= '" . $file_demo . "',\r\n\r\n\t\tmediumTime \t\t\t= '" . $_POST['course_medium_time'] . "',\r\n\t\tpermCloseLO \t\t= '" . $_POST['course_em'] . "',\r\n\t\tuserStatusOp \t\t= '" . $user_status . "',\r\n\t\tdifficult \t\t\t= '" . $_POST['course_difficult'] . "',\r\n\r\n\t\tshow_progress \t\t= '" . (isset($_POST['course_progress']) ? 1 : 0) . "',\r\n\t\tshow_time \t\t\t= '" . (isset($_POST['course_time']) ? 1 : 0) . "',\r\n\r\n\t\tshow_who_online\t\t= '" . $_POST['show_who_online'] . "',\r\n\r\n\t\tshow_extra_info \t= '" . (isset($_POST['course_advanced']) ? 1 : 0) . "',\r\n\t\tshow_rules \t\t\t= '" . (int) $_POST['course_show_rules'] . "',\r\n\r\n\t\tdirect_play \t\t= '" . (isset($_POST['direct_play']) ? 1 : 0) . "',\r\n\r\n\t\tdate_begin \t\t\t= '" . $date_begin . "',\r\n\t\tdate_end \t\t\t= '" . $date_end . "',\r\n\t\thour_begin \t\t\t= '" . $hour_begin . "',\r\n\t\thour_end \t\t\t= '" . $hour_end . "',\r\n\r\n\t\tvalid_time \t\t\t= '" . (int) $_POST['course_day_of'] . "',\r\n\r\n\t\tmin_num_subscribe \t= '" . (int) $_POST['min_num_subscribe'] . "',\r\n\t\tmax_num_subscribe \t= '" . (int) $_POST['max_num_subscribe'] . "',\r\n\t\tselling \t\t\t= '" . (isset($_POST['course_sell']) ? '1' : '0') . "',\r\n\t\tprize \t\t\t\t= '" . $_POST['course_prize'] . "',\r\n\r\n\t\tcourse_type \t\t= '" . $_POST['course_type'] . "',\r\n\r\n\t\tcourse_edition \t\t= '" . (isset($_POST['course_edition']) ? 1 : 0) . "',\r\n\r\n\t\tcourse_quota \t\t= '" . $_POST['course_quota'] . "',\r\n\t\tused_space\t\t\t= '" . $total_file_size . "',\r\n\t\tallow_overbooking \t= '" . (isset($_POST["allow_overbooking"]) ? 1 : 0) . "',\r\n\t\tcan_subscribe \t\t= '" . (int) $_POST["can_subscribe"] . "',\r\n\t\tsub_start_date \t\t= " . ($_POST["can_subscribe"] == '2' ? "'" . $sub_start_date . "'" : 'NULL') . ",\r\n\t\tsub_end_date \t\t= " . ($_POST["can_subscribe"] == '2' ? "'" . $sub_end_date . "'" : 'NULL') . ",\r\n\r\n\t\tadvance \t\t\t= '" . $_POST['advance'] . "',\r\n\t\tshow_result \t\t= '" . (isset($_POST["show_result"]) ? 1 : 0) . "',\r\n\r\n\t\tuse_logo_in_courselist = '" . (isset($_POST['use_logo_in_courselist']) ? '1' : '0') . "'";
    if (isset($_POST['random_course_autoregistration_code'])) {
        $control = 1;
        $str = '';
        while ($control) {
            for ($i = 0; $i < 10; $i++) {
                $seed = mt_rand(0, 10);
                if ($seed > 5) {
                    $str .= mt_rand(0, 9);
                } else {
                    $str .= chr(mt_rand(65, 90));
                }
            }
            $control_query = "SELECT COUNT(*)" . " " . $GLOBALS['prefix_lms'] . "_course" . " WHERE autoregistration_code = '" . $str . "'";
            $control_result = sql_query($control_query);
            list($result) = sql_fetch_row($control_result);
            $control = $result;
        }
        $query_course .= ", autoregistration_code = '" . $str . "'";
    } else {
        $query_course .= ", autoregistration_code = '" . $_POST['course_autoregistration_code'] . "'";
    }
    if (!sql_query($query_course)) {
        // course save failed, delete uploaded file
        if ($file_sponsor != '') {
            sl_unlink($path . $file_sponsor);
        }
        if ($file_logo != '') {
            sl_unlink($path . $file_logo);
        }
        if ($file_material != '') {
            sl_unlink($path . $file_material);
        }
        if ($file_othermaterial != '') {
            sl_unlink($path . $file_othermaterial);
        }
        if ($file_demo != '') {
            sl_unlink($path . $file_demo);
        }
        Util::jump_to($url . 'err_course');
    }
    // recover the id of the course inserted --------------------------------------------
    list($id_course) = sql_fetch_row(sql_query("SELECT LAST_INSERT_ID()"));
    require_once _lms_ . '/admin/models/LabelAlms.php';
    $label_model = new LabelAlms();
    $label = Get::req('label', DOTY_INT, 0);
    $label_model->associateLabelToCourse($label, $id_course);
    // add this corse to the pool of course visible by the user that have create it -----
    if (Docebo::user()->getUserLevelId() != ADMIN_GROUP_GODADMIN) {
        require_once _base_ . '/lib/lib.preference.php';
        $adminManager = new AdminPreference();
        $adminManager->addAdminCourse($id_course, Docebo::user()->getIdSt());
    }
    //if the scs exist create a room ----------------------------------------------------
    if ($GLOBALS['where_scs'] !== false) {
        require_once $GLOBALS['where_scs'] . '/lib/lib.room.php';
        $rules = array('room_name' => $_POST['course_name'], 'room_type' => 'course', 'id_source' => $id_course);
        //$admin_rules = getAdminRules();
        //$rules = array_merge($rules, $admin_rules);
        $re = insertRoom($rules);
    }
    $course_idst =& DoceboCourse::createCourseLevel($id_course);
    // create the course menu -----------------------------------------------------------
    if (!cerateCourseMenuFromCustom($id_custom, $id_course, $course_idst)) {
        Util::jump_to($url . 'err_coursemenu');
    }
    /*
    // send alert -------------------------------------------------------------------------------
    require_once($GLOBALS['where_framework'] . '/lib/lib.eventmanager.php');
    
    $msg_composer = new EventMessageComposer();
    
    $msg_composer->setSubjectLangText('email', '_ALERT_SUBJECT', false);
    $msg_composer->setBodyLangText('email', '_ALERT_TEXT', array(	'[url]' => Get::sett('url'),
    																'[course_code]' => $_POST['course_code'],
    																'[course]' => $_POST['course_name'] ) );
    
    $msg_composer->setBodyLangText('sms', '_ALERT_TEXT_SMS', array(	'[url]' => Get::sett('url'),
    																'[course_code]' => $_POST['course_code'],
    																'[course]' => $_POST['course_name'] ) );
    
    require_once($GLOBALS['where_lms'] . '/lib/lib.course.php');
    $course_man = new Man_Course();
    $recipients = $course_man->getIdUserOfLevel($id_course);
    createNewAlert(	'CoursePropModified',
    				'course',
    				'add',
    				'1',
    				'Inserted course '.$_POST['course_name'],
    				$recipients,
    				$msg_composer );
    */
    Util::jump_to($url . ($error ? 'err_course' : 'ok_course') . ($quota_exceeded ? '&limit_reach=1' : ''));
}