add_document.php 4.2 KB

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