class.tx_nG6_db.php 102 KB
Newer Older
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
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 PF bioinformatique de Toulouse <>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_utils.php');

/**
 * Class_nG6_db' for the 'nG6' extension.
 * This class is in charge of all communication with the database
 *
 * @author	PF bioinformatique de Toulouse <>
 */
class tx_nG6_db {
Penom Nom's avatar
Penom Nom committed
34
35
36
37
38
39
40
41
	
	/*
	 * Global functions
	*------------------------------------------------------------*/
	
	/**
	* Update the specified field of the table
	 *
42
	* @param	string  $table the entity to change
Penom Nom's avatar
Penom Nom committed
43
44
45
46
	* @param	string  $id     the element id to update
	* @param	string  $field  the field to change
	* @param	string  $value  the new value
	*/
47
	function update_field($table, $id, $field, $value) {
48
49
50
		if( $field == 'uid' ) {
			throw new Exception ("The function 'update_field' cannot change an id.") ;
		}	
51
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ($table, 'uid='.$id, array($field => $value));
52
	}
53
54
	
	
Gerald Salin's avatar
Gerald Salin committed
55
56
57
58
59
60
61
	/**
	 * All retention data for projet 
     *
     * @param	string	$project_id	
     * @return	table with obsoletes projects, runs, analyzes and users
     */
    function select_retention_data_for_projects($project_id) {
62

Gerald Salin's avatar
Gerald Salin committed
63
64
65
66
		$queryWHERE_ANDproject = '';
		if (isset($project_id)){
			$queryWHERE_ANDproject = "AND tx_nG6_project.uid=$project_id";
		}
67
68
69
		# select runs
		$queryParts = array(
					'SELECT' 	=> 'tx_nG6_project.uid as Project_id , tx_nG6_project.name AS Project_name,  tx_nG6_run.uid ,  tx_nG6_run.storage_size /1000000000 as storage_size , tx_nG6_run.purged_size/1000000000 as purged_size, tx_nG6_run.data_state AS Run_State',
Gerald Salin's avatar
Gerald Salin committed
70
71
72
73
74
					'FROM' 		=> 'tx_nG6_run '
					.'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.run_id = tx_nG6_run.uid '
					.'INNER JOIN tx_nG6_project ON tx_nG6_project.uid = tx_nG6_project_run.project_id ',
					'WHERE' 	=> 'DATEDIFF( FROM_UNIXTIME( tx_nG6_run.retention_date ) , now( ) ) <0 '
					.$queryWHERE_ANDproject,
75
					'GROUPBY' 	=> '',
Gerald Salin's avatar
Gerald Salin committed
76
77
					'ORDERBY' 	=> '',
					'LIMIT' 	=> ''
78
			);	
Gerald Salin's avatar
Gerald Salin committed
79
80
81
82
83
84
85
86

    	$by_project = array() ;
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (!isset ($by_project[$res_row['Project_id']])) {
    			$by_project[$res_row['Project_id']]= array(
    					'project_id'		=> $res_row['Project_id'],
    					'project_name'		=> $res_row['Project_name'],
87
    					'state'		=> array( $res_row['Run_State']  => array ('nb_run' => 1, 'size_run' => $res_row['storage_size']+$res_row['purged_size'], 'run_ids'=> array($res_row['uid']))));
Gerald Salin's avatar
Gerald Salin committed
88
89
90
			}
			else
			{
91
92
93
94
95
96
97
				if (!isset ($by_project[$res_row['Project_id']]['state'][$res_row['Run_State']])) {
					$by_project[$res_row['Project_id']]['state'][$res_row['Run_State']]  = array ('nb_run' => 1, 'size_run' =>$res_row['storage_size']+$res_row['purged_size'], 'run_ids'=> array($res_row['uid']));
				} else {
					$by_project[$res_row['Project_id']]['state'][$res_row['Run_State']]['nb_run']+=1;
					$by_project[$res_row['Project_id']]['state'][$res_row['Run_State']]['size_run']+=$res_row['storage_size']+$res_row['purged_size'];
					array_push($by_project[$res_row['Project_id']]['state'][$res_row['Run_State']]['run_ids'], $res_row['uid']);
				}
Gerald Salin's avatar
Gerald Salin committed
98
99
100
101
102
103
104
			}
						
		}
		
		
		# select runs-analyzes
		$queryParts = array(
105
    				'SELECT' 	=> 'tx_nG6_project.uid as Project_id , tx_nG6_project.name AS Project_name, tx_nG6_analyze.uid as analyze_id , tx_nG6_analyze.storage_size /1000000000 AS analyze_storage, tx_nG6_analyze.purged_size/1000000000 AS analyze_purged, tx_nG6_analyze.data_state AS Analyze_State',
Gerald Salin's avatar
Gerald Salin committed
106
107
108
109
110
111
112
	    			'FROM' 		=> 'tx_nG6_run '
	    			.'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.run_id = tx_nG6_run.uid '
	    			.'INNER JOIN tx_nG6_project ON tx_nG6_project.uid = tx_nG6_project_run.project_id '
	    			.'INNER JOIN tx_nG6_run_analyze ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '
	    			.'INNER JOIN tx_nG6_analyze ON tx_nG6_run_analyze.analyze_id = tx_nG6_analyze.uid',
	    			'WHERE' 	=> 'DATEDIFF( FROM_UNIXTIME( tx_nG6_analyze.retention_date ) , now( ) ) <0 '
	    			.$queryWHERE_ANDproject,
113
	    			'GROUPBY' 	=> '',
Gerald Salin's avatar
Gerald Salin committed
114
115
116
117
118
119
120
121
122
123
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
	    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (!isset ($by_project[$res_row['Project_id']])) {
				
    			$by_project[$res_row['Project_id']]= array(
    					'project_id'		=> $res_row['Project_id'],
    					'project_name'		=> $res_row['Project_name'],
124
125
126
127
128
129
130
131
132
133
    					'state'				=> array($res_row['Analyze_State'] => array("nb_analyze"=> 1, 
																						"size_analyze"=> $res_row['analyze_storage']+$res_row['analyze_purged'], 
																						'analysis_ids'=> array($res_row['analyze_id']))));
			}
			if (!isset ($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['analysis_ids'])){
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']] = array_merge($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']],array("nb_analyze"=>1, "size_analyze"=>$res_row['analyze_storage']+$res_row['analyze_purged'],'analysis_ids'=>array($res_row['analyze_id'])));
			} else {				
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['nb_analyze'] += 1;
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['size_analyze'] += $res_row['analyze_storage']+$res_row['analyze_purged'];
				array_push($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['analysis_ids'],$res_row['analyze_id']);
Gerald Salin's avatar
Gerald Salin committed
134
135
136
137
138
			}
		}
		
		# select project-analyzes
		$queryParts = array(
139
    				'SELECT' 	=> 'tx_nG6_project.uid as Project_id , tx_nG6_project.name AS Project_name, tx_nG6_analyze.uid as analyze_id, tx_nG6_analyze.storage_size /1000000000 analyze_storage, tx_nG6_analyze.purged_size/1000000000) AS analyze_purged, tx_nG6_analyze.data_state AS Analyze_State',
Gerald Salin's avatar
Gerald Salin committed
140
141
142
143
144
	    			'FROM' 		=> 'tx_nG6_project '
	    			.'INNER JOIN tx_nG6_project_analyze ON tx_nG6_project_analyze.project_id = tx_nG6_project.uid '
	    			.'INNER JOIN tx_nG6_analyze ON tx_nG6_project_analyze.analyze_id = tx_nG6_analyze.uid',
	    			'WHERE' 	=> 'DATEDIFF( FROM_UNIXTIME( tx_nG6_analyze.retention_date ) , now( ) ) <0 '
	    			.$queryWHERE_ANDproject,
145
	    			'GROUPBY' 	=> '',
Gerald Salin's avatar
Gerald Salin committed
146
147
148
149
150
151
152
153
154
155
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
		);
    	
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (!isset ($by_project[$res_row['Project_id']])) {
    			$by_project[$res_row['Project_id']]= array(
    					'project_id'		=> $res_row['Project_id'],
    					'project_name'		=> $res_row['Project_name'],
156
157
158
159
    					'state'				=> array());
			}
			if (!isset ($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['analysis_ids'])){
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']] = array_merge($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']],array("nb_analyze"=>1, "size_analyze"=>$res_row['analyze_storage']+$res_row['analyze_purged'],'analysis_ids'=>array($res_row['analyze_id'])));
Gerald Salin's avatar
Gerald Salin committed
160
161
162
			}
			else
			{
163
164
165
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['nb_analyze'] += 1;
				$by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['size_analyze'] += $res_row['analyze_storage']+$res_row['analyze_purged'];
				array_push($by_project[$res_row['Project_id']]['state'][$res_row['Analyze_State']]['analysis_ids'], $res_row['analyze_id']);
Gerald Salin's avatar
Gerald Salin committed
166
167
168
			}
		}

