comments.module
Search API
- 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,
- ),
- "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" => "Comment History",
- "page_callback" => "comments_popup_display_all_comments",
- "access_arguments" => array("view_comments"),
- "page_settings" => array(
- "page_is_popup" => TRUE,
- ),
- "type" => MENU_TYPE_TAB,
- "tab_family" => "comments_popup_all",
- );
-
-
- $items["comments/popup-display-comment"] = array(
- "title" => "Comment",
- "page_callback" => "comments_popup_display_comment",
- "access_arguments" => array("view_comments"),
- "page_settings" => array(
- "page_is_popup" => TRUE,
- ),
- "type" => MENU_TYPE_TAB,
- "tab_family" => "comments_popup",
- );
-
- return $items;
- }
-
-
-
-
- /**
- * hook_cron
- */
- function comments_cron() {
-
- // Delete comments which has been marked with "delete_flag = 1"
-
- $last_run = intval(variable_get("comments_last_run_delete_flag_removal", 0));
- $check_against = strtotime("NOW - 7 DAYS"); // don't run any more often than once every 7 days
- $c = 0;
- if ($check_against > $last_run) {
-
- $res = db_query("DELETE FROM advising_comments
- WHERE delete_flag = 1 ");
- $c = db_affected_rows($res);
-
- watchdog("comments", "Delete from db complete. $c items removed.", array(), WATCHDOG_DEBUG);
- variable_set("comments_last_run_delete_flag_removal", time());
-
-
- } // check against > last_run, so we should do it.
-
-
-
- } // hook_cron
-
-
-
-
-
-
-
-
-
- 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);
-
- watchdog("comments", "popup_display_comment id:$id", array(), WATCHDOG_DEBUG);
-
-
- 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."));
- }
-
- watchdog("comments", "deleted comment for:$current_student_id, comment_id:$comment_id", array());
-
-
- 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 for $current_student_id: $comment");
-
-
- }
-
-
-
- /**
- * This is the form to enter a new comment.
- */
- function comments_comment_form() {
- global $current_student_id;
-
- $school_id = db_get_school_id_for_student_id($current_student_id);
-
- $term_id = variable_get_for_school("advising_term_id", '', $school_id);
-
- fp_set_title("");
-
- $form = array();
-
- $form["type"] = array(
- "type" => "radios",
- "label" => t("Visible to:"),
- "options" => array("public" => t("Anyone (incl student)"), "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_editor",
- );
-
- $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, $screen, $user;
- $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, " new-comment-fs");
- }
-
- fp_set_title('');
-
- $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) {
-
- $delete_link = "";
- // Should we present a "delete link" to the user for this comment?
- if (user_has_permission("can_delete_own_comments_3_months") && $comment['faculty_id'] == $user->cwid) {
- // See if this comment is younger than 3 months.
- $del_range = strtotime("now -3 months");
- $then = intval($comment["posted"]);
-
-
- if ($then > $del_range) {
-
- $delete_link = "<a href='javascript:deleteComment(\"{$comment["id"]}\");' class='button'>" . t("Delete") . "</a>";
- }
- }
-
-
- $rtn .= comments_render_comment($comment, $delete_link);
- }
-
- // Let's set our breadcrumbs
- $db = get_global_database_handler();
- $crumbs = array();
- $crumbs[] = array(
- 'text' => 'Students',
- 'path' => 'student-search',
- );
- $crumbs[] = array(
- 'text' => $db->get_student_name($current_student_id) . " ({$current_student_id})",
- 'path' => 'student-profile',
- 'query' => "current_student_id={$current_student_id}",
- );
- fp_set_breadcrumbs($crumbs);
-
- watchdog("comments", "view all $current_student_id", array(), WATCHDOG_DEBUG);
-
- 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 = "";
- $delete_link = "";
- 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);
- }
-
- watchdog("comments", "popup_display_all_comments $current_student_id", array(), WATCHDOG_DEBUG);
-
-
- return $rtn;
- }
-
-
-
-
- /**
- * Display the comment array in a pretty way.
- */
- function comments_render_comment($comment, $delete_link = "") {
- global $user;
- $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>";
- }
- if ($comment["access_type"] == "audit private" && module_enabled('audit')) {
- if (!audit_can_access_audit($comment['student_id'])) {
- return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is an audit comment).") . "</p>";
- }
- }
- if ($comment["access_type"] == "audit private" && !module_enabled('audit')) {
- 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>";
- }
-
-
- // If the comment is visible to students, AND the user is a student, make sure the comment is ABOUT THIS STUDENT. This prevents
- // students from seeing each others' comments.
- if ($user->is_student == TRUE) {
- if ($comment['student_id'] != $user->cwid) {
- return "<p>" . t("Sorry, but you do not have permission to view the requested comment (it is saved for a different student).") . "</p>";
- }
- }
-
-
-
- $access_type = t("Anyone (incl. student)");
- if ($comment['access_type'] == 'faculty') {
- $access_type = "<i class='fa fa-lock' title='Visibile to Faculty/Staff only'></i> " . t("Faculty/Staff");
- }
-
- $rtn .= "<div class='comment-comment comment-comment-" . $comment["access_type"] . "'>
- <div class='comment-by-line'>" .$access_type . " " . t("comment by") . " " . fp_get_faculty_name($comment["faculty_id"]) . "</div>
- <div class='comment-datetime'>" . format_date(convert_time($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", array($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` = ?
- ", array($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_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. |