summaryrefslogtreecommitdiff
path: root/cloud_comments.module
blob: 2addaf96da911708e06d0b42f4da8bafc2a9bdbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

function cloud_comments_help($path, $arg) {
    $output = '';
    switch($path) {
        case "admin/help#cloud_comments":
            $output = "<p>" . t("Displays the Comments on cloud and enables the admin to reply to them via e-mail: textbook@scilab.in") . "</p>";
            break;
    }
    return $output;
} // function cloud_comments_help

function cloud_comments_perm() {
    return array("access cloud_comments", "reply cloud_comments");
} // function cloud_comments_perm

function cloud_comments_all($example_id=0) {
    $types = array(
            "None",
            "Blank Code / Incorrect code",
            "Output error",
            "Execution error",
            "Missing example(s)",
            "None",
            "Blank output",
            "Any other"
    );
    $categories = array(
            "Others",
            "Fluid Mechanics",
            "Control Theory &amp; Control Systems",
            "Chemical Engineering",
            "Thermodynamics",
            "Mechanical Engineering",
            "Signal Processing",
            "Digital Communications",
            "Electrical Technology",
            "Mathematics &amp; Pure Science",
            "Analog Electronics",
            "Digital Electronics",
            "Computer Programming",
            "Others"
    );

    $page_content = "";
    if($example_id) {
        $query = "SELECT scc.*, tcc.number AS chapter_no, tce.number AS example_no FROM scilab_cloud_comment scc LEFT JOIN textbook_companion_chapter tcc ON tcc.id = scc.chapter LEFT JOIN textbook_companion_example tce ON tce.id = scc.example WHERE scc.example = {$example_id} ORDER BY scc.date DESC";
    } else {
        $query = "SELECT scc.*, tcc.number AS chapter_no, tce.number AS example_no FROM scilab_cloud_comment scc LEFT JOIN textbook_companion_chapter tcc ON tcc.id = scc.chapter LEFT JOIN textbook_companion_example tce ON tce.id = scc.example ORDER BY scc.date DESC";
    }
    $query_result = pager_query($query, 5, 0 );
    //$query_result = db_query($query);

    while ($row = db_fetch_object($query_result)) {

        $border = $row->reply_status?"sent":"not-sent";
        $page_content .= t("<div class='feedback {$border}'>");
        
        $page_content .= t("<div class='timestamp'>") . $row->date . t("</div>");
        if (user_access("reply cloud_comments")) {
            $page_content .= t("<div class='from'> <b>From:</b> ") . $row->email . t("</div>");
        }

        $page_content .= t("<div class='type'> <b>Type:</b> ") . $types[$row->type] . t("</div>");

        $q = "SELECT book from {textbook_companion_preference} where id = '%d'";
        $qr = db_query($q, $row->books);
        $obj = db_fetch_object($qr);
        $page_content .= t("<div class='type'> <b>Category: </b>") . $categories[$row->category] . t("</div>");
        $page_content .= t("<div class='type'> <b>Book:</b> ") . $obj->book . t("</div>");

        $q = "SELECT name from {textbook_companion_chapter} where id = '%d'";
        $qr = db_query($q, $row->chapter);
        $obj = db_fetch_object($qr);
        $chapter = $obj->name?$obj->name:"None";
        $page_content .= t("<div class='type'> <b>Chapter: </b> ") . $chapter . ($row->chapter_no?" (<b>{$row->chapter_no}</b>)":"") . t("</div>");


        $q = "SELECT caption from {textbook_companion_example} where id = '%d'";
        $qr = db_query($q, $row->example);
        $obj = db_fetch_object($qr);
        $example_link = $obj->caption?l($obj->caption,"http://cloud.scilab.in/index.php?eid=".$row->example):"None";

        $page_content .= t("<div class='example'> <b>Example: </b>") . $example_link . ($row->example_no?" (<b>{$row->example_no}</b>)":"") . t("</div>");

        $page_content .= t("<div class='comment'> <br><b>Comment:</b><br>") . $row->comment . t("</div>");

        if ($row->reply) {
            if(!$row->user){
          $page_content .= t("<div class='reply'> <br><b>Reply Sent:</b><br>") . $row->reply . t("</div>");           
        }else{         
          $page_content .= t("<div class='reply'> <br><b>Reply Sent By ".$row->user.":</b><br>") . $row->reply . t("</div>");
        }
        }
        else {
            if (user_access("reply cloud_comments")) {
                $page_content .= t("<span class='btn-reply'><a class='form_{$row->id}' href='#form_{$row->id}'>Reply</a></span><br>");
                $page_content .= t("<div id='form_{$row->id}' class='form-wrapper'>");
                $page_content .= drupal_get_form("cloud_comments_reply_form_".$row->id, $row->id);
                $page_content .= t("</div>");
                $page_content .= t("<a href='cloud_comments/delete/{$row->id}' id='del_{$row->id}' class='delete'>x</a>");
            }
        }
        $page_content .= t("</div>");
    } // while ends
	$page_content .= theme('pager', NULL, 5, 0);
    return $page_content;
} // function cloud_comments_all

