Example #1
0
function loadImportCourseUser2()
{
    require_once _base_ . '/lib/lib.upload.php';
    require_once $GLOBALS['where_framework'] . '/lib/lib.import.php';
    require_once $GLOBALS['where_lms'] . '/lib/lib.course.php';
    require_once _base_ . '/lib/lib.table.php';
    $lang =& DoceboLanguage::CreateInstance('subscribe', 'lms');
    $back_url = 'index.php?modname=course&op=course_list';
    $acl_man =& Docebo::user()->getAclManager();
    $out =& $GLOBALS['page'];
    $out->setWorkingZone('content');
    $id_course = importVar('id_course', true, 0);
    $id_course_edition = importVar('edition', true, 0);
    $level_idst =& getCourseLevel($id_course);
    if (count($level_idst) == 0) {
        $level_idst =& DoceboCourse::createCourseLevel($id_course);
    }
    // ----------- file upload -----------------------------------------
    if ($_FILES['file_import']['name'] == '') {
        $_SESSION['last_error'] = Lang::t('_FILEUNSPECIFIED');
        Util::jump_to($back_url . '&import_result=-1');
    } else {
        $path = '/appCore/';
        $savefile = mt_rand(0, 100) . '_' . time() . '_' . $_FILES['file_import']['name'];
        if (!file_exists($GLOBALS['where_files_relative'] . $path . $savefile)) {
            sl_open_fileoperations();
            if (!sl_upload($_FILES['file_import']['tmp_name'], $path . $savefile)) {
                sl_close_fileoperations();
                $_SESSION['last_error'] = Lang::t('_ERROR_UPLOAD');
                Util::jump_to($back_url . '&import_result=-1');
            }
            sl_close_fileoperations();
        } else {
            $_SESSION['last_error'] = Lang::t('_ERROR_UPLOAD');
            Util::jump_to($back_url . '&create_result=-1');
        }
    }
    $out->add(getTitleArea($lang->def('_SUBSCRIBE')) . '<div class="std_block">');
    $separator = importVar('import_separator', false, ',');
    if ($separator == '') {
        $separator = ',';
    }
    $first_row_header = importVar('import_first_row_header', false, false);
    $import_charset = importVar('import_charset', false, 'UTF-8');
    if (trim($import_charset) === '') {
        $import_charset = 'UTF-8';
    }
    $src = new DeceboImport_SourceCSV(array('filename' => $GLOBALS['where_files_relative'] . $path . $savefile, 'separator' => $separator, 'first_row_header' => $first_row_header, 'import_charset' => $import_charset));
    $src->connect();
    $user_added = 0;
    $user_error = 0;
    $user_not_needed = 0;
    $id_user_added = array();
    $counter = 0;
    if (is_array($row = $src->get_first_row()) && !empty($row)) {
        $user_info = $acl_man->getUser(false, $row[0]);
        if ($user_info) {
            $id_user = $user_info[ACL_INFO_IDST];
            // Add in group for permission
            $acl_man->addToGroup($level_idst['3'], $id_user);
            // Add to edition group
            if ($id_course_edition > 0) {
                $group = '/lms/course_edition/' . $id_course_edition . '/subscribed';
                $group_idst = $acl_man->getGroupST($group);
                if ($group_idst === FALSE) {
                    $group_idst = $acl_man->registerGroup($group, 'all the user of a course edition', true, "course");
                }
                $acl_man->addToGroup($group_idst, $id_user);
            }
            // Add in table
            $re = sql_query("\r\n\t\t\tINSERT INTO " . $GLOBALS['prefix_lms'] . "_courseuser\r\n\t\t\t( idUser, idCourse, edition_id, level, waiting, subscribed_by, date_inscr )\r\n\t\t\tVALUES\r\n\t\t\t( '" . $id_user . "', '" . $id_course . "', '" . $id_course_edition . "', '3', '0', '" . getLogUserId() . "', '" . date("Y-m-d H:i:s") . "' )\t");
            if ($re) {
                addUserToTimeTable($id_user, $id_course, $id_course_edition);
                $user_added++;
                $id_user_added[$counter]['id_user'] = $id_user;
                $id_user_added[$counter]['status'] = '_CORRECT';
            } else {
                $query = "SELECT COUNT(*)" . " FROM " . $GLOBALS['prefix_lms'] . "_courseuser" . " WHERE idUser = '******'" . " AND idCourse = '" . $id_course . "'" . " AND edition_id = '" . $id_course_edition . "'";
                list($control) = sql_fetch_row(sql_query($query));
                if ($control) {
                    $user_not_needed++;
                    $id_user_added[$counter]['id_user'] = $id_user;
                    $id_user_added[$counter]['status'] = '_NOT_NEEDED';
                } else {
                    $user_error++;
                    $id_user_added[$counter]['id_user'] = $id_user;
                    $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                }
            }
            $counter++;
        } else {
            $user_error++;
            $id_user_added[$counter]['id_user'] = $id_user;
            $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
        }
    }
    while (is_array($row = $src->get_next_row()) && !empty($row)) {
        $user_info = $acl_man->getUser(false, $row[0]);
        if ($user_info) {
            $id_user = $user_info[ACL_INFO_IDST];
            // Add in group for permission
            $acl_man->addToGroup($level_idst['3'], $id_user);
            // Add to edition group
            if ($id_course_edition > 0) {
                $group = '/lms/course_edition/' . $id_course_edition . '/subscribed';
                $group_idst = $acl_man->getGroupST($group);
                if ($group_idst === FALSE) {
                    $group_idst = $acl_man->registerGroup($group, 'all the user of a course edition', true, "course");
                }
                $acl_man->addToGroup($group_idst, $id_user);
            }
            // Add in table
            $re = sql_query("\r\n\t\t\tINSERT INTO " . $GLOBALS['prefix_lms'] . "_courseuser\r\n\t\t\t( idUser, idCourse, edition_id, level, waiting, subscribed_by, date_inscr )\r\n\t\t\tVALUES\r\n\t\t\t( '" . $id_user . "', '" . $id_course . "', '" . $id_course_edition . "', '3', '0', '" . getLogUserId() . "', '" . date("Y-m-d H:i:s") . "' )\t");
            if ($re) {
                addUserToTimeTable($id_user, $id_course, $id_course_edition);
                $user_added++;
                $id_user_added[$counter]['id_user'] = $id_user;
                $id_user_added[$counter]['status'] = '_CORRECT';
            } else {
                $query = "SELECT COUNT(*)" . " FROM " . $GLOBALS['prefix_lms'] . "_courseuser" . " WHERE idUser = '******'" . " AND idCourse = '" . $id_course . "'" . " AND edition_id = '" . $id_course_edition . "'";
                list($control) = sql_fetch_row(sql_query($query));
                if ($control) {
                    $user_not_needed++;
                    $id_user_added[$counter]['id_user'] = $id_user;
                    $id_user_added[$counter]['status'] = '_NOT_NEEDED';
                } else {
                    $user_error++;
                    $id_user_added[$counter]['id_user'] = $id_user;
                    $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                }
            }
            $counter++;
        } else {
            $user_error++;
            $id_user_added[$counter]['id_user'] = $id_user;
            $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
        }
    }
    $src->close();
    unset($row);
    $type_h = array('align_center', 'align_center', 'align_center', 'align_center');
    $cont_h = array($lang->def('_USERNAME'), $lang->def('_LASTNAME'), $lang->def('_FIRSTNAME'), $lang->def('_INSER_STATUS'));
    $tb = new Table(false, $lang->def('_USER_SUBSCRIBED'), $lang->def('_USER_SUBSCRIBED'));
    $tb->addHead($cont_h, $type_h);
    while (list(, $id_user_added_detail) = each($id_user_added)) {
        $cont = array();
        $user_info = $acl_man->getUser($id_user_added_detail['id_user'], false);
        $cont[] = $acl_man->relativeId($user_info[ACL_INFO_USERID]);
        $cont[] = $user_info[ACL_INFO_FIRSTNAME];
        $cont[] = $user_info[ACL_INFO_LASTNAME];
        $cont[] = $lang->def($id_user_added_detail['status']);
        $tb->addBody($cont);
    }
    sl_open_fileoperations();
    sl_unlink($path . $savefile);
    sl_close_fileoperations();
    $out->add(getBackUi($back_url, $lang->def('_BACK')) . '<b>' . $lang->def('_INSERT_CORRECT') . ' : ' . '</b>' . $user_added . ' ' . '<b>' . $lang->def('_OPERATION_FAILURE') . ' : ' . '</b>' . $user_error . ' ' . '<b>' . $lang->def('_INSERT_NOT_NEEDED') . ' : ' . '</b>' . $user_not_needed . '<br/>' . '<br/>' . $tb->getTable() . '<br/>' . getBackUi($back_url, $lang->def('_BACK')) . '</div>');
}
Example #2
0
function importCode_step2()
{
    require_once _base_ . '/lib/lib.upload.php';
    require_once $GLOBALS['where_framework'] . '/lib/lib.import.php';
    require_once _base_ . '/lib/lib.table.php';
    if ($_FILES['file_import']['name'] == '') {
        Util::jump_to('index.php?modname=code&amp;op=list&result=err');
    } else {
        $path = '/appCore/';
        $savefile = mt_rand(0, 100) . '_' . time() . '_' . $_FILES['file_import']['name'];
        if (!file_exists($GLOBALS['where_files_relative'] . $path . $savefile)) {
            sl_open_fileoperations();
            if (!sl_upload($_FILES['file_import']['tmp_name'], $path . $savefile)) {
                sl_close_fileoperations();
                Util::jump_to('index.php?modname=code&amp;op=list&result=err');
            }
            sl_close_fileoperations();
        } else {
            Util::jump_to('index.php?modname=directory&amp;op=listgroup&import_result=-1');
        }
    }
    $lang =& DoceboLanguage::createInstance('code');
    $code_manager = new CodeManager();
    $id_code_group = Get::req('id_code_group', DOTY_INT, '0');
    cout(getTitleArea($lang->def('_CODE')) . '<div class="std_block">');
    $separator = Get::req('import_separator', DOTY_MIXED, ',');
    $first_row_header = isset($_POST['import_first_row_header']) ? $_POST['import_first_row_header'] == 'true' : FALSE;
    $import_charset = Get::req('import_charset', DOTY_MIXED, 'UTF-8');
    if (trim($import_charset) === '') {
        $import_charset = 'UTF-8';
    }
    $src = new DeceboImport_SourceCSV(array('filename' => $GLOBALS['where_files_relative'] . $path . $savefile, 'separator' => $separator, 'first_row_header' => $first_row_header, 'import_charset' => $import_charset));
    $src->connect();
    $code_added = array();
    $code_error = array();
    $code_present = array();
    if (is_array($row = $src->get_first_row()) && !empty($row)) {
        $code = addslashes($row[0]);
        $result = $code_manager->addCode($code, $id_code_group);
        if ($result === 'dup') {
            $code_present[] = $code;
        } elseif ($result) {
            $code_added[] = $code;
        } else {
            $code_error[] = $code;
        }
    }
    while (is_array($row = $src->get_next_row()) && !empty($row)) {
        $code = addslashes($row[0]);
        $result = $code_manager->addCode($code, $id_code_group);
        if ($result === 'dup') {
            $code_present[] = $code;
        } elseif ($result) {
            $code_added[] = $code;
        } else {
            $code_error[] = $code;
        }
    }
    $src->close();
    unset($row);
    sl_open_fileoperations();
    sl_unlink($path . $savefile);
    sl_close_fileoperations();
    cout(getBackUi('index.php?modname=code&amp;op=list', $lang->def('_BACK')) . '<br/>' . $lang->def('_CODE_ADDED') . ' : ' . count($code_added) . '<br/>' . $lang->def('_CODE_PRESENT') . ' : ' . count($code_present) . '<br/>' . $lang->def('_CODE_ERROR') . ' : ' . count($code_error) . '<br/>');
    if (count($code_present)) {
        $tb = new Table(false, $lang->def('_CODE_PRESENT'), $lang->def('_CODE_PRESENT'));
        $type_h = array('align_center');
        $cont_h = array($lang->def('_CODE'));
        $tb->setColsStyle($type_h);
        $tb->addHead($cont_h);
        foreach ($code_present as $code) {
            $cont = array();
            $cont[] = stripslashes($code);
            $tb->addBody($cont);
        }
        cout($tb->getTable());
    }
    if (count($code_error)) {
        $tb = new Table(false, $lang->def('_CODE_ERROR'), $lang->def('_CODE_ERROR'));
        $type_h = array('align_center');
        $cont_h = array($lang->def('_CODE'));
        $tb->setColsStyle($type_h);
        $tb->addHead($cont_h);
        foreach ($code_error as $code) {
            $cont = array();
            $cont[] = stripslashes($code);
            $tb->addBody($cont);
        }
        cout($tb->getTable());
    }
    cout(getBackUi('index.php?modname=code&amp;op=list', $lang->def('_BACK')) . '</div>');
}
 public function import_csv()
 {
     if (!$this->permissions['subscribe_course']) {
         $this->render('invalid', array('message' => $this->_getErrorMessage('no permission'), 'back_url' => 'index.php?r=' . $this->link_course . '/show'));
         return;
     }
     require_once _base_ . '/lib/lib.form.php';
     //Step info
     $step = Get::req('step', DOTY_INT, 1);
     if (isset($_POST['next'])) {
         $step++;
     }
     if (isset($_POST['back'])) {
         $step--;
     }
     if (isset($_POST['undo'])) {
         $step = 0;
     }
     switch ($step) {
         case '0':
             Util::jump_to('index.php?r=' . $this->link_course . '/show');
             break;
         case '1':
             $course_info = $this->model->getCourseInfoForSubscription();
             $course_name = ($course_info['code'] !== '' ? '[' . $course_info['code'] . '] ' : '') . $course_info['name'];
             $params = array('table' => $tb, 'id_course' => $this->id_course, 'id_date' => $this->id_date, 'id_edition' => $this->id_edition, 'course_name' => $course_name, 'model' => $this->model);
             $this->render('import_csv_step_1', $params);
             break;
         case '2':
             require_once _lms_ . '/lib/lib.course.php';
             require_once _base_ . '/lib/lib.upload.php';
             require_once _adm_ . '/lib/lib.import.php';
             $separator = Get::req('import_separator', DOTY_MIXED, ',');
             $first_row_header = Get::req('import_first_row_header', DOTY_BOOL, false);
             $import_charset = Get::req('import_charset', DOTY_MIXED, 'UTF-8');
             $docebo_course = new DoceboCourse($this->id_course);
             $level_idst =& $docebo_course->getCourseLevel($this->id_course);
             if (count($level_idst) == 0 || $level_idst[1] == '') {
                 $level_idst =& $docebo_course->createCourseLevel($this->id_course);
             }
             $back_url = 'index.php?r=' . $this->link . '/show&id_course=' . $this->id_course . '&id_edition=' . $this->id_edition . '&id_date=' . $this->id_date;
             // ----------- file upload -----------------------------------------
             if ($_FILES['file_import']['name'] == '') {
                 $_SESSION['last_error'] = Lang::t('_FILEUNSPECIFIED');
                 jumpTo($back_url . '&err=_file_unspecified');
             } else {
                 $path = '/appCore/';
                 $savefile = mt_rand(0, 100) . '_' . time() . '_' . $_FILES['file_import']['name'];
                 if (!file_exists($GLOBALS['where_files_relative'] . $path . $savefile)) {
                     sl_open_fileoperations();
                     if (!sl_upload($_FILES['file_import']['tmp_name'], $path . $savefile)) {
                         sl_close_fileoperations();
                         $_SESSION['last_error'] = Lang::t('_ERROR_UPLOAD', 'subscribe');
                         jumpTo($back_url . '&err=_err_upload');
                     }
                     sl_close_fileoperations();
                 } else {
                     $_SESSION['last_error'] = Lang::t('_ERROR_UPLOAD', 'subscribe');
                     jumpTo($back_url . '&err=_err_upload');
                 }
             }
             $src = new DeceboImport_SourceCSV(array('filename' => $GLOBALS['where_files_relative'] . $path . $savefile, 'separator' => $separator, 'first_row_header' => $first_row_header, 'import_charset' => $import_charset));
             $src->connect();
             $user_added = 0;
             $user_error = 0;
             $user_not_needed = 0;
             $id_user_added = array();
             $counter = 0;
             $course_info = $this->model->getCourseInfoForSubscription();
             $can_subscribe = true;
             $max_num_subscribe = $course_info['max_num_subscribe'];
             $subscribe_method = $course_info['subscribe_method'];
             if (Docebo::user()->getUserLevelId() != ADMIN_GROUP_GODADMIN) {
                 $limited_subscribe = Docebo::user()->preference->getAdminPreference('admin_rules.limit_course_subscribe');
                 $max_subscribe = Docebo::user()->preference->getAdminPreference('admin_rules.max_course_subscribe');
                 $direct_subscribe = Docebo::user()->preference->getAdminPreference('admin_rules.direct_course_subscribe');
                 if ($limited_subscribe == 'on') {
                     $limited_subscribe = true;
                 } else {
                     $limited_subscribe = false;
                 }
                 if ($direct_subscribe == 'on') {
                     $direct_subscribe = true;
                 } else {
                     $direct_subscribe = false;
                 }
             } else {
                 $limited_subscribe = false;
                 $max_subscribe = 0;
                 $direct_subscribe = true;
             }
             if (is_array($row = $src->get_first_row()) && !empty($row)) {
                 $user_info = $this->acl_man->getUser(false, $row[0]);
                 if ($user_info) {
                     $id_user = $user_info[ACL_INFO_IDST];
                     if ($can_subscribe) {
                         require_once _lms_ . '/lib/lib.course.php';
                         $docebo_course = new DoceboCourse($this->id_course);
                         $level_idst =& $docebo_course->getCourseLevel($this->id_course);
                         if (count($level_idst) == 0 || $level_idst[1] == '') {
                             $level_idst =& $docebo_course->createCourseLevel($this->id_course);
                         }
                         if (!$direct_subscribe) {
                             $waiting = 1;
                         } else {
                             $waiting = 0;
                         }
                         if (!$limited_subscribe || $max_subscribe) {
                             //$this->acl_man->addToGroup($level_idst['3'], $id_user);
                             $this->_addToCourseGroup($level_idst['3'], $id_user);
                             if ($this->model->subscribeUser($id_user, '3', $waiting)) {
                                 $user_added++;
                                 $id_user_added[$counter]['id_user'] = $row[0];
                                 $id_user_added[$counter]['status'] = '_CORRECT';
                                 $user_subscribed[] = $id_user;
                                 $max_subscribe--;
                             } else {
                                 $query = "SELECT COUNT(*)" . " FROM " . $GLOBALS['prefix_lms'] . "_courseuser" . " WHERE idUser = '******'" . " AND idCourse = '" . $this->id_course . "'";
                                 list($control) = sql_fetch_row(sql_query($query));
                                 if ($control) {
                                     $user_not_needed++;
                                     $id_user_added[$counter]['id_user'] = $row[0];
                                     $id_user_added[$counter]['status'] = '_NOT_NEEDED';
                                 } else {
                                     $user_error++;
                                     $id_user_added[$counter]['id_user'] = $row[0];
                                     $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                                     $this->acl_man->removeFromGroup($level_idst['3'], $id_user);
                                 }
                             }
                         }
                     }
                 } else {
                     $user_error++;
                     $id_user_added[$counter]['id_user'] = $row[0];
                     $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                 }
                 $counter++;
             }
             while (is_array($row = $src->get_next_row()) && !empty($row)) {
                 $user_info = $this->acl_man->getUser(false, $row[0]);
                 if ($user_info) {
                     $id_user = $user_info[ACL_INFO_IDST];
                     if ($can_subscribe) {
                         require_once _lms_ . '/lib/lib.course.php';
                         $docebo_course = new DoceboCourse($this->id_course);
                         $level_idst =& $docebo_course->getCourseLevel($this->id_course);
                         if (count($level_idst) == 0 || $level_idst[1] == '') {
                             $level_idst =& $docebo_course->createCourseLevel($this->id_course);
                         }
                         if (!$direct_subscribe) {
                             $waiting = 1;
                         } else {
                             $waiting = 0;
                         }
                         if (!$limited_subscribe || $max_subscribe) {
                             //$this->acl_man->addToGroup($level_idst['3'], $id_user);
                             $this->_addToCourseGroup($level_idst['3'], $id_user);
                             if ($this->model->subscribeUser($id_user, '3', $waiting)) {
                                 $user_added++;
                                 $id_user_added[$counter]['id_user'] = $row[0];
                                 $id_user_added[$counter]['status'] = '_CORRECT';
                                 $user_subscribed[] = $id_user;
                                 $max_subscribe--;
                             } else {
                                 $query = "SELECT COUNT(*)" . " FROM " . $GLOBALS['prefix_lms'] . "_courseuser" . " WHERE idUser = '******'" . " AND idCourse = '" . $this->id_course . "'";
                                 list($control) = sql_fetch_row(sql_query($query));
                                 if ($control) {
                                     $user_not_needed++;
                                     $id_user_added[$counter]['id_user'] = $row[0];
                                     $id_user_added[$counter]['status'] = '_NOT_NEEDED';
                                 } else {
                                     $user_error++;
                                     $id_user_added[$counter]['id_user'] = $row[0];
                                     $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                                     $this->acl_man->removeFromGroup($level_idst['3'], $id_user);
                                 }
                             }
                         }
                     }
                 } else {
                     $user_error++;
                     $id_user_added[$counter]['id_user'] = $row[0];
                     $id_user_added[$counter]['status'] = '_OPERATION_FAILURE';
                 }
                 $counter++;
             }
             $src->close();
             unset($row);
             require_once _base_ . '/lib/lib.table.php';
             $type_h = array('align_center', 'align_center', 'align_center', 'align_center');
             $cont_h = array(Lang::t('_USERNAME', 'subscribe'), Lang::t('_LASTNAME', 'subscribe'), Lang::t('_FIRSTNAME', 'subscribe'), Lang::t('_INSER_STATUS', 'subscribe'));
             $tb = new Table(false, Lang::t('_USER_SUBSCRIBED', 'subscribe'), Lang::t('_USER_SUBSCRIBED', 'subscribe'));
             $tb->addHead($cont_h, $type_h);
             while (list(, $id_user_added_detail) = each($id_user_added)) {
                 $cont = array();
                 $user_info = $this->acl_man->getUser(false, $id_user_added_detail['id_user']);
                 $cont[] = $this->acl_man->relativeId($user_info[ACL_INFO_USERID]);
                 $cont[] = $user_info[ACL_INFO_FIRSTNAME];
                 $cont[] = $user_info[ACL_INFO_LASTNAME];
                 $cont[] = Lang::t($id_user_added_detail['status'], 'subscribe');
                 $tb->addBody($cont);
             }
             sl_open_fileoperations();
             sl_unlink($GLOBALS['where_files_relative'] . $path . $savefile);
             sl_close_fileoperations();
             $course_info = $this->model->getCourseInfoForSubscription();
             $course_name = ($course_info['code'] !== '' ? '[' . $course_info['code'] . '] ' : '') . $course_info['name'];
             $params = array('table' => $tb, 'id_course' => $this->id_course, 'id_date' => $this->id_date, 'id_edition' => $this->id_edition, 'course_name' => $course_name, 'back_link' => getBackUi('index.php?r=' . $this->link . '/show&id_course=' . $this->id_course . '&id_edition=' . $this->id_edition . '&id_date=' . $this->id_date, Lang::t('_BACK', 'subscribe')));
             $this->render('import_csv_step_2', $params);
             break;
     }
 }