diff options
-rwxr-xr-x | dependency.inc | 191 | ||||
-rwxr-xr-x | lab_migration.module | 38 | ||||
-rwxr-xr-x | proposal.inc | 218 | ||||
-rwxr-xr-x | upload_code.inc | 136 |
4 files changed, 467 insertions, 116 deletions
diff --git a/dependency.inc b/dependency.inc new file mode 100755 index 0000000..da9c567 --- /dev/null +++ b/dependency.inc @@ -0,0 +1,191 @@ +<?php +// $Id$ + +function upload_dependency_form($form_state) +{ + global $user; + + $proposal_data = get_proposal(); + if (!$proposal_data) { + drupal_goto(''); + return; + } + + $form['#attributes'] = array('enctype' => "multipart/form-data"); + + $form['lab_title'] = array( + '#type' => 'item', + '#value' => $proposal_data->lab_title, + '#title' => t('Title of the Lab'), + ); + $form['name'] = array( + '#type' => 'item', + '#value' => $proposal_data->name, + '#title' => t('Contributor Name'), + ); + + $form['existing_depfile'] = array( + '#type' => 'item', + '#value' => _list_existing_dependency($proposal_data->id), + '#title' => t('List of existing dependency files for this book'), + ); + + $form['depfile'] = array( + '#type' => 'fieldset', + '#title' => t('Upload Dependency Files'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + ); + $form['depfile']['depfile1'] = array( + '#type' => 'file', + '#title' => t('Upload dependency file'), + '#description' => t("Allowed file extensions : ") . variable_get('lab_migration_dependency_extensions', ''), + ); + $form['depfile']['depfile1_caption'] = array( + '#type' => 'textfield', + '#title' => t('Caption for dependency file'), + '#size' => 15, + '#maxlength' => 100, + '#required' => TRUE, + ); + $form['depfile']['depfile1_description'] = array( + '#type' => 'textarea', + '#title' => t('Brief Description of the dependency file'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Back'), 'lab_migration/code'), + ); + return $form; +} + +function upload_dependency_form_validate($form, &$form_state) +{ + global $user; + + /* get approved proposal details */ + $proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE uid = %d ORDER BY id DESC LIMIT 1", $user->uid); + $proposal_data = db_fetch_object($proposal_q); + if (!$proposal_data) + { + form_set_error('', t('Invalid')); + } + + if (isset($_FILES['files'])) + { + /* check for valid filename extensions */ + $allowed_extensions = explode(',' , variable_get('lab_migration_dependency_extensions', '')); + foreach ($_FILES['files']['name'] as $file_form_name => $file_name) + { + if ($file_name) + { + $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 ' . variable_get('lab_migration_dependency_extensions', '') . ' 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 file already exists */ + $dep_exists_data = db_fetch_object(db_query("SELECT * FROM {lab_migration_dependency_files} WHERE filename = '%s'", $_FILES['files']['name'][$file_form_name])); + if ($dep_exists_data) + form_set_error($file_form_name, t('Dependency file with the same name has already been uploaded in this or some other lab solution. Please rename the file and try again.')); + + /* check if valid file name */ + if (!lab_migration_check_valid_filename($_FILES['files']['name'][$file_form_name])) + form_set_error($file_form_name, t('Invalid file name specified. Only alphabets, numbers and underscore is allowed as a valid filename.')); + } + } + } +} + +function upload_dependency_form_submit($form, &$form_state) { + global $user; + + $root_path = lab_migration_path(); + + $proposal_data = get_proposal(); + if (!$proposal_data) { + drupal_goto(''); + return; + } + + $dest_path .= 'DEPENDENCIES' . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* uploading dependencies */ + $file_upload_counter = 0; + $dependency_ids = array(); + $dependency_names = array(); + + foreach ($_FILES['files']['name'] as $file_form_name => $file_name) + { + if ($file_name) + { + /* 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 {lab_migration_dependency_files} (proposal_id, filename, filepath, filemime, filesize, caption, description, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', '%s', %d)", + $proposal_data->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], + check_plain($form_state['values'][$file_form_name . '_caption']), + check_plain($form_state['values'][$file_form_name . '_description']), + time() + ); + drupal_set_message($file_name . ' uploaded successfully.', 'status'); + $dependency_ids[] = db_last_insert_id('lab_migration_dependency_files', 'id'); + $dependency_names[] = $_FILES['files']['name'][$file_form_name]; + $file_upload_counter++; + } else { + drupal_set_message('Error uploading dependency : ' . $dest_path . '/' . $_FILES['files']['name'][$file_form_name], 'error'); + } + } + } + + if ($file_upload_counter > 0) + { + drupal_set_message('Dependencies uploaded successfully.', 'status'); + + /* sending email */ + $param['dependency_uploaded']['user_id'] = $user->uid; + $param['dependency_uploaded']['dependency_names'] = $dependency_names; + + $email_to = $user->mail . ', ' . variable_get('lab_migration_emails', ''); + if (!drupal_mail('lab_migration', 'dependency_uploaded', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } + + drupal_goto('lab_migration/code/upload_dep'); +} + +function _list_existing_dependency($proposal_id) +{ + $return_html = '<ul>'; + $proposal_dependency_files_q = db_query("SELECT * FROM {lab_migration_dependency_files} WHERE proposal_id = %d ORDER BY filename ASC", $proposal_id); + + $counter = 0; + while ($proposal_dependency_files_data = db_fetch_object($proposal_dependency_files_q)) + { + $temp_caption = ''; + if ($proposal_dependency_files_data->caption) + $temp_caption = ' (' . $proposal_dependency_files_data->caption . ')'; + $return_html .= '<li>' . l($proposal_dependency_files_data->filename . $temp_caption, 'download/dependency/' . $proposal_dependency_files_data->id) . '</li>'; + $counter++; + } + if ($counter == 0) + $return_html .= '<li>(None)</li>'; + $return_html .= '</ul>'; + return $return_html; +} + diff --git a/lab_migration.module b/lab_migration.module index fc7be3a..5af3f3c 100755 --- a/lab_migration.module +++ b/lab_migration.module @@ -1047,6 +1047,7 @@ function lab_migration_ajax() } /*************************** VALIDATION FUNCTIONS *****************************/ + function lab_migration_check_valid_filename($file_name) { if (!preg_match('/^[0-9a-zA-Z\_\.]+$/', $file_name)) return FALSE; @@ -1071,12 +1072,43 @@ function check_code_number($number = '') { return TRUE; } -function lab_migration_upload_path() { +function lab_migration_path() { return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'lab_migration/uploads/'; } -function lab_migration_path() { - return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'lab_migration/uploads/'; +/************************* USER VERIFICATION FUNCTIONS ************************/ + +function get_proposal() { + global $user; + + $proposal_q = db_query("SELECT * FROM {lab_migration_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', 'lab_migration/proposal') . ".", 'error'); + drupal_goto(''); + } + if ($proposal_data->proposal_status != 1) + { + switch ($proposal_data->approval_status) + { + case 0: + drupal_set_message(t('We have already received your proposal. We will get back to you soon.'), 'status'); + return FALSE; + case 1: + return $proposal_data; + case 2: + drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'lab_migration/proposal') . '.'), 'error'); + return FALSE; + case 3: + drupal_set_message(t('Congratulations! You have completed your last book proposal. You have to create another proposal ' . l('here', 'lab_migration/proposal') . '.'), 'status'); + return FALSE; + default: + drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error'); + return FALSE; + } + } + return FALSE; } /****************************** DELETION FUNCTIONS ****************************/ diff --git a/proposal.inc b/proposal.inc new file mode 100755 index 0000000..629f068 --- /dev/null +++ b/proposal.inc @@ -0,0 +1,218 @@ +<?php +// $Id$ + +/* + Approval Status : + 0 - Pending + 1 - Approved + 2 - Dis-Approved + 3 - Solved + Solution Status : + 0 - Pending + 1 - Approved + 2 - Dis-Approved + 3 - Completed + Solution Display : + 0 - No + 1 - Yes + + Tables : + lab_migration_solution : approval_status + 0 - Pending + 1 - Approved +*/ + +function lab_migration_proposal_form($form_state) +{ + global $user; + + /************************ start approve book details ************************/ + $proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE uid = %d ORDER BY id DESC LIMIT 1", $user->uid); + $proposal_data = db_fetch_object($proposal_q); + if ($proposal_data) + { + if ($proposal_data->approval_status == 0 || $proposal_data->approval_status == 1) { + drupal_set_message(t('We have already received your proposal.'), 'status'); + drupal_goto(''); + return; + } + } + + $form['#attributes'] = array('enctype' => "multipart/form-data"); + + $form['name_title'] = array( + '#type' => 'select', + '#title' => t('Title'), + '#options' => array('Mr' => 'Mr', 'Ms' => 'Ms', 'Mrs' => 'Mrs', 'Dr' => 'Dr', 'Prof' => 'Prof'), + '#required' => TRUE, + ); + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name of the Proposer'), + '#size' => 30, + '#maxlength' => 50, + '#required' => TRUE, + ); + $form['email_id'] = array( + '#type' => 'textfield', + '#title' => t('Email'), + '#size' => 30, + '#value' => $user->mail, + '#disabled' => TRUE, + ); + $form['contact_ph'] = array( + '#type' => 'textfield', + '#title' => t('Contact Phone No.'), + '#size' => 30, + '#maxlength' => 15, + '#required' => TRUE, + ); + $form['department'] = array( + '#type' => 'select', + '#title' => t('Department/Branch'), + '#options' => array('' => 'Please select...', + 'Computer Engineering' => 'Computer Engineering', + 'Electrical Engineering' => 'Electrical Engineering', + 'Electronics Engineering' => 'Electronics 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['lab_title'] = array( + '#type' => 'textfield', + '#title' => t('Title of the Lab'), + '#size' => 50, + '#required' => TRUE, + ); + + $first_experiemnt = TRUE; + for ($counter = 1; $counter <= 15; $counter++) { + $form['lab_experiment-' . $counter] = array( + '#type' => 'textfield', + '#title' => t('Title of the Experiment ') . $counter, + '#size' => 50, + '#required' => $first_experiemnt, + ); + $first_experiemnt = FALSE; + } + + $form['solution_provider_uid'] = array( + '#type' => 'radios', + '#title' => t('Do you want to provide the solution'), + '#options' => array('1' => 'Yes', '2' => 'No'), + '#required' => TRUE, + '#default_value' => '1', + ); + + $form['solution_display'] = array( + '#type' => 'radios', + '#title' => t('Do you want to display the solution on the www.scilab.in website'), + '#options' => array('1' => 'Yes', '2' => 'No'), + '#required' => TRUE, + '#default_value' => '1', + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + return $form; +} + +function lab_migration_proposal_form_validate($form, &$form_state) +{ + if (!preg_match('/^[0-9\ \+]{0,15}$/', $form_state['values']['contact_ph'])) + form_set_error('mobile', t('Invalid contact phone number')); + return; +} + +function lab_migration_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; + } + + $solution_provider_uid = 0; + if ($form_state['values']['solution_provider_uid'] == "1") + $solution_provider_uid = $user->uid; + else + $solution_provider_uid = 0; + + $solution_display = 0; + if ($form_state['values']['solution_display'] == "1") + $solution_display = 1; + else + $solution_display = 0; + + /* inserting the user proposal */ + $result = db_query("INSERT INTO {lab_migration_proposal} + (uid, approver_uid, name_title, name, contact_ph, department, university, lab_title, approval_status, solution_status, solution_provider_uid, solution_display, creation_date, approval_date, solution_date) VALUES + (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, 0, 0)", + $user->uid, + 0, + $form_state['values']['name_title'], + $form_state['values']['name'], + $form_state['values']['contact_ph'], + $form_state['values']['department'], + $form_state['values']['university'], + $form_state['values']['lab_title'], + 0, + 0, + $solution_provider_uid, + $solution_display, + time() + ); + if (!$result) + { + drupal_set_message(t('Error receiving your proposal. Please try again.'), 'error'); + return; + } + /* proposal id */ + $proposal_id = db_last_insert_id('lab_migration_proposal', 'id'); + + /* adding experiments */ + $number = 1; + for ($counter = 1; $counter <= 15; $counter++) { + $experiment_field_name = 'lab_experiment-' . $counter; + if (strlen(trim($form_state['values'][$experiment_field_name])) >= 1) { + $result = db_query("INSERT INTO {lab_migration_experiment} (proposal_id, number, title) VALUES (%d, %d, '%s')", $proposal_id, $number, trim($form_state['values'][$experiment_field_name])); + if (!$result) + { + drupal_set_message(t('Could not insert Title of the Experiment : ') . trim($form_state['values'][$experiment_field_name]), 'error'); + } else { + $number++; + } + } + } + + /* sending email */ + $email_to = $user->mail; + $param['proposal_received']['proposal_id'] = $proposal_id; + $param['proposal_received']['user_id'] = $user->uid; + if (!drupal_mail('lab_migration', 'proposal_received', $email_to , language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + /* sending email */ + $email_to = variable_get('lab_migration_emails', ''); + if (!drupal_mail('lab_migration', 'proposal_received', $email_to , language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + + drupal_set_message(t('We have received you lab migration proposal. We will get back to you soon.'), 'status'); + drupal_goto(''); +} + diff --git a/upload_code.inc b/upload_code.inc index 7f6201e..7d65d54 100755 --- a/upload_code.inc +++ b/upload_code.inc @@ -5,40 +5,10 @@ function list_experiments() { global $user; - $proposal_q = db_query("SELECT * FROM {lab_migration_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', 'lab_migration/proposal') . ".", 'error'); - drupal_goto(''); - } - if ($proposal_data->proposal_status != 1) - { - switch ($proposal_data->approval_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: - break; - case 2: - drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'lab_migration/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', 'lab_migration/proposal') . '.'), 'status'); + $proposal_data = get_proposal(); + if (!$proposal_data) { drupal_goto(''); return; - break; - default: - drupal_set_message(t('Invalid proposal state. Please contact site administrator for further information.'), 'error'); - drupal_goto(''); - return; - break; - } } $return_html = '<br />'; @@ -83,46 +53,16 @@ function upload_code_form($form_state) { global $user; - $proposal_q = db_query("SELECT * FROM {lab_migration_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', 'lab_migration/proposal') . ".", 'error'); - drupal_goto(''); - } - if ($proposal_data->proposal_status != 1) - { - switch ($proposal_data->approval_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: - break; - case 2: - drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'lab_migration/proposal') . '.'), 'error'); + $proposal_data = get_proposal(); + if (!$proposal_data) { 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', 'lab_migration/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; - } } /* add javascript for automatic book title, check if example uploaded, dependency selection effects */ $dep_selection_js = " $(document).ready(function() { $('#edit-existing-depfile-dep-lab-title').change(function() { - var dep_selected = ''; + var dep_selected = ''; /* showing and hiding relevant files */ $('.form-checkboxes .option').hide(); $('.form-checkboxes .option').each(function(index) { @@ -139,9 +79,9 @@ function upload_code_form($form_state) }); $('.form-checkboxes .option').change(function() { - $('#edit-existing-depfile-dep-book-title').trigger('change'); + $('#edit-existing-depfile-dep-lab-title').trigger('change'); }); - $('#edit-existing-depfile-dep-book-title').trigger('change'); + $('#edit-existing-depfile-dep-lab-title').trigger('change'); });"; drupal_add_js($dep_selection_js, 'inline', 'header'); @@ -370,12 +310,12 @@ function upload_code_form_validate($form, &$form_state) /* add javascript again for automatic book title, check if example uploaded, dependency selection effects */ $dep_selection_js = " $(document).ready(function() { - $('#edit-existing-depfile-dep-lab-title-wrapper').change(function() { + $('#edit-existing-depfile-dep-lab-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(); + var activeClass = $('#edit-existing-depfile-dep-lab-title').val(); if ($(this).children().hasClass(activeClass)) { $(this).show(); } @@ -388,9 +328,9 @@ function upload_code_form_validate($form, &$form_state) }); $('.form-checkboxes .option').change(function() { - $('#edit-existing-depfile-dep-book-title').trigger('change'); + $('#edit-existing-depfile-dep-lab-title').trigger('change'); }); - $('#edit-existing-depfile-dep-book-title').trigger('change'); + $('#edit-existing-depfile-dep-lab-title').trigger('change'); });"; drupal_add_js($dep_selection_js, 'inline', 'header'); } @@ -400,42 +340,12 @@ function upload_code_form_submit($form, &$form_state) { $root_path = lab_migration_path(); - /************************ check proposal details ************************/ - $proposal_q = db_query("SELECT * FROM {lab_migration_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', 'lab_migration/proposal') . ".", 'error'); - drupal_goto(''); - } - if ($proposal_data->proposal_status != 1) - { - switch ($proposal_data->approval_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: - break; - case 2: - drupal_set_message(t('Your proposal has been dis-approved. Please create another proposal ' . l('here', 'lab_migration/proposal') . '.'), 'error'); + $proposal_data = get_proposal(); + if (!$proposal_data) { 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', 'lab_migration/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; - } } + $proposal_id = $proposal_data->id; /************************ check experiment details ************************/ @@ -579,18 +489,18 @@ function _list_of_lab_titles() function _list_of_dependency_files() { - $book_dependency_files = array(); - $book_dependency_files_class = array(); - $book_dependency_files_q = db_query("SELECT * FROM {lab_migration_dependency_files} ORDER BY filename ASC"); + $dependency_files = array(); + $dependency_files_class = array(); + $dependency_files_q = db_query("SELECT * FROM {lab_migration_dependency_files} ORDER BY filename ASC"); - while ($book_dependency_files_data = db_fetch_object($book_dependency_files_q)) + while ($dependency_files_data = db_fetch_object($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; + if ($dependency_files_data->caption) + $temp_caption .= ' (' . $dependency_files_data->caption . ')'; + $dependency_files[$dependency_files_data->id] = l($dependency_files_data->filename . $temp_caption, 'download/dependency/' . $dependency_files_data->id); + $dependency_files_class[$dependency_files_data->id] = $dependency_files_data->proposal_id; } - return array($book_dependency_files, $book_dependency_files_class); + return array($dependency_files, $dependency_files_class); } |