summaryrefslogtreecommitdiff
path: root/run.inc
blob: e8dc457a213303217a7307b6fcd63896cd8ed6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
// $Id$
function lab_migration_run_form($form_state)
{
  $form['#redirect'] = FALSE;

  ahah_helper_register($form, $form_state);

  /* default value for ahah fields */
  if (!isset($form_state['storage']['run']['lab']))
  {
    /* get the book id from url */
    $url_lab_id = (int)arg(1);
    if ($url_lab_id)
    {
      /* add javascript for book selected */
      $js = " $(document).ready(function() {
        $('#edit-run-lab').val(" . $url_lab_id . ");
        $('#edit-run-lab').change();
      });";
      drupal_add_js($js, 'inline', 'footer');
    } else {
      $lab_default_value = 0;
    }
  } else {
    $lab_default_value =  $form_state['storage']['run']['lab'];
  }

  if (!isset($form_state['storage']['run']['experiment']))
  {
    $experiment_default_value = 0;
  } else {
    if ($form_state['values']['run']['lab_hidden'] !=  $form_state['values']['run']['lab'])
      $experiment_default_value = 0;
    else
      $experiment_default_value = $form_state['storage']['run']['experiment'];
  }

  if (!isset($form_state['storage']['run']['solution']))
  {
    $solution_default_value = 0;
  } else {
    if ($form_state['values']['run']['lab_hidden'] !=  $form_state['values']['run']['lab'])
      $solution_default_value = 0;
    else if ($form_state['values']['run']['experiment_hidden'] !=  $form_state['values']['run']['experiment'])
      $solution_default_value = 0;
    else
      $solution_default_value = $form_state['storage']['run']['solution'];
  }

  $form['run'] = array(
    '#type' => 'fieldset',
    '#title' => t('Download Codes'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#prefix' => '<div id="run-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  $form['run']['lab'] = array(
      '#type' => 'select',
      '#title' => t('Title of the Lab'),
      '#options' => _list_of_labs(),
      '#default_value' => $lab_default_value,
      '#tree' => TRUE,
      '#ahah' => array(
        'event'   => 'change',
        'effect' => 'none',
        'path'    => ahah_helper_path(array('run')),
        'wrapper' => 'run-wrapper',
        'progress' => array(
          'type' => 'throbber',
          'message' => t(''),
        ),
      ),
  );

  /* hidden form elements */
  $form['run']['lab_hidden'] = array(
    '#type'  => 'hidden',
    '#value' =>  $form_state['values']['run']['lab'],
  );

  /* hidden form elements */
  $form['run']['experiment_hidden'] = array(
    '#type'  => 'hidden',
    '#value' =>  $form_state['values']['run']['experiment'],
  );

  if ($lab_default_value > 0)
  {
    $lab_details = _lab_information($lab_default_value);

    /* solution provider */
    if ($lab_details->solution_provider_uid > 0) {
    	$user_solution_provider = user_load($lab_details->solution_provider_uid);
    	if ($user_solution_provider) {
    		$solution_provider = '<span style="color: rgb(128, 0, 0);"><strong>Solution Provider</strong></span></td><td style="width: 35%;"><br />' .
          '<ul>' .
          '<li><strong>Solution Provider Name:</strong> ' . $lab_details->solution_provider_name_title . ' ' . $lab_details->solution_provider_name . '</li>' .
          '<li><strong>Department:</strong> ' . $lab_details->solution_provider_department . '</li>' .
          '<li><strong>University:</strong> ' . $lab_details->solution_provider_university . '</li>' .
          '</ul>';
    	} else {
    		$solution_provider = '<span style="color: rgb(128, 0, 0);"><strong>Solution Provider</strong></span></td><td style="width: 35%;"><br />' .
					'<ul>' .
					'<li><strong>Solution Provider: </strong> (Open) </li>' .
					'</ul>';
    	}
    } else {
    	$solution_provider = '<span style="color: rgb(128, 0, 0);"><strong>Solution Provider</strong></span></td><td style="width: 35%;"><br />' .
        '<ul>' .
        '<li><strong>Solution Provider: </strong> (Open) </li>' .
        '</ul>';
    }

    $category_data = lab_migration_get_category_name($lab_details->category);

    $form['run']['lab_details'] = array(
      '#type' => 'item',
      '#value' => '<span style="color: rgb(128, 0, 0);"><strong>About the Lab</strong></span></td><td style="width: 35%;"><br />' .
        '<ul>' .
        '<li><strong>Proposer Name:</strong> ' . $lab_details->name_title . ' ' . $lab_details->name . '</li>' .
        '<li><strong>Title of the Lab:</strong> ' . $lab_details->lab_title . '</li>' .
        '<li><strong>Department:</strong> ' . $lab_details->department . '</li>' .
        '<li><strong>University:</strong> ' . $lab_details->university . '</li>' .
        '<li><strong>Category:</strong> ' .  $category_data . '</li>' .
        '</ul>' .
        $solution_provider,
    );

    $form['run']['download_lab'] = array(
      '#type' => 'item',
      '#value' => l('Download Lab Solutions', 'lab_migration/download/lab/' . $lab_default_value),
    );
    $form['run']['download_pdf'] = array(
      '#type' => 'item',
      '#value' => l('Download PDF of Lab Solutions', 'lab_migration/generate_lab/' . $lab_default_value . '/1'),
    );

    $form['run']['experiment'] = array(
      '#type' => 'select',
      '#title' => t('Title of the Experiment'),
      '#options' => _list_of_experiments($lab_default_value),
      '#default_value' => $experiment_default_value,
      '#tree' => TRUE,
      '#ahah' => array(
        'event'   => 'change',
        'effect' => 'none',
        'path'    => ahah_helper_path(array('run')),
        'wrapper' => 'run-wrapper',
        'progress' => array(
          'type' => 'throbber',
          'message' => t(''),
        ),
      ),
    );
    if ($experiment_default_value > 0)
    {
      $form['run']['download_experiment'] = array(
        '#type' => 'item',
        '#value' => l('Download Experiment', 'lab_migration/download/experiment/' . $experiment_default_value),
      );

      $form['run']['solution'] = array(
        '#type' => 'select',
        '#title' => t('Solution No. (Caption)'),
        '#options' => _list_of_solutions($experiment_default_value),
        '#default_value' => $solution_default_value,
        '#tree' => TRUE,
        '#ahah' => array(
          'event'   => 'change',
          'effect' => 'none',
          'path'    => ahah_helper_path(array('run')),
          'wrapper' => 'run-wrapper',
          'progress' => array(
            'type' => 'throbber',
            'message' => t(''),
          ),
        ),
      );
    }
  }

  /************ START OF $_POST **************/
  if ($_POST)
  {
    if (($lab_default_value > 0) && ($experiment_default_value > 0) && ($solution_default_value > 0))
    {
      $solution_list_q = db_query("SELECT * FROM {lab_migration_solution_files} WHERE solution_id = %d", $form_state['values']['run']['solution']);
      if ($solution_list_q)
      {
        $solution_files_rows = array();
        while ($solution_list_data = db_fetch_object($solution_list_q))
        {
          $solution_file_type = '';
          switch ($solution_list_data->filetype)
          {
          case 'S' : $solution_file_type = 'Source or Main file'; break;
          case 'R' : $solution_file_type = 'Result file'; break;
          case 'X' : $solution_file_type = 'xcos file'; break;
          default  : $solution_file_type = 'Unknown'; break;
          }
          $solution_files_rows[] = array(l($solution_list_data->filename, 'lab_migration/download/file/' . $solution_list_data->id), $solution_file_type);
        }

        /* dependency files */
        $dependency_q = db_query("SELECT * FROM {lab_migration_solution_dependency} WHERE solution_id = %d", $form_state['values']['run']['solution']);
				while ($dependency_data = db_fetch_object($dependency_q))
				{
					$dependency_files_q = db_query("SELECT * FROM {lab_migration_dependency_files} WHERE id = %d", $dependency_data->dependency_id);
					$dependency_files_data = db_fetch_object($dependency_files_q);
					$solution_file_type = 'Dependency file';
					$solution_files_rows[] = array(l($dependency_files_data->filename, 'lab_migration/download/dependency/' . $dependency_files_data->dependency_id), $solution_file_type);
				}

        /* creating list of files table */
        $solution_files_header = array('Filename', 'Type');
        $solution_files = theme_table($solution_files_header, $solution_files_rows); 
      }
      $form['run']['download_solution'] = array(
        '#type' => 'item',
        '#value' => l('Download Solution', 'lab_migration/download/solution/' . $solution_default_value),
      );

      $form['run']['solution_files'] = array(
        '#type'  => 'item',
        '#title' => 'List of solution files',
        '#value' => $solution_files,
      );
    }
  }
  /************ END OF $_POST **************/

  return $form;
}

