edit.php 11 KB

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