upload_from_template.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. require_once '../inc/global.inc.php';
  5. $current_course_tool = TOOL_STUDENTPUBLICATION;
  6. api_protect_course_script(true);
  7. // Including necessary files
  8. require_once 'work.lib.php';
  9. $this_section = SECTION_COURSES;
  10. $work_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  11. $documentId = isset($_REQUEST['document_id']) ? intval($_REQUEST['document_id']) : null;
  12. $is_allowed_to_edit = api_is_allowed_to_edit();
  13. $course_id = api_get_course_int_id();
  14. $user_id = api_get_user_id();
  15. $userInfo = api_get_user_info();
  16. $session_id = api_get_session_id();
  17. $course_info = api_get_course_info();
  18. $course_code = $course_info['code'];
  19. $group_id = api_get_group_id();
  20. if (empty($work_id)) {
  21. api_not_allowed(true);
  22. }
  23. protectWork($course_info, $work_id);
  24. $workInfo = get_work_data_by_id($work_id);
  25. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  26. $user_id,
  27. $course_id,
  28. $session_id
  29. );
  30. $is_course_member = $is_course_member || api_is_platform_admin();
  31. if ($is_course_member == false) {
  32. api_not_allowed(true);
  33. }
  34. $check = Security::check_token('post');
  35. $token = Security::get_token();
  36. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  37. $homework = get_work_assignment_by_id($workInfo['id']);
  38. $validationStatus = getWorkDateValidationStatus($homework);
  39. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(), 'name' => get_lang('StudentPublications'));
  40. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id, 'name' => $workInfo['title']);
  41. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UploadFromTemplate'));
  42. $form = new FormValidator(
  43. 'form',
  44. 'POST',
  45. api_get_self()."?".api_get_cidreq()."&id=".$work_id,
  46. '',
  47. array('enctype' => "multipart/form-data")
  48. );
  49. setWorkUploadForm($form, $workInfo['allow_text_assignment']);
  50. $form->addElement('hidden', 'document_id', $documentId);
  51. $form->addElement('hidden', 'id', $work_id);
  52. $form->addElement('hidden', 'sec_token', $token);
  53. $documentTemplateData = getDocumentTemplateFromWork($work_id, $course_info, $documentId);
  54. $defaults = [];
  55. if (!empty($documentTemplateData)) {
  56. $defaults['title'] = $userInfo['complete_name'].'_'.$documentTemplateData['title'].'_'.substr(api_get_utc_datetime(), 0, 10);
  57. $defaults['description'] = $documentTemplateData['file_content'];
  58. }
  59. $form->setDefaults($defaults);
  60. $error_message = null;
  61. $succeed = false;
  62. if ($form->validate()) {
  63. if ($student_can_edit_in_session && $check) {
  64. $values = $form->getSubmitValues();
  65. // Process work
  66. $error_message = processWorkForm(
  67. $workInfo,
  68. $values,
  69. $course_info,
  70. $id_session,
  71. $group_id,
  72. $user_id
  73. );
  74. $script = 'work_list.php';
  75. if ($is_allowed_to_edit) {
  76. $script = 'work_list_all.php';
  77. }
  78. if (!empty($error_message)) {
  79. Session::write('error_message', $error_message);
  80. }
  81. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  82. exit;
  83. } else {
  84. // Bad token or can't add works
  85. $error_message = Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  86. }
  87. }
  88. $htmlHeadXtra[] = to_javascript_work();
  89. Display :: display_header(null);
  90. if (!empty($work_id)) {
  91. echo $validationStatus['message'];
  92. if ($is_allowed_to_edit) {
  93. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  94. echo Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
  95. } else {
  96. $form->display();
  97. }
  98. } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) {
  99. $form->display();
  100. } else {
  101. Display::display_error_message(get_lang('ActionNotAllowed'));
  102. }
  103. } else {
  104. Display::display_error_message(get_lang('ActionNotAllowed'));
  105. }
  106. Display :: display_footer();