editthread.php 9.5 KB

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