Ejemplo n.º 1
0
 /**
  * Import users in course.
  *
  * @author Dimitri Rambout <*****@*****.**>
  * @param $courseId id of the course
  *
  * @return boolean
  */
 public function importUsersInCourse($userList, $courseId, $canCreateUser = true, $enrollUserInCourse = true, $class_id = 0, $sendEmail = 0)
 {
     if (empty($this->data)) {
         return false;
     }
     if (!(isset($userList) && count($userList))) {
         return false;
     }
     $logs = array();
     $tbl_mdb_names = claro_sql_get_main_tbl();
     $tbl_user = $tbl_mdb_names['user'];
     $tbl_course_user = $tbl_mdb_names['rel_course_user'];
     $tbl_cdb_names = claro_sql_get_course_tbl();
     $tbl_group_rel_team_user = $tbl_cdb_names['group_rel_team_user'];
     $groupsImported = array();
     $userInfo = array();
     foreach ($userList as $user_id) {
         if (!isset($this->data[$user_id])) {
             $logs['errors'][] = get_lang('Unable to find the user in the csv');
         } else {
             $userInfo['username'] = $this->data[$user_id]['username'];
             $userInfo['firstname'] = $this->data[$user_id]['firstname'];
             $userInfo['lastname'] = $this->data[$user_id]['lastname'];
             $userInfo['email'] = isset($this->data[$user_id]['email']) && !empty($this->data[$user_id]['email']) ? $this->data[$user_id]['email'] : '';
             $userInfo['password'] = isset($this->data[$user_id]['password']) && !empty($this->data[$user_id]['password']) ? $this->data[$user_id]['password'] : mk_password(8);
             $userInfo['officialCode'] = isset($this->data[$user_id]['officialCode']) ? $this->data[$user_id]['officialCode'] : '';
             if (isset($this->data[$user_id]['groupName'])) {
                 $groupNames = $this->data[$user_id]['groupName'];
             } else {
                 $groupNames = null;
             }
             //check user existe if not create is asked
             $resultSearch = user_search(array('username' => $userInfo['username']), null, true, true);
             if (empty($resultSearch)) {
                 if (!$canCreateUser) {
                     $userId = 0;
                     $logs['errors'][] = get_lang('Unable to create user %username, option is disabled in configuration', array('%username' => $userInfo['username']));
                 } else {
                     $userId = user_create($userInfo);
                     if ($userId != 0) {
                         $logs['success'][] = get_lang('User profile %username created successfully', array('%username' => $userInfo['username']));
                         if ($sendEmail) {
                             user_send_registration_mail($userId, $userInfo);
                         }
                     } else {
                         $logs['errors'][] = get_lang('Unable to create user %username', array('%username' => $userInfo['username']));
                     }
                 }
             } else {
                 $userId = $resultSearch[0]['uid'];
                 $logs['errors'][] = get_lang('User %username not created because it already exists in the database', array('%username' => $userInfo['username']));
             }
             if ($userId == 0) {
                 $logs['errors'][] = get_lang('Unable to add user %username in this course', array('%username' => $userInfo['username']));
             } else {
                 if (!$enrollUserInCourse) {
                     $logs['errors'][] = get_lang('Unable to add user %username in this course, option is disabled in configuration', array('%username' => $userInfo['username']));
                 } else {
                     if (!user_add_to_course($userId, $courseId, false, false, null)) {
                         $logs['errors'][] = get_lang('Unable to add user %username in this course', array('%username' => $userInfo['username']));
                     } else {
                         $logs['success'][] = get_lang('User %username added in course %courseId', array('%username' => $userInfo['username'], '%courseId' => $courseId));
                         //join class if needed
                         if ($class_id) {
                             if (!($return = user_add_to_class($userId, $class_id))) {
                                 $logs['errors'][] = get_lang('Unable to add %username in the selected class', array('%username' => $userInfo['username']));
                             } else {
                                 $logs['success'][] = get_lang('User %username added in the selected class', array('%username' => $userInfo['username']));
                             }
                         }
                         //join group
                         $groups = explode(',', $groupNames);
                         if (is_array($groups)) {
                             foreach ($groups as $group) {
                                 $group = trim($group);
                                 if (!empty($group)) {
                                     $groupsImported[$group][] = $userId;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     foreach ($groupsImported as $group => $users) {
         $GLOBALS['currentCourseRepository'] = claro_get_course_path($courseId);
         $groupId = create_group($group, null);
         if ($groupId == 0) {
             $logs['errors'][] = get_lang('Unable to create group %groupname', array('%groupname' => $group));
         } else {
             foreach ($users as $userId) {
                 $sql = "INSERT INTO `" . $tbl_group_rel_team_user . "`\n                            SET user = "******",\n                                team = " . (int) $groupId;
                 if (!claro_sql_query($sql)) {
                     $logs['errors'][] = get_lang('Unable to add user in group %groupname', array('%groupname' => $group));
                 }
             }
         }
     }
     return $logs;
 }
Ejemplo n.º 2
0
/**
 * This function move a class in the class tree
 *
 * @author Damien Garros <*****@*****.**>
 *
 * @param  int class_id , id of the class what you want to move
 * @param  int class_id_towards, id of the parent destination class
 *
 * @return true if everything is good or an error string
 **/
function move_class($class_id, $class_id_towards)
{
    $tbl_mdb_names = claro_sql_get_main_tbl();
    $tbl_user = $tbl_mdb_names['user'];
    $tbl_class_user = $tbl_mdb_names['rel_class_user'];
    $tbl_class = $tbl_mdb_names['class'];
    // 1 - Check if $class_id is different with $move_class_id
    if ($class_id == $class_id_towards) {
        return claro_failure::set_failure('move_same_class');
    }
    // 2 - Check if $class_id and $moved_class_id are in the main DB
    $sql = "SELECT `id`,`class_parent_id`\n            FROM `" . $tbl_class . "`\n            WHERE `id` = '" . (int) $class_id . "' ";
    $result = claro_sql_query_fetch_all($sql);
    if (!isset($result[0]['id'])) {
        return claro_failure::set_failure('class_not_found');
        // the class doesn't exist
    }
    if ($class_id_towards != 0) {
        $sql = "SELECT `id`, `class_level`\n                FROM `" . $tbl_class . "`\n                WHERE `id` = '" . (int) $class_id_towards . "' ";
        $result = claro_sql_query_fetch_all($sql);
        if (!isset($result[0]['id'])) {
            return claro_failure::set_failure('class_not_found');
            // the parent class doesn't exist
        } else {
            $class_level = $result[0]['class_level'] + 1;
        }
    } else {
        //if $class_id_parent is root
        $class_id_towards = "NULL";
        $class_level = 1;
    }
    //Move class
    $sql_update = "UPDATE `" . $tbl_class . "`\n                 SET class_parent_id= " . $class_id_towards . ",\n                 class_level = " . $class_level . "\n                 WHERE id= " . (int) $class_id;
    claro_sql_query($sql_update);
    //Get user list
    $sql = "SELECT *\n        FROM `" . $tbl_class_user . "` `rel_c_u`, `" . $tbl_user . "` `u`\n        WHERE `class_id`='" . (int) $class_id . "'\n        AND `rel_c_u`.`user_id` = `u`.`user_id`";
    $userList = claro_sql_query_fetch_all($sql);
    //suscribe each user to parent class
    foreach ($userList as $user) {
        user_add_to_class($user['user_id'], $class_id_towards);
    }
    return true;
}
Ejemplo n.º 3
0
$tbl_class = $tbl_mdb_names['user_category'];
$tbl_class_user = $tbl_mdb_names['user_rel_profile_category'];
// Main section
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : null;
$user_id = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0;
$class_id = isset($_REQUEST['class_id']) ? (int) $_REQUEST['class_id'] : 0;
$search = isset($_REQUEST['search']) ? trim($_REQUEST['search']) : '';
// find info about the class
if (($classinfo = class_get_properties($class_id)) === false) {
    $class_id = 0;
}
$dialogBox = new DialogBox();
if (!empty($class_id)) {
    switch ($cmd) {
        case 'subscribe':
            if (user_add_to_class($user_id, $class_id)) {
                $dialogBox->success(get_lang('User has been sucessfully registered to the class'));
            }
            break;
        case 'unsubscribe':
            if (user_remove_to_class($user_id, $class_id)) {
                $dialogBox->success(get_lang('User has been sucessfully unregistered from the class'));
            }
            break;
    }
    //----------------------------------
    // Build query and find info in db
    //----------------------------------
    $sql = "SELECT *, U.`user_id`\n            FROM  `" . $tbl_user . "` AS U\n            LEFT JOIN `" . $tbl_class_user . "` AS CU\n                   ON  CU.`user_id` = U.`user_id`\n                  AND CU.`class_id` = " . (int) $class_id;
    if (!empty($search)) {
        $escapedSearchTerm = claro_sql_escape($search);