editthread.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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(
  80. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.$cidreq,
  81. 'name' => get_lang('Groups'),
  82. );
  83. $interbreadcrumb[] = array(
  84. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.$cidreq,
  85. 'name' => get_lang('GroupSpace').' '.$groupProperties['name'],
  86. );
  87. $interbreadcrumb[] = array(
  88. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
  89. 'name' => $currentForum['forum_title'],
  90. );
  91. $interbreadcrumb[] = array(
  92. 'url' => api_get_path(WEB_CODE_PATH).'forum/newthread.php?'.$cidreq.'&forum='.$forumId,
  93. 'name' => get_lang('EditThread'),
  94. );
  95. } else {
  96. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'forum/index.php?'.$cidreq, 'name' => $nameTools);
  97. $interbreadcrumb[] = array(
  98. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforumcategory.php?'.$cidreq.'&forumcategory='.$currentForumCategory['cat_id'],
  99. 'name' => $currentForumCategory['cat_title'],
  100. );
  101. $interbreadcrumb[] = array(
  102. 'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
  103. 'name' => $currentForum['forum_title'],
  104. );
  105. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditThread'));
  106. }
  107. $tableLink = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  108. /* Header */
  109. $htmlHeadXtra[] = <<<JS
  110. <script>
  111. $(document).on('ready', function() {
  112. if ($('#thread_qualify_gradebook').is(':checked') == true) {
  113. document.getElementById('options_field').style.display = 'block';
  114. } else {
  115. document.getElementById('options_field').style.display = 'none';
  116. }
  117. $('#thread_qualify_gradebook').click(function() {
  118. if ($('#thread_qualify_gradebook').is(':checked') == true) {
  119. document.getElementById('options_field').style.display = 'block';
  120. } else {
  121. document.getElementById('options_field').style.display = 'none';
  122. $("[name='numeric_calification']").val(0);
  123. $("[name='calification_notebook_title']").val('');
  124. $("[name='weight_calification']").val(0);
  125. $("[name='thread_peer_qualify'][value='0']").prop('checked', true);
  126. }
  127. });
  128. });
  129. </script>
  130. JS;
  131. // Action links
  132. $actions = [
  133. Display::url(
  134. Display::return_icon('back.png', get_lang('BackToForum'), '', ICON_SIZE_MEDIUM),
  135. 'viewforum.php?forum='.$forumId.'&'.$cidreq
  136. ),
  137. search_link()
  138. ];
  139. $threadData = getThreadInfo($threadId, $cId);
  140. $form = new FormValidator(
  141. 'thread',
  142. 'post',
  143. api_get_self().'?'.http_build_query([
  144. 'forum' => $forumId,
  145. 'thread' => $threadId,
  146. ]).'&'.api_get_cidreq()
  147. );
  148. $form->addElement('header', get_lang('EditThread'));
  149. $form->setConstants(array('forum' => '5'));
  150. $form->addElement('hidden', 'forum_id', $forumId);
  151. $form->addElement('hidden', 'thread_id', $threadId);
  152. $form->addElement('hidden', 'gradebook', $gradebookId);
  153. $form->addElement('text', 'thread_title', get_lang('Title'));
  154. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  155. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  156. if ((api_is_course_admin() || api_is_session_general_coach() || api_is_course_tutor()) && ($threadId)) {
  157. // Thread qualify
  158. if (Gradebook::is_active()) {
  159. //Loading gradebook select
  160. GradebookUtils::load_gradebook_select_in_tool($form);
  161. $form->addElement(
  162. 'checkbox',
  163. 'thread_qualify_gradebook',
  164. '',
  165. get_lang('QualifyThreadGradebook'),
  166. ['id' => 'thread_qualify_gradebook']
  167. );
  168. } else {
  169. $form->addElement('hidden', 'thread_qualify_gradebook', false);
  170. }
  171. $form->addElement('html', '<div id="options_field" style="display:none">');
  172. $form->addElement('text', 'numeric_calification', get_lang('QualificationNumeric'));
  173. $form->applyFilter('numeric_calification', 'html_filter');
  174. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
  175. $form->applyFilter('calification_notebook_title', 'html_filter');
  176. $form->addElement(
  177. 'number',
  178. 'weight_calification',
  179. get_lang('QualifyWeight'),
  180. ['value' => '0.00', 'step' => '0.01']
  181. );
  182. $form->applyFilter('weight_calification', 'html_filter');
  183. $group = array();
  184. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('Yes'), 1);
  185. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('No'), 0);
  186. $form->addGroup(
  187. $group,
  188. '',
  189. [get_lang('ForumThreadPeerScoring'), get_lang('ForumThreadPeerScoringComment'), ]
  190. );
  191. $form->addElement('html', '</div>');
  192. }
  193. if ($forumSettings['allow_sticky'] && api_is_allowed_to_edit(null, true)) {
  194. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  195. }
  196. $form->addElement('html', '</div>');
  197. if (!empty($threadData)) {
  198. $defaults['thread_qualify_gradebook'] = ($threadData['threadQualifyMax'] > 0 && empty($_POST)) ? 1 : 0;
  199. $defaults['thread_title'] = prepare4display($threadData['threadTitle']);
  200. $defaults['thread_sticky'] = strval(intval($threadData['threadSticky']));
  201. $defaults['thread_peer_qualify'] = intval($threadData['threadPeerQualify']);
  202. $defaults['numeric_calification'] = $threadData['threadQualifyMax'];
  203. $defaults['calification_notebook_title'] = $threadData['threadTitleQualify'];
  204. $defaults['weight_calification'] = $threadData['threadWeight'];
  205. } else {
  206. $defaults['thread_qualify_gradebook'] = 0;
  207. $defaults['numeric_calification'] = 0;
  208. $defaults['calification_notebook_title'] = '';
  209. $defaults['weight_calification'] = 0;
  210. $defaults['thread_peer_qualify'] = 0;
  211. }
  212. $form->addButtonUpdate(get_lang('ModifyThread'), 'SubmitPost');
  213. if ($form->validate()) {
  214. $redirectUrl = api_get_path(WEB_CODE_PATH).'forum/viewforum.php?forum='.$forumId.'&'.api_get_cidreq();
  215. $check = Security::check_token('post');
  216. if ($check) {
  217. $values = $form->exportValues();
  218. Security::clear_token();
  219. updateThread($values);
  220. header('Location: '.$redirectUrl);
  221. exit;
  222. }
  223. }
  224. $form->setDefaults(isset($defaults) ? $defaults : null);
  225. $token = Security::get_token();
  226. $form->addElement('hidden', 'sec_token');
  227. $form->setConstants(array('sec_token' => $token));
  228. $originIsLearnPath = $origin == 'learnpath';
  229. $view = new Template(
  230. '',
  231. !$originIsLearnPath,
  232. !$originIsLearnPath,
  233. $originIsLearnPath,
  234. $originIsLearnPath
  235. );
  236. $view->assign(
  237. 'actions',
  238. Display::toolbarAction('toolbar', $actions)
  239. );
  240. $view->assign('content', $form->returnForm());
  241. $view->display_one_col_template();