summaryrefslogtreecommitdiff
path: root/textbook_companion.module
blob: 6e38740ae698c62427fdc86229d50cd9d9604c54 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
<?php
// $Id$

/**
 * Implementation of hook_menu().
 */
function textbook_companion_menu()
{
  $items = array();

  /* users */
  $items['proposal'] = array(
    'title' => 'Book Proposal Form',
    'description' => 'Book Proposal Form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_proposal_form'),
    'access callback' => 'user_access',
    'access arguments' => array('create book proposal'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'proposal.inc',
  );
  
  /* for reviewers */
  $items['manage_proposal'] = array(
    'title' => 'Manage Book Proposals',
    'description' => 'Manage Book Proposals',
    'page callback' => '_proposal_pending',
    'access callback' => 'user_access',
    'access arguments' => array('approve book proposal'),
    'file' => 'manage_proposal.inc',
  );
  $items['manage_proposal/pending'] = array(
    'title' => 'Pending Proposals',
    'description' => 'Pending Proposals Queue',
    'page callback' => '_proposal_pending',
    'access callback' => 'user_access',
    'access arguments' => array('approve book proposal'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 1,
    'file' => 'manage_proposal.inc',
  );
  $items['manage_proposal/all'] = array(
    'title' => 'All Proposals',
    'description' => 'All Proposals',
    'page callback' => '_proposal_all',
    'access callback' => 'user_access',
    'access arguments' => array('approve book proposal'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'manage_proposal.inc',
  );
  /*$items['manage_proposal/category'] = array(
    'title' => 'Categorize',
    'description' => 'Categorize Books',
    'page callback' => '_category_all',
    'access callback' => 'user_access',
    'access arguments' => array('approve book proposal'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'manage_proposal.inc',
  );*/
  $items['manage_proposal/approve'] = array(
    'title' => 'Proposal Approval',
    'description' => 'Proposal Approval',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('proposal_approval_form'),
    'access arguments' => array('approve book proposal'),
    'type' => MENU_CALLBACK,
    'file' => 'manage_proposal.inc',
  );
  $items['manage_proposal/status'] = array(
    'title' => 'Proposal Status',
    'description' => 'Proposal Status',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('proposal_status_form'),
    'access arguments' => array('approve book proposal'),
    'type' => MENU_CALLBACK,
    'file' => 'manage_proposal.inc',
  );
  $items['manage_proposal/edit'] = array(
    'title' => 'Edit Proposal',
    'description' => 'Edit Proposal',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('proposal_edit_form'),
    'access arguments' => array('edit book proposal'),
    'type' => MENU_CALLBACK,
    'file' => 'manage_proposal.inc',
  );
  $items['manage_proposal/category/edit'] = array(
    'title' => 'Edit Category',
    'description' => 'Edit category',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('category_edit_form'),
    'access arguments' => array('edit book proposal'),
    'type' => MENU_CALLBACK,
    'file' => 'manage_proposal.inc',
  );
  $items['textbook_companion/code'] = array(
    'title' => 'Code Submission',
    'description' => 'Code Submission',
    'page callback' => 'list_chapters',
    'access callback' => 'user_access',
    'access arguments' => array('upload code'),
    'file' => 'general.inc',
  );
  $items['textbook_companion/code/list_chapters'] = array(
    'title' => 'List Chapters',
    'description' => 'List Chapters', 
    'page callback' => 'list_chapters',
    'access arguments' => array('upload code'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'general.inc',
    'weight' => 1,
  );
  $items['textbook_companion/code/upload'] = array(
    'title' => 'Code Submission',
    'description' => 'Code Submission',
    'page callback' => 'upload_examples',
    'access arguments' => array('upload code'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'code.inc',
    'weight' => 2,
  );
  $items['textbook_companion/code/edit'] = array(
    'title' => 'Edit Example',
    'description' => 'Edit Example', 
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_examples_edit_form'),
    'access arguments' => array('edit uploaded code'),
    'type' => MENU_CALLBACK,
    'file' => 'editcode.inc',
  );
  $items['textbook_companion/code/delete'] = array(
    'title' => 'Delete Example',
    'description' => 'Delete Example', 
    'page callback' => '_upload_examples_delete',
    'access arguments' => array('upload code'),
    'type' => MENU_CALLBACK,
    'file' => 'code.inc',
  );
  $items['textbook_companion/code/chapter/edit'] = array(
    'title' => 'Edit Chapter Title',
    'description' => 'Edit Chapter Title',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('edit_chapter_title_form'),
    'access arguments' => array('upload code'),
    'type' => MENU_CALLBACK,
    'file' => 'editcode.inc',
  );
  $items['textbook_companion/code/list_examples'] = array(
    'title' => 'List Examples',
    'description' => 'List Examples', 
    'page callback' => 'list_examples',
    'access arguments' => array('upload code'),
    'type' => MENU_CALLBACK,
    'file' => 'general.inc',
    'weight' => 3,
  );
  
  $items['code_approval'] = array(
    'title' => 'Manage Code Approval',
    'description' => 'Manage Code Approval',
    'page callback' => 'code_approval',
    'access arguments' => array('approve code'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'code_approval.inc',
  );
  $items['code_approval/approve'] = array(
    'title' => 'Code Approval',
    'description' => 'Code Approval',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('code_approval_form'),
    'access arguments' => array('approve code'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 1,
    'file' => 'code_approval.inc',
  );
  $items['code_approval/bulk'] = array(
    'title' => 'Bulk Manage',
    'description' => 'Bulk Mangage',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bulk_approval_form'),
    'access arguments' => array('bulk manage code'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'code_approval.inc',
  );
  $items['code_approval/editcode'] = array(
    'title' => 'Admin Edit Example',
    'description' => 'Admin Edit Example',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_examples_admin_edit_form'),
    'access arguments' => array('approve code'),
    'type' => MENU_CALLBACK,
    'weight' => 3,
    'file' => 'editcodeadmin.inc',
  );
  $items['code_approval/notes'] = array(
    'title' => 'Notes for Reviewers',
    'description' => 'Notes for Reviewers',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_notes_form'),
    'access arguments' => array('bulk manage code'),
    'type' => MENU_CALLBACK,
    'weight' => 4,
    'file' => 'notes.inc',
  );
  
  /* reviewer download */
  $items['full_download/chapter'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_full_chapter',
    'access arguments' => array('approve code'),
    'type' => MENU_CALLBACK,
    'file' => 'full_download.inc',
  );
  $items['full_download/book'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_full_book',
    'access arguments' => array('approve code'),
    'type' => MENU_CALLBACK,
    'file' => 'full_download.inc',
  );
  
  $items['textbook_run'] = array(
    'title' => 'Download Codes',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('textbook_companion_run_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'run.inc',
  );
  
  /* general purpose callbacks */
  $items['textbook_companion/ajax'] = array(
    'title' => 'Ajax',
    'description' => 'Ajax',
    'page callback' => 'textbook_companion_ajax',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  /* download callbacks */
  $items['download/file'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_example_file',
    'access arguments' => array('download code'),
    'type' => MENU_CALLBACK,
    'file' => 'download.inc',
  );
  $items['download/example'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_example',
    'access arguments' => array('download code'),
    'type' => MENU_CALLBACK,
    'file' => 'download.inc',
  );
  $items['download/chapter'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_chapter',
    'access arguments' => array('download code'),
    'type' => MENU_CALLBACK,
    'file' => 'download.inc',
  );
  $items['download/book'] = array(
    'title' => 'Code Download',
    'description' => 'Code Download', 
    'page callback' => 'textbook_companion_download_book',
    'access arguments' => array('download code'),
    'type' => MENU_CALLBACK,
    'file' => 'download.inc',
  );
  /* for admin */
  $items['admin/settings/book_companion'] = array(
    'title' => 'Book Companion Settings',
    'description' => 'Book Companion Settings', 
    'page callback' => 'drupal_get_form',
    'page arguments' => array('textbook_companion_settings_form'),
    'access arguments' => array('administer book companion'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'settings.inc',
  );

  return $items;
}

/**
 * Implementation of hook_perm().
 */
function textbook_companion_perm() {
  return array('create book proposal', 'approve book proposal', 'approve code', 'upload code', 'edit uploaded code', 'download code', 'create feedback', 'bulk manage code', 'bulk delete code', 'edit book proposal', 'administer book companion', 'generate book');
}

/* AJAX CALLS */
function textbook_companion_ajax()
{
  $query_type = arg(2);
  if ($query_type == 'chapter_title')
  {
    $chapter_number = arg(3);
    $preference_id = arg(4);
    $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE number = %d AND preference_id = %d LIMIT 1", $chapter_number, $preference_id);
    if ($chapter_data = db_fetch_object($chapter_q))
    {
      echo $chapter_data->name;
      return;
    }
  } else if ($query_type == 'example_exists') {
    $chapter_number = arg(3);
    $preference_id = arg(4);
    $example_number = arg(5);

    $chapter_id = 0;
    $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE number = %d AND preference_id = %d LIMIT 1", $chapter_number, $preference_id);
    if (!$chapter_data = db_fetch_object($chapter_q))
    {
      echo '';
      return;
    } else {
      $chapter_id = $chapter_data->id;
    }

    $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND number = '%s' LIMIT 1", $chapter_id, $example_number);
    if ($example_data = db_fetch_object($example_q))
    {
      if ($example_data->approval_status == 1)
        echo 'Warning! Example already approved. You cannot upload the same example again.';
      else
        echo 'Warning! Example already uploaded. Delete the example and reupload it.';
      return;
    }
  }
  echo '';
}

/*************************** VALIDATION FUNCTIONS *****************************/
function textbook_companion_check_valid_filename($file_name) {
  if (!preg_match('/^[0-9a-zA-Z\_\.]+$/', $file_name))
    return FALSE;
  else
    if (substr_count($file_name, ".") > 1)
      return FALSE;
    else
      return TRUE;
}

function check_name($name = '') {
  if (!preg_match('/^[0-9a-zA-Z\ ]+$/', $name))
    return FALSE;
  else
    return TRUE;
}

function check_chapter_number($name = '') {
  if (!preg_match('/^([0-9])+(\.([0-9a-zA-Z])+)+$/', $name))
    return FALSE;
  else
    return TRUE;
}

function textbook_companion_path() {
  return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'uploads/';
}

/****************************** DELETION FUNCTIONS ****************************/

function delete_example($example_id)
{
  global $user;
  $root_path = textbook_companion_path();
  $status = TRUE;

  $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d", $example_id);
  $example_data = db_fetch_object($example_q);
  if (!$example_data)
  {
    drupal_set_message(t('Invalid example.'), 'error');
    return FALSE;
  }

  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id);
  $chapter_data = db_fetch_object($chapter_q);
  if (!$chapter_data)
  {
    drupal_set_message(t('Invalid example chapter.'), 'error');
    return FALSE;
  }

  /* deleting example files */
  $examples_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_id);
  while ($examples_files_data = db_fetch_object($examples_files_q))
  {
    if (!file_exists($root_path . $examples_files_data->filepath))
    {
      $status = FALSE;
      drupal_set_message(t('Error deleting !file. File does not exists.', array('!file' => $examples_files_data->filepath)), 'error');
      continue;
    }

    /* removing example file */
    if (!unlink($root_path . $examples_files_data->filepath))
    {
      $status = FALSE;
      drupal_set_message(t('Error deleting !file', array('!file' => $examples_files_data->filepath)), 'error');

      /* sending email to admins */
      $email_to = variable_get('textbook_companion_emails', '');
      $param['standard']['subject'] = "[ERROR] Error deleting example file";
      $param['standard']['body'] = "Error deleting example files by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " :
        example id : " . $example_id . "
        file id : " .  $examples_files_data->id . "
        file path : " . $examples_files_data->filepath;
      if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
        drupal_set_message('Error sending email message.', 'error');
    } else {
      /* deleting example files database entries */
      db_query("DELETE FROM {textbook_companion_example_files} WHERE id = %d", $examples_files_data->id);
    }
  }

  if (!$status)
    return FALSE;

  /* removing example folder */
  $ex_path = $chapter_data->preference_id . '/' . 'CH' . $chapter_data->number . '/' . 'EX' . $example_data->number;
  $dir_path = $root_path . $ex_path;
  if (is_dir($dir_path))
  {
    if (!rmdir($dir_path))
    {
      drupal_set_message(t('Error deleting folder !folder', array('!folder' => $dir_path)), 'error');

      /* sending email to admins */
      $email_to = variable_get('textbook_companion_emails', '');
      $param['standard']['subject'] = "[ERROR] Error deleting folder";
      $param['standard']['body'] = "Error deleting folder " . $dir_path . " by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
      if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
        drupal_set_message('Error sending email message.', 'error');
      return FALSE;
    }
  } else {
    drupal_set_message(t('Cannot delete example folder. !folder does not exists.', array('!folder' => $dir_path)), 'error');
    return FALSE;
  }

  /* deleting exmaple database entries */
  db_query("DELETE FROM {textbook_companion_example} WHERE id = %d", $example_id);

  return $status;
}

function delete_file($file_id)
{
  $root_path = textbook_companion_path();

  $file_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE id = %d LIMIT 1", $file_id);
  $file_data = db_fetch_object($file_q);
  if (!$file_data)
  {
    drupal_set_message('Invalid file specified.', 'error');
    return FALSE;
  }

  if (!file_exists($root_path . $file_data->filepath))
  {
    drupal_set_message(t('Error deleting !file. File does not exists.', array('!file' => $file_data->filepath)), 'error');
    return FALSE;
  }

  /* removing example file */
  if (!unlink($root_path . $file_data->filepath))
  {
    drupal_set_message(t('Error deleting !file', array('!file' => $file_data->filepath)), 'error');

    /* sending email to admins */
    $email_to = variable_get('textbook_companion_emails', '');
    $param['standard']['subject'] = "[ERROR] Error deleting file";
    $param['standard']['body'] = "Error deleting file by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " :
      file id : " .  $file_id . "
      file path : " . $file_data->filepath;
    if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
      drupal_set_message('Error sending email message.', 'error');
    return FALSE;
  } else {
    /* deleting example files database entries */
    db_query("DELETE FROM {textbook_companion_example_files} WHERE id = %d", $file_id);
    return TRUE;
  }
}

function delete_chapter($chapter_id)
{
  $status = TRUE;
  $root_path = textbook_companion_path();

  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id);
  $chapter_data = db_fetch_object($chapter_q);
  if (!$chapter_data)
  {
    drupal_set_message('Invalid chapter.', 'error');
    return FALSE;
  }

  /* deleting examples */
  $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d", $chapter_id);
  while ($example_data = db_fetch_object($example_q))
  {
    if (!delete_example($example_data->id))
      $status = FALSE;
  }

  if ($status)
  {
    $dir_path = $root_path . $chapter_data->preference_id . '/CH' . $chapter_data->number;

    if (is_dir($dir_path))
    {
      $res = rmdir($dir_path);
      if (!$res)
      {
        drupal_set_message(t('Error deleting chapter folder !folder', array('!folder' => $dir_path)), 'error');
  
        /* sending email to admins */
        $email_to = variable_get('textbook_companion_emails', '');
        $param['standard']['subject'] = "[ERROR] Error deleting folder";
        $param['standard']['body'] = "Error deleting folder " . $dir_path;
        if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
          drupal_set_message('Error sending email message.', 'error');
        return FALSE;
      } else {
        /* deleting chapter details from database */
        db_query("DELETE FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id);
        return TRUE;
      }
    } else {
      drupal_set_message(t('Cannot delete chapter folder. !folder does not exists.', array('!folder' => $dir_path)), 'error');
      return FALSE;
    }
  }
  return FALSE;
}

function delete_book($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;
}

/**
 * Implementation of hook_mail().
 */
function textbook_companion_mail($key, &$message, $params)
{
  global $user;
  $language = $message['lianguage'];
  $tbc_bcc_emails = array(
    'Bcc' => variable_get('textbook_companion_emails', ''),
  );
  switch ($key)
  {
    case 'proposal_received':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      /* initializing data */
      $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d LIMIT 1", $params['proposal_received']['proposal_id']);
      $proposal_data = db_fetch_object($proposal_q);
      $preference1_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_received']['proposal_id'], 1);
      $preference1_data = db_fetch_object($preference1_q);
      $preference2_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_received']['proposal_id'], 2);
      $preference2_data = db_fetch_object($preference2_q);
      $preference3_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_received']['proposal_id'], 3);
      $preference3_data = db_fetch_object($preference3_q);
      $user_data = user_load($params['proposal_received']['user_id']);
 
      $message['subject'] = t('[!site_name] Your book proposal has been received', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have received your following book proposal:

Full Name : ' . $proposal_data->full_name . '
Email : ' . $user_data->mail . '
Mobile : ' . $proposal_data->mobile . '
Course : ' . $proposal_data->course . '
Department/Branch : ' . $proposal_data->branch . '
University/Institute : ' .  $proposal_data->university . '
College Teacher / Professor : ' . $proposal_data->faculty . '
Reviewer : ' .  $proposal_data->reviewer . '
Expected date of completion : ' .  date('d-m-Y', $proposal_data->completion_date) . '

Your Book Preferences :

Book Preference 1 :-
Title of the book : ' . $preference1_data->book . '
Author name : ' . $preference1_data->author . '
ISBN No. : ' . $preference1_data->isbn . '
Publisher and Place : ' . $preference1_data->publisher . '
Edition : ' . $preference1_data->edition . '
Year of publication : ' . $preference1_data->year . '

Book Preference 2 :-
Title of the book : ' . $preference2_data->book . '
Author name : ' . $preference2_data->author . '
ISBN No. : ' . $preference2_data->isbn . '
Publisher and Place : ' . $preference2_data->publisher . '
Edition : ' . $preference2_data->edition . '
Year of publication : ' . $preference2_data->year . '

Book Preference 3 :-
Title of the book : ' . $preference3_data->book . '
Author name : ' . $preference3_data->author . '
ISBN No. : ' . $preference3_data->isbn . '
Publisher and Place : ' . $preference3_data->publisher . '
Edition : ' . $preference3_data->edition . '
Year of publication : ' . $preference3_data->year . '

Your proposal is under review and you will soon receive an email from us regarding the same.

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'proposal_disapproved':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      /* initializing data */
      $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d LIMIT 1", $params['proposal_disapproved']['proposal_id']);
      $proposal_data = db_fetch_object($proposal_q);
      $preference1_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_disapproved']['proposal_id'], 1);
      $preference1_data = db_fetch_object($preference1_q);
      $preference2_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_disapproved']['proposal_id'], 2);
      $preference2_data = db_fetch_object($preference2_q);
      $preference3_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $params['proposal_disapproved']['proposal_id'], 3);
      $preference3_data = db_fetch_object($preference3_q);
      $user_data = user_load($params['proposal_disapproved']['user_id']);

      $message['subject'] = t('[!site_name] Your book proposal has been disapproved', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Your following book proposal has been disapproved:

Reason for disapproval: ' . $proposal_data->message . '

Full Name : ' . $proposal_data->full_name . '
Email : ' . $user_data->mail . '
Mobile : ' . $proposal_data->mobile . '
Course : ' . $proposal_data->course . '
Department/Branch : ' . $proposal_data->branch . '
University/Institute : ' .  $proposal_data->university . '
College Teacher / Professor : ' . $proposal_data->faculty . '
Reviewer : ' .  $proposal_data->reviewer . '
Expected date of completion : ' .  date('d-m-Y', $proposal_data->completion_date) . '

Your Book Preferences :

Book Preference 1 :-
Title of the book : ' . $preference1_data->book . '
Author name : ' . $preference1_data->author . '
ISBN No. : ' . $preference1_data->isbn . '
Publisher and Place : ' . $preference1_data->publisher . '
Edition : ' . $preference1_data->edition . '
Year of publication : ' . $preference1_data->year . '

Book Preference 2 :-
Title of the book : ' . $preference2_data->book . '
Author name : ' . $preference2_data->author . '
ISBN No. : ' . $preference2_data->isbn . '
Publisher and Place : ' . $preference2_data->publisher . '
Edition : ' . $preference2_data->edition . '
Year of publication : ' . $preference2_data->year . '

Book Preference 3 :-
Title of the book : ' . $preference3_data->book . '
Author name : ' . $preference3_data->author . '
ISBN No. : ' . $preference3_data->isbn . '
Publisher and Place : ' . $preference3_data->publisher . '
Edition : ' . $preference3_data->edition . '
Year of publication : ' . $preference3_data->year . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'proposal_approved':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d LIMIT 1", $params['proposal_approved']['proposal_id']);
      $proposal_data = db_fetch_object($proposal_q);
      $approved_preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND approval_status = 1 LIMIT 1", $params['proposal_approved']['proposal_id']);
      $approved_preference_data = db_fetch_object($approved_preference_q);
      $user_data = user_load($params['proposal_approved']['user_id']);

      $message['subject'] = t('[!site_name] Your book proposal has been approved', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Your following book proposal has been approved:

Full Name : ' . $proposal_data->full_name . '
Email : ' . $user_data->mail . '
Mobile : ' .  $proposal_data->mobile . '
Course : ' . $proposal_data->course . '
Department/Branch : ' . $proposal_data->branch . '
University/Institute : ' .  $proposal_data->university . '
College Teacher / Professor : ' . $proposal_data->faculty . '
Reviewer : ' .  $proposal_data->reviewer . '
Expected date of completion : ' .  date('d-m-Y', $proposal_data->completion_date) . '

Title of the book : ' . $approved_preference_data->book . '
Author name : ' . $approved_preference_data->author . '
ISBN No. : ' . $approved_preference_data->isbn . '
Publisher and Place : ' . $approved_preference_data->publisher . '
Edition : ' . $approved_preference_data->edition . '
Year of publication : ' . $approved_preference_data->year . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'proposal_completed':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d LIMIT 1", $params['proposal_completed']['proposal_id']);
      $proposal_data = db_fetch_object($proposal_q);
      $approved_preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND approval_status = 1 LIMIT 1", $params['proposal_completed']['proposal_id']);
      $approved_preference_data = db_fetch_object($approved_preference_q);
      $user_data = user_load($params['proposal_completed']['user_id']);

      $message['subject'] = t('[!site_name] Congratulations for completion of the book.', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Following book has been completed sucessfully by you:

Full Name : ' . $proposal_data->full_name . '
Email : ' . $user_data->mail . '
Mobile : ' .  $proposal_data->mobile . '
Course : ' . $proposal_data->course . '
Department/Branch : ' . $proposal_data->branch . '
University/Institute : ' .  $proposal_data->university . '
College Teacher / Professor : ' . $proposal_data->faculty . '
Reviewer : ' .  $proposal_data->reviewer . '
Expected date of completion : ' .  date('d-m-Y', $proposal_data->completion_date) . '

Title of the book : ' . $approved_preference_data->book . '
Author name : ' . $approved_preference_data->author . '
ISBN No. : ' . $approved_preference_data->isbn . '
Publisher and Place : ' . $approved_preference_data->publisher . '
Edition : ' . $approved_preference_data->edition . '
Year of publication : ' . $approved_preference_data->year . '

Your book is now available at following link to download.

http://scilab.in/textbook_run/' . $approved_preference_data->id . '

Now you should be able to propose a new book...

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_uploaded':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $params['example_uploaded']['example_id']);
      $example_data = db_fetch_object($example_q);
      $user_data = user_load($params['example_uploaded']['user_id']);

      $message['subject'] = t('[!site_name] You have uploaded example', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

You have uploaded the following example:

Example number : ' . $example_data->number . '
Caption : ' .  $example_data->caption . '

The example is under review. You will be notified when it has been approved.

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_updated':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $params['example_updated']['example_id']);
      $example_data = db_fetch_object($example_q);
      $user_data = user_load($params['example_updated']['user_id']);

      $message['subject'] = t('[!site_name] You have updated example', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

You have updated the following example:

Example number : ' . $example_data->number . '
Caption : ' .  $example_data->caption . '

The example is still under review. You will be notified when it has been approved.

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_updated_admin':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $params['example_updated_admin']['example_id']);
      $example_data = db_fetch_object($example_q);
      $user_data = user_load($params['example_updated_admin']['user_id']);

      $message['subject'] = t('[!site_name] Reviewer have updated example', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Reviewer have updated the following example:

Example number : ' . $example_data->number . '
Caption : ' .  $example_data->caption . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_approved':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $params['example_approved']['example_id']);
      $example_data = db_fetch_object($example_q);
      $user_data = user_load($params['example_approved']['user_id']);

      $message['subject'] = t('[!site_name] Your uploaded example has been approved', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Your following example has been approved:

Example number : ' . $example_data->number . '
Caption : ' .  $example_data->caption . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_disapproved':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $user_data = user_load($params['example_disapproved']['user_id']);

      $message['subject'] = t('[!site_name] Your uploaded example has been disapproved', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Your following example has been disapproved:

Example number : ' . $params['example_disapproved']['example_number'] . '
Caption : ' . $params['example_disapproved']['example_caption'] . '

Reason for dis-approval : ' . $params['example_disapproved']['message'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'example_deleted_user':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $user_data = user_load($params['example_deleted_user']['user_id']);

      $message['subject'] = t('[!site_name] User has deleted pending example', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

Your following pending example has been deleted :

Title of the Book : ' . $params['example_deleted_user']['book_title'] . '
Title of the Chapter : ' . $params['example_deleted_user']['chapter_title'] . '
Example number : ' . $params['example_deleted_user']['example_number'] . '
Caption : ' . $params['example_deleted_user']['example_caption'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'dependency_uploaded':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $user_data = user_load($params['dependency_uploaded']['user_id']);
      $dependency_files = implode(',', $params['dependency_uploaded']['dependency_names']);

      $message['subject'] = t('[!site_name] You have uploaded dependency file', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

You have uploaded following dependency files :
    ' . $dependency_files . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'feedback_received':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $user_data = user_load($params['feedback_received']['user_id']);

      $message['subject'] = t('[!site_name] We have received your feedback', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have received your following feedback

Title of the Book: ' . $params['feedback_received']['book_title'] . '
Title of the Chapter: ' . $params['feedback_received']['chapter_number'] . ' ' . $params['feedback_received']['chapter_title'] . '
Example No.: ' . $params['feedback_received']['example_no'] . '

Your feedback :
' . $params['feedback_received']['feedback'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'internshipform':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $user_data = user_load($params['internshipform']['user_id']);

      $message['subject'] = t('[!site_name] We have received your feedback', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have received your Internship Form Application for the book 

Title of the Book: ' . $params['internshipform']['book_title'] . '
Title of the Chapter: ' . $params['internshipform']['chapter_number'] . ' ' . $params['internshipform']['chapter_title'] . '
Example No.: ' . $params['internshipform']['example_no'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

case 'copyrighttransferform':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $user_data = user_load($params['copyrighttransferform']['user_id']);

      $message['subject'] = t('[!site_name] We have received your feedback', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have received your Copyright Form Application for the book 

Title of the Book: ' . $params['copyrighttransferform']['book_title'] . '
Title of the Chapter: ' . $params['copyrighttransferform']['chapter_number'] . ' ' . $params['copyrighttransferform']['chapter_title'] . '
Example No.: ' . $params['copyrighttransferform']['example_no'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

case 'undertakingform':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $user_data = user_load($params['undertakingform']['user_id']);

      $message['subject'] = t('[!site_name] We have received your feedback', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have received your Undertaking Form Application for the book 

Title of the Book: ' . $params['undertakingform']['book_title'] . '
Title of the Chapter: ' . $params['undertakingform']['chapter_number'] . ' ' . $params['undertakingform']['chapter_title'] . '
Example No.: ' . $params['undertakingform']['example_no'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

   
case 'remark':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $user_data = user_load($params['remark']['user_id']);

      $message['subject'] = t('[!site_name] A remark has been given.Please check your contact detail form', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

A Remark has been given.Please check your Contact Detail Form

Title of the Book: ' . $params['internshipform']['book_title'] . '
Title of the Chapter: ' . $params['internshipform']['chapter_number'] . ' ' . $params['internshipform']['chapter_title'] . '
Example No.: ' . $params['internshipform']['example_no'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;

    case 'cheque_sent':
      /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;

      $user_data = user_load($params['cheque_sent']['user_id']);

      $message['subject'] = t('[!site_name] We have received your feedback', array('!site_name' => variable_get('site_name', '')), $language->language);
      $message['body'] = t('
Dear !user_name,

We have Sent Cheque for the following book proposed

Title of the Book: ' . $params['cheque_sent']['book_title'] . '
Title of the Chapter: ' . $params['cheque_sent']['chapter_number'] . ' ' . $params['cheque_sent']['chapter_title'] . '
Example No.: ' . $params['cheque_sent']['example_no'] . '

Best Wishes,

!site_name', array('!site_name' => variable_get('site_name', ''), '!user_name' => $user_data->name), $language->language);
      break;


    case 'standard':
       /* bcc to textbook_companion_emails */
      $message['headers'] += $tbc_bcc_emails;
      $message['subject'] = $params['standard']['subject'];
      $message['body'] = $params['standard']['body'];
      break;
  }
}