Skip to content

Commit c5a63ce

Browse files
authored
Merge pull request #2149 from wariosolis/1.11.x
Add some methods to Rest Api
2 parents ca901a4 + 1b1e87c commit c5a63ce

File tree

2 files changed

+309
-3
lines changed

2 files changed

+309
-3
lines changed

main/inc/lib/webservices/Rest.php

Lines changed: 296 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class Rest extends WebService
4242
const SAVE_COURSE_NOTEBOOK = 'save_course_notebook';
4343
const SAVE_FORUM_THREAD = 'save_forum_thread';
4444

45+
const SAVE_COURSE = 'save_course';
46+
const SAVE_USER = 'save_user';
47+
const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course';
48+
4549
const EXTRAFIELD_GCM_ID = 'gcm_registration_id';
4650

4751
/**
@@ -80,7 +84,8 @@ public function setCourse($id)
8084
/** @var Course $course */
8185
$course = $em->find('ChamiloCoreBundle:Course', $id);
8286

83-
if (!$course) {
87+
if (!$course)
88+
{
8489
throw new Exception(get_lang('NoCourse'));
8590
}
8691

@@ -1015,4 +1020,294 @@ public function saveForumThread(array $values, $forumId)
10151020
'registered' => $id
10161021
];
10171022
}
1023+
1024+
/**
1025+
* @param array $course_param
1026+
* @return array
1027+
*/
1028+
public function saveNewCourse($course_param)
1029+
{
1030+
1031+
$table_course = Database::get_main_table(TABLE_MAIN_COURSE);
1032+
$extra_list= array();
1033+
1034+
$title = isset($course_param['title']) ? $course_param['title'] : '';
1035+
$category_code = isset($course_param['category_code']) ? $course_param['category_code'] : '';
1036+
$wanted_code = isset($course_param['wanted_code']) ? intval($course_param['wanted_code']) : 0;
1037+
$tutor_name = isset($course_param['tutor_name']) ? $course_param['tutor_name'] : '';
1038+
$admin_id = isset($course_param['admin_id']) ? $course_param['admin_id'] : null;
1039+
$language = isset($course_param['language']) ? $course_param['language'] : null;
1040+
$original_course_id = isset($course_param['original_course_id']) ? $course_param['original_course_id'] : null;
1041+
$diskQuota = isset($course_param['disk_quota']) ? $course_param['disk_quota'] : '100';
1042+
$visibility = isset($course_param['visibility']) ? $course_param['visibility'] : null;;
1043+
1044+
if (isset($course_param['visibility'])) {
1045+
if ($course_param['visibility'] &&
1046+
$course_param['visibility'] >= 0 &&
1047+
$course_param['visibility'] <= 3
1048+
) {
1049+
$visibility = $course_param['visibility'];
1050+
}
1051+
}
1052+
1053+
// Check whether exits $x_course_code into user_field_values table.
1054+
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
1055+
"id",
1056+
$course_param['original_course_id_name']
1057+
);
1058+
1059+
if (!empty($courseInfo)) {
1060+
if ($courseInfo['visibility'] != 0) {
1061+
$sql = "UPDATE $table_course SET
1062+
course_language='".Database::escape_string($course_language)."',
1063+
title='".Database::escape_string($title)."',
1064+
category_code='".Database::escape_string($category_code)."',
1065+
tutor_name='".Database::escape_string($tutor_name)."',
1066+
visual_code='".Database::escape_string($wanted_code)."'";
1067+
if ($visibility !== null) {
1068+
$sql .= ", visibility = '$visibility' ";
1069+
}
1070+
$sql .= " WHERE id='".$courseInfo['real_id']."'";
1071+
Database::query($sql);
1072+
if (is_array($extra_list) && count($extra_list) > 0) {
1073+
foreach ($extra_list as $extra) {
1074+
$extra_field_name = $extra['field_name'];
1075+
$extra_field_value = $extra['field_value'];
1076+
// Save the external system's id into course_field_value table.
1077+
CourseManager::update_course_extra_field_value(
1078+
$courseInfo['code'],
1079+
$extra_field_name,
1080+
$extra_field_value
1081+
);
1082+
}
1083+
}
1084+
$results[] = $courseInfo['code'];
1085+
} else {
1086+
$results[] = 0;
1087+
}
1088+
}
1089+
1090+
if (!empty($course_param['course_language'])) {
1091+
$course_language = $course_param['course_language'];
1092+
}
1093+
1094+
$params = array();
1095+
$params['title'] = $title;
1096+
$params['wanted_code'] = $wanted_code;
1097+
$params['category_code'] = $category_code;
1098+
$params['course_category'] = $category_code;
1099+
$params['tutor_name'] = $tutor_name;
1100+
$params['course_language'] = $course_language;
1101+
$params['user_id'] = $this->user->getId();
1102+
$params['visibility'] = $visibility;
1103+
$params['disk_quota'] = $diskQuota;
1104+
1105+
if (isset($subscribe) && $subscribe != '') { // Valid values: 0, 1
1106+
$params['subscribe'] = $subscribe;
1107+
}
1108+
if (isset($unsubscribe) && $subscribe != '') { // Valid values: 0, 1
1109+
$params['unsubscribe'] = $unsubscribe;
1110+
}
1111+
1112+
$course_info = CourseManager::create_course($params, $params['user_id']);
1113+
1114+
if (!empty($course_info)) {
1115+
$course_code = $course_info['code'];
1116+
1117+
// Save new field label into course_field table
1118+
CourseManager::create_course_extra_field(
1119+
$original_course_id_name,
1120+
1,
1121+
$original_course_id_name,
1122+
''
1123+
);
1124+
1125+
// Save the external system's id into user_field_value table.
1126+
CourseManager::update_course_extra_field_value(
1127+
$course_code,
1128+
$original_course_id_name,
1129+
$original_course_id_value
1130+
);
1131+
1132+
if (is_array($extra_list) && count($extra_list) > 0) {
1133+
foreach ($extra_list as $extra) {
1134+
$extra_field_name = $extra['field_name'];
1135+
$extra_field_value = $extra['field_value'];
1136+
// Save new fieldlabel into course_field table.
1137+
CourseManager::create_course_extra_field(
1138+
$extra_field_name,
1139+
1,
1140+
$extra_field_name,
1141+
''
1142+
);
1143+
// Save the external system's id into course_field_value table.
1144+
CourseManager::update_course_extra_field_value(
1145+
$course_code,
1146+
$extra_field_name,
1147+
$extra_field_value
1148+
);
1149+
}
1150+
}
1151+
$results[] = $course_code;
1152+
} else {
1153+
$results[] = 0;
1154+
}
1155+
1156+
return $results;
1157+
}
1158+
1159+
/**
1160+
*/
1161+
public function saveNewUser($user_param)
1162+
{
1163+
global $_user;
1164+
$results = array();
1165+
$orig_user_id_value = array();
1166+
1167+
$userManager = UserManager::getManager();
1168+
$userRepository = UserManager::getRepository();
1169+
1170+
$firstName = $user_param['firstname'];
1171+
$lastName = $user_param['lastname'];
1172+
$status = $user_param['status'];
1173+
$email = $user_param['email'];
1174+
$loginName = $user_param['loginname'];
1175+
$password = $user_param['password'];
1176+
$official_code = '';
1177+
$language = '';
1178+
$phone = '';
1179+
$picture_uri = '';
1180+
$auth_source = PLATFORM_AUTH_SOURCE;
1181+
$expiration_date = '';
1182+
$active = 1;
1183+
$hr_dept_id = 0;
1184+
$extra = null;
1185+
$original_user_id_name = $user_param['original_user_id_name'];
1186+
$original_user_id_value = $user_param['original_user_id_value'];
1187+
$orig_user_id_value[] = $user_param['original_user_id_value'];
1188+
$extra_list = $user_param['extra'];
1189+
if (!empty($user_param['language'])) {
1190+
$language = $user_param['language'];
1191+
}
1192+
if (!empty($user_param['phone'])) {
1193+
$phone = $user_param['phone'];
1194+
}
1195+
if (!empty($user_param['expiration_date'])) {
1196+
$expiration_date = $user_param['expiration_date'];
1197+
}
1198+
1199+
// Default language.
1200+
if (empty($language)) {
1201+
$language = api_get_setting('platformLanguage');
1202+
}
1203+
1204+
if (!empty($_user['user_id'])) {
1205+
$creator_id = $_user['user_id'];
1206+
} else {
1207+
$creator_id = '';
1208+
}
1209+
1210+
// First check wether the login already exists.
1211+
if (!UserManager::is_username_available($loginName)) {
1212+
$results[] = 0;
1213+
}
1214+
1215+
$userId = UserManager::create_user(
1216+
$firstName,
1217+
$lastName,
1218+
$status,
1219+
$email,
1220+
$loginName,
1221+
$password,
1222+
$official_code,
1223+
$language,
1224+
$phone,
1225+
$picture_uri,
1226+
$auth_source,
1227+
$expiration_date,
1228+
$active,
1229+
$hr_dept_id
1230+
);
1231+
1232+
if ($userId) {
1233+
if (api_is_multiple_url_enabled()) {
1234+
if (api_get_current_access_url_id() != -1) {
1235+
UrlManager::add_user_to_url(
1236+
$userId,
1237+
api_get_current_access_url_id()
1238+
);
1239+
} else {
1240+
UrlManager::add_user_to_url($userId, 1);
1241+
}
1242+
} else {
1243+
// We add by default the access_url_user table with access_url_id = 1
1244+
UrlManager::add_user_to_url($userId, 1);
1245+
}
1246+
1247+
// Save new field label into user_field table.
1248+
UserManager::create_extra_field(
1249+
$original_user_id_name,
1250+
1,
1251+
$original_user_id_name,
1252+
''
1253+
);
1254+
// Save the external system's id into user_field_value table.
1255+
UserManager::update_extra_field_value(
1256+
$userId,
1257+
$original_user_id_name,
1258+
$original_user_id_value
1259+
);
1260+
1261+
if (is_array($extra_list) && count($extra_list) > 0) {
1262+
foreach ($extra_list as $extra) {
1263+
$extra_field_name = $extra['field_name'];
1264+
$extra_field_value = $extra['field_value'];
1265+
// Save new field label into user_field table.
1266+
UserManager::create_extra_field(
1267+
$extra_field_name,
1268+
1,
1269+
$extra_field_name,
1270+
''
1271+
);
1272+
// Save the external system's id into user_field_value table.
1273+
UserManager::update_extra_field_value(
1274+
$userId,
1275+
$extra_field_name,
1276+
$extra_field_value
1277+
);
1278+
}
1279+
}
1280+
$results[] = $userId;
1281+
} else {
1282+
$results[] = 0;
1283+
}
1284+
1285+
// $results[] = $userId;
1286+
return $results;
1287+
}
1288+
1289+
/**
1290+
* Subscribe User to Course
1291+
*/
1292+
public function subscribeUserToCourse($params)
1293+
{
1294+
$course_id = $params['course_id'];
1295+
$course_code = $params['course_code'];
1296+
$user_id = $params['user_id'];
1297+
if (!$course_id && !$course_code)
1298+
{
1299+
return [false];
1300+
}
1301+
if (!$course_code)
1302+
{
1303+
$course_code = CourseManager::get_course_code_from_course_id($course_id);
1304+
}
1305+
if (CourseManager::subscribe_user($user_id, $course_code))
1306+
{
1307+
return [true];
1308+
} else {
1309+
return [false];
1310+
}
1311+
return [true];
1312+
}
10181313
}

main/webservices/api/v2.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/* For licensing terms, see /license.txt */
3-
43
require_once __DIR__.'/../../inc/global.inc.php';
54

65
$hash = isset($_REQUEST['hash']) ? $_REQUEST['hash'] : null;
@@ -116,9 +115,21 @@
116115
$restResponse->setData($data);
117116
break;
118117
case Rest::GET_COURSE_LEARNPATH:
119-
$lpId = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : 0;
118+
$lpId = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : 1;
120119
$restApi->showLearningPath($lpId);
121120
break;
121+
case Rest::SAVE_COURSE:
122+
$data = $restApi->saveNewCourse($_POST);
123+
$restResponse->setData($data);
124+
break;
125+
case Rest::SAVE_USER:
126+
$data = $restApi->saveNewUser($_POST);
127+
$restResponse->setData($data);
128+
break;
129+
case Rest::SUBSCRIBE_USER_TO_COURSE:
130+
$data = $restApi->subscribeUserToCourse($_POST);
131+
$restResponse->setData($data);
132+
break;
122133
case Rest::SAVE_FORUM_POST:
123134
if (
124135
empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum'])

0 commit comments

Comments
 (0)