edit.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* Configuration settings */
  8. api_protect_course_script(true);
  9. // Including necessary files
  10. require_once 'work.lib.php';
  11. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  14. $this_section = SECTION_COURSES;
  15. $work_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  16. $item_id = isset($_REQUEST['item_id']) ? intval($_REQUEST['item_id']) : null;
  17. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  18. $is_allowed_to_edit = api_is_allowed_to_edit();
  19. $course_id = api_get_course_int_id();
  20. $user_id = api_get_user_id();
  21. $session_id = api_get_session_id();
  22. $course_code = api_get_course_id();
  23. $course_info = api_get_course_info();
  24. if (empty($work_id) || empty($item_id)) {
  25. api_not_allowed(true);
  26. }
  27. $parent_data = $my_folder_data = get_work_data_by_id($work_id);
  28. if (empty($parent_data)) {
  29. api_not_allowed(true);
  30. }
  31. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code, $session_id);
  32. $is_course_member = $is_course_member || api_is_platform_admin();
  33. if ($is_course_member == false) {
  34. api_not_allowed(true);
  35. }
  36. $check = Security::check_token('post');
  37. $token = Security::get_token();
  38. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  39. $has_ended = false;
  40. $is_author = false;
  41. $work_item = get_work_data_by_id($item_id);
  42. // Get the author ID for that document from the item_property table
  43. $is_author = user_is_author($item_id);
  44. if (!$is_author) {
  45. api_not_allowed(true);
  46. }
  47. // Student's can't edit work only if he can delete his docs.
  48. if (!api_is_allowed_to_edit()) {
  49. if (api_get_course_setting('student_delete_own_publication') != 1) {
  50. api_not_allowed(true);
  51. }
  52. }
  53. if (!empty($my_folder_data)) {
  54. $homework = get_work_assignment_by_id($my_folder_data['id']);
  55. if ($homework['expires_on'] != '0000-00-00 00:00:00' || $homework['ends_on'] != '0000-00-00 00:00:00') {
  56. $time_now = time();
  57. if (!empty($homework['expires_on']) && $homework['expires_on'] != '0000-00-00 00:00:00') {
  58. $time_expires = api_strtotime($homework['expires_on'], 'UTC');
  59. $difference = $time_expires - $time_now;
  60. if ($difference < 0) {
  61. $has_expired = true;
  62. }
  63. }
  64. if (empty($homework['expires_on']) || $homework['expires_on'] == '0000-00-00 00:00:00') {
  65. $has_expired = false;
  66. }
  67. if (!empty($homework['ends_on']) && $homework['ends_on'] != '0000-00-00 00:00:00') {
  68. $time_ends = api_strtotime($homework['ends_on'], 'UTC');
  69. $difference2 = $time_ends - $time_now;
  70. if ($difference2 < 0) {
  71. $has_ended = true;
  72. }
  73. }
  74. $ends_on = api_convert_and_format_date($homework['ends_on']);
  75. $expires_on = api_convert_and_format_date($homework['expires_on']);
  76. }
  77. }
  78. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(), 'name' => get_lang('StudentPublications'));
  79. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id, 'name' => $parent_data['title']);
  80. // form title
  81. $form_title = get_lang('Edit');
  82. $interbreadcrumb[] = array('url' => '#', 'name' => $form_title);
  83. $form = new FormValidator(
  84. 'form',
  85. 'POST',
  86. api_get_self()."?".api_get_cidreq()."&id=".$work_id."&gradebook=".Security::remove_XSS($_GET['gradebook'])."&origin=$origin",
  87. '',
  88. array('enctype' => "multipart/form-data")
  89. );
  90. $form->addElement('header', $form_title);
  91. $show_progress_bar = false;
  92. if ($submitGroupWorkUrl) {
  93. // For user coming from group space to publish his work
  94. $realUrl = str_replace($_configuration['root_sys'], api_get_path(WEB_PATH), str_replace("\\", '/', realpath($submitGroupWorkUrl)));
  95. $form->addElement('hidden', 'newWorkUrl', $submitGroupWorkUrl);
  96. $text_document = $form->addElement('text', 'document', get_lang('Document'));
  97. $defaults['document'] = '<a href="' . format_url($submitGroupWorkUrl) . '">' . $realUrl . '</a>';
  98. $text_document->freeze();
  99. } elseif ($item_id && ($is_allowed_to_edit or $is_author)) {
  100. $workUrl = $currentCourseRepositoryWeb . $workUrl;
  101. }
  102. $form->addElement('hidden', 'id', $work_id);
  103. $form->addElement('hidden', 'item_id', $item_id);
  104. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'file_upload', 'class' => 'span4'));
  105. $form->add_html_editor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
  106. $defaults['title'] = $work_item['title'];
  107. $defaults["description"] = $work_item['description'];
  108. $defaults['qualification'] = $work_item['qualification'];
  109. if ($is_allowed_to_edit && !empty($item_id)) {
  110. // Get qualification from parent_id that'll allow the validation qualification over
  111. $sql = "SELECT qualification FROM $work_table WHERE c_id = $course_id AND id ='$work_id' ";
  112. $result = Database::query($sql);
  113. $row = Database::fetch_array($result);
  114. $qualification_over = $row['qualification'];
  115. if (!empty($qualification_over) && intval($qualification_over) > 0) {
  116. $form->addElement('text', 'qualification', array(get_lang('Qualification'), null, " / ".$qualification_over), 'size="10"');
  117. $form->addElement('hidden', 'qualification_over', $qualification_over);
  118. }
  119. }
  120. $form->addElement('hidden', 'active', 1);
  121. $form->addElement('hidden', 'accepted', 1);
  122. $form->addElement('hidden', 'item_to_edit', $item_id);
  123. $form->addElement('hidden', 'sec_token', $token);
  124. $text = get_lang('UpdateWork');
  125. $class = 'save';
  126. // fix the Ok button when we see the tool in the learn path
  127. $form->addElement('style_submit_button', 'editWork', $text, array('class'=> $class, 'value' => "editWork"));
  128. $form->setDefaults($defaults);
  129. $error_message = null;
  130. $_course = api_get_course_info();
  131. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$_course['path'] . '/';
  132. $succeed = false;
  133. if ($form->validate()) {
  134. if ($student_can_edit_in_session && $check) {
  135. if (isset($_POST['editWork'])) {
  136. /*
  137. * SPECIAL CASE ! For a work edited
  138. */
  139. //Get the author ID for that document from the item_property table
  140. $item_to_edit_id = intval($_POST['item_to_edit']);
  141. $is_author = user_is_author($item_to_edit_id);
  142. if ($is_author) {
  143. $work_data = get_work_data_by_id($item_to_edit_id);
  144. if (!empty($_POST['title'])) {
  145. $title = isset($_POST['title']) ? $_POST['title'] : $work_data['title'];
  146. }
  147. $description = isset($_POST['description']) ? $_POST['description'] : $work_data['description'];
  148. if ($is_allowed_to_edit && ($_POST['qualification'] !='' )) {
  149. $add_to_update = ', qualificator_id ='."'".api_get_user_id()."', ";
  150. $add_to_update .= ' qualification = '."'".Database::escape_string($_POST['qualification'])."',";
  151. $add_to_update .= ' date_of_qualification = '."'".api_get_utc_datetime()."'";
  152. }
  153. if ($_POST['qualification'] > $_POST['qualification_over']) {
  154. Display::display_error_message(get_lang('QualificationMustNotBeMoreThanQualificationOver'));
  155. } else {
  156. $sql = "UPDATE " . $work_table . "
  157. SET title = '".Database::escape_string($title)."',
  158. description = '".Database::escape_string($description)."'
  159. ".$add_to_update."
  160. WHERE c_id = $course_id AND id = $item_to_edit_id";
  161. Database::query($sql);
  162. }
  163. api_item_property_update($_course, 'work', $item_to_edit_id, 'DocumentUpdated', $user_id);
  164. $succeed = true;
  165. $error_message .= Display::return_message(get_lang('ItemUpdated'), false);
  166. } else {
  167. $error_message .= Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  168. }
  169. } else {
  170. $error_message .= Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  171. }
  172. Security::clear_token();
  173. } else {
  174. // Bad token or can't add works
  175. $error_message = Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error');
  176. }
  177. $script = 'work_list.php';
  178. if ($is_allowed_to_edit) {
  179. $script = 'work_list_all.php';
  180. }
  181. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id.'&error_message='.$error_message);
  182. exit;
  183. }
  184. $htmlHeadXtra[] = to_javascript_work();
  185. Display :: display_header(null);
  186. if (!empty($work_id)) {
  187. if ($is_allowed_to_edit) {
  188. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  189. echo Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
  190. } else {
  191. $form->display();
  192. }
  193. } elseif ($is_author) {
  194. if (empty($work_item['qualificator_id']) || $work_item['qualificator_id'] == 0) {
  195. $form->display();
  196. } else {
  197. Display::display_error_message(get_lang('ActionNotAllowed'));
  198. }
  199. } elseif ($student_can_edit_in_session && $has_ended == false) {
  200. $form->display();
  201. } else {
  202. Display::display_error_message(get_lang('ActionNotAllowed'));
  203. }
  204. } else {
  205. Display::display_error_message(get_lang('ActionNotAllowed'));
  206. }
  207. Display :: display_footer();