-
Notifications
You must be signed in to change notification settings - Fork 4
/
general.inc
executable file
·544 lines (538 loc) · 26.3 KB
/
general.inc
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
<?php
// $Id$
function list_chapters()
{
global $user;
/************************ start approve book details ************************/
/*$proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE uid = %d ORDER BY id DESC LIMIT 1", $user->uid);
$proposal_data = db_fetch_object($proposal_q);*/
$query = db_select('textbook_companion_proposal');
$query->fields('textbook_companion_proposal');
$query->condition('uid', $user->uid);
$query->orderBy('id', 'DESC');
$query->range(0, 1);
$result = $query->execute();
$proposal_data = $result->fetchObject();
if (!$proposal_data)
{
drupal_set_message("Please submit a " . l('proposal', 'textbook-companion/proposal') . ".", 'error');
drupal_goto('');
}
if ($proposal_data->proposal_status != 1 && $proposal_data->proposal_status != 4)
{
switch ($proposal_data->proposal_status)
{
case 0:
drupal_set_message(t('We have already received your proposal. We will get back to you soon.'), 'status');
drupal_goto('');
return;
break;
case 2:
drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'error');
drupal_goto('');
return;
break;
case 3:
drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'status');
drupal_goto('');
return;
break;
default:
drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
break;
}
}
/*$preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND approval_status = 1 LIMIT 1", $proposal_data->id);
$preference_data = db_fetch_object($preference_q);*/
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('proposal_id', $proposal_data->id);
$query->condition('approval_status', 1);
$query->range(0, 1);
$result = $query->execute();
$preference_data = $result->fetchObject();
if ($preference_data->submited_all_examples_code == 1)
{
drupal_set_message(t('You have submited your all codes for this book to review, hence you can not upload more code, for any query please write us.'), 'status');
drupal_goto('');
return;
}
if (!$preference_data)
{
drupal_set_message(t('Invalid Book Preference status. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
}
/************************ end approve book details **************************/
$return_html = '<br />';
$return_html .= '<strong>Title of the Book:</strong><br />' . $preference_data->book . '<br /><br />';
$return_html .= '<strong>Contributor Name:</strong><br />' . $proposal_data->full_name . '<br /><br />';
$return_html .= l('Upload Example Code', 'textbook-companion/code/upload') . '<br />';
/* get chapter list */
$chapter_rows = array();
/*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number ASC", $preference_data->id);*/
$query = db_select('textbook_companion_chapter');
$query->fields('textbook_companion_chapter');
$query->condition('preference_id', $preference_data->id);
$query->orderBy('number', 'ASC');
$chapter_q = $query->execute();
while ($chapter_data = $chapter_q->fetchObject())
{
/* get example list */
/* $example_q = db_query("SELECT count(*) as example_count FROM {textbook_companion_example} WHERE chapter_id = %d", $chapter_data->id);
$example_data = db_fetch_object($example_q);*/
$query = db_select('textbook_companion_example');
$query->addExpression('count(*)', 'example_count');
$query->condition('chapter_id', $chapter_data->id);
$result = $query->execute();
$example_data = $result->fetchObject();
$chapter_rows[] = array(
$chapter_data->number,
$chapter_data->name . ' (' . l('Edit', 'textbook-companion/code/chapter/edit/' . $chapter_data->id) . ')',
$example_data->example_count,
l('View', 'textbook-companion/code/list-examples/' . $chapter_data->id)
);
}
/* check if there are any chapters */
if (!$chapter_rows)
{
drupal_set_message(t('No uploads found.'), 'status');
return $return_html;
}
$chapter_header = array(
'Chapter No.',
'Title of the Chapter',
'Uploaded Examples',
'Actions'
);
$return_html .= theme('table', array(
'header' => $chapter_header,
'rows' => $chapter_rows
));
$submited_all_example = drupal_get_form("all_example_submitted_check_form",$preference_data->id);
$return_html .= drupal_render($submited_all_example);
return $return_html;
}
function list_examples()
{
global $user;
/************************ start approve book details ************************/
/*$proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE uid = %d ORDER BY id DESC LIMIT 1", $user->uid);
$proposal_data = db_fetch_object($proposal_q);*/
$query = db_select('textbook_companion_proposal');
$query->fields('textbook_companion_proposal');
$query->condition('uid', $user->uid);
$query->orderBy('id', 'DESC');
$query->range(0, 1);
$result = $query->execute();
$proposal_data = $result->fetchObject();
if (!$proposal_data)
{
drupal_set_message("Please submit a " . l('proposal', 'textbook-companion/proposal') . ".", 'error');
drupal_goto('');
}
if ($proposal_data->proposal_status != 1 && $proposal_data->proposal_status != 4)
{
switch ($proposal_data->proposal_status)
{
case 0:
drupal_set_message(t('We have already received your proposal. We will get back to you soon.'), 'status');
drupal_goto('');
return;
break;
case 2:
drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'error');
drupal_goto('');
return;
break;
case 3:
drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'status');
drupal_goto('');
return;
break;
default:
drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
break;
}
}
/*$preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND approval_status = 1 LIMIT 1", $proposal_data->id);
$preference_data = db_fetch_object($preference_q);*/
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('proposal_id', $proposal_data->id);
$query->condition('approval_status', 1);
$query->range(0, 1);
$result = $query->execute();
$preference_data = $result->fetchObject();
if (!$preference_data)
{
drupal_set_message(t('Invalid Book Preference status. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
}
/************************ end approve book details **************************/
/* get chapter details */
$chapter_id = arg(3);
/*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d AND preference_id = %d LIMIT 1", $chapter_id, $preference_data->id);*/
$query = db_select('textbook_companion_chapter');
$query->fields('textbook_companion_chapter');
$query->condition('id', $chapter_id);
$query->condition('preference_id', $preference_data->id);
$query->range(0, 1);
$chapter_q = $query->execute();
if ($chapter_data = $chapter_q->fetchObject())
{
$return_html = '<br />';
$return_html .= '<strong>Title of the Book:</strong><br />' . $preference_data->book . '<br /><br />';
$return_html .= '<strong>Contributor Name:</strong><br />' . $proposal_data->full_name . '<br /><br />';
$return_html .= '<strong>Chapter Number:</strong><br />' . $chapter_data->number . '<br /><br />';
$return_html .= '<strong>Title of the Chapter:</strong><br />' . $chapter_data->name . '<br />';
}
else
{
drupal_set_message(t('Invalid chapter.'), 'error');
drupal_goto('textbook-companion/code');
return;
}
$return_html .= '<br />' . l('Back to Chapter List', 'textbook-companion/code');
/* get example list */
$example_rows = array();
$query = db_select('textbook_companion_example');
$query->fields('textbook_companion_example');
$query->condition('chapter_id', $chapter_id);
$example_q = $query->execute();
while ($example_data = $example_q->fetchObject())
{
/* approval status */
$approval_status = '';
switch ($example_data->approval_status)
{
case 0:
$approval_status = 'Pending';
break;
case 1:
$approval_status = 'Approved';
break;
case 2:
$approval_status = 'Rejected';
break;
}
/* example files */
$example_files = '';
/*$example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d ORDER BY filetype", $example_data->id);*/
$query = db_select('textbook_companion_example_files');
$query->fields('textbook_companion_example_files');
$query->condition('example_id', $example_data->id);
$query->orderBy('filetype', 'ASC');
$example_files_q = $query->execute();
while ($example_files_data = $example_files_q->fetchObject())
{
$file_type = '';
switch ($example_files_data->filetype)
{
case 'S':
$file_type = 'Main or Source';
break;
case 'D':
$file_type = 'Dataset';
break;
case 'X':
$file_type = 'xcos';
break;
default:
}
$example_files .= l($example_files_data->filename, 'textbook-companion/download/file/' . $example_files_data->id) . ' (' . $file_type . ')<br />';
}
if ($example_data->approval_status == 0)
{
$example_rows[] = array(
'data' => array(
$example_data->number,
$example_data->caption,
$approval_status,
$example_files,
l('Edit', 'textbook-companion/code/edit/' . $example_data->id) . ' | ' . l('Delete', 'textbook-companion/code/delete/' . $example_data->id, array(
'attributes' => array(
'onClick' => 'return confirm("Are you sure you want to delete the example?")'
)
))
),
'valign' => 'top'
);
}
else
{
$example_rows[] = array(
'data' => array(
$example_data->number,
$example_data->caption,
$approval_status,
$example_files,
l('Download', 'textbook-companion/download/example/' . $example_data->id)
),
'valign' => 'top'
);
}
}
$example_header = array(
'Example No.',
'Caption',
'Status',
'Files',
'Action'
);
$return_html .= theme('table', array(
'header' => $example_header,
'rows' => $example_rows
));
return $return_html;
}
function all_example_submitted_check_form($form,&$form_state,$preference_id){
//$form = array();
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('id', $preference_id);
$query->condition('approval_status', 1);
$query->range(0, 1);
$result = $query->execute();
$preference_data = $result->fetchObject();
$form['#attributes'] = array(
'enctype' => "multipart/form-data"
);
$form['all_example_submitted'] = array(
'#type' => 'checkbox',
'#title' => t('I have submitted codes for all the examples'),
'#description' => 'Once you have submited this option you are not able to upload more examples.',
'#required' => TRUE,
);
//var_dump($preference_data->approved_codable_example_files);die;
if($preference_data->approved_codable_example_files == 0){
$form['upload_codable_examples'] = array(
'#type' => 'file',
'#states' => array(
'visible' => array(
':input[name="all_example_submitted"]' => array(
'checked' => True
)
)
),
'#title' => t('Upload a document file containing a list of codable examples as shown in the template <a href="https://static.fossee.in/r/manuals/Template.docx" target="_blank">here</a><span style="color:red">*</span>'),
'#description' => t('Separate filenames with underscore. No spaces or any special characters allowed in filename.') . '<br />' . t('<span style="color:red;">Allowed file extensions : ') . variable_get('textbook_companion_codable_examples_extensions', '') . '</span>',
);
}
$form['hidden_preference_id'] = array(
'#type' => 'hidden',
'#value' => $preference_id
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
function all_example_submitted_check_form_validate($form,&$form_state){
if ($form_state['values']['all_example_submitted'] != 1) {
form_set_error('all_example_submitted', t('Please check the field if you are intrested to submit the all uploaded examples for review!'));
}
if (isset($_FILES['files']))
{
/* check if atleast one source or result file is uploaded */
if (!($_FILES['files']['name']['upload_codable_examples']))
form_set_error('upload_codable_examples', t('Please upload the file containing a list of codable examples.'));
/* check for valid filename extensions */
foreach ($_FILES['files']['name'] as $file_form_name => $file_name)
{
if ($file_name)
{
/* checking file type */
$allowed_extensions_str = variable_get('textbook_companion_codable_examples_extensions', '');
$allowed_extensions = explode(',', $allowed_extensions_str);
$fnames = explode('.', strtolower($_FILES['files']['name'][$file_form_name]));
$temp_extension = end($fnames);
if (!in_array($temp_extension, $allowed_extensions))
form_set_error($file_form_name, t('Only file with ' . $allowed_extensions_str . ' extensions can be uploaded.'));
if ($_FILES['files']['size'][$file_form_name] <= 0)
form_set_error($file_form_name, t('File size cannot be zero.'));
/* check if valid file name */
if (!textbook_companion_check_valid_filename($_FILES['files']['name'][$file_form_name]))
form_set_error($file_form_name, t('Invalid file name specified. Only alphabets and numbers are allowed as a valid filename.'));
} //$file_name
} //$_FILES['files']['name'] as $file_form_name => $file_name
}
return;
}
function all_example_submitted_check_form_submit($form,&$form_state){
global $user;
/*if ($form_state['values']['all_example_submitted'] == 1) {
db_query('UPDATE textbook_companion_preference SET submited_all_examples_code = 1 WHERE id = :preference_id',array(':preference_id' => $form_state['values']['hidden_preference_id']));*/
$query = db_select('textbook_companion_proposal');
$query->fields('textbook_companion_proposal');
$query->condition('uid', $user->uid);
$query->orderBy('id', 'DESC');
$query->range(0, 1);
$result = $query->execute();
$proposal_data = $result->fetchObject();
if (!$proposal_data)
{
drupal_set_message("Please submit a " . l('proposal', 'textbook-companion/proposal') . ".", 'error');
drupal_goto('');
}
if ($proposal_data->proposal_status != 1 && $proposal_data->proposal_status != 4)
{
switch ($proposal_data->proposal_status)
{
case 0:
drupal_set_message(t('We have already received your proposal. We will get back to you soon.'), 'status');
drupal_goto('');
return;
break;
case 2:
drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'error');
drupal_goto('');
return;
break;
case 3:
drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'textbook-companion/proposal') . '.'), 'status');
drupal_goto('');
return;
break;
case 5:
drupal_set_message(t('You have submmited your all codes'), 'status');
drupal_goto('');
return;
break;
default:
drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
break;
}
}
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('proposal_id', $proposal_data->id);
$query->condition('approval_status', 1);
$query->range(0, 1);
$result = $query->execute();
$preference_data = $result->fetchObject();
if (!$preference_data)
{
drupal_set_message(t('Invalid Book Preference status. Please contact site administrator for further information.'), 'error');
drupal_goto('');
return;
}
if($preference_data->approved_codable_example_files == 0){
$root_path = textbook_companion_path();
$proposal_directory = $preference_data->directory_name;
$dest_path = $proposal_directory . '/';
if (!is_dir($root_path . $dest_path)){
if(!mkdir($root_path . $dest_path))
{
drupal_set_message(t('You cannot upload your code. Error in creating directory'), 'error');
}
}
/* creating directories */
$dest_path .= 'codable_example_file';
if (!is_dir($root_path . $dest_path))
mkdir($root_path . $dest_path);
//var_dump($root_path . $dest_path);die;
$filepath = 'codable_example_file/';
foreach ($_FILES['files']['name'] as $file_form_name => $file_name)
{
if (file_exists($root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]))
{
move_uploaded_file($_FILES['files']['tmp_name'][$file_form_name], $root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]);
drupal_set_message(t("File !filename already exists hence overwirtten the exisitng file ", array(
'!filename' => $_FILES['files']['name'][$file_form_name]
)), 'status');
$query = "UPDATE {textbook_companion_codable_example_files} SET filename = :filename, filepath=:filepath, filemime=:filemime, filesize=:filesize, timestamp=:timestamp WHERE proposal_id = :proposal_id";
$args = array(
":filename" => $_FILES['files']['name'][$file_form_name],
":filepath" => $filepath . $_FILES['files']['name'][$file_form_name],
":filemime" => mime_content_type($root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]),
":filesize" => $_FILES['files']['size'][$file_form_name],
":timestamp" => time(),
":proposal_id" => $preference_data->proposal_id
);
db_query($query, $args);
db_query('UPDATE textbook_companion_preference SET submited_all_examples_code = 1, submitted_codable_examples_file = 1 WHERE id = :preference_id',array(':preference_id' => $preference_data->id));
return;
}
/* uploading file */
else if (move_uploaded_file($_FILES['files']['tmp_name'][$file_form_name], $root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]))
{
$query_codable_examples_file = "SELECT * FROM textbook_companion_codable_example_files WHERE proposal_id = :proposal_id";
$args_query_codable_examples_file = array(
":proposal_id" => $preference_data->proposal_id
);
$query_codable_examples_file_result = db_query($query_codable_examples_file, $args_query_codable_examples_file)->fetchObject();
if(!$query_codable_examples_file_result){
$query = "INSERT INTO {textbook_companion_codable_example_files} (proposal_id, filename, filepath,filemime, filesize, filetype, timestamp) VALUES (:proposal_id, :filename ,:filepath,:filemime, :filesize, :filetype, :timestamp)";
$args = array(
":proposal_id" => $preference_data->proposal_id,
":filename" => $_FILES['files']['name'][$file_form_name],
":filepath" => $filepath . $_FILES['files']['name'][$file_form_name],
":filemime" => mime_content_type($root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]),
":filesize" => $_FILES['files']['size'][$file_form_name],
":filetype" => 'C',
":timestamp" => time()
);
$result = db_query($query, $args, array(
'return' => Database::RETURN_INSERT_ID
));
drupal_set_message('File uploaded successfully.', 'status');
}
else
{
unlink($root_path . $dest_path . '/' . $query_codable_examples_file_result->filename);
$query = "UPDATE {textbook_companion_codable_example_files} SET filename = :filename, filepath=:filepath, filemime=:filemime, filesize=:filesize, timestamp=:timestamp WHERE proposal_id = :proposal_id";
$args = array(
":filename" => $_FILES['files']['name'][$file_form_name],
":filepath" => $filepath . $_FILES['files']['name'][$file_form_name],
":filemime" => mime_content_type($root_path . $dest_path . '/' . $_FILES['files']['name'][$file_form_name]),
":filesize" => $_FILES['files']['size'][$file_form_name],
":timestamp" => time(),
":proposal_id" => $preference_data->proposal_id
);
db_query($query, $args);
drupal_set_message($file_name . ' file updated successfully.', 'status');
}
db_query('UPDATE textbook_companion_preference SET submited_all_examples_code = 1, submitted_codable_examples_file = 1 WHERE id = :preference_id',array(':preference_id' => $preference_data->id));
}
else
{
drupal_set_message('Error uploading file : ' . $dest_path . '/' . $file_name, 'error');
}
}
}
else{
db_query('UPDATE textbook_companion_preference SET submited_all_examples_code = 1 WHERE id = :preference_id',array(':preference_id' => $preference_data->id));
}
//drupal_set_message('File uploaded successfully.', 'status');
/* sending email */
$query = ("SELECT proposal_id FROM textbook_companion_preference WHERE id= :preference_id");
$args = array(":preference_id" => $form_state['values']['hidden_preference_id']);
$proposal_data = db_query($query,$args);
$proposal_data_result = $proposal_data->fetchObject();
$email_to = $user->mail;
$from = variable_get('textbook_companion_from_email', '');
$bcc = variable_get('textbook_companion_emails', '');
$cc = variable_get('textbook_companion_cc_emails', '');
$param['all_code_submitted']['proposal_id'] = $proposal_data_result->proposal_id;
$param['all_code_submitted']['user_id'] = $user->uid;
$param['all_code_submitted']['headers'] = array(
'From' => $from,
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => $cc,
'Bcc' => $bcc
);
if (!drupal_mail('textbook_companion', 'all_code_submitted', $email_to, language_default(), $param, $from, TRUE))
drupal_set_message('Error sending email message.', 'error');
}