comments.module
Search API
- 7.x modules/comments/comments.module
- 6.x modules/comments/comments.module
- 4.x modules/comments/comments.module
- 5.x modules/comments/comments.module
File
modules/comments/comments.moduleView source
- <?php
-
- /*
- * This is the Comments module for FlightPath, which lets advisors write comments
- * for students.
- */
-
-
- function comments_menu() {
- $items = array();
-
- $items["comments"] = array(
- "title" => "Comments",
- "page_callback" => "comments_display_main",
- "access_callback" => "comments_can_access_comments",
- "type" => MENU_TYPE_TAB,
- "tab_family" => "system",
- "page_settings" => array (
- "display_currently_advising" => TRUE,
- "display_greeting" => TRUE,
- "page_has_search" => TRUE,
- ),
- "weight" => 35,
- );
-
- $items["comments/save-comment"] = array(
- "page_callback" => "comments_perform_save_comment",
- "access_arguments" => array("can_save_comments"),
- "type" => MENU_TYPE_CALLBACK,
- );
-
- $items["comments/delete-comment"] = array(
- "page_callback" => "comments_perform_delete_comment",
- "access_arguments" => array("can_save_comments"),
- "type" => MENU_TYPE_CALLBACK,
- );
-
-
- $items["comments/popup-display-all-comments"] = array(
- "title" => "Comments",
- "page_callback" => "comments_popup_display_all_comments",
- "access_arguments" => array("view_comments"),
- "page_settings" => array(
- "page_has_search" => FALSE,
- "page_is_popup" => TRUE,
- "page_hide_report_error" => TRUE,
- "bool_print" => TRUE,
- ),
- "type" => MENU_TYPE_CALLBACK,
- );
-
-
- $items["comments/popup-display-comment"] = array(
- "title" => "Comment",
- "page_callback" => "comments_popup_display_comment",
- "access_arguments" => array("view_comments"),
- "page_settings" => array(
- "page_has_search" => FALSE,
- "page_is_popup" => TRUE,
- "page_hide_report_error" => TRUE,
- "bool_print" => TRUE,
- ),
- "type" => MENU_TYPE_CALLBACK,
- );
-
- return $items;
- }
-
-
- function comments_popup_display_comment() {
- $rtn = "";
-
- fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
-
- $id = $_REQUEST["id"];
-
- // Try to render this comment id.
- $comment = comments_get_comment($id);
- $rtn .= comments_render_comment($comment);
-
- return $rtn;
- }
-
-
- /**
- * Used by the menu to determine if the comments tab should appear.
- */
- function comments_can_access_comments() {
- global $current_student_id, $user;
-
- // must be logged in first...
- if (!user_has_permission("access_logged_in_content")) return FALSE;
-
- // Do they have the correct permission to view comments?
- if (!user_has_permission("view_comments")) return FALSE;
-
- if ($current_student_id != "") return TRUE;
-
- return FALSE;
- }
-
-
- /**
- * "delete" a comment (actually, all we do is flag it as deleted)
- */
- function comments_perform_delete_comment() {
- global $current_student_id, $user;
-
- $comment_id = $_REQUEST["comment_id"];
- // Let's get some details about the comment to make sure this user can delete it.
- $comment = comments_get_comment($comment_id);
-
- if ($comment["faculty_id"] == $user->cwid && user_has_permission("can_delete_own_comments_3_months")) {
- // TODO: We should really ALSO check to make sure it's been less than 3 months.
- db_query("UPDATE advising_comments
- SET delete_flag = '1'
- WHERE `id` = '?' ", $comment_id);
- fp_add_message(t("Comment has been deleted successfully."));
- }
-
-
- if ($_GET["destination"] != "") {
- fp_goto($_GET["destination"]);
- }
- else {
- fp_goto("comments");
- }
-
-
- }
-
-
- function comments_comment_form_submit($form, $form_state) {
- global $user, $current_student_id;
- $faculty_cwid = $user->cwid;
- $type = $form_state["values"]["type"];
- $term_id = $form_state["values"]["term_id"];
- $comment = trim($form_state["values"]["comment"]);
-
- // Perform the save!
- if ($comment) {
- db_query("INSERT INTO advising_comments
- (student_id, faculty_id, term_id,
- comment, posted, access_type)
- VALUES
- ('?', '?', '?', '?', '?', '?')
- ", $current_student_id, $faculty_cwid, $term_id, $comment, time(), $type);
-
- fp_add_message(t("Comment saved successfully."));
- }
-
- watchdog("save_comment", "Comment saved");
-
- }
-
-
-
- /**
- * This is the form to enter a new comment.
- */
- function comments_comment_form() {
- global $current_student_id;
-
- $term_id = variable_get("advising_term_id");
-
- fp_set_title("");
-
- $form = array();
-
- $form["type"] = array(
- "type" => "radios",
- "label" => t("Visible to:"),
- "options" => array("public" => t("Anyone (including students)"), "faculty" => t("Faculty/Staff only")),
- "value" => "faculty",
- );
-
- $form["term_id"] = array(
- "type" => "hidden",
- "value" => $term_id,
- );
-
- $form["current_student_id"] = array(
- "type" => "hidden",
- "value" => $current_student_id,
- );
-
- $form["comment"] = array(
- "type" => "textarea",
- );
-
- $form["submit"] = array(
- "type" => "submit",
- "value" => t("Save"),
- );
-
-
-
- return $form;
- }
-
-
-
-
-
-
- /**
- * This displays the primary Comments tab, where we see past comments and can enter a
- * new one (with the right permissions).
- */
- function comments_display_main() {
- global $current_student_id;
- $rtn = "";
-
- fp_add_js(fp_get_module_path("comments") . "/js/comments.js");
- fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
-
- if (user_has_permission("can_save_comments")) {
-
- $form = fp_render_form("comments_comment_form");
-
- $rtn .= fp_render_c_fieldset($form, t("Click to enter comment"), true);
- }
-
- $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
-
- $comments = comments_get_comments($current_student_id, FALSE, $access_types);
- //fpm($comments);
- foreach($comments as $comment) {
-
- $delete_link = "";
- // Should we present a "delete link" to the user for this comment?
- if (user_has_permission("can_delete_own_comments_3_months")) {
- // See if this comment is younger than 3 months.
- $del_range = strtotime("-3 month");
- $then = $comment["posted"];
-
-
- if ($then > $del_range) {
- $delete_link = fp_render_button("Delete", "deleteComment(\"{$comment["id"]}\");");
- }
- }
-
-
- $rtn .= comments_render_comment($comment, $delete_link);
- }
-
-
- return $rtn;
- }
-
-
-
- /**
- * Displays all comments for a student in a popup window, meant for printing.
- */
- function comments_popup_display_all_comments() {
- global $current_student_id;
- $rtn = "";
-
- fp_add_css(fp_get_module_path("comments") . "/css/comments.css");
-
- $access_types = (user_has_permission("view_faculty_comments")) ? array("faculty", "public") : array("public");
-
- $comments = comments_get_comments($current_student_id, FALSE, $access_types);
-
- foreach($comments as $comment) {
- $rtn .= comments_render_comment($comment, $delete_link);
- }
-
-
- return $rtn;
- }
-
-
-
-
- /**
- * Display the comment array in a pretty way.
- */
- function comments_render_comment($comment, $delete_link = "") {
- $rtn = "";
-
- // Make sure the user has access to view it!
- if (!user_has_permission("view_comments")) {
- return "<p>" . t("Sorry, you do not have permission to view comments.") . "</p>";
- }
- if ($comment["access_type"] == "faculty" && !user_has_permission("view_faculty_comments")) {
- return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is marked as faculty-only).") . "</p>";
- }
-
-
- $rtn .= "<div class='comment-comment comment-comment-" . $comment["access_type"] . "'>
- <div class='comment-by-line'>" .ucwords($comment["access_type"]) . " " . t("comment by") . " " . fp_get_faculty_name($comment["faculty_id"]) . "</div>
- <div class='comment-datetime'>" . format_date($comment["posted"], "pretty") . "</div>
- <div class='comment-text'>" . filter_markup($comment["comment"], "full") . "</div>
- ";
- if ($delete_link) {
- $rtn .= "<div class='comment-delete'>$delete_link</div>";
- }
- $rtn .= "
- </div>";
-
-
- return $rtn;
- }
-
-
- /**
- * Returns an array of comments for this student, sorted most recent first.
- */
- function comments_get_comments($student_id, $bool_included_deleted = FALSE, $access_types = array()) {
- $rtn = array();
-
-
- $deleted_line = "AND delete_flag = 0";
- if ($bool_included_deleted) {
- $deleted_line = "";
- }
-
- $access_type_line = "";
-
- // Build up the "access_type_line" for the query, based on the values
- // in the access_types array.
- if (count($access_types) > 0) {
- $access_type_line = "AND ( ";
- foreach ($access_types as $access_type) {
- $access_type_line .= " access_type = '$access_type' OR";
- }
-
- // remove the last OR
- $access_type_line = substr($access_type_line, 0, -2);
-
- $access_type_line .= ")";
- }
-
- //if ($access_type == "public" || $access_type == "faculty") {
- // $access_type_line = "AND access_type = '$access_type' ";
- //}
-
- $res = db_query("SELECT * FROM advising_comments
- WHERE student_id = '?'
- $deleted_line
- $access_type_line
- ORDER BY posted DESC", $student_id);
- while ($cur = db_fetch_array($res)) {
- $rtn[$cur["id"]] = $cur;
- }
-
- return $rtn;
- }
-
-
- function comments_get_comment($comment_id) {
- $rtn = array();
-
-
- $res = db_query("SELECT * FROM advising_comments
- WHERE `id` = '?'
- ", $comment_id);
- $cur = db_fetch_array($res);
-
- return $cur;
- }
-
-
-
-
-
- function comments_perm() {
-
- return array(
- "view_comments" => array(
- "title" => t("Can view comments"),
- "description" => t("The user may view comments (only public by default)."),
- ),
- "view_faculty_comments" => array(
- "title" => t("View 'Faculty' comments"),
- "description" => t("The user is allowed to view 'Faculty' comments."),
- ),
- "can_save_comments" => array(
- "title" => t("Can save comments"),
- "description" => t("The user may save new comments, visible to either everyone or only faculty/staff."),
- ),
- "can_delete_own_comments_3_months" => array(
- "title" => t("Can delete own comments for 3 months"),
- "description" => t("The user may delete comments they have made for up to 3 months. After 3 months, they
- will not be able to delete their comment."),
- ),
-
- );
-
-
-
- }
-
-
-
-
-
-
Functions
Name | 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. |