comments.module

  1. 7.x modules/comments/comments.module
  2. 6.x modules/comments/comments.module
  3. 4.x modules/comments/comments.module
  4. 5.x modules/comments/comments.module

File

modules/comments/comments.module
View source
  1. <?php
  2. /*
  3. * This is the Comments module for FlightPath, which lets advisors write comments
  4. * for students.
  5. */
  6. function comments_menu() {
  7. $items = array();
  8. $items["comments"] = array(
  9. "title" => "Comments",
  10. "page_callback" => "comments_display_main",
  11. "access_callback" => "comments_can_access_comments",
  12. "type" => MENU_TYPE_TAB,
  13. "tab_family" => "system",
  14. "page_settings" => array (
  15. "display_currently_advising" => TRUE,
  16. "display_greeting" => TRUE,
  17. "page_has_search" => TRUE,
  18. ),
  19. "weight" => 35,
  20. );
  21. $items["comments/save-comment"] = array(
  22. "page_callback" => "comments_perform_save_comment",
  23. "access_arguments" => array("can_save_comments"),
  24. "type" => MENU_TYPE_CALLBACK,
  25. );
  26. $items["comments/delete-comment"] = array(
  27. "page_callback" => "comments_perform_delete_comment",
  28. "access_arguments" => array("can_save_comments"),
  29. "type" => MENU_TYPE_CALLBACK,
  30. );
  31. $items["comments/popup-display-all-comments"] = array(
  32. "title" => "Comments",
  33. "page_callback" => "comments_popup_display_all_comments",
  34. "access_arguments" => array("view_comments"),
  35. "page_settings" => array(
  36. "page_has_search" => FALSE,
  37. "page_is_popup" => TRUE,
  38. "page_hide_report_error" => TRUE,
  39. "bool_print" => TRUE,
  40. ),
  41. "type" => MENU_TYPE_CALLBACK,
  42. );
  43. $items["comments/popup-display-comment"] = array(
  44. "title" => "Comment",
  45. "page_callback" => "comments_popup_display_comment",
  46. "access_arguments" => array("view_comments"),
  47. "page_settings" => array(
  48. "page_has_search" => FALSE,
  49. "page_is_popup" => TRUE,
  50. "page_hide_report_error" => TRUE,
  51. "bool_print" => TRUE,
  52. ),
  53. "type" => MENU_TYPE_CALLBACK,
  54. );
  55. return $items;
  56. }
  57. function comments_popup_display_comment() {
  58. $rtn = "";
  59. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  60. $id = $_REQUEST["id"];
  61. // Try to render this comment id.
  62. $comment = comments_get_comment($id);
  63. $rtn .= comments_render_comment($comment);
  64. return $rtn;
  65. }
  66. /**
  67. * Used by the menu to determine if the comments tab should appear.
  68. */
  69. function comments_can_access_comments() {
  70. global $current_student_id, $user;
  71. // must be logged in first...
  72. if (!user_has_permission("access_logged_in_content")) return FALSE;
  73. // Do they have the correct permission to view comments?
  74. if (!user_has_permission("view_comments")) return FALSE;
  75. if ($current_student_id != "") return TRUE;
  76. return FALSE;
  77. }
  78. /**
  79. * "delete" a comment (actually, all we do is flag it as deleted)
  80. */
  81. function comments_perform_delete_comment() {
  82. global $current_student_id, $user;
  83. $comment_id = $_REQUEST["comment_id"];
  84. // Let's get some details about the comment to make sure this user can delete it.
  85. $comment = comments_get_comment($comment_id);
  86. if ($comment["faculty_id"] == $user->cwid && user_has_permission("can_delete_own_comments_3_months")) {
  87. // TODO: We should really ALSO check to make sure it's been less than 3 months.
  88. db_query("UPDATE advising_comments
  89. SET delete_flag = '1'
  90. WHERE `id` = '?' ", $comment_id);
  91. fp_add_message(t("Comment has been deleted successfully."));
  92. }
  93. if ($_GET["destination"] != "") {
  94. fp_goto($_GET["destination"]);
  95. }
  96. else {
  97. fp_goto("comments");
  98. }
  99. }
  100. function comments_comment_form_submit($form, $form_state) {
  101. global $user, $current_student_id;
  102. $faculty_cwid = $user->cwid;
  103. $type = $form_state["values"]["type"];
  104. $term_id = $form_state["values"]["term_id"];
  105. $comment = trim($form_state["values"]["comment"]);
  106. // Perform the save!
  107. if ($comment) {
  108. db_query("INSERT INTO advising_comments
  109. (student_id, faculty_id, term_id,
  110. comment, posted, access_type)
  111. VALUES
  112. ('?', '?', '?', '?', '?', '?')
  113. ", $current_student_id, $faculty_cwid, $term_id, $comment, time(), $type);
  114. fp_add_message(t("Comment saved successfully."));
  115. }
  116. watchdog("save_comment", "Comment saved");
  117. }
  118. /**
  119. * This is the form to enter a new comment.
  120. */
  121. function comments_comment_form() {
  122. global $current_student_id;
  123. $term_id = variable_get("advising_term_id");
  124. fp_set_title("");
  125. $form = array();
  126. $form["type"] = array(
  127. "type" => "radios",
  128. "label" => t("Visible to:"),
  129. "options" => array("public" => t("Anyone (including students)"), "faculty" => t("Faculty/Staff only")),
  130. "value" => "faculty",
  131. );
  132. $form["term_id"] = array(
  133. "type" => "hidden",
  134. "value" => $term_id,
  135. );
  136. $form["current_student_id"] = array(
  137. "type" => "hidden",
  138. "value" => $current_student_id,
  139. );
  140. $form["comment"] = array(
  141. "type" => "textarea",
  142. );
  143. $form["submit"] = array(
  144. "type" => "submit",
  145. "value" => t("Save"),
  146. );
  147. return $form;
  148. }
  149. /**
  150. * This displays the primary Comments tab, where we see past comments and can enter a
  151. * new one (with the right permissions).
  152. */
  153. function comments_display_main() {
  154. global $current_student_id;
  155. $rtn = "";
  156. fp_add_js(fp_get_module_path("comments") . "/js/comments.js");
  157. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  158. if (user_has_permission("can_save_comments")) {
  159. $form = fp_render_form("comments_comment_form");
  160. $rtn .= fp_render_c_fieldset($form, t("Click to enter comment"), true);
  161. }
  162. $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
  163. $comments = comments_get_comments($current_student_id, FALSE, $access_types);
  164. //fpm($comments);
  165. foreach($comments as $comment) {
  166. $delete_link = "";
  167. // Should we present a "delete link" to the user for this comment?
  168. if (user_has_permission("can_delete_own_comments_3_months")) {
  169. // See if this comment is younger than 3 months.
  170. $del_range = strtotime("-3 month");
  171. $then = $comment["posted"];
  172. if ($then > $del_range) {
  173. $delete_link = fp_render_button("Delete", "deleteComment(\"{$comment["id"]}\");");
  174. }
  175. }
  176. $rtn .= comments_render_comment($comment, $delete_link);
  177. }
  178. return $rtn;
  179. }
  180. /**
  181. * Displays all comments for a student in a popup window, meant for printing.
  182. */
  183. function comments_popup_display_all_comments() {
  184. global $current_student_id;
  185. $rtn = "";
  186. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  187. $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
  188. $comments = comments_get_comments($current_student_id, FALSE, $access_types);
  189. foreach($comments as $comment) {
  190. $rtn .= comments_render_comment($comment, $delete_link);
  191. }
  192. return $rtn;
  193. }
  194. /**
  195. * Display the comment array in a pretty way.
  196. */
  197. function comments_render_comment($comment, $delete_link = "") {
  198. $rtn = "";
  199. // Make sure the user has access to view it!
  200. if (!user_has_permission("view_comments")) {
  201. return "<p>" . t("Sorry, you do not have permission to view comments.") . "</p>";
  202. }
  203. if ($comment["access_type"] == "faculty" && !user_has_permission("view_faculty_comments")) {
  204. return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is marked as faculty-only).") . "</p>";
  205. }
  206. $rtn .= "<div class='comment-comment comment-comment-" . $comment["access_type"] . "'>
  207. <div class='comment-by-line'>" .ucwords($comment["access_type"]) . " " . t("comment by") . " " . fp_get_faculty_name($comment["faculty_id"]) . "</div>
  208. <div class='comment-datetime'>" . format_date($comment["posted"], "pretty") . "</div>
  209. <div class='comment-text'>" . filter_markup($comment["comment"], "full") . "</div>
  210. ";
  211. if ($delete_link) {
  212. $rtn .= "<div class='comment-delete'>$delete_link</div>";
  213. }
  214. $rtn .= "
  215. </div>";
  216. return $rtn;
  217. }
  218. /**
  219. * Returns an array of comments for this student, sorted most recent first.
  220. */
  221. function comments_get_comments($student_id, $bool_included_deleted = FALSE, $access_types = array()) {
  222. $rtn = array();
  223. $deleted_line = "AND delete_flag = 0";
  224. if ($bool_included_deleted) {
  225. $deleted_line = "";
  226. }
  227. $access_type_line = "";
  228. // Build up the "access_type_line" for the query, based on the values
  229. // in the access_types array.
  230. if (count($access_types) > 0) {
  231. $access_type_line = "AND ( ";
  232. foreach ($access_types as $access_type) {
  233. $access_type_line .= " access_type = '$access_type' OR";
  234. }
  235. // remove the last OR
  236. $access_type_line = substr($access_type_line, 0, -2);
  237. $access_type_line .= ")";
  238. }
  239. //if ($access_type == "public" || $access_type == "faculty") {
  240. // $access_type_line = "AND access_type = '$access_type' ";
  241. //}
  242. $res = db_query("SELECT * FROM advising_comments
  243. WHERE student_id = '?'
  244. $deleted_line
  245. $access_type_line
  246. ORDER BY posted DESC", $student_id);
  247. while ($cur = db_fetch_array($res)) {
  248. $rtn[$cur["id"]] = $cur;
  249. }
  250. return $rtn;
  251. }
  252. function comments_get_comment($comment_id) {
  253. $rtn = array();
  254. $res = db_query("SELECT * FROM advising_comments
  255. WHERE `id` = '?'
  256. ", $comment_id);
  257. $cur = db_fetch_array($res);
  258. return $cur;
  259. }
  260. function comments_perm() {
  261. return array(
  262. "view_comments" => array(
  263. "title" => t("Can view comments"),
  264. "description" => t("The user may view comments (only public by default)."),
  265. ),
  266. "view_faculty_comments" => array(
  267. "title" => t("View 'Faculty' comments"),
  268. "description" => t("The user is allowed to view 'Faculty' comments."),
  269. ),
  270. "can_save_comments" => array(
  271. "title" => t("Can save comments"),
  272. "description" => t("The user may save new comments, visible to either everyone or only faculty/staff."),
  273. ),
  274. "can_delete_own_comments_3_months" => array(
  275. "title" => t("Can delete own comments for 3 months"),
  276. "description" => t("The user may delete comments they have made for up to 3 months. After 3 months, they
  277. will not be able to delete their comment."),
  278. ),
  279. );
  280. }

Functions

Namesort descending Description
comments_can_access_comments Used by the menu to determine if the comments tab should appear.
comments_comment_form This is the form to enter a new comment.
comments_comment_form_submit
comments_display_main This displays the primary Comments tab, where we see past comments and can enter a new one (with the right permissions).
comments_get_comment
comments_get_comments Returns an array of comments for this student, sorted most recent first.
comments_menu
comments_perform_delete_comment "delete" a comment (actually, all we do is flag it as deleted)
comments_perm
comments_popup_display_all_comments Displays all comments for a student in a popup window, meant for printing.
comments_popup_display_comment
comments_render_comment Display the comment array in a pretty way.