PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/interaction/forum/view.php

https://github.com/Br3nda/mahara
PHP | 273 lines | 216 code | 22 blank | 35 comment | 39 complexity | 080cd4fd236f69d8437684fe119cf8fb MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * Mahara: Electronic portfolio, weblog, resume builder and social networking
  4. * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package mahara
  20. * @subpackage interaction-forum
  21. * @author Catalyst IT Ltd
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  23. * @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
  24. *
  25. */
  26. define('INTERNAL', 1);
  27. define('PUBLIC', 1);
  28. define('MENUITEM', 'groups/forums');
  29. define('SECTION_PLUGINTYPE', 'interaction');
  30. define('SECTION_PLUGINNAME', 'forum');
  31. define('SECTION_PAGE', 'view');
  32. require(dirname(dirname(dirname(__FILE__))) . '/init.php');
  33. require_once('group.php');
  34. safe_require('interaction', 'forum');
  35. require_once(get_config('docroot') . 'interaction/lib.php');
  36. require_once('pieforms/pieform.php');
  37. $forumid = param_integer('id');
  38. $offset = param_integer('offset', 0);
  39. $userid = $USER->get('id');
  40. $topicsperpage = 25;
  41. // if offset isn't a multiple of $topicsperpage, make it the closest smaller multiple
  42. $offset = (int)($offset / $topicsperpage) * $topicsperpage;
  43. $forum = get_record_sql(
  44. 'SELECT f.title, f.description, f.id, COUNT(t.id) AS topiccount, s.forum AS subscribed, g.id AS groupid, g.name AS groupname
  45. FROM {interaction_instance} f
  46. INNER JOIN {group} g ON (g.id = f."group" AND g.deleted = ?)
  47. LEFT JOIN {interaction_forum_topic} t ON (t.forum = f.id AND t.deleted != 1 AND t.sticky != 1)
  48. LEFT JOIN {interaction_forum_subscription_forum} s ON (s.forum = f.id AND s."user" = ?)
  49. WHERE f.id = ?
  50. AND f.deleted != 1
  51. GROUP BY 1, 2, 3, 5, 6, 7',
  52. array(0, $userid, $forumid)
  53. );
  54. define('GROUP', $forum->groupid);
  55. if (!$forum) {
  56. throw new InteractionInstanceNotFoundException(get_string('cantfindforum', 'interaction.forum', $forumid));
  57. }
  58. $membership = user_can_access_forum((int)$forumid);
  59. $admin = (bool)($membership & INTERACTION_FORUM_ADMIN);
  60. $moderator = (bool)($membership & INTERACTION_FORUM_MOD);
  61. if (!$membership
  62. && !get_field('group', 'public', 'id', $forum->groupid)) {
  63. throw new GroupAccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
  64. }
  65. define('TITLE', $forum->groupname . ' - ' . $forum->title);
  66. $moderators = get_column_sql(
  67. 'SELECT gm.user FROM {interaction_forum_moderator} gm
  68. INNER JOIN {usr} u ON (u.id = gm.user AND u.deleted = 0)
  69. WHERE gm.forum = ?',
  70. array($forumid)
  71. );
  72. // updates the selected topics as subscribed/closed/sticky
  73. if ($membership && isset($_POST['checked'])) {
  74. $checked = array_keys($_POST['checked']);
  75. // get type based on which button was pressed
  76. if (isset($_POST['updatetopics1'])) {
  77. $type = $_POST['type1'];
  78. }
  79. else if (isset($_POST['updatetopics2'])) {
  80. $type = $_POST['type2'];
  81. }
  82. // check that user is only messing with topics from this forum
  83. $alltopics = get_column('interaction_forum_topic', 'id', 'forum', $forumid, 'deleted', 0);
  84. if ($checked == array_intersect($checked, $alltopics)) { // $checked is a subset of the topics in this forum
  85. if ($moderator && $type == 'sticky') {
  86. set_field_select('interaction_forum_topic', 'sticky', 1, 'id IN (' . implode($checked, ',') . ')', array());
  87. $SESSION->add_ok_msg(get_string('topicstickysuccess', 'interaction.forum'));
  88. }
  89. else if ($moderator && $type == 'unsticky') {
  90. set_field_select('interaction_forum_topic', 'sticky', 0, 'id IN (' . implode($checked, ',') . ')', array());
  91. $SESSION->add_ok_msg(get_string('topicunstickysuccess', 'interaction.forum'));
  92. }
  93. else if ($moderator && $type == 'closed') {
  94. set_field_select('interaction_forum_topic', 'closed', 1, 'id IN (' . implode($checked, ',') . ')', array());
  95. $SESSION->add_ok_msg(get_string('topicclosedsuccess', 'interaction.forum'));
  96. }
  97. else if ($moderator && $type == 'open') {
  98. set_field_select('interaction_forum_topic', 'closed', 0, 'id IN (' . implode($checked, ',') . ')', array());
  99. $SESSION->add_ok_msg(get_string('topicopenedsuccess', 'interaction.forum'));
  100. }
  101. else if ($type == 'subscribe' && !$forum->subscribed) {
  102. db_begin();
  103. foreach ($checked as $key => $value) {
  104. insert_record('interaction_forum_subscription_topic',
  105. (object) array(
  106. 'user' => $USER->get('id'),
  107. 'topic' => $value
  108. ));
  109. }
  110. db_commit();
  111. $SESSION->add_ok_msg(get_string('topicsubscribesuccess', 'interaction.forum'));
  112. }
  113. else if ($type == 'unsubscribe' && !$forum->subscribed) {
  114. delete_records_sql('DELETE FROM {interaction_forum_subscription_topic}
  115. WHERE topic IN (' . implode($checked, ',') . ') AND "user" = ?',
  116. array($USER->get('id')
  117. ));
  118. $SESSION->add_ok_msg(get_string('topicunsubscribesuccess', 'interaction.forum'));
  119. }
  120. }
  121. else { // $checked contains bad values
  122. $SESSION->add_error_msg(get_string('topicupdatefailed', 'interaction.forum'));
  123. }
  124. redirect('/interaction/forum/view.php?id=' . $forumid . '&offset=' . $offset);
  125. }
  126. if ($membership) {
  127. $forum->subscribe = pieform(array(
  128. 'name' => 'subscribe_forum',
  129. 'renderer' => 'div',
  130. 'plugintype' => 'interaction',
  131. 'pluginname' => 'forum',
  132. 'autofocus' => false,
  133. 'elements' => array(
  134. 'submit' => array(
  135. 'type' => 'submit',
  136. 'value' => $forum->subscribed ? get_string('unsubscribefromforum', 'interaction.forum') : get_string('subscribetoforum', 'interaction.forum'),
  137. 'help' => true
  138. ),
  139. 'forum' => array(
  140. 'type' => 'hidden',
  141. 'value' => $forumid
  142. ),
  143. 'redirect' => array(
  144. 'type' => 'hidden',
  145. 'value' => 'view'
  146. ),
  147. 'offset' => array(
  148. 'type' => 'hidden',
  149. 'value' => $offset
  150. ),
  151. 'type' => array(
  152. 'type' => 'hidden',
  153. 'value' => $forum->subscribed ? 'unsubscribe' : 'subscribe'
  154. )
  155. )
  156. ));
  157. }
  158. // gets the info about topics
  159. // the last post is found by taking the max id of the posts in a topic with the max post time
  160. // taking the max id is needed because multiple posts can have the same post time
  161. $sql = 'SELECT t.id, p1.subject, p1.body, p1.poster, p1.deleted, m.user AS moderator, COUNT(p2.id) AS postcount, t.closed, s.topic AS subscribed, p4.id AS lastpost, ' . db_format_tsfield('p4.ctime', 'lastposttime') . ', p4.poster AS lastposter, m2.user AS lastpostermoderator
  162. FROM {interaction_forum_topic} t
  163. INNER JOIN {interaction_forum_post} p1 ON (p1.topic = t.id AND p1.parent IS NULL)
  164. LEFT JOIN (
  165. SELECT m.forum, m.user
  166. FROM {interaction_forum_moderator} m
  167. INNER JOIN {usr} u ON (m.user = u.id AND u.deleted = 0)
  168. ) m ON (m.forum = t.forum AND p1.poster = m.user)
  169. INNER JOIN {interaction_forum_post} p2 ON (p2.topic = t.id AND p2.deleted != 1)
  170. LEFT JOIN {interaction_forum_subscription_topic} s ON (s.topic = t.id AND s."user" = ?)
  171. INNER JOIN (
  172. SELECT MAX(p2.id) AS post, t.id AS topic
  173. FROM {interaction_forum_topic} t
  174. INNER JOIN (
  175. SELECT MAX(p.ctime) AS ctime, t.id AS topic
  176. FROM {interaction_forum_topic} t
  177. INNER JOIN {interaction_forum_post} p ON (p.topic = t.id AND p.deleted = 0)
  178. GROUP BY 2
  179. ) p1 ON t.id = p1.topic
  180. INNER JOIN {interaction_forum_post} p2 ON (p1.topic = p2.topic AND p1.ctime = p2.ctime AND p2.deleted = 0)
  181. GROUP BY 2
  182. ) p3 ON p3.topic = t.id
  183. LEFT JOIN {interaction_forum_post} p4 ON (p4.id = p3.post)
  184. LEFT JOIN {interaction_forum_topic} t2 ON (p4.topic = t2.id)
  185. LEFT JOIN (
  186. SELECT m.forum, m.user
  187. FROM {interaction_forum_moderator} m
  188. INNER JOIN {usr} u ON (m.user = u.id AND u.deleted = 0)
  189. ) m2 ON (p4.poster = m2.user AND t2.forum = m2.forum)
  190. WHERE t.forum = ?
  191. AND t.sticky = ?
  192. AND t.deleted != 1
  193. GROUP BY 1, 2, 3, 4, 5, 6, 8, 9, 10, p4.ctime, p4.poster, p4.id, m2.user
  194. ORDER BY p4.ctime DESC, p4.id DESC';
  195. $stickytopics = get_records_sql_array($sql, array($userid, $forumid, 1));
  196. $regulartopics = get_records_sql_array($sql, array($userid, $forumid, 0), $offset, $topicsperpage);
  197. setup_topics($stickytopics);
  198. setup_topics($regulartopics);
  199. $pagination = build_pagination(array(
  200. 'url' => get_config('wwwroot') . 'interaction/forum/view.php?id=' . $forumid,
  201. 'count' => $forum->topiccount,
  202. 'limit' => $topicsperpage,
  203. 'offset' => $offset,
  204. 'resultcounttextsingular' => get_string('topiclower', 'interaction.forum'),
  205. 'resultcounttextplural' => get_string('topicslower', 'interaction.forum')
  206. ));
  207. $inlinejavascript = <<<EOF
  208. addLoadEvent(function() {
  209. forEach(getElementsByTagAndClassName('input', 'topic-checkbox'), function(checkbox) {
  210. var tr = getFirstParentByTagAndClassName(checkbox, 'tr', null);
  211. var origColour = tr.style.backgroundColor;
  212. connect(checkbox, 'onclick', function(e) {
  213. if (tr.style.backgroundColor == origColour) {
  214. tr.style.backgroundColor = '#ffc';
  215. }
  216. else {
  217. tr.style.backgroundColor = origColour;
  218. }
  219. });
  220. });
  221. });
  222. EOF;
  223. $smarty = smarty();
  224. $smarty->assign('heading', $forum->groupname);
  225. $smarty->assign('subheading', $forum->title);
  226. $smarty->assign('forum', $forum);
  227. $smarty->assign('membership', $membership);
  228. $smarty->assign('moderator', $moderator);
  229. $smarty->assign('admin', $admin);
  230. $smarty->assign('groupadmins', group_get_admin_ids($forum->groupid));
  231. $smarty->assign('stickytopics', $stickytopics);
  232. $smarty->assign('regulartopics', $regulartopics);
  233. $smarty->assign('moderators', $moderators);
  234. $smarty->assign('closedicon', theme_get_url('images/closed.gif', 'interaction/forum/'));
  235. $smarty->assign('subscribedicon', theme_get_url('images/subscribed.gif', 'interaction/forum/'));
  236. $smarty->assign('pagination', $pagination['html']);
  237. $smarty->assign('INLINEJAVASCRIPT', $inlinejavascript);
  238. $smarty->display('interaction:forum:view.tpl');
  239. /**
  240. * format body
  241. * format lastposttime
  242. */
  243. function setup_topics(&$topics) {
  244. if ($topics) {
  245. foreach ($topics as $topic) {
  246. $topic->body = str_shorten_html($topic->body, 50, true, false);
  247. $topic->lastposttime = relative_date(get_string('strftimerecentrelative', 'interaction.forum'), get_string('strftimerecent'), $topic->lastposttime);
  248. }
  249. }
  250. }
  251. ?>