169
	
Gerald Salin's avatar
Gerald Salin committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
		#retrieve user per project
		$query_for_user = str_replace("AND","",$queryWHERE_ANDproject);
			$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid AS Project_id, fe_users.username AS user_name, fe_users.email, fe_users.usergroup, fe_rights.right_id',
	    			'FROM' 		=> 'tx_nG6_project '
	    			.'INNER JOIN fe_rights ON fe_rights.project_id = tx_nG6_project.uid '
	    			.'INNER JOIN fe_users ON fe_rights.fe_user_id = fe_users.uid '
	    			.'INNER JOIN fe_groups ON fe_users.usergroup = fe_groups.uid',
	    			'WHERE' 	=> $query_for_user,
	    			'GROUPBY' 	=> '',
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
		);

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
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
			if(isset($by_project[$res_row['Project_id']])){
				if (!isset ($by_project[$res_row['Project_id']]["users"])) {
					$by_project[$res_row['Project_id']]["users"]=array();
				}
				array_push($by_project[$res_row['Project_id']]["users"],array ('user_name' => $res_row['user_name'], 'email' =>$res_row['email'], 'groups' =>$res_row['usergroup'], 'right' =>$res_row['right_id']));
			}
		}
			
		#select count All runs per project
		$queryParts = array(
					'SELECT' 	=> 'tx_nG6_project.uid as Project_id , tx_nG6_project.name AS Project_name, count( DISTINCT tx_nG6_run.uid ) AS Nb_all_run',
					'FROM' 		=> 'tx_nG6_run '
					.'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.run_id = tx_nG6_run.uid '
					.'INNER JOIN tx_nG6_project ON tx_nG6_project.uid = tx_nG6_project_run.project_id ',
					'WHERE' 	=> $query_for_user,
					'GROUPBY' 	=> 'tx_nG6_project.uid',
					'ORDERBY' 	=> '',
					'LIMIT' 	=> ''
			);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (isset ($by_project[$res_row['Project_id']])) {
				$by_project[$res_row['Project_id']]["nb_runs"]=$res_row['Nb_all_run'];
			}
		}

		#select count All run-analyse per project
		$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project_run.project_id as Project_id , count( DISTINCT tx_nG6_run_analyze.analyze_id) as nb_all_analyse',
	    			'FROM' 		=> 'tx_nG6_run '
	    			.'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.run_id = tx_nG6_run.uid '
	    			.'INNER JOIN tx_nG6_run_analyze ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '
	    			.'INNER JOIN tx_nG6_analyze ON tx_nG6_run_analyze.analyze_id = tx_nG6_analyze.uid',
	    			'WHERE' 	=> $query_for_user,
	    			'GROUPBY' 	=> ' tx_nG6_project_run.project_id',
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
	    	);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (isset ($by_project[$res_row['Project_id']])) {
				$by_project[$res_row['Project_id']]["nb_analyses"]=$res_row['nb_all_analyse'];
Gerald Salin's avatar
Gerald Salin committed
228
229
230
231
			}
		}


232

Gerald Salin's avatar
Gerald Salin committed
233
234
235
		return ($by_project);
    }

Jerome Mariette's avatar
Jerome Mariette committed
236
		
237
238
239
    /**
     * Select all project for the current user
     *
240
     * @param	string	$by	by can group, organism, location
241
242
     * @param	string	$role	role can be create_user, manager
     * @return	table with all projects
243
244
245
246
     */
    function select_projects_repartition($role, $by='title') {
    	
    	if ($role == "create_user") {
247
248
249
250
251
252
253
254
255
    		$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
    				'FROM' 		=> 'tx_nG6_project '
    				.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
    				.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ',
    				'WHERE' 	=> "",
    				'GROUPBY' 	=> '',
    				'ORDERBY' 	=> '',
    				'LIMIT' 	=> ''
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    		);
    		//$where = "tx_nG6_project.cruser_id="
    	} else if ($role == "manager") {
	    	$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
	    			'FROM' 		=> 'tx_nG6_project '
	    			.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
	    			.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
	    			.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ',
	    			'WHERE' 	=> "fe_rights.right_id=1",
	    			'GROUPBY' 	=> '',
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
	    	);
    	}
    	$by_title = array() ;
    	$by_organism = array();
    	$by_location = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
276
277
278
279
280
    		if (!isset ($by_title[$res_row['title']])) {
    			$by_title[$res_row['title']] = array(
    					'count'			=> 1,
    					'organism'		=> $res_row['organism'],
    					'location'		=> $res_row['location']);
281
282
283
284
    		} else {
    			$by_title[$res_row['title']]['count'] += 1;
    		}
    		
