3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Fusionforge.
7 * Fusionforge is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Fusionforge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
21 require_once 'HudsonOverviewWidget.class.php';
22 //require_once('common/user/UserManager.class.php');
23 require_once 'common/include/HTTPRequest.class.php';
24 require_once 'PluginHudsonJobDao.class.php';
25 require_once 'HudsonJob.class.php';
27 class hudson_Widget_MyMonitoredJobs extends HudsonOverviewWidget {
31 var $_not_monitored_jobs;
32 var $_use_global_status;
35 var $_global_status_icon;
37 function hudson_Widget_MyMonitoredJobs($plugin) {
38 $this->Widget('plugin_hudson_my_jobs');
39 $this->plugin = $plugin;
40 $user=UserManager::instance()->getCurrentUser();
41 $this->_not_monitored_jobs = $user->getPreference('plugin_hudson_my_not_monitored_jobs');
42 if ($this->_not_monitored_jobs === false) {
43 $this->_not_monitored_jobs = array();
45 $this->_not_monitored_jobs = explode(",", $this->_not_monitored_jobs);
48 $this->_use_global_status = $user->getPreference('plugin_hudson_use_global_status');
49 if ($this->_use_global_status === false) {
50 $this->_use_global_status = "false";
51 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
54 if ($this->_use_global_status == "true") {
55 $this->_all_status = array(
61 $this->computeGlobalStatus();
65 function computeGlobalStatus() {
66 $monitored_jobs = $this->_getMonitoredJobsByUser();
67 foreach ($monitored_jobs as $monitored_job) {
69 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
70 $dar = $job_dao->searchByJobID($monitored_job);
72 $row = $dar->current();
73 $job_url = $row['job_url'];
74 $job = new HudsonJob($job_url);
75 $this->_all_status[(string)$job->getColorNoAnime()] = $this->_all_status[(string)$job->getColorNoAnime()] + 1;
77 } catch (Exception $e) {
78 // Do not display wrong jobs
81 if ($this->_all_status['grey'] > 0 || $this->_all_status['red'] > 0) {
82 $this->_global_status = _("One or more failure or pending job");
83 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_red.png";
84 } elseif ($this->_all_status['yellow'] > 0) {
85 $this->_global_status = _("One or more unstable job");
86 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_yellow.png";
88 $this->_global_status = _("Success");
89 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_blue.png";
95 if ($this->_use_global_status == "true") {
96 $title = '<img src="'.$this->_global_status_icon.'" title="'.$this->_global_status.'" alt="'.$this->_global_status.'" /> ';
98 $title .= _("My Hudson Jobs");
102 function getDescription() {
103 return _("Show an overview of all the jobs of all the projects you're member of. You can of course select the jobs you wish to display by selecting the preferences link of the widget.");
106 function updatePreferences(&$request) {
107 $request->valid(new Valid_String('cancel'));
108 if (!$request->exist('cancel')) {
109 $monitored_jobs = $request->get('myhudsonjobs');
110 $user = UserManager::instance()->getCurrentUser();
111 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
112 $dar = $job_dao->searchByUserID($user->getId());
113 $not_monitored_jobs = array();
114 while ($dar->valid()) {
115 $row = $dar->current();
116 if ( ! in_array($row['job_id'], $monitored_jobs)) {
117 $not_monitored_jobs[] = $row['job_id'];
122 $this->_not_monitored_jobs = $not_monitored_jobs;
123 $user->setPreference('plugin_hudson_my_not_monitored_jobs', implode(",", $this->_not_monitored_jobs));
124 $use_global_status = $request->get('use_global_status');
125 $this->_use_global_status = ($use_global_status !== false)?"true":"false";
126 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
131 function hasPreferences() {
135 function getPreferences() {
138 $prefs .= '<strong>'._("Monitored jobs:").'</strong><br />';
139 $user = UserManager::instance()->getCurrentUser();
140 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
141 $dar = $job_dao->searchByUserID($user->getId());
142 while ($dar->valid()) {
143 $row = $dar->current();
145 $job = new Hudsonjob($row['job_url']);
146 $prefs .= '<input type="checkbox" name="myhudsonjobs[]" value="'.$row['job_id'].'" '.(in_array($row['job_id'], $this->_not_monitored_jobs)?'':'checked="checked"').'> '.$job->getName().'<br />';
147 } catch (Exception $e) {
148 // Do not display wrong jobs
154 $prefs .= '<strong>'._("Use global status:").'</strong>';
155 $prefs .= '<input type="checkbox" name="use_global_status" value="use_global" '.(($this->_use_global_status == "true")?'checked="checked"':'').'><br />';
159 function getContent() {
160 $monitored_jobs = $this->_getMonitoredJobsByUser();
161 if (sizeof($monitored_jobs) > 0) {
163 $html .= '<table style="width:100%">';
166 foreach ($monitored_jobs as $monitored_job) {
168 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
169 $dar = $job_dao->searchByJobID($monitored_job);
171 $row = $dar->current();
172 $job_url = $row['job_url'];
173 $job_id = $row['job_id'];
174 $group_id = $row['group_id'];
175 $job = new HudsonJob($job_url);
177 $class="boxitemalt bgcolor-white";
179 $class="boxitem bgcolor-grey";
182 $html .= '<tr class="'. $class .'">';
184 $html .= ' <img src="'.$job->getStatusIcon().'" title="'.$job->getStatus().'" >';
186 $html .= ' <td style="width:99%">';
187 $html .= ' <a href="/plugins/hudson/?action=view_job&group_id='.$group_id.'&job_id='.$job_id.'">'.$job->getName().'</a><br />';
193 } catch (Exception $e) {
194 // Do not display wrong jobs
202 function _getMonitoredJobsByUser() {
203 $user = UserManager::instance()->getCurrentUser();
204 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
205 $dar = $job_dao->searchByUserID($user->getId());
206 $monitored_jobs = array();
207 while ($dar->valid()) {
208 $row = $dar->current();
209 if ( ! in_array($row['job_id'], $this->_not_monitored_jobs)) {
210 $monitored_jobs[] = $row['job_id'];
214 return $monitored_jobs;