3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi 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 * Codendi 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 Codendi. If not, see <http://www.gnu.org/licenses/>.
22 require_once('HudsonOverviewWidget.class.php');
23 //require_once('common/user/UserManager.class.php');
24 require_once('common/include/HTTPRequest.class.php');
25 require_once('PluginHudsonJobDao.class.php');
26 require_once('HudsonJob.class.php');
28 class hudson_Widget_MyMonitoredJobs extends HudsonOverviewWidget {
32 var $_not_monitored_jobs;
33 var $_use_global_status;
36 var $_global_status_icon;
38 function hudson_Widget_MyMonitoredJobs($plugin) {
39 $this->Widget('plugin_hudson_my_jobs');
40 $this->plugin = $plugin;
41 $user=UserManager::instance()->getCurrentUser();
43 $this->_not_monitored_jobs = $user->getPreference('plugin_hudson_my_not_monitored_jobs');
44 if ($this->_not_monitored_jobs === false) {
45 $this->_not_monitored_jobs = array();
47 $this->_not_monitored_jobs = explode(",", $this->_not_monitored_jobs);
50 $this->_use_global_status = $user->getPreference('plugin_hudson_use_global_status');
51 if ($this->_use_global_status === false) {
52 $this->_use_global_status = "false";
53 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
56 if ($this->_use_global_status == "true") {
57 $this->_all_status = array(
63 $this->computeGlobalStatus();
68 function computeGlobalStatus() {
69 $monitored_jobs = $this->_getMonitoredJobsByUser();
70 foreach ($monitored_jobs as $monitored_job) {
72 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
73 $dar = $job_dao->searchByJobID($monitored_job);
75 $row = $dar->current();
76 $job_url = $row['job_url'];
77 $job = new HudsonJob($job_url);
78 $this->_all_status[(string)$job->getColorNoAnime()] = $this->_all_status[(string)$job->getColorNoAnime()] + 1;
80 } catch (Exception $e) {
81 // Do not display wrong jobs
84 if ($this->_all_status['grey'] > 0 || $this->_all_status['red'] > 0) {
85 $this->_global_status = _("One or more failure or pending job");
86 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_red.png";
87 } elseif ($this->_all_status['yellow'] > 0) {
88 $this->_global_status = _("One or more unstable job");
89 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_yellow.png";
91 $this->_global_status = _("Success");
92 $this->_global_status_icon = $this->plugin->getThemePath() . "/images/ic/" . "status_blue.png";
98 if ($this->_use_global_status == "true") {
99 $title = '<img src="'.$this->_global_status_icon.'" title="'.$this->_global_status.'" alt="'.$this->_global_status.'" /> ';
101 $title .= _("My Hudson Jobs");
105 function getDescription() {
106 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.");
109 function updatePreferences(&$request) {
110 $request->valid(new Valid_String('cancel'));
111 if (!$request->exist('cancel')) {
112 $monitored_jobs = $request->get('myhudsonjobs');
114 $user = UserManager::instance()->getCurrentUser();
115 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
116 $dar = $job_dao->searchByUserID($user->getId());
117 $not_monitored_jobs = array();
118 while ($dar->valid()) {
119 $row = $dar->current();
120 if ( ! in_array($row['job_id'], $monitored_jobs)) {
121 $not_monitored_jobs[] = $row['job_id'];
126 $this->_not_monitored_jobs = $not_monitored_jobs;
128 $user->setPreference('plugin_hudson_my_not_monitored_jobs', implode(",", $this->_not_monitored_jobs));
130 $use_global_status = $request->get('use_global_status');
131 $this->_use_global_status = ($use_global_status !== false)?"true":"false";
132 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
136 function hasPreferences() {
139 function getPreferences() {
142 $prefs .= '<strong>'._("Monitored jobs:").'</strong><br />';
143 $user = UserManager::instance()->getCurrentUser();
144 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
145 $dar = $job_dao->searchByUserID($user->getId());
146 while ($dar->valid()) {
147 $row = $dar->current();
149 $job = new Hudsonjob($row['job_url']);
150 $prefs .= '<input type="checkbox" name="myhudsonjobs[]" value="'.$row['job_id'].'" '.(in_array($row['job_id'], $this->_not_monitored_jobs)?'':'checked="checked"').'> '.$job->getName().'<br />';
151 } catch (Exception $e) {
152 // Do not display wrong jobs
158 $prefs .= '<strong>'._("Use global status:").'</strong>';
159 $prefs .= '<input type="checkbox" name="use_global_status" value="use_global" '.(($this->_use_global_status == "true")?'checked="checked"':'').'><br />';
163 function getContent() {
164 $monitored_jobs = $this->_getMonitoredJobsByUser();
165 if (sizeof($monitored_jobs) > 0) {
167 $html .= '<table style="width:100%">';
170 foreach ($monitored_jobs as $monitored_job) {
173 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
174 $dar = $job_dao->searchByJobID($monitored_job);
176 $row = $dar->current();
177 $job_url = $row['job_url'];
178 $job_id = $row['job_id'];
179 $group_id = $row['group_id'];
180 $job = new HudsonJob($job_url);
182 $class="boxitemalt bgcolor-white";
184 $class="boxitem bgcolor-grey";
187 $html .= '<tr class="'. $class .'">';
189 $html .= ' <img src="'.$job->getStatusIcon().'" title="'.$job->getStatus().'" >';
191 $html .= ' <td style="width:99%">';
192 $html .= ' <a href="/plugins/hudson/?action=view_job&group_id='.$group_id.'&job_id='.$job_id.'">'.$job->getName().'</a><br />';
198 } catch (Exception $e) {
199 // Do not display wrong jobs
207 function _getMonitoredJobsByUser() {
208 $user = UserManager::instance()->getCurrentUser();
209 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
210 $dar = $job_dao->searchByUserID($user->getId());
211 $monitored_jobs = array();
212 while ($dar->valid()) {
213 $row = $dar->current();
214 if ( ! in_array($row['job_id'], $this->_not_monitored_jobs)) {
215 $monitored_jobs[] = $row['job_id'];
219 return $monitored_jobs;