-
Notifications
You must be signed in to change notification settings - Fork 4
/
editcodeadmin.inc
executable file
·569 lines (567 loc) · 24.4 KB
/
editcodeadmin.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
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
<?php
// $Id$
/******************************************************************************/
/***************************** EDIT EXAMPLE ***********************************/
/******************************************************************************/
function upload_examples_admin_edit_form($form, &$form_state)
{
global $user;
$example_id = arg(3);
/* get example details */
/*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $example_id);
$example_data = db_fetch_object($example_q);*/
$query = db_select('textbook_companion_example');
$query->fields('textbook_companion_example');
$query->condition('id', $example_id);
$query->range(0, 1);
$example_q = $query->execute();
$example_data = $example_q->fetchObject();
if (!$example_q)
{
drupal_set_message(t("Invalid example selected."), 'error');
drupal_goto('');
return;
}
/* get examples files */
$source_file = "";
$source_id = 0;
$dataset_file = "";
$dataset_id = 0;
$result2_file = "";
$result2_id = 0;
$xcos1_file = "";
$xcos1_id = 0;
$xcos2_file = "";
$xcos2_id = 0;
/*$example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_id);*/
$query = db_select('textbook_companion_example_files');
$query->fields('textbook_companion_example_files');
$query->condition('example_id', $example_id);
$example_files_q = $query->execute();
while ($example_files_data = $example_files_q->fetchObject())
{
if ($example_files_data->filetype == "S")
{
$source_file = l($example_files_data->filename, 'textbook-companion/download/file/' . $example_files_data->id);
$source_file_id = $example_files_data->id;
}
else if ($example_files_data->filetype == "D")
{
$dataset_file = l($example_files_data->filename, 'textbook-companion/download/file/' . $example_files_data->id);
$dataset_file_id = $example_files_data->id;
}
}
/* get chapter details */
/*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id);
$chapter_data = db_fetch_object($chapter_q);*/
$query = db_select('textbook_companion_chapter');
$query->fields('textbook_companion_chapter');
$query->condition('id', $example_data->chapter_id);
$result = $query->execute();
$chapter_data = $result->fetchObject();
if (!$chapter_data)
{
drupal_set_message(t("Invalid chapter selected."), 'error');
drupal_goto('');
return;
}
/* get preference details */
/*$preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $chapter_data->preference_id);
$preference_data = db_fetch_object($preference_q);*/
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('id', $chapter_data->preference_id);
$result = $query->execute();
$preference_data = $result->fetchObject();
if (!$preference_data)
{
drupal_set_message(t("Invalid book selected."), 'error');
drupal_goto('');
return;
}
/* get proposal details */
/*$proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $preference_data->proposal_id);
$proposal_data = db_fetch_object($proposal_q);*/
$query = db_select('textbook_companion_proposal');
$query->fields('textbook_companion_proposal');
$query->condition('id', $preference_data->proposal_id);
$result = $query->execute();
$proposal_data = $result->fetchObject();
if (!$proposal_data)
{
drupal_set_message(t("Invalid proposal selected."), 'error');
drupal_goto('');
return;
}
$user_data = user_load($proposal_data->uid);
$form['#redirect'] = 'code_approval/bulk';
$form['#attributes'] = array(
'enctype' => "multipart/form-data"
);
$form['book_details']['book'] = array(
'#type' => 'item',
'#markup' => $preference_data->book,
'#title' => t('Title of the Book')
);
$form['contributor_name'] = array(
'#type' => 'item',
'#markup' => $proposal_data->full_name,
'#title' => t('Contributor Name')
);
$form['number'] = array(
'#type' => 'item',
'#title' => t('Chapter No'),
'#markup' => $chapter_data->number
);
$form['name'] = array(
'#type' => 'item',
'#title' => t('Title of the Chapter'),
'#markup' => $chapter_data->name
);
$form['example_number'] = array(
'#type' => 'item',
'#title' => t('Example No'),
'#markup' => $example_data->number
);
$form['example_caption'] = array(
'#type' => 'textfield',
'#title' => t('Caption'),
'#size' => 40,
'#maxlength' => 255,
'#required' => TRUE,
'#default_value' => $example_data->caption
);
$form['example_warning'] = array(
'#type' => 'item',
'#title' => t('You should upload all the files (main or source files, result files, executable file if any)'),
'#prefix' => '<div style="color:red">',
'#suffix' => '</div>'
);
$form['sourcefile'] = array(
'#type' => 'fieldset',
'#title' => t('Main or Source Files'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
if ($source_file)
{
$form['sourcefile']['cur_source'] = array(
'#type' => 'item',
'#title' => t('Existing Main or Source File'),
'#markup' => $source_file
);
$form['sourcefile']['cur_source_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Delete Existing Main or Source File'),
'#description' => 'Check to delete the existing Main or Source file.'
);
$form['sourcefile']['sourcefile1'] = array(
'#type' => 'file',
'#title' => t('Upload New Main or Source File'),
'#size' => 48,
'#description' => t("Upload new Main or Source file above if you want to replace the existing file. Leave blank if you want to keep using the existing file. <br />") . t('Allowed file extensions : ') . variable_get('textbook_companion_source_extensions', '')
);
$form['sourcefile']['cur_source_file_id'] = array(
'#type' => 'hidden',
'#default_value' => $source_file_id
);
}
else
{
$form['sourcefile']['sourcefile1'] = array(
'#type' => 'file',
'#title' => t('Upload New Main or Source File'),
'#size' => 48,
'#description' => t('Allowed file extensions : ') . variable_get('textbook_companion_source_extensions', '')
);
}
$form['dataset'] = array(
'#type' => 'fieldset',
'#title' => t('Dataset Files'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
if ($dataset_file)
{
$form['dataset']['cur_dataset'] = array(
'#type' => 'item',
'#title' => t('Existing Dataset File'),
'#markup' => $dataset_file
);
$form['dataset']['cur_dataset_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Delete Existing Dataset File'),
'#description' => 'Check to delete the existing Dataset file.',
'#attributes' => array(
'onClick' => 'return confirm("Are you sure you want to delete the example?")'
)
);
$form['dataset']['upload_dataset'] = array(
'#type' => 'file',
'#title' => t('Upload New Dataset File'),
'#size' => 48,
'#description' => t("Upload new Dataset file above if you want to replace the existing file. Leave blank if you want to keep using the existing file. <br />") . t('Allowed file extensions : ') . variable_get('textbook_companion_dataset_extensions', '')
);
$form['dataset']['cur_dataset_file_id'] = array(
'#type' => 'hidden',
'#value' => $dataset_file_id
);
}
else
{
$form['dataset']['upload_dataset'] = array(
'#type' => 'file',
'#title' => t('Upload New Dataset File'),
'#size' => 48,
'#description' => t('Allowed file extensions : ') . variable_get('textbook_companion_dataset_extensions', '')
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
$form['cancel'] = array(
'#type' => 'markup',
'#value' => l(t('Cancel'), 'textbook-companion/code')
);
return $form;
}
function upload_examples_admin_edit_form_validate($form, &$form_state)
{
if (!check_name($form_state['values']['example_caption']))
form_set_error('example_caption', t('Example Caption can contain only alphabets, numbers and spaces.'));
if (isset($_FILES['files']))
{
/* check for valid filename extensions */
foreach ($_FILES['files']['name'] as $file_form_name => $file_name)
{
if ($file_name)
{
/* checking file type */
if (strstr($file_form_name, 'sourcefile1'))
$file_type = 'S';
else if (strstr($file_form_name, 'upload_dataset'))
$file_type = 'D';
$allowed_extensions_str = '';
switch ($file_type)
{
case 'S':
$allowed_extensions_str = variable_get('textbook_companion_source_extensions', '');
break;
case 'D':
$allowed_extensions_str = variable_get('textbook_companion_dataset_extensions', '');
break;
}
$allowed_extensions = explode(',', $allowed_extensions_str);
$temp_ext = explode('.', strtolower($_FILES['files']['name'][$file_form_name]));
$temp_extension = end($temp_ext);
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, numbers and underscore is allowed as a valid filename.'));
}
}
}
}
function upload_examples_admin_edit_form_submit($form, &$form_state)
{
global $user;
$example_id = arg(3);
/* get example details */
/*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $example_id);
$example_data = db_fetch_object($example_q);*/
$query = db_select('textbook_companion_example');
$query->fields('textbook_companion_example');
$query->condition('id', $example_id);
$query->range(0, 1);
$example_q = $query->execute();
$example_data = $example_q->fetchObject();
if (!$example_q)
{
drupal_set_message(t("Invalid example selected."), 'error');
drupal_goto('');
return;
}
/* get chapter details */
/*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id);
$chapter_data = db_fetch_object($chapter_q);*/
$query = db_select('textbook_companion_chapter');
$query->fields('textbook_companion_chapter');
$query->condition('id', $example_data->chapter_id);
$chapter_q = $query->execute();
$chapter_data = $chapter_q->fetchObject();
if (!$chapter_data)
{
drupal_set_message(t("Invalid chapter selected."), 'error');
drupal_goto('');
return;
}
/* get preference details */
/*$preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $chapter_data->preference_id);
$preference_data = db_fetch_object($preference_q);*/
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$query->condition('id', $chapter_data->preference_id);
$result = $query->execute();
$preference_data = $result->fetchObject();
if (!$preference_data)
{
drupal_set_message(t("Invalid book selected."), 'error');
drupal_goto('');
return;
}
/* get proposal details */
/*$proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $preference_data->proposal_id);
$proposal_data = db_fetch_object($proposal_q);*/
$query = db_select('textbook_companion_proposal');
$query->fields('textbook_companion_proposal');
$query->condition('id', $preference_data->proposal_id);
$result = $query->execute();
$proposal_data = $result->fetchObject();
if (!$proposal_data)
{
drupal_set_message(t("Invalid proposal selected."), 'error');
drupal_goto('');
return;
}
$user_data = user_load($proposal_data->uid);
/* creating directories */
$root_path = textbook_companion_path();
$dest_path = $preference_data->directory_name . '/';
if (!is_dir($root_path . $dest_path))
mkdir($root_path . $dest_path);
$dest_path .= 'CH' . $chapter_data->number . '/';
if (!is_dir($root_path . $dest_path))
mkdir($root_path . $dest_path);
$dest_path .= 'EX' . $example_data->number . '/';
if (!is_dir($root_path . $dest_path))
mkdir($root_path . $dest_path);
$filepath = 'CH' . $chapter_data->number . '/' . 'EX' . $example_data->number . '/';
/* updating example caption */
/*db_query("UPDATE {textbook_companion_example} SET caption = '%s' WHERE id = %d", $form_state['values']['example_caption'], $example_id);*/
$query = db_update('textbook_companion_example');
$query->fields(array(
'caption' => $form_state['values']['example_caption']
));
$query->condition('id', $example_id);
$num_updated = $query->execute();
/* handle source file */
if (isset($form_state['values']['cur_source_file_id']) || isset($form_state['values']['cur_dataset_file_id']))
{
$cur_file_id = $form_state['values']['cur_source_file_id'];
$cur_dataset_id = $form_state['values']['cur_dataset_file_id'];
}
else
{
$cur_file_id = isset($form_state['values']['cur_source_file_id']);
$cur_dataset_id = isset($form_state['values']['cur_dataset_file_id']);
}
//var_dump($cur_file_id);die;
if ($cur_file_id > 0)
{
/*$file_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE id = %d AND example_id = %d", $cur_file_id, $example_data->id);
$file_data = db_fetch_object($file_q);*/
//var_dump($cur_file_id. $example_data->id);die;
$query = db_select('textbook_companion_example_files');
$query->fields('textbook_companion_example_files');
$query->condition('id', $cur_file_id);
$query->condition('example_id', $example_data->id);
$result = $query->execute();
$file_data = $result->fetchObject();
if (!$file_data)
{
drupal_set_message("Error deleting example source file. File not present in database.", 'error');
return;
}
if (($form_state['values']['cur_source_checkbox'] == 1) && (!$_FILES['files']['name']['sourcefile1']))
{
if (!delete_file($cur_file_id))
{
drupal_set_message("Error deleting example source file.", 'error');
return;
}
}
}
if ($cur_dataset_id > 0)
{
$query = db_select('textbook_companion_example_files');
$query->fields('textbook_companion_example_files');
$query->condition('id', $cur_dataset_id);
$query->condition('example_id', $example_data->id);
$result = $query->execute();
$file_data = $result->fetchObject();
if (!$file_data)
{
drupal_set_message("Error deleting example dataset file. File not present in database.", 'error');
return;
}
if (($form_state['values']['cur_dataset_checkbox'] == 1) && (!$_FILES['files']['name']['upload_dataset']))
{
if (!delete_file($cur_dataset_id))
{
drupal_set_message("Error deleting example dataset file.", 'error');
return;
}
}
}
if ($_FILES['files']['name']['sourcefile1'])
{
if ($cur_file_id > 0)
{
if (!delete_file($cur_file_id))
{
drupal_set_message("Error removing previous example source file.", 'error');
return;
}
}
}
else if($_FILES['files']['name']['upload_dataset'])
{
if ($cur_dataset_id > 0)
{
if (!delete_file($cur_dataset_id))
{
drupal_set_message("Error removing previous dataset file.", 'error');
return;
}
}
}
foreach ($_FILES['files']['name'] as $file_form_name => $file_name)
{
if ($file_name)
{
/* checking file type */
if (strstr($file_form_name, 'sourcefile1'))
{
$file_type = 'S';
} //strstr($file_form_name, 'upload_flowsheet_developed_process')
else if (strstr($file_form_name, 'upload_dataset'))
{
$file_type = 'D';
}
//$file_type = 'S';
switch ($file_type) {
case 'S':
if (file_exists($root_path . $dest_path . $_FILES['files']['name'][$file_form_name]))
{
drupal_set_message(t("Error uploading file. File !filename already exists.", array(
'!filename' => $_FILES['files']['name'][$file_form_name]
)), 'error');
return;
}
/* uploading file */
if (move_uploaded_file($_FILES['files']['tmp_name'][$file_form_name], $root_path . $dest_path . $_FILES['files']['name'][$file_form_name]))
{
$query = "INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath,filemime, filesize, filetype, timestamp)
VALUES (:example_id, :filename ,:filepath,:filemime, :filesize, :filetype, :timestamp)";
$args = array(
":example_id" => $example_id,
":filename" => $_FILES['files']['name'][$file_form_name],
":filepath" => $filepath . $_FILES['files']['name'][$file_form_name],
":filemime" => 'application/R',
":filesize" => $_FILES['files']['size'][$file_form_name],
":filetype" => $file_type,
":timestamp" => time()
);
$result = db_query($query, $args, array(
'return' => Database::RETURN_INSERT_ID
));
drupal_set_message($file_name . ' uploaded successfully.', 'status');
}
else
{
drupal_set_message('Error uploading file : ' . $dest_path . '/' . $file_name, 'error');
}
break;
case 'D':
if (file_exists($root_path . $dest_path . $_FILES['files']['name'][$file_form_name]))
{
drupal_set_message(t("Error uploading file. File !filename already exists.", array(
'!filename' => $_FILES['files']['name'][$file_form_name]
)), 'error');
return;
}
/* uploading file */
if (move_uploaded_file($_FILES['files']['tmp_name'][$file_form_name], $root_path . $dest_path . $_FILES['files']['name'][$file_form_name]))
{
$query = "INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath,filemime, filesize, filetype, timestamp)
VALUES (:example_id, :filename ,:filepath,:filemime, :filesize, :filetype, :timestamp)";
$args = array(
":example_id" => $example_id,
":filename" => $_FILES['files']['name'][$file_form_name],
":filepath" => $filepath . $_FILES['files']['name'][$file_form_name],
":filemime" => 'application/csv',
":filesize" => $_FILES['files']['size'][$file_form_name],
":filetype" => $file_type,
":timestamp" => time()
);
$result = db_query($query, $args, array(
'return' => Database::RETURN_INSERT_ID
));
drupal_set_message($file_name . ' uploaded successfully.', 'status');
}
else
{
drupal_set_message('Error uploading file : ' . $dest_path . '/' . $file_name, 'error');
}
break;
}
}
}
drupal_set_message('Example uploaded successfully.', 'status');
/* sending email */
$email_to = $user_data->mail;
$param['example_updated_admin']['example_id'] = $example_id;
$param['example_updated_admin']['user_id'] = $proposal_data->uid;
if (!drupal_mail('textbook_companion', 'example_updated_admin', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE))
drupal_set_message('Error sending email message.', 'error');
drupal_set_message(t("Example successfully udpated."), 'status');
}
/******************************************************************************/
/************************** GENERAL FUNCTIONS *********************************/
/******************************************************************************/
function _list_of_book_titles()
{
$book_titles = array(
'0' => 'Please select...'
);
/*$book_titles_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE approval_status = 1 OR approval_status = 3 ORDER BY book ASC");*/
$query = db_select('textbook_companion_preference');
$query->fields('textbook_companion_preference');
$or = db_or();
$or->condition('approval_status', 1);
$or->condition('approval_status', 3);
$query->condition($or);
$query->orderBy('book', 'ASC');
$book_titles_q = $query->execute();
while ($book_titles_data = $book_titles_q->fetchObject())
{
$book_titles[$book_titles_data->id] = $book_titles_data->book . ' (Written by ' . $book_titles_data->author . ')';
}
return $book_titles;
}
function _list_of_book_dependency_files()
{
$book_dependency_files = array();
$book_dependency_files_class = array();
/*$book_dependency_files_q = db_query("SELECT * FROM {textbook_companion_dependency_files} ORDER BY filename ASC");*/
$query = db_select('textbook_companion_dependency_files');
$query->fields('textbook_companion_dependency_files');
$query->orderBy('filename', 'ASC');
$book_dependency_files_q = $query->execute();
while ($book_dependency_files_data = $book_dependency_files_q->fetchObject())
{
$temp_caption = '';
if ($book_dependency_files_data->caption)
$temp_caption .= ' (' . $book_dependency_files_data->caption . ')';
$book_dependency_files[$book_dependency_files_data->id] = l($book_dependency_files_data->filename . $temp_caption, '/textbook-companion/download/dependency/' . $book_dependency_files_data->id);
$book_dependency_files_class[$book_dependency_files_data->id] = $book_dependency_files_data->preference_id;
}
return array(
$book_dependency_files,
$book_dependency_files_class
);
}