editthread.php 9.1 KB

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