3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
4 * Copyright 2019, Franck Villaume - TrivialDev
6 * This file is a part of Fusionforge.
8 * Fusionforge is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Fusionforge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Fusionforge. 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 __construct($plugin) {
39 parent::__construct('plugin_hudson_my_jobs');
40 $this->plugin = $plugin;
41 $user=UserManager::instance()->getCurrentUser();
42 $this->_not_monitored_jobs = $user->getPreference('plugin_hudson_my_not_monitored_jobs');
43 if ($this->_not_monitored_jobs === false) {
44 $this->_not_monitored_jobs = array();
46 $this->_not_monitored_jobs = explode(",", $this->_not_monitored_jobs);
49 $this->_use_global_status = $user->getPreference('plugin_hudson_use_global_status');
50 if ($this->_use_global_status === false) {
51 $this->_use_global_status = "false";
52 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
55 if ($this->_use_global_status == "true") {
56 $this->_all_status = array(
62 $this->computeGlobalStatus();
66 function computeGlobalStatus() {
67 $monitored_jobs = $this->_getMonitoredJobsByUser();
68 foreach ($monitored_jobs as $monitored_job) {
70 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
71 $dar = $job_dao->searchByJobID($monitored_job);
73 $row = $dar->current();
74 $job_url = $row['job_url'];
75 $job = new HudsonJob($job_url);
76 $this->_all_status[(string)$job->getColorNoAnime()] = $this->_all_status[(string)$job->getColorNoAnime()] + 1;
78 } catch (Exception $e) {
79 // Do not display wrong jobs
82 if ($this->_all_status['grey'] > 0 || $this->_all_status['red'] > 0) {
83 $this->_global_status = _("One or more failure or pending job");
84 $this->_global_status_icon = $this->plugin->getIconsPath() . "status_red.png";
85 } elseif ($this->_all_status['yellow'] > 0) {
86 $this->_global_status = _("One or more unstable job");
87 $this->_global_status_icon = $this->plugin->getIconsPath() . "status_yellow.png";
89 $this->_global_status = _("Success");
90 $this->_global_status_icon = $this->plugin->getIconsPath() . "status_blue.png";
96 if ($this->_use_global_status == "true") {
97 $title = '<img src="'.$this->_global_status_icon.'" title="'.$this->_global_status.'" alt="'.$this->_global_status.'" /> ';
99 $title .= _("My Hudson Jobs");
103 function getDescription() {
104 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.");
107 function updatePreferences(&$request) {
108 $request->valid(new Valid_String('cancel'));
109 if (!$request->exist('cancel')) {
110 $monitored_jobs = $request->get('myhudsonjobs');
111 $user = UserManager::instance()->getCurrentUser();
112 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
113 $dar = $job_dao->searchByUserID($user->getId());
114 $not_monitored_jobs = array();
115 while ($dar->valid()) {
116 $row = $dar->current();
117 if ( ! in_array($row['job_id'], $monitored_jobs)) {
118 $not_monitored_jobs[] = $row['job_id'];
123 $this->_not_monitored_jobs = $not_monitored_jobs;
124 $user->setPreference('plugin_hudson_my_not_monitored_jobs', implode(",", $this->_not_monitored_jobs));
125 $use_global_status = $request->get('use_global_status');
126 $this->_use_global_status = ($use_global_status !== false)?"true":"false";
127 $user->setPreference('plugin_hudson_use_global_status', $this->_use_global_status);
132 function hasPreferences() {
136 function getPreferences() {
139 $prefs .= '<strong>'._("Monitored jobs:").'</strong><br />';
140 $user = UserManager::instance()->getCurrentUser();
141 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
142 $dar = $job_dao->searchByUserID($user->getId());
143 while ($dar->valid()) {
144 $row = $dar->current();
146 $job = new Hudsonjob($row['job_url']);
147 $prefs .= '<input type="checkbox" name="myhudsonjobs[]" value="'.$row['job_id'].'" '.(in_array($row['job_id'], $this->_not_monitored_jobs)?'':'checked="checked"').'> '.$job->getName().'<br />';
148 } catch (Exception $e) {
149 // Do not display wrong jobs
155 $prefs .= '<strong>'._("Use global status:").'</strong>';
156 $prefs .= '<input type="checkbox" name="use_global_status" value="use_global" '.(($this->_use_global_status == "true")?'checked="checked"':'').'><br />';
160 function getContent() {
161 $monitored_jobs = $this->_getMonitoredJobsByUser();
162 if (sizeof($monitored_jobs) > 0) {
164 $html .= '<table style="width:100%">';
167 foreach ($monitored_jobs as $monitored_job) {
169 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
170 $dar = $job_dao->searchByJobID($monitored_job);
172 $row = $dar->current();
173 $job_url = $row['job_url'];
174 $job_id = $row['job_id'];
175 $group_id = $row['group_id'];
176 $job = new HudsonJob($job_url);
178 $class="boxitemalt bgcolor-white";
180 $class="boxitem bgcolor-grey";
183 $html .= '<tr class="'. $class .'">';
185 $html .= ' <img src="'.$job->getStatusIcon().'" title="'.$job->getStatus().'" >';
187 $html .= ' <td style="width:99%">';
188 $html .= ' <a href="/plugins/hudson/?action=view_job&group_id='.$group_id.'&job_id='.$job_id.'">'.$job->getName().'</a><br />';
194 } catch (Exception $e) {
195 // Do not display wrong jobs
203 function _getMonitoredJobsByUser() {
204 $user = UserManager::instance()->getCurrentUser();
205 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
206 $dar = $job_dao->searchByUserID($user->getId());
207 $monitored_jobs = array();
208 while ($dar->valid()) {
209 $row = $dar->current();
210 if ( ! in_array($row['job_id'], $this->_not_monitored_jobs)) {
211 $monitored_jobs[] = $row['job_id'];
215 return $monitored_jobs;