285
286
287
288
289
    		if (!isset ($by_organism[$res_row['organism']])) {
    			$by_organism[$res_row['organism']] = array(
    					'count'			=> 1);
    		} else {
    			$by_organism[$res_row['organism']]['count'] += 1;
290
291
    		}
    		
292
293
294
295
296
297
    		if (!isset ($by_location[$res_row['location']])) {
    			$by_location[$res_row['location']] = array(
    					'count'			=> 1);
    		} else {
    			$by_location[$res_row['location']]['count'] += 1;
    		}
298
299
300
301
302
303
304
305
306
    	}
    	if ($by=='title') {
    		return ($by_title);
    	} else if ($by=='organism') {
    		return ($by_organism);
    	} else if ($by=='location') {
    		return ($by_location);
    	}
    }
Jerome Mariette's avatar
Jerome Mariette committed
307

308
    function select_projects_evolution($values, $by, $role, $cumulate) {
Penom Nom's avatar
Penom Nom committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
		if ($by == "organism" || $by == "location") {
			$by = "tx_nG6_".$by;
		}
	
		$from = "" ;
		$where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
    	if ($role == "create_user") {
    		$from = 'tx_nG6_project '
	    			.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
	    			.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
    	} else if ($role == "manager") {
    		$from = 'tx_nG6_project '
    				.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
    				.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
    				.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ';
Jerome Mariette's avatar
Jerome Mariette committed
324
    		$where .= " AND fe_rights.right_id=1" ;
Penom Nom's avatar
Penom Nom committed
325
326
    	}
    	$queryParts = array(
Jerome Mariette's avatar
Jerome Mariette committed
327
    			'SELECT' 	=> 'tx_nG6_project.crdate, count(tx_nG6_project.crdate) nb',
Penom Nom's avatar
Penom Nom committed
328
329
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
Jerome Mariette's avatar
Jerome Mariette committed
330
331
    			'GROUPBY' 	=> 'tx_nG6_project.crdate',
    			'ORDERBY' 	=> 'tx_nG6_project.crdate',
Penom Nom's avatar
Penom Nom committed
332
333
334
335
336
    			'LIMIT' 	=> ''
    	);
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
337
338
339
340
341
    		if ($cumulate && count($results) > 0) {
    			$results[] = array($res_row["crdate"], $res_row["nb"] + $results[count($results)-1][1]);
    		} else {
    			$results[] = array($res_row["crdate"], $res_row["nb"]);
    		}
Penom Nom's avatar
Penom Nom committed
342
343
344
345
346
    	}

    	return $results;
    }

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    function select_projects_distribution($values, $by, $role) {
		$from = "" ;
		$where = "" ;
		if ($by == "organism" || $by == "location") {
			$by = "tx_nG6_".$by;
		}
    	if ($role == "create_user") {
    		$from = 'tx_nG6_project '
	    			.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
	    			.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
    	} else if ($role == "manager") {
    		$from = 'tx_nG6_project '
    				.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
    				.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
    				.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ';
    		$where = "fe_rights.right_id=1" ;
363
    	}
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    	$queryParts = array(
    			'SELECT' 	=> 'fe_groups.'.$by.', count(fe_groups.'.$by.') nb',
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
    			'GROUPBY' 	=> 'fe_groups.'.$by,
    			'ORDERBY' 	=> '',
    			'LIMIT' 	=> ''
    	);
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$others = 0 ;
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		if( in_array($res_row[$by], $values) ) {
    			$results[] = array($res_row[$by], $res_row["nb"]);
    		} else {
    			$others += $res_row["nb"] ;
    		}
    	}
    	if ($others != 0) { 
    		$results[] = array("OTHERS", $others);
    	}
    	return $results;
    }
    
388
389
390
    function select_storage_distribution($values, $by, $role, $get_analyzes, $octet) {
    	if ($by == "organism" || $by == "location") {
    		$by = "tx_nG6_".$by;
Jerome Mariette's avatar
Jerome Mariette committed
391
392
393
394
395
    	}
    	$storage_unit = "full_seq_size";
    	if ($octet) {
    		$storage_unit = "storage_size";
    	}
396
397
398
    	$from = "" ;
    	$where = "" ;
    	if ($role == "create_user") {
Jerome Mariette's avatar
Jerome Mariette committed
399
400
    		$from = 'tx_nG6_project '
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
401
402
403
404
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
    		.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
    		.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
    	} else if ($role == "manager") {
Jerome Mariette's avatar
Jerome Mariette committed
405
    		$from = 'tx_nG6_project '
406
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
Jerome Mariette's avatar
Jerome Mariette committed
407
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
408
409
410
411
412
413
414
415
416
417
418
419
    		.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
    		.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
    		.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ';
    		$where = "fe_rights.right_id=1" ;
    	}
    	$queryParts = array(
    			'SELECT' 	=> 'tx_nG6_run.uid, tx_nG6_run.'.$storage_unit.', fe_groups.'.$by,
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
    			'GROUPBY' 	=> '',
    			'ORDERBY' 	=> '',
    			'LIMIT' 	=> ''
Jerome Mariette's avatar
Jerome Mariette committed
420
    	);
421
422
423
424
425
    	
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$others = 0 ;
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
Jerome Mariette's avatar
Jerome Mariette committed
426
			
427
428
429
430
431
432
    		$analyses_size = 0;
    		if ($get_analyzes && $octet) {
    			$run_analysis = tx_nG6_db::get_run_analysis($res_row["uid"]);
    			foreach($run_analysis as $analyse_id => $analyze_values) {
    				$analyses_size += intval($analyze_values["storage_size"]);
    			}
Jerome Mariette's avatar
Jerome Mariette committed
433
434
435
    		}
    		if( in_array($res_row[$by], $values) ) {
    			
436
    			if( array_key_exists( $res_row[$by], $results) ) {
Jerome Mariette's avatar
Jerome Mariette committed
437
438
439
    				$results[$res_row[$by]] += $res_row[$storage_unit] + $analyses_size;
    			} else {
    				$results[$res_row[$by]] = $res_row[$storage_unit] + $analyses_size;
440
441
442
443
444
445
446
    			}
    		} else {
    			$others += $res_row[$storage_unit] + $analyses_size;
    		}
    	}
    	if ($others != 0) {
    		$results["OTHERS"] = $others;
Jerome Mariette's avatar
Jerome Mariette committed
447
448
    	}
    	$final_results = array();
449
450
451
452
    	foreach($results as $group_key => $group_values) {
    		$final_results[] = array( $group_key, $group_values );
    	}
    	return $final_results;
453
454
    }
    
