From 65759be3ccaf5e519d11c7c97d7a2620619883af Mon Sep 17 00:00:00 2001 From: Prashant P. Shah Date: Thu, 26 Apr 2012 16:11:09 +0530 Subject: adds code upload and deletion Signed-off-by: Prashant P. Shah --- general_deletion.inc | 92 ++++++++ lab_migration.module | 293 ++---------------------- manage_proposal.inc | 36 ++- upload_code.inc | 596 +++++++++++++++++++++++++++++++++++++++++++++++++ upload_code_delete.inc | 71 ++++++ 5 files changed, 808 insertions(+), 280 deletions(-) create mode 100644 general_deletion.inc create mode 100755 upload_code.inc create mode 100644 upload_code_delete.inc diff --git a/general_deletion.inc b/general_deletion.inc new file mode 100644 index 0000000..dc06a7a --- /dev/null +++ b/general_deletion.inc @@ -0,0 +1,92 @@ +experiment_id); + $experiment_data = db_fetch_object($experiment_q); + if (!$experiment_data) + { + drupal_set_message(t('Invalid experiment.'), 'error'); + return FALSE; + } + + /* deleting solution files */ + $solution_files_q = db_query("SELECT * FROM {lab_migration_solution_files} WHERE solution_id = %d", $solution_id); + while ($solution_files_data = db_fetch_object($solution_files_q)) + { + if (!file_exists($root_path . $solution_files_data->filepath)) + { + $status = FALSE; + drupal_set_message(t('Error deleting !file. File does not exists.', array('!file' => $solution_files_data->filepath)), 'error'); + continue; + } + + /* removing solution file */ + if (!unlink($root_path . $solution_files_data->filepath)) + { + $status = FALSE; + drupal_set_message(t('Error deleting !file', array('!file' => $solution_files_data->filepath)), 'error'); + + /* sending email to admins */ + $email_to = variable_get('lab_migration_emails', ''); + $param['standard']['subject'] = "[ERROR] Error deleting example file"; + $param['standard']['body'] = "Error deleting solution files by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " : + solution id : " . $solution_id . " + file id : " . $solution_files_data->id . " + file path : " . $solution_files_data->filepath; + if (!drupal_mail('lab_migration', 'standard', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } else { + /* deleting example files database entries */ + db_query("DELETE FROM {lab_migration_solution_files} WHERE id = %d", $solution_files_data->id); + } + } + + if (!$status) + return FALSE; + + /* removing code folder */ + $ex_path = $experiment_data->proposal_id . '/EXP' . $experiment_data->number . '/CODE' . $solution_data->code_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('lab_migration_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('lab_migration', 'standard', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + return FALSE; + } + } else { + drupal_set_message(t('Cannot delete solution folder. !folder does not exists.', array('!folder' => $dir_path)), 'error'); + return FALSE; + } + + /* deleting solution dependency and solution database entries */ + db_query("DELETE FROM {lab_migration_solution_dependency} WHERE solution_id = %d", $solution_id); + db_query("DELETE FROM {lab_migration_solution} WHERE id = %d", $solution_id); + + return $status; +} + diff --git a/lab_migration.module b/lab_migration.module index 40327f4..fc7be3a 100755 --- a/lab_migration.module +++ b/lab_migration.module @@ -1,6 +1,8 @@ MENU_NORMAL_ITEM, 'file' => 'proposal.inc', ); -/* - $items['lab_migration/experiment'] = array( - 'title' => 'Lab Migration Experiment', - 'description' => 'Lab Migration Experiment', - 'page callback' => 'lab_migration_experiment_list', - 'access arguments' => array('create proposal'), - 'file' => 'experiment.inc', - ); - $items['lab_migration/experiment/add'] = array( - 'title' => 'Lab Migration Experiment', - 'description' => 'Lab Migration Experiment', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_experiment_add_form'), - 'access arguments' => array('create proposal'), - 'file' => 'experiment.inc', - ); - $items['lab_migration/experiment/edit'] = array( - 'title' => 'Lab Migration Experiment', - 'description' => 'Lab Migration Experiment', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_experiment_edit_form'), - 'access arguments' => array('create proposal'), - 'file' => 'experiment.inc', - ); - $items['lab_migration/experiment/delete'] = array( - 'title' => 'Lab Migration Experiment', - 'description' => 'Lab Migration Experiment', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_experiment_delete_form'), - 'access arguments' => array('create proposal'), - 'file' => 'experiment.inc', - ); */ - $items['lab_migration/labs'] = array( - 'title' => 'Migrated Labs', - 'description' => 'Migrated Labs', - 'page callback' => 'migrated_labs', - 'access arguments' => array('access content'), - 'type' => MENU_NORMAL_ITEM, - 'file' => 'migrated_labs.inc', - ); - /* for reviewers */ $items['lab_migration/manage_proposal'] = array( 'title' => 'Manage Lab Migration', @@ -117,7 +78,6 @@ function lab_migration_menu() 'type' => MENU_CALLBACK, 'file' => 'manage_proposal.inc', ); - $items['lab_migration/manage_proposal/approve'] = array( 'title' => 'Approve Proposal', 'description' => 'Approve Proposal', @@ -157,81 +117,33 @@ function lab_migration_menu() 'file' => 'settings.inc', ); - return $items; - - $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', - ); - + /* CODE UPLOAD */ $items['lab_migration/code'] = array( 'title' => 'Code Submission', 'description' => 'Code Submission', - 'page callback' => 'list_chapters', + 'page callback' => 'list_experiments', 'access callback' => 'user_access', 'access arguments' => array('upload code'), - 'file' => 'general.inc', + 'file' => 'upload_code.inc', ); - $items['lab_migration/code/list_chapters'] = array( - 'title' => 'List Chapters', - 'description' => 'List Chapters', - 'page callback' => 'list_chapters', + $items['lab_migration/code/list_experiments'] = array( + 'title' => 'List Experiments', + 'description' => 'List Experiments', + 'page callback' => 'list_experiments', 'access arguments' => array('upload code'), 'type' => MENU_DEFAULT_LOCAL_TASK, - 'file' => 'general.inc', + 'file' => 'upload_code.inc', 'weight' => 1, ); $items['lab_migration/code/upload'] = array( 'title' => 'Code Submission', 'description' => 'Code Submission', - 'page callback' => 'upload_examples', + //'page callback' => 'upload_examples', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('upload_code_form'), 'access arguments' => array('upload code'), 'type' => MENU_LOCAL_TASK, - 'file' => 'code.inc', + 'file' => 'upload_code.inc', 'weight' => 2, ); $items['lab_migration/code/upload_dep'] = array( @@ -256,173 +168,10 @@ function lab_migration_menu() $items['lab_migration/code/delete'] = array( 'title' => 'Delete Example', 'description' => 'Delete Example', - 'page callback' => '_upload_examples_delete', + 'page callback' => 'upload_code_delete', 'access arguments' => array('upload code'), 'type' => MENU_CALLBACK, - 'file' => 'code.inc', - ); - $items['lab_migration/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['lab_migration/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['textbook_search'] = array( - 'title' => 'Book Search', - 'description' => '', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_search_form'), - 'access arguments' => array('access content'), - 'type' => MENU_NORMAL_ITEM, - 'file' => 'search.inc', - ); - $items['textbook_search/search'] = array( - 'title' => 'Book Search', - 'description' => '', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_search_form'), - 'access arguments' => array('access content'), - 'type' => MENU_DEFAULT_LOCAL_TASK, - 'file' => 'search.inc', - 'weight' => 1, - ); - $items['textbook_search/book'] = array( - 'title' => 'Browse By Title of the Book', - 'description' => '', - 'page callback' => 'lab_migration_browse_book', - 'access arguments' => array('access content'), - 'type' => MENU_LOCAL_TASK, - 'file' => 'search.inc', - 'weight' => 2, - ); - $items['textbook_search/author'] = array( - 'title' => 'Browse By Author Name', - 'description' => '', - 'page callback' => 'lab_migration_browse_author', - 'access arguments' => array('access content'), - 'type' => MENU_LOCAL_TASK, - 'file' => 'search.inc', - 'weight' => 3, - ); - - $items['textbook_run'] = array( - 'title' => 'Download Codes', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_run_form'), - 'access arguments' => array('access content'), - 'type' => MENU_NORMAL_ITEM, - 'file' => 'run.inc', - ); - - /* download callbacks */ - $items['download/file'] = array( - 'title' => 'Code Download', - 'description' => 'Code Download', - 'page callback' => 'lab_migration_download_example_file', - 'access arguments' => array('download code'), - 'type' => MENU_CALLBACK, - 'file' => 'download.inc', - ); - $items['download/dependency'] = array( - 'title' => 'Code Download', - 'description' => 'Code Download', - 'page callback' => 'lab_migration_download_dependency_file', - 'access arguments' => array('download code'), - 'type' => MENU_CALLBACK, - 'file' => 'download.inc', - ); - $items['download/example'] = array( - 'title' => 'Code Download', - 'description' => 'Code Download', - 'page callback' => 'lab_migration_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' => 'lab_migration_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' => 'lab_migration_download_book', - 'access arguments' => array('download code'), - 'type' => MENU_CALLBACK, - 'file' => 'download.inc', - ); - - /* reviewer download */ - $items['full_download/chapter'] = array( - 'title' => 'Code Download', - 'description' => 'Code Download', - 'page callback' => 'lab_migration_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' => 'lab_migration_download_full_book', - 'access arguments' => array('approve code'), - 'type' => MENU_CALLBACK, - 'file' => 'full_download.inc', - ); - - /* latex script for book generation */ - $items['lab_migration/generate_book'] = array( - 'title' => 'Generate Book', - 'description' => 'Generate Book From Latex Script', - 'page callback' => 'lab_migration_download_book', - 'access arguments' => array('generate book'), - 'type' => MENU_CALLBACK, - 'file' => 'latex.inc', - ); - $items['lab_migration/delete_book'] = array( - 'title' => 'Delete Book PDF', - 'description' => 'Delete Book PDF', - 'page callback' => 'lab_migration_delete_book', - 'access arguments' => array('approve code'), - 'type' => MENU_CALLBACK, - 'file' => 'latex.inc', - ); - - /* general purpose callbacks */ - $items['lab_migration/ajax'] = array( - 'title' => 'Ajax', - 'description' => 'Ajax', - 'page callback' => 'lab_migration_ajax', - 'access arguments' => array('access content'), - 'type' => MENU_CALLBACK, - ); - - /* for admin */ - $items['admin/settings/book_companion'] = array( - 'title' => 'Book Companion Settings', - 'description' => 'Book Companion Settings', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('lab_migration_settings_form'), - 'access arguments' => array('administer book companion'), - 'type' => MENU_NORMAL_ITEM, - 'file' => 'settings.inc', + 'file' => 'upload_code_delete.inc', ); return $items; @@ -1315,15 +1064,19 @@ function check_name($name = '') { return TRUE; } -function check_chapter_number($name = '') { - if (!preg_match('/^([0-9])+(\.([0-9a-zA-Z])+)+$/', $name)) +function check_code_number($number = '') { + if (!preg_match('/^[0-9]\.[0-9]$/', $number)) return FALSE; else return TRUE; } +function lab_migration_upload_path() { + return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'lab_migration/uploads/'; +} + function lab_migration_path() { - return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'uploads/'; + return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'lab_migration/uploads/'; } /****************************** DELETION FUNCTIONS ****************************/ diff --git a/manage_proposal.inc b/manage_proposal.inc index 10a51f5..641eb1d 100755 --- a/manage_proposal.inc +++ b/manage_proposal.inc @@ -612,22 +612,28 @@ function proposal_edit_form($form_state) } } - if ($proposal_data->solution_provider_uid == 0) + if ($proposal_data->solution_provider_uid == 0) { $solution_provider_user = 1; - else if ($proposal_data->solution_provider_uid == $proposal_data->uid) + } else if ($proposal_data->solution_provider_uid == $proposal_data->uid) { $solution_provider_user = 2; - else + } else { $solution_provider_user = 3; + $user_data = user_load($proposal_data->solution_provider_uid); + if (!$user_data) { + $solution_provider_user = 1; + drupal_set_message('Solution provider user name is invalid', 'error'); + } + } $form['solution_provider_uid'] = array( '#type' => 'radios', '#title' => t('Who will provide the solution'), - '#options' => array('1' => 'Open', '2' => 'Proposer', '3' => 'User id mentioned below'), + '#options' => array('1' => 'Open', '2' => 'Proposer', '3' => 'User name mentioned below'), '#required' => TRUE, '#default_value' => $solution_provider_user, ); - $form['solution_provider_user_id'] = array( + $form['solution_provider_user_name'] = array( '#type' => 'textfield', - '#default_value' => ($solution_provider_user == 3) ? $proposal_data->solution_provider_uid : "", + '#default_value' => ($solution_provider_user == 3) ? $user_data->name : "", ); $form['solution_display'] = array( @@ -657,8 +663,8 @@ function proposal_edit_form($form_state) function proposal_edit_form_validate($form, &$form_state) { if ($form_state['values']['solution_provider_uid'] == 3) { - if (!(user_load((int)$form_state['values']['solution_provider_user_id']))) { - form_set_error('solution_provider_user_id', t('Solution provider user id does not exists')); + if (!user_load(array('name' => check_plain($form_state['values']['solution_provider_user_name'])))) { + form_set_error('solution_provider_user_name', t('Solution provider user name does not exists')); } } } @@ -701,7 +707,8 @@ function proposal_edit_form_submit($form, &$form_state) } else if ($form_state['values']['solution_provider_uid'] == 2) { $solution_provider_uid = $proposal_data->uid; } else if ($form_state['values']['solution_provider_uid'] == 3) { - $solution_provider_uid = (int)$form_state['values']['solution_provider_user_id']; + $user_data = user_load(array('name' => check_plain($form_state['values']['solution_provider_user_name']))); + $solution_provider_uid = (int)$user_data->uid; } $solution_display = 0; @@ -742,12 +749,21 @@ function proposal_edit_form_submit($form, &$form_state) } /* inserting new experiments */ + $number_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE proposal_id = %d ORDER BY number DESC LIMIT 1", $proposal_id); + if ($number_data = db_fetch_object($number_q)) { + $number = (int)$number_data->number; + $number++; + } else { + $number = 1; + } foreach ($form_state['values']['lab_experiment']['insert'] as $insert_id => $insert_value) { if (strlen(trim($insert_value)) >= 1) { - $result = db_query("INSERT INTO {lab_migration_experiment} (proposal_id, title) VALUES (%d, '%s')", $proposal_id, trim($insert_value)); + $result = db_query("INSERT INTO {lab_migration_experiment} (proposal_id, number, title) VALUES (%d, %d, '%s')", $proposal_id, $number, trim($insert_value)); if (!$result) { drupal_set_message(t('Could not insert Title of the Experiment : ') . trim($insert_value), 'error'); + } else { + $number++; } } } diff --git a/upload_code.inc b/upload_code.inc new file mode 100755 index 0000000..7f6201e --- /dev/null +++ b/upload_code.inc @@ -0,0 +1,596 @@ +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'); + 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 = '
'; + $return_html .= 'Title of the Experiment:
' . $proposal_data->lab_title . '

'; + $return_html .= 'Proposer Name:
' . $proposal_data->name_title . ' ' . $proposal_data->name . '

'; + $return_html .= l('Upload Example Code', 'lab_migration/code/upload') . '
'; + + /* get experiment list */ + $experiment_rows = array(); + $experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE proposal_id = %d ORDER BY number ASC", $proposal_data->id); + while ($experiment_data = db_fetch_object($experiment_q)) + { + $experiment_rows[] = array($experiment_data->number, $experiment_data->title, '', ''); + /* get solution list */ + $solution_q = db_query("SELECT * FROM {lab_migration_solution} WHERE experiment_id = %d ORDER BY id ASC", $experiment_data->id); + if ($solution_q) { + while ($solution_data = db_fetch_object($solution_q)) { + $solution_status = ''; + switch ($solution_data->approval_status) { + case 0: $solution_status = "Pending"; break; + case 1: $solution_status = "Approved"; break; + default: $solution_status = "Unknown"; break; + } + $experiment_rows[] = array('', "        " . $solution_data->code_number . " " . $solution_data->caption, $solution_status, l('Delete', 'lab_migration/code/delete/' . $solution_data->id)); + /* get solution files */ + $solution_files_q = db_query("SELECT * FROM {lab_migration_solution_files} WHERE solution_id = %d ORDER BY id ASC", $solution_data->id); + if ($solution_files_q) { + while ($solution_files_data = db_fetch_object($solution_files_q)) { + $experiment_rows[] = array('', "                " . $solution_files_data->filename, '', ''); + } + } + } + } + } + + $experiment_header = array('Exp. Number', 'Title of the Experiment', 'Status', 'Actions'); + $return_html .= theme_table($experiment_header, $experiment_rows); + return $return_html; +} + +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'); + 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 = ''; + /* showing and hiding relevant files */ + $('.form-checkboxes .option').hide(); + $('.form-checkboxes .option').each(function(index) { + var activeClass = $('#edit-existing-depfile-dep-lab-title').val(); + if ($(this).children().hasClass(activeClass)) { + $(this).show(); + } + if ($(this).children().attr('checked') == true) { + dep_selected += $(this).children().next().text() + '
'; + } + }); + /* 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($dep_selection_js, 'inline', 'header'); + + $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'), + ); + + /* get experiment list */ + $experiment_rows = array(); + $experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE proposal_id = %d ORDER BY id ASC", $proposal_data->id); + while ($experiment_data = db_fetch_object($experiment_q)) + { + $experiment_rows[$experiment_data->id] = $experiment_data->number . '. ' . $experiment_data->title; + } + $form['experiment'] = array( + '#type' => 'select', + '#title' => t('Title of the Experiment'), + '#options' => $experiment_rows, + '#multiple' => FALSE, + '#size' => 1, + '#required' => TRUE, + ); + + $form['code_number'] = array( + '#type' => 'textfield', + '#title' => t('Code No'), + '#size' => 5, + '#maxlength' => 10, + '#description' => t(""), + '#required' => TRUE, + ); + $form['code_caption'] = array( + '#type' => 'textfield', + '#title' => t('Caption'), + '#size' => 40, + '#maxlength' => 255, + '#description' => t(''), + '#required' => TRUE, + ); + $form['code_warning'] = array( + '#type' => 'item', + '#title' => t('You should upload all the files (main or source files, result files, executable file if any)'), + '#prefix' => '
', + '#suffix' => '
', + ); + + $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.') . '
' . + t('Allowed file extensions : ') . variable_get('lab_migration_source_extensions', ''), + ); + + $form['dep_files'] = array( + '#type' => 'item', + '#title' => t('Dependency Files'), + ); + + /************ START OF EXISTING DEPENDENCIES **************/ + + /* existing dependencies */ + $form['existing_depfile'] = array( + '#type' => 'fieldset', + '#title' => t('Use Already Existing Dependency Files'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + '#prefix' => '
', + '#suffix' => '
', + '#tree' => TRUE, + ); + + /* existing dependencies */ + $form['existing_depfile']['selected'] = array( + '#type' => 'item', + '#title' => t('Existing Dependency Files Selected'), + '#value' => '
', + ); + + $form['existing_depfile']['dep_lab_title'] = array( + '#type' => 'select', + '#title' => t('Title of the Lab'), + '#options' => _list_of_lab_titles(), + ); + + list($files_options, $files_options_class) = _list_of_dependency_files(); + $form['existing_depfile']['dep_experiment_files'] = array( + '#type' => 'checkboxes', + '#title' => t('Dependency Files'), + '#options' => $files_options, + '#options_class' => $files_options_class, + '#multiple' => TRUE, + ); + + $form['existing_depfile']['dep_upload'] = array( + '#type' => 'item', + '#value' => l('Upload New Depedency Files', 'lab_migration/code/upload_dep'), + ); + /************ END OF EXISTING DEPENDENCIES **************/ + + $form['result'] = array( + '#type' => 'fieldset', + '#title' => t('Result Files'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + ); + $form['result']['result1'] = array( + '#type' => 'file', + '#title' => t('Upload result file'), + '#size' => 48, + '#description' => t('Separate filenames with underscore. No spaces or any special characters allowed in filename.') . '
' . + t('Allowed file extensions : ') . variable_get('lab_migration_result_extensions', ''), + ); + $form['result']['result2'] = array( + '#type' => 'file', + '#title' => t('Upload result file'), + '#size' => 48, + '#description' => t('Separate filenames with underscore. No spaces or any special characters allowed in filename.') . '
' . + t('Allowed file extensions : ') . variable_get('lab_migration_result_extensions', ''), + ); + + $form['xcos'] = array( + '#type' => 'fieldset', + '#title' => t('XCOS Files'), + '#collapsible' => FALSE, + '#collapsed' => FALSE, + ); + $form['xcos']['xcos1'] = array( + '#type' => 'file', + '#title' => t('Upload xcos file'), + '#size' => 48, + '#description' => t('Separate filenames with underscore. No spaces or any special characters allowed in filename.') . '
' . + t('Allowed file extensions : ') . variable_get('lab_migration_xcos_extensions', ''), + ); + $form['xcos']['xcos2'] = array( + '#type' => 'file', + '#title' => t('Upload xcos file'), + '#size' => 48, + '#description' => t('Separate filenames with underscore. No spaces or any special characters allowed in filename.') . '
' . + t('Allowed file extensions : ') . variable_get('lab_migration_xcos_extensions', ''), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') + ); + + $form['cancel'] = array( + '#type' => 'markup', + '#value' => l(t('Cancel'), 'lab_migration/code'), + ); + return $form; +} + +function upload_code_form_validate($form, &$form_state) +{ + if (!check_code_number($form_state['values']['code_number'])) + form_set_error('code_number', t('Invalid Code Number. Code Number can contain only alphabets and numbers sepereated by dot.')); + + if (!check_name($form_state['values']['code_caption'])) + form_set_error('code_caption', t('Caption can contain only alphabets, numbers and spaces.')); + + if (isset($_FILES['files'])) + { + /* check if atleast one source or result file is uploaded */ + if ( ! ($_FILES['files']['name']['sourcefile1'] || $_FILES['files']['name']['xcos1'])) + form_set_error('sourcefile1', t('Please upload atleast one main or source file or xcos file.')); + + /* 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('lab_migration_source_extensions', ''); + break; + case 'R': + $allowed_extensions_str = variable_get('lab_migration_result_extensions', ''); + break; + case 'X': + $allowed_extensions_str = variable_get('lab_migration_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 (!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.')); + } + } + } + + /* 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() { + 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() + '
'; + } + }); + /* 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($dep_selection_js, 'inline', 'header'); +} + +function upload_code_form_submit($form, &$form_state) { + global $user; + + $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'); + 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 ************************/ + $experiment_id = (int)$form_state['values']['experiment']; + $experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE id = %d AND proposal_id = %d LIMIT 1", $experiment_id, $proposal_id); + $experiment_data = db_fetch_object($experiment_q); + if (!$experiment_data) + { + drupal_set_message("Invalid experiment seleted", 'error'); + drupal_goto('lab_migration/code'); + } + + /* create proposal folder if not present */ + $dest_path = $proposal_id . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* get solution details - dont allow if already solution present */ + $cur_solution_q = db_query("SELECT * FROM {lab_migration_solution} WHERE experiment_id = %d AND code_number = '%s'", $experiment_id, $form_state['values']['code_number']); + if ($cur_solution_d = db_fetch_object($cur_solution_q)) + { + if ($cur_solution_d->approval_status == 1) + { + drupal_set_message(t("Solution already approved. Cannot overwrite it."), 'error'); + drupal_goto('lab_migration/code'); + return; + } else if ($cur_solution_d->approval_status == 0) { + drupal_set_message(t("Solution is under pending review. Delete the solution and reupload it."), 'error'); + drupal_goto('lab_migration/code'); + return; + } else { + drupal_set_message(t("Error uploading solution. Please contact administrator."), 'error'); + drupal_goto('lab_migration/code'); + return; + } + } + + /* creating experiment directories */ + $dest_path .= 'EXP' . $experiment_data->number . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* creating code directories */ + $dest_path .= 'CODE' . $form_state['values']['code_number'] . '/'; + if (!is_dir($root_path . $dest_path)) + mkdir($root_path . $dest_path); + + /* creating solution database entry */ + db_query("INSERT INTO {lab_migration_solution} (experiment_id, approver_uid, code_number, caption, approval_date, approval_status, timestamp) VALUES (%d, %d, '%s', '%s', %d, %d, %d)", + $experiment_id, + 0, + $form_state['values']['code_number'], + $form_state['values']['code_caption'], + 0, + 0, + time() + ); + $solution_id = db_last_insert_id('lab_migration_solution', 'id'); + + /* linking existing dependencies */ + foreach ($form_state['values']['existing_depfile']['dep_experiment_files'] as $row) + { + if ($row > 0) + { + /* insterting into database */ + db_query("INSERT INTO {lab_migration_solution_dependency} (solution_id, dependency_id) + VALUES (%d, %d)", + $solution_id, + $row + ); + } + } + + /* uploading files */ + 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'; + + 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 {lab_migration_solution_files} (solution_id, filename, filepath, filemime, filesize, filetype, timestamp) + VALUES (%d, '%s', '%s', '%s', %d, '%s', %d)", + $solution_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 . ', ' . variable_get('lab_migration_emails', ''); + $param['solution_uploaded']['solution_id'] = $example_id; + $param['solution_uploaded']['user_id'] = $user->uid; + if (!drupal_mail('lab_migration', 'solution_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'); +} + +/******************************************************************************/ +/************************** GENERAL FUNCTIONS *********************************/ +/******************************************************************************/ + +function _list_of_lab_titles() +{ + $lab_titles = array('0' => 'Please select...'); + $lab_titles_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE approval_status = 1 OR approval_status = 3 ORDER BY lab_title ASC"); + while ($lab_titles_data = db_fetch_object($lab_titles_q)) + { + $lab_titles[$lab_titles_data->id] = $lab_titles_data->lab_title . ' (Proposed by ' . $lab_titles_data->name . ')'; + } + return $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"); + + 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/upload_code_delete.inc b/upload_code_delete.inc new file mode 100644 index 0000000..5117a1e --- /dev/null +++ b/upload_code_delete.inc @@ -0,0 +1,71 @@ +approval_status != 0) + { + drupal_set_message('You cannnot delete a solution after it has been approved. Please contact site administrator if you want to delete this solution.', 'error'); + drupal_goto('lab_migration/code'); + return; + } + + $experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE id = %d LIMIT 1", $solution_data->experiment_id); + $experiment_data = db_fetch_object($experiment_q); + if (!$experiment_data) + { + drupal_set_message('You do not have permission to delete this solution.', 'error'); + drupal_goto('lab_migration/code'); + return; + } + + $proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE id = %d AND solution_provider_uid = %d LIMIT 1", $experiment_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 solution.', 'error'); + drupal_goto('lab_migration/code'); + return; + } + + /* deleting solution files */ + if (delete_solution($solution_data->id)) + { + drupal_set_message('Solution deleted.', 'status'); + + /* sending email */ + $email_to = $user->mail . ', ' . variable_get('lab_migration_emails', ''); + $param['solution_deleted_user']['book_title'] = $proposal_data->lab_title; + $param['solution_deleted_user']['chapter_title'] = $experiment_data->title; + $param['solution_deleted_user']['example_number'] = $solution_data->number; + $param['solution_deleted_user']['example_caption'] = $solution_data->caption; + $param['example_deleted_user']['user_id'] = $user->uid; + + if (!drupal_mail('lab_migration', 'solution_deleted_user', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } else { + drupal_set_message('Error deleting example.', 'status'); + } + + drupal_goto('lab_migration/code'); + return; +} + +?> -- cgit