edit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $current_course_tool = TOOL_STUDENTPUBLICATION;
  5. api_protect_course_script(true);
  6. $blockEdition = api_get_configuration_value('block_student_publication_edition');
  7. if ($blockEdition && !api_is_platform_admin()) {
  8. api_not_allowed(true);
  9. }
  10. // Including files
  11. require_once 'work.lib.php';
  12. $this_section = SECTION_COURSES;
  13. $work_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  14. $item_id = isset($_REQUEST['item_id']) ? intval($_REQUEST['item_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. $session_id = api_get_session_id();
  20. $course_code = api_get_course_id();
  21. $course_info = api_get_course_info();
  22. if (empty($work_id) || empty($item_id)) {
  23. api_not_allowed(true);
  24. }
  25. $parent_data = $my_folder_data = get_work_data_by_id($work_id);
  26. if (empty($parent_data)) {
  27. api_not_allowed(true);
  28. }
  29. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  30. $user_id,
  31. $course_id,
  32. $session_id
  33. );
  34. $is_course_member = $is_course_member || api_is_platform_admin();
  35. if ($is_course_member == false) {
  36. api_not_allowed(true);
  37. }
  38. $check = Security::check_token('post');
  39. $token = Security::get_token();
  40. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  41. $has_ended = false;
  42. $is_author = false;
  43. $work_item = get_work_data_by_id($item_id);
  44. // Get the author ID for that document from the item_property table
  45. $is_author = user_is_author($item_id);
  46. if (!$is_author) {
  47. api_not_allowed(true);
  48. }
  49. // Student's can't edit work only if he can delete his docs.
  50. if (!api_is_allowed_to_edit()) {
  51. if (api_get_course_setting('student_delete_own_publication') != 1) {
  52. api_not_allowed(true);
  53. }
  54. }
  55. if (!empty($my_folder_data)) {
  56. $homework = get_work_assignment_by_id($my_folder_data['id']);
  57. if (!empty($homework['expires_on']) || !empty($homework['ends_on'])) {
  58. $time_now = time();
  59. if (!empty($homework['expires_on']) &&
  60. !empty($homework['expires_on'])
  61. ) {
  62. $time_expires = api_strtotime($homework['expires_on'], 'UTC');
  63. $difference = $time_expires - $time_now;
  64. if ($difference < 0) {
  65. $has_expired = true;
  66. }
  67. }
  68. if (empty($homework['expires_on'])) {
  69. $has_expired = false;
  70. }
  71. if (!empty($homework['ends_on'])) {
  72. $time_ends = api_strtotime($homework['ends_on'], 'UTC');
  73. $difference2 = $time_ends - $time_now;
  74. if ($difference2 < 0) {
  75. $has_ended = true;
  76. }
  77. }
  78. $ends_on = api_convert_and_format_date($homework['ends_on']);
  79. $expires_on = api_convert_and_format_date($homework['expires_on']);
  80. }
  81. }
  82. $interbreadcrumb[] = [
  83. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  84. 'name' => get_lang('StudentPublications'),
  85. ];
  86. if (api_is_allowed_to_edit()) {
  87. $interbreadcrumb[] = [
  88. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work_id,
  89. 'name' => $parent_data['title'],
  90. ];
  91. } else {
  92. $interbreadcrumb[] = [
  93. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id,
  94. 'name' => $parent_data['title'],
  95. ];
  96. }
  97. // form title
  98. $form_title = get_lang('Edit');
  99. $interbreadcrumb[] = ['url' => '#', 'name' => $form_title];
  100. $form = new FormValidator(
  101. 'form',
  102. 'POST',
  103. api_get_self()."?".api_get_cidreq()."&id=".$work_id,
  104. '',
  105. ['enctype' => "multipart/form-data"]
  106. );
  107. $form->addElement('header', $form_title);
  108. $show_progress_bar = false;
  109. $form->addElement('hidden', 'id', $work_id);
  110. $form->addElement('hidden', 'item_id', $item_id);
  111. $form->addText('title', get_lang('Title'), true, ['id' => 'file_upload']);
  112. if ($is_allowed_to_edit && !empty($item_id)) {
  113. $sql = "SELECT contains_file, url
  114. FROM $work_table
  115. WHERE c_id = $course_id AND id ='$item_id' ";
  116. $result = Database::query($sql);
  117. if ($result !== false && Database::num_rows($result) > 0) {
  118. $row = Database::fetch_array($result);
  119. if ($row['contains_file'] || !empty($row['url'])) {
  120. $form->addLabel(
  121. get_lang('Download'),
  122. '<a href="'.api_get_path(WEB_CODE_PATH).'work/download.php?id='.$item_id.'&'.api_get_cidreq().'">'.
  123. Display::return_icon('save.png', get_lang('Save'), [], ICON_SIZE_MEDIUM).'
  124. </a>'
  125. );
  126. }
  127. }
  128. }
  129. $form->addHtmlEditor(
  130. 'description',
  131. get_lang('Description'),
  132. false,
  133. false,
  134. getWorkDescriptionToolbar()
  135. );
  136. $defaults['title'] = $work_item['title'];
  137. $defaults["description"] = $work_item['description'];
  138. $defaults['qualification'] = $work_item['qualification'];
  139. if ($is_allowed_to_edit && !empty($item_id)) {
  140. // Get qualification from parent_id that will allow the validation qualification over
  141. /*$sql = "SELECT qualification FROM $work_table
  142. WHERE c_id = $course_id AND id ='$work_id' ";
  143. $result = Database::query($sql);
  144. $row = Database::fetch_array($result);
  145. $qualification_over = $row['qualification'];
  146. if (!empty($qualification_over) && intval($qualification_over) > 0) {
  147. $form->addText('qualification', array(get_lang('Qualification'), " / ".$qualification_over), false, 'size="10"');
  148. $form->addElement('hidden', 'qualification_over', $qualification_over);
  149. }*/
  150. $form->addCheckBox(
  151. 'send_email',
  152. null,
  153. get_lang('SendMailToStudent')
  154. );
  155. // Check if user to qualify has some DRHs
  156. $drhList = UserManager::getDrhListFromUser($work_item['user_id']);
  157. if (!empty($drhList)) {
  158. $form->addCheckBox(
  159. 'send_to_drh_users',
  160. null,
  161. get_lang('SendMailToHR')
  162. );
  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->addButtonUpdate($text);
  173. $form->setDefaults($defaults);
  174. $_course = api_get_course_info();
  175. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
  176. $succeed = false;
  177. if ($form->validate()) {
  178. if ($student_can_edit_in_session && $check) {
  179. /*
  180. * SPECIAL CASE ! For a work edited
  181. */
  182. //Get the author ID for that document from the item_property table
  183. $item_to_edit_id = intval($_POST['item_to_edit']);
  184. $is_author = user_is_author($item_to_edit_id);
  185. if ($is_author) {
  186. $work_data = get_work_data_by_id($item_to_edit_id);
  187. if (!empty($_POST['title'])) {
  188. $title = isset($_POST['title']) ? $_POST['title'] : $work_data['title'];
  189. }
  190. $description = isset($_POST['description']) ? $_POST['description'] : $work_data['description'];
  191. $add_to_update = null;
  192. if ($is_allowed_to_edit && ($_POST['qualification'] != '')) {
  193. /*$add_to_update = ', qualificator_id ='."'".api_get_user_id()."', ";
  194. $add_to_update .= ' qualification = '."'".api_float_val($_POST['qualification'])."',";
  195. $add_to_update .= ' date_of_qualification = '."'".api_get_utc_datetime()."'";*/
  196. if (isset($_POST['send_email'])) {
  197. $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$item_to_edit_id;
  198. $subject = sprintf(get_lang('ThereIsANewWorkFeedback'), $work_item['title']);
  199. $message = sprintf(get_lang('ThereIsANewWorkFeedbackInWorkXHere'), $work_item['title'], $url);
  200. MessageManager::send_message_simple(
  201. $work_item['user_id'],
  202. $subject,
  203. $message,
  204. api_get_user_id(),
  205. isset($_POST['send_to_drh_users'])
  206. );
  207. }
  208. }
  209. if ($_POST['qualification'] > $_POST['qualification_over']) {
  210. Display::addFlash(Display::return_message(
  211. get_lang('QualificationMustNotBeMoreThanQualificationOver'),
  212. 'error'
  213. ));
  214. } else {
  215. $sql = "UPDATE ".$work_table."
  216. SET title = '".Database::escape_string($title)."',
  217. description = '".Database::escape_string($description)."'
  218. ".$add_to_update."
  219. WHERE c_id = $course_id AND id = $item_to_edit_id";
  220. Database::query($sql);
  221. }
  222. api_item_property_update(
  223. $_course,
  224. 'work',
  225. $item_to_edit_id,
  226. 'DocumentUpdated',
  227. $user_id
  228. );
  229. $succeed = true;
  230. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  231. }
  232. Security::clear_token();
  233. } else {
  234. // Bad token or can't add works
  235. Display::addFlash(Display::return_message(get_lang('ImpossibleToSaveTheDocument'), 'error'));
  236. }
  237. $script = 'work_list.php';
  238. if ($is_allowed_to_edit) {
  239. $script = 'work_list_all.php';
  240. }
  241. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  242. exit;
  243. }
  244. $htmlHeadXtra[] = to_javascript_work();
  245. $tpl = new Template();
  246. $content = null;
  247. if (!empty($work_id)) {
  248. if ($is_allowed_to_edit) {
  249. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  250. echo Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
  251. } else {
  252. $content .= $form->returnForm();
  253. }
  254. } elseif ($is_author) {
  255. if (empty($work_item['qualificator_id']) || $work_item['qualificator_id'] == 0) {
  256. $content .= $form->returnForm();
  257. } else {
  258. $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
  259. }
  260. } elseif ($student_can_edit_in_session && $has_ended == false) {
  261. $content .= $form->returnForm();
  262. } else {
  263. $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
  264. }
  265. } else {
  266. $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
  267. }
  268. $tpl->assign('content', $content);
  269. $tpl->display_one_col_template();