455
    function select_storage_evolution($values, $by, $role, $get_analyzes, $octet, $cumulate) {
Jerome Mariette's avatar
Jerome Mariette committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    	if ($by == "organism" || $by == "location") {
    		$by = "tx_nG6_".$by;
    	}
    	$storage_unit = "full_seq_size";
    	if ($octet) {
    		$storage_unit = "storage_size";
    	}
    	$from = "" ;
    	$where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
    	if ($role == "create_user") {
    		$from = 'tx_nG6_project '
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
    		.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
    		.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
    	} else if ($role == "manager") {
    		$from = 'tx_nG6_project '
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
    		.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
    		.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
    		.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ';
    		$where .= " AND fe_rights.right_id=1" ;
    	}
    	$queryParts = array(
    			'SELECT' 	=> 'tx_nG6_run.uid, tx_nG6_run.crdate, tx_nG6_run.'.$storage_unit.', fe_groups.'.$by,
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
    			'GROUPBY' 	=> '',
    			'ORDERBY' 	=> 'tx_nG6_run.crdate',
    			'LIMIT' 	=> ''
    	);
    	
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			
    		$analyses_size = 0;
    		if ($get_analyzes && $octet) {
    			$run_analysis = tx_nG6_db::get_run_analysis($res_row["uid"]);
    			foreach($run_analysis as $analyse_id => $analyze_values) {
    				$analyses_size += intval($analyze_values["storage_size"]);
    			}
    		}
    		
    		if( array_key_exists( $res_row["crdate"], $results) ) {
    			$results[$res_row["crdate"]] += $res_row[$storage_unit] + $analyses_size;
    		} else {
    			$results[$res_row["crdate"]] = $res_row[$storage_unit] + $analyses_size;
    		}
    	}
    	
    	$final_results = array();
    	foreach($results as $group_key => $group_values) {
    		
511
512
513
514
    		if ($cumulate && count($final_results) > 0) {
    			$final_results[] = array( $group_key, $group_values + $final_results[count($final_results)-1][1]);
    		} else {
    			$final_results[] = array( $group_key, $group_values );
Jerome Mariette's avatar
Jerome Mariette committed
515
516
517
    		}

    	}
518
    	return $final_results;
Jerome Mariette's avatar
Jerome Mariette committed
519
    }
520
    
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
	/**
	 * Add a comment to the comment table
	 * @param unknown $table_name
	 * @param unknown $id_label
	 * @param unknown $element_id
	 * @param unknown $cruser_id
	 * @param unknown $new_comment
	 */
    function add_comment($table_name, $id_label, $id, $cruser_id, $new_comment){
    	$new_comment = htmlentities($new_comment, ENT_QUOTES | ENT_COMPAT | ENT_HTML5);
    	$comment_data = array(
    	 		'cruser_id'			=>	$cruser_id,
    	 		'comment'			=>	$new_comment,
    	 		'tstamp'			=> 	time(),
    	 		'crdate'			=> 	time()
    	);
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_nG6_comment', $comment_data);
    	$comment_id =  $GLOBALS['TYPO3_DB']->sql_insert_id();
    	
    	$comment_link_data = array(
    	 		'cruser_id'		=>	$cruser_id,
    	 		$id_label		=>	$id,
    			'comment_id'	=> 	$comment_id,
    	 		'tstamp'		=> 	time(),
    	 		'crdate'		=> 	time()
    	);
    	
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery($table_name, $comment_link_data);
    	return $GLOBALS['TYPO3_DB']->sql_insert_id();
    }

    /**
     * Retrieve all comments from a given project, run or analysis
     * @param unknown $table_name
     * @param unknown $id_label
     * @param unknown $uid
     * @return multitype:multitype:unknown
     */
    function get_all_comments($table_name, $id_label, $id){
    	$comments = array();
    	$temp = array();
    	$queryParts = array(
    			'SELECT' 	=> 'tx_nG6_comment.uid AS comment_id, '.
    			'tx_nG6_comment.comment AS comment, '.
    			'tx_nG6_comment.cruser_id AS cruser_id, '.
    			'tx_nG6_comment.crdate AS comment_crdate ',
    			'FROM' 		=> $table_name .' INNER JOIN tx_nG6_comment ON '. $table_name . '.comment_id=tx_nG6_comment.uid',
    			'WHERE' 	=> $table_name . '.' . $id_label . '=' . $id,
    			'ORDERBY' 	=> 'comment_id',
    	);
    	 
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$comment_id = $row['comment_id'];
    		if (!isset ($temp['comment_'.$comment_id])) {
    			$temp['comment_'.$comment_id] = '';
    			$user_info = tx_nG6_db::get_user_informations($row['cruser_id']);
    			$comments[] = array(
    					'id'			=> $comment_id,
    					'comment'		=> html_entity_decode($row['comment'], ENT_QUOTES | ENT_COMPAT | ENT_HTML5),
    					'date'			=> date( 'd.M.Y-H:i' ,$row['comment_crdate']),
    					'cruser_info'	=> $user_info,
    			);
    		}
    	}
    	return $comments;
    }
    
    /**
     * Delete a given commentary from the database
     * @param unknown $table_name
     * @param unknown $id_label
     * @param unknown $id
     * @param unknown $comment_id
     */
    function delete_comment($table_name, $id_label, $id, $comment_id){
    	
    	$res = 0 ;
    	//from association table
600
    	$where = $table_name.'.'.$id_label.'='.$id.' AND '.$table_name.'.comment_id='.$comment_id ;
601
602
    	$res = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table_name, $where);
    	
603
604
605
606
607
    	//from comment table
    	$where = 'tx_nG6_comment.uid='.$comment_id ;
    	$res = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ('tx_nG6_comment', $where);
    	if ($res != 1) {
    		$res = 1 ;
608
609
610
611
612
613
614
615
616
617
618
619
620
621
    	}
    	return $res ;
    }
    
    /**
     * Update an existing comment
     * @param unknown $comment_id
     * @param unknown $new_comment
     */
    function update_comment($comment_id, $new_comment){
    	return $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_comment', 'uid='.$comment_id, array('comment' => $new_comment));
    }
    
    
622
623
624
625
	/*
	 * Project functions
	 *------------------------------------------------------------*/

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    /**
     * Add a comment to the project described by project_id
     * @param unknown $project_id
     * @param unknown $cruser_id
     * @param unknown $new_comment
     */
    function add_project_comment($project_id, $cruser_id, $new_comment){
    	return tx_nG6_db::add_comment('tx_nG6_project_comment', 'project_id', $project_id, $cruser_id, $new_comment);
    }
    
    /**
     * Retrieve all comments from a given project
     * @param unknown $project_id
     */
    function get_all_project_comments($project_id){
    	return tx_nG6_db::get_all_comments('tx_nG6_project_comment', 'project_id', $project_id);
    }
    
    /**
     * Delete a given project comment
     * @param unknown $project_id
     * @param unknown $comment_id
     */
    function delete_project_comment($project_id, $comment_id){
    	return tx_nG6_db::delete_comment('tx_nG6_project_comment', 'project_id', $project_id, $comment_id);
    }
    
