Skip to content

Commit

Permalink
Limit stock to genus in validation and allow project to change organi…
Browse files Browse the repository at this point in the history
…sm - Issue #12 Stock name lookup

- Allow admin to alter genus only when project has no data associated
to it.

- Limit stock validation to just the stocks in a given genus.
  • Loading branch information
reynoldtan committed May 26, 2017
1 parent 92298c6 commit 6f3e83b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
132 changes: 132 additions & 0 deletions include/rawpheno.admin.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,77 @@ function rawpheno_admin_project($form, &$form_state, $project_id) {
$project = chado_generate_var('project', array('project_id' => $project_id));
$markup .= '<h2>PROJECT: ' . $project->name . '</h2>';

// 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 = '&nbsp;&nbsp;<img src="../../../../../../../misc/message-16-warning.png" title="Project genus not set." alt="Project genus not set." /> Not set';
}
else {
$warning = '';
}

$markup .= '<div class="tag-name">GENUS: ' . $project_genus . '</div>' . $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);

Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion include/rawpheno.upload.excel.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions include/rawpheno.validation.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion rawpheno.module
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6f3e83b

Please sign in to comment.