add_document.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. require_once 'work.lib.php';
  5. $current_course_tool = TOOL_STUDENTPUBLICATION;
  6. $workId = isset($_GET['id']) ? (int) $_GET['id'] : null;
  7. $docId = isset($_GET['document_id']) ? (int) $_GET['document_id'] : null;
  8. $action = isset($_GET['action']) ? $_GET['action'] : null;
  9. if (empty($workId)) {
  10. api_not_allowed(true);
  11. }
  12. $blockAddDocuments = api_get_configuration_value('block_student_publication_add_documents');
  13. if ($blockAddDocuments) {
  14. api_not_allowed(true);
  15. }
  16. $my_folder_data = get_work_data_by_id($workId);
  17. if (empty($my_folder_data)) {
  18. api_not_allowed(true);
  19. }
  20. $work_data = get_work_assignment_by_id($workId);
  21. if (!api_is_allowed_to_edit()) {
  22. api_not_allowed(true);
  23. }
  24. $courseInfo = api_get_course_info();
  25. $interbreadcrumb[] = [
  26. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  27. 'name' => get_lang('Assignments'),
  28. ];
  29. $interbreadcrumb[] = [
  30. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId,
  31. 'name' => $my_folder_data['title'],
  32. ];
  33. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add document')];
  34. switch ($action) {
  35. case 'delete':
  36. if (!empty($workId) && !empty($docId)) {
  37. deleteDocumentToWork($docId, $workId, api_get_course_int_id());
  38. $url = api_get_path(WEB_CODE_PATH).'work/add_document.php?id='.$workId.'&'.api_get_cidreq();
  39. Display::addFlash(Display::return_message(get_lang('Deleted'), 'success'));
  40. header('Location: '.$url);
  41. exit;
  42. }
  43. break;
  44. }
  45. if (empty($docId)) {
  46. Display::display_header(null);
  47. $documents = getAllDocumentToWork($workId, api_get_course_int_id());
  48. if (!empty($documents)) {
  49. echo Display::page_subheader(get_lang('Documents added'));
  50. echo '<div class="well">';
  51. $urlDocument = api_get_path(WEB_CODE_PATH).'work/add_document.php';
  52. foreach ($documents as $doc) {
  53. $documentId = $doc['document_id'];
  54. $docData = DocumentManager::get_document_data_by_id($documentId, $courseInfo['code']);
  55. if ($docData) {
  56. $url = $urlDocument.'?action=delete&id='.$workId.'&document_id='.$documentId.'&'.api_get_cidreq();
  57. $link = Display::url(get_lang('Remove'), $url, ['class' => 'btn btn-danger']);
  58. echo $docData['title'].' '.$link.'<br />';
  59. }
  60. }
  61. echo '</div>';
  62. }
  63. $documentTree = DocumentManager::get_document_preview(
  64. $courseInfo,
  65. false,
  66. null,
  67. api_get_session_id(),
  68. false,
  69. '/',
  70. api_get_path(WEB_CODE_PATH).'work/add_document.php?id='.$workId.'&'.api_get_cidreq(),
  71. false,
  72. false,
  73. false,
  74. false
  75. );
  76. echo Display::page_subheader(get_lang('Documents'));
  77. echo $documentTree;
  78. echo '<hr /><div class="clear"></div>';
  79. Display::display_footer();
  80. } else {
  81. $documentInfo = DocumentManager::get_document_data_by_id($docId, $courseInfo['code']);
  82. $url = api_get_path(WEB_CODE_PATH).'work/add_document.php?id='.$workId.'&document_id='.$docId.'&'.api_get_cidreq();
  83. $form = new FormValidator('add_doc', 'post', $url);
  84. $form->addElement('header', get_lang('Add document'));
  85. $form->addElement('hidden', 'add_doc', '1');
  86. $form->addElement('hidden', 'id', $workId);
  87. $form->addElement('hidden', 'document_id', $docId);
  88. $form->addElement('label', get_lang('File'), $documentInfo['title']);
  89. $form->addButtonCreate(get_lang('Add'));
  90. if ($form->validate()) {
  91. $values = $form->exportValues();
  92. $workId = $values['id'];
  93. $docId = $values['document_id'];
  94. $data = getDocumentToWork($docId, $workId, api_get_course_int_id());
  95. if (empty($data)) {
  96. addDocumentToWork($docId, $workId, api_get_course_int_id());
  97. Display::addFlash(Display::return_message(get_lang('Added'), 'success'));
  98. } else {
  99. Display::addFlash(Display::return_message(get_lang('Document already added'), 'warning'));
  100. }
  101. $url = api_get_path(WEB_CODE_PATH).'work/add_document.php?id='.$workId.'&'.api_get_cidreq();
  102. header('Location: '.$url);
  103. exit;
  104. }
  105. Display::display_header(null);
  106. $form->display();
  107. Display::display_footer();
  108. }