653
654
	/**
	 * Select all project for the current user
655
	 * 
656
	 * @param	string	$user_id	the user id
657
658
	 * @return	table with all projects
	 */
659
	function select_all_user_projects($user_id, $orderby='', $limit='') {		
660
		$projects = array();
Jerome Mariette's avatar
Jerome Mariette committed
661
		
662
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
663
664
665
		if ($user_id == null) { $where = 'tx_nG6_project.public=0'; } 
		else { $where = 'fe_rights.fe_user_id='.$user_id; }
		
666
		//First, get the analyzis project-scale
667
		$queryParts = array(
668
669
670
			'SELECT' => 'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.description AS project_description, '.
671
						'tx_nG6_project.public AS project_public, '.
Jerome Mariette's avatar
Jerome Mariette committed
672
						'tx_nG6_project.crdate AS project_crdate, '.
673
674
						'tx_nG6_project.hidden AS project_hidden ',
			'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
675
676
			'WHERE' => $where,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
677
678
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
679
		);
680
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);		
681
682
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
Jerome Mariette's avatar
Jerome Mariette committed
683
			if (!isset ($projects['project_'.$project_id])) {		
684
				$projects['project_'.$project_id] = array(
685
					'id'			=> $project_id,				
686
					'name'			=> $row['project_name'],
Jerome Mariette's avatar
Jerome Mariette committed
687
					'hidden'		=> $row['project_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
688
					'date'			=> $row['project_crdate'],
689
					'public'		=> $row['project_public'],
690
691
					'description'	=> $row['project_description']);
			}
692
						
693
		}		
694
695
696
		return $projects;
	}

Penom Nom's avatar
Penom Nom committed
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	/**
	* Select all project for the current user
	*
	* @param	string	$user_id	the user id
	* @return	table with all projects
	*/
	function get_user_projects($user_id, $orderby='', $limit='') {
		$projects = array();
	
		// If the user is not logged on display demonstration project
		if ($user_id == null) {
			$where = 'tx_nG6_project.public=0';
		}
		else { 
				$where = 'fe_rights.fe_user_id='.$user_id.' AND ((fe_rights.right_id<>2 AND tx_nG6_project.hidden=0) '
				.'OR fe_rights.right_id=2)' ;
		}
	
		//First, get the analyzis project-scale
		$queryParts = array(
				'SELECT' => 'tx_nG6_project.uid AS project_id, '.
							'tx_nG6_project.name AS project_name, '.
							'tx_nG6_project.description AS project_description, '.
							'tx_nG6_project.public AS project_public, '.
							'tx_nG6_project.crdate AS project_crdate, '.
							'tx_nG6_project.hidden AS project_hidden ',
				'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
				'WHERE' => $where,
				'GROUPBY' => '',
				'ORDERBY' => $orderby,
				'LIMIT' => $limit
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
			if (!isset ($projects['project_'.$project_id])) {
				$projects['project_'.$project_id] = array(
						'id'			=> $project_id,				
						'name'			=> $row['project_name'],
						'hidden'		=> $row['project_hidden'],
						'date'			=> $row['project_crdate'],
						'public'		=> $row['project_public'],
						'description'	=> $row['project_description']);
			}
	
		}
		return $projects;
	}

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
	/**
	 * Select a run from the database
	 *
	 * @param	string	$project_id	the project id to return
	 * @return	hash table with all project information
	 */
	function select_project($project_id) {
		$queryParts = array(
			'SELECT' => '*',
			'FROM' => 'tx_nG6_project',
			'WHERE' => 'tx_nG6_project.uid = '.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
Jerome Mariette's avatar
Jerome Mariette committed
762
763
764
765
		$vals = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
		return array('id'			=> $vals['uid'],				
					 'name'			=> $vals['name'],
					 'hidden'		=> $vals['hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
766
					 'date'			=> $vals['crdate'],
Jerome Mariette's avatar
Jerome Mariette committed
767
768
					 'public'		=> $vals['public'],
					 'description'	=> $vals['description']);
769
770
771
772
773
774
775
776
	}

	/**
	 * Select all runs linked to the specified project
	 *
	 * @param	string	$project_id	the project id
	 * @return	hash table with all runs information
	 */
777
function get_project_runs($project_id, $orderby='', $limit='') {
778
    	
779
		// First select all runs from the database
780
		$queryParts = Array( 
781
782
783
784
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
					 	'tx_nG6_run.species AS run_species,'.
						'tx_nG6_run.description AS run_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
785
786
787
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
788
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
789
790
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
791
792
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
793
794
795
796
797
						'tx_nG6_run.data_state AS run_data_state, '.
						'tx_nG6_run.retention_date AS run_retention_date, '.
						'tx_nG6_run.purged_date AS run_purged_date, '.
						'tx_nG6_run.purged_size AS run_purged_size, '.
						'tx_nG6_run.mail_sent_date AS run_mail_sent_date, '.
798
799
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name ',
Jerome Mariette's avatar
Jerome Mariette committed
800
			'FROM' => 'tx_nG6_project '.
801
802
  						'INNER JOIN tx_nG6_project_run ON  tx_nG6_project.uid=tx_nG6_project_run.project_id '.
  						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
Jerome Mariette's avatar
Jerome Mariette committed
803
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
804
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
805
806
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
Penom Nom's avatar
Penom Nom committed
807
808
809
		);

	
810
811
812
813
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
Jerome Mariette's avatar
Jerome Mariette committed
814
815
			$run_id = $row['run_id'];				
			if (!isset($results['run_'.$run_id])) {				
816
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
817
					'id'			=> $run_id,
818
					'directory'		=> $row['run_directory'], 
819
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
820
821
					'project_name' 	=> $row['project_name'],
					'hidden'		=> $row['run_hidden'],
822
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
823
824
825
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
826
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
827
828
829
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
830
831
832
833
834
835
					'description' 	=> $row['run_description'],
					'data_state' 	=> $row['run_data_state'],
					'retention_date' 	=> $row['run_retention_date'],
					'purged_date' 	=> $row['run_purged_date'],
					'purged_size' 	=> $row['run_purged_size'],
					'mail_sent_date' 	=> $row['run_mail_sent_date'],
836
				);
837
			}						
838
839
840
		}
		return $results;
    }
