work.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.work
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $current_course_tool = TOOL_STUDENTPUBLICATION;
  8. api_protect_course_script(true);
  9. require_once 'work.lib.php';
  10. $courseInfo = api_get_course_info();
  11. $user_id = api_get_user_id();
  12. $sessionId = api_get_session_id();
  13. $groupId = api_get_group_id();
  14. // Section (for the tabs)
  15. $this_section = SECTION_COURSES;
  16. $work_id = isset($_GET['id']) ? (int) $_GET['id'] : null;
  17. $my_folder_data = get_work_data_by_id($work_id);
  18. $curdirpath = '';
  19. $htmlHeadXtra[] = api_get_jqgrid_js();
  20. $htmlHeadXtra[] = to_javascript_work();
  21. /* Constants and variables */
  22. $tool_name = get_lang('StudentPublications');
  23. $item_id = isset($_REQUEST['item_id']) ? (int) $_REQUEST['item_id'] : null;
  24. $origin = api_get_origin();
  25. $course_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'];
  26. $base_work_dir = $course_dir.'/work';
  27. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'list';
  28. // Download folder
  29. if ($action === 'downloadfolder') {
  30. require 'downloadfolder.inc.php';
  31. }
  32. $display_upload_form = false;
  33. if ($action === 'upload_form') {
  34. $display_upload_form = true;
  35. }
  36. /* Header */
  37. if (api_is_in_gradebook()) {
  38. $interbreadcrumb[] = [
  39. 'url' => api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq(),
  40. 'name' => get_lang('ToolGradebook'),
  41. ];
  42. }
  43. if (!empty($groupId)) {
  44. api_protect_course_group(GroupManager::GROUP_TOOL_WORK);
  45. $group_properties = GroupManager::get_group_properties($groupId);
  46. $interbreadcrumb[] = [
  47. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  48. 'name' => get_lang('Groups'),
  49. ];
  50. $interbreadcrumb[] = [
  51. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  52. 'name' => get_lang('GroupSpace').' '.$group_properties['name'],
  53. ];
  54. $interbreadcrumb[] = [
  55. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  56. 'name' => get_lang('StudentPublications'),
  57. ];
  58. $url_dir = api_get_path(WEB_CODE_PATH).'work/work.php?&id='.$work_id.'&'.api_get_cidreq();
  59. if (!empty($my_folder_data)) {
  60. $interbreadcrumb[] = ['url' => $url_dir, 'name' => $my_folder_data['title']];
  61. }
  62. if ($action == 'upload_form') {
  63. $interbreadcrumb[] = [
  64. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  65. 'name' => get_lang('UploadADocument'),
  66. ];
  67. }
  68. if ($action == 'create_dir') {
  69. $interbreadcrumb[] = [
  70. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  71. 'name' => get_lang('CreateAssignment'),
  72. ];
  73. }
  74. } else {
  75. if ($origin != 'learnpath') {
  76. if (isset($_GET['id']) &&
  77. !empty($_GET['id']) || $display_upload_form || $action == 'create_dir'
  78. ) {
  79. $interbreadcrumb[] = [
  80. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  81. 'name' => get_lang('StudentPublications'),
  82. ];
  83. } else {
  84. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('StudentPublications')];
  85. }
  86. if (!empty($my_folder_data)) {
  87. $interbreadcrumb[] = [
  88. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?id='.$work_id.'&'.api_get_cidreq(),
  89. 'name' => $my_folder_data['title'],
  90. ];
  91. }
  92. if ($action === 'upload_form') {
  93. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('UploadADocument')];
  94. }
  95. if ($action === 'create_dir') {
  96. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('CreateAssignment')];
  97. }
  98. }
  99. }
  100. Event::event_access_tool(TOOL_STUDENTPUBLICATION);
  101. $logInfo = [
  102. 'tool' => TOOL_STUDENTPUBLICATION,
  103. 'tool_id' => 0,
  104. 'tool_id_detail' => 0,
  105. 'action' => $action,
  106. ];
  107. Event::registerLog($logInfo);
  108. $groupId = api_get_group_id();
  109. $isTutor = false;
  110. if (!empty($groupId)) {
  111. $groupInfo = GroupManager::get_group_properties($groupId);
  112. $isTutor = GroupManager::is_tutor_of_group(
  113. api_get_user_id(),
  114. $groupInfo
  115. );
  116. }
  117. $is_allowed_to_edit = api_is_allowed_to_edit();
  118. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  119. /* Display links to upload form and tool options */
  120. if (!in_array($action, ['add', 'create_dir'])) {
  121. $token = Security::get_token();
  122. }
  123. $currentUrl = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq();
  124. $content = null;
  125. // For teachers
  126. switch ($action) {
  127. case 'add':
  128. case 'create_dir':
  129. if (!($is_allowed_to_edit || $isTutor)) {
  130. api_not_allowed(true);
  131. }
  132. $addUrl = api_get_path(WEB_CODE_PATH).'work/work.php?action=create_dir&'.api_get_cidreq();
  133. $form = new FormValidator(
  134. 'form1',
  135. 'post',
  136. $addUrl
  137. );
  138. $form->addHeader(get_lang('CreateAssignment'));
  139. $form->addElement('hidden', 'action', 'add');
  140. // Set default values
  141. $defaults = !empty($_POST) ? $_POST : ['allow_text_assignment' => 2];
  142. $form = getFormWork($form, $defaults);
  143. $form->addButtonCreate(get_lang('CreateDirectory'));
  144. if ($form->validate()) {
  145. $values = $form->getSubmitValues();
  146. $result = addDir(
  147. $values,
  148. $user_id,
  149. $courseInfo,
  150. $groupId,
  151. $sessionId
  152. );
  153. if ($result) {
  154. Skill::saveSkills($form, ITEM_TYPE_STUDENT_PUBLICATION, $result);
  155. $message = Display::return_message(get_lang('DirectoryCreated'), 'success');
  156. } else {
  157. $currentUrl = $addUrl;
  158. $message = Display::return_message(get_lang('CannotCreateDir'), 'error');
  159. }
  160. Display::addFlash($message);
  161. header('Location: '.$currentUrl);
  162. exit;
  163. } else {
  164. $content = $form->returnForm();
  165. }
  166. break;
  167. case 'delete_dir':
  168. if ($is_allowed_to_edit) {
  169. $work_to_delete = get_work_data_by_id($_REQUEST['id']);
  170. $result = deleteDirWork($_REQUEST['id']);
  171. if ($result) {
  172. $message = Display::return_message(
  173. get_lang('DirDeleted').': '.$work_to_delete['title'],
  174. 'success'
  175. );
  176. Display::addFlash($message);
  177. }
  178. header('Location: '.$currentUrl);
  179. exit;
  180. }
  181. break;
  182. case 'move':
  183. // Move file form request
  184. if ($is_allowed_to_edit) {
  185. if (!empty($item_id)) {
  186. $content = generateMoveForm(
  187. $item_id,
  188. $curdirpath,
  189. $courseInfo,
  190. $groupId,
  191. $sessionId
  192. );
  193. }
  194. }
  195. break;
  196. case 'move_to':
  197. /* Move file command */
  198. if ($is_allowed_to_edit) {
  199. $move_to_path = get_work_path($_REQUEST['move_to_id']);
  200. if ($move_to_path == -1) {
  201. $move_to_path = '/';
  202. } elseif (substr($move_to_path, -1, 1) != '/') {
  203. $move_to_path = $move_to_path.'/';
  204. }
  205. // Security fix: make sure they can't move files that are not in the document table
  206. if ($path = get_work_path($item_id)) {
  207. if (move($course_dir.'/'.$path, $base_work_dir.$move_to_path)) {
  208. // Update db
  209. updateWorkUrl(
  210. $item_id,
  211. 'work'.$move_to_path,
  212. $_REQUEST['move_to_id']
  213. );
  214. api_item_property_update(
  215. $courseInfo,
  216. 'work',
  217. $_REQUEST['move_to_id'],
  218. 'FolderUpdated',
  219. $user_id
  220. );
  221. $message = Display::return_message(get_lang('DirMv'), 'success');
  222. } else {
  223. $message = Display::return_message(get_lang('Impossible'), 'error');
  224. }
  225. } else {
  226. $message = Display::return_message(get_lang('Impossible'), 'error');
  227. }
  228. Display::addFlash($message);
  229. header('Location: '.$currentUrl);
  230. exit;
  231. }
  232. break;
  233. case 'visible':
  234. if (!$is_allowed_to_edit) {
  235. api_not_allowed();
  236. }
  237. api_item_property_update(
  238. $courseInfo,
  239. 'work',
  240. $work_id,
  241. 'visible',
  242. api_get_user_id(),
  243. null,
  244. null,
  245. null,
  246. null,
  247. $sessionId
  248. );
  249. Display::addFlash(
  250. Display::return_message(
  251. get_lang('VisibilityChanged'),
  252. 'confirmation'
  253. )
  254. );
  255. header('Location: '.$currentUrl);
  256. exit;
  257. break;
  258. case 'invisible':
  259. if (!$is_allowed_to_edit) {
  260. api_not_allowed();
  261. }
  262. api_item_property_update(
  263. $courseInfo,
  264. 'work',
  265. $work_id,
  266. 'invisible',
  267. api_get_user_id(),
  268. null,
  269. null,
  270. null,
  271. null,
  272. $sessionId
  273. );
  274. Display::addFlash(
  275. Display::return_message(
  276. get_lang('VisibilityChanged'),
  277. 'confirmation'
  278. )
  279. );
  280. header('Location: '.$currentUrl);
  281. exit;
  282. break;
  283. case 'list':
  284. /* Display list of student publications */
  285. if (!empty($my_folder_data['description'])) {
  286. $content = '<div>'.
  287. get_lang('Description').':'.Security::remove_XSS($my_folder_data['description'], STUDENT).
  288. '</div>';
  289. }
  290. // Work list
  291. if (api_is_allowed_to_edit() || api_is_coach()) {
  292. $content .= '<div class="row">';
  293. $content .= '<div class="col-md-12">';
  294. $content .= '<div class="table-responsive">';
  295. $content .= Display::panel(showTeacherWorkGrid());
  296. $content .= '</div>';
  297. $content .= '</div>';
  298. $content .= '<div id="student-list-work" style="display: none" class="table-responsive">';
  299. $content .= '<div class="toolbar"><a id="closed-view-list" href="#">
  300. <em class="fa fa-times-circle"></em> '.get_lang('Close').'</a></div>';
  301. $content .= showStudentList($work_id);
  302. $content .= '</div>';
  303. } else {
  304. $content .= Display::panel(showStudentWorkGrid());
  305. }
  306. break;
  307. }
  308. Display::display_header(null);
  309. Display::display_introduction_section(TOOL_STUDENTPUBLICATION);
  310. if ($origin === 'learnpath') {
  311. echo '<div style="height:15px">&nbsp;</div>';
  312. }
  313. displayWorkActionLinks($work_id, $action, $isTutor);
  314. echo $content;
  315. Display::display_footer();