Use entities in CourseManager::subscribeUser

Use course id instead course code
pull/3890/head
Julio Montoya 5 years ago
parent 2a8bd78ad3
commit 9763d82a5d
  1. 4
      public/main/admin/course_import.php
  2. 3
      public/main/admin/course_user_import.php
  3. 2
      public/main/admin/course_user_import_by_email.php
  4. 3
      public/main/admin/ldap_import_students.php
  5. 3
      public/main/admin/subscribe_user2course.php
  6. 5
      public/main/admin/user_import.php
  7. 3
      public/main/admin/user_update_import.php
  8. 4
      public/main/auth/inscription.php
  9. 2
      public/main/course_info/legal.php
  10. 2
      public/main/cron/import_csv.php
  11. 10
      public/main/inc/lib/api.lib.php
  12. 109
      public/main/inc/lib/course.lib.php
  13. 14
      public/main/inc/lib/events.lib.php
  14. 6
      public/main/inc/lib/exercise.lib.php
  15. 4
      public/main/inc/lib/sessionmanager.lib.php
  16. 7
      public/main/inc/lib/usergroup.lib.php
  17. 5
      public/main/inc/lib/webservices/Rest.php
  18. 8
      public/main/user/subscribe_user.php
  19. 2
      public/main/user/user_import.php
  20. 6
      public/main/webservices/cm_webservice_course.php
  21. 10
      public/main/webservices/registration.soap.php
  22. 5
      public/main/webservices/user_import/import.lib.php
  23. 2
      public/main/webservices/webservice_course.php
  24. 3
      public/plugin/buycourses/src/buy_course_plugin.class.php
  25. 2
      public/plugin/justification/cron.php
  26. 1
      src/CoreBundle/Entity/CourseRelUser.php
  27. 4
      tests/scripts/users_no_course.php

@ -125,7 +125,7 @@ function save_courses_data($courses)
$params['course_category'] = $course['CourseCategory'];
$params['course_language'] = $course_language;
$params['user_id'] = $creatorId;
$addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false;
$addMeAsTeacher = $_POST['add_me_as_teacher'] ?? false;
$params['add_user_as_teacher'] = $addMeAsTeacher;
$courseInfo = CourseManager::create_course($params);
@ -134,7 +134,7 @@ function save_courses_data($courses)
foreach ($teacherList as $teacher) {
CourseManager::subscribeUser(
$teacher['user_id'],
$courseInfo['code'],
$courseInfo['real_id'],
COURSEMANAGER
);
}

