editthread.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Edit a Forum Thread.
  5. *
  6. * @Author José Loguercio <jose.loguercio@beeznest.com>
  7. *
  8. * @package chamilo.forum
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. // The section (tabs).
  12. $this_section = SECTION_COURSES;
  13. // Notification for unauthorized people.
  14. api_protect_course_script(true);
  15. $cidreq = api_get_cidreq();
  16. $nameTools = get_lang('ToolForum');
  17. $_user = api_get_user_info();
  18. require_once 'forumfunction.inc.php';
  19. // Are we in a lp ?
  20. $origin = api_get_origin();
  21. /* MAIN DISPLAY SECTION */
  22. $forumId = (int) $_GET['forum'];
  23. $currentForum = get_forum_information($forumId);
  24. $currentForumCategory = get_forumcategory_information($currentForum['forum_category']);
  25. if (api_is_in_gradebook()) {
  26. $interbreadcrumb[] = [
  27. 'url' => Category::getUrl(),
  28. 'name' => get_lang('ToolGradebook'),
  29. ];
  30. }
  31. $threadId = isset($_GET['thread']) ? intval($_GET['thread']) : 0;
  32. $courseInfo = isset($_GET['cidReq']) ? api_get_course_info($_GET['cidReq']) : 0;
  33. $cId = isset($courseInfo['real_id']) ? intval($courseInfo['real_id']) : 0;
  34. $gradebookId = intval(api_is_in_gradebook());
  35. /* Is the user allowed here? */
  36. // The user is not allowed here if:
  37. // 1. the forumcategory or forum is invisible (visibility==0) and the user is not a course manager
  38. if (!api_is_allowed_to_edit(false, true) &&
  39. (($currentForumCategory['visibility'] && $currentForumCategory['visibility'] == 0) || $currentForum['visibility'] == 0)
  40. ) {
  41. api_not_allowed();
  42. }
  43. // 2. the forumcategory or forum is locked (locked <>0) and the user is not a course manager
  44. if (!api_is_allowed_to_edit(false, true) &&
  45. (($currentForumCategory['visibility'] && $currentForumCategory['locked'] != 0) || $currentForum['locked'] != 0)
  46. ) {
  47. api_not_allowed();
  48. }
  49. // 3. new threads are not allowed and the user is not a course manager
  50. if (!api_is_allowed_to_edit(false, true) &&
  51. $currentForum['allow_new_threads'] != 1
  52. ) {
  53. api_not_allowed();
  54. }
  55. // 4. anonymous posts are not allowed and the user is not logged in
  56. if (!$_user['user_id'] && $currentForum['allow_anonymous'] != 1) {
  57. api_not_allowed();
  58. }
  59. // 5. Check user access
  60. if ($currentForum['forum_of_group'] != 0) {
  61. $show_forum = GroupManager::user_has_access(
  62. api_get_user_id(),
  63. $currentForum['forum_of_group'],
  64. GroupManager::GROUP_TOOL_FORUM
  65. );
  66. if (!$show_forum) {
  67. api_not_allowed();
  68. }
  69. }
  70. // 6. Invited users can't create new threads
  71. if (api_is_invitee()) {
  72. api_not_allowed(true);
  73. }
  74. $groupId = api_get_group_id();
  75. if (!empty($groupId)) {
  76. $groupProperties = GroupManager:: get_group_properties($groupId);
  77. $interbreadcrumb[] = [
  78. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.$cidreq,
  79. 'name' => get_lang('Groups'),
  80. ];
  81. $interbreadcrumb[] = [
  82. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.$cidreq,
  83. 'name' => get_lang('GroupSpace').' '.$groupProperties['name'],
  84. ];
  85. $interbreadcrumb[] = [
  86. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
  87. 'name' => $currentForum['forum_title'],
  88. ];
  89. $interbreadcrumb[] = [
  90. 'url' => api_get_path(WEB_CODE_PATH).'forum/newthread.php?'.$cidreq.'&forum='.$forumId,
  91. 'name' => get_lang('EditThread'),
  92. ];
  93. } else {
  94. $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'forum/index.php?'.$cidreq, 'name' => $nameTools];
  95. $interbreadcrumb[] = [
  96. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforumcategory.php?'.$cidreq.'&forumcategory='.$currentForumCategory['cat_id'],
  97. 'name' => $currentForumCategory['cat_title'],
  98. ];
  99. $interbreadcrumb[] = [
  100. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
  101. 'name' => $currentForum['forum_title'],
  102. ];
  103. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('EditThread')];
  104. }
  105. $tableLink = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  106. /* Header */
  107. $htmlHeadXtra[] = <<<JS
  108. <script>
  109. $(function() {
  110. $('[name="thread_qualify_gradebook"]:checkbox').change(function () {
  111. if (this.checked) {
  112. $('#options_field').show();
  113. } else {
  114. $('#options_field').hide();
  115. $("[name='numeric_calification']").val(0);
  116. $("[name='calification_notebook_title']").val('');
  117. $("[name='weight_calification']").val(0);
  118. $("[name='thread_peer_qualify'][value='0']").prop('checked', true);
  119. }
  120. });
  121. });
  122. </script>
  123. JS;
  124. // Action links
  125. $actions = [
  126. Display::url(
  127. Display::return_icon('back.png', get_lang('BackToForum'), '', ICON_SIZE_MEDIUM),
  128. 'viewforum.php?forum='.$forumId.'&'.$cidreq
  129. ),
  130. search_link(),
  131. ];
  132. $threadData = getThreadInfo($threadId, $cId);
  133. $gradeThisThread = empty($_POST) && ($threadData['threadQualifyMax'] > 0 || $threadData['threadWeight'] > 0);
  134. $form = new FormValidator(
  135. 'thread',
  136. 'post',
  137. api_get_self().'?'.http_build_query([
  138. 'forum' => $forumId,
  139. 'thread' => $threadId,
  140. ]).'&'.api_get_cidreq()
  141. );
  142. $form->addElement('header', get_lang('EditThread'));
  143. $form->setConstants(['forum' => '5']);
  144. $form->addElement('hidden', 'forum_id', $forumId);
  145. $form->addElement('hidden', 'thread_id', $threadId);
  146. $form->addElement('hidden', 'gradebook', $gradebookId);
  147. $form->addElement('text', 'thread_title', get_lang('Title'));
  148. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  149. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  150. if ((api_is_course_admin() || api_is_session_general_coach() || api_is_course_tutor()) && $threadId) {
  151. // Thread qualify
  152. if (Gradebook::is_active()) {
  153. //Loading gradebook select
  154. GradebookUtils::load_gradebook_select_in_tool($form);
  155. $form->addElement(
  156. 'checkbox',
  157. 'thread_qualify_gradebook',
  158. '',
  159. get_lang('QualifyThreadGradebook')
  160. );
  161. } else {
  162. $form->addElement('hidden', 'thread_qualify_gradebook', false);
  163. }
  164. $form->addElement('html', '<div id="options_field" style="'.($gradeThisThread ? '' : 'display:none;').'">');
  165. $form->addElement('text', 'numeric_calification', get_lang('QualificationNumeric'));
  166. $form->applyFilter('numeric_calification', 'html_filter');
  167. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
  168. $form->applyFilter('calification_notebook_title', 'html_filter');
  169. $form->addElement(
  170. 'number',
  171. 'weight_calification',
  172. get_lang('QualifyWeight'),
  173. ['value' => '0.00', 'step' => '0.01']
  174. );
  175. $form->applyFilter('weight_calification', 'html_filter');
  176. $group = [];
  177. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('Yes'), 1);
  178. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('No'), 0);
  179. $form->addGroup(
  180. $group,
  181. '',
  182. [get_lang('ForumThreadPeerScoring'), get_lang('ForumThreadPeerScoringComment')]
  183. );
  184. $form->addElement('html', '</div>');
  185. }
  186. if (api_is_allowed_to_edit(null, true)) {
  187. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  188. }
  189. $form->addElement('html', '</div>');
  190. $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_FORUM_THREAD, $threadId);
  191. if (!empty($threadData)) {
  192. $defaults['thread_qualify_gradebook'] = $gradeThisThread;
  193. $defaults['thread_title'] = prepare4display($threadData['threadTitle']);
  194. $defaults['thread_sticky'] = strval(intval($threadData['threadSticky']));
  195. $defaults['thread_peer_qualify'] = intval($threadData['threadPeerQualify']);
  196. $defaults['numeric_calification'] = $threadData['threadQualifyMax'];
  197. $defaults['calification_notebook_title'] = $threadData['threadTitleQualify'];
  198. $defaults['weight_calification'] = $threadData['threadWeight'];
  199. } else {
  200. $defaults['thread_qualify_gradebook'] = 0;
  201. $defaults['numeric_calification'] = 0;
  202. $defaults['calification_notebook_title'] = '';
  203. $defaults['weight_calification'] = 0;
  204. $defaults['thread_peer_qualify'] = 0;
  205. }
  206. $defaults['skills'] = array_keys($skillList);
  207. $form->addButtonUpdate(get_lang('ModifyThread'), 'SubmitPost');
  208. if ($form->validate()) {
  209. $redirectUrl = api_get_path(WEB_CODE_PATH).'forum/viewforum.php?forum='.$forumId.'&'.api_get_cidreq();
  210. $check = Security::check_token('post');
  211. if ($check) {
  212. $values = $form->exportValues();
  213. Security::clear_token();
  214. updateThread($values);
  215. Skill::saveSkills($form, ITEM_TYPE_FORUM_THREAD, $threadId);
  216. header('Location: '.$redirectUrl);
  217. exit;
  218. }
  219. }
  220. $form->setDefaults(isset($defaults) ? $defaults : null);
  221. $token = Security::get_token();
  222. $form->addElement('hidden', 'sec_token');
  223. $form->setConstants(['sec_token' => $token]);
  224. $originIsLearnPath = $origin == 'learnpath';
  225. $view = new Template(
  226. '',
  227. !$originIsLearnPath,
  228. !$originIsLearnPath,
  229. $originIsLearnPath,
  230. $originIsLearnPath
  231. );
  232. $view->assign(
  233. 'actions',
  234. Display::toolbarAction('toolbar', $actions)
  235. );
  236. $view->assign('content', $form->returnForm());
  237. $view->display_one_col_template();