upload.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. use ChamiloSession as Session;
  3. $language_file = array('exercice', 'work', 'document', 'admin', 'gradebook');
  4. require_once '../inc/global.inc.php';
  5. $current_course_tool = TOOL_STUDENTPUBLICATION;
  6. /* Configuration settings */
  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. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  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. allowOnlySubscribedUser($user_id, $work_id, $course_id);
  28. $parent_data = $my_folder_data = get_work_data_by_id($work_id);
  29. if (empty($parent_data)) {
  30. api_not_allowed(true);
  31. }
  32. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code, $session_id);
  33. $is_course_member = $is_course_member || api_is_platform_admin();
  34. if ($is_course_member == false) {
  35. api_not_allowed(true);
  36. }
  37. $check = Security::check_token('post');
  38. $token = Security::get_token();
  39. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  40. $has_ended = false;
  41. $is_author = false;
  42. $parent_data['qualification'] = intval($parent_data['qualification']);
  43. // @todo add an option to allow/block multiple attempts.
  44. /*
  45. if (!empty($parent_data) && !empty($parent_data['qualification'])) {
  46. $count = get_work_count_by_student($user_id, $work_id);
  47. if ($count >= 1) {
  48. Display::display_header();
  49. if (api_get_course_setting('student_delete_own_publication') == '1') {
  50. Display::display_warning_message(get_lang('CantUploadDeleteYourPaperFirst'));
  51. } else {
  52. Display::display_warning_message(get_lang('YouAlreadySentAPaperYouCantUpload'));
  53. }
  54. Display::display_footer();
  55. exit;
  56. }
  57. }*/
  58. $has_expired = false;
  59. $has_ended = false;
  60. $message = null;
  61. if (!empty($my_folder_data)) {
  62. $homework = get_work_assignment_by_id($my_folder_data['id']);
  63. if ($homework['expires_on'] != '0000-00-00 00:00:00' || $homework['ends_on'] != '0000-00-00 00:00:00') {
  64. $time_now = time();
  65. if (!empty($homework['expires_on']) && $homework['expires_on'] != '0000-00-00 00:00:00') {
  66. $time_expires = api_strtotime($homework['expires_on'], 'UTC');
  67. $difference = $time_expires - $time_now;
  68. if ($difference < 0) {
  69. $has_expired = true;
  70. }
  71. }
  72. if (empty($homework['expires_on']) || $homework['expires_on'] == '0000-00-00 00:00:00') {
  73. $has_expired = false;
  74. }
  75. if (!empty($homework['ends_on']) && $homework['ends_on'] != '0000-00-00 00:00:00') {
  76. $time_ends = api_strtotime($homework['ends_on'], 'UTC');
  77. $difference2 = $time_ends - $time_now;
  78. if ($difference2 < 0) {
  79. $has_ended = true;
  80. }
  81. }
  82. $ends_on = api_convert_and_format_date($homework['ends_on']);
  83. $expires_on = api_convert_and_format_date($homework['expires_on']);
  84. }
  85. if ($has_ended) {
  86. $message = Display::return_message(get_lang('EndDateAlreadyPassed').' '.$ends_on, 'error');
  87. } elseif ($has_expired) {
  88. $message = Display::return_message(get_lang('ExpiryDateAlreadyPassed').' '.$expires_on, 'warning');
  89. } else {
  90. if ($has_expired) {
  91. $message = Display::return_message(get_lang('ExpiryDateToSendWorkIs').' '.$expires_on);
  92. }
  93. }
  94. }
  95. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(), 'name' => get_lang('StudentPublications'));
  96. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id, 'name' => $parent_data['title']);
  97. // form title
  98. $form_title = get_lang('UploadADocument');
  99. $interbreadcrumb[] = array('url' => '#', 'name' => $form_title);
  100. $form = new FormValidator('form', 'POST', api_get_self()."?".api_get_cidreq()."&id=".$work_id."&gradebook=".Security::remove_XSS($_GET['gradebook'])."&origin=$origin", '', array('enctype' => "multipart/form-data"));
  101. $form->addElement('header', $form_title);
  102. $show_progress_bar = false;
  103. if ($submitGroupWorkUrl) {
  104. // For user coming from group space to publish his work
  105. $realUrl = str_replace($_configuration['root_sys'], api_get_path(WEB_PATH), str_replace("\\", '/', realpath($submitGroupWorkUrl)));
  106. $form->addElement('hidden', 'newWorkUrl', $submitGroupWorkUrl);
  107. $text_document = $form->addElement('text', 'document', get_lang('Document'));
  108. $defaults['document'] = '<a href="' . format_url($submitGroupWorkUrl) . '">' . $realUrl . '</a>';
  109. $text_document->freeze();
  110. } else {
  111. // else standard upload option
  112. $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
  113. $show_progress_bar = true;
  114. }
  115. $form->addElement('hidden', 'id', $work_id);
  116. $form->addElement('hidden', 'contains_file', 0, array('id'=>'contains_file_id'));
  117. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'file_upload', 'class' => 'span4'));
  118. $form->add_html_editor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
  119. $form->addElement('hidden', 'active', 1);
  120. $form->addElement('hidden', 'accepted', 1);
  121. $form->addElement('hidden', 'sec_token', $token);
  122. $text = get_lang('Send');
  123. $class = 'upload';
  124. // fix the Ok button when we see the tool in the learn path
  125. if ($origin == 'learnpath') {
  126. $form->addElement('html', '<div style="margin-left:137px">');
  127. $form->addElement('style_submit_button', 'submitWork', $text, array('class'=> $class, 'value' => "submitWork"));
  128. $form->addElement('html', '</div>');
  129. } else {
  130. $form->addElement('style_submit_button', 'submitWork', $text, array('class'=> $class, 'value' => "submitWork"));
  131. }
  132. if (!empty($_POST['submitWork']) || $item_id) {
  133. $form->addElement('style_submit_button', 'cancelForm', get_lang('Cancel'), 'class="cancel"');
  134. }
  135. if ($show_progress_bar) {
  136. $form->add_real_progress_bar('uploadWork', 'file');
  137. }
  138. $documentTemplateData = getDocumentTemplateFromWork($work_id, $course_info);
  139. if (!empty($documentTemplateData)) {
  140. $defaults['title'] = $userInfo['complete_name'].'_'.$documentTemplateData['title'].'_'.substr(api_get_utc_datetime(), 0, 10);
  141. $defaults['description'] = $documentTemplateData['file_content'];
  142. }
  143. $form->setDefaults($defaults);
  144. $error_message = null;
  145. $_course = api_get_course_info();
  146. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$_course['path'] . '/';
  147. $succeed = false;
  148. if ($form->validate()) {
  149. if ($student_can_edit_in_session && $check) {
  150. // Check the token inserted into the form
  151. if (isset($_POST['submitWork'])) {
  152. $url = null;
  153. $contains_file = 0;
  154. $title = isset($_POST['title']) ? $_POST['title'] : null;
  155. $description = isset($_POST['description']) ? $_POST['description'] : null;
  156. if ($_POST['contains_file'] && !empty($_FILES['file']['size'])) {
  157. $updir = $currentCourseRepositorySys.'work/'; //directory path to upload
  158. // Try to add an extension to the file if it has'nt one
  159. $new_file_name = add_ext_on_mime(stripslashes($_FILES['file']['name']), $_FILES['file']['type']);
  160. // Replace dangerous characters
  161. $new_file_name = replace_dangerous_char($new_file_name, 'strict');
  162. // Transform any .php file in .phps fo security
  163. $new_file_name = php2phps($new_file_name);
  164. $filesize = filesize($_FILES['file']['tmp_name']);
  165. if (empty($filesize)) {
  166. $error_message .= Display :: return_message(get_lang('UplUploadFailedSizeIsZero'), 'error');
  167. $succeed = false;
  168. } elseif (!filter_extension($new_file_name)) {
  169. //filter extension
  170. $error_message .= Display :: return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
  171. $succeed = false;
  172. }
  173. if (!$title) {
  174. $title = $_FILES['file']['name'];
  175. }
  176. // Compose a unique file name to avoid any conflict
  177. $new_file_name = api_get_unique_id();
  178. $curdirpath = basename($my_folder_data['url']);
  179. // If we come from the group tools the groupid will be saved in $work_table
  180. $result = move_uploaded_file($_FILES['file']['tmp_name'], $updir.$curdirpath.'/'.$new_file_name);
  181. if ($result) {
  182. $url = 'work/'.$curdirpath.'/'.$new_file_name;
  183. $contains_file = 1;
  184. }
  185. }
  186. if (empty($title)) {
  187. $title = get_lang('Untitled');
  188. }
  189. $documents_total_space = DocumentManager::documents_total_space();
  190. $course_max_space = DocumentManager::get_course_quota();
  191. $total_size = $filesize + $documents_total_space;
  192. if ($total_size > $course_max_space) {
  193. $error_message .= Display::return_message(get_lang('NoSpace'), 'warning');
  194. } else {
  195. $active = '1';
  196. $sql_add_publication = "INSERT INTO ".$work_table." SET
  197. c_id = $course_id ,
  198. url = '".$url . "',
  199. title = '".Database::escape_string($title)."',
  200. description = '".Database::escape_string($description)."',
  201. contains_file = '".$contains_file."',
  202. active = '" . $active."',
  203. accepted = '1',
  204. post_group_id = '".$group_id."',
  205. sent_date = '".api_get_utc_datetime()."',
  206. parent_id = '".$work_id."' ,
  207. session_id = '".intval($id_session)."' ,
  208. user_id = '".$user_id."'";
  209. Database::query($sql_add_publication);
  210. $id = Database::insert_id();
  211. }
  212. if ($id) {
  213. api_item_property_update($course_info, 'work', $id, 'DocumentAdded', $user_id, api_get_group_id());
  214. $succeed = true;
  215. }
  216. } else {
  217. $error_message .= Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  218. }
  219. Security::clear_token();
  220. } else {
  221. //Bad token or can't add works
  222. $error_message = Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  223. }
  224. if (!empty($succeed) && !empty($id)) {
  225. //last value is to check this is not "just" an edit
  226. //YW Tis part serve to send a e-mail to the tutors when a new file is sent
  227. $send = api_get_course_setting('email_alert_manager_on_new_doc');
  228. if ($send > 0) {
  229. // Lets predefine some variables. Be sure to change the from address!
  230. if (empty($id_session)) {
  231. //Teachers
  232. $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), null, null, null, COURSEMANAGER);
  233. } else {
  234. //Coaches
  235. $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), $session_id, null, null, 2);
  236. }
  237. $subject = "[" . api_get_setting('siteName') . "] ".get_lang('SendMailBody')."\n".get_lang('CourseName')." : ".$_course['name']." ";
  238. foreach ($user_list as $user_data) {
  239. $to_user_id = $user_data['user_id'];
  240. $emailbody = get_lang('SendMailBody')."\n".get_lang('CourseName')." : ".$_course['name']."\n";
  241. $user_info = api_get_user_info($user_id);
  242. $emailbody .= get_lang('UserName')." : ".api_get_person_name($user_info['firstname'], $user_info['lastname'])."\n";
  243. $emailbody .= get_lang('DateSent')." : ".api_format_date(api_get_local_time())."\n";
  244. $emailbody .= get_lang('WorkName')." : ".$title."\n\n".get_lang('DownloadLink')."\n";
  245. $url = api_get_path(WEB_CODE_PATH)."work/work.php?".api_get_cidreq()."&amp;id=".$work_id;
  246. $emailbody .= $url;
  247. MessageManager::send_message_simple($to_user_id, $subject, $emailbody);
  248. }
  249. }
  250. $message = get_lang('DocAdd');
  251. event_upload($id);
  252. $error_message .= Display::return_message(get_lang('DocAdd'));
  253. $script = 'work_list.php';
  254. if ($is_allowed_to_edit) {
  255. $script = 'work_list_all.php';
  256. }
  257. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id.'&error_message='.$error_message);
  258. exit;
  259. }
  260. }
  261. $htmlHeadXtra[] = to_javascript_work();
  262. Display :: display_header(null);
  263. if (!empty($work_id)) {
  264. echo $message;
  265. if ($is_allowed_to_edit) {
  266. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  267. echo Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
  268. } else {
  269. $form->display();
  270. }
  271. } elseif ($is_author) {
  272. if (empty($work_item['qualificator_id']) || $work_item['qualificator_id'] == 0) {
  273. $form->display();
  274. } else {
  275. Display::display_error_message(get_lang('ActionNotAllowed'));
  276. }
  277. } elseif ($student_can_edit_in_session && $has_ended == false) {
  278. $form->display();
  279. } else {
  280. Display::display_error_message(get_lang('ActionNotAllowed'));
  281. }
  282. } else {
  283. Display::display_error_message(get_lang('ActionNotAllowed'));
  284. }
  285. Display :: display_footer();