Esempio n. 1
0
$tbl_course_user = $tbl_mdb_names['rel_course_user'];
// See SESSION variables used for reorder criteria :
if (isset($_REQUEST['dir'])) {
    $_SESSION['admin_register_dir'] = $_REQUEST['dir'];
}
if (isset($_REQUEST['order_crit'])) {
    $_SESSION['admin_register_order_crit'] = $_REQUEST['order_crit'];
}
//------------------------------------
// Execute COMMAND section
//------------------------------------
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : null;
switch ($cmd) {
    case 'sub':
        //execute subscription command...
        $done = user_add_to_course($user_id, $cidToEdit, false, false, null);
        // Set status requested
        if ($_REQUEST['isCourseManager']) {
            $properties['isCourseManager'] = 1;
            $properties['tutor'] = 1;
        } else {
            $properties['isCourseManager'] = 0;
            $properties['tutor'] = 0;
        }
        user_set_course_properties($user_id, $cidToEdit, $properties);
        //set dialogbox message
        if ($done) {
            $dialogBox->success(get_lang('The user has been enroled to the course'));
        }
        break;
}
Esempio n. 2
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;
 }
Esempio n. 3
0
/**
 * subscribe a specific user to a class
 *
 * @author Guillaume Lederer < *****@*****.** >
 *
 * @param int $user_id user ID from the course_user table
 * @param int $class_id class id from the class table
 *
 * @return boolean TRUE  if subscribtion succeed
 *         boolean FALSE otherwise.
 */
function user_add_to_class($user_id, $class_id)
{
    $user_id = (int) $user_id;
    $class_id = (int) $class_id;
    // get database information
    $tbl_mdb_names = claro_sql_get_main_tbl();
    $tbl_rel_class_user = $tbl_mdb_names['rel_class_user'];
    $tbl_class = $tbl_mdb_names['class'];
    $tbl_course_class = $tbl_mdb_names['rel_course_class'];
    $tbl_course = $tbl_mdb_names['course'];
    // 1. See if there is a user with such ID in the main database
    $user_data = user_get_properties($user_id);
    if (!$user_data) {
        return claro_failure::get_last_failure('USER_NOT_FOUND');
    }
    // 2. See if there is a class with such ID in the main DB
    $sql = "SELECT `class_parent_id`\n            FROM `" . $tbl_class . "`\n            WHERE `id` = '" . $class_id . "' ";
    $result = claro_sql_query_get_single_row($sql);
    if (count($result) == 0) {
        return claro_failure::set_failure('CLASS_NOT_FOUND');
        // the class doesn't exist
    }
    // 3. See if user is not already in class
    $sql = "SELECT `user_id`\n            FROM `" . $tbl_rel_class_user . "`\n            WHERE `user_id` = '" . $user_id . "'\n            AND `class_id` = '" . $class_id . "'";
    $handle = claro_sql_query($sql);
    if (mysql_num_rows($handle) > 0) {
        return claro_failure::set_failure('USER_ALREADY_IN_CLASS');
        // the user is already subscrided to the class
    }
    // 4. Add user to class in the rel_class_user table
    $sql = "INSERT INTO `" . $tbl_rel_class_user . "`\n            SET `user_id` = '" . $user_id . "',\n               `class_id` = '" . $class_id . "' ";
    claro_sql_query($sql);
    // 5. Add user to each course whose link with class
    $sql = "SELECT `c`.`code`\n            FROM `" . $tbl_course_class . "` `cc`, `" . $tbl_course . "` `c`\n            WHERE `cc`.`courseId` = `c`.`code`\n            AND `cc`.`classId` = " . $class_id;
    $courseList = claro_sql_query_fetch_all($sql);
    foreach ($courseList as $course) {
        //check if every think is good
        if (!user_add_to_course($user_id, $course['code'], false, false, $class_id)) {
            return claro_failure::set_failure('PROBLEM_WITH_COURSE_SUBSCRIBE');
            //TODO : ameliorer la  gestion d'erreur ...
        }
    }
    return true;
}
Esempio n. 4
0
if ($cmd == 'unregister') {
    if (isset($_REQUEST['deleteClasses'])) {
        $req['keepClasses'] = false;
    } else {
        $req['keepClasses'] = true;
    }
}
/*=====================================================================
  Main section
  =====================================================================*/
$disp_tool_link = false;
if ($is_allowedToEdit) {
    $disp_tool_link = true;
    // Register a new user
    if ($cmd == 'register' && $req['user_id']) {
        $done = user_add_to_course($req['user_id'], claro_get_current_course_id(), false, false, null);
        if ($done) {
            Console::log("{$req['user_id']} subscribe to course " . claro_get_current_course_id(), 'COURSE_SUBSCRIBE');
            $dialogBox->success(get_lang('User registered to the course'));
        }
    }
    // Unregister a user
    if ($cmd == 'unregister') {
        $forceUnenrolment = false;
        if (claro_is_platform_admin()) {
            if (isset($_REQUEST['force']) && $_REQUEST['force'] == '1') {
                $forceUnenrolment = true;
            }
        }
        // Unregister user from course
        // (notice : it does not delete user from claroline main DB)