editthread.php 9.3 KB

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