123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\UserBundle\Entity\User;
- /**
- * Responses to AJAX calls.
- */
- require_once __DIR__.'/../global.inc.php';
- $action = $_REQUEST['a'];
- switch ($action) {
- case 'get_user_sessions':
- if (api_is_platform_admin() || api_is_session_admin()) {
- $user_id = (int) $_POST['user_id'];
- $list_sessions = SessionManager::get_sessions_by_user($user_id, true);
- if (!empty($list_sessions)) {
- foreach ($list_sessions as $session_item) {
- echo $session_item['session_name'].'<br />';
- }
- } else {
- echo get_lang('This user isn\'t subscribed in a session');
- }
- unset($list_sessions);
- }
- break;
- case 'order':
- api_protect_admin_script();
- $allowOrder = api_get_configuration_value('session_list_order');
- if ($allowOrder) {
- $order = isset($_GET['order']) ? $_GET['order'] : [];
- $order = json_decode($order);
- if (!empty($order)) {
- $table = Database::get_main_table(TABLE_MAIN_SESSION);
- foreach ($order as $data) {
- if (isset($data->order) && isset($data->id)) {
- $orderId = (int) $data->order;
- $sessionId = (int) $data->id;
- $sql = "UPDATE $table SET position = $orderId WHERE id = $sessionId ";
- Database::query($sql);
- }
- }
- }
- }
- break;
- case 'search_session':
- if (api_is_platform_admin()) {
- $sessions = SessionManager::get_sessions_list(
- [
- 's.name' => [
- 'operator' => 'LIKE',
- 'value' => "%".$_REQUEST['q']."%",
- ],
- ]
- );
- $list = [
- 'items' => [],
- ];
- if (empty($sessions)) {
- echo json_encode([]);
- break;
- }
- foreach ($sessions as $session) {
- $list['items'][] = [
- 'id' => $session['id'],
- 'text' => $session['name'],
- ];
- }
- echo json_encode($list);
- }
- break;
- case 'search_session_all':
- if (api_is_platform_admin()) {
- $results = SessionManager::get_sessions_list(
- [
- 's.name' => ['operator' => 'like', 'value' => "%".$_REQUEST['q']."%"],
- 'c.id' => ['operator' => '=', 'value' => $_REQUEST['course_id']],
- ]
- );
- $results2 = [];
- if (!empty($results)) {
- foreach ($results as $item) {
- $item2 = [];
- foreach ($item as $id => $internal) {
- if ($id == 'id') {
- $item2[$id] = $internal;
- }
- if ($id == 'name') {
- $item2['text'] = $internal;
- }
- }
- $results2[] = $item2;
- }
- $results2[] = ['T', 'text' => 'TODOS', 'id' => 'T'];
- echo json_encode($results2);
- } else {
- echo json_encode([['T', 'text' => 'TODOS', 'id' => 'T']]);
- }
- }
- break;
- case 'search_session_by_course':
- if (api_is_platform_admin()) {
- $results = SessionManager::get_sessions_list(
- [
- 's.name' => ['operator' => 'like', 'value' => "%".$_REQUEST['q']."%"],
- 'c.id' => ['operator' => '=', 'value' => $_REQUEST['course_id']],
- ]
- );
- $json = [
- 'items' => [
- ['id' => 'T', 'text' => get_lang('All')],
- ],
- ];
- if (!empty($results)) {
- foreach ($results as $item) {
- $item2 = [];
- foreach ($item as $id => $internal) {
- if ($id == 'id') {
- $item2[$id] = $internal;
- }
- if ($id == 'name') {
- $item2['text'] = $internal;
- }
- }
- $json['items'][] = $item2;
- }
- }
- echo json_encode($json);
- }
- break;
- case 'session_info':
- $sessionId = isset($_GET['session_id']) ? $_GET['session_id'] : '';
- $sessionInfo = api_get_session_info($sessionId);
- $extraFieldValues = new ExtraFieldValue('session');
- $extraField = new ExtraField('session');
- $values = $extraFieldValues->getAllValuesByItem($sessionId);
- $load = isset($_GET['load_empty_extra_fields']) ? true : false;
- if ($load) {
- $allExtraFields = $extraField->get_all();
- $valueList = array_column($values, 'id');
- foreach ($allExtraFields as $extra) {
- if (!in_array($extra['id'], $valueList)) {
- $values[] = [
- 'id' => $extra['id'],
- 'variable' => $extra['variable'],
- 'value' => '',
- 'field_type' => $extra['field_type'],
- ];
- }
- }
- }
- $sessionInfo['extra_fields'] = $values;
- if (!empty($sessionInfo)) {
- echo json_encode($sessionInfo);
- }
- break;
- case 'get_description':
- if (isset($_GET['session'])) {
- $sessionInfo = api_get_session_info($_GET['session']);
- echo '<h2>'.$sessionInfo['name'].'</h2>';
- echo '<div class="home-course-intro"><div class="page-course"><div class="page-course-intro">';
- echo $sessionInfo['show_description'] == 1 ? $sessionInfo['description'] : get_lang('none');
- echo '</div></div></div>';
- }
- break;
- case 'search_general_coach':
- SessionManager::protectSession(null, false);
- api_protect_limit_for_session_admin();
- if (api_is_anonymous()) {
- echo '';
- break;
- }
- $list = [
- 'items' => [],
- ];
- $usersRepo = UserManager::getRepository();
- $users = $usersRepo->searchUsersByStatus($_GET['q'], COURSEMANAGER, api_get_current_access_url_id());
- /** @var User $user */
- foreach ($users as $user) {
- $list['items'][] = [
- 'id' => $user->getId(),
- 'text' => UserManager::formatUserFullName($user),
- ];
- }
- header('Content-Type: application/json');
- echo json_encode($list);
- break;
- case 'get_courses_inside_session':
- $userId = api_get_user_id();
- $isAdmin = api_is_platform_admin();
- if ($isAdmin) {
- $sessionList = SessionManager::get_sessions_list();
- $sessionIdList = array_column($sessionList, 'id');
- } else {
- $sessionList = SessionManager::get_sessions_by_user($userId);
- $sessionIdList = array_column($sessionList, 'session_id');
- }
- $sessionId = isset($_GET['session_id']) ? (int) $_GET['session_id'] : 0;
- $courseList = [];
- if (empty($sessionId)) {
- $preCourseList = CourseManager::get_courses_list_by_user_id(
- $userId,
- false,
- true
- );
- $courseList = array_column($preCourseList, 'real_id');
- } else {
- if ($isAdmin) {
- $courseList = SessionManager::getCoursesInSession($sessionId);
- } else {
- if (in_array($sessionId, $sessionIdList)) {
- $courseList = SessionManager::getCoursesInSession($sessionId);
- }
- }
- }
- $courseListToSelect = [];
- if (!empty($courseList)) {
- // Course List
- foreach ($courseList as $courseId) {
- $courseInfo = api_get_course_info_by_id($courseId);
- $courseListToSelect[] = [
- 'id' => $courseInfo['real_id'],
- 'name' => $courseInfo['title'],
- ];
- }
- }
- echo json_encode($courseListToSelect);
- break;
- case 'get_basic_course_documents_list':
- case 'get_basic_course_documents_form':
- $courseId = isset($_GET['course']) ? (int) $_GET['course'] : 0;
- $sessionId = isset($_GET['session']) ? (int) $_GET['session'] : 0;
- $currentUserId = api_get_user_id();
- $em = Database::getManager();
- $course = $em->find('ChamiloCoreBundle:Course', $courseId);
- $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
- if (!$course || !$session) {
- break;
- }
- if (!api_is_platform_admin(true) || $session->getSessionAdminId() != $currentUserId) {
- break;
- }
- $folderName = '/basic-course-documents__'.$session->getId().'__0';
- if ('get_basic_course_documents_list' === $action) {
- $courseInfo = api_get_course_info_by_id($course->getId());
- $exists = DocumentManager::folderExists('/basic-course-documents', $courseInfo, $session->getId(), 0);
- if (!$exists) {
- $courseDir = $courseInfo['directory'].'/document';
- $sysCoursePath = api_get_path(SYS_COURSE_PATH);
- $baseWorkDir = $sysCoursePath.$courseDir;
- $newFolderData = create_unexisting_directory(
- $courseInfo,
- $currentUserId,
- $session->getId(),
- 0,
- 0,
- $baseWorkDir,
- '/basic-course-documents',
- get_lang('Basic course documents'),
- 1
- );
- $id = (int) $newFolderData['iid'];
- } else {
- $id = DocumentManager::get_document_id($courseInfo, $folderName, $session->getId());
- }
- $http_www = api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/document';
- $documentAndFolders = DocumentManager::getAllDocumentData(
- $courseInfo,
- $folderName,
- 0,
- 0,
- false,
- false,
- $session->getId()
- );
- $documentAndFolders = array_filter(
- $documentAndFolders,
- function (array $documentData) {
- return $documentData['filetype'] != 'folder';
- }
- );
- $documentAndFolders = array_map(
- function (array $documentData) use ($course, $session, $courseInfo, $currentUserId, $http_www, $folderName, $id) {
- $downloadUrl = api_get_path(WEB_CODE_PATH).'document/document.php?'
- .api_get_cidreq_params($course->getCode(), $session->getId()).'&'
- .http_build_query(['action' => 'download', 'id' => $documentData['id']]);
- $deleteUrl = api_get_path(WEB_AJAX_PATH).'session.ajax.php?'
- .http_build_query(
- [
- 'a' => 'delete_basic_course_documents',
- 'deleteid' => $documentData['id'],
- 'curdirpath' => $folderName,
- 'course' => $course->getId(),
- 'session' => $session->getId(),
- ]
- );
- $row = [];
- $row[] = DocumentManager::build_document_icon_tag($documentData['filetype'], $documentData['path']);
- $row[] = Display::url($documentData['title'], $downloadUrl);
- $row[] = format_file_size($documentData['size']);
- $row[] = date_to_str_ago($documentData['lastedit_date']).PHP_EOL
- .'<div class="muted"><small>'
- .api_get_local_time($documentData['lastedit_date'])
- ."</small></div>";
- $row[] = Display::url(
- Display::return_icon('save.png', get_lang('Download')),
- $downloadUrl
- )
- .PHP_EOL
- .Display::url(
- Display::return_icon('delete.png', get_lang('Delete')),
- $deleteUrl,
- [
- 'class' => 'delete_document',
- 'data-course' => $course->getId(),
- 'data-session' => $session->getId(),
- ]
- );
- return $row;
- },
- $documentAndFolders
- );
- $table = new SortableTableFromArray($documentAndFolders, 1, count($documentAndFolders));
- $table->set_header(0, get_lang('Type'), false, [], ['class' => 'text-center', 'width' => '60px']);
- $table->set_header(1, get_lang('Name'), false);
- $table->set_header(2, get_lang('Size'), false, [], ['class' => 'text-right', 'style' => 'width: 80px;']);
- $table->set_header(3, get_lang('Date'), false, [], ['class' => 'text-center', 'style' => 'width: 200px;']);
- $table->set_header(4, get_lang('Detail'), false, [], ['class' => 'text-center']);
- $table->display();
- }
- if ('get_basic_course_documents_form' === $action) {
- $form = new FormValidator('get_basic_course_documents_form_'.$session->getId());
- $form->addMultipleUpload(
- api_get_path(WEB_AJAX_PATH).'document.ajax.php?'
- .api_get_cidreq_params($course->getCode(), $session->getId())
- .'&a=upload_file&curdirpath='.$folderName,
- ''
- );
- $form->display();
- }
- break;
- case 'delete_basic_course_documents':
- $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
- $docId = isset($_GET['deleteid']) ? (int) $_GET['deleteid'] : 0;
- $courseId = isset($_GET['course']) ? (int) $_GET['course'] : 0;
- $sessionId = isset($_GET['session']) ? (int) $_GET['session'] : 0;
- if (empty($curdirpath) || empty($docId) || empty($courseId) || empty($sessionId)) {
- break;
- }
- $em = Database::getManager();
- $courseInfo = api_get_course_info_by_id($courseId);
- $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
- $currentUserId = api_get_user_id();
- if (empty($courseInfo) || !$session) {
- break;
- }
- if (!api_is_platform_admin(true) || $session->getSessionAdminId() != $currentUserId) {
- break;
- }
- $sysCoursePath = api_get_path(SYS_COURSE_PATH);
- $courseDir = $courseInfo['directory'].'/document';
- $baseWorkDir = $sysCoursePath.$courseDir;
- $documentInfo = DocumentManager::get_document_data_by_id(
- $docId,
- $courseInfo['code'],
- false,
- $session->getId()
- );
- if (empty($documentInfo)) {
- break;
- }
- if ($documentInfo['filetype'] != 'link') {
- $deletedDocument = DocumentManager::delete_document(
- $courseInfo,
- null,
- $baseWorkDir,
- $session->getId(),
- $docId
- );
- } else {
- $deletedDocument = DocumentManager::deleteCloudLink(
- $courseInfo,
- $docId
- );
- }
- if (!$deletedDocument) {
- break;
- }
- echo true;
- break;
- default:
- echo '';
- }
- exit;
|