function cloud_comments_forms($form_id) {
  $forms = array();
  if (strpos($form_id, 'cloud_comments_reply_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'cloud_comments_reply_form',
    );
  }
  return $forms;
} // function cloud_comments_forms

function cloud_comments_reply_form($form_state, $comment_id) {
    $form = array();
    $form["#submit"] = array(
        'cloud_comments_reply_form_submit',
    );

    $form["content"] = array(
        '#type' => 'textarea',
        '#wysiwyg' => True
    );
    $form["hidden"] = array(
        '#type' => 'hidden',
        '#value' => $comment_id
    );
    $form["submit"] = array(
        '#type' => 'submit',
        '#value' => 'submit'
    );

    return $form;
} // function reply_form

function cloud_comments_reply_form_submit($form, &$form_state) {
    global $user;
     
    $query = "UPDATE {scilab_cloud_comment} SET reply = '%s', user= '%s', reply_status = 1 WHERE id = %d";
    $query_result = db_query($query, $form_state["values"]["content"], $user->name, $form_state["values"]["hidden"]);
    
    $email_query = "SELECT email FROM {scilab_cloud_comment} WHERE id = %d";
    $result = db_query($email_query, $form_state["values"]["hidden"]);
    $obj = db_fetch_object($result);

    $message = array(
        'to' => $obj->email,
        'subject' => t("Comment Reply"),
        'body' => t($form_state["values"]["content"]),
        'headers' => array("From" => "textbook@scilab.in", "Content-type" => "text/html; charset=iso-8859-1")

    );
    drupal_mail_send($message);

    if (!$query_result) {
        drupal_set_message("Database updation failed");
    } else {
        drupal_set_message("Message sent. . .");
    }

} // function reply_form_submit

function cloud_comments_delete($comment_id=NULL) {
    if ($comment_id && user_access("reply cloud_comments")) {
        $query = "DELETE FROM {scilab_cloud_comment} WHERE id = %d";
        $query_result = db_query($query, $comment_id);
        if (db_affected_rows() > 0) {
            drupal_set_message("Deleted");
        } else {
            drupal_set_message("MySQL Error: row deletion failed.");
        }
    }
    drupal_goto("cloud_comments");
}

function cloud_comments_menu() {
    $items = array();
    $items['cloud_comments'] = array(
        'title' => 'Cloud Comments',
        'page callback' => 'cloud_comments_all',
        'access arguments' => array("access cloud_comments"),
        'type' => MENU_CALLBACK,
    );

    $items['cloud_comments/delete'] = array(
        'title' => 'Cloud Comments',
        'page callback' => 'cloud_comments_delete',
        'access arguments' => array("access cloud_comments"),
        'type' => MENU_CALLBACK,
    );

    return $items;
} // function cloud_comments_menu

function cloud_comments_init() {
    drupal_add_css(drupal_get_path("module", "cloud_comments") . "/css/cloud_comments.css");
    drupal_add_js(drupal_get_path("module", "cloud_comments") . "/js/cloud_comments.js");
} //function cloud_comments_init

?>