function _list_of_labs()
{
  $lab_titles = array('0' => 'Please select...');
  $lab_titles_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE solution_display = 1 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_experiments($proposal_id = 0)
{
  $experiments = array('0' => 'Please select...');
  $experiments_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE proposal_id = %d ORDER BY number ASC", $proposal_id);
  while ($experiments_data = db_fetch_object($experiments_q))
  {
    $experiments[$experiments_data->id] = $experiments_data->number . '. ' . $experiments_data->title;
  }
  return $experiments;
}

function _list_of_solutions($experiment_id = 0)
{
  $solutions = array('0' => 'Please select...');
  $solutions_q = db_query("SELECT * FROM {lab_migration_solution} WHERE experiment_id = %d ORDER BY
    CAST(SUBSTRING_INDEX(code_number, '.', 1) AS BINARY) ASC,
    CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(code_number , '.', 2), '.', -1) AS UNSIGNED) ASC,
    CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(code_number , '.', -1), '.', 1) AS UNSIGNED) ASC", $experiment_id);
  while ($solutions_data = db_fetch_object($solutions_q))
  {
    $solutions[$solutions_data->id] = $solutions_data->code_number . ' (' . $solutions_data->caption . ')';
  }
  return $solutions;
}

function _lab_information($proposal_id)
{
	$lab_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE id = %d", $proposal_id);
  $lab_data = db_fetch_object($lab_q);
  return $lab_data;
}