diff --git a/include/rawpheno.admin.form.inc b/include/rawpheno.admin.form.inc index 64e2fbf..94eaba4 100644 --- a/include/rawpheno.admin.form.inc +++ b/include/rawpheno.admin.form.inc @@ -688,11 +688,77 @@ function rawpheno_admin_project($form, &$form_state, $project_id) { $project = chado_generate_var('project', array('project_id' => $project_id)); $markup .= '

PROJECT: ' . $project->name . '

'; + // Fetch the genus of this project. + $project_genus = rawpheno_function_project_organism($project_id); + + // When project has no genus set, warn admin. + if (!$project_genus) { + $project_genus = '---'; + $warning = '  Project genus not set. Not set'; + } + else { + $warning = ''; + } + + $markup .= '
GENUS: ' . $project_genus . '
' . $warning; + $form['project_info'] = array( '#type' => 'markup', '#markup' => $markup, ); + // Check if project has data and so, do not allow admin to change genus. + // Otherwise, it's safe, allow admin to change. + $project_has_data = rawpheno_function_data($project_id); + + if ($project_genus == '---' || !$project_has_data) { + // Fetch genus. + // NOTE: saving genus instead of the organism. + $sql = "SELECT genus, UPPER(genus) FROM {organism} GROUP BY genus ORDER BY genus"; + $g = chado_query($sql); + + if ($g->rowCount() > 0) { + $genus = $g->fetchAllKeyed(); + // When project has organism set, remove it from options. + if ($project_genus) { + unset($genus[$project_genus]); + } + + $options = array('' => '---') + $genus; + + // Not set, show the form. + $collapse = (!$project_genus) ? FALSE : TRUE; + + $form['fieldset_update_genus'] = array( + '#type' => 'fieldset', + '#title' => t('Update Genus'), + '#collapsed' => $collapse, + '#collapsible' => TRUE, + ); + + // List genus - select box to select genus the project is collecting data for/to. + $form['fieldset_update_genus']['sel_genus'] = array( + '#type' => 'select', + '#title' => t('Project is collecting data for genus:'), + '#options' => $options, + '#default_value' => array_keys($options)[0], + '#id' => 'admin-sel-genus' + ); + + $form['fieldset_update_genus']['save_genus'] = array( + '#type' => 'submit', + '#value' => t('Update genus'), + '#validate' => array('rawpheno_admin_validate_update_genus'), + '#submit' => array('rawpheno_admin_submit_update_genus'), + '#limit_validation_errors' => array( + array('sel_genus') + ) + ); + + $form['#project_id'] = $project_id; + } + } + // Arguments to all SQL queries below. $args = array(':project_id' => $project_id); @@ -1901,6 +1967,72 @@ function rawpheno_admin_user_assets($asset_id) { } +/** + * Function callback: Update project genus. + */ +function rawpheno_admin_submit_update_genus($form, $form_state) { + $project_id = $form['#project_id']; + + // Form field values. + $genus = $form_state['values']['sel_genus']; + // Ensure that only the first letter is capitalized. + $genus = ucfirst($genus); + + // Get genus/organism controlled vocabulary term. + // Term. + $term = rawpheno_function_organism_cv(); + $genus_cvterm = tripal_get_cvterm($term); + + // Locate project in projectprop. + $sql = "SELECT projectprop_id FROM {projectprop} WHERE project_id = :project_id AND type_id = :organism_cvterm_id"; + $args = array(':project_id' => $project_id, ':organism_cvterm_id' => $genus_cvterm->cvterm_id); + + $g = chado_query($sql, $args); + + if ($g->rowCount() > 0) { + // No record found. + // Update is an insert row command. + // Final check before updating. Should have no data. + $project_has_data = rawpheno_function_data($project_id); + + if (!$project_has_data) { + db_update('chado.projectprop') + ->fields(array( + 'value' => $genus + ) + ) + ->condition('project_id', $project_id, '=') + ->condition('type_id', $genus_cvterm->cvterm_id, '=') + ->execute(); + } + } + else { + // Appears the project has an entry in the projectprop table. + // Update is an update row command. + db_insert('chado.projectprop') + ->fields(array( + 'project_id' => $project_id, + 'type_id' => $genus_cvterm->cvterm_id, + 'value' => $genus, + 'rank' => 0, + ) + ) + ->execute(); + } +} + + +/** + * Function callback: Validate update genus. + */ +function rawpheno_admin_validate_update_genus($form, &$form_state) { + $genus = $form_state['values']['sel_genus']; + if (empty($genus)) { + form_set_error('sel_organism', t('No genus selected. Please select genus and try again')); + } +} + + function rawpheno_admin_mark_empty($val) { return (empty($val)) ? '-' : $val; } diff --git a/include/rawpheno.upload.excel.inc b/include/rawpheno.upload.excel.inc index cdbd10b..1217d42 100755 --- a/include/rawpheno.upload.excel.inc +++ b/include/rawpheno.upload.excel.inc @@ -515,6 +515,10 @@ function rawpheno_validate_excel_file($file, $project_id, $source) { // Change to the correct spreadsheet. rawpheno_change_sheet($xls_obj, 'measurements'); + // Limit the stocks for comparison/validation to just the organism that matched the project genus. + $project_prop = rawpheno_function_project_organism($project_id, 'full'); + $project_genus_organism_ids = array_keys($project_prop['organism']); + // This increment variable $i is required since xls and xlsx // parsers assign array index differently. // XLS starts at 1, while XLSX at 0; @@ -616,7 +620,7 @@ function rawpheno_validate_excel_file($file, $project_id, $source) { $context['plot_req'] = $plot_req; } - $result = $validator['validation callback']($cell, $context, $tmp_storage); + $result = $validator['validation callback']($cell, $context, $tmp_storage, $project_genus_organism_ids); // Note: we use tmp storage b/c passing $storage[$validator_name] directly // doesn't seem to work. diff --git a/include/rawpheno.validation.inc b/include/rawpheno.validation.inc index a38bf3b..c8ae6c1 100644 --- a/include/rawpheno.validation.inc +++ b/include/rawpheno.validation.inc @@ -639,14 +639,19 @@ function validator_germplasm_present_generate_msg($error_info) { * 0 = no matching stock. * 9 = too many matching stocks (not unique). */ -function validator_germplasm_present_validate_cell($value, $context, &$storage) { +function validator_germplasm_present_validate_cell($value, $context, &$storage, $project_genus_organism_ids) { // We don't want this test to catch empty cells. There is a separate test for testing required values are resent. if (empty($value)) { return TRUE; } - $stocks = chado_query('SELECT stock_id FROM {stock} WHERE name=:name', array(':name' => $value))->fetchAll(); + // Limit the stocks for comparison/validation to just the organism defined in the project. + $sql = "SELECT stock_id FROM {stock} WHERE name = :name AND organism_id IN (:project_organism_ids)"; + $args = array(':name' => $value, ':project_organism_ids' => $project_genus_organism_ids); + + $stocks = chado_query($sql, $args) + ->fetchAll(); if (sizeof($stocks) == 1) { return TRUE; diff --git a/rawpheno.module b/rawpheno.module index 54b42d0..38e2b93 100755 --- a/rawpheno.module +++ b/rawpheno.module @@ -258,7 +258,7 @@ function rawpheno_permission() { function rawpheno_preprocess_rawpheno_rawdata(&$variables, $hook) { // Colour scheme, project check and data check. $rawpheno_load = rawpheno_module_defaults(); - list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load; + list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data'], $variables['has_genus']) = $rawpheno_load; // Cross-link rawdata page to download page. $variables['rel_url'] = url('phenotypes/raw/download');