Penom Nom's avatar
Penom Nom committed
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
    /**
     * Select all runs linked to the specified project for the user
     *
     * @param	string	$user_id	the user id
     * @param	string	$project_id	the project id
     * @return	hash table with all runs information
     */
     function get_user_project_runs($user_id, $project_id, $orderby='', $limit='') {
    	// If the user is not logged on display demonstration project
	    if ($user_id == null) {
	    	$where = 'tx_nG6_project.uid='.$project_id.' AND tx_nG6_project.public=0 AND tx_nG6_run.hidden=0';
	    }
	    else {
	    	$where = 'fe_users.uid='.$user_id.' AND '.'tx_nG6_project.uid='.$project_id.
	    		' AND ((fe_rights.right_id<>2 AND tx_nG6_run.hidden=0) OR fe_rights.right_id=2)' ;
	    }
    	
	    // First select all runs from the database
	    $queryParts = Array(
		    'SELECT' => 'tx_nG6_run.uid AS run_id,'.
			    'tx_nG6_run.directory AS run_directory,'.
			    'tx_nG6_run.species AS run_species,'.
			    'tx_nG6_run.description AS run_description, '.
			    'tx_nG6_run.data_nature AS run_data_nature, '.
			    'tx_nG6_run.sequencer AS run_sequencer, '.
			    'tx_nG6_run.type AS run_type, '.
			    'tx_nG6_run.name AS run_name, '.
			    'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
			    'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
			    'tx_nG6_run.date AS run_date, '.
			    'tx_nG6_run.hidden AS run_hidden, '.
			    'tx_nG6_project.uid AS project_id, '.
			    'tx_nG6_project.name AS project_name ',
		    'FROM' => 'tx_nG6_run '.
			    'INNER JOIN tx_nG6_project_run ON tx_nG6_run.uid=tx_nG6_project_run.run_id '.
			    'INNER JOIN tx_nG6_project ON tx_nG6_project_run.project_id=tx_nG6_project.uid '.
			    'INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '.
			    'INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid ',
		    'WHERE' => $where,
		    'GROUPBY' => '',
		    'ORDERBY' => $orderby,
		    'LIMIT' 	=> $limit
	    );
    
	    // Then create the result hash table
	    $results = array();
	    $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
	    while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
	    	$run_id = $row['run_id'];
	    	if (!isset($results['run_'.$run_id])) {
			    $results['run_'.$run_id] = array(
			    'id'			=> $run_id,
			    'directory'		=> $row['run_directory'],
			    'name' 			=> $row['run_name'],
			    							'project_name' 	=> $row['project_name'],
			    'hidden'		=> $row['run_hidden'],
			    							'species' 		=> $row['run_species'],
			    'nb_sequences' 	=> $row['run_nb_sequences'],
			    							'full_seq_size' => $row['run_full_seq_size'],
			    							'project_id' 	=> $row['project_id'],
			    							'date'			=> $row['run_date'],
			    							'data_nature'	=> $row['run_data_nature'],
			    							'sequencer'		=> $row['run_sequencer'],
			    							'type'			=> $row['run_type'],
			    'description' 	=> $row['run_description']
			    				);
		    }
	    }
	    return $results;
    }

912
913
914
915
916
917
	/**
	 * Select all analysis linked to the specified project
	 *
	 * @param	string	$project_id	the project id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
918
    function get_project_analysis($project_id, $orderby='', $limit='') {
919
920
    	
    	// First select all analysis from the database    	
921
    	$queryParts = array( 
922
923
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
924
						'tx_nG6_analyze.name AS analyze_name, '.
925
						'tx_nG6_analyze.class AS analyze_class, '.
926
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
927
928
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
929
						'tx_nG6_analyze.params AS analyze_params, '.
930
						'tx_nG6_analyze.description AS analyze_description, '.
931
    					'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Jerome Mariette's avatar
Jerome Mariette committed
932
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
933
						'tx_nG6_analyze.hidden AS analyze_hidden ',
Jerome Mariette's avatar
Jerome Mariette committed
934
			'FROM' => 'tx_nG6_project '.
935
936
						'INNER JOIN tx_nG6_project_analyze ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id '.
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id ',
Jerome Mariette's avatar
Jerome Mariette committed
937
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
938
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
939
940
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
941
942
943
944
945
946
		);
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$analyze_id = $row['analyze_id'];
Jerome Mariette's avatar
Jerome Mariette committed
947
			if (!isset ($results['analyse_'.$analyze_id])) {				
948
949
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
950
					'name' 			=> $row['analyze_name'],
951
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
952
953
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
954
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
955
956
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
957
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
958
					'description' 	=> $row['analyze_description'],
959
					'is_editable' 	=> $row['analyze_is_editable'],
Jerome Mariette's avatar
Jerome Mariette committed
960
					'parent_id'		=> $row['analyze_parent_uid']	
961
				);
962
			}						
963
964
965
966
		}
		return $results;
    }

Penom Nom's avatar
Penom Nom committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
    /**
    * Select all analysis linked to the specified project for the user
    *
    * @param	string	$user_id	the user id
    * @param	string	$project_id	the project id
    * @return	hash table with all analysis information
    */
    function get_user_project_analysis($user_id, $project_id, $orderby='', $limit='') {
    	// If the user is not logged on display demonstration project
    	if ($user_id == null) {
    		$where = 'tx_nG6_project.uid='.$project_id.' AND tx_nG6_project.public=0 AND tx_nG6_run.hidden=0';
    	}
    	else {
    		$where = 'fe_users.uid='.$user_id.' AND tx_nG6_project.uid='.$project_id.
    					' AND ((fe_rights.right_id<>2 AND tx_nG6_analyze.hidden=0) OR fe_rights.right_id=2)' ;
    	}
    	
    	// First select all analysis from the database
    	$queryParts = array(
    			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
    						'tx_nG6_analyze.directory AS analyze_directory,'. 
    						'tx_nG6_analyze.name AS analyze_name, '.
    						'tx_nG6_analyze.class AS analyze_class, '.
    						'tx_nG6_analyze.date AS analyze_date, '.
    						'tx_nG6_analyze.software AS analyze_software, '.
    						'tx_nG6_analyze.version AS analyze_version, '.
    						'tx_nG6_analyze.params AS analyze_params, '.
    						'tx_nG6_analyze.description AS analyze_description, '.
995
    						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Penom Nom's avatar
Penom Nom committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
    						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
    						'tx_nG6_analyze.hidden AS analyze_hidden ',
    			'FROM' => 'tx_nG6_analyze '.
    						'INNER JOIN tx_nG6_project_analyze ON tx_nG6_analyze.uid = tx_nG6_project_analyze.analyze_id '.
    						'INNER JOIN tx_nG6_project ON tx_nG6_project_analyze.project_id=tx_nG6_project.uid '.
							'INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '.
					  		'INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid ',
    			'WHERE' => $where,
    			'GROUPBY' => '',
    			'ORDERBY' => $orderby,
    			'LIMIT' => $limit
    	);
    	// Then create the result hash table
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$analyze_id = $row['analyze_id'];
    		if (!isset ($results['analyse_'.$analyze_id])) {
    			$results['analyse_'.$analyze_id] = array(
    					'directory'		=> $row['analyze_directory'], 
    					'name' 			=> $row['analyze_name'],
    					'class'			=> $row['analyze_class'],
    					'id' 			=> $row['analyze_id'],
    					'hidden' 		=> $row['analyze_hidden'],
    					'params' 		=> $row['analyze_params'],
    					'software' 		=> $row['analyze_software'],
    					'version' 		=> $row['analyze_version'],
    					'date'			=> $row['analyze_date'],
    					'description' 	=> $row['analyze_description'],
1025
    					'is_editable' 	=> $row['analyze_is_editable'],
Penom Nom's avatar
Penom Nom committed
1026
1027
1028
1029
1030
1031
1032
    					'parent_id'		=> $row['analyze_parent_uid']	
    			);
    		}
    	}
    	return $results;
    }

