upload_from_template.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $current_course_tool = TOOL_STUDENTPUBLICATION;
  7. api_protect_course_script(true);
  8. // Including necessary files
  9. require_once 'work.lib.php';
  10. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  11. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  13. $this_section = SECTION_COURSES;
  14. $work_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  15. $documentId = isset($_REQUEST['document_id']) ? intval($_REQUEST['document_id']) : null;
  16. $is_allowed_to_edit = api_is_allowed_to_edit();
  17. $course_id = api_get_course_int_id();
  18. $user_id = api_get_user_id();
  19. $userInfo = api_get_user_info();
  20. $session_id = api_get_session_id();
  21. $course_code = api_get_course_id();
  22. $course_info = api_get_course_info();
  23. $group_id = api_get_group_id();
  24. if (empty($work_id)) {
  25. api_not_allowed(true);
  26. }
  27. $workInfo = get_work_data_by_id($work_id);
  28. if (empty($workInfo)) {
  29. api_not_allowed(true);
  30. }
  31. allowOnlySubscribedUser($user_id, $work_id, $course_id);
  32. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  33. $user_id,
  34. $course_code,
  35. $session_id
  36. );
  37. $is_course_member = $is_course_member || api_is_platform_admin();
  38. if ($is_course_member == false) {
  39. api_not_allowed(true);
  40. }
  41. $check = Security::check_token('post');
  42. $token = Security::get_token();
  43. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  44. $homework = get_work_assignment_by_id($workInfo['id']);
  45. $validationStatus = getWorkDateValidationStatus($homework);
  46. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(), 'name' => get_lang('StudentPublications'));
  47. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id, 'name' => $workInfo['title']);
  48. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UploadFromTemplate'));
  49. $form = new FormValidator('form', 'POST', api_get_self()."?".api_get_cidreq()."&id=".$work_id, '', array('enctype' => "multipart/form-data"));
  50. setWorkUploadForm($form, $workInfo['allow_text_assignment']);
  51. $form->addElement('hidden', 'document_id', $documentId);
  52. $form->addElement('hidden', 'id', $work_id);
  53. $form->addElement('hidden', 'sec_token', $token);
  54. $documentTemplateData = getDocumentTemplateFromWork($work_id, $course_info, $documentId);
  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($workInfo, $values, $course_info, $id_session, $group_id, $user_id);
  67. $script = 'work_list.php';
  68. if ($is_allowed_to_edit) {
  69. $script = 'work_list_all.php';
  70. }
  71. if (!empty($error_message)) {
  72. Session::write('error_message', $error_message);
  73. }
  74. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  75. exit;
  76. } else {
  77. // Bad token or can't add works
  78. $error_message = Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  79. }
  80. }
  81. $htmlHeadXtra[] = to_javascript_work();
  82. Display :: display_header(null);
  83. if (!empty($work_id)) {
  84. echo $validationStatus['message'];
  85. if ($is_allowed_to_edit) {
  86. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  87. echo Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
  88. } else {
  89. $form->display();
  90. }
  91. } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) {
  92. $form->display();
  93. } else {
  94. Display::display_error_message(get_lang('ActionNotAllowed'));
  95. }
  96. } else {
  97. Display::display_error_message(get_lang('ActionNotAllowed'));
  98. }
  99. Display :: display_footer();