1<?php
2/*=========================================================================
3
4  Program:   CDash - Cross-Platform Dashboard System
5  Module:    $Id$
6  Language:  PHP
7  Date:      $Date$
8  Version:   $Revision$
9
10  Copyright (c) 2002 Kitware, Inc.  All rights reserved.
11  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12
13     This software is distributed WITHOUT ANY WARRANTY; without even
14     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15     PURPOSE.  See the above copyright notices for more information.
16
17=========================================================================*/
18/** Coverage file to users */
19class CoverageFile2User
20{
21  var $UserId;
22  var $FileId;
23  var $Priority;
24  var $FullPath;
25  var $ProjectId;
26
27  /** Constructor */
28  function __construct()
29    {
30    $this->UserId = 0;
31    $this->FileId = 0;
32    $this->Priority = 0;
33    $this->Fullpath = '';
34    $this->ProjectId = 0;
35    }
36
37  /** Return if exists */
38  function Exists()
39    {
40    $fileid = $this->GetId();
41
42    if($fileid == 0)
43      {
44      return false;
45      }
46
47    $query = pdo_query("SELECT count(*) AS c FROM coveragefile2user WHERE userid=".qnum($this->UserId)."
48                        AND fileid=".qnum($fileid));
49    $query_array = pdo_fetch_array($query);
50    if($query_array['c']>0)
51      {
52      return true;
53      }
54    return false;
55    }
56
57  /** Insert the new user */
58  function Insert()
59    {
60    if(!isset($this->UserId) || $this->UserId<1)
61      {
62      echo "CoverageFile2User:Insert: UserId not set";
63      return false;
64      }
65
66    if($this->FullPath=='' || $this->ProjectId<1)
67      {
68      echo "CoverageFile2User:Insert: FullPath or ProjectId not set";
69      return false;
70      }
71
72    // Check if is already in the database
73    if(!$this->Exists())
74      {
75      $this->FileId = $this->GetId();
76
77      if($this->FileId == 0)
78        {
79        $query = "INSERT INTO coveragefilepriority (projectid,fullpath,priority)
80                  VALUES (".qnum($this->ProjectId).",'".$this->FullPath."',0)";
81        if(!pdo_query($query))
82          {
83          add_last_sql_error("CoverageFile2User:Insert");
84          return false;
85          }
86        $this->FileId = pdo_insert_id("coveragefilepriority");
87        }
88
89      // Find the new position
90      $query = pdo_query("SELECT count(*) AS c FROM coveragefile2user WHERE fileid=".qnum($this->FileId));
91      $query_array = pdo_fetch_array($query);
92      $position = $query_array['c']+1;
93
94      $query = "INSERT INTO coveragefile2user (userid,fileid,position)
95                VALUES (".qnum($this->UserId).",".qnum($this->FileId).",".qnum($position).")";
96      if(!pdo_query($query))
97        {
98        add_last_sql_error("CoverageFile2User:Insert");
99        return false;
100        }
101      return true;
102      }
103    return false;
104    } // function Insert
105
106  /** Remove authors */
107  function RemoveAuthors()
108    {
109    if($this->FullPath=='' || $this->ProjectId<1)
110      {
111      echo "CoverageFile2User:RemoveAuthors: FullPath or ProjectId not set";
112      return false;
113      }
114
115    $query = "DELETE FROM coveragefile2user WHERE fileid=".qnum($this->GetId());
116    if(!pdo_query($query))
117      {
118      add_last_sql_error("CoverageFile2User:RemoveAuthors");
119      echo $query;
120      return false;
121      }
122    }
123
124  /** Remove the new user */
125  function Remove()
126    {
127    if(!isset($this->UserId) || $this->UserId<1)
128      {
129      return false;
130      }
131    if(!isset($this->FileId) || $this->FileId<1)
132      {
133      return false;
134      }
135
136    $query = "DELETE FROM coveragefile2user WHERE userid=".qnum($this->UserId)."
137                AND fileid=".qnum($this->FileId);
138    if(!pdo_query($query))
139      {
140      add_last_sql_error("CoverageFile2User:Remove");
141      return false;
142      }
143
144    $this->FixPosition();
145
146    return true;
147    } // end function Remove
148
149  /** Fix the position given a file */
150  private function FixPosition()
151    {
152    if(!isset($this->FileId) || $this->FileId<1)
153      {
154      return false;
155      }
156
157    $query = pdo_query("SELECT userid FROM coveragefile2user WHERE fileid=".qnum($this->FileId)." ORDER BY position ASC");
158    if(!$query)
159      {
160      add_last_sql_error("CoverageFile2User:FixPosition");
161      return false;
162      }
163
164    $position = 1;
165    while($query_array = pdo_fetch_array($query))
166      {
167      pdo_query("UPDATE coveragefile2user SET position=".qnum($position)." WHERE fileid=".qnum($this->FileId)."
168                 AND userid=".qnum($query_array['userid']));
169      $position ++;
170      }
171    return true;
172    } // end FixPosition
173
174  /** Get authors of a file */
175  function GetAuthors()
176    {
177    if($this->FullPath=='' || $this->ProjectId<1)
178      {
179      echo "CoverageFile2User:GetAuthors: FullPath or ProjectId not set";
180      return false;
181      }
182    $query = pdo_query("SELECT userid FROM coveragefile2user,coveragefilepriority WHERE
183                       coveragefile2user.fileid=coveragefilepriority.id AND
184                       coveragefilepriority.fullpath='".$this->FullPath."' AND coveragefilepriority.projectid=".qnum($this->ProjectId)." ORDER BY position ASC");
185    if(!$query)
186      {
187      add_last_sql_error("CoverageFile2User:GetAuthors");
188      return false;
189      }
190    $authorids = array();
191    while($query_array = pdo_fetch_array($query))
192      {
193      $authorids[] = $query_array['userid'];
194      }
195    return $authorids;
196    } // end function GetAuthors
197
198  /** Get id of a file */
199  function GetId()
200    {
201    if($this->FullPath=='' || $this->ProjectId<1)
202      {
203      echo "CoverageFile2User:GetId: FullPath or ProjectId not set";
204      return false;
205      }
206    $query = pdo_query("SELECT id FROM coveragefilepriority WHERE
207                       coveragefilepriority.fullpath='".$this->FullPath."' AND coveragefilepriority.projectid=".qnum($this->ProjectId));
208    if(!$query)
209      {
210      add_last_sql_error("CoverageFile2User:GetId");
211      return false;
212      }
213    if(pdo_num_rows($query) == 0)
214      {
215      return 0;
216      }
217    $query_array = pdo_fetch_array($query);
218    return $query_array['id'];
219    } // end function GetAuthors
220
221  /** Get files given an author */
222  function GetFiles()
223    {
224    if(!isset($this->UserId) || $this->UserId<1)
225      {
226      echo "CoverageFile2User:GetFiles: UserId not set";
227      return false;
228      }
229    $query = pdo_query("SELECT fileid FROM coveragefile2user WHERE userid=".qnum($this->UserId));
230    if(!$query)
231      {
232      add_last_sql_error("CoverageFile2User:GetFiles");
233      return false;
234      }
235
236    $fileids = array();
237    while($query_array = pdo_fetch_array($query))
238      {
239      $fileids[] = $query_array['fileid'];
240      }
241    return $fileids;
242    } // end function GetFiles
243
244  /** Return the actualy coverage file id */
245  function GetCoverageFileId($buildid)
246    {
247    if($this->FileId == 0)
248      {
249      echo "CoverageFile2User:GetCoverageFileId: FileId not set";
250      return false;
251      }
252
253    $query = pdo_query("SELECT coveragefile.id AS id FROM coveragefile,coveragefilepriority,coverage WHERE
254                        coveragefilepriority.id=".qnum($this->FileId)."
255                        AND coverage.buildid=".qnum($buildid)."
256                        AND coverage.fileid=coveragefile.id
257                        AND coveragefilepriority.fullpath=coveragefile.fullpath");
258    if(!$query)
259      {
260      add_last_sql_error("CoverageFile2User:GetCoverageFileId");
261      return false;
262      }
263
264    $query_array = pdo_fetch_array($query);
265    return $query_array['id'];
266    }
267
268  /** Get the list of authors for the project */
269  function GetUsersFromProject()
270    {
271    if(!isset($this->ProjectId) || $this->ProjectId<1)
272      {
273      echo "CoverageFile2User:GetUsersFromProject: projectid not valid";
274      return false;
275      }
276
277    $query = pdo_query("SELECT DISTINCT userid FROM coveragefile2user,coveragefilepriority WHERE
278                        coveragefilepriority.id=coveragefile2user.fileid
279                        AND coveragefilepriority.projectid=".qnum($this->ProjectId));
280    if(!$query)
281      {
282      add_last_sql_error("CoverageFile2User:GetUsersFromProject");
283      return false;
284      }
285    $userids = array();
286    while($query_array = pdo_fetch_array($query))
287      {
288      $userids[] = $query_array['userid'];
289      }
290    return $userids;
291    } // end GetUsersFromProject
292
293  /** Assign the last author */
294  function AssignLastAuthor($buildid,$beginUTCTime,$currentUTCTime)
295    {
296    include_once('models/dailyupdate.php');
297
298    if(!isset($this->ProjectId) || $this->ProjectId<1)
299      {
300      echo "CoverageFile2User:AssignLastAuthor: ProjectId not set";
301      return false;
302      }
303
304    if($buildid==0)
305      {
306      echo "CoverageFile2User:AssignLastAuthor: buildid not valid";
307      return false;
308      }
309
310    // Find the files associated with the build
311    $Coverage = new Coverage();
312    $Coverage->BuildId = $buildid;
313    $fileIds = $Coverage->GetFiles();
314    foreach($fileIds as $fileid)
315      {
316      $CoverageFile = new CoverageFile();
317      $CoverageFile->Id = $fileid;
318      $fullpath = $CoverageFile->GetPath();
319
320      $DailyUpdate = new DailyUpdate();
321      $DailyUpdate->ProjectId = $this->ProjectId;
322      $userids = $DailyUpdate->GetAuthors($fullpath,true); // only last
323
324      foreach($userids as $userid)
325        {
326        $this->FullPath = $fullpath;
327        $this->UserId = $userid;
328        $this->Insert();
329        }
330      }
331
332    return true;
333    } // end AssignLastAuthor
334
335  /** Assign all author author */
336  function AssignAllAuthors($buildid,$beginUTCTime,$currentUTCTime)
337    {
338    include_once('models/dailyupdate.php');
339
340    if(!isset($this->ProjectId) || $this->ProjectId<1)
341      {
342      echo "CoverageFile2User:AssignLastAuthor: ProjectId not set";
343      return false;
344      }
345
346    if($buildid==0)
347      {
348      echo "CoverageFile2User:AssignLastAuthor: buildid not valid";
349      return false;
350      }
351
352    // Find the files associated with the build
353    $Coverage = new Coverage();
354    $Coverage->BuildId = $buildid;
355    $fileIds = $Coverage->GetFiles();
356    foreach($fileIds as $fileid)
357      {
358      $CoverageFile = new CoverageFile();
359      $CoverageFile->Id = $fileid;
360      $fullpath = $CoverageFile->GetPath();
361
362      $DailyUpdate = new DailyUpdate();
363      $DailyUpdate->ProjectId = $this->ProjectId;
364      $userids = $DailyUpdate->GetAuthors($fullpath);
365
366      foreach($userids as $userid)
367        {
368        $this->FullPath = $fullpath;
369        $this->UserId = $userid;
370        $this->Insert();
371        }
372      }
373
374    return true;
375    } // end AssignAllAuthors
376
377  // Function get the priority to a file
378  function GetPriority()
379    {
380    if($this->FullPath=='' || $this->ProjectId<1)
381      {
382      echo "CoverageFile2User:GetPriority: FullPath or ProjectId not set";
383      return false;
384      }
385
386    $query = pdo_query("SELECT priority FROM coveragefilepriority WHERE fullpath='".$this->FullPath."' AND projectid=".qnum($this->ProjectId));
387    if(!$query)
388      {
389      add_last_sql_error("CoverageFile2User:GetPriority");
390      return false;
391      }
392
393    if(pdo_num_rows($query) == 0)
394      {
395      return 0;
396      }
397    $query_array = pdo_fetch_array($query);
398    return $query_array[0];
399    }
400
401  // Function set the priority to a file
402  function SetPriority($priority)
403    {
404    if($this->ProjectId == 0)
405      {
406      echo "CoverageFile2User:SetPriority:ProjectId not set";
407      return false;
408      }
409    if($this->FullPath == '')
410      {
411      echo "CoverageFile2User:SetPriority:FullPath not set";
412      return false;
413      }
414    $query = pdo_query("SELECT count(*) FROM coveragefilepriority WHERE FullPath='".$this->FullPath."'");
415    if(!$query)
416      {
417      add_last_sql_error("CoverageFile2User:SetPriority");
418      return false;
419      }
420
421    $sql = "";
422    $query_array = pdo_fetch_array($query);
423    if($query_array[0] == 0)
424      {
425      $sql = "INSERT INTO coveragefilepriority (projectid,priority,fullpath) VALUES (".qnum($this->ProjectId).",".qnum($priority).",'".$this->FullPath."')";
426      }
427    else
428      {
429      $sql = "UPDATE coveragefilepriority set priority=".qnum($priority)." WHERE fullpath='".$this->FullPath."' AND projectid=".qnum($this->ProjectId);
430      }
431
432    $query = pdo_query($sql);
433    if(!$query)
434      {
435      add_last_sql_error("CoverageFile2User:SetPriority");
436      return false;
437      }
438    return true;
439    }
440
441
442
443}
444?>
445