add_document.php 4.1 KB

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