edit.php 11 KB

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