summaryrefslogtreecommitdiff
path: root/bulk_code.module
blob: f3fa31bc20facd8d5c9637d80ece8371b3b6159f (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php


  function bulk_code_menu() {
          $items = array();
          $items["bulk_code"] = array(
              "title"  => "Bulk code manage",
              "page callback" => "bulk_code_all",
              "access arguments" => array("bulk code comments"),
              "type" => MENU_NORMAL_ITEM
          );
          $items["bulk_code/ajax"] = array(
              "page callback" => "bulk_code_ajax",
              "access callback" => TRUE,
              "type" => MENU_CALLBACK
          );
          $items["bulk_code/books/ajax"] = array(
              "page callback" => "bulk_code_books_ajax",
              "access callback" => TRUE,
              "type" => MENU_CALLBACK
          );
          //  $items["bulk_code/books/all/ajax"] = array(
          //     "page callback" => "bulk_code_books_all_ajax",
          //     "access callback" => TRUE,
          //     "type" => MENU_CALLBACK
          // );
          //  $items["bulk_code/forms/ajax"] = array(
          //     "page callback" => "bulk_code_forms_ajax",
          //     "access callback" => TRUE,
          //     "type" => MENU_CALLBACK
          // );
          //  $items["comments/rand/ajax"] = array(
          //     "page callback" => "bulk_code_rand_refresh_ajax",
          //     "access callback" => TRUE,
          //     "type" => MENU_CALLBACK
          // );
           return $items;
  }  

  function bulk_code_perm() {
        return array(
            "bulk code approve/disapprove",
        );
  }

function bulk_code_all(){      
  $book_titles_q = db_query("
    SELECT * FROM {textbook_companion_preference} WHERE approval_status = 1 OR approval_status = 3 ORDER BY book ASC
    ");
  
        $page_content .= "";
        $page_content .= "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>";
        $page_content .= "<div id = 'bulk_code_manage'>";
        $page_content .= "";
        $page_content .= "<form id='form_bulk_approve' method='post' class= 'form_comments'>";
        //$page_content .= "<input type='hidden' name='postid' value=".$id.">";
        $page_content .= "<label>";
        $page_content .= "<span>Title of the Book: </span><br><br>";
        $page_content.= "<select id='select_book'><option value='0'>Please Select a book</option>";
        while ($book_titles_data = db_fetch_object($book_titles_q))
  {
    $page_content.= "<option value='".$book_titles_data->id."'>".$book_titles_data->book . ' (Written by ' . $book_titles_data->author . ')'."</option>";
  } 
        $page_content .= "</select>"; 
        $page_content .= "<div id= 'download_entire_book'></div>";
        $page_content .= "<div id= 'download_entire_chapter'></div>";
        $page_content .= "<div id= 'download_entire_example'></div>";
        $page_content .= "<div id= 'disapprove_textrea'>";
        $page_content .= "<label>If Dis-Approved please specify reason for Dis-Appr…</label>";
        $page_content .= "<textarea id='dis_approve' rows='5' cols='60'></textarea></div>";
        $page_content .= "<input type='submit' value='Submit' class= 'submit_form_bulk_approve' >";
        $page_content .= "</form>";
        $page_content .= "</div>";

        return $page_content;
  }

      function bulk_code_ajax() {      
      $data = "";
      //$value =$_POST['value'];
      if(isset($_POST['id']) && isset($_POST['type'])){
      $type =mysql_real_escape_string($_POST['type']);
      $id =mysql_real_escape_string($_POST['id']);
      }
      //$chapter_id = 
      if($type== "select_book"){ 
      $query= "
      SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number ASC
      ";
      $book_chapters_q= db_query($query, $id);
      $data .= "<br>";      
      $data .= "<span><a href= 'full_download/book/".$id."'>Download</a> (Download all the approved and unapproved examples of the entire book) </span><br>";
      $data .= "<span><a href= 'textbook_companion/generate_book/".$id."/1'>Download PDF</a> (Download PDF of all the approved and unapproved examples of the entire book) </span><br>";
      $data .= "<span><a href= 'textbook_companion/delete_book/".$id."'>Regenerate PDF</a> (Manually Regenerate PDF of the entire book) </span><br><br>";
      $data .= "<input type='radio' class = 'form_action' name='approve_disaprave_book' value='1'>Approve Entire Book<br>";     
      $data .= "<input type='radio'  class = 'form_action' name='approve_disaprave_book' value='4'>Delete Entire Book Including Proposal<br><br>";
      echo $data;
      exit();
    }else {

      $data .= "Please try again";
       echo json_encode($data);
      exit();
    }
    
  }

  function bulk_code_books_ajax() {
    global $user;
         $data .= "";
        if(isset($_POST['pref_id']) && isset($_POST['action'])){
        $pref_id = mysql_real_escape_string($_POST['pref_id']);
        $action = mysql_real_escape_string($_POST['action']);
        $disapproval_reason = mysql_real_escape_string($_POST['dis_approve_reason']);
        }
        if($action == 1)
        {
          $data .= "";
              /* approving entire book */
          $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $pref_id);
          while ($chapter_data = db_fetch_object($chapter_q))
          {
            db_query("UPDATE {textbook_companion_example} SET approval_status = 1, approver_uid = %d WHERE chapter_id = %d AND approval_status = 0", $user->uid, $chapter_data->id);
          } 
              $email_subject = t('Your uploaded examples have been approved');
              $email_body = t('Your all the uploaded examples for the book have been approved.');
              if ($email_subject)
            {
              $email_to = $user->mail;
              $param['standard']['subject'] = $email_subject;
              $param['standard']['body'] = $email_body;
              if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
                  $data .='Error sending email message.';
            }    
            $data .= "Approved Entire Book";
            echo $data;
            exit();     
                       
        }elseif($action == 4){
          $root_path = textbook_companion_path();
              $data .= "";
              /* check if dependency files are present */
              // $dep_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE preference_id = %d", $pref_id);
              // if ($dep_data = db_fetch_object($dep_q))
              // {
              //   drupal_set_message(t("Cannot delete book since it has dependency files that can be used by others. First delete the dependency files before deleing the Book."), 'error');
               
              // }
              if (delete_books($pref_id))
              { 
                  $dir_path = $root_path . $pref_id;
                  if (is_dir($dir_path))
                  {
                      $res = rmdir($dir_path);
                      if (!$res)
                      {
                          $data .= "Cannot delete Book directory : " . $dir_path . ". Please contact administrator.";       
                           echo $data; 
                           exit();                 
                      }
                  } else {
                      $data = "Book directory not present : " . $dir_path . ". Skipping deleting book directory.";
                       echo $data;
                       exit();
                  }
                  /* deleting preference and proposal */
                  $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $pref_id);
                  $preference_data = db_fetch_object($preference_q);
                  $proposal_id = $preference_data->proposal_id;
                  db_query("DELETE FROM {textbook_companion_preference} WHERE proposal_id = %d", $proposal_id);
                  db_query("DELETE FROM {textbook_companion_proposal} WHERE id = %d", $proposal_id);
                  $data .= 'Deleted Book Proposal.';
                  /* email */
                  $email_subject = t('Your uploaded examples including the book proposal have been deleted');
                  $email_body = t('Your all the uploaded examples including the book have been deleted permanently.

Reason for deletion:

' . $disapproval_reason); 
          if ($email_subject)
            {
              $email_to = $user->mail;
              $param['standard']['subject'] = $email_subject;
              $param['standard']['body'] = $email_body;
              if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
                  $data .='Error sending email message.';
                  echo $data;
                   exit();   
            }              
                  
              } else {
                   $data .= "Error Dis-Approving and Deleting Entire Book."; 
                   echo $data;
                   exit();                 
              }
                                 
            }      
}

// function bulk_code_books_all_ajax($id) {
//         $data = "";
//         $base_url = $GLOBALS['base_url']. '/sites/all/modules/tbc_comments/image/';
//         $comment_query = db_query("
//                                     SELECT * FROM tbc_comments WHERE post_id = %s ORDER BY id DESC
//                                 "); 
//         $data .= "<h2>Comments.....</h2>";
//         $data .= "<div id='comment-block_".$id."' class='comment-block'>";
//         while ($comments_data = db_fetch_object($comment_query,$id)) {
//           $data .= "<div class='comment-item'>";
//           $data .= "<div class='comment-avatar'>"; 
//           $data .= "<img src='".$base_url."default_avatar.gif' alt='avatar'>";
//           $data .= "</div>";
//           $data .= "<div class='comment-post'>";
//           $data .= "<h3>".$comments_data->name."<span> said.... </span></h3><div style='float:right;margin-top: -27px;margin-right: -81px;'>".$comments_data->date."</div>";
//           $data .= "<p>".$comments_data->comment."</p>";
//           $data .= "</div></div>";
//         }        
//           $data .= "</div>";
//           echo $data;
//           exit();

// }

// function bulk_code_forms_ajax($id){
//             $id = $_POST['id'];
//             $data = "";


  
//         $base_url = $GLOBALS['base_url']. '/sites/all/modules/tbc_comments/image/';
//         $comment_query = db_query("
//                       SELECT * FROM tbc_comments WHERE post_id = %d ORDER BY id DESC LIMIT 15
//                     ", $id);

//                     $data .= "<div id ='close_button_".$id."' class = 'close_box'>&#x2716;</div>";                   
//                     $data .= "<h2>Comments.....</h2>";
//                     $data .= "<div id='comment-block' class='comment-block'>";                    
//                    while ($comments_data = db_fetch_object($comment_query)) {
//                     $data .= "<div class='comment-item' id = 'comment-item_".$comments_data->id."'>";
//                     $data .= "<div class='comment-avatar'>";  
//                     $data .= "<img src='".$base_url."default_avatar.gif' alt='avatar'>";                    
//                     $data .= "</div>";
//                     $data .= "<div class='comment-post'>";
//                     $data .= "<h3>".$comments_data->name."<span> said....</span></h3><div style='float:right;margin-top: -27px;margin-right: -81px;'>".$comments_data->date."</div>";
//                     $data .= "<p>".$comments_data->comment."</p>";
//                     $data .= "</div></div>";
//                            }
//                     $data .= "</div>";
//                     $data .= "<form id='form_".$id."' method='post' class= 'form_comments' data-fid='".$id."'>";
//                     $data .= "<input type='hidden' name='postid' value=".$id.">";
//                     $data .= "<label>";
//                     $data .= "<span>Name *</span>";
//                     $data .= "<input type='text' name='name' id='comment-name' class= 'form_input' placeholder='Your name here.... 'required>";
//                     $data .= "</label><label>";
//                     $data .= "<span>Email *</span>";
//                     $data .= "<input type='email' name='mail' id='comment-mail' class= 'form_input' placeholder='Your mail here....' required>";
//                     $data .= " </label><label>";                    
//                     $data .= "<input type='checkbox' name='notify' value='1'>";
//                     $data .= "<span> I want to be notified. </span>";
//                     $data .= " </label><label>";
//                     $data .= "<span>Your comment *</span>";
//                     $data .= " <textarea name='comment' id='comment' cols='30' rows='10' class= 'form_input' placeholder='Type your comment here....' required></textarea>";
//                     $data .= "</label>";                                                    
//                     $data .= " <input type='submit' id='submit_".$id."' value='Submit Comment' class= 'submit_form' data-sid = '".$id."' >";                     
//                     $data .= "</form>";
//                     $data .= "<div id = 'rand_fun' style='margin-top:-49px'>";
//                          $num1 =rand (1,5);
//                          $num2 =rand (1,5);
//                          $num3 = $num1 + $num2;
//                     $data .= "<label>";
//                     $data .= "<font style='color:red'>".$num1. " + ". $num2. "</font>&nbsp;&nbsp;&nbsp;(Are you human, or spambot?)*";
//                     $data .= "</label>";                    
//                     $data .= "<input type='text' name='rand_num' id='rand_num' class= 'num_input' data-num= '".$num3."' placeholder='Enter the total of above numbers here.... ' required></div>";                  
//                     $data .= "</div></div>";
                  
//                     echo $data;
                    
                    
//                     exit();
//   }

//   function bulk_code_rand_refresh_ajax(){
//                     $data = "";
//                          $num1 =rand (1,5);
//                          $num2 =rand (1,5);
//                          $num3 = $num1 + $num2;
//                     $data .= "<div id = 'rand_fun' style='margin-top:-49px'>";
//                     $data .= "<label>";
//                     $data .= "<font style='color:red'>".$num1. " + ". $num2. "</font>&nbsp;&nbsp;&nbsp;(Are you human, or spambot?)*";
//                     $data .= "</label>";                    
//                     $data .= "<input type='text' name='rand_num' id='rand_num' class= 'num_input' data-num= '".$num3."' placeholder='Enter the total of above numbers here.... ' required></div>";
//                     echo $data;
//                     exit();

//   }

  function bulk_code_init() {
        //drupal_add_css(drupal_get_path("module", "tbc_comments") . "/css/tbc_comments_styles.css");
        //drupal_add_css(drupal_get_path("module", "tbc_comments") . "/css/tbc_comments.css");
        drupal_add_js(drupal_get_path("module", "bulk_code") . "/js/bulk_code_ajax.js");        
        drupal_add_js(drupal_get_path("module", "bulk_code") . "/js/jquery-1.4.1.min.js");
        drupal_add_js(drupal_get_path("module", "bulk_code") . "/js/jquery-1.2.6.min.js");
        
        
  }


function delete_books($book_id)
{
  $status = TRUE;
  $root_path = textbook_companion_path();

  $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $book_id);
  $preference_data = db_fetch_object($preference_q);
  if (!$preference_data)
  {
    drupal_set_message('Invalid book.', 'error');
    return FALSE;
  }

  /* delete chapters */
  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $preference_data->id);
  while ($chapter_data = db_fetch_object($chapter_q))
  {
    if (!delete_chapter($chapter_data->id))
    {
      $status = FALSE;
    }
  }
  return $status;
}

?>