comments.module

  1. 6.x modules/comments/comments.module
  2. 7.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. ),
  17. "weight" => 35,
  18. );
  19. $items["comments/save-comment"] = array(
  20. "page_callback" => "comments_perform_save_comment",
  21. "access_arguments" => array("can_save_comments"),
  22. "type" => MENU_TYPE_CALLBACK,
  23. );
  24. $items["comments/delete-comment"] = array(
  25. "page_callback" => "comments_perform_delete_comment",
  26. "access_arguments" => array("can_save_comments"),
  27. "type" => MENU_TYPE_CALLBACK,
  28. );
  29. $items["comments/popup-display-all-comments"] = array(
  30. "title" => "Comment History",
  31. "page_callback" => "comments_popup_display_all_comments",
  32. "access_arguments" => array("view_comments"),
  33. "page_settings" => array(
  34. "page_is_popup" => TRUE,
  35. ),
  36. "type" => MENU_TYPE_TAB,
  37. "tab_family" => "comments_popup_all",
  38. );
  39. $items["comments/popup-display-comment"] = array(
  40. "title" => "Comment",
  41. "page_callback" => "comments_popup_display_comment",
  42. "access_arguments" => array("view_comments"),
  43. "page_settings" => array(
  44. "page_is_popup" => TRUE,
  45. ),
  46. "type" => MENU_TYPE_TAB,
  47. "tab_family" => "comments_popup",
  48. );
  49. return $items;
  50. }
  51. /**
  52. * hook_cron
  53. */
  54. function comments_cron() {
  55. // Delete comments which has been marked with "delete_flag = 1"
  56. $last_run = intval(variable_get("comments_last_run_delete_flag_removal", 0));
  57. $check_against = strtotime("NOW - 7 DAYS"); // don't run any more often than once every 7 days
  58. $c = 0;
  59. if ($check_against > $last_run) {
  60. $res = db_query("DELETE FROM advising_comments
  61. WHERE delete_flag = 1 ");
  62. $c = db_affected_rows($res);
  63. watchdog("comments", "Delete from db complete. $c items removed.", array(), WATCHDOG_DEBUG);
  64. variable_set("comments_last_run_delete_flag_removal", time());
  65. } // check against > last_run, so we should do it.
  66. } // hook_cron
  67. function comments_popup_display_comment() {
  68. $rtn = "";
  69. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  70. $id = $_REQUEST["id"];
  71. // Try to render this comment id.
  72. $comment = comments_get_comment($id);
  73. $rtn .= comments_render_comment($comment);
  74. watchdog("comments", "popup_display_comment id:$id", array(), WATCHDOG_DEBUG);
  75. return $rtn;
  76. }
  77. /**
  78. * Used by the menu to determine if the comments tab should appear.
  79. */
  80. function comments_can_access_comments() {
  81. global $current_student_id, $user;
  82. // must be logged in first...
  83. if (!user_has_permission("access_logged_in_content")) return FALSE;
  84. // Do they have the correct permission to view comments?
  85. if (!user_has_permission("view_comments")) return FALSE;
  86. if ($current_student_id != "") return TRUE;
  87. return FALSE;
  88. }
  89. /**
  90. * "delete" a comment (actually, all we do is flag it as deleted)
  91. */
  92. function comments_perform_delete_comment() {
  93. global $current_student_id, $user;
  94. $comment_id = $_REQUEST["comment_id"];
  95. // Let's get some details about the comment to make sure this user can delete it.
  96. $comment = comments_get_comment($comment_id);
  97. if ($comment["faculty_id"] == $user->cwid && user_has_permission("can_delete_own_comments_3_months")) {
  98. // TODO: We should really ALSO check to make sure it's been less than 3 months.
  99. db_query("UPDATE advising_comments
  100. SET delete_flag = '1'
  101. WHERE `id` = '?' ", $comment_id);
  102. fp_add_message(t("Comment has been deleted successfully."));
  103. }
  104. watchdog("comments", "deleted comment for:$current_student_id, comment_id:$comment_id", array());
  105. if (@$_GET["destination"] != "") {
  106. fp_goto($_GET["destination"]);
  107. }
  108. else {
  109. fp_goto("comments");
  110. }
  111. }
  112. function comments_comment_form_submit($form, $form_state) {
  113. global $user, $current_student_id;
  114. $faculty_cwid = $user->cwid;
  115. $type = $form_state["values"]["type"];
  116. $term_id = $form_state["values"]["term_id"];
  117. $comment = trim($form_state["values"]["comment"]);
  118. // Perform the save!
  119. if ($comment) {
  120. db_query("INSERT INTO advising_comments
  121. (student_id, faculty_id, term_id,
  122. comment, posted, access_type)
  123. VALUES
  124. ('?', '?', '?', '?', '?', '?')
  125. ", $current_student_id, $faculty_cwid, $term_id, $comment, time(), $type);
  126. fp_add_message(t("Comment saved successfully."));
  127. }
  128. watchdog("save_comment", "Comment saved for $current_student_id: $comment");
  129. }
  130. /**
  131. * This is the form to enter a new comment.
  132. */
  133. function comments_comment_form() {
  134. global $current_student_id;
  135. $school_id = db_get_school_id_for_student_id($current_student_id);
  136. $term_id = variable_get_for_school("advising_term_id", '', $school_id);
  137. fp_set_title("");
  138. $form = array();
  139. $form["type"] = array(
  140. "type" => "radios",
  141. "label" => t("Visible to:"),
  142. "options" => array("public" => t("Anyone (incl student)"), "faculty" => t("Faculty/Staff only")),
  143. "value" => "faculty",
  144. );
  145. $form["term_id"] = array(
  146. "type" => "hidden",
  147. "value" => $term_id,
  148. );
  149. $form["current_student_id"] = array(
  150. "type" => "hidden",
  151. "value" => $current_student_id,
  152. );
  153. $form["comment"] = array(
  154. "type" => "textarea_editor",
  155. );
  156. $form["submit"] = array(
  157. "type" => "submit",
  158. "value" => t("Save"),
  159. );
  160. return $form;
  161. }
  162. /**
  163. * This displays the primary Comments tab, where we see past comments and can enter a
  164. * new one (with the right permissions).
  165. */
  166. function comments_display_main() {
  167. global $current_student_id, $screen, $user;
  168. $rtn = "";
  169. fp_add_js(fp_get_module_path("comments") . "/js/comments.js");
  170. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  171. if (user_has_permission("can_save_comments")) {
  172. $form = fp_render_form("comments_comment_form");
  173. $rtn .= fp_render_c_fieldset($form, t("Click to enter comment"), true, " new-comment-fs");
  174. }
  175. fp_set_title('');
  176. $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
  177. $comments = comments_get_comments($current_student_id, FALSE, $access_types);
  178. foreach($comments as $comment) {
  179. $delete_link = "";
  180. // Should we present a "delete link" to the user for this comment?
  181. if (user_has_permission("can_delete_own_comments_3_months") && $comment['faculty_id'] == $user->cwid) {
  182. // See if this comment is younger than 3 months.
  183. $del_range = strtotime("now -3 months");
  184. $then = intval($comment["posted"]);
  185. if ($then > $del_range) {
  186. $delete_link = "<a href='javascript:deleteComment(\"{$comment["id"]}\");' class='button'>" . t("Delete") . "</a>";
  187. }
  188. }
  189. $rtn .= comments_render_comment($comment, $delete_link);
  190. }
  191. // Let's set our breadcrumbs
  192. $db = get_global_database_handler();
  193. $crumbs = array();
  194. $crumbs[] = array(
  195. 'text' => 'Students',
  196. 'path' => 'student-search',
  197. );
  198. $crumbs[] = array(
  199. 'text' => $db->get_student_name($current_student_id) . " ({$current_student_id})",
  200. 'path' => 'student-profile',
  201. 'query' => "current_student_id={$current_student_id}",
  202. );
  203. fp_set_breadcrumbs($crumbs);
  204. watchdog("comments", "view all $current_student_id", array(), WATCHDOG_DEBUG);
  205. return $rtn;
  206. }
  207. /**
  208. * Displays all comments for a student in a popup window, meant for printing.
  209. */
  210. function comments_popup_display_all_comments() {
  211. global $current_student_id;
  212. $rtn = "";
  213. $delete_link = "";
  214. fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
  215. $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
  216. $comments = comments_get_comments($current_student_id, FALSE, $access_types);
  217. foreach($comments as $comment) {
  218. $rtn .= comments_render_comment($comment, $delete_link);
  219. }
  220. watchdog("comments", "popup_display_all_comments $current_student_id", array(), WATCHDOG_DEBUG);
  221. return $rtn;
  222. }
  223. /**
  224. * Display the comment array in a pretty way.
  225. */
  226. function comments_render_comment($comment, $delete_link = "") {
  227. global $user;
  228. $rtn = "";
  229. // Make sure the user has access to view it!
  230. if (!user_has_permission("view_comments")) {
  231. return "<p>" . t("Sorry, you do not have permission to view comments.") . "</p>";
  232. }
  233. if ($comment["access_type"] == "faculty" && !user_has_permission("view_faculty_comments")) {
  234. return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is marked as faculty-only).") . "</p>";
  235. }
  236. if ($comment["access_type"] == "audit private" && module_enabled('audit')) {
  237. if (!audit_can_access_audit($comment['student_id'])) {
  238. return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is an audit comment).") . "</p>";
  239. }
  240. }
  241. if ($comment["access_type"] == "audit private" && !module_enabled('audit')) {
  242. return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is an audit comment, and the audit module is not enabled).") . "</p>";
  243. }
  244. // If the comment is visible to students, AND the user is a student, make sure the comment is ABOUT THIS STUDENT. This prevents
  245. // students from seeing each others' comments.
  246. if ($user->is_student == TRUE) {
  247. if ($comment['student_id'] != $user->cwid) {
  248. return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is saved for a different student).") . "</p>";
  249. }
  250. }
  251. $access_type = t("Anyone (incl. student)");
  252. if ($comment['access_type'] == 'faculty') {
  253. $access_type = "<i class='fa fa-lock' title='Visibile to Faculty/Staff only'></i> &nbsp; " . t("Faculty/Staff");
  254. }
  255. $rtn .= "<div class='comment-comment comment-comment-" . $comment["access_type"] . "'>
  256. <div class='comment-by-line'>" .$access_type . " " . t("comment by") . " " . fp_get_faculty_name($comment["faculty_id"]) . "</div>
  257. <div class='comment-datetime'>" . format_date(convert_time($comment["posted"]), "pretty") . "</div>
  258. <div class='comment-text'>" . filter_markup($comment["comment"], "full") . "</div>
  259. ";
  260. if ($delete_link) {
  261. $rtn .= "<div class='comment-delete'>$delete_link</div>";
  262. }
  263. $rtn .= "
  264. </div>";
  265. return $rtn;
  266. }
  267. /**
  268. * Returns an array of comments for this student, sorted most recent first.
  269. */
  270. function comments_get_comments($student_id, $bool_included_deleted = FALSE, $access_types = array()) {
  271. $rtn = array();
  272. $deleted_line = "AND delete_flag = 0";
  273. if ($bool_included_deleted) {
  274. $deleted_line = "";
  275. }
  276. $access_type_line = "";
  277. // Build up the "access_type_line" for the query, based on the values
  278. // in the access_types array.
  279. if (count($access_types) > 0) {
  280. $access_type_line = "AND ( ";
  281. foreach ($access_types as $access_type) {
  282. $access_type_line .= " access_type = '$access_type' OR";
  283. }
  284. // remove the last OR
  285. $access_type_line = substr($access_type_line, 0, -2);
  286. $access_type_line .= ")";
  287. }
  288. //if ($access_type == "public" || $access_type == "faculty") {
  289. // $access_type_line = "AND access_type = '$access_type' ";
  290. //}
  291. $res = db_query("SELECT * FROM advising_comments
  292. WHERE student_id = ?
  293. $deleted_line
  294. $access_type_line
  295. ORDER BY posted DESC", array($student_id));
  296. while ($cur = db_fetch_array($res)) {
  297. $rtn[$cur["id"]] = $cur;
  298. }
  299. return $rtn;
  300. }
  301. function comments_get_comment($comment_id) {
  302. $rtn = array();
  303. $res = db_query("SELECT * FROM advising_comments
  304. WHERE `id` = ?
  305. ", array($comment_id));
  306. $cur = db_fetch_array($res);
  307. return $cur;
  308. }
  309. function comments_perm() {
  310. return array(
  311. "view_comments" => array(
  312. "title" => t("Can view comments"),
  313. "description" => t("The user may view comments (only public by default)."),
  314. ),
  315. "view_faculty_comments" => array(
  316. "title" => t("View 'Faculty' comments"),
  317. "description" => t("The user is allowed to view 'Faculty' comments."),
  318. ),
  319. "can_save_comments" => array(
  320. "title" => t("Can save comments"),
  321. "description" => t("The user may save new comments, visible to either everyone or only faculty/staff."),
  322. ),
  323. "can_delete_own_comments_3_months" => array(
  324. "title" => t("Can delete own comments for 3 months"),
  325. "description" => t("The user may delete comments they have made for up to 3 months. After 3 months, they
  326. will not be able to delete their comment."),
  327. ),
  328. );
  329. }

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_cron hook_cron
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.