1033
1034
1035
1036
1037
    /**
     * Hide a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param	string	$p_id	the project id to hide
     */
Jerome Mariette's avatar
Jerome Mariette committed
1038
	function hide_project($p_id) {
1039
1040
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
1041
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1042
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
1043
			tx_nG6_db::hide_run($run_values['id']);
1044
1045
1046
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
1047
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1048
1049
1050
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::hide_analysis($analysis_values['id']);
		}
1051
		tx_nG6_db::unpublish_project($p_id);
1052
1053
1054
1055
1056
1057
1058
1059
1060
		// Finaly change the project itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '1'));
	}

    /**
     * Unhide a project and sublevels (runs & analyzes) datas
     * 
     * @param	string	$p_id	the project id to hide
     */
Jerome Mariette's avatar
Jerome Mariette committed
1061
	function unhide_project($p_id) {
1062
1063
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
1064
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1065
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
1066
			tx_nG6_db::unhide_run($run_values['id']);
1067
1068
1069
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
1070
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
		}
		
		// Finaly change the project itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '0'));
	}

    /**
     * Publish the project
     * 
     * @param	string	$p_id	the project id to publish
     */
Jerome Mariette's avatar
Jerome Mariette committed
1084
	function publish_project($p_id) {
1085
		// First unhide the project
Jerome Mariette's avatar
Jerome Mariette committed
1086
		tx_nG6_db::unhide_project($p_id);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
		// The set the project as visible
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '0'));
	}

    /**
     * Unpublish the project
     * 
     * @param	string	$p_id	the project id to publish
     */
	function unpublish_project($p_id) {
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '1'));
	}

    /**
     * Delete a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param string $p_id			the project id
     * @param string $user_login	the user login
     * @param string $user_pwd		the user password
     * @param string $data_folder	the data folder
     */
Jerome Mariette's avatar
Jerome Mariette committed
1108
	function delete_project($p_id, $user_login, $user_pwd, $data_folder) {
1109
1110
1111
		
		$res = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
1112
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1113
1114
1115
		foreach($project_runs as $run_id => $run_values) {
			if ($run_values['id'] != 'undefined' && $run_values['id'] != '') {
				//delete the run sublevels
Jerome Mariette's avatar
Jerome Mariette committed
1116
				$res = tx_nG6_db::delete_run($run_values['id'], $user_login, $user_pwd, $data_folder);
1117
1118
1119
1120
1121
1122
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
Jerome Mariette's avatar
Jerome Mariette committed
1123
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1124
1125
1126
1127
1128
1129
1130
1131
1132
			foreach($project_analysis as $analyse_id => $analyze_values) {  
				if ($analyze_values['id'] != 'undefined' && $analyze_values['id'] != '') {
					// delete table 'analyze' entries
					$res = tx_nG6_db::delete_analysis($analyze_values['id'], $user_login, $user_pwd, $data_folder);
					if ($res != 0) { break; }
				}
			}
		}
		
1133
1134
1135
1136
1137
1138
		//all project comments
		$project_comments = tx_nG6_db::get_all_project_comments($p_id);
		foreach($project_comments as $id => $values ){
			tx_nG6_db::delete_project_comment($p_id, $values['id']);
		}
		
1139
1140
1141
1142
1143
1144
1145
1146
		// Delete DB project entry
		if ($res == 0) {
			$table='tx_nG6_project';
			$where='tx_nG6_project.uid='.$p_id;
			$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			if ($res1 != 1) {$res = 1;}
		}
		
Jerome Mariette's avatar
Jerome Mariette committed
1147
1148
1149
1150
1151
1152
1153
1154
		// Delete right associated to the project
		if ($res == 0) {
			$table='fe_rights';
			$where='fe_rights.project_id='.$p_id;
			$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			if ($res1 != 1) {$res = 1;}
		}
		
1155
1156
1157
		return $res;
	}

1158
1159
1160
1161
1162
    /**
     * Return the size of the project
     * 
     * @param	string	$p_id	the project id to return the size
	 * @param	string	$data_folder	the data folder
Jerome Mariette's avatar
Jerome Mariette committed
1163
	 * @param	boolean $get_analyzes	get analyzes size ? 
1164
     */
1165
	function get_project_size($p_id, $get_analyzes=false) {
1166
1167
1168
		
		$full_size = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
1169
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1170
		foreach($project_runs as $run_id => $run_values) {
1171
			$full_size += tx_nG6_db::get_run_size($run_values['id'], $get_analyzes);
1172
		}
Jerome Mariette's avatar
Jerome Mariette committed
1173
1174
1175
1176
		if ($get_analyzes) {
			// All analysis
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
			foreach($project_analysis as $analyse_id => $analyze_values) {  
1177
				$full_size += tx_nG6_db::get_analysis_size($analyze_values['id']);
Jerome Mariette's avatar
Jerome Mariette committed
1178
			}
1179
1180
1181
1182
		}
		return $full_size;
	}

1183
1184
1185
1186

	/*
	 * Run functions
	 *------------------------------------------------------------*/
1187
	
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
	/**
     * Add a comment to the run described by run_id
     * @param unknown $run_id
     * @param unknown $cruser_id
     * @param unknown $new_comment
     */
    function add_run_comment($run_id, $cruser_id, $new_comment){
    	return tx_nG6_db::add_comment('tx_nG6_run_comment', 'run_id', $run_id, $cruser_id, $new_comment);
    }
    
    /**
     * Retrieve all comments from a given run
     * @param unknown $run_id
     */
    function get_all_run_comments($run_id){
    	return tx_nG6_db::get_all_comments('tx_nG6_run_comment', 'run_id', $run_id);
    }
    
    /**
     * Delete a given run comment
     * @param unknown $run_id
     * @param unknown $comment_id
     */
    function delete_run_comment($run_id, $comment_id){
    	return tx_nG6_db::delete_comment('tx_nG6_run_comment', 'run_id', $run_id, $comment_id);
    }
1214
	
Jerome Mariette's avatar
Jerome Mariette committed
1215
1216
	/**
	 * Select all run for the specified user
1217
	 * 
1218
	 * @param	string	$user_id	the user id
Jerome Mariette's avatar
Jerome Mariette committed
1219
1220
	 * @return	table with all projects
	 */
1221
	function select_all_user_runs($user_id, $orderby='', $limit='') {
1222

Jerome Mariette's avatar
Jerome Mariette committed
1223
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
1224
1225
1226
		if ($user_id == null) { $where = 'tx_nG6_project.public=0'; } 
		else { $where = 'fe_rights.fe_user_id='.$user_id; }
		
Jerome Mariette's avatar
Jerome Mariette committed
1227
     	// Execute the request
1228
		$queryParts = Array( 
Jerome Mariette's avatar
Jerome Mariette committed
1229
1230
1231
1232
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
					 	'tx_nG6_run.species AS run_species,'.
						'tx_nG6_run.description AS run_description, '.
1233
						'tx_nG6_run.data_nature AS run_data_nature, '. 
Jerome Mariette's avatar
Jerome Mariette committed
1234
1235
1236
1237
1238
1239
1240
1241
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
						'tx_nG6_run.name AS run_name, '.
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
						'tx_nG6_project.uid AS project_id, '.
1242
1243
1244
1245
1246
						'tx_nG6_project.name AS project_name ',
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
						'INNER JOIN tx_nG6_project_run ON tx_nG6_project.uid=tx_nG6_project_run.project_id '.
						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
Jerome Mariette's avatar
Jerome Mariette committed
1247
1248
1249
1250
			'WHERE' => $where,
			'GROUPBY' => '',
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
1251
		); 
Jerome Mariette's avatar
Jerome Mariette committed
1252
1253
1254
1255
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
Jerome Mariette's avatar
Jerome Mariette committed
1256
1257
			$run_id = $row['run_id'];	
			if (!isset($results['run_'.$run_id])) {				
Jerome Mariette's avatar
Jerome Mariette committed
1258
				$results['run_'.$run_id] = array(
1259
1260
					'id'			=> $run_id,  
					'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
					'name' 			=> $row['run_name'],
					'project_name' 	=> $row['project_name'],
					'project_id' 	=> $row['project_id'],
					'hidden'		=> $row['run_hidden'],
					'species' 		=> $row['run_species'],
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'date'			=> $row['run_date'],
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
1272
					'description' 	=> $row['run_description']				
Jerome Mariette's avatar
Jerome Mariette committed
1273
				);
1274
			}						
Jerome Mariette's avatar
Jerome Mariette committed
1275
1276
1277
1278
1279
		}
		return $results;

	}

