edit.php 10 KB

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