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=========================================================================*/
18include_once('build.php');
19
20class BuildGroup
21{
22  var $Id;
23  var $Name;
24  var $StartTime;
25  var $EndTime;
26  var $Description;
27  var $SummaryEmail;
28  var $ProjectId;
29
30  function __construct()
31    {
32    $this->StartTime = '1980-01-01 00:00:00';
33    $this->EndTime = '1980-01-01 00:00:00';
34    $this->SummaryEmail = 0;
35    }
36
37  function SetPosition($position)
38    {
39    $position->GroupId = $this->Id;
40    $position->Add();
41    }
42
43  function AddRule($rule)
44    {
45    $rule->GroupId = $this->Id;
46    $rule->Add();
47    }
48
49  /** Get the next position available for that group */
50  function GetNextPosition()
51    {
52    $query = pdo_query("SELECT bg.position FROM buildgroupposition as bg,buildgroup as g
53                        WHERE bg.buildgroupid=g.id AND g.projectid='".$this->ProjectId."'
54                        AND bg.endtime='1980-01-01 00:00:00'
55                        ORDER BY bg.position DESC LIMIT 1");
56    if(pdo_num_rows($query)>0)
57      {
58      $query_array = pdo_fetch_array($query);
59      return $query_array['position']+1;
60      }
61    return 1;
62    }
63
64  /** Check if the group already exists */
65  function Exists()
66    {
67    // If no id specify return false
68    if(!$this->Id || !$this->ProjectId)
69      {
70      return false;
71      }
72
73    $query = pdo_query("SELECT count(*) AS c FROM buildgroup WHERE id='".$this->Id."' AND projectid='".$this->ProjectId."'");
74    add_last_sql_error("BuildGroup:Exists",$this->ProjectId);
75    $query_array = pdo_fetch_array($query);
76    if($query_array['c']==0)
77      {
78      return false;
79      }
80
81    return true;
82    }
83
84  /** Save the group */
85  function Save()
86    {
87    if($this->Exists())
88      {
89      // Update the project
90      $query = "UPDATE buildgroup SET";
91      $query .= " name='".$this->Name."'";
92      $query .= ",projectid='".$this->ProjectId."'";
93      $query .= ",starttime='".$this->StartTime."'";
94      $query .= ",endtime='".$this->EndTime."'";
95      $query .= ",description='".$this->Description."'";
96      $query .= ",summaryemail='".$this->SummaryEmail."'";
97      $query .= " WHERE id='".$this->Id."'";
98
99      if(!pdo_query($query))
100        {
101        add_last_sql_error("BuildGroup:Update",$this->ProjectId);
102        return false;
103        }
104      }
105    else
106      {
107      $id = "";
108      $idvalue = "";
109      if($this->Id)
110        {
111        $id = "id,";
112        $idvalue = "'".$this->Id."',";
113        }
114
115      if(!pdo_query("INSERT INTO buildgroup (".$id."name,projectid,starttime,endtime,description)
116                     VALUES (".$idvalue."'$this->Name','$this->ProjectId','$this->StartTime','$this->EndTime','$this->Description')"))
117        {
118        add_last_sql_error("Buildgroup Insert",$this->ProjectId);
119        return false;
120        }
121
122      if(!$this->Id)
123        {
124        $this->Id = pdo_insert_id("buildgroup");
125        }
126
127      // Insert the default position for this group
128      // Find the position for this group
129      $position = $this->GetNextPosition();
130      pdo_query("INSERT INTO buildgroupposition(buildgroupid,position,starttime,endtime)
131                 VALUES ('".$this->Id."','".$position."','".$this->StartTime."','".$this->EndTime."')");
132
133      }
134    } // end function save
135
136  function GetGroupIdFromRule($build)
137    {
138    $name = $build->Name;
139    $type = $build->Type;
140    $siteid = $build->SiteId;
141    $starttime = $build->StartTime;
142    $projectid = $build->ProjectId;
143
144    // Insert the build into the proper group
145    // 1) Check if we have any build2grouprules for this build
146    $build2grouprule = pdo_query("SELECT b2g.groupid FROM build2grouprule AS b2g, buildgroup as bg
147                                  WHERE b2g.buildtype='$type' AND b2g.siteid='$siteid' AND b2g.buildname='$name'
148                                  AND (b2g.groupid=bg.id AND bg.projectid='$projectid')
149                                  AND '$starttime'>b2g.starttime
150                                  AND ('$starttime'<b2g.endtime OR b2g.endtime='1980-01-01 00:00:00')");
151
152    if(pdo_num_rows($build2grouprule)>0)
153      {
154      $build2grouprule_array = pdo_fetch_array($build2grouprule);
155      return $build2grouprule_array["groupid"];
156      }
157    else // we don't have any rules we use the type
158      {
159      $buildgroup = pdo_query("SELECT id FROM buildgroup WHERE name='$type' AND projectid='$projectid'");
160      if(pdo_num_rows($buildgroup)==0) // if the group does not exist we assign it to experimental
161        {
162        $buildgroup = pdo_query("SELECT id FROM buildgroup WHERE name='Experimental' AND projectid='$projectid'");
163        }
164      $buildgroup_array = pdo_fetch_array($buildgroup);
165      return $buildgroup_array["id"];
166      }
167    }
168
169  // Return the value of summaryemail
170  function GetSummaryEmail()
171    {
172    if(!$this->Id)
173      {
174      echo "BuildGroup GetSummaryEmail(): Id not set";
175      return false;
176      }
177    $summaryemail = pdo_query("SELECT summaryemail FROM buildgroup WHERE id=".qnum($this->Id));
178    if(!$summaryemail)
179      {
180      add_last_sql_error("BuildGroup GetSummaryEmail",$this->ProjectId);
181      return false;
182      }
183
184    $summaryemail_array = pdo_fetch_array($summaryemail);
185    return $summaryemail_array["summaryemail"];
186    }
187
188  // Return the value of emailcommitters, 0 or 1
189  function GetEmailCommitters()
190    {
191    if(!$this->Id)
192      {
193      add_log('no BuildGroup Id, cannot query database, returning default value of 0',
194        'BuildGroup::GetEmailCommitters', LOG_ERR);
195      return 0;
196      }
197
198    $emailCommitters = pdo_get_field_value(
199      "SELECT emailcommitters FROM buildgroup WHERE id=".qnum($this->Id),
200      'emailcommitters', 0);
201
202    return $emailCommitters;
203    }
204}
205
206?>
207