@ -103,10 +103,9 @@ function save_data($users_courses)
if (isset($_POST['subscribe']) && $_POST['subscribe']) {
foreach ($to_subscribe as $courseId) {
$courseInfo = $courseListById[$courseId];
$courseCode = $courseInfo['code'];
$result = CourseManager::subscribeUser(
$user_id,
$courseCode,
$courseId,
$csv_subscriptions[$courseId]
);
if ($result) {

@ -104,7 +104,7 @@ function save_data($users_courses)
CourseManager::subscribeUser(
$user_id,
$course_code,
$courseId,
$csv_subscriptions[$course_code]
);
$inserted_in_course[$course_info['code']] = $course_info['title'];

@ -137,7 +137,8 @@ if (empty($annee) && empty($course)) {
}
if (!empty($_POST['course'])) {
foreach ($UserList as $user_id) {
CourseManager::subscribeUser($user_id, $_POST['course']);
$courseInfo = api_get_course_info($_POST['course']);
CourseManager::subscribeUser($user_id, $courseInfo['real_id']);
}
header('Location: course_information.php?code='.Security::remove_XSS($_POST['course']));
exit;

@ -96,7 +96,8 @@ if (isset($_POST['form_sent']) && $_POST['form_sent']) {
foreach ($users as $user_id) {
$user = api_get_user_info($user_id);
if (DRH != $user['status']) {
CourseManager::subscribeUser($user_id, $course_code);
$courseInfo = api_get_course_info($course_code);
CourseManager::subscribeUser($user_id, $courseInfo['real_id']);
} else {
$errorDrh = 1;
}

@ -260,9 +260,10 @@ function save_data($users, $sendMail = false)
if (isset($user['Courses']) && is_array($user['Courses'])) {
foreach ($user['Courses'] as $course) {
if (CourseManager::course_exists($course)) {
$result = CourseManager::subscribeUser($user_id, $course, $user['Status']);
$course_info = api_get_course_info($course);
$result = CourseManager::subscribeUser($user_id, $course_info['real_id'], $user['Status']);
if ($result) {
$course_info = api_get_course_info($course);
$inserted_in_course[$course] = $course_info['title'];
}
}

@ -174,7 +174,8 @@ function updateUsers(
if (!empty($user['Courses']) && is_array($user['Courses'])) {
foreach ($user['Courses'] as $course) {
if (CourseManager::course_exists($course)) {
CourseManager::subscribeUser($user_id, $course, $user['Status']);
$courseInfo = api_get_course_info($course);
CourseManager::subscribeUser($user_id, $courseInfo['real_id'], $user['Status']);
}
}
}

@ -792,7 +792,7 @@ if ($form->validate()) {
) {
CourseManager::subscribeUser(
$user_id,
$course_info['code']
$course_info['real_id']
);
}
}
@ -1048,7 +1048,7 @@ if ($form->validate()) {
) {
CourseManager::subscribeUser(
api_get_user_id(),
$course_info['code']
$course_info['real_id']
);
}
}

@ -84,7 +84,7 @@ if ($form->validate()) {
COURSE_VISIBILITY_REGISTERED == $course_info['visibility'] &&
1 == $course_info['subscribe']
) {
CourseManager::subscribeUser($user_id, $course_info['code'], STUDENT, 0);
CourseManager::subscribeUser($user_id, $course_info['real_id'], STUDENT, 0);
}
CourseManager::save_user_legal($user_id, $course_info, $session_id);

@ -2508,7 +2508,7 @@ class ImportCsv
$result = CourseManager::subscribeUser(
$userId,
$courseInfo['code'],
$courseInfo['real_id'],
$status,
0,
$userCourseCategory

@ -2053,12 +2053,12 @@ function api_get_course_int_id($code = null)
* Gets a course setting from the current course_setting table. Try always using integer values.
*
* @param string $settingName The name of the setting we want from the table
* @param array $courseInfo
* @param Course|array $courseInfo
* @param bool $force force checking the value in the database
*
* @return mixed The value of that setting in that table. Return -1 if not found.
*/
function api_get_course_setting($settingName, $courseInfo = [], $force = false)
function api_get_course_setting($settingName, $courseInfo = null, $force = false)
{
if (empty($courseInfo)) {
$courseInfo = api_get_course_info();
@ -2068,7 +2068,11 @@ function api_get_course_setting($settingName, $courseInfo = [], $force = false)
return -1;
}
$courseId = isset($courseInfo['real_id']) && !empty($courseInfo['real_id']) ? $courseInfo['real_id'] : 0;
if ($courseInfo instanceof Course) {
$courseId = $courseInfo->getId();
} else {
$courseId = isset($courseInfo['real_id']) && !empty($courseInfo['real_id']) ? $courseInfo['real_id'] : 0;
}
if (empty($courseId)) {
return -1;

@ -4,6 +4,7 @@
use Chamilo\CoreBundle\Entity\AccessUrlRelSession;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
use Chamilo\CoreBundle\Entity\SequenceResource;
use Chamilo\CoreBundle\Framework\Container;
@ -701,7 +702,7 @@ class CourseManager
return true;
}
return self::subscribeUser($userId, $course->getCode(), $status, 0);
return self::subscribeUser($userId, $course->getId(), $status, 0);
}
/**
@ -709,7 +710,7 @@ class CourseManager
* course subscription is allowed.
*
* @param int $userId
* @param string $courseCode
* @param int $courseId
* @param int $status (STUDENT, COURSEMANAGER, COURSE_ADMIN, NORMAL_COURSE_MEMBER)
* @param int $sessionId
* @param int $userCourseCategoryId
@ -721,7 +722,7 @@ class CourseManager
*/
public static function subscribeUser(
$userId,
$courseCode,
$courseId,
$status = STUDENT,
$sessionId = 0,
$userCourseCategoryId = 0,
@ -730,28 +731,27 @@ class CourseManager
$userId = (int) $userId;
$status = (int) $status;
if (empty($userId) || empty($courseCode)) {
if (empty($userId) || empty($courseId)) {
return false;
}
$courseInfo = api_get_course_info($courseCode);
$course = api_get_course_entity($courseId);
if (empty($courseInfo)) {
if (null === $course) {
Display::addFlash(Display::return_message(get_lang('This course doesn\'t exist'), 'warning'));
return false;
}
$userInfo = api_get_user_info($userId);
$user = api_get_user_entity($userId);
if (empty($userInfo)) {
if (null === $user) {
Display::addFlash(Display::return_message(get_lang('This user doesn\'t exist'), 'warning'));
return false;
}
$courseId = $courseInfo['real_id'];
$courseCode = $courseInfo['code'];
$courseCode = $course->getCode();
$userCourseCategoryId = (int) $userCourseCategoryId;
$sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
$status = STUDENT === $status || COURSEMANAGER === $status ? $status : STUDENT;
@ -777,7 +777,7 @@ class CourseManager
Event::addEvent(
LOG_SUBSCRIBE_USER_TO_COURSE,
LOG_USER_OBJECT,
$userInfo,
$user,
api_get_utc_datetime(),
api_get_user_id(),
$courseId,
@ -801,7 +801,7 @@ class CourseManager
if ($checkTeacherPermission && !api_is_course_admin()) {
// Check in advance whether subscription is allowed or not for this course.
if (SUBSCRIBE_NOT_ALLOWED === (int) $courseInfo['subscribe']) {
if (SUBSCRIBE_NOT_ALLOWED === (int) $course->getSubscribe()) {
Display::addFlash(Display::return_message(get_lang('SubscriptionNotAllowed'), 'warning'));
return false;
@ -811,74 +811,87 @@ class CourseManager
if (STUDENT === $status) {
// Check if max students per course extra field is set
$extraFieldValue = new ExtraFieldValue('course');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($courseId, 'max_subscribed_students');
if (!empty($value) && isset($value['value'])) {
$maxStudents = $value['value'];
if ('' !== $maxStudents) {
$maxStudents = (int) $maxStudents;
$count = self::get_user_list_from_course_code(
$courseCode,
0,
null,
null,
STUDENT,
true,
false
);
$value = $extraFieldValue->get_values_by_handler_and_field_variable(
$courseId,
'max_subscribed_students'
);
if (!empty($value) && isset($value['value']) && '' !== $value['value']) {
$maxStudents = (int) $value['value'];
$count = self::get_user_list_from_course_code(
$courseCode,
0,
null,
null,
STUDENT,
true,
false
);
if ($count >= $maxStudents) {
Display::addFlash(Display::return_message(get_lang('The maximum number of student has already been reached, it is not possible to subscribe more student.'), 'warning'));
if ($count >= $maxStudents) {
Display::addFlash(
Display::return_message(
get_lang(
'The maximum number of student has already been reached, it is not possible to subscribe more student.'
),
'warning'
)
);
return false;
}
return false;
}
}
}
$maxSort = api_max_sort_value('0', $userId);
$params = [
'c_id' => $courseId,
'user_id' => $userId,
'status' => $status,
'sort' => $maxSort + 1,
'relation_type' => 0,
'user_course_cat' => $userCourseCategoryId,
];
$insertId = Database::insert($courseUserTable, $params);
$courseRelUser = new CourseRelUser();
$courseRelUser
->setCourse($course)
->setUser($user)
->setStatus($status)
->setSort($maxSort + 1)
->setUserCourseCat($userCourseCategoryId)
;
$em = Database::getManager();
$em->persist($courseRelUser);
$em->flush();
$insertId = $courseRelUser->getId();
if ($insertId) {
Display::addFlash(
Display::return_message(
sprintf(
get_lang('User %s has been registered to course %s'),
$userInfo['complete_name_with_username'],
$courseInfo['title']
UserManager::formatUserFullName($user),
$course->getTitle()
)
)
);
$send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $courseInfo);
$send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course);
if (1 == $send) {
self::email_to_tutor(
$userId,
$courseInfo['real_id'],
$courseId,
false
);
} elseif (2 == $send) {
self::email_to_tutor(
$userId,
$courseInfo['real_id'],
$courseId,
true
);
}
$subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications', $courseInfo);
$subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications', $course);
if (1 === $subscribe) {
$forums = get_forums(0, $courseCode, true, $sessionId);
/*$forums = get_forums(0, true, $sessionId);
foreach ($forums as $forum) {
set_notification('forum', $forum->getIid(), false, $userInfo, $courseInfo);
}
}*/
}
// Add event to the system log
@ -894,7 +907,7 @@ class CourseManager
Event::addEvent(
LOG_SUBSCRIBE_USER_TO_COURSE,
LOG_USER_OBJECT,
$userInfo,
$user,
api_get_utc_datetime(),
api_get_user_id(),
$courseId

@ -60,7 +60,8 @@ class Event
$autoSubscribe = explode('|', $autoSubscribe);
foreach ($autoSubscribe as $code) {
if (CourseManager::course_exists($code)) {
CourseManager::subscribeUser($userId, $code);
$courseInfo = api_get_course_info($code);
CourseManager::subscribeUser($userId, $courseInfo['real_id']);
}
}
}
@ -817,6 +818,17 @@ class Event
unset($event_value['picture_uri']);
$event_value = serialize($event_value);
}
if ($event_value instanceof \Chamilo\CoreBundle\Entity\User) {
$event_value = serialize(
[
'id' => $event_value->getId(),
'username' => $event_value->getUsername(),
'firstname' => $event_value->getFirstName(),
'lastname' => $event_value->getLastname(),
]
);
}
}
// If event is an array then the $event_value_type should finish with
// the suffix _array for example LOG_WORK_DATA = work_data_array

@ -5997,7 +5997,11 @@ EOT;
switch ($action) {
case 'subscribe_student_to_courses':
foreach ($params as $code) {
CourseManager::subscribeUser($currentUserId, $code);
$courseInfo = api_get_course_info($code);
CourseManager::subscribeUser(
$currentUserId,
$courseInfo['real_id']
);
break;
}
break;

@ -5656,7 +5656,7 @@ class SessionManager
CourseManager::subscribeUser(
$teacherToAdd,
$course_code,
$courseId,
COURSEMANAGER,
0,
$userCourseCategory
@ -5777,7 +5777,7 @@ class SessionManager
CourseManager::subscribeUser(
$teacherId,
$course_code,
$courseInfo['real_id'],
COURSEMANAGER,
0,
$userCourseCategory

@ -1116,7 +1116,7 @@ class UserGroup extends Model
foreach ($user_list as $user_id) {
CourseManager::subscribeUser(
$user_id,
$course_info['code']
$course_id
);
}
}
@ -1268,11 +1268,10 @@ class UserGroup extends Model
}
foreach ($new_items as $user_id) {
// Adding courses
// Adding courses.
if (!empty($course_list)) {
foreach ($course_list as $course_id) {
$course_info = api_get_course_info_by_id($course_id);
CourseManager::subscribeUser($user_id, $course_info['code']);
CourseManager::subscribeUser($user_id, $course_id);
}
}
$params = [

@ -1392,10 +1392,7 @@ class Rest extends WebService
if (!$course_id && !$course_code) {
return [false];
}
if (!$course_code) {
$course_code = CourseManager::get_course_code_from_course_id($course_id);
}
if (CourseManager::subscribeUser($user_id, $course_code, $status)) {
if (CourseManager::subscribeUser($user_id, $course_id, $status)) {
return [true];
}

@ -71,14 +71,14 @@ if (isset($_REQUEST['Registerister'])) {
} else {
CourseManager::subscribeUser(
$_REQUEST['user_id'],
$courseInfo['code'],
$courseInfo['real_id'],
COURSEMANAGER
);
}
} else {
CourseManager::subscribeUser(
$_REQUEST['user_id'],
$courseInfo['code']
$courseInfo['real_id']
);
}
}
@ -106,10 +106,10 @@ if (isset($_POST['action'])) {
$isSuscribe[] = $message;
}
} else {
CourseManager::subscribeUser($user_id, $courseInfo['code'], COURSEMANAGER);
CourseManager::subscribeUser($user_id, $courseInfo['real_id'], COURSEMANAGER);
}
} else {
CourseManager::subscribeUser($user_id, $courseInfo['code']);
CourseManager::subscribeUser($user_id, $courseInfo['real_id']);
}
}
}

@ -105,7 +105,7 @@ if ($form->validate()) {
foreach ($clean_users as $userId) {
$userInfo = api_get_user_info($userId);
CourseManager::subscribeUser($userId, $course_code, $userType, $session_id);
CourseManager::subscribeUser($userId, $courseId, $userType, $session_id);
if (empty($session_id)) {
//just to make sure
if (CourseManager::is_user_subscribed_in_course($userId, $course_code)) {

@ -453,7 +453,7 @@ class WSCMCourse extends WSCM
$condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
$sql_query = "SELECT COUNT(*) as number_messages
FROM $table_message
FROM $table_message
WHERE $condition_msg_status AND user_receiver_id=".$user_id;
$sql_result = Database::query($sql_query);
@ -694,10 +694,10 @@ class WSCMCourse extends WSCM
return true;
} else {
// Subscribe user
if (CourseManager::subscribeUser($user_id, $course_code, $status)) {
if (CourseManager::subscribeUser($user_id, $course_id, $status)) {
return true;
} else {
return new WSError(203, 'An error occured subscribing to this course');
return new WSError(203, 'An error occurred subscribing to this course');
}
}
}

@ -4640,9 +4640,9 @@ function WSSubscribeUserToCourse($params)
$original_course_id['original_course_id_name']
);
$courseCode = isset($courseInfo['code']) ? $courseInfo['code'] : '';
$courseId = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : '';
if (empty($courseCode)) {
if (empty($courseId)) {
if ($debug) {
error_log('WSSubscribeUserToCourse course not found');
}
@ -4650,9 +4650,9 @@ function WSSubscribeUserToCourse($params)
$resultValue = 0;
} else {
if ($debug) {
error_log('WSSubscribeUserToCourse courseCode: '.$courseCode);
error_log('WSSubscribeUserToCourse $courseId: '.$courseId);
}
$result = CourseManager::subscribeUser($user_id, $courseCode, $status, 0, 0, false);
$result = CourseManager::subscribeUser($user_id, $courseId, $status, 0, 0, false);
if ($result) {
$resultValue = 1;
if ($debug) {
@ -4766,7 +4766,7 @@ function WSSubscribeUserToCourseSimple($params)
if ($debug) {
error_log('Try to register: user_id= '.$user_id.' to course: '.$course_data['code']);
}
if (!CourseManager::subscribeUser($user_id, $course_data['code'], $status, 0, false, false)) {
if (!CourseManager::subscribeUser($user_id, $course_data['real_id'], $status, 0, false, false)) {
$result = 'User was not registered possible reasons: User already registered to the course,
Course visibility doesnt allow user subscriptions ';
if ($debug) {

@ -95,7 +95,7 @@ function complete_missing_data($user)
function save_data($users)
{
if (is_array($users)) {
foreach ($users as $index => $user) {
foreach ($users as $user) {
$user = complete_missing_data($user);
$user['Status'] = api_status_key($user['Status']);
$user_id = UserManager:: create_user(
@ -115,9 +115,10 @@ function save_data($users)
if (!empty($user['Courses'])) {
foreach ($user['Courses'] as $course) {
if (CourseManager::course_exists($course)) {
$courseInfo = api_get_course_info($course);
CourseManager::subscribeUser(
$user_id,
$course,
$courseInfo['real_id'],
$user['Status']
);
}

@ -714,7 +714,7 @@ class WSCourse extends WS
// Subscribe user
if (CourseManager::subscribeUser(
$user_id,
$course_code,
$course_id,
$status
)
) {

@ -1127,8 +1127,7 @@ class BuyCoursesPlugin extends Plugin
$saleIsCompleted = false;
switch ($sale['product_type']) {
case self::PRODUCT_TYPE_COURSE:
$course = api_get_course_info_by_id($sale['product_id']);
$saleIsCompleted = CourseManager::subscribeUser($sale['user_id'], $course['code']);
$saleIsCompleted = CourseManager::subscribeUser($sale['user_id'], $sale['product_id']);
break;
case self::PRODUCT_TYPE_SESSION:
SessionManager::subscribeUsersToSession(

@ -80,7 +80,7 @@ function subscribeUser($userId, $courseInfo)
$courseId = $courseInfo['real_id'];
$isUserSubscribed = CourseManager::is_user_subscribed_in_course($userId, $courseInfo['code']);
if (false === $isUserSubscribed) {
CourseManager::subscribeUser($userId, $courseInfo['code'], STUDENT);
CourseManager::subscribeUser($userId, $courseId, STUDENT);
echo "Subscribe user id #$userId to course #$courseId".PHP_EOL;
} else {
echo "Nothing to do user id #$userId is already subscribed to #$courseId".PHP_EOL;

@ -115,6 +115,7 @@ class CourseRelUser
$this->sort = 0;
$this->tutor = false;
$this->status = User::STUDENT;
$this->relationType = 0;
}
public function __toString(): string

@ -12,7 +12,7 @@ require __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
// Course that users with no course will be registered:
$courseCode = '';
$courseId = '';
$user = Database::get_main_table(TABLE_MAIN_USER);
$userCourse = Database::get_main_table(TABLE_MAIN_COURSE_USER);
@ -27,7 +27,7 @@ $students = Database::store_result($result);
if (!empty($students)) {
foreach ($students as $student) {
var_dump($student['username'].'- '.$student['user_id']);
$result = CourseManager::subscribeUser($student['user_id'], $courseCode);
$result = CourseManager::subscribeUser($student['user_id'], $courseId);
var_dump($result);
echo '<br />';
}

Loading…
Cancel
Save