1280
1281
1282
1283
1284
1285
	/**
	 * Select all information for the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all the run information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
1286
	function select_run($run_id) {
1287
		
1288
		// First select the run from the database
1289
		$queryParts = Array( 
1290
1291
1292
1293
1294
			'SELECT' => 'tx_nG6_project.uid AS project_id,'.
						'tx_nG6_project.name AS project_name,'.
						'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
						'tx_nG6_run.description AS run_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
1295
						'tx_nG6_run.species AS run_species, '.
1296
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
1297
1298
1299
1300
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
						'tx_nG6_run.full_seq_size, '.
Jerome Mariette's avatar
Jerome Mariette committed
1301
						'tx_nG6_run.hidden AS run_hidden, '.
1302
						'tx_nG6_run.nb_sequences, '.
1303
						'tx_nG6_run.storage_size, '.
1304
						'tx_nG6_run.date AS run_date',
1305
1306
1307
1308
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
						'INNER JOIN tx_nG6_project_run ON tx_nG6_project.uid=tx_nG6_project_run.project_id '.
						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
1309
1310
1311
1312
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
1313
		); 
1314
1315
1316
		// Then create the result hash table
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
1317
1318
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$result = array( 
Jerome Mariette's avatar
Jerome Mariette committed
1319
				'id'			=> $row['run_id'],  
1320
1321
1322
				'project_id'	=> $row['project_id'], 
				'project_name'	=> $row['project_name'], 
				'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
1323
				'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
1324
				'hidden'		=> $row['run_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
1325
1326
				'species' 		=> $row['run_species'],
				'nb_sequences' 	=> $row['nb_sequences'],
1327
				'storage_size' 	=> intval($row['storage_size']),
Jerome Mariette's avatar
Jerome Mariette committed
1328
				'full_seq_size' => $row['full_seq_size'],
Jerome Mariette's avatar
Jerome Mariette committed
1329
				'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
1330
1331
1332
				'data_nature' 	=> $row['run_data_nature'],
				'sequencer' 	=> $row['run_sequencer'],
				'type' 			=> $row['run_type'],
1333
1334
				'description' 	=> $row['run_description']				
			);					
1335
1336
1337
		}
		return $result;
    }
1338

1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
	/**
	 * Select all mids linked to the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all mids description
	 */
	function select_mid_descriptions($run_id) {
		
		// First select all analysis from the database
		$queryParts = array( 
Penom Nom's avatar
Penom Nom committed
1349
			'SELECT' => 'sample_id, name ',
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
			'FROM' => 'tx_nG6_sample ',
			'WHERE' => 'run_id='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
Penom Nom's avatar
Penom Nom committed
1360
			$result[$row["sample_id"]] = $row["name"];
1361
1362
1363
1364
1365
		}
		return $result;
		
	}	

1366
1367
1368
1369
1370
1371
	/**
	 * Select all analysis linked to the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
1372
    function get_run_analysis($run_id, $orderby='', $limit='') {
1373
		// First select all analysis from the database
1374
1375
1376
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
1377
						'tx_nG6_analyze.name AS analyze_name, '.
1378
1379
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
1380
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
1381
1382
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
1383
1384
1385
1386
1387
						'tx_nG6_analyze.data_state AS analyze_data_state, '.
						'tx_nG6_analyze.retention_date AS analyze_retention_date, '.
						'tx_nG6_analyze.purged_date AS analyze_purged_date, '.
						'tx_nG6_analyze.purged_size AS analyze_purged_size, '.
						'tx_nG6_analyze.mail_sent_date AS analyze_mail_sent_date, '.
1388
1389
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_analyze.description AS analyze_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
1390
						'tx_nG6_analyze.storage_size AS analyze_storage_size, '.
1391
						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
1392
						'tx_nG6_analyze.parent_uid AS analyze_pare