tripal.registration.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to the display of Tripal registration
  5. * form in a Tripal website.
  6. */
  7. /**
  8. * Provides the page for the Tripal registration page
  9. *
  10. */
  11. function tripal_registration_form($form, &$form_state) {
  12. $form_data = unserialize(variable_get('tripal_site_registration', NULL));
  13. $form_state['details']['funding'] =
  14. isset($form_state['details']['funding']) ? $form_state['details']['funding'] : 1;
  15. $form['description'] = [
  16. '#title' => 'Why Register your Site?',
  17. '#type' => 'item',
  18. '#markup' => t('Registering your site is important for continued improvements to the software. You may opt-in by providing answers to the
  19. following questions or opt-out by checking the box below. If you opt-in, your site will periodically
  20. connect to the http://tripal.info website and provide updated registration details. If you opt-out, no
  21. registration information is communictated. You can opt-out at any time. If you want previously submitted information
  22. deleted from the tripal.info database please email !admin.', ['!admin' => l('admin@tripal.info', 'mailto:admin@tripal.info')])
  23. ];
  24. $form['usage_details'] = [
  25. '#title' => 'How will this data be used?',
  26. '#type' => 'item',
  27. '#markup' => t('Tripal is open-source, freely-available, but
  28. dependent on external funding. When you register your site, it provides important details that can
  29. be used to justify continued support from funding agencies. The information provided will not be shared publically.
  30. Information about the Tripal modules installed on your site will be used to help justify continued development to
  31. funding agencies. Registration details may be shared with members of Tripal\'s Project Management Committee (PMC) and
  32. Tripal\'s Steering Committee (TSC) and Tripal extension module usage may be shared with developers
  33. of the the extension modules to aid in their funding requests.'),
  34. ];
  35. $form['disable_tripal_reporting'] = array(
  36. '#type' => 'checkbox',
  37. '#title' => t('Do not register this site (opt-out).'),
  38. '#default_value' => isset($form_data['values']['disable_tripal_reporting']) ? $form_data['values']['disable_tripal_reporting'] : NULL,
  39. '#description' => "If you do not want to register your site please check
  40. this box as it will stop the reminder notifications. You can return later and register at any time.",
  41. '#ajax' => array(
  42. 'callback' => '_tripal_form_disable_reg_callback',
  43. 'event' => 'click',
  44. ),
  45. );
  46. $purpose = array(0 => t('Production'), 1 => t('Development'), 2 => t('Experimental'));
  47. $form['details']['tripal_reg_site_purpose'] = array(
  48. '#type' => 'radios',
  49. '#title' => t('Site Status'),
  50. '#default_value' => isset($form_data['values']['tripal_reg_site_purpose']) ? $form_data['values']['tripal_reg_site_purpose'] : NULL,
  51. '#options' => $purpose,
  52. '#required' => FALSE,
  53. '#description' => t('Please register your site regardless if it is experimental (to explore tripal),
  54. for development of a future site (or updates to an existing site), or a site currently
  55. in production. For funding, it is important to know how many sites are active for each category. If your site changes
  56. status, such as from development to production, please remember to return and update the purpose.')
  57. );
  58. $form['details']['tripal_reg_site_modules'] = array(
  59. '#type' => 'checkbox',
  60. '#default_value' => isset($form_data['values']['tripal_reg_site_modules']) ? $form_data['values']['tripal_reg_site_modules'] : 1,
  61. '#title' => t('Report your installed Tripal Extensions.'),
  62. '#description' => t('When checked, any Tripal extension modules that you have installed will be reported with your site\'s registration information.')
  63. );
  64. $form['details']['tripal_reg_site_description']= array(
  65. '#type' => 'textarea',
  66. '#title' => t('Description of the site'),
  67. '#default_value' => isset($form_data['values']['tripal_reg_site_description']) ? $form_data['values']['tripal_reg_site_description'] : NULL,
  68. '#required' => FALSE,
  69. '#description' => t('Please provide a brief description of this site. Consider including
  70. details such as its purpose, the primary data types your site provides, and the
  71. research community your site serves.')
  72. );
  73. $form['details']['principal_investigator'] = array(
  74. '#type' => 'fieldset',
  75. '#title' => t('Principal Investigator Contact Information'),
  76. '#collapsible' => TRUE,
  77. '#collapsed' => FALSE,
  78. '#description' => t('Please provide the name and email of this site\'s principal
  79. investigator (PI). If the name and email are provided then the PI agrees to
  80. receive periodic communication from either the Tripal Advisory Committee (TAC) or
  81. Project Management Committee (PMC) for the purposes of engaging with the larger
  82. Tripal user community. The PI will NOT be automatically subscribed to mailing lists.')
  83. );
  84. $form['details']['principal_investigator']['principal_investigator_name'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Name'),
  87. '#default_value' => isset($form_data['values']['principal_investigator_name']) ? $form_data['values']['principal_investigator_name'] : NULL,
  88. '#required' => FALSE,
  89. );
  90. $form['details']['principal_investigator']['principal_investigator_email'] = array(
  91. '#type' => 'textfield',
  92. '#title' => t('Email'),
  93. '#default_value' => isset($form_data['values']['principal_investigator_email']) ? $form_data['values']['principal_investigator_email'] : NULL,
  94. '#required' => FALSE,
  95. );
  96. $form['details']['tripal_reg_site_admin'] = array(
  97. '#type' => 'fieldset',
  98. '#title' => t('Site Manager (if different from the principal investigator)'),
  99. '#collapsible' => TRUE,
  100. '#collapsed' => TRUE,
  101. '#description' => t('Please provide the name and email of this site\'s manager if
  102. different from the PI. Sometimes, site managers desire involvement in community
  103. activites as well as the PI. If the name and email are provided then the site manager agrees to
  104. receive periodic communication from either the Tripal Advisory Committee (TAC) or
  105. Project Management Committee (PMC) for the purposes of engaging with the larger
  106. Tripal user community. The site manager will NOT be automatically subscribed to mailing lists.')
  107. );
  108. $form['details']['tripal_reg_site_admin']['tripal_reg_site_admin_name'] = array(
  109. '#type' => 'textfield',
  110. '#title' => t('Name'),
  111. '#default_value' => isset($form_data['values']['tripal_reg_site_admin_name']) ? $form_data['values']['tripal_reg_site_admin_name'] : NULL,
  112. '#required' => FALSE,
  113. );
  114. $form['details']['tripal_reg_site_admin']['tripal_reg_site_admin_email'] = array(
  115. '#type' => 'textfield',
  116. '#title' => t('Email'),
  117. '#default_value' => isset($form_data['values']['tripal_reg_site_admin_email']) ? $form_data['values']['tripal_reg_site_admin_email'] : NULL,
  118. '#required' => FALSE,
  119. );
  120. $form['details']['funding'] = array(
  121. '#type' => 'container',
  122. '#tree' => TRUE,
  123. '#prefix' => '<div id="funding">',
  124. '#suffix' => '</div>',
  125. );
  126. $count = count($form_data['values']['funding']);
  127. if ($form_state['details']['funding'] < $count) {
  128. $form_state['details']['funding'] = $form_state['details']['funding'] + $count;
  129. }
  130. else {
  131. $form_state['details']['funding'] = $form_state['details']['funding'] + 1;
  132. }
  133. for ($i = 1; $i <= $form_state['details']['funding']; $i++) {
  134. if ($i === $form_state['details']['funding']) {
  135. $form['details']['funding'][$i] = array(
  136. '#type' => 'fieldset',
  137. '#title' => t("Funding Source $i"),
  138. '#tree' => TRUE,
  139. '#collapsible' => TRUE,
  140. '#collapsed' => FALSE,
  141. '#description' => t('When requesting funds for additional Tripal development,
  142. it is important to report the breadth of of funding sources for Tripal sites.
  143. Please consider sharing this information by providing the granting
  144. agency, and funding periods.')
  145. );
  146. }
  147. else {
  148. $form['details']['funding'][$i] = array(
  149. '#type' => 'fieldset',
  150. '#title' => t("Funding Source $i"),
  151. '#tree' => TRUE,
  152. '#collapsible' => TRUE,
  153. '#collapsed' => TRUE,
  154. '#description' => t('When requesting funds for additional Tripal development,
  155. it is important to report the breadth of of funding sources for Tripal sites.
  156. Please consider sharing this information by providing the granting
  157. agency, and funding periods.')
  158. );
  159. }
  160. $form['details']['funding'][$i]['tripal_reg_site_agency'] = array(
  161. '#type' => 'textfield',
  162. '#default_value' => isset($form_data['values']['funding'][$i]['tripal_reg_site_agency']) ? $form_data['values']['funding'][$i]['tripal_reg_site_agency'] : NULL,
  163. '#title' => t('Funding Agency'),
  164. );
  165. $form['details']['funding'][$i]['tripal_reg_site_grant'] = array(
  166. '#type' => 'textfield',
  167. '#default_value' => isset($form_data['values']['funding'][$i]['tripal_reg_site_grant']) ? $form_data['values']['funding'][$i]['tripal_reg_site_grant'] : NULL,
  168. '#title' => t('Grant Number'),
  169. );
  170. $form['details']['funding'][$i]['tripal_reg_site_amount'] = array(
  171. '#type' => 'textfield',
  172. '#default_value' => isset($form_data['values']['funding'][$i]['tripal_reg_site_amount']) ? $form_data['values']['funding'][$i]['tripal_reg_site_amount'] : NULL,
  173. '#title' => t('Funding Amount'),
  174. );
  175. $form['details']['funding'][$i]['funding_period'] = array(
  176. '#type' => 'fieldset',
  177. '#title' => t('Funding Period'),
  178. '#tree' => TRUE,
  179. );
  180. $form['details']['funding'][$i]['funding_period']['tripal_reg_site_start'] = array(
  181. '#type' => 'date_select',
  182. '#title' => t("Start"),
  183. '#default_value' => isset($form_data['values']['funding'][$i]['funding_period']['tripal_reg_site_start']) ? $form_data['values']['funding'][$i]['funding_period']['tripal_reg_site_start'] : date('Y', time()),
  184. '#date_year_range' => '-20:+20',
  185. '#date_format' => 'Y',
  186. );
  187. $form['details']['funding'][$i]['funding_period']['tripal_reg_site_end'] = array(
  188. '#type' => 'date_select',
  189. '#title' => t('End'),
  190. '#default_value' => isset($form_data['values']['funding'][$i]['funding_period']['tripal_reg_site_end']) ? $form_data['values']['funding'][$i]['funding_period']['tripal_reg_site_end'] : date('Y', time()),
  191. '#date_year_range' => '-20:+20',
  192. '#date_format' => 'Y',
  193. );
  194. }
  195. $form['details']['funding']['add_funding'] = array(
  196. '#type' => 'button',
  197. '#value' => t('Add additional funding sources'),
  198. '#href' => '',
  199. '#ajax' => array(
  200. 'callback' => 'custom_registration_ajax_add_funding',
  201. 'wrapper' => 'funding',
  202. ),
  203. );
  204. // Provide a submit button.
  205. if (!empty($form_data)) {
  206. $form['submit'] = array(
  207. '#type' => 'submit',
  208. '#value' => 'Update registration information',
  209. );
  210. }
  211. else {
  212. $form['submit'] = array(
  213. '#type' => 'submit',
  214. '#value' => 'Register Site',
  215. );
  216. }
  217. return $form;
  218. }
  219. function custom_registration_ajax_add_funding($form, $form_state) {
  220. return $form['details']['funding'];
  221. }
  222. function _tripal_form_disable_reg_callback($form, &$form_state) {
  223. variable_set('disable_tripal_reporting', TRUE);
  224. }
  225. /**
  226. * Implements validation from the Form API.
  227. *
  228. * @param $form
  229. * A structured array containing the elements and properties of the form.
  230. * @param $form_state
  231. * An array that stores information about the form's current state
  232. * during processing.
  233. */
  234. function tripal_registration_form_validate($form, &$form_state){
  235. $mail_pi = $form_state['values']['principal_investigator_email'];
  236. $mail_sa = $form_state['values']['tripal_reg_site_admin_email'];
  237. if ($form_state['values']['disable_tripal_reporting'] != TRUE) {
  238. if (!empty($mail_pi) && !valid_email_address($mail_pi)) {
  239. form_set_error('[details][principal_investigator][principal_investigator_email]', t('The email address for the principal investigator appears to be invalid.'));
  240. }
  241. if(!empty($mail_sa) && !valid_email_address($mail_sa)) {
  242. form_set_error('[details][site_admin][tripal_reg_site_admin_email]', t("The email address for the site administrator appears to be invalid."));
  243. }
  244. }
  245. }
  246. /**
  247. * Implements submit from the Form API.
  248. *
  249. * @param $form
  250. * A structured array containing the elements and properties of the form.
  251. * @param $form_state
  252. * An array that stores information about the form's current state
  253. * during processing.
  254. */
  255. function tripal_registration_form_submit($form, &$form_state) {
  256. variable_set('disable_tripal_reporting', TRUE);
  257. //Check for empty funding periods and remove them.
  258. $j = 1;
  259. foreach ($form_state['values']['funding'] as $funding_source) {
  260. if (!empty($funding_source['tripal_reg_site_agency']) && !empty($funding_source['tripal_reg_site_grant'])) {
  261. $form_state['values']['fundings'][$j] = $funding_source;
  262. $j++;
  263. }
  264. }
  265. $form_state['values']['funding'] = $form_state['values']['fundings'] ?? [];
  266. $registration = serialize($form_state);
  267. variable_set('tripal_site_registration', $registration);
  268. //Now send the updated info to the Tripal Site.
  269. // Only register with tripal.info if the user has not opt'd out.
  270. if ($form_state['values']['disable_tripal_reporting'] == FALSE) {
  271. tripal_registration_remote_submit($registration);
  272. drupal_set_message(t('Registration sent to tripal.info'), 'status');
  273. drupal_set_message(t('Thank you for registering. You can update your details at any time.'), 'status');
  274. }
  275. else {
  276. drupal_set_message(t('You are not registered with tripal.info. You can
  277. change this at any time by unchecking the opt out checkbox and
  278. submitting the form.'), 'status');
  279. }
  280. }
  281. function tripal_registration_remote_submit($data) {
  282. global $base_url;
  283. $endpoint = 'http://tripal.info/registration/content/50619jdi8ciayjhygidf';
  284. //Are we getting the modules?
  285. $form_data = unserialize($data);
  286. if ($form_data['values']['tripal_reg_site_modules']) {
  287. // Get current list of modules.
  288. $files = system_rebuild_module_data();
  289. // Remove hidden modules from display list.
  290. $visible_files = $files;
  291. foreach ($visible_files as $filename => $file) {
  292. if (!empty($file->info['hidden'])) {
  293. unset($visible_files[$filename]);
  294. }
  295. }
  296. // Iterate through each of the modules.
  297. $tripal_modules = [];
  298. foreach ($visible_files as $filename => $module) {
  299. if ($module->info['package'] == 'Tripal Extensions') {
  300. $module_name = $module->info['name'];
  301. if (!in_array($module->info, $tripal_modules)) {
  302. $tripal_modules[$module_name]['info'] = $module->info;
  303. $tripal_modules[$module_name]['status'] = $module->status;
  304. }
  305. }
  306. }
  307. }
  308. //Clean up form data
  309. $outgoing_data['pi_name'] = $form_data['values']['principal_investigator_name'];
  310. $outgoing_data['pi_email'] = $form_data['values']['principal_investigator_email'];
  311. $outgoing_data['sa_name'] = $form_data['values']['tripal_reg_site_admin_name'];
  312. $outgoing_data['sa_email'] = $form_data['values']['tripal_reg_site_admin_email'];
  313. $outgoing_data['description'] = $form_data['values']['tripal_reg_site_description'];
  314. $i = 0;
  315. foreach ($form_data['values']['funding'] as $funding_source){
  316. $outgoing_data['funding_period'][$i]['funding_agency'] = $funding_source['tripal_reg_site_agency'];
  317. $outgoing_data['funding_period'][$i]['funding_grant'] = $funding_source['tripal_reg_site_grant'];
  318. $outgoing_data['funding_period'][$i]['funding_start'] = $funding_source['funding_period']['tripal_reg_site_start'];
  319. $outgoing_data['funding_period'][$i]['funding_end'] = $funding_source['funding_period']['tripal_reg_site_end'];
  320. $outgoing_data['funding_period'][$i]['funding_amount'] = $funding_source['tripal_reg_site_amount'];
  321. $i++;
  322. }
  323. $outgoing_data['type'] = $form_data['values']['tripal_reg_site_purpose'];
  324. //Build the info to send out.
  325. $outgoing_data['tripal_modules'] = $tripal_modules;
  326. $outgoing_data['site_name'] = variable_get('site_name', 'Default');
  327. $outgoing_data['site_url'] = $base_url;
  328. //Send
  329. $result = drupal_http_request($endpoint, array(
  330. 'method' => 'POST',
  331. 'headers' => array('Content-Type' => 'application/json', 'Accept' => 'application/json'),
  332. 'data' => json_encode($outgoing_data),
  333. ));
  334. variable_set('tripal_site_registration_last_update', time());
  335. return $result;
  336. }