edit.php 12 KB

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