diff options
author | Jayaram Pai | 2014-06-01 23:36:39 +0530 |
---|---|---|
committer | Jayaram Pai | 2014-06-01 23:36:39 +0530 |
commit | 8a663361ae1e6bce4c0bcaf7efafbc2e4febf0d2 (patch) | |
tree | 29d2bb26d6e9c31b517c8c59bdefe96d5f4a9336 | |
download | oscad_textbook_companion-master.tar.gz oscad_textbook_companion-master.tar.bz2 oscad_textbook_companion-master.zip |
-rwxr-xr-x | code.inc | 531 | ||||
-rwxr-xr-x | code_approval.inc | 673 | ||||
-rwxr-xr-x | download.inc | 168 | ||||
-rwxr-xr-x | editcode.inc | 907 | ||||
-rwxr-xr-x | full_download.inc | 143 | ||||
-rwxr-xr-x | general.inc | 192 | ||||
-rwxr-xr-x | manage_proposal.inc | 982 | ||||
-rwxr-xr-x | notes.inc | 120 | ||||
-rwxr-xr-x | proposal.inc | 452 | ||||
-rwxr-xr-x | run.inc | 332 | ||||
-rwxr-xr-x | settings.inc | 54 | ||||
-rwxr-xr-x | textbook_companion.info | 6 | ||||
-rwxr-xr-x | textbook_companion.module | 1078 |
13 files changed, 5638 insertions, 0 deletions
diff --git a/code.inc b/code.inc new file mode 100755 index 0000000..58a23e2 --- /dev/null +++ b/code.inc @@ -0,0 +1,531 @@ +<?php +// $Id$ + +function upload_examples() +{ + return drupal_get_form('upload_examples_form'); +} + +function upload_examples_form($form_state) +{ + 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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto(''); + } + if ($proposal_data->proposal_status != 1) + { + 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', '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', '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); + 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 **************************/ + + /* add javascript for automatic book title, check if example uploaded, dependency selection effects */ + $chapter_name_js = " $(document).ready(function() { + $('#edit-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/chapter_title/' + $('#edit-number').val() + '/' + " . $preference_data->id . ", function(data) { + data = data.toString().replace('\t', ''); + $('#edit-name').val(data); + }); + }); + $('#edit-example-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/example_exists/' + $('#edit-number').val() + '/' + " . $preference_data->id . " + '/' + $('#edit-example-number').val(), function(data) { + if (!data) { + alert(data); + } + }); + }); + $('#edit-existing-depfile-dep-book-title').change(function() { + var dep_selected = ''; + /* showing and hiding relevant files */ + $('.form-checkboxes .option').hide(); + $('.form-checkboxes .option').each(function(index) { + var activeClass = $('#edit-existing-depfile-dep-book-title').val(); + if ($(this).children().hasClass(activeClass)) { + $(this).show(); + } + if ($(this).children().attr('checked') == true) { + dep_selected += $(this).children().next().text() + '<br />'; + } + }); + /* showing list of already existing dependencies */ + $('#existing_depfile_selected').html(dep_selected); + }); + + $('.form-checkboxes .option').change(function() { + $('#edit-existing-depfile-dep-book-title').trigger('change'); + }); + $('#edit-existing-depfile-dep-book-title').trigger('change'); + });"; + drupal_add_js($chapter_name_js, 'inline', 'header'); + + $form['#attributes'] = array('enctype' => "multipart/form-data"); + + $form['book_details']['book'] = array( + '#type' => 'item', + '#value' => $preference_data->book, + '#title' => t('Title of the Book'), + ); + $form['contributor_name'] = array( + '#type' => 'item', + '#value' => $proposal_data->full_name, + '#title' => t('Contributor Name'), + ); + + $form['number'] = array( + '#type' => 'select', + '#title' => t('Chapter No'), + '#options' => array('' => '(Select)', '1' => '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'), + '#multiple' => FALSE, + '#size' => 1, + '#required' => TRUE, + ); + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Title of the Chapter'), + '#size' => 40, + '#maxlength' => 255, + '#required' => TRUE, + ); + $form['example_type'] = array( + '#type' => 'select', + '#title' => t('Example Type'), + '#options' => array('' => '(Select)', 1 => 'Simulation', 2 => 'PCB', 3 => 'Simulation & PCB'), + '#multiple' => FALSE, + '#size' => 1, + '#required' => TRUE, + ); + $form['example_number'] = array( + '#type' => 'textfield', + '#title' => t('Example No'), + '#size' => 5, + '#maxlength' => 10, + '#description' => t("Example number should be separated by dots only.<br />Example: 1.1.a or 1.1.1"), + '#required' => TRUE, + ); + $form['example_caption'] = array( + '#type' => 'textfield', + '#title' => t('Caption'), + '#size' => 40, + '#maxlength' => 255, + '#description' => t('Example caption should contain only alphabets, numbers and spaces.'), + '#required' => TRUE, + ); + $form['example_warning'] = array( + '#type' => 'item', + '#title' => t('You should upload all the files as zip (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, + ); + $form['sourcefile']['sourcefile1'] = array( + '#type' => 'file', + '#title' => t('Upload main or source file'), + '#size' => 48, + '#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_source_extensions', '').'</span>', + ); + + $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_form_validate($form, &$form_state) +{ + if (!check_name($form_state['values']['name'])) + form_set_error('name', t('Title of the Chapter can contain only alphabets, numbers and spaces.')); + + if (!check_name($form_state['values']['example_caption'])) + form_set_error('example_caption', t('Example Caption can contain only alphabets, numbers and spaces.')); + + if (!check_chapter_number($form_state['values']['example_number'])) + form_set_error('example_number', t('Invalid Example Number. Example Number can contain only alphabets and numbers sepereated by dot.')); + + if (isset($_FILES['files'])) + { + /* check if atleast one source or result file is uploaded */ + if ( ! ($_FILES['files']['name']['sourcefile1'])) + form_set_error('sourcefile1', t('Please upload source file.')); + /* check for valid filename extensions */ + foreach ($_FILES['files']['name'] as $file_form_name => $file_name) + { + if ($file_name) + { + $allowed_extensions_str = variable_get('textbook_companion_source_extensions', ''); + + $allowed_extensions = explode(',' , $allowed_extensions_str); + $temp_extension = end(explode('.', strtolower($_FILES['files']['name'][$file_form_name]))); + //$temp_extension = substr($_FILES['files']['name'][$file_form_name], strripos($_FILES['files']['name'][$file_form_name], '.')); // get file name + //var_dump($temp_extension); die; + 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.'));*/ + } + } + + } + + /* add javascript again for automatic book title, check if example uploaded, dependency selection effects */ + $chapter_name_js = " $(document).ready(function() { + $('#edit-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/chapter_title/' + $('#edit-number').val() + '/' + " . $row->pre_id . ", function(data) { + $('#edit-name').val(data); + }); + }); + $('#edit-example-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/example_exists/' + $('#edit-number').val() + '/' + $('#edit-example-number').val(), function(data) { + if (data) { + alert(data); + } + }); + }); + $('#edit-existing-depfile-dep-book-title').change(function() { + var dep_selected = ''; + /* showing and hiding relevant files */ + $('.form-checkboxes .option').hide(); + $('.form-checkboxes .option').each(function(index) { + var activeClass = $('#edit-existing-depfile-dep-book-title').val(); + if ($(this).children().hasClass(activeClass)) { + $(this).show(); + } + if ($(this).children().attr('checked') == true) { + dep_selected += $(this).children().next().text() + '<br />'; + } + }); + /* showing list of already existing dependencies */ + $('#existing_depfile_selected').html(dep_selected); + }); + + $('.form-checkboxes .option').change(function() { + $('#edit-existing-depfile-dep-book-title').trigger('change'); + }); + $('#edit-existing-depfile-dep-book-title').trigger('change'); + });"; + drupal_add_js($chapter_name_js, 'inline', 'header'); +} + +function upload_examples_form_submit($form, &$form_state) { + global $user; + + $root_path = textbook_companion_path(); + + /************************ 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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto(''); + } + if ($proposal_data->proposal_status != 1) + { + 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', '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); + 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 **************************/ + + $preference_id = $preference_data->id; + + $dest_path = $preference_id . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* inserting chapter details */ + $chapter_id = 0; + $chapter_result = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d AND number = %d", $preference_id, $form_state['values']['number']); + if (!$chapter_row = db_fetch_object($chapter_result)) + { + db_query("INSERT INTO {textbook_companion_chapter} (preference_id, number, name) VALUES (%d, '%s', '%s')", + $preference_id, + $form_state['values']['number'], + $form_state['values']['name'] + ); + $chapter_id = db_last_insert_id('textbook_companion_chapter', 'id'); + } else { + $chapter_id = $chapter_row->id; + db_query("UPDATE {textbook_companion_chapter} SET name = '%s' WHERE id = %d", $form_state['values']['name'], $chapter_id); + } + + /* get example details - dont allow if already example present */ + $cur_example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND number = '%s'", $chapter_id, $form_state['values']['example_number']); + if ($cur_example_d = db_fetch_object($cur_example_q)) + { + if ($cur_example_d->approval_status == 1) + { + drupal_set_message(t("Example already approved. Cannot overwrite it."), 'error'); + drupal_goto('textbook_companion/code'); + return; + } else if ($cur_example_d->approval_status == 0) { + drupal_set_message(t("Example is under pending review. Delete the example and reupload it."), 'error'); + drupal_goto('textbook_companion/code'); + return; + } else { + drupal_set_message(t("Error uploading example. Please contact administrator."), 'error'); + drupal_goto('textbook_companion/code'); + return; + } + } + + /* creating directories */ + $dest_path .= 'CH' . $form_state['values']['number'] . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + $dest_path .= 'EX' . $form_state['values']['example_number'] . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* creating example database entry */ + db_query("INSERT INTO {textbook_companion_example} (chapter_id, number, caption, approval_status, timestamp, example_type) VALUES (%d, '%s', '%s', %d, %d, %d)", + $chapter_id, + $form_state['values']['example_number'], + $form_state['values']['example_caption'], + 0, + time(), + $form_state['values']['example_type'] + ); + $example_id = db_last_insert_id('textbook_companion_example', 'id'); + + /* uploading files */ + foreach ($_FILES['files']['name'] as $file_form_name => $file_name) + { + if ($file_name) + { + /* checking file type */ + $file_type = '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])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_id, + $_FILES['files']['name'][$file_form_name], + $dest_path . $_FILES['files']['name'][$file_form_name], + $_FILES['files']['type'][$file_form_name], + $_FILES['files']['size'][$file_form_name], + $file_type, + time() + ); + drupal_set_message($file_name . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $file_name, 'error'); + } + } + } + drupal_set_message('Example uploaded successfully.', 'status'); + + /* sending email */ + $email_to = $user->mail; + $param['example_uploaded']['example_id'] = $example_id; + $param['example_uploaded']['user_id'] = $user->uid; + if (!drupal_mail('textbook_companion', 'example_uploaded', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + drupal_goto('textbook_companion/code'); +} + +/******************************************************************************/ +/***************************** DELETE EXAMPLE *********************************/ +/******************************************************************************/ + +function _upload_examples_delete() +{ + global $user; + $root_path = textbook_companion_path(); + $example_id = arg(3); + + /* check example */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $example_id); + $example_data = db_fetch_object($example_q); + if (!$example_data) + { + drupal_set_message('Invalid example.', 'error'); + drupal_goto('textbook_companion/code'); + return; + } + if ($example_data->approval_status != 0) + { + drupal_set_message('You cannnot delete an example after it has been approved. Please contact site administrator if you want to delete this example.', 'error'); + drupal_goto('textbook_companion/code'); + return; + } + + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d LIMIT 1", $example_data->chapter_id); + $chapter_data = db_fetch_object($chapter_q); + if (!$chapter_data) + { + drupal_set_message('You do not have permission to delete this example.', 'error'); + drupal_goto('textbook_companion/code'); + return; + } + + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d LIMIT 1", $chapter_data->preference_id); + $preference_data = db_fetch_object($preference_q); + if (!$preference_data) + { + drupal_set_message('You do not have permission to delete this example.', 'error'); + drupal_goto('textbook_companion/code'); + return; + } + + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d AND uid = %d LIMIT 1", $preference_data->proposal_id, $user->uid); + $proposal_data = db_fetch_object($proposal_q); + if (!$proposal_data) + { + drupal_set_message('You do not have permission to delete this example.', 'error'); + drupal_goto('textbook_companion/code'); + return; + } + + /* deleting example files */ + if (delete_example($example_data->id)) + { + drupal_set_message('Example deleted.', 'status'); + + /* sending email */ + $email_to = $user->mail; + $param['example_deleted_user']['book_title'] = $preference_data->book; + $param['example_deleted_user']['chapter_title'] = $chapter_data->name; + $param['example_deleted_user']['example_number'] = $example_data->number; + $param['example_deleted_user']['example_caption'] = $example_data->caption; + + $param['example_deleted_user']['user_id'] = $user->uid; + + if (!drupal_mail('textbook_companion', 'example_deleted_user', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } else { + drupal_set_message('Error deleting example.', 'status'); + } + + drupal_goto('textbook_companion/code'); + return; +} + +/******************************************************************************/ +/************************** 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"); + while ($book_titles_data = db_fetch_object($book_titles_q)) + { + $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"); + + while ($book_dependency_files_data = db_fetch_object($book_dependency_files_q)) + { + $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, '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); +} + diff --git a/code_approval.inc b/code_approval.inc new file mode 100755 index 0000000..4d60e31 --- /dev/null +++ b/code_approval.inc @@ -0,0 +1,673 @@ +<?php +// $Id$ + +function code_approval() +{ + /* get a list of unapproved chapters */ + $pending_chapter_q = db_query("SELECT c.id as c_id, c.number as c_number, c.name as c_name, c.preference_id as c_preference_id FROM {textbook_companion_example} as e JOIN {textbook_companion_chapter} as c ON c.id = e.chapter_id WHERE e.approval_status = 0"); + if (!$pending_chapter_q) + { + drupal_set_message(t('There are no pending code approvals.'), 'status'); + return ''; + } + $rows = array(); + while ($row = db_fetch_object($pending_chapter_q)) + { + /* get preference data */ + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $row->c_preference_id); + $preference_data = db_fetch_object($preference_q); + /* get proposal data */ + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $preference_data->proposal_id); + $proposal_data = db_fetch_object($proposal_q); + /* setting table row information */ + $rows[] = array($preference_data->book, $row->c_number, $row->c_name, $proposal_data->full_name, l('Edit', 'code_approval/approve/' . $row->c_id)); + } + + /* check if there are any pending proposals */ + if (!$rows) + { + drupal_set_message(t('There are no pending proposals'), 'status'); + return ''; + } + + $header = array('Title of the Book', 'Chapter Number', 'Title of the Chapter', 'Contributor Name', 'Actions'); + $output = theme_table($header, $rows); + return $output; +} + +function code_approval_form($form_state) +{ + /* get a list of unapproved chapters */ + $chapter_id = arg(2); + $pending_chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id); + if ($pending_chapter_data = db_fetch_object($pending_chapter_q)) + { + /* get preference data */ + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $pending_chapter_data->preference_id); + $preference_data = db_fetch_object($preference_q); + /* get proposal data */ + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $preference_data->proposal_id); + $proposal_data = db_fetch_object($proposal_q); + } else { + drupal_set_message(t('Invalid chapter selected.'), 'error'); + drupal_goto('code_approval'); + return; + } + + $form['#tree'] = TRUE; + + $form['contributor'] = array( + '#type' => 'item', + '#value' => $proposal_data->full_name, + '#title' => t('Contributor Name'), + ); + + $form['book_details']['book'] = array( + '#type' => 'item', + '#value' => $preference_data->book, + '#title' => t('Title of the Book'), + ); + + $form['book_details']['number'] = array( + '#type' => 'item', + '#value' => $pending_chapter_data->number, + '#title' => t('Chapter Number'), + ); + + $form['book_details']['name'] = array( + '#type' => 'item', + '#value' => $pending_chapter_data->name, + '#title' => t('Title of the Chapter'), + ); + + $form['book_details']['back_to_list'] = array( + '#type' => 'item', + '#value' => l('Back to Code Approval List', 'code_approval'), + ); + + /* get example data */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 0", $chapter_id); + while ($example_data = db_fetch_object($example_q)) + { + $form['example_details'][$example_data->id] = array( + '#type' => 'fieldset', + '#collapsible' => FALSE, + '#collapsed' => TRUE, + ); + $form['example_details'][$example_data->id]['example_number'] = array( + '#type' => 'item', + '#value' => $example_data->number, + '#title' => t('Example Number'), + ); + + $form['example_details'][$example_data->id]['example_caption'] = array( + '#type' => 'item', + '#value' => $example_data->caption, + '#title' => t('Example Caption'), + ); + + $form['example_details'][$example_data->id]['download'] = array( + '#type' => 'markup', + '#value' => l('Download Example', 'download/example/' . $example_data->id), + ); + + $form['example_details'][$example_data->id]['approved'] = array( + '#type' => 'radios', + '#options' => array('Approved', 'Dis-approved'), + ); + + $form['example_details'][$example_data->id]['message'] = array( + '#type' => 'textfield', + '#title' => t('Reason for dis-approval'), + ); + + $form['example_details'][$example_data->id]['example_id'] = array( + '#type' => 'hidden', + '#value' => $example_data->id, + ); + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + return $form; +} + +function code_approval_form_submit($form, &$form_state) +{ + global $user; + + foreach ($form_state['values']['example_details'] as $ex_id => $ex_data) + { + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d LIMIT 1", $ex_data['example_id']); + $example_data = db_fetch_object($example_q); + $chapter_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d LIMIT 1", $example_data->chapter_id)); + $preference_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d LIMIT 1", $chapter_data->preference_id)); + $proposal_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d LIMIT 1", $preference_data->proposal_id)); + $user_data = user_load($proposal_data->uid); + + // del_book_pdf($preference_data->id); + + if ($ex_data['approved'] == "0") + { + db_query("UPDATE {textbook_companion_example} SET approval_status = 1, approver_uid = %d, approval_date = %d WHERE id = %d", $user->uid, time(), $ex_data['example_id']); + + /* sending email */ + $email_to = $user_data->mail; + $param['example_approved']['example_id'] = $ex_data['example_id']; + $param['example_approved']['user_id'] = $user_data->uid; + if (!drupal_mail('textbook_companion', 'example_approved', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + } else if ($ex_data['approved'] == "1") { + if (delete_example($ex_data['example_id'])) + { + /* sending email */ + $email_to = $user_data->mail; + $param['example_disapproved']['example_number'] = $example_data->number; + $param['example_disapproved']['example_caption'] = $example_data->caption; + $param['example_disapproved']['user_id'] = $user_data->uid; + $param['example_disapproved']['message'] = $ex_data['message']; + if (!drupal_mail('textbook_companion', 'example_disapproved', $email_to, language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } else { + drupal_set_message('Error disapproving and deleting example. Please contact administrator.', 'error'); + } + } + } + drupal_set_message('Updated successfully.', 'status'); + drupal_goto('code_approval'); +} + + +/******************************************************************************/ +/********************************* BULK APPROVAL ******************************/ +/******************************************************************************/ + +function bulk_approval_form($form_state) +{ + $form['#redirect'] = FALSE; + + ahah_helper_register($form, $form_state); + + /* default value for ahah fields */ + if (!isset($form_state['storage']['run']['book'])) + { + $book_default_value = 0; + } else { + $book_default_value = $form_state['storage']['run']['book']; + } + + if (!isset($form_state['storage']['run']['chapter'])) + { + $chapter_default_value = 0; + } else { + if ($form_state['values']['run']['book_hidden'] != $form_state['values']['run']['book']) + $chapter_default_value = 0; + else + $chapter_default_value = $form_state['storage']['run']['chapter']; + } + + if (!isset($form_state['storage']['run']['example'])) + { + $example_default_value = 0; + } else { + if ($form_state['values']['run']['book_hidden'] != $form_state['values']['run']['book']) + $example_default_value = 0; + else if ($form_state['values']['run']['chapter_hidden'] != $form_state['values']['run']['chapter']) + $example_default_value = 0; + else + $example_default_value = $form_state['storage']['run']['example']; + } + + $form['run'] = array( + '#type' => 'fieldset', + '#title' => t('Bulk Manage Code'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + '#prefix' => '<div id="run-wrapper">', + '#suffix' => '</div>', + '#tree' => TRUE, + ); + + $form['run']['book'] = array( + '#type' => 'select', + '#title' => t('Title of the Book'), + '#options' => _list_of_books(), + '#default_value' => $book_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + + /* hidden form elements */ + $form['run']['book_hidden'] = array( + '#type' => 'hidden', + '#value' => $form_state['values']['run']['book'], + ); + + /* hidden form elements */ + $form['run']['chapter_hidden'] = array( + '#type' => 'hidden', + '#value' => $form_state['values']['run']['chapter'], + ); + + if ($book_default_value > 0) + { + $form['run']['download_book'] = array( + '#type' => 'item', + '#value' => l('Download', 'full_download/book/' . $book_default_value) . ' ' . t('(Download all the approved and unapproved examples of the entire book)'), + ); + /*$form['run']['download_pdf'] = array( + '#type' => 'item', + '#value' => l('Download PDF', 'textbook_companion/generate_book/' . $book_default_value . '/1') . ' ' . t('(Download PDF of all the approved and unapproved examples of the entire book)'), + ); + $form['run']['regenrate_book'] = array( + '#type' => 'item', + '#value' => l('Regenerate PDF', 'textbook_companion/delete_book/' . $book_default_value) . ' ' . t('(Manually Regenerate PDF of the entire book)'), + );*/ + $form['run']['notes_book'] = array( + '#type' => 'item', + '#value' => l('Notes for Reviewers', 'code_approval/notes/' . $book_default_value), + ); + + $form['run']['approve_book'] = array( + '#type' => 'checkbox', + '#title' => t('Approve Entire Book'), + ); + $form['run']['unapprove_book'] = array( + '#type' => 'checkbox', + '#title' => t('Pending Review Entire Book'), + ); + $form['run']['disapprove_book'] = array( + '#type' => 'checkbox', + '#title' => t('Dis-Approve Entire Book (This will delete all the examples in the book)'), + '#prefix' => '<div style="color:red;"><strong>', + '#suffix' => '</strong></div>', + ); + $form['run']['delete_book_including_proposal'] = array( + '#type' => 'checkbox', + '#title' => t('Delete Entire Book Including Proposal'), + '#prefix' => '<div style="color:red;"><strong>', + '#suffix' => '</strong></div>', + ); + + $form['run']['chapter'] = array( + '#type' => 'select', + '#title' => t('Title of the Chapter'), + '#options' => _list_of_chapters($book_default_value), + '#default_value' => $chapter_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + if ($chapter_default_value > 0) + { + $form['run']['download_chapter'] = array( + '#type' => 'item', + '#value' => l('Download', 'full_download/chapter/' . $chapter_default_value) . ' ' . t('(Download all the approved and unapproved examples of the entire chapter)'), + ); + + $form['run']['approve_chapter'] = array( + '#type' => 'checkbox', + '#title' => t('Approve Entire Chapter'), + ); + $form['run']['unapprove_chapter'] = array( + '#type' => 'checkbox', + '#title' => t('Pending Review Entire Chapter'), + ); + $form['run']['disapprove_chapter'] = array( + '#type' => 'checkbox', + '#title' => t('Dis-Approve Entire Chapter (This will delete all the examples in the chapter)'), + '#prefix' => '<div style="color:red;"><strong>', + '#suffix' => '</strong></div>', + ); + + $form['run']['example'] = array( + '#type' => 'select', + '#title' => t('Example No. (Caption)'), + '#options' => _list_of_examples($chapter_default_value), + '#default_value' => $example_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + } + } + + /************ START OF $_POST **************/ + if ($_POST) + { + if (($book_default_value > 0) && ($chapter_default_value > 0) && ($example_default_value > 0)) + { + $example_list_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $form_state['values']['run']['example']); + if ($example_list_q) + { + $example_files_rows = array(); + while ($example_list_data = db_fetch_object($example_list_q)) + { + $example_file_type = ''; + switch ($example_list_data->filetype) + { + case 'S' : $example_file_type = 'Source or Main file'; break; + case 'R' : $example_file_type = 'Result file'; break; + case 'X' : $example_file_type = 'xcos file'; break; + default : $example_file_type = 'Unknown'; break; + } + $example_files_rows[] = array(l($example_list_data->filename, 'download/file/' . $example_list_data->id), $example_file_type); + } + + /* creating list of files table */ + $example_files_header = array('Filename', 'Type'); + $example_files = theme_table($example_files_header, $example_files_rows); + } + $form['run']['download_example'] = array( + '#type' => 'item', + '#value' => l('Download Example', 'download/example/' . $example_default_value), + ); + $form['run']['edit_example'] = array( + '#type' => 'item', + '#value' => l('Edit Example', 'code_approval/editcode/' . $example_default_value), + ); + + $form['run']['example_files'] = array( + '#type' => 'item', + '#title' => 'List of example files', + '#value' => $example_files, + ); + + $form['run']['approve_example'] = array( + '#type' => 'checkbox', + '#title' => t('Approve Example'), + ); + $form['run']['unapprove_example'] = array( + '#type' => 'checkbox', + '#title' => t('Pending Review Example'), + ); + $form['run']['disapprove_example'] = array( + '#type' => 'checkbox', + '#title' => t('Dis-approve Example (This will delete the example)'), + '#prefix' => '<div style="color:red;"><strong>', + '#suffix' => '</strong></div>', + ); + } + } + /************ END OF $_POST **************/ + + if ($book_default_value > 0) + { + $form['run']['message'] = array( + '#type' => 'textarea', + '#title' => t('If Dis-Approved please specify reason for Dis-Approval'), + ); + + $form['run']['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + } + + return $form; +} + + +function bulk_approval_form_submit($form, &$form_state) +{ + global $user; + $root_path = textbook_companion_path(); + + if ($form_state['clicked_button']['#value'] == 'Submit') + { + /*if ($form_state['values']['run']['book']) + del_book_pdf($form_state['values']['run']['book']);*/ + + if (user_access('bulk manage code')) + { + if ($form_state['values']['run']['approve_book'] == "1") + { + /* approving entire book */ + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $form_state['values']['run']['book']); + 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); + } + drupal_set_message(t('Approved Entire Book.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded examples have been approved'); + $email_body = t('Your all the uploaded examples for the book have been approved.'); + + } else if ($form_state['values']['run']['unapprove_book'] == "1") { + + /* approving entire book */ + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $form_state['values']['run']['book']); + while ($chapter_data = db_fetch_object($chapter_q)) + { + db_query("UPDATE {textbook_companion_example} SET approval_status = 0 WHERE chapter_id = %d", $chapter_data->id); + } + drupal_set_message(t('Pending Review Entire Book.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded examples have been marked as pending'); + $email_body = t('Your all the uploaded examples for the book have been marked as pending to be review. You will be able to see the exmaples after they have been approved by one of our reviewers.'); + + } else if ($form_state['values']['run']['disapprove_book'] == "1") { + + if (!user_access('bulk delete code')) + { + drupal_set_message(t('You do not have permission to Bulk Dis-Approved and Deleted Entire Book.'), 'error'); + return; + } + + if (delete_book($form_state['values']['run']['book'])) + { + drupal_set_message(t('Dis-Approved and Deleted Entire Book.'), 'status'); + } else { + drupal_set_message(t('Error Dis-Approving and Deleting Entire Book.'), 'error'); + } + + /* email */ + $email_subject = t('Your uploaded examples have been marked as dis-approved'); + $email_body = t('Your all the uploaded examples for the whole book have been marked as dis-approved. + +Reason for dis-approval: + +' . $form_state['values']['run']['message']); + + } else if ($form_state['values']['run']['delete_book_including_proposal'] == "1") { + + if (!user_access('bulk delete code')) + { + drupal_set_message(t('You do not have permission to Bulk Delete Entire Book Including Proposal.'), 'error'); + return; + } + + if (delete_book($form_state['values']['run']['book'])) + { + drupal_set_message(t('Dis-Approved and Deleted Entire Book examples.'), 'status'); + + $dir_path = $root_path . $form_state['values']['run']['book']; + if (is_dir($dir_path)) + { + $res = rmdir($dir_path); + if (!$res) + { + drupal_set_message(t("Cannot delete Book directory : " . $dir_path . ". Please contact administrator."), 'error'); + return; + } + } else { + drupal_set_message(t("Book directory not present : " . $dir_path . ". Skipping deleting book directory."), 'status'); + } + + /* deleting preference and proposal */ + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $form_state['values']['run']['book']); + $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); + + drupal_set_message(t('Deleted Book Proposal.'), 'status'); + + /* 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.'); + + } else { + drupal_set_message(t('Error Dis-Approving and Deleting Entire Book.'), 'error'); + } + + } else if ($form_state['values']['run']['approve_chapter'] == "1") { + + db_query("UPDATE {textbook_companion_example} SET approval_status = 1, approver_uid = %d WHERE chapter_id = %d AND approval_status = 0", $user->uid, $form_state['values']['run']['chapter']); + drupal_set_message(t('Approved Entire Chapter.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded examples have been approved'); + $email_body = t('Your all the uploaded examples for the chapter have been approved.'); + + } else if ($form_state['values']['run']['unapprove_chapter'] == "1") { + + db_query("UPDATE {textbook_companion_example} SET approval_status = 0 WHERE chapter_id = %d", $form_state['values']['run']['chapter']); + drupal_set_message(t('Entire Chapter marked as Pending Review.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded examples have been marked as pending'); + $email_body = t('Your all the uploaded examples for the chapter have been marked as pending to be review.'); + + } else if ($form_state['values']['run']['disapprove_chapter'] == "1") { + + if (!user_access('bulk delete code')) + { + drupal_set_message(t('You do not have permission to Bulk Dis-Approved and Deleted Entire Chapter.'), 'error'); + return; + } + + if (delete_chapter($form_state['values']['run']['chapter'])) + { + drupal_set_message(t('Dis-Approved and Deleted Entire Chapter.'), 'status'); + } else { + drupal_set_message(t('Error Dis-Approving and Deleting Entire Chapter.'), 'error'); + } + + /* email */ + $email_subject = t('Your uploaded example have been marked as dis-approved'); + $email_body = t('Your uploaded example for the entire chapter have been marked as dis-approved. + +Reason for dis-approval: + +' . $form_state['values']['run']['message']); + + } else if ($form_state['values']['run']['approve_example'] == "1") { + + db_query("UPDATE {textbook_companion_example} SET approval_status = 1, approver_uid = %d WHERE id = %d", $user->uid, $form_state['values']['run']['example']); + drupal_set_message(t('Example approved.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded example has been approved'); + $email_body = t('Your uploaded example has been approved.'); + + } else if ($form_state['values']['run']['unapprove_example'] == "1") { + + db_query("UPDATE {textbook_companion_example} SET approval_status = 0 WHERE id = %d", $form_state['values']['run']['example']); + drupal_set_message(t('Example marked as Pending Review.'), 'status'); + + /* email */ + $email_subject = t('Your uploaded example has been marked as pending'); + $email_body = t('Your uploaded example has been marked as pending to be review.'); + + } else if ($form_state['values']['run']['disapprove_example'] == "1") { + + if (delete_example($form_state['values']['run']['example'])) + { + drupal_set_message(t('Example Dis-Approved and Deleted.'), 'status'); + } else { + drupal_set_message(t('Error Dis-Approving and Deleting Example.'), 'error'); + } + + /* email */ + $email_subject = t('Your uploaded example has been marked as dis-approved'); + $email_body = t('Your uploaded example has been marked as dis-approved. + +Reason for dis-approval: + +' . $form_state['values']['run']['message']); + + } + + /****** sending email when everything done ******/ + 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)) + drupal_set_message('Error sending email message.', 'error'); + } + + } else { + drupal_set_message(t('You do not have permission to bulk manage code.'), 'error'); + } + } +} + +function _list_of_books() +{ + $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"); + while ($book_titles_data = db_fetch_object($book_titles_q)) + { + $book_titles[$book_titles_data->id] = $book_titles_data->book . ' (Written by ' . $book_titles_data->author . ')'; + } + return $book_titles; +} + +function _list_of_chapters($preference_id = 0) +{ + $book_chapters = array('0' => 'Please select...'); + $book_chapters_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number ASC", $preference_id); + while ($book_chapters_data = db_fetch_object($book_chapters_q)) + { + $book_chapters[$book_chapters_data->id] = $book_chapters_data->number . '. ' . $book_chapters_data->name; + } + return $book_chapters; +} + +function _list_of_examples($chapter_id = 0) +{ + $book_examples = array('0' => 'Please select...'); + $book_examples_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d ORDER BY + CAST(SUBSTRING_INDEX(number, '.', 1) AS BINARY) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', 2), '.', -1) AS UNSIGNED) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', -1), '.', 1) AS UNSIGNED) ASC", $chapter_id); + while ($book_examples_data = db_fetch_object($book_examples_q)) + { + $book_examples[$book_examples_data->id] = $book_examples_data->number . ' (' . $book_examples_data->caption . ')'; + } + return $book_examples; +} + diff --git a/download.inc b/download.inc new file mode 100755 index 0000000..4274718 --- /dev/null +++ b/download.inc @@ -0,0 +1,168 @@ +<?php +// $Id$ + +function textbook_companion_download_example_file() +{ + $example_file_id = arg(2); + $root_path = textbook_companion_path(); + + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE id = %d LIMIT 1", $example_file_id); + $example_file_data = db_fetch_object($example_files_q); + ob_clean(); + header('Content-Type: ' . $example_file_data->filemime); + header('Content-disposition: attachment; filename="' . $example_file_data->filename . '"'); + header('Content-Length: ' . filesize($root_path . $example_file_data->filepath)); + readfile($root_path . $example_file_data->filepath); +} + +function textbook_companion_download_dependency_file() +{ + $dependency_file_id = arg(2); + $root_path = textbook_companion_path(); + + $dependency_file_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $dependency_file_id); + $dependency_file_data = db_fetch_object($dependency_file_q); + ob_clean(); + header('Content-Type: ' . $dependency_file_data->filemime); + header('Content-disposition: attachment; filename="' . $dependency_file_data->filename . '"'); + header('Content-Length: ' . filesize($root_path . $dependency_file_data->filepath)); + readfile($root_path . $dependency_file_data->filepath); +} + +function textbook_companion_download_example() +{ + $example_id = arg(2); + $root_path = textbook_companion_path(); + + /* get example data */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d", $example_id); + $example_data = db_fetch_object($example_q); + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id); + $chapter_data = db_fetch_object($chapter_q); + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_id); + + $EX_PATH = 'EX' . $example_data->number . '/'; + + /* zip filename */ + $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip'; + /* creating zip archive on the server */ + $zip = new ZipArchive; + $zip->open($zip_filename, ZipArchive::CREATE); + + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $EX_PATH . $example_files_row->filename); + } + + $zip_file_count = $zip->numFiles; + $zip->close(); + + if ($zip_file_count > 0) + { + /* download zip file */ + ob_clean(); + header('Content-Type: application/zip'); + header('Content-disposition: attachment; filename="EX' . $example_data->number . '.zip"'); + header('Content-Length: ' . filesize($zip_filename)); + readfile($zip_filename); + unlink($zip_filename); + } else { + drupal_set_message("There are no files in this examples to download", 'error'); + drupal_goto('textbook_run'); + } +} + +function textbook_companion_download_chapter() +{ + $chapter_id = arg(2); + $root_path = textbook_companion_path(); + + /* get example data */ + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id); + $chapter_data = db_fetch_object($chapter_q); + $CH_PATH = 'CH' . $chapter_data->number . '/'; + + /* zip filename */ + $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip'; + + /* creating zip archive on the server */ + $zip = new ZipArchive; + $zip->open($zip_filename, ZipArchive::CREATE); + + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $CH_PATH . $EX_PATH . $example_files_row->filename); + } + } + $zip_file_count = $zip->numFiles; + $zip->close(); + + if ($zip_file_count > 0) + { + /* download zip file */ + ob_clean(); + header('Content-Type: application/zip'); + header('Content-disposition: attachment; filename="CH' . $chapter_data->number . '.zip"'); + header('Content-Length: ' . filesize($zip_filename)); + readfile($zip_filename); + unlink($zip_filename); + } else { + drupal_set_message("There are no examples in this chapter to download", 'error'); + drupal_goto('textbook_run'); + } +} + +function textbook_companion_download_book() +{ + $book_id = arg(2); + $root_path = textbook_companion_path(); + /* get example data */ + $book_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $book_id); + $book_data = db_fetch_object($book_q); + $BK_PATH = $book_data->book . '/'; + + /* zip filename */ + $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip'; + + /* creating zip archive on the server */ + $zip = new ZipArchive; + $zip->open($zip_filename, ZipArchive::CREATE); + + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $book_id); + while ($chapter_row = db_fetch_object($chapter_q)) + { + $CH_PATH = 'CH' . $chapter_row->number . '/'; + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_row->id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $BK_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename); + } + } + } + $zip_file_count = $zip->numFiles; + $zip->close(); + + if ($zip_file_count > 0) + { + /* download zip file */ + ob_clean(); + header('Content-Type: application/zip'); + header('Content-disposition: attachment; filename="' . $book_data->book . '.zip"'); + header('Content-Length: ' . filesize($zip_filename)); + readfile($zip_filename); + unlink($zip_filename); + } else { + drupal_set_message("There are no examples in this book to download", 'error'); + drupal_goto('textbook_run'); + } +} + diff --git a/editcode.inc b/editcode.inc new file mode 100755 index 0000000..37a6f2a --- /dev/null +++ b/editcode.inc @@ -0,0 +1,907 @@ +<?php +// $Id$ + +/******************************************************************************/ +/***************************** EDIT EXAMPLE ***********************************/ +/******************************************************************************/ + +function upload_examples_edit_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); + if (!$example_q) + { + drupal_set_message(t("Invalid example selected."), 'error'); + drupal_goto(''); + return; + } + if ($example_data->approval_status != 0) + { + drupal_set_message(t("You cannot edit an example after it has been approved or dis-approved. Please contact site administrator if you want to edit this example."), 'error'); + drupal_goto(''); + return; + } + + /* get examples files */ + $source_file = ""; $source_id = 0; + $result1_file = ""; $result1_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); + while ($example_files_data = db_fetch_object($example_files_q)) + { + if ($example_files_data->filetype == "S") + { + $source_file = l($example_files_data->filename, 'download/file/' . $example_files_data->id); + $source_file_id = $example_files_data->id; + } + if ($example_files_data->filetype == "R") + { + if (strlen($result1_file) == 0) + { + $result1_file = l($example_files_data->filename, 'download/file/' . $example_files_data->id); + $result1_file_id = $example_files_data->id; + } else { + $result2_file = l($example_files_data->filename, 'download/file/' . $example_files_data->id); + $result2_file_id = $example_files_data->id; + } + } + if ($example_files_data->filetype == "X") + { + if (strlen($xcos1_file) <= 0) + { + $xcos1_file = l($example_files_data->filename, 'download/file/' . $example_files_data->id); + $xcos1_file_id = $example_files_data->id; + } else { + $xcos2_file = l($example_files_data->filename, 'download/file/' . $example_files_data->id); + $xcos2_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); + 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); + if (!$preference_data) + { + drupal_set_message(t("Invalid book selected."), 'error'); + drupal_goto(''); + return; + } + if ($preference_data->approval_status != 1) + { + drupal_set_message(t("Cannot edit example. Either the book proposal has not been approved or it has been rejected."), '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); + if (!$proposal_data) + { + drupal_set_message(t("Invalid proposal selected."), 'error'); + drupal_goto(''); + return; + } + if ($proposal_data->uid != $user->uid) + { + drupal_set_message(t("You do not have permissions to edit this example."), 'error'); + drupal_goto(''); + return; + } + + $user_data = user_load($proposal_data->uid); + + /* add javascript for automatic book title, check if example uploaded, dependency selection effects */ + $chapter_name_js = " $(document).ready(function() { + $('#edit-existing-depfile-dep-book-title').change(function() { + var dep_selected = ''; + /* showing and hiding relevant files */ + $('.form-checkboxes .option').hide(); + $('.form-checkboxes .option').each(function(index) { + var activeClass = $('#edit-existing-depfile-dep-book-title').val(); + if ($(this).children().hasClass(activeClass)) { + $(this).show(); + } + if ($(this).children().attr('checked') == true) { + dep_selected += $(this).children().next().text() + '<br />'; + } + }); + /* showing list of already existing dependencies */ + $('#existing_depfile_selected').html(dep_selected); + }); + + $('.form-checkboxes .option').change(function() { + $('#edit-existing-depfile-dep-book-title').trigger('change'); + }); + $('#edit-existing-depfile-dep-book-title').trigger('change'); + });"; + drupal_add_js($chapter_name_js, 'inline', 'header'); + + $form['#redirect'] = 'textbook_companion/code'; + $form['#attributes'] = array('enctype' => "multipart/form-data"); + + $form['book_details']['book'] = array( + '#type' => 'item', + '#value' => $preference_data->book, + '#title' => t('Title of the Book'), + ); + $form['contributor_name'] = array( + '#type' => 'item', + '#value' => $proposal_data->full_name, + '#title' => t('Contributor Name'), + ); + $form['number'] = array( + '#type' => 'item', + '#title' => t('Chapter No'), + '#value' => $chapter_data->number, + ); + $form['name'] = array( + '#type' => 'item', + '#title' => t('Title of the Chapter'), + '#value' => $chapter_data->name, + ); + $form['example_number'] = array( + '#type' => 'item', + '#title' => t('Example No'), + '#value' => $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'), + '#value' => $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', + '#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['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'textbook_companion/code'), + ); + return $form; +} + +function upload_examples_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, 'source')) + $file_type = 'S'; + else if (strstr($file_form_name, 'result')) + $file_type = 'R'; + else if (strstr($file_form_name, 'xcos')) + $file_type = 'X'; + else + $file_type = 'U'; + + $allowed_extensions_str = ''; + switch ($file_type) + { + case 'S': + $allowed_extensions_str = variable_get('textbook_companion_source_extensions', ''); + break; + case 'R': + $allowed_extensions_str = variable_get('textbook_companion_result_extensions', ''); + break; + case 'X': + $allowed_extensions_str = variable_get('textbook_companion_xcos_extensions', ''); + break; + } + + $allowed_extensions = explode(',' , $allowed_extensions_str); + $temp_extension = end(explode('.', strtolower($_FILES['files']['name'][$file_form_name]))); + 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.')); */ + } + } + } + + /* add javascript again for automatic book title, check if example uploaded, dependency selection effects */ + $chapter_name_js = " $(document).ready(function() { + $('#edit-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/chapter_title/' + $('#edit-number').val() + '/' + " . $row->pre_id . ", function(data) { + $('#edit-name').val(data); + }); + }); + $('#edit-example-number').change(function() { + $.get('" . base_path() . "textbook_companion/ajax/example_exists/' + $('#edit-number').val() + '/' + $('#edit-example-number').val(), function(data) { + if (data) { + alert(data); + } + }); + }); + $('#edit-existing-depfile-dep-book-title').change(function() { + var dep_selected = ''; + /* showing and hiding relevant files */ + $('.form-checkboxes .option').hide(); + $('.form-checkboxes .option').each(function(index) { + var activeClass = $('#edit-existing-depfile-dep-book-title').val(); + if ($(this).children().hasClass(activeClass)) { + $(this).show(); + } + if ($(this).children().attr('checked') == true) { + dep_selected += $(this).children().next().text() + '<br />'; + } + }); + /* showing list of already existing dependencies */ + $('#existing_depfile_selected').html(dep_selected); + }); + + $('.form-checkboxes .option').change(function() { + $('#edit-existing-depfile-dep-book-title').trigger('change'); + }); + $('#edit-existing-depfile-dep-book-title').trigger('change'); + });"; + drupal_add_js($chapter_name_js, 'inline', 'header'); +} + +function upload_examples_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); + if (!$example_q) + { + drupal_set_message(t("Invalid example selected."), 'error'); + drupal_goto(''); + return; + } + if ($example_data->approval_status != 0) + { + drupal_set_message(t("You cannot edit an example after it has been approved or dis-approved. Please contact site administrator if you want to edit this example."), '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); + 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); + if (!$preference_data) + { + drupal_set_message(t("Invalid book selected."), 'error'); + drupal_goto(''); + return; + } + if ($preference_data->approval_status != 1) + { + drupal_set_message(t("Cannot edit example. Either the book proposal has not been approved or it has been rejected."), '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); + if (!$proposal_data) + { + drupal_set_message(t("Invalid proposal selected."), 'error'); + drupal_goto(''); + return; + } + if ($proposal_data->uid != $user->uid) + { + drupal_set_message(t("You do not have permissions to edit this example."), 'error'); + drupal_goto(''); + return; + } + + /* creating directories */ + $root_path = textbook_companion_path(); + + $dest_path = $preference_data->id . '/'; + 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); + + /* updating example caption */ + db_query("UPDATE {textbook_companion_example} SET caption = '%s' WHERE id = %d", $form_state['values']['example_caption'], $example_id); + + /* handling dependencies + db_query("DELETE FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_data->id); + foreach ($form_state['values']['existing_depfile']['dep_chapter_example_files'] as $row) + { + if ($row > 0) + { + // insterting into database + db_query("INSERT INTO {textbook_companion_example_dependency} (example_id, dependency_id, approval_status, timestamp) + VALUES (%d, %d, %d, %d)", + $example_data->id, + $row, + 0, + time() + ); + } + } */ + + /* handle source file */ + $cur_file_id = $form_state['values']['cur_source_file_id']; + 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); + 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 ($_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; + } + } + if (file_exists($root_path . $dest_path . $_FILES['files']['name']['sourcefile1'])) + { + drupal_set_message(t("Error uploading source file. File !filename already exists.", array('!filename' => $_FILES['files']['name']['sourcefile1'])), 'error'); + return; + } + /* uploading file */ + if (move_uploaded_file($_FILES['files']['tmp_name']['sourcefile1'], $root_path . $dest_path . $_FILES['files']['name']['sourcefile1'])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_data->id, + $_FILES['files']['name']['sourcefile1'], + $dest_path . $_FILES['files']['name']['sourcefile1'], + $_FILES['files']['type']['sourcefile1'], + $_FILES['files']['size']['sourcefile1'], + 'S', + time() + ); + drupal_set_message($_FILES['files']['name']['sourcefile1'] . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $_FILES['files']['name']['sourcefile1'], 'error'); + } + } + + /* handle result1 file */ + $cur_file_id = $form_state['values']['cur_result1_file_id']; + 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); + if (!$file_data) + { + drupal_set_message("Error deleting example result 1 file. File not present in database.", 'error'); + return; + } + if (($form_state['values']['cur_result1_checkbox'] == 1) && (!$_FILES['files']['name']['result1'])) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error deleting example result 1 file.", 'error'); + return; + } + } + } + if ($_FILES['files']['name']['result1']) + { + if ($cur_file_id > 0) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error removing previous example result 1 file.", 'error'); + return; + } + } + if (file_exists($root_path . $dest_path . $_FILES['files']['name']['result1'])) + { + drupal_set_message(t("Error uploading result 1 file. File !filename already exists.", array('!filename' => $_FILES['files']['name']['result1'])), 'error'); + return; + } + /* uploading file */ + if (move_uploaded_file($_FILES['files']['tmp_name']['result1'], $root_path . $dest_path . $_FILES['files']['name']['result1'])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_data->id, + $_FILES['files']['name']['result1'], + $dest_path . $_FILES['files']['name']['result1'], + $_FILES['files']['type']['result1'], + $_FILES['files']['size']['result1'], + 'R', + time() + ); + drupal_set_message($_FILES['files']['name']['result1'] . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $_FILES['files']['name']['result1'], 'error'); + } + } + + /* handle result2 file */ + $cur_file_id = $form_state['values']['cur_result2_file_id']; + 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); + if (!$file_data) + { + drupal_set_message("Error deleting example result 2 file. File not present in database.", 'error'); + return; + } + if (($form_state['values']['cur_result2_checkbox'] == 1) && (!$_FILES['files']['name']['result2'])) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error deleting example result 2 file.", 'error'); + return; + } + } + } + if ($_FILES['files']['name']['result2']) + { + if ($cur_file_id > 0) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error removing previous example result 2 file.", 'error'); + return; + } + } + if (file_exists($root_path . $dest_path . $_FILES['files']['name']['result2'])) + { + drupal_set_message(t("Error uploading result 2 file. File !filename already exists.", array('!filename' => $_FILES['files']['name']['result2'])), 'error'); + return; + } + /* uploading file */ + if (move_uploaded_file($_FILES['files']['tmp_name']['result2'], $root_path . $dest_path . $_FILES['files']['name']['result2'])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_data->id, + $_FILES['files']['name']['result2'], + $dest_path . $_FILES['files']['name']['result2'], + $_FILES['files']['type']['result2'], + $_FILES['files']['size']['result2'], + 'R', + time() + ); + drupal_set_message($_FILES['files']['name']['result2'] . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $_FILES['files']['name']['result2'], 'error'); + } + } + + /* handle xcos1 file */ + $cur_file_id = $form_state['values']['cur_xcos1_file_id']; + 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); + if (!$file_data) + { + drupal_set_message("Error deleting example xcos 1 file. File not present in database.", 'error'); + return; + } + if (($form_state['values']['cur_xcos1_checkbox'] == 1) && (!$_FILES['files']['name']['xcos1'])) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error deleting example xcos 1 file.", 'error'); + return; + } + } + } + if ($_FILES['files']['name']['xcos1']) + { + if ($cur_file_id > 0) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error removing previous example xcos 1 file.", 'error'); + return; + } + } + if (file_exists($root_path . $dest_path . $_FILES['files']['name']['xcos1'])) + { + drupal_set_message(t("Error uploading xcos 1 file. File !filename already exists.", array('!filename' => $_FILES['files']['name']['xcos1'])), 'error'); + return; + } + /* uploading file */ + if (move_uploaded_file($_FILES['files']['tmp_name']['xcos1'], $root_path . $dest_path . $_FILES['files']['name']['xcos1'])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_data->id, + $_FILES['files']['name']['xcos1'], + $dest_path . $_FILES['files']['name']['xcos1'], + $_FILES['files']['type']['xcos1'], + $_FILES['files']['size']['xcos1'], + 'X', + time() + ); + drupal_set_message($_FILES['files']['name']['xcos1'] . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $_FILES['files']['name']['xcos1'], 'error'); + } + } + + /* handle xcos2 file */ + $cur_file_id = $form_state['values']['cur_xcos2_file_id']; + 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); + if (!$file_data) + { + drupal_set_message("Error deleting example xcos 2 file. File not present in database.", 'error'); + return; + } + if (($form_state['values']['cur_xcos2_checkbox'] == 1) && (!$_FILES['files']['name']['xcos2'])) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error deleting example xcos 2 file.", 'error'); + return; + } + } + } + if ($_FILES['files']['name']['xcos2']) + { + if ($cur_file_id > 0) + { + if (!delete_file($cur_file_id)) + { + drupal_set_message("Error removing previous example xcos 2 file.", 'error'); + return; + } + } + if (file_exists($root_path . $dest_path . $_FILES['files']['name']['xcos2'])) + { + drupal_set_message(t("Error uploading xcos 2 file. File !filename already exists.", array('!filename' => $_FILES['files']['name']['xcos2'])), 'error'); + return; + } + /* uploading file */ + if (move_uploaded_file($_FILES['files']['tmp_name']['xcos2'], $root_path . $dest_path . $_FILES['files']['name']['xcos2'])) + { + /* for uploaded files making an entry in the database */ + db_query("INSERT INTO {textbook_companion_example_files} (example_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $example_data->id, + $_FILES['files']['name']['xcos2'], + $dest_path . $_FILES['files']['name']['xcos2'], + $_FILES['files']['type']['xcos2'], + $_FILES['files']['size']['xcos2'], + 'X', + time() + ); + drupal_set_message($_FILES['files']['name']['xcos2'] . ' uploaded successfully.', 'status'); + } else { + drupal_set_message('Error uploading file : ' . $dest_path . '/' . $_FILES['files']['name']['xcos2'], 'error'); + } + } + + /* sending email */ + $email_to = $user->mail; + $param['example_updated']['example_id'] = $example_id; + $param['example_updated']['user_id'] = $user->uid; + if (!drupal_mail('textbook_companion', 'example_updated', $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'); +} + + +/******************************************************************************/ +/**************************** EDIT CHAPTER TITLE ******************************/ +/******************************************************************************/ + +function edit_chapter_title_form($form_state) +{ + 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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto('textbook_companion/code'); + } + if ($proposal_data->proposal_status != 1) + { + 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('textbook_companion/code'); + return; + break; + case 2: + drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'proposal') . '.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + break; + case 3: + drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'proposal') . '.'), 'status'); + drupal_goto('textbook_companion/code'); + return; + break; + default: + drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error'); + drupal_goto('textbook_companion/code'); + 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); + if (!$preference_data) + { + drupal_set_message(t('Invalid Book Preference status. Please contact site administrator for further information.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + } + /************************ end approve book details **************************/ + + $chapter_id = arg(4); + + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d AND preference_id = %d", $chapter_id, $preference_data->id); + $chapter_data = db_fetch_object($chapter_q); + if (!$chapter_data) + { + drupal_set_message(t('Invalid chapter.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + } + + $form['#redirect'] = 'textbook_companion/code'; + + $form['book_details']['book'] = array( + '#type' => 'item', + '#value' => $preference_data->book, + '#title' => t('Title of the Book'), + ); + $form['contributor_name'] = array( + '#type' => 'item', + '#value' => $proposal_data->full_name, + '#title' => t('Contributor Name'), + ); + $form['number'] = array( + '#type' => 'item', + '#title' => t('Chapter No'), + '#value' => $chapter_data->number, + ); + + $form['chapter_title'] = array( + '#type' => 'textfield', + '#title' => t('Title of the Chapter'), + '#size' => 40, + '#maxlength' => 255, + '#required' => TRUE, + '#default_value' => $chapter_data->name, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'textbook_companion/code'), + ); + return $form; +} + +function edit_chapter_title_form_validate($form, &$form_state) +{ + if (!check_name($form_state['values']['chapter_title'])) + form_set_error('chapter_title', t('Title of the Chapter can contain only alphabets, numbers and spaces.')); +} + +function edit_chapter_title_form_submit($form, &$form_state) +{ + 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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto('textbook_companion/code'); + } + if ($proposal_data->proposal_status != 1) + { + 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('textbook_companion/code'); + return; + break; + case 2: + drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'proposal') . '.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + break; + case 3: + drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'proposal') . '.'), 'status'); + drupal_goto('textbook_companion/code'); + return; + break; + default: + drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error'); + drupal_goto('textbook_companion/code'); + 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); + if (!$preference_data) + { + drupal_set_message(t('Invalid Book Preference status. Please contact site administrator for further information.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + } + /************************ end approve book details **************************/ + + $chapter_id = arg(4); + + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d AND preference_id = %d", $chapter_id, $preference_data->id); + $chapter_data = db_fetch_object($chapter_q); + if (!$chapter_data) + { + drupal_set_message(t('Invalid chapter.'), 'error'); + drupal_goto('textbook_companion/code'); + return; + } + db_query("UPDATE {textbook_companion_chapter} SET name = '%s' WHERE id = %d", $form_state['values']['chapter_title'], $chapter_id); + drupal_set_message(t('Title of the Chapter updated.'), '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"); + while ($book_titles_data = db_fetch_object($book_titles_q)) + { + $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"); + + while ($book_dependency_files_data = db_fetch_object($book_dependency_files_q)) + { + $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, '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); +} + diff --git a/full_download.inc b/full_download.inc new file mode 100755 index 0000000..e51599a --- /dev/null +++ b/full_download.inc @@ -0,0 +1,143 @@ +<?php +// $Id$ + +function textbook_companion_download_full_chapter() +{ + $chapter_id = arg(2); + $root_path = textbook_companion_path(); + $APPROVE_PATH = 'APPROVED/'; + $PENDING_PATH = 'PENDING/'; + + /* get example data */ + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id); + $chapter_data = db_fetch_object($chapter_q); + $CH_PATH = 'CH' . $chapter_data->number . '/'; + + /* zip filename */ + $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip'; + + /* creating zip archive on the server */ + $zip = new ZipArchive; + $zip->open($zip_filename, ZipArchive::CREATE); + + /* approved examples */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $APPROVE_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename); + } + } + + /* unapproved examples */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 0", $chapter_id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $PENDING_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename); + } + } + + $zip_file_count = $zip->numFiles; + $zip->close(); + + if ($zip_file_count > 0) + { + /* download zip file */ + header('Content-Type: application/zip'); + header('Content-disposition: attachment; filename="CH' . $chapter_data->number . '.zip"'); + header('Content-Length: ' . filesize($zip_filename)); + readfile($zip_filename); + unlink($zip_filename); + } else { + drupal_set_message("There are no examples in this chapter to download", 'error'); + drupal_goto('code_approval/bulk'); + } +} + +function textbook_companion_download_full_book() +{ + $book_id = arg(2); + $root_path = textbook_companion_path(); + $APPROVE_PATH = 'APPROVED/'; + $PENDING_PATH = 'PENDING/'; + /* get example data */ + $book_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $book_id); + $book_data = db_fetch_object($book_q); + $BK_PATH = $book_data->book . '/'; + + /* zip filename */ + $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip'; + + /* creating zip archive on the server */ + $zip = new ZipArchive; + $zip->open($zip_filename, ZipArchive::CREATE); + + /* approved examples */ + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $book_id); + while ($chapter_row = db_fetch_object($chapter_q)) + { + $CH_PATH = 'CH' . $chapter_row->number . '/'; + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_row->id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + $example_dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $BK_PATH . $APPROVE_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename); + } + /* dependency files */ + while ($example_dependency_files_row = db_fetch_object($example_dependency_files_q)) + { + $dependency_file_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $example_dependency_files_row->dependency_id)); + if ($dependency_file_data) + $zip->addFile($root_path . $dependency_file_data->filepath, $BK_PATH . $APPROVE_PATH . $CH_PATH . $EX_PATH . 'DEPENDENCIES/' . $dependency_file_data->filename); + } + } + + /* unapproved examples */ + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 0", $chapter_row->id); + while ($example_row = db_fetch_object($example_q)) + { + $EX_PATH = 'EX' . $example_row->number . '/'; + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id); + $example_dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_row->id); + while ($example_files_row = db_fetch_object($example_files_q)) + { + $zip->addFile($root_path . $example_files_row->filepath, $BK_PATH . $PENDING_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename); + } + /* dependency files */ + while ($example_dependency_files_row = db_fetch_object($example_dependency_files_q)) + { + $dependency_file_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $example_dependency_files_row->dependency_id)); + if ($dependency_file_data) + $zip->addFile($root_path . $dependency_file_data->filepath, $BK_PATH . $PENDING_PATH . $CH_PATH . $EX_PATH . 'DEPENDENCIES/' . $dependency_file_data->filename); + } + } + } + + $zip_file_count = $zip->numFiles; + $zip->close(); + + if ($zip_file_count > 0) + { + /* download zip file */ + header('Content-Type: application/zip'); + header('Content-disposition: attachment; filename="' . $book_data->book . '.zip"'); + header('Content-Length: ' . filesize($zip_filename)); + readfile($zip_filename); + unlink($zip_filename); + } else { + drupal_set_message("There are no examples in this book to download", 'error'); + drupal_goto('code_approval/bulk'); + } +} + diff --git a/general.inc b/general.inc new file mode 100755 index 0000000..2e36eae --- /dev/null +++ b/general.inc @@ -0,0 +1,192 @@ +<?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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto(''); + } + if ($proposal_data->proposal_status != 1) + { + 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', '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', '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); + 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); + while ($chapter_data = db_fetch_object($chapter_q)) + { + /* 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); + $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($chapter_header, $chapter_rows); + 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); + if (!$proposal_data) + { + drupal_set_message("Please submit a " . l('proposal', 'proposal') . ".", 'error'); + drupal_goto(''); + } + if ($proposal_data->proposal_status != 1) + { + 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', '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', '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); + 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); + if ($chapter_data = db_fetch_object($chapter_q)) + { + $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(); + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d ORDER BY + CAST(SUBSTRING_INDEX(number, '.', 1) AS BINARY) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', 2), '.', -1) AS UNSIGNED) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', -1), '.', 1) AS UNSIGNED) ASC", $chapter_id); + while ($example_data = db_fetch_object($example_q)) + { + /* 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); + while ($example_files_data = db_fetch_object($example_files_q)) + { + $file_type = ''; + switch ($example_files_data->filetype) + { + case 'S': $file_type = 'Main or Source'; break; + case 'R': $file_type = 'Result'; break; + case 'X': $file_type = 'xcos'; break; + default: + } + $example_files .= l($example_files_data->filename, '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', 'download/example/' . $example_data->id)), 'valign' => 'top'); + } + } + + $example_header = array('Example No.', 'Caption', 'Status', 'Files', 'Action'); + $return_html .= theme_table($example_header, $example_rows); + return $return_html; +} diff --git a/manage_proposal.inc b/manage_proposal.inc new file mode 100755 index 0000000..ce4ca2b --- /dev/null +++ b/manage_proposal.inc @@ -0,0 +1,982 @@ +<?php +// $Id$ + +function _proposal_pending() +{ + /* get pending proposals to be approved */ + $pending_rows = array(); + $pending_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE proposal_status = 0 ORDER BY id DESC"); + while ($pending_data = db_fetch_object($pending_q)) + { + $pending_rows[$pending_data->id] = array(date('d-m-Y', $pending_data->creation_date), l($pending_data->full_name, 'user/' . $pending_data->uid), date('d-m-Y', $pending_data->completion_date), l('Approve', 'manage_proposal/approve/' . $pending_data->id) . ' | ' . l('Edit', 'manage_proposal/edit/' . $pending_data->id)); + } + + /* check if there are any pending proposals */ + if (!$pending_rows) + { + drupal_set_message(t('There are no pending proposals.'), 'status'); + return ''; + } + + $pending_header = array('Date of Submission', 'Contributor Name', 'Date of Completion', 'Action'); + $output = theme_table($pending_header, $pending_rows); + return $output; +} + +function _proposal_all() +{ + /* get pending proposals to be approved */ + $proposal_rows = array(); + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} ORDER BY id DESC"); + while ($proposal_data = db_fetch_object($proposal_q)) + { + /* get preference */ + $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); + + $proposal_status = ''; + switch ($proposal_data->proposal_status) + { + case 0: $proposal_status = 'Pending'; break; + case 1: $proposal_status = 'Approved'; break; + case 2: $proposal_status = 'Dis-approved'; break; + case 3: $proposal_status = 'Completed'; break; + default: $proposal_status = 'Unknown'; break; + } + $proposal_rows[] = array(date('d-m-Y', $proposal_data->creation_date), $preference_data->book, $preference_data->author, l($proposal_data->full_name, 'user/' . $proposal_data->uid), date('d-m-Y', $proposal_data->completion_date), $proposal_status, l('Status', 'manage_proposal/status/' . $proposal_data->id) . ' | ' . l('Edit', 'manage_proposal/edit/' . $proposal_data->id)); + } + + /* check if there are any pending proposals */ + if (!$proposal_rows) + { + drupal_set_message(t('There are no proposals.'), 'status'); + return ''; + } + + $proposal_header = array('Date of Submission', 'Title of the Book', 'Author', 'Contributor Name', 'Expected Date of Completion', 'Status'); + $output = theme_table($proposal_header, $proposal_rows); + return $output; +} + +function _category_all() +{ + /* get pending proposals to be approved */ + $preference_rows = array(); + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE approval_status = 1 ORDER BY id DESC"); + while ($preference_data = db_fetch_object($preference_q)) + { + switch ($preference_data->category) + { + case 0: $category_data = 'Not Selected'; break; + case 1: $category_data = 'Fluid Mechanics'; break; + case 2: $category_data = 'Control Theory & Control Systems'; break; + case 3: $category_data = 'Chemical Engineering'; break; + case 4: $category_data = 'Thermodynamics'; break; + case 5: $category_data = 'Mechanical Engineering'; break; + case 6: $category_data = 'Signal Processing'; break; + case 7: $category_data = 'Digital Communications'; break; + case 8: $category_data = 'Electrical Technology'; break; + case 9: $category_data = 'Mathematics & Pure Science'; break; + case 10: $category_data = 'Analog Electronics'; break; + case 11: $category_data = 'Digital Electronics'; break; + case 12: $category_data = 'Computer Programming'; break; + case 13: $category_data = 'Others'; break; + default: $category_data = 'Unknown'; break; + } + $preference_rows[] = array($preference_data->book, $preference_data->author, $preference_data->isbn, $preference_data->publisher, $preference_data->edition, $preference_data->year, $category_data, l('Edit', 'manage_proposal/category/edit/' . $preference_data->id)); + } + + $preference_header = array('Book', 'Author', 'ISBN', 'Publisher', 'Edition', 'Year', 'Category', 'Status'); + $output = theme_table($preference_header, $preference_rows); + return $output; +} + +/******************************************************************************/ +/************************** PROPOSAL APPROVAL FORM ****************************/ +/******************************************************************************/ + +function proposal_approval_form($form_state) +{ + global $user; + + /* get current proposal */ + $proposal_id = arg(2); + $result = db_query("SELECT * FROM {textbook_companion_proposal} WHERE proposal_status = 0 and id = %d", $proposal_id); + if ($result) + { + if ($row = db_fetch_object($result)) + { + /* everything ok */ + } else { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + } else { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + + $form['full_name'] = array( + '#type' => 'item', + '#value' => l($row->full_name, 'user/' . $row->uid), + '#title' => t('Contributor Name'), + ); + $form['email'] = array( + '#type' => 'item', + '#value' => user_load($row->uid)->mail, + '#title' => t('Email'), + ); + $form['mobile'] = array( + '#type' => 'item', + '#value' => $row->mobile, + '#title' => t('Mobile'), + ); + $form['how_project'] = array( + '#type' => 'item', + '#value' => $row->how_project, + '#title' => t('How did you come to know about this project'), + ); + $form['course'] = array( + '#type' => 'item', + '#value' => $row->course, + '#title' => t('Course'), + ); + $form['branch'] = array( + '#type' => 'item', + '#value' => $row->branch, + '#title' => t('Department/Branch'), + ); + $form['university'] = array( + '#type' => 'item', + '#value' => $row->university, + '#title' => t('University/Institute'), + ); + $form['faculty'] = array( + '#type' => 'item', + '#value' => $row->faculty, + '#title' => t('College Teacher/Professor'), + ); + $form['reviewer'] = array( + '#type' => 'item', + '#value' => $row->reviewer, + '#title' => t('Reviewer'), + ); + $form['completion_date'] = array( + '#type' => 'item', + '#value' => date('d-m-Y', $row->completion_date), + '#title' => t('Expected Date of Completion'), + ); + + /* get book preference */ + $preference_rows = array(); + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d ORDER BY pref_number ASC", $proposal_id); + while ($preference_data = db_fetch_object($preference_q)) + { + $preference_rows[$preference_data->id] = $preference_data->book . ' (Written by ' . $preference_data->author . ')'; + } + + $form['book_preference'] = array( + '#type' => 'radios', + '#title' => t('Book Preferences'), + '#options' => $preference_rows, + ); + + $form['disapprove'] = array( + '#type' => 'checkbox', + '#title' => t('Disapprove all the above book preferences'), + ); + + $form['message'] = array( + '#type' => 'textarea', + '#title' => t('Reason for disapproval'), + ); + + $form['proposal_id'] = array( + '#type' => 'hidden', + '#value' => $proposal_id, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'manage_proposal'), + ); + return $form; +} + +function proposal_approval_form_submit($form, &$form_state) +{ + global $user; + + /* get current proposal */ + $proposal_id = $form_state['values']['proposal_id']; + $result = db_query("SELECT * FROM {textbook_companion_proposal} WHERE proposal_status = 0 and id = %d", $proposal_id); + if ($result) + { + if ($row = db_fetch_object($result)) + { + /* everything ok */ + } else { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + } else { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + + /* disapprove */ + if ($form_state['values']['disapprove']) + { + db_query("UPDATE {textbook_companion_proposal} SET approver_uid = %d, approval_date = %d, proposal_status = 2, message = '%s' WHERE id = %d", $user->uid, time(), $form_state['values']['message'], $proposal_id); + db_query("UPDATE {textbook_companion_preference} SET approval_status = 2 WHERE proposal_id = %d", $proposal_id); + + /* sending email */ + $book_user = user_load($row->uid); + $param['proposal_disapproved']['proposal_id'] = $proposal_id; + $param['proposal_disapproved']['user_id'] = $row->uid; + $email_to = $book_user->mail; + if (!drupal_mail('textbook_companion', 'proposal_disapproved', $email_to , language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + drupal_set_message('Book proposal dis-approved. User has been notified of the dis-approval.', 'error'); + drupal_goto('manage_proposal'); + return; + } + + /* get book preference and set the status */ + $preference_id = $form_state['values']['book_preference']; + db_query("UPDATE {textbook_companion_proposal} SET approver_uid = %d, approval_date = %d, proposal_status = 1 WHERE id = %d", $user->uid, time(), $proposal_id); + db_query("UPDATE {textbook_companion_preference} SET approval_status = 1 WHERE id = %d", $preference_id); + + /* sending email */ + $book_user = user_load($row->uid); + $param['proposal_approved']['proposal_id'] = $proposal_id; + $param['proposal_approved']['user_id'] = $row->uid; + $email_to = $book_user->mail; + if (!drupal_mail('textbook_companion', 'proposal_approved', $email_to , language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + drupal_set_message('Book proposal approved. User has been notified of the approval', 'status'); + drupal_goto('manage_proposal'); + return; +} + + +/******************************************************************************/ +/*************************** PROPOSAL STATUS FORM *****************************/ +/******************************************************************************/ + +function proposal_status_form($form_state) +{ + global $user; + + /* get current proposal */ + $proposal_id = arg(2); + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $proposal_id); + if (!$proposal_data = db_fetch_object($proposal_q)) + { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + + $form['full_name'] = array( + '#type' => 'item', + '#value' => $proposal_data->full_name, + '#title' => t('Contributor Name'), + ); + $form['email'] = array( + '#type' => 'item', + '#value' => user_load($proposal_data->uid)->mail, + '#title' => t('Email'), + ); + $form['mobile'] = array( + '#type' => 'item', + '#value' => $proposal_data->mobile, + '#title' => t('Mobile'), + ); + $form['how_project'] = array( + '#type' => 'item', + '#value' => $proposal_data->how_project, + '#title' => t('How did you come to know about this project'), + ); + $form['course'] = array( + '#type' => 'item', + '#value' => $proposal_data->course, + '#title' => t('Course'), + ); + $form['branch'] = array( + '#type' => 'item', + '#value' => $proposal_data->branch, + '#title' => t('Department/Branch'), + ); + $form['university'] = array( + '#type' => 'item', + '#value' => $proposal_data->university, + '#title' => t('University/Institute'), + ); + $form['faculty'] = array( + '#type' => 'item', + '#value' => $proposal_data->faculty, + '#title' => t('College Teacher/Professor'), + ); + $form['reviewer'] = array( + '#type' => 'item', + '#value' => $proposal_data->reviewer, + '#title' => t('Reviewer'), + ); + $form['completion_date'] = array( + '#type' => 'item', + '#value' => date('d-m-Y', $proposal_data->completion_date), + '#title' => t('Expected Date of Completion'), + ); + + /* get book preference */ + $preference_html = '<ul>'; + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d ORDER BY pref_number ASC", $proposal_id); + while ($preference_data = db_fetch_object($preference_q)) + { + if ($preference_data->approval_status == 1) + $preference_html .= '<li><strong>' . $preference_data->book . ' (Written by ' . $preference_data->author . ') - Approved Book</strong></li>'; + else + $preference_html .= '<li>' . $preference_data->book . ' (Written by ' . $preference_data->author . ')</li>'; + } + $preference_html .= '</ul>'; + + $form['book_preference'] = array( + '#type' => 'item', + '#value' => $preference_html, + '#title' => t('Book Preferences'), + ); + + $proposal_status = ''; + switch ($proposal_data->proposal_status) + { + case 0: $proposal_status = t('Pending'); break; + case 1: $proposal_status = t('Approved'); break; + case 2: $proposal_status = t('Dis-approved'); break; + case 3: $proposal_status = t('Completed'); break; + default: $proposal_status = t('Unkown'); break; + } + $form['proposal_status'] = array( + '#type' => 'item', + '#value' => $proposal_status, + '#title' => t('Proposal Status'), + ); + + if ($proposal_data->proposal_status == 2) { + $form['message'] = array( + '#type' => 'item', + '#value' => $proposal_data->message, + '#title' => t('Reason for disapproval'), + ); + } + + if ($proposal_data->proposal_status == 1) + { + $form['completed'] = array( + '#type' => 'checkbox', + '#title' => t('Completed'), + '#description' => t('Check if user has completed all the book examples.'), + ); + } + + if ($proposal_data->proposal_status == 0) + { + $form['approve'] = array( + '#type' => 'item', + '#value' => l('Click here', 'manage_proposal/approve/' . $proposal_id), + '#title' => t('Approve'), + ); + } + + $form['proposal_id'] = array( + '#type' => 'hidden', + '#value' => $proposal_id, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'manage_proposal/all'), + ); + return $form; +} + +function proposal_status_form_submit($form, &$form_state) +{ + global $user; + + /* get current proposal */ + $proposal_id = $form_state['values']['proposal_id']; + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $proposal_id); + if (!$proposal_data = db_fetch_object($proposal_q)) + { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + + /* set the book status to completed */ + if ($form_state['values']['completed'] == 1) + { + db_query("UPDATE {textbook_companion_proposal} SET proposal_status = 3 WHERE id = %d", $proposal_id); + + /* sending email */ + $book_user = user_load($proposal_data->uid); + $param['proposal_completed']['proposal_id'] = $proposal_id; + $param['proposal_completed']['user_id'] = $proposal_data->uid; + $email_to = $book_user->mail; + if (!drupal_mail('textbook_companion', 'proposal_completed', $email_to , language_default(), $param, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + drupal_set_message('Congratulations! Book proposal has been marked as completed. User has been notified of the completion.', 'status'); + } + drupal_goto('manage_proposal'); + return; +} + + +/******************************************************************************/ +/**************************** PROPOSAL EDIT FORM ******************************/ +/******************************************************************************/ + +function proposal_edit_form($form_state) +{ + global $user; + + /* get current proposal */ + $proposal_id = arg(2); + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $proposal_id); + if ($proposal_q) + { + $proposal_data = db_fetch_object($proposal_q); + if (!$proposal_data) + { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + } else { + drupal_set_message(t('Invalid proposal selected. Please try again.'), 'error'); + drupal_goto('manage_proposal'); + return; + } + + $user_data = user_load($proposal_data->uid); + + $preference1_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $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", $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", $proposal_id, 3); + $preference3_data = db_fetch_object($preference3_q); + + $form['full_name'] = array( + '#type' => 'textfield', + '#title' => t('Full Name'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + '#default_value' => $proposal_data->full_name, + ); + $form['email_id'] = array( + '#type' => 'textfield', + '#title' => t('Email'), + '#size' => 30, + '#value' => $user_data->mail, + '#disabled' => TRUE, + ); + $form['mobile'] = array( + '#type' => 'textfield', + '#title' => t('Mobile No.'), + '#size' => 30, + '#maxlength' => 15, + '#required' => TRUE, + '#default_value' => $proposal_data->mobile, + ); + $form['how_project'] = array( + '#type' => 'select', + '#title' => t('How did you come to know about this project'), + '#options' => array('Oscad Website' => 'Oscad Website', + 'Friend' => 'Friend', + 'Professor/Teacher' => 'Professor/Teacher', + 'Mailing List' => 'Mailing List', + 'Poster in my/other college' => 'Poster in my/other college', + 'Others' => 'Others'), + '#required' => TRUE, + '#default_value' => $proposal_data->how_project, + ); + $form['course'] = array( + '#type' => 'textfield', + '#title' => t('Course'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + '#default_value' => $proposal_data->course, + ); + $form['branch'] = array( + '#type' => 'select', + '#title' => t('Department/Branch'), + '#options' => array('Electrical Engineering' => 'Electrical Engineering', + 'Electronics Engineering' => 'Electronics Engineering', + 'Computer Engineering' => 'Computer Engineering', + 'Chemical Engineering' => 'Chemical Engineering', + 'Instrumentation Engineering' => 'Instrumentation Engineering', + 'Mechanical Engineering' => 'Mechanical Engineering', + 'Civil Engineering' => 'Civil Engineering', + 'Physics' => 'Physics', + 'Mathematics' => 'Mathematics', + 'Others' => 'Others'), + '#required' => TRUE, + '#default_value' => $proposal_data->branch, + ); + $form['university'] = array( + '#type' => 'textfield', + '#title' => t('University/Institute'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $proposal_data->university, + ); + $form['faculty'] = array( + '#type' => 'textfield', + '#title' => t('College Teacher/Professor'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $proposal_data->faculty, + ); + $form['reviewer'] = array( + '#type' => 'textfield', + '#title' => t('Reviewer'), + '#size' => 30, + '#maxlength' => 100, + '#default_value' => $proposal_data->reviewer, + ); + $form['completion_date'] = array( + '#type' => 'textfield', + '#title' => t('Expected Date of Completion'), + '#description' => t('Input date format should be DD-MM-YYYY. Eg: 23-03-2011'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => date('d-m-Y', $proposal_data->completion_date), + ); + $form['preference1'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 1'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference1']['book1'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference1_data->book, + ); + $form['preference1']['author1'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference1_data->author, + ); + $form['preference1']['isbn1'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + '#default_value' => $preference1_data->isbn, + ); + $form['preference1']['publisher1'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + '#default_value' => $preference1_data->publisher, + ); + $form['preference1']['edition1'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + '#default_value' => $preference1_data->edition, + ); + $form['preference1']['year1'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + '#default_value' => $preference1_data->year, + ); + $form['preference2'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 2'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference2']['book2'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference2_data->book, + ); + $form['preference2']['author2'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference2_data->author, + ); + $form['preference2']['isbn2'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + '#default_value' => $preference2_data->isbn, + ); + $form['preference2']['publisher2'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + '#default_value' => $preference2_data->publisher, + ); + $form['preference2']['edition2'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + '#default_value' => $preference2_data->edition, + ); + $form['preference2']['year2'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + '#default_value' => $preference2_data->year, + ); + $form['preference3'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 3'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference3']['book3'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference3_data->book, + ); + $form['preference3']['author3'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $preference3_data->author, + ); + $form['preference3']['isbn3'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + '#default_value' => $preference3_data->isbn, + ); + $form['preference3']['publisher3'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + '#default_value' => $preference3_data->publisher, + ); + $form['preference3']['edition3'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + '#default_value' => $preference3_data->edition, + ); + $form['preference3']['year3'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + '#default_value' => $preference3_data->year, + ); + + /* hidden fields */ + $form['hidden_proposal_id'] = array( + '#type' => 'hidden', + '#value' => $proposal_id, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'manage_proposal'), + ); + return $form; +} + +function proposal_edit_form_validate($form, &$form_state) +{ + /* mobile */ + if (!preg_match('/^[0-9\ \+]{0,15}$/', $form_state['values']['mobile'])) + form_set_error('mobile', t('Invalid mobile number')); + + /* date of completion */ + if (!preg_match('/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/', $form_state['values']['completion_date'])) + form_set_error('completion_date', t('Invalid expected date of completion')); + + list($d, $m, $y) = explode('-', $form_state['values']['completion_date']); + $d = (int)$d; $m = (int)$m; $y = (int)$y; + if (!checkdate($m, $d, $y)) + form_set_error('completion_date', t('Invalid expected date of completion')); + if (mktime(0, 0, 0, $m, $d, $y) <= time()) + form_set_error('completion_date', t('Expected date of completion should be in future')); + + /* edition */ + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition1'])) + form_set_error('edition1', t('Invalid edition for Book Preference 1')); + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition2'])) + form_set_error('edition2', t('Invalid edition for Book Preference 2')); + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition3'])) + form_set_error('edition3', t('Invalid edition for Book Preference 3')); + + /* year of publication */ + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year1'])) + form_set_error('year1', t('Invalid year of pulication for Book Preference 1')); + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year2'])) + form_set_error('year2', t('Invalid year of pulication for Book Preference 2')); + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year3'])) + form_set_error('year3', t('Invalid year of pulication for Book Preference 3')); + + /* year of publication */ + $cur_year = date('Y'); + if ((int)$form_state['values']['year1'] > $cur_year) + form_set_error('year1', t('Year of pulication should be not in the future for Book Preference 1')); + if ((int)$form_state['values']['year2'] > $cur_year) + form_set_error('year2', t('Year of pulication should be not in the future for Book Preference 2')); + if ((int)$form_state['values']['year3'] > $cur_year) + form_set_error('year3', t('Year of pulication should be not in the future for Book Preference 3')); + + /* isbn */ + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn1'])) + form_set_error('isbn1', t('Invalid ISBN for Book Preference 1')); + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn2'])) + form_set_error('isbn2', t('Invalid ISBN for Book Preference 2')); + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn3'])) + form_set_error('isbn3', t('Invalid ISBN for Book Preference 3')); + return; +} + +function proposal_edit_form_submit($form, &$form_state) +{ + /* completion date to timestamp */ + list($d, $m, $y) = explode('-', $form_state['values']['completion_date']); + $completion_date_timestamp = mktime(0, 0, 0, $m, $d, $y); + + $proposal_id = $form_state['values']['hidden_proposal_id']; + + db_query("UPDATE {textbook_companion_proposal} SET full_name = '%s', mobile = '%s', how_project = '%s', course = '%s', branch = '%s', university = '%s', faculty = '%s', reviewer = '%s', completion_date = %d WHERE id = %d", + $form_state['values']['full_name'], + $form_state['values']['mobile'], + $form_state['values']['how_project'], + $form_state['values']['course'], + $form_state['values']['branch'], + $form_state['values']['university'], + $form_state['values']['faculty'], + $form_state['values']['reviewer'], + $completion_date_timestamp, + $proposal_id); + + $preference1_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $proposal_id, 1); + $preference1_data = db_fetch_object($preference1_q); + if ($preference1_data) + $preference1_id = $preference1_data->id; + $preference2_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $proposal_id, 2); + $preference2_data = db_fetch_object($preference2_q); + if ($preference2_data) + $preference2_id = $preference2_data->id; + $preference3_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE proposal_id = %d AND pref_number = %d LIMIT 1", $proposal_id, 3); + $preference3_data = db_fetch_object($preference3_q); + if ($preference3_data) + $preference3_id = $preference3_data->id; + + if ($preference1_data) + { + del_book_pdf($preference1_data->id); + db_query("UPDATE {textbook_companion_preference} SET book = '%s', author = '%s', isbn = '%s', publisher = '%s', edition = %d, year = %d WHERE id = %d", + $form_state['values']['book1'], + $form_state['values']['author1'], + $form_state['values']['isbn1'], + $form_state['values']['publisher1'], + $form_state['values']['edition1'], + $form_state['values']['year1'], + $preference1_id); + } + if ($preference2_data) + { + del_book_pdf($preference2_data->id); + db_query("UPDATE {textbook_companion_preference} SET book = '%s', author = '%s', isbn = '%s', publisher = '%s', edition = %d, year = %d WHERE id = %d", + $form_state['values']['book2'], + $form_state['values']['author2'], + $form_state['values']['isbn2'], + $form_state['values']['publisher2'], + $form_state['values']['edition2'], + $form_state['values']['year2'], + $preference2_id); + } + if ($preference3_data) + { + del_book_pdf($preference3_data->id); + db_query("UPDATE {textbook_companion_preference} SET book = '%s', author = '%s', isbn = '%s', publisher = '%s', edition = %d, year = %d WHERE id = %d", + $form_state['values']['book3'], + $form_state['values']['author3'], + $form_state['values']['isbn3'], + $form_state['values']['publisher3'], + $form_state['values']['edition3'], + $form_state['values']['year3'], + $preference3_id); + } + drupal_set_message(t('Proposal Updated'), 'status'); +} + + + +/******************************************************************************/ +/**************************** CATEGORY EDIT FORM ******************************/ +/******************************************************************************/ + +function category_edit_form($form_state) +{ + /* get current proposal */ + $preference_id = arg(3); + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $preference_id); + $preference_data = db_fetch_object($preference_q); + if (!$preference_data) + { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('manage_proposal/category'); + return; + } + + $form['book'] = array( + '#type' => 'item', + '#title' => t('Title of the book'), + '#value' => $preference_data->book, + ); + $form['author'] = array( + '#type' => 'item', + '#title' => t('Author Name'), + '#value' => $preference_data->author, + ); + $form['isbn'] = array( + '#type' => 'item', + '#title' => t('ISBN No'), + '#value' => $preference_data->isbn, + ); + $form['publisher'] = array( + '#type' => 'item', + '#title' => t('Publisher & Place'), + '#value' => $preference_data->publisher, + ); + $form['edition'] = array( + '#type' => 'item', + '#title' => t('Edition'), + '#value' => $preference_data->edition, + ); + $form['year'] = array( + '#type' => 'item', + '#title' => t('Year of pulication'), + '#value' => $preference_data->year, + ); + + $form['category'] = array( + '#type' => 'select', + '#title' => t('Category'), + '#options' => array(0 => 'Please select', + 1 => 'Fluid Mechanics', + 2 => 'Control Theory & Control Systems', + 3 => 'Chemical Engineering', + 4 => 'Thermodynamics', + 5 => 'Mechanical Engineering', + 6 => 'Signal Processing', + 7 => 'Digital Communications', + 8 => 'Electrical Technology', + 9 => 'Mathematics & Pure Science', + 10 => 'Analog Electronics', + 11 => 'Digital Electronics', + 12 => 'Computer Programming', + 13 => 'Others'), + '#required' => TRUE, + '#default_value' => $preference_data->category, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'manage_proposal/category'), + ); + return $form; +} + +function category_edit_form_submit($form, &$form_state) +{ + /* get current proposal */ + $preference_id = (int)arg(3); + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $preference_id); + $preference_data = db_fetch_object($preference_q); + if (!$preference_data) + { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('manage_proposal/category'); + return; + } + + db_query("UPDATE {textbook_companion_preference} SET category = %d WHERE id = %d", $form_state['values']['category'], $preference_data->id); + + drupal_set_message(t('Book Category Updated'), 'status'); + drupal_goto('manage_proposal/category'); +} + diff --git a/notes.inc b/notes.inc new file mode 100755 index 0000000..72fbd10 --- /dev/null +++ b/notes.inc @@ -0,0 +1,120 @@ +<?php +// $Id$ + +/******************************************************************************/ +/***************************** BOOK NOTES *************************************/ +/******************************************************************************/ + +function book_notes_form($form_state) +{ + global $user; + + /* get current proposal */ + $preference_id = arg(2); + $preference_id = (int)$preference_id; + $result = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $preference_id); + if ($result) + { + if ($row = db_fetch_object($result)) + { + /* everything ok */ + } else { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('code_approval/bulk'); + return; + } + } else { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('code_approval/bulk'); + return; + } + + /* get current notes */ + $notes = ''; + $notes_q = db_query("SELECT * FROM {textbook_companion_notes} WHERE preference_id = %d LIMIT 1", $preference_id); + if ($notes_q) + { + $notes_data = db_fetch_object($notes_q); + $notes = $notes_data->notes; + } + + $book_details = _book_information($preference_id); + $form['book_details'] = array( + '#type' => 'item', + '#value' => '<span style="color: rgb(128, 0, 0);"><strong>About the Book</strong></span><br />' . + '<strong>Author:</strong> ' . $book_details->preference_author . '<br />' . + '<strong>Title of the Book:</strong> ' . $book_details->preference_book . '<br />' . + '<strong>Publisher:</strong> ' . $book_details->preference_publisher . '<br />' . + '<strong>Year:</strong> ' . $book_details->preference_year . '<br />' . + '<strong>Edition:</strong> ' . $book_details->preference_edition . '<br /><br />' . + '<span style="color: rgb(128, 0, 0);"><strong>About the Contributor</strong></span><br />' . + '<strong>Contributor Name:</strong> ' . $book_details->proposal_full_name . ', ' . $book_details->proposal_course . ', ' . $book_details->proposal_branch . ', ' . $book_details->proposal_university . '<br />', + ); + + $form['notes'] = array( + '#type' => 'textarea', + '#rows' => 20, + '#title' => t('Notes for Reviewers'), + '#default_value' => $notes, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Back'), 'code_approval/bulk'), + ); + return $form; +} + +function book_notes_form_submit($form, &$form_state) +{ + global $user; + + /* get current proposal */ + $preference_id = arg(2); + $preference_id = (int)$preference_id; + $result = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $preference_id); + if ($result) + { + if ($row = db_fetch_object($result)) + { + /* everything ok */ + } else { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('code_approval/bulk'); + return; + } + } else { + drupal_set_message(t('Invalid book selected. Please try again.'), 'error'); + drupal_goto('code_approval/bulk'); + return; + } + + /* find existing notes */ + $notes_q = db_query("SELECT * FROM {textbook_companion_notes} WHERE preference_id = %d LIMIT 1", $preference_id); + $notes_data = db_fetch_object($notes_q); + + /* add or update notes in database */ + if ($notes_data) { + db_query("UPDATE {textbook_companion_notes} SET notes = '%s' WHERE id = %d", $form_state['values']['notes'], $notes_data->id); + drupal_set_message('Notes updated successfully.', 'status'); + } else { + db_query("INSERT INTO {textbook_companion_notes} (preference_id, notes) VALUES (%d, '%s')", $preference_id, $form_state['values']['notes']); + drupal_set_message('Notes added successfully.', 'status'); + } +} + +/* return proposal and author information */ +function _book_information($preference_id) +{ + $book_data = db_fetch_object(db_query("SELECT + preference.book as preference_book, preference.author as preference_author, preference.isbn as preference_isbn, preference.publisher as preference_publisher, preference.edition as preference_edition, preference.year as preference_year, + proposal.full_name as proposal_full_name, proposal.faculty as proposal_faculty, proposal.reviewer as proposal_reviewer, proposal.course as proposal_course, proposal.branch as proposal_branch, proposal.university as proposal_university + FROM {textbook_companion_proposal} proposal LEFT JOIN {textbook_companion_preference} preference ON proposal.id = preference.proposal_id WHERE preference.id = %d", $preference_id)); + return $book_data; +} + diff --git a/proposal.inc b/proposal.inc new file mode 100755 index 0000000..6b45639 --- /dev/null +++ b/proposal.inc @@ -0,0 +1,452 @@ +<?php + +function book_proposal_form($form_state) +{ + global $user; + + if (!$user->uid) { + drupal_set_message('It is mandatory to login on this website to access the proposal form', 'error'); + return; + } + + /* check if user has already submitted a proposal */ + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE uid = %d ORDER BY id DESC LIMIT 1", $user->uid); + if ($proposal_q) + { + if ($proposal_data = db_fetch_object($proposal_q)) + { + 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 1: + drupal_set_message(t('Your proposal has been approved. Please go to ' . l('Code Submission', 'textbook_companion/code') . ' to upload your code'), 'status'); + drupal_goto(''); + return; + break; + case 2: + drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal below.'), 'error'); + break; + case 3: + drupal_set_message(t('Congratulations! You have completed your last book proposal. You can create another proposal below.'), 'status'); + break; + default: + drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error'); + drupal_goto(''); + return; + break; + } + } + } + + $form['imp_notice'] = array( + '#type' => 'item', + '#value' => '<font color="red"><b>Please fill up this form carefully as the details entered here will be exactly written in the Textbook Companion</b></font>', + ); + + $form['full_name'] = array( + '#type' => 'textfield', + '#title' => t('Full Name'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['email_id'] = array( + '#type' => 'textfield', + '#title' => t('Email'), + '#size' => 30, + '#value' => $user->mail, + '#disabled' => TRUE, + ); + $form['mobile'] = array( + '#type' => 'textfield', + '#title' => t('Mobile No.'), + '#size' => 30, + '#maxlength' => 15, + '#required' => TRUE, + ); + $form['how_project'] = array( + '#type' => 'select', + '#title' => t('How did you come to know about this project'), + '#options' => array('Oscad Website' => 'Oscad Website', + 'Friend' => 'Friend', + 'Professor/Teacher' => 'Professor/Teacher', + 'Mailing List' => 'Mailing List', + 'Poster in my/other college' => 'Poster in my/other college', + 'Others' => 'Others'), + '#required' => TRUE, + ); + $form['course'] = array( + '#type' => 'textfield', + '#title' => t('Course'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['branch'] = array( + '#type' => 'select', + '#title' => t('Department/Branch'), + '#options' => array('Electrical Engineering' => 'Electrical Engineering', + 'Electronics Engineering' => 'Electronics Engineering', + 'Computer Engineering' => 'Computer Engineering', + 'Chemical Engineering' => 'Chemical Engineering', + 'Instrumentation Engineering' => 'Instrumentation Engineering', + 'Mechanical Engineering' => 'Mechanical Engineering', + 'Civil Engineering' => 'Civil Engineering', + 'Physics' => 'Physics', + 'Mathematics' => 'Mathematics', + 'Others' => 'Others'), + '#required' => TRUE, + ); + $form['university'] = array( + '#type' => 'textfield', + '#title' => t('University/Institute'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['faculty'] = array( + '#type' => 'textfield', + '#title' => t('College Teacher/Professor'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['reviewer'] = array( + '#type' => 'textfield', + '#title' => t('Reviewer'), + '#size' => 30, + '#maxlength' => 50, + ); + $form['completion_date'] = array( + '#type' => 'textfield', + '#title' => t('Expected Date of Completion'), + '#description' => t('Input date format should be DD-MM-YYYY. Eg: 23-03-2011'), + '#size' => 10, + '#maxlength' => 10, + ); + $form['preference1'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 1'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference1']['book1'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference1']['author1'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference1']['isbn1'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + ); + $form['preference1']['publisher1'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['preference1']['edition1'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + ); + $form['preference1']['year1'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + ); + $form['preference2'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 2'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference2']['book2'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference2']['author2'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference2']['isbn2'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + ); + $form['preference2']['publisher2'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['preference2']['edition2'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + ); + $form['preference2']['year2'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + ); + $form['preference3'] = array( + '#type' => 'fieldset', + '#title' => t('Book Preference 3'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['preference3']['book3'] = array( + '#type' => 'textfield', + '#title' => t('Title of the book'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference3']['author3'] = array( + '#type' => 'textfield', + '#title' => t('Author Name'), + '#size' => 30, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['preference3']['isbn3'] = array( + '#type' => 'textfield', + '#title' => t('ISBN No'), + '#size' => 30, + '#maxlength' => 25, + '#required' => TRUE, + ); + $form['preference3']['publisher3'] = array( + '#type' => 'textfield', + '#title' => t('Publisher & Place'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['preference3']['edition3'] = array( + '#type' => 'textfield', + '#title' => t('Edition'), + '#size' => 4, + '#maxlength' => 2, + '#required' => TRUE, + ); + $form['preference3']['year3'] = array( + '#type' => 'textfield', + '#title' => t('Year of pulication'), + '#size' => 4, + '#maxlength' => 4, + '#required' => TRUE, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + return $form; +} + +function book_proposal_form_validate($form, &$form_state) +{ + /* mobile */ + if (!preg_match('/^[0-9\ \+]{0,15}$/', $form_state['values']['mobile'])) + form_set_error('mobile', t('Invalid mobile number')); + + /* date of completion */ + if (!preg_match('/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/', $form_state['values']['completion_date'])) + form_set_error('completion_date', t('Invalid expected date of completion')); + + list($d, $m, $y) = explode('-', $form_state['values']['completion_date']); + $d = (int)$d; $m = (int)$m; $y = (int)$y; + if (!checkdate($m, $d, $y)) + form_set_error('completion_date', t('Invalid expected date of completion')); + if (mktime(0, 0, 0, $m, $d, $y) <= time()) + form_set_error('completion_date', t('Expected date of completion should be in future')); + + /* edition */ + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition1'])) + form_set_error('edition1', t('Invalid edition for Book Preference 1')); + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition2'])) + form_set_error('edition2', t('Invalid edition for Book Preference 2')); + if (!preg_match('/^[1-9][0-9]{0,1}$/', $form_state['values']['edition3'])) + form_set_error('edition3', t('Invalid edition for Book Preference 3')); + + /* year of publication */ + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year1'])) + form_set_error('year1', t('Invalid year of pulication for Book Preference 1')); + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year2'])) + form_set_error('year2', t('Invalid year of pulication for Book Preference 2')); + if (!preg_match('/^[1-3][0-9][0-9][0-9]$/', $form_state['values']['year3'])) + form_set_error('year3', t('Invalid year of pulication for Book Preference 3')); + + /* year of publication */ + $cur_year = date('Y'); + if ((int)$form_state['values']['year1'] > $cur_year) + form_set_error('year1', t('Year of pulication should be not in the future for Book Preference 1')); + if ((int)$form_state['values']['year2'] > $cur_year) + form_set_error('year2', t('Year of pulication should be not in the future for Book Preference 2')); + if ((int)$form_state['values']['year3'] > $cur_year) + form_set_error('year3', t('Year of pulication should be not in the future for Book Preference 3')); + + /* isbn */ + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn1'])) + form_set_error('isbn1', t('Invalid ISBN for Book Preference 1')); + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn2'])) + form_set_error('isbn2', t('Invalid ISBN for Book Preference 2')); + if (!preg_match('/^[0-9\-xX]+$/', $form_state['values']['isbn3'])) + form_set_error('isbn3', t('Invalid ISBN for Book Preference 3')); + return; +} + +function book_proposal_form_submit($form, &$form_state) +{ + global $user; + + if (!$user->uid) { + drupal_set_message('It is mandatory to login on this website to access the proposal form', 'error'); + return; + } + + /* completion date to timestamp */ + list($d, $m, $y) = explode('-', $form_state['values']['completion_date']); + $completion_date_timestamp = mktime(0, 0, 0, $m, $d, $y); + + /* inserting the user proposal */ + $result = db_query("INSERT INTO {textbook_companion_proposal} + (uid, approver_uid, full_name, mobile, how_project, course, branch, university, faculty, reviewer, completion_date, creation_date, approval_date, proposal_status) VALUES + (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d)", + $user->uid, + 0, + ucwords(strtolower($form_state['values']['full_name'])), + $form_state['values']['mobile'], + $form_state['values']['how_project'], + $form_state['values']['course'], + $form_state['values']['branch'], + $form_state['values']['university'], + ucwords(strtolower($form_state['values']['faculty'])), + ucwords(strtolower($form_state['values']['reviewer'])), + $completion_date_timestamp, + time(), + 0, + 0 + ); + if (!$result) + { + drupal_set_message(t('Error receiving your proposal. Please try again.'), 'error'); + return; + } + /* proposal id */ + $proposal_id = db_last_insert_id('textbook_companion_proposal', 'id'); + + /* inserting first book preference */ + if ($form_state['values']['book1']) + { + $result = db_query("INSERT INTO {textbook_companion_preference} + (proposal_id, pref_number, book, author, isbn, publisher, edition, year, category, approval_status) VALUES + (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", + $proposal_id, + 1, + ucwords(strtolower($form_state['values']['book1'])), + ucwords(strtolower($form_state['values']['author1'])), + $form_state['values']['isbn1'], + ucwords(strtolower($form_state['values']['publisher1'])), + $form_state['values']['edition1'], + $form_state['values']['year1'], + 0, + 0 + ); + if (!$result) + { + drupal_set_message(t('Error receiving your first book preference.'), 'error'); + } + } + + /* inserting second book preference */ + if ($form_state['values']['book2']) + { + $result = db_query("INSERT INTO {textbook_companion_preference} + (proposal_id, pref_number, book, author, isbn, publisher, edition, year, category, approval_status) VALUES + (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", + $proposal_id, + 2, + ucwords(strtolower($form_state['values']['book2'])), + ucwords(strtolower($form_state['values']['author2'])), + $form_state['values']['isbn2'], + ucwords(strtolower($form_state['values']['publisher2'])), + $form_state['values']['edition2'], + $form_state['values']['year2'], + 0, + 0 + ); + if (!$result) + { + drupal_set_message(t('Error receiving your second book preference.'), 'error'); + } + } + + /* inserting third book preference */ + if ($form_state['values']['book3']) + { + $result = db_query("INSERT INTO {textbook_companion_preference} + (proposal_id, pref_number, book, author, isbn, publisher, edition, year, category, approval_status) VALUES + (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", + $proposal_id, + 3, + ucwords(strtolower($form_state['values']['book3'])), + ucwords(strtolower($form_state['values']['author3'])), + $form_state['values']['isbn3'], + ucwords(strtolower($form_state['values']['publisher3'])), + $form_state['values']['edition3'], + $form_state['values']['year3'], + 0, + 0 + ); + if (!$result) + { + drupal_set_message(t('Error receiving your third book preference.'), 'error'); + } + } + + /* sending email */ + $email_to = $user->mail; + $param['proposal_received']['proposal_id'] = $proposal_id; + $param['proposal_received']['user_id'] = $user->uid; + if (!drupal_mail('textbook_companion', 'proposal_received', $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('We have received you book proposal. We will get back to you soon.'), 'status'); + drupal_goto(''); +} @@ -0,0 +1,332 @@ +<?php +// $Id$ + +function textbook_companion_run_form($form_state) +{ + $form['#redirect'] = FALSE; + + ahah_helper_register($form, $form_state); + + /* default value for ahah fields */ + if (!isset($form_state['storage']['run']['book'])) + { + /* get the book id from url */ + $url_book_id = (int)arg(1); + if ($url_book_id) + { + /* add javascript for book selected */ + $chapter_name_js = " $(document).ready(function() { + $('#edit-run-book').val(" . $url_book_id . "); + $('#edit-run-book').change(); + });"; + drupal_add_js($chapter_name_js, 'inline', 'footer'); + } else { + $book_default_value = 0; + } + } else { + $book_default_value = $form_state['storage']['run']['book']; + } + + if (!isset($form_state['storage']['run']['chapter'])) + { + $chapter_default_value = 0; + } else { + if ($form_state['values']['run']['book_hidden'] != $form_state['values']['run']['book']) + $chapter_default_value = 0; + else + $chapter_default_value = $form_state['storage']['run']['chapter']; + } + + if (!isset($form_state['storage']['run']['example'])) + { + $example_default_value = 0; + } else { + if ($form_state['values']['run']['book_hidden'] != $form_state['values']['run']['book']) + $example_default_value = 0; + else if ($form_state['values']['run']['chapter_hidden'] != $form_state['values']['run']['chapter']) + $example_default_value = 0; + else + $example_default_value = $form_state['storage']['run']['example']; + } + + $form['run'] = array( + '#type' => 'fieldset', + '#title' => t('Run Book Example'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + '#prefix' => '<div id="run-wrapper">', + '#suffix' => '</div>', + '#tree' => TRUE, + ); + + $form['run']['book'] = array( + '#type' => 'select', + '#title' => t('Title of the Book'), + '#options' => _list_of_books(), + '#default_value' => $book_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + + /* hidden form elements */ + $form['run']['book_hidden'] = array( + '#type' => 'hidden', + '#value' => $form_state['values']['run']['book'], + ); + + /* hidden form elements */ + $form['run']['chapter_hidden'] = array( + '#type' => 'hidden', + '#value' => $form_state['values']['run']['chapter'], + ); + + if ($book_default_value > 0) + { + $book_details = _book_information($book_default_value); + $form['run']['book_details'] = array( + '#type' => 'item', + '#value' => '<table cellspacing="1" cellpadding="1" border="0" style="width: 100%;" valign="top">' . + '<tr><td style="width: 35%;"><span style="color: rgb(128, 0, 0);"><strong>About the Book</strong></span></td><td style="width: 35%;"><span style="color: rgb(128, 0, 0);"><strong>About the Contributor</strong></span></td>' . + '<tr><td valign="top"><ul>' . + '<li><strong>Author:</strong> ' . $book_details->preference_author . '</li>' . + '<li><strong>Title of the Book:</strong> ' . $book_details->preference_book . '</li>' . + '<li><strong>Publisher:</strong> ' . $book_details->preference_publisher . '</li>' . + '<li><strong>Year:</strong> ' . $book_details->preference_year . '</li>' . + '<li><strong>Edition:</strong> ' . $book_details->preference_edition . '</li>' . + '</ul></td><td valign="top"><ul>' . + '<li><strong>Contributor Name: </strong>' . $book_details->proposal_full_name . ', ' . $book_details->proposal_course . ', ' . $book_details->proposal_branch . ', ' . $book_details->proposal_university . '</li>' . + '<li><strong>College Teacher: </strong>' . $book_details->proposal_faculty . '</li>' . + '<li><strong>Reviewer: </strong>' . $book_details->proposal_reviewer . '</li>' . + '</ul></td></tr>' . + '</table>', + ); + + $form['run']['download_book'] = array( + '#type' => 'item', + '#value' => l('Download', 'download/book/' . $book_default_value) . ' ' . t('(Download all the examples of the entire book)'), + ); + + /*$form['run']['download_book_pdf'] = array( + '#type' => 'item', + '#value' => l('Download PDF', 'textbook_companion/generate_book/' . $book_default_value) . ' ' . t('(Download PDF for all examples of the entire book)'), + );*/ + + $form['run']['chapter'] = array( + '#type' => 'select', + '#title' => t('Title of the Chapter'), + '#options' => _list_of_chapters($book_default_value), + '#default_value' => $chapter_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + if ($chapter_default_value > 0) + { + $form['run']['download_chapter'] = array( + '#type' => 'item', + '#value' => l('Download', 'download/chapter/' . $chapter_default_value) . ' ' . t('(Download all the examples of the entire chapter)'), + ); + $form['run']['example'] = array( + '#type' => 'select', + '#title' => t('Example No. (Caption)'), + '#options' => _list_of_examples($chapter_default_value), + '#default_value' => $example_default_value, + '#tree' => TRUE, + '#ahah' => array( + 'event' => 'change', + 'effect' => 'none', + 'path' => ahah_helper_path(array('run')), + 'wrapper' => 'run-wrapper', + 'progress' => array( + 'type' => 'throbber', + 'message' => t(''), + ), + ), + ); + } + } + + /* hidden form elements */ + $form['run']['update_book'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#submit' => array('ahah_helper_generic_submit'), + '#attributes' => array('class' => 'no-js'), + ); + + $form['run']['update_chapter'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#submit' => array('ahah_helper_generic_submit'), + '#attributes' => array('class' => 'no-js'), + ); + + /************ START OF $_POST **************/ + if ($_POST) + { + if (($book_default_value > 0) && ($chapter_default_value > 0) && ($example_default_value > 0)) + { + $example_list_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $form_state['values']['run']['example']); + if ($example_list_q) + { + $example_files_rows = array(); + while ($example_list_data = db_fetch_object($example_list_q)) + { + $example_file_type = 'Source or Main file'; + $example_files_rows[] = array(l($example_list_data->filename, 'download/file/' . $example_list_data->id), $example_file_type); + } + + /* creating list of files table */ + $example_files_header = array('Filename', 'Type'); + $example_files = theme_table($example_files_header, $example_files_rows); + } + $form['run']['download_example'] = array( + '#type' => 'item', + '#value' => l('Download Example', 'download/example/' . $example_default_value), + ); + $form['run']['example_files'] = array( + '#type' => 'item', + '#title' => 'List of example files', + '#value' => $example_files, + ); + //$form['run']['submit'] = array( + // '#type' => 'submit', + // '#value' => t('Run') + //); + if (user_access('create feedback')) + { + $form['run']['feedback'] = array( + '#type' => 'textarea', + '#title' => t('Feedback on the above example') + ); + $form['run']['feedback_submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + } + } + } + /************ END OF $_POST **************/ + + return $form; +} + +function textbook_companion_run_form_submit($form, &$form_state) +{ + global $user; + + if ($form_state['clicked_button']['#value'] == 'Submit') + { + if (user_access('create feedback')) + { + $example_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d", $form_state['values']['run']['example'])); + if (!$example_data) + { + drupal_set_message(t('Invalid example selected'), 'error'); + return; + } + $chapter_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id)); + if (!$chapter_data) + { + drupal_set_message(t('Invalid chapter selected'), 'error'); + return; + } + $book_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $chapter_data->preference_id)); + if (!$book_data) + { + drupal_set_message(t('Invalid book selected'), 'error'); + return; + } + $proposal_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $book_data->proposal_id)); + if (!$proposal_data) + { + drupal_set_message(t('Invalid proposal selected'), 'error'); + return; + } + db_query("INSERT INTO {textbook_companion_feedback} (example_id, uid, feedback, ip_address, timestamp) VALUES (%d, %d, '%s', '%s', %d)", + $example_data->id, + $user->uid, + $form_state['values']['run']['feedback'], + $_SERVER['REMOTE_ADDR'], + time()); + + /* sending email */ + $param['feedback_received']['user_id'] = $user->uid; + $param['feedback_received']['book_title'] = $book_data->book; + $param['feedback_received']['chapter_number'] = $chapter_data->number; + $param['feedback_received']['chapter_title'] = $chapter_data->name; + $param['feedback_received']['example_no'] = $example_data->number; + $param['feedback_received']['feedback'] = $form_state['values']['run']['feedback']; + + $email_to = $user->mail;// . ', ' . user_load($proposal_data->uid)->mail. ', ' . user_load($example_data->approver_uid)->mail; + if (!drupal_mail('textbook_companion', 'feedback_received', $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('Thank you for your feedback.'), 'status'); + } else { + drupal_set_message(t('You do not have permission to submit feeback.'), 'error'); + } + } +} + +function _list_of_books() +{ + $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"); + while ($book_titles_data = db_fetch_object($book_titles_q)) + { + $book_titles[$book_titles_data->id] = $book_titles_data->book . ' (Written by ' . $book_titles_data->author . ')'; + } + return $book_titles; +} + +function _list_of_chapters($preference_id = 0) +{ + $book_chapters = array('0' => 'Please select...'); + $book_chapters_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number ASC", $preference_id); + while ($book_chapters_data = db_fetch_object($book_chapters_q)) + { + $book_chapters[$book_chapters_data->id] = $book_chapters_data->number . '. ' . $book_chapters_data->name; + } + return $book_chapters; +} + +function _list_of_examples($chapter_id = 0) +{ + $book_examples = array('0' => 'Please select...'); + $book_examples_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1 ORDER BY + CAST(SUBSTRING_INDEX(number, '.', 1) AS BINARY) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', 2), '.', -1) AS UNSIGNED) ASC, + CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(number , '.', -1), '.', 1) AS UNSIGNED) ASC", $chapter_id); + while ($book_examples_data = db_fetch_object($book_examples_q)) + { + $book_examples[$book_examples_data->id] = $book_examples_data->number . ' (' . $book_examples_data->caption . ')'; + } + return $book_examples; +} + +function _book_information($preference_id) +{ + $book_data = db_fetch_object(db_query("SELECT + preference.book as preference_book, preference.author as preference_author, preference.isbn as preference_isbn, preference.publisher as preference_publisher, preference.edition as preference_edition, preference.year as preference_year, + proposal.full_name as proposal_full_name, proposal.faculty as proposal_faculty, proposal.reviewer as proposal_reviewer, proposal.course as proposal_course, proposal.branch as proposal_branch, proposal.university as proposal_university + FROM {textbook_companion_proposal} proposal LEFT JOIN {textbook_companion_preference} preference ON proposal.id = preference.proposal_id WHERE preference.id = %d", $preference_id)); + return $book_data; +} diff --git a/settings.inc b/settings.inc new file mode 100755 index 0000000..45667f0 --- /dev/null +++ b/settings.inc @@ -0,0 +1,54 @@ +<?php +// $Id$ + +function textbook_companion_settings_form($form_state) +{ + $form['emails'] = array( + '#type' => 'textfield', + '#title' => t('Notification emails'), + '#description' => t('A comma separated list of email addresses to receive notifications emails'), + '#size' => 50, + '#maxlength' => 255, + '#required' => TRUE, + '#default_value' => variable_get('textbook_companion_emails', ''), + ); + + $form['from_email'] = array( + '#type' => 'textfield', + '#title' => t('Outgoing from email address'), + '#description' => t('Email address to be display in the from field of all outgoing messages'), + '#size' => 50, + '#maxlength' => 255, + '#required' => TRUE, + '#default_value' => variable_get('textbook_companion_from_email', ''), + ); + + $form['extensions']['source'] = array( + '#type' => 'textfield', + '#title' => t('Allowed source file extensions'), + '#description' => t('A comma separated list WITHOUT SPACE of source file extensions that are permitted to be uploaded on the server'), + '#size' => 50, + '#maxlength' => 255, + '#required' => TRUE, + '#default_value' => variable_get('textbook_companion_source_extensions', ''), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + return $form; +} + +function textbook_companion_settings_form_validate($form, &$form_state) +{ + return; +} + +function textbook_companion_settings_form_submit($form, &$form_state) +{ + variable_set('textbook_companion_emails', $form_state['values']['emails']); + variable_set('textbook_companion_from_email', $form_state['values']['from_email']); + variable_set('textbook_companion_source_extensions', $form_state['values']['source']); + drupal_set_message(t('Settings updated'), 'status'); +} diff --git a/textbook_companion.info b/textbook_companion.info new file mode 100755 index 0000000..318a814 --- /dev/null +++ b/textbook_companion.info @@ -0,0 +1,6 @@ +name = Textbook Companion +description = Textbook Companion project, IIT Bombay +package = Oscad +version = VERSION +core = 6.x +dependencies[] = ahah_helper diff --git a/textbook_companion.module b/textbook_companion.module new file mode 100755 index 0000000..6e38740 --- /dev/null +++ b/textbook_companion.module @@ -0,0 +1,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; + } +} + |