/** * Add a list of users given their user id to the course * @param array $userIdList list of user ids to add * @param Claro_Class $class execute class registration instead of individual registration if given (default:null) * @param bool $forceClassRegistrationOfExistingClassUsers transform individual registration to class registration if set to true (default: false) * @param array $userListAlreadyInClass user already in class as an array of user_id => user * @param bool $forceValidationOfPendingUsers pending user enrollments will be validated if set to true (default: false) * @return boolean */ public function addUserIdListToCourse($userIdList, $class = null, $forceClassRegistrationOfExistingClassUsers = false, $userListAlreadyInClass = array(), $forceValidationOfPendingUsers = false) { if (!count($userIdList)) { return false; } $classMode = is_null($class) ? false : true; $courseCode = $this->course->courseId; $sqlCourseCode = $this->database->quote($courseCode); $updateUserList = array(); $failedUserList = array(); // 1. PROCESS USERS ALREADY IN COURSE // get user id already in course $usersAlreadyInCourse = $this->getUsersAlreadyInCourse($userIdList, $courseCode); // update registration of existing users if classMode if ($classMode) { // register class to course if not already done if (!$class->isRegisteredToCourse($courseCode)) { $class->registerToCourse($courseCode); } foreach ($usersAlreadyInCourse as $userId => $courseUser) { if ($forceClassRegistrationOfExistingClassUsers) { $courseUser['count_user_enrol'] = 0; } if (!array_key_exists($courseUser['user_id'], $userListAlreadyInClass) || $courseUser['count_class_enrol'] <= 0) { $courseUser['count_class_enrol']++; } $pending = $forceValidationOfPendingUsers ? '`isPending` = 0,' : ''; // update user in DB if (!$this->database->exec("\n UPDATE\n `{$this->tableNames['rel_course_user']}`\n SET\n {$pending}\n `count_user_enrol` = " . $courseUser['count_user_enrol'] . ",\n `count_class_enrol` = " . $courseUser['count_class_enrol'] . "\n WHERE\n user_id = " . Claroline::getDatabase()->escape($userId) . "\n AND\n code_cours = {$sqlCourseCode}")) { $failedUserList[$courseUser['user_id']] = $courseUser; } $updateUserList[$courseUser['user_id']] = $courseUser; } if (count($failedUserList)) { $this->result->addFailed($failedUserList); $this->result->setStatus(Claro_BatchRegistrationResult::STATUS_ERROR_UPDATE_FAIL); $this->result->addError(get_lang("Cannot update course registration information for users %userlist% in course %course%", array('%userlist%' => implode(",", $failedUserList), '%course%' => $courseCode))); Console::error("Cannot update course registration information for users " . implode(",", $failedUserList) . " in course {$courseCode}"); } $this->result->addUpdated($updateUserList); } // 2. PROCESS USERS NOT ALREADY IN COURSE // construct the query for insertion of new users $sqlProfileId = $this->database->escape(claro_get_profile_id(USER_PROFILE)); $userNewRegistrations = array(); $userListToInsert = array(); foreach ($userIdList as $userId) { if (!array_key_exists($userId, $usersAlreadyInCourse)) { if ($classMode) { $userNewRegistration = array('user_id' => $this->database->escape($userId), 'count_user_enrol' => 0, 'count_class_enrol' => 1, 'isPending' => 0); } else { $userNewRegistration = array('user_id' => $this->database->escape($userId), 'count_user_enrol' => 1, 'count_class_enrol' => 0, 'isPending' => 0); } // user_id, profile_id, isCourseManager, isPending, tutor, count_user_enrol, count_class_enrol, enrollment_date $userNewRegistrations[] = "({$userNewRegistration['user_id']},{$sqlCourseCode}, {$sqlProfileId}, 0, 0, 0, {$userNewRegistration['count_user_enrol']},{$userNewRegistration['count_class_enrol']}, NOW())"; $userListToInsert[$userId] = $userNewRegistration; } } // execute the query if (count($userNewRegistrations)) { if (!$this->database->exec("\n INSERT INTO\n `{$this->tableNames['rel_course_user']}`\n (user_id, code_cours, profile_id, isCourseManager, isPending, tutor, count_user_enrol, count_class_enrol, enrollment_date)\n VALUES\n" . implode(",\n\t", $userNewRegistrations))) { $this->result->setStatus(Claro_BatchRegistrationResult::STATUS_ERROR_INSERT_FAIL); $this->result->addError(get_lang("Cannot insert userlist %userlist% in course %course%", array('%userlist%' => implode(",", $userListToInsert), '%course%' => $courseCode))); Console::error("Cannot insert userlist " . implode(",", $userListToInsert) . " in course {$courseCode}"); $this->result->addFailed($userListToInsert); } else { $this->result->addInserted($userListToInsert); } } if ($this->course->hasSourceCourse()) { $sourceCourse = $this->course->getSourceCourse(); $sourceReg = new Claro_BatchCourseRegistration($sourceCourse, $this->database, $this->result); $sourceReg->addUserIdListToCourse($userIdList, $class, $forceClassRegistrationOfExistingClassUsers, $userListAlreadyInClass, $forceValidationOfPendingUsers); $this->result->mergeResult($sourceReg->getResult()); } return !$this->result->hasError(); }
public function removeUser($keepTrackingData = true, $moduleDataToPurge = array()) { if (!$this->isUnregistrationAllowed()) { return false; } if (!$this->isUserRegisteredToCourse()) { $this->status = self::STATUS_SYSTEM_ERROR; $this->errorMessage = get_lang('User not found in course'); return false; } else { $batchRegistration = new Claro_BatchCourseRegistration($this->course); $batchRegistration->removeUserIdListFromCourse(array($this->userAuthProfile->getUserId()), $this->class, $keepTrackingData, $moduleDataToPurge, true); if ($batchRegistration->hasError()) { Console::error(var_export($batchRegistration->getErrorLog(), true)); return false; } else { return true; } } }
/** * helper to unregister a class (and recursively unregister all its subclasses) from a course * @param Claro_Class $claroClass * @param Claro_Course $courseObj * @param Claro_BatchRegistrationResult $result * @return Claro_BatchRegistrationResult * @since Claroline 1.11.9 */ function object_unregister_class_from_course($claroClass, $courseObj, $result) { if ($claroClass->isRegisteredToCourse($courseObj->courseId)) { $classUserIdList = $claroClass->getClassUserList()->getClassUserIdList(); $courseBatchRegistretion = new Claro_BatchCourseRegistration($courseObj); $courseBatchRegistretion->removeUserIdListFromCourse($classUserIdList, $claroClass); if ($claroClass->hasSubclasses()) { pushClaroMessage("Class has subclass", 'debug'); // recursion ! foreach ($claroClass->getSubClassesIterator() as $subClass) { pushClaroMessage("Process subclass{$subClass->getName()}", 'debug'); $result = object_unregister_class_from_course($subClass, $courseObj, $result); } } else { pushClaroMessage("Class has no subclass", 'debug'); } $claroClass->unregisterFromCourse($courseObj->courseId); return $result; } else { return $result; } }
} // 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) if ('allStudent' == $req['user_id']) { // TODO : add a function to unenroll all users from a course $course = new Claro_Course(claro_get_current_course_id()); $course->load(); $claroCourseRegistration = new Claro_BatchCourseRegistration($course); $claroCourseRegistration->removeAllUsers($req['keepClasses']); $result = $claroCourseRegistration->getResult(); if (!$result->hasError() || !$result->checkStatus(Claro_BatchRegistrationResult::STATUS_ERROR_DELETE_FAIL)) { $unregisterdUserCount = count($result->getDeletedUserList()); if ($unregisterdUserCount) { Console::log("{$req['user_id']} ({$unregisterdUserCount}) removed by user " . claro_get_current_user_id(), 'COURSE_UNSUBSCRIBE'); } $dialogBox->info(get_lang('%number student(s) unregistered from this course', array('%number' => $unregisterdUserCount))); } else { Console::error("Error while deleting all users from course " . claro_get_current_course_id() . " : " . var_export($result->getErrorLog(), true)); $dialogBox->error(get_lang('An error occured') . ' : <ul><li>' . implode('</li><li>', $result->getErrorLog()) . '</li></ul>'); } } elseif (0 < (int) $req['user_id']) { if ($forceUnenrolment) { $course = new Claro_Course(claro_get_current_course_id());