Tripal 0.3b
tripal_stock/tripal_stock-administration.inc
Go to the documentation of this file.
00001 <?php
00002 // $Id$
00003 
00004 /**
00005  * Purpose: Provide Guidance to new Tripal Admin
00006  *
00007  * @return
00008  *   HTML Formatted text
00009  *
00010  * @ingroup tripal_stock
00011  */
00012 function tripal_stock_module_description_page() {
00013   $text = '';
00014   
00015   $text = '<h3>Tripal Stock Administrative Tools Quick Links</h3>';
00016     $text .= '<ul>';
00017       $text .= '<li>'.l('Configuration', 'admin/tripal/tripal_stock/configuration').'</li>';
00018       $text .= '<li>'.l('Stock Listing', 'stocks').'</li>';
00019     $text .= '</ul>';
00020     
00021   $text .= '<h3>Module Description:</h3>';
00022   //================================================================================
00023   $text .= '<p>The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado stocks. The stock module was designed to store information about stock collections in a laboratory. What is called a stock could also be called a strain or an accession. There is a lot in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all come from some taxon, have genotypes, physical locations in the lab, some conceivable relationship with a publication, some conceivable relationship with a sequence feature (such as a transgene), and could be described by some ontology term. For more information about the chado Stock Module <a href="http://gmod.org/wiki/Chado_Stock_Module">see the GMOD Wiki Page</a></p>';
00024   
00025   $text .= '<h3>Setup Instructions:</h3>';
00026   //================================================================================
00027   $text .= '<ol>';
00028   $text .= '<li><b>Set Ontologies</b>: Since at the time of this modules developement there is no accepted ontology for 
00029             describing stocks, their properties and relationships, this module allows you to select the controlled 
00030             vocabularies (CVs) in your Chado Database you would like to govern these data. To Set the Controlled Vocabularies for Stocks: 
00031             First, ensure your Controlled Vocabulary is in Chado. This can be done by either loading an existing Ontology into Chado using 
00032             the <a href="tripal_cv/ontology_loader">Tripal Ontology Loader</a> OR create your ontology from scratch by first 
00033             <a href="tripal_cv/add_cv">creating a controlled vocabulary</a> and then <a href="tripal_cv/add_cvterm">adding terms to it</a>.
00034             Then go to the <a href="tripal_stock/configuration">Configuration Page for Stocks</a> and, in the "Set Stock Controlled Vocabularies" Fieldset, 
00035             select the Controlled Vocaulary name for Stock Types, Stock Properties and Stock Relationship Types.</li>';
00036       $text .= '<ol type="i">';
00037         $text .= '<li>Stock Types: When you are creating stocks, the type of each stock must be indicated. This might include "DNA extraction", "Individual Plant/Animal" or even "Progeny Population".</li>';
00038         $text .= '<li>Stock Properties: This module also allows you to assign properties to any stock. Each property has a type and a value where type is required an value is not. Therefore, if you want to say that a stock was grown at 23 degrees Celcius then the Type would be "temperature grown at" and the value would be 23 degrees Celcius. As such the Stock Properties controlled vocabulary might include "temperature grown at", "diet", "extraction date", "stock location", etc.</li>';
00039         $text .= '<li>Stock Relationship Types: You can also specify relationships between stocks. For example, a stock of type="DNA extraction" (Stock 1a) is related to the stock of type="Individual Plant/Animal" (Stock 1) that it was extracted from. Thus you might specify the relationship Stock 1 is the source material for Stock 1a where the relationship type is "is the source material for". As such Stock Relationship Types might include "is the source material for", "is maternal parent of", "is individual of population", etc.</li>';
00040       $text .= '</ol>';
00041   
00042   $text .= '<li><p><b>Set Permissions</b>: The stock module supports the Drupal user permissions interface for 
00043                controlling access to stock content and functions. These permissions include viewing, 
00044                creating, editing or administering of
00045                stock content. The default is that only the original site administrator has these 
00046                permissions.  You can <a href="'.url('admin/user/roles').'">add roles</a> for classifying users, 
00047                <a href="'.url('admin/user/user').'">assign users to roles</a> and
00048                <a href="'.url('admin/user/permissions').'">assign permissions</a> for the stock content to 
00049                those roles.  For a simple setup, allow anonymous users access to view organism content and 
00050                allow the site administrator all other permissions.</p></li>';
00051   
00052   $text .= '<li><b>Sync Stocks</b>: if you chado database already contains stocks, they need to be sync\'d with Drupal</b>. This creates Drupal Content including detail pages for each stock (known as nodes in Drupal). To sync\' Chado with Drupal simply go to the <a href="tripal_stock/configuration">Configuration Page for Stocks</a> and in the "Sync Stocks" Fieldset select the Organisms whose associated stocks you would like to sync. If this list doesn\'t contain an organism which you know is in Chado go to the Organism Configuration Page and make sure it is sync\'d with Drupal.</p>';
00053   $text .= '</ol>';  
00054   $text .= '<h3>Features of this Module:</h3>';
00055   //================================================================================
00056   $text .= '<ul>';
00057   $text .= '<li><b><a href="../../node/add/chado_stock">Create a Generic Stock:</a></b>';
00058   $text .= '<p>This allows you to create content in your drupal and chado for a stock (only the unique stock identifier is duplicated). A Generic Stock must have a unique name, a type and an organism. In addition, you can optionally supply a more human-readable name, a primary database reference and even a short description. The Create Generic Stock form is a multistep form with the first step creating the Basic stock (stored in the stock table). All the remaining steps are optional and descriptions of each follow:</p>';
00059       $text .= '<ol type="i">';
00060         $text .= '<li>The Next Step is to Add Properties to the newly created stock. Properties allow you to specify additional details about a given stock. Since the types of properties you can add are goverened by a controlled vocaulary that you can create, you have complete control over what additional properties you want to allow.</li>';
00061         $text .= '<li>Then you can Add External Database References. A Database Reference can be thought of as a synonym for the current stock where you want to specify a source for that synonym. The source would then be thought of as the database where a database can either be online and provide automatic linking out to the synonymous record or offline and simply be a text name of the source. To create a database reference with a given source you must first add the database to chado <a href="tripal_db/add_db">here</a>.</li>';
00062         $text .= '<li>Finally you can Add Relationships between Stocks. This allows you to specify, for example, the source material of a stock or one of it\'s parents. To create a relationship between your newly added stock and another stock, the other stock must first be created as this one was. Also, since the types of relationships is governed by a controlled vocabulary, just like with properties you have complete control over which relationships you want to allow. Once you click "Finish" you will be re-directed to the Details Page of the new Stock.</li>';
00063       $text .= '</ol></li>';
00064       
00065   $text .= '<li><b>Details Page of a Stock:</b>';
00066   $text .= '<p>Each stock get\'s it\'s own page on this website. This page is meant to give an overall picture of the stock including listing the basic details, as well as, all properties, database references and relationships. To understand where it is -All page content in Drupal is known as a node and is given a unique identifier or nid. Thus every drupal page has a path of node/<nid>. You can get to the Details page for a given stock from either of the stock listings described below.</p>';
00067   $text .= '<p>If you want to customize the look of the stock Details page simply copy the PHP/HTML template node-chado_stock.tpl.php from theme_tripal to the base theme you are currently using. Then edit it as desired. There are plans to integrate this details page with Drupal Panels which will provide a much more user-friendly and no-programming-needed method to customize this page.</p>';
00068   
00069   $text .= '<li><b>Adding/Updating/Deleting Stocks and their Properties, Database References and Relationships:</b>';
00070   $text .= '<p>The Stock Details Page also acts as a landing pad for updating/deleting stocks. To <b>update a stock</b>, go to the stocks details page and click on the Edit tab near the top of the page. This tab will only be visable if you have permission to edit chado stock content (See post installation steps above for information on setting user permissions). If you want to <b>delete a stock</b>, click the Edit tab and then near the bottom of the form, click the Delete button. This will delete the entire stock including it\'s properties, database references and any relationships including it.</p>';
00071   $text .= '<p>To <b>update/delete a given property of a stock</b>, click the "Edit Properties" Tab near the top of the stock details page. This form provides a listing of all existing properties for the given stock with form elements allowing you to change their value. After editing the properties you wanted to, simply click the "Update Properties" button to update all the properties for that stock. To delete a given property simply click the "Delete" Button beside the property you want to delete. You cannot undo this action! To <b>add a property to the given stock</b> simply fill out the "Add Property" form at the bottom of the "Edit Properties" Tab.</p>';
00072   $text .= '<p><b>Adding, updating and deleting Database References and Relationships</b> for a given stock is exactly the same as the method for properties. To edit Database References, click the "Edit DB References" tab and to add/edit/update stock relationships, click the "Edit Relationships" tab.</p></li>';
00073   
00074   $text .= '<li><b><a href="../../stocks">Basic Listing of Stocks:</a></b>';
00075   $text .= '<p>This module also provides a basic listing of all stocks currently sync\'d with Drupal. To access this listing, there should be a Stocks Primary Menu item which links you to <a href="../../stocks">this page</a>. This page lists each stock on it\'s own row and provides a link to each stock by clicking on it\'s name. Currently there is no way to easily customize this listing.</p></li>';
00076   
00077   $text .= '<li><b><a href="../build/views/">Flexible Listing of Stocks using Drupal Views:</a></b>';
00078   $text .= '<p>In order to access a more flexible listing of stocks you must first install the <a href="http://drupal.org/project/views">Drupal Views2 module</a>. You should then be able to access the default views <a href="../build/views/">here</a>. Essentially, Views is a module which allows you to create custom SQL queries completely through the web interface without knowing SQL. Furthermore, it also does some formatting of the results allowing you to display them as HTML lists, tables or grids. You can also expose filters to the user to let them customize the results they see and even implement various sorting.</p>';
00079   $text .= '<p>To use one of the Default Views simply click "Enable" and then "Edit" to change it to show exactly what you want. To view the current listing simply clikc "View Page" at the top of the Edit user interface. There are a number of good tutorials out there for Views2, any of which can be used to help you create your own custom listings of biological content. (Note: there aren\'t any tutorials specifically for tripal content but any tutorial for Views2 will show you how to use the views interface.</p></li>';
00080 
00081    $text .= '<h3>Page Customizations</h3>';
00082    $text .= '<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal. 
00083              Below is a description of several methods.  These methods may be used in conjunction with one another to
00084              provide fine-grained control. 
00085              <ul>
00086 
00087              <li><p><b>Integration with Drupal Panels</b>:  <a href="http://drupal.org/project/views">Drupal Panels</a> 
00088               allows for customization of a page layout if you don\'t want to do PHP/Javascript/CSS programming.  Tripal comes with pre-set layouts for stock pages.  However, 
00089               Panels become useful if you prefer a layout that is different from the pre-set layouts.  Chado content
00090               is provided to Panels in the form of Drupal "blocks" which you can then place anywhere on a page using the 
00091               Panel\'s GUI.</p></li>
00092 
00093              <li><p><b>Drupal\'s Content Construction Kit (CCK)</b>: the 
00094              <a href="http://drupal.org/project/cck">Content Construction Kit (CCK) </a> is a powerful way to add non-Chado content
00095              to any page without need to edit template files or knowing PHP.  You must first download and install CCK.
00096              With CCK, the site administartor can create a new field to appear on the page.  For example, currently,
00097              the Chado publication module is not yet supported by Tripal.  Therefore, the site administrator can add a text 
00098              field to the stock pages.  This content is not stored in Chado, but will appear on the stock page.  A field
00099              added by CCK will also appear in the form when editing a stock to allow users to manually enter the appropriate
00100              text.  If the default pre-set layout and themeing for Tripal is used, it is better to create the CCK element,
00101              indicate that it is not to be shown (using the CCK interface), then manually add the new content type 
00102              where desired by editing the templates (as described below).  If using Panels, the CCK field can be added to the
00103              location desired using the Panels interface.</p></li>
00104 
00105              <li><p><b>Drupal Node Templates</b>:  The Tripal packages comes with a "theme_tripal" directory that contains the
00106              themeing for Chado content.    The stock module has a template file for stock "nodes" (Tripal stock pages).  This file
00107              is named "node-chado_stock.tpl.php", and provides javascript, HTML and PHP code for display of the stock
00108              pages.  You can edit this file to control which types of information (or which stock "blocks") are displayed for stocks. Be sure to 
00109              copy these template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
00110              future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
00111              for instructions on how to access variables and other Chado content within the template file.</p></li>
00112 
00113              <li><p><b>Stock "Block" Templates</b>:  In the "theme_tripal" directory is a subdirectory named "tripal_stock".
00114              Inside this directory is a set of templates that control distinct types of information for stocks.  For example,
00115              there is a "base" template for displaying of data directly from the Chado stock table, and a "references" 
00116              template for showing external site references for a stock (data from the stock_dbxref table).  These templates are used both by Drupal blocks
00117              for use in Drupal Panels (as described above) or for use in the default pre-set layout that the node template 
00118              provides (also desribed above).  You can customize this template as you desire.  Be sure to copy the
00119              template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
00120              future Tripal updates may overwrite your customizations.  See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
00121              for instructions on how to access variables and other Chado content within the template files.</p></li>
00122              </li>
00123 
00124              <li><p><b>Adding Links to the "Resources" Sidebar</b>: If you use the pre-set default Tripal layout for theming, you
00125              will see a "Resources" sidebar on each page.  The links that appear on the sidebar are automatically generated
00126              using Javascript for all of the stock "Blocks" that appear on the page. If you want to add additional links 
00127              (e.g. a dynamic link to additional stock content) and you want that link to appear in the 
00128              "Resources" sidebar, simply edit the Drupal Node Template (as described above) and add the link to the 
00129              section at the bottom of the template file where the resources section is found.</p></li>
00130 
00131              </ul>
00132              </p>';  
00133   
00134   return $text;
00135 }
00136 
00137 /**
00138  * Purpose: Provide administration options for chado_stocks
00139  *
00140  * @return
00141  *   Form array (as described by the drupal form api)
00142  *
00143  * @ingroup tripal_stock
00144  */
00145 function tripal_stock_admin() {
00146   $form = array();
00147 
00148    // before proceeding check to see if we have any
00149    // currently processing jobs. If so, we don't want
00150    // to give the opportunity to sync Stocks
00151    $active_jobs = FALSE;
00152    if(tripal_get_module_active_jobs('tripal_stock')){
00153       $active_jobs = TRUE;
00154    }
00155    if($active_jobs){
00156    
00157    $form['notice'] = array(
00158          '#type' => 'fieldset',
00159          '#title' => t('Stock Management Temporarily Unavailable')
00160       );
00161 
00162    $form['notice']['message'] = array(
00163          '#value' => t("Currently, stock management jobs are waiting or ".
00164             "are running. Managemment features have been hidden until these ".
00165             "jobs complete.  Please check back later once these jobs have ".
00166             "finished.  You can view the status of pending jobs in the Tripal ".
00167             "jobs page."),
00168       );
00169 
00170    } else {
00171 
00172    // SET Vocabularies -----------------------------------------------------------------------------------------
00173    $form['set_cv'] = array(
00174       '#type' => 'fieldset',
00175       '#title' => t('Set Stock Controlled Vocabularies'),
00176       '#weight' => -10
00177    );   
00178    
00179    $form['set_cv']['message'] = array(
00180          '#value' => t("This setting allows you to set which chado controlled vocabularies (cv)"
00181                 ." are used. Cvs are used to control user input for the type of stock,"
00182           ." any properties they enter for a stock & the types of relationships"
00183           ." between stocks. Only cvs already loaded into chado can be selected here.")
00184    );
00185 
00186    $cv_options = tripal_cv_get_cv_options();
00187    
00188    $form['set_cv']['stock_types_cv'] = array(
00189      '#type' => 'select',
00190      '#title' => t('Controlled Vocabulary governing Stock Types'),
00191      '#options' => $cv_options,
00192      '#default_value' => variable_get('chado_stock_types_cv', 0)
00193    );
00194 
00195    $form['set_cv']['stock_prop_types_cv'] = array(
00196      '#type' => 'select',
00197      '#title' => t('Controlled Vocabulary governing Types of Stock Properties'),
00198      '#description' => t("This cv must contain a cvterm entry where name='synonym'."),
00199      '#options' => $cv_options,
00200      '#default_value' => variable_get('chado_stock_prop_types_cv', 0)
00201    );
00202 
00203    $form['set_cv']['stock_relationship_cv'] = array(
00204      '#type' => 'select',
00205      '#title' => t('Controlled Vocabulary governing Types of Relationsips between Stocks'),
00206      '#options' => $cv_options,
00207      '#default_value' => variable_get('chado_stock_relationship_cv', 0)
00208    );
00209 
00210    $form['set_cv']['button'] = array(
00211       '#type' => 'submit',
00212       '#value' => t('Set Controlled Vacabularies')
00213    );
00214 
00215    // SYNC STOCKS-----------------------------------------------------------------------------------------------
00216    $form['sync'] = array(
00217       '#type' => 'fieldset',
00218       '#title' => t('Sync Stocks'),
00219       '#weight' => -10
00220    );
00221 
00222    $form['sync']['description'] = array(
00223       '#type' => 'item',
00224       '#value' => t("Click the 'Sync all Germplasm' button to create Drupal ".
00225          "content for stocks in chado. Depending on the ".
00226          "number of stocks in the chado database this may take a long ".
00227          "time to complete. ")
00228    );
00229 
00230    $form['sync']['organisms'] = array(
00231       '#type' => 'checkboxes',
00232       '#title' => t('Organisms for which Stocks should be sync\'d'),
00233       '#description' => t('Only sync\'d Organisms are listed. Leaving an organism unchecked does not delete already sync\'d Stocks.'),
00234       '#options' => tripal_organism_get_organism_options(),
00235       '#required'    => FALSE,
00236       '#prefix'      => '<div id="lib_boxes">',
00237       '#suffix'      => '</div>'
00238    );
00239 
00240    $form['sync']['button'] = array(
00241       '#type' => 'submit',
00242       '#value' => t('Sync Stocks')
00243    );
00244 }
00245 
00246   return system_settings_form($form);
00247 
00248 }
00249 
00250 /**
00251  * Implements hook_form_validate(): Validates user input
00252  *
00253  * @param $form
00254  *   An array describing the form that was rendered
00255  * @param $form_state
00256  *   An array describing the current state of the form including user input
00257  *
00258  * @ingroup tripal_stock
00259  */
00260 function tripal_stock_admin_validate($form, &$form_state) {
00261    global $user;  // we need access to the user info
00262    $job_args = array();
00263 
00264    // Sync Stocks
00265    if ($form_state['values']['op'] == t('Sync Stocks')) {
00266       // Array organism_id => organims common_name
00267       //  which only includes those organisms which the user wants to select stocks for
00268       $organisms_2b_syncd = $form_state['values']['organisms'];
00269 
00270       //for each organism selected submit job (handled by tripal_stock_sync_stock_set)
00271       //  which syncs all stocks with an organism_id equal to the selelcted organism
00272       foreach ( $organisms_2b_syncd as $organism_id ) {
00273         if($organism_id != 0) {
00274           $job_args[0] = $organism_id;
00275           tripal_add_job("Sync Stocks from Organism $organism_id",'tripal_stock',
00276             'tripal_stock_sync_stock_set',$job_args,$user->uid);
00277         }
00278       }
00279     }
00280 
00281     if ($form_state['values']['op'] == t('Set Controlled Vacabularies')) {
00282       variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
00283       variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
00284       variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
00285     }
00286 }
00287 
00288 /** 
00289  * Syncs all Stocks associated with an organism
00290  *
00291  * Note: Handling of multiple organisms is done in tripal_stock_admin_validate()
00292  *
00293  * @param $organism_id
00294  *   The chado primary key of the organism for which stocks should be sync'd
00295  * @param $job_id
00296  *   The tripal job ID
00297  *
00298  * @return
00299  *   TRUE if successful; FALSE otherwise
00300  *
00301  * @ingroup tripal_stock
00302  */
00303  function tripal_stock_sync_stock_set($organism_id, $job_id) {
00304   global $user;
00305 
00306   if(!$organism_id) {
00307     print '0 Stocks to Sync -No Organisms Selected.\n';
00308   } else {
00309 
00310     // Get list of stocks to sync
00311     $previous_db = tripal_db_set_active('chado');
00312     $result = db_query(
00313        "SELECT stock_id, uniquename, type_id, organism_id FROM stock WHERE organism_id=%d",
00314        $organism_id
00315     );
00316     tripal_db_set_active($previous_db);
00317 
00318     $stocks_created_count = 0; //keeps track of total number of stocks successfully created
00319     $stocks_attempted = 0;
00320     // foreach stock to be sync'd -> create node & add stock_id
00321     while ( $r = db_fetch_object($result) ) {
00322       // $r is the current stock to be sync'd
00323       $stocks_attempted++;
00324 
00325       print 'Processing '.$r->uniquename."\n";
00326 
00327       // check not already in drupal
00328       $in_drupal_query = db_query(
00329         "SELECT * FROM {chado_stock} WHERE stock_id=%d",
00330         $r->stock_id
00331       );
00332       if ( !db_fetch_object($in_drupal_query) ) {
00333 
00334         //create new chado_stock node
00335         $new_node = new stdClass();
00336         $new_node->type = 'chado_stock';
00337         $new_node->uid = $user->uid;
00338         $new_node->title = $r->uniquename;
00339         $new_node->type_id = $r->type_id;
00340         $new_node->organism_id = $r->organism_id;
00341         $new_node->stock_id = $r->stock_id;
00342         //print 'New Node:';
00343         //print_r($new_node);
00344         
00345         node_validate($new_node);
00346         
00347         if(!form_get_errors()){
00348           //print 'Try to Create Node ';
00349           $node = node_submit($new_node);
00350           node_save($node);
00351           if($node->nid){
00352             $stocks_created_count++;
00353    
00354             //Add stock id to chado_stock table
00355             db_query(
00356               "UPDATE {chado_stock} SET stock_id=%d WHERE nid=%d AND vid=%d",
00357               $r->stock_id,
00358               $node->nid,
00359               $node->vid
00360             );
00361           }
00362         } else {
00363           print "\tCreate Stock Form Errors: ";
00364           print_r(form_get_errors());
00365         }
00366         print "\n\tNid=".$node->nid."\n";
00367       } else {
00368         print "\tSkipped $r->uniquename because it's already in drupal.\n";
00369       } //end of if not already in drupal
00370     } //end of while still stocks to be sync'd
00371   } //end of if organism_id not supplied
00372    
00373   if ($stocks_attempted == 0) {
00374     print "No stocks retrieved for organism (".$organism_id.")\n";
00375     return 1;
00376   } else {
00377     if ($stocks_created_count > 0) {
00378       print "$stocks_created_count Stocks Successfully Created\n";
00379       return 1;
00380     } else {
00381       return 0;
00382     }
00383   }
00384 }
 All Classes Files Functions Variables