4 * ContribTrackerPlugin Class
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * GForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 class ContribTrackerPlugin extends Plugin {
26 function ContribTrackerPlugin () {
28 $this->name = "contribtracker" ;
29 $this->text = "Contribution Tracker" ; // To show in the tabs, use...
30 $this->hooks[] = "groupmenu" ; // To put into the project tabs
31 $this->hooks[] = "groupisactivecheckbox" ; // The "use ..." checkbox in editgroupinfo
32 $this->hooks[] = "groupisactivecheckboxpost" ; //
33 $this->hooks[] = "project_admin_plugins"; // to show up in the admin page for group
34 $this->hooks[] = "project_before_frs"; // project summary page
35 $this->hooks[] = "site_admin_option_hook"; // to show in the site admin page
38 function CallHook ($hookname, $params) {
39 if ($hookname == "groupmenu") {
40 $group_id=$params['group'];
41 $project = &group_get_object($group_id);
42 if (!$project || !is_object($project)) {
45 if ($project->isError()) {
48 if (!$project->isProject()) {
51 if ( $project->usesPlugin ( $this->name ) ) {
52 $params['TITLES'][] = _('Contribution tracker') ;
53 $params['DIRS'][]='/plugins/'.$this->name.'/?group_id=' . $group_id ;
55 (($params['toptab'] == $this->name) ? $params['selected']=(count($params['TITLES'])-1) : '' );
56 } elseif ($hookname == "groupisactivecheckbox") {
57 //Check if the group is active
58 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
59 $group_id=$params['group'];
60 $group = &group_get_object($group_id);
63 echo ' <input type="CHECKBOX" name="use_contribtrackerplugin" value="1" ';
64 // CHECKED OR UNCHECKED?
65 if ( $group->usesPlugin ( $this->name ) ) {
72 echo _('Use the Contribution Tracker plugin') ;
76 } elseif ($hookname == "groupisactivecheckboxpost") {
77 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
78 $group_id=$params['group'];
79 $group = &group_get_object($group_id);
80 $use_contribtrackerplugin = getStringFromRequest('use_contribtrackerplugin');
81 if ( $use_contribtrackerplugin == 1 ) {
82 $group->setPluginUse ( $this->name );
84 $group->setPluginUse ( $this->name, false );
86 } elseif ($hookname == "project_admin_plugins") {
87 // this displays the link in the project admin options page to its ContribTracker administration
88 $group_id = $params['group_id'];
89 $group = &group_get_object($group_id);
90 if ( $group->usesPlugin ( $this->name ) ) {
91 echo util_make_link ("/plugins/".$this->name."/project_admin.php?group_id=".$group->getID(),
92 _('Contribution Tracker admin')) ;
97 elseif ($hookname == "project_before_frs") {
98 $group_id = $params['group_id'];
99 $group = &group_get_object ($group_id);
101 if ($group->usesPlugin($this->name)) {
103 echo $HTML->boxTop(_('Latest Major Contributions'));
106 <table cellspacing="1" cellpadding="5" width="100%" border="0">
108 <td style="text-align:left">
109 '._('Contribution').'
110 <td style="text-align:center">
111 '._('Contributing organisation').'
115 $contribs = $this->getContributionsByGroup ($group) ;
116 usort ($contribs, array ($this, "ContribComparator")) ;
118 if (count ($contribs) == 0) {
119 echo '<tr><td colspan="5"><strong>'._('No contributions have been recorded for this project yet.').'</strong></td></tr>';
121 $max_displayed_contribs = 3 ;
123 foreach ($contribs as $c) {
126 echo strftime (_('%Y-%m-%d'), $c->getDate ()) ;
128 echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id.'&contrib_id='.$c->getId(),htmlspecialchars($c->getName())) ;
129 echo '</td><td><ul>' ;
131 $parts = $c->getParticipations () ;
132 foreach ($parts as $p) {
134 printf (_('%s: %s (%s)'),
135 htmlspecialchars ($p->getRole()->getName()),
136 util_make_link ('/plugins/'.$this->name.'/?actor_id='.$p->getActor()->getId (),
137 htmlspecialchars ($p->getActor()->getName())),
138 htmlspecialchars ($p->getActor()->getLegalStructure()->getName())) ;
139 if ($p->getActor()->getLogo() != '') {
140 print ' <img type="image/png" src="'.util_make_url ('/plugins/'.$this->name.'/actor_logo.php?actor_id='.$p->getActor()->getId ()).'" />' ;
144 echo '</ul></td></tr>' ;
147 if ($i > $max_displayed_contribs) {
153 <div style="text-align:center">
154 <?php echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id,_('[View All Contributions]')); ?>
157 echo $HTML->boxBottom();
160 elseif ($hookname == "site_admin_option_hook") {
162 <li><?php echo util_make_link ('/plugins/'.$this->name.'/global_admin.php',
163 _('Edit actors and roles for the contribution tracker plugin')
170 function getActors () {
171 $res = db_query_params ('SELECT actor_id FROM plugin_contribtracker_actor',
173 $ids = util_result_column_to_array ($res, 0) ;
175 $results = array () ;
176 foreach ($ids as $id) {
177 $results[] = new ContribTrackerActor ($id) ;
183 function getLegalStructures () {
184 $res = db_query_params ('SELECT struct_id FROM plugin_contribtracker_legal_structure',
186 $ids = util_result_column_to_array ($res, 0) ;
188 $results = array () ;
189 foreach ($ids as $id) {
190 $results[] = new ContribTrackerLegalStructure ($id) ;
196 function getRoles () {
197 $res = db_query_params ('SELECT role_id FROM plugin_contribtracker_role',
199 $ids = util_result_column_to_array ($res, 0) ;
201 $results = array () ;
202 foreach ($ids as $id) {
203 $results[] = new ContribTrackerRole ($id) ;
209 function getContributions () {
210 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution',
212 $ids = util_result_column_to_array ($res, 0) ;
214 $results = array () ;
215 foreach ($ids as $id) {
216 $results[] = new ContribTrackerContribution ($id) ;
222 function getContributionsByGroup ($group) {
223 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution WHERE group_id = $1',
224 array ($group->getId())) ;
225 $ids = util_result_column_to_array ($res, 0) ;
227 $results = array () ;
228 foreach ($ids as $id) {
229 $results[] = new ContribTrackerContribution ($id) ;
235 function ContribComparator ($a, $b) {
236 if ($a->getDate() != $b->getDate()) {
237 return ($a->getDate() < $b->getDate()) ? -1 : 1 ;
238 } elseif ($a->getName() != $b->getName()) {
239 return ($a->getName() < $b->getName()) ? -1 : 1 ;
246 class ContribTrackerRole extends Error {
249 function ContribTrackerRole ($id=false) {
254 return $this->fetchData ($id) ;
257 function fetchData ($id) {
258 $res = db_query_params ('SELECT * FROM plugin_contribtracker_role WHERE role_id=$1',
260 if (!$res || db_numrows($res) < 1) {
261 $this->setError(sprintf('ContribTrackerRole(): %s',db_error()));
265 $this->data_array = db_fetch_array ($res) ;
269 function create ($name, $description) {
270 if ($this->getId ()) {
271 $this->setError(_('Object already exists')) ;
276 $res = db_query_params ('INSERT INTO plugin_contribtracker_role (name, description) VALUES ($1,$2)',
279 if (!$res || db_affected_rows ($res) < 1) {
280 $this->setError (sprintf(_('Could not create object in database: %s'),
286 $id = db_insertid ($res, 'plugin_contribtracker_role', 'role_id') ;
288 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
295 return $this->fetchData ($id) ;
298 function update ($name, $description) {
299 if (! $this->getId ()) {
300 $this->setError(_('Object does not exist')) ;
304 $id = $this->getId () ;
307 $res = db_query_params ('UPDATE plugin_contribtracker_role SET (name, description) = ($1,$2) WHERE role_id = $3',
311 if (!$res || db_affected_rows ($res) < 1) {
312 $this->setError (sprintf(_('Could not update object in database: %s'),
319 return $this->fetchData ($id) ;
323 $id = $this->getId () ;
325 $this->setError (_('Cannot delete a non-existing object')) ;
329 $res = db_query_params ('DELETE FROM plugin_contribtracker_role WHERE role_id = $1',
332 $this->setError (sprintf(_('Could not delete object in database: %s'),
337 $this->data_array = array () ;
343 if (isset ($this->data_array['role_id'])) {
344 return $this->data_array['role_id'] ;
349 function getName () { return $this->data_array['name'] ; }
350 function getDescription () { return $this->data_array['description'] ; }
354 class ContribTrackerLegalStructure extends Error {
357 function ContribTrackerLegalStructure ($id=false) {
362 return $this->fetchData ($id) ;
365 function fetchData ($id) {
366 $res = db_query_params ('SELECT * FROM plugin_contribtracker_legal_structure WHERE struct_id=$1',
368 if (!$res || db_numrows($res) < 1) {
369 $this->setError(sprintf('ContribTrackerLegalStructure(): %s',db_error()));
373 $this->data_array = db_fetch_array ($res) ;
377 function create ($name) {
378 if ($this->getId ()) {
379 $this->setError(_('Object already exists')) ;
384 $res = db_query_params ('INSERT INTO plugin_contribtracker_legal_structure (name) VALUES ($1)',
386 if (!$res || db_affected_rows ($res) < 1) {
387 $this->setError (sprintf(_('Could not create object in database: %s'),
393 $id = db_insertid ($res, 'plugin_contribtracker_legal_structure', 'struct_id') ;
395 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
402 return $this->fetchData ($id) ;
405 function update ($name) {
406 if (! $this->getId ()) {
407 $this->setError(_('Object does not exist')) ;
411 $id = $this->getId () ;
414 $res = db_query_params ('UPDATE plugin_contribtracker_legal_structure SET (name) = ($1) WHERE struct_id = $2',
417 if (!$res || db_affected_rows ($res) < 1) {
418 $this->setError (sprintf(_('Could not update object in database: %s'),
425 return $this->fetchData ($id) ;
429 $id = $this->getId () ;
431 $this->setError (_('Cannot delete a non-existing object')) ;
435 $res = db_query_params ('DELETE FROM plugin_contribtracker_legal_structure WHERE struct_id = $1',
438 $this->setError (sprintf(_('Could not delete object in database: %s'),
443 $this->data_array = array () ;
449 if (isset ($this->data_array['struct_id'])) {
450 return $this->data_array['struct_id'] ;
455 function getName () { return $this->data_array['name'] ; }
458 class ContribTrackerActor extends Error {
461 function ContribTrackerActor ($id=false) {
466 return $this->fetchData ($id) ;
469 function fetchData ($id) {
470 $res = db_query_params ('SELECT * FROM plugin_contribtracker_actor WHERE actor_id=$1',
472 if (!$res || db_numrows($res) < 1) {
473 $this->setError(sprintf('ContribTrackerActor(): %s',db_error()));
477 $this->data_array = db_fetch_array ($res) ;
481 function create ($name, $address, $email, $description, $logo, $structure) {
482 if ($this->getId ()) {
483 $this->setError(_('Object already exists')) ;
488 $res = db_query_params ('INSERT INTO plugin_contribtracker_actor (name,address,email,description,logo,struct_id) VALUES ($1,$2,$3,$4,$5,$6)',
493 base64_encode ($logo),
494 $structure->getID())) ;
495 if (!$res || db_affected_rows ($res) < 1) {
496 $this->setError (sprintf(_('Could not create object in database: %s'),
502 $id = db_insertid ($res, 'plugin_contribtracker_actor', 'actor_id') ;
504 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
511 return $this->fetchData ($id) ;
514 function update ($name, $address, $email, $description, $logo, $structure) {
515 if (! $this->getId ()) {
516 $this->setError(_('Object does not exist')) ;
520 $id = $this->getId () ;
523 $res = db_query_params ('UPDATE plugin_contribtracker_actor SET (name,address,email,description,logo,struct_id) = ($1,$2,$3,$4,$5,$6) WHERE actor_id = $7',
528 base64_encode ($logo),
531 if (!$res || db_affected_rows ($res) < 1) {
532 $this->setError (sprintf(_('Could not create object in update: %s'),
539 return $this->fetchData ($id) ;
543 $id = $this->getId () ;
545 $this->setError (_('Cannot delete a non-existing object')) ;
549 $res = db_query_params ('DELETE FROM plugin_contribtracker_actor WHERE actor_id = $1',
552 $this->setError (sprintf(_('Could not delete object in database: %s'),
557 $this->data_array = array () ;
563 if (isset ($this->data_array['actor_id'])) {
564 return $this->data_array['actor_id'] ;
569 function getName () { return $this->data_array['name'] ; }
570 function getAddress () { return $this->data_array['address'] ; }
571 function getEmail () { return $this->data_array['email'] ; }
572 function getDescription () { return $this->data_array['description'] ; }
573 function getLegalStructure () {
574 return new ContribTrackerLegalStructure ($this->data_array['struct_id']) ;
576 function getLogo () { return base64_decode ($this->data_array['logo']) ; }
578 function getParticipations () {
579 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE actor_id = $1',
580 array ($this->getId())) ;
581 $ids = util_result_column_to_array ($res, 0) ;
583 $results = array () ;
584 foreach ($ids as $id) {
585 $results[] = new ContribTrackerParticipation ($id) ;
593 class ContribTrackerContribution extends Error {
596 function ContribTrackerContribution ($id=false) {
601 return $this->fetchData ($id) ;
604 function fetchData ($id) {
605 $res = db_query_params ('SELECT * FROM plugin_contribtracker_contribution WHERE contrib_id=$1',
607 if (!$res || db_numrows($res) < 1) {
608 $this->setError(sprintf('ContribTrackerContribution(): %s',db_error()));
612 $this->data_array = db_fetch_array ($res) ;
616 function create ($name, $date, $description, $group) {
617 if ($this->getId ()) {
618 $this->setError(_('Object already exists')) ;
623 $res = db_query_params ('INSERT INTO plugin_contribtracker_contribution (name,date,description,group_id) VALUES ($1,$2,$3,$4)',
628 if (!$res || db_affected_rows ($res) < 1) {
629 $this->setError (sprintf(_('Could not create object in database: %s'),
635 $id = db_insertid ($res, 'plugin_contribtracker_contribution', 'contrib_id') ;
637 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
644 return $this->fetchData ($id) ;
647 function update ($name, $date, $description, $group) {
648 if (! $this->getId ()) {
649 $this->setError(_('Object does not exist')) ;
653 $id = $this->getId () ;
656 $res = db_query_params ('UPDATE plugin_contribtracker_contribution SET (name,date,description,group_id) = ($1,$2,$3,$4) WHERE contrib_id = $5',
662 if (!$res || db_affected_rows ($res) < 1) {
663 $this->setError (sprintf(_('Could not create object update database: %s'),
670 return $this->fetchData ($id) ;
674 $id = $this->getId () ;
676 $this->setError (_('Cannot delete a non-existing object')) ;
680 $res = db_query_params ('DELETE FROM plugin_contribtracker_contribution WHERE contrib_id = $1',
683 $this->setError (sprintf(_('Could not delete object in database: %s'),
688 $this->data_array = array () ;
694 if (isset ($this->data_array['contrib_id'])) {
695 return $this->data_array['contrib_id'] ;
700 function getName () { return $this->data_array['name'] ; }
701 function getDate () { return $this->data_array['date'] ; }
702 function getDescription () { return $this->data_array['description'] ; }
703 function getGroup () {
704 return group_get_object ($this->data_array['group_id']) ;
707 function getParticipations () {
708 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE contrib_id = $1',
709 array ($this->getId())) ;
710 $ids = util_result_column_to_array ($res, 0) ;
712 $results = array () ;
713 foreach ($ids as $id) {
714 $results[] = new ContribTrackerParticipation ($id) ;
721 class ContribTrackerParticipation extends Error {
724 function ContribTrackerParticipation ($id=false) {
729 return $this->fetchData ($id) ;
732 function fetchData ($id) {
733 $res = db_query_params ('SELECT * FROM plugin_contribtracker_participation WHERE participation_id=$1',
735 if (!$res || db_numrows($res) < 1) {
736 $this->setError(sprintf('ContribTrackerParticipation(): %s',db_error()));
740 $this->data_array = db_fetch_array ($res) ;
744 function create ($contrib, $actor, $role) {
745 if ($this->getId ()) {
746 $this->setError(_('Object already exists')) ;
751 $res = db_query_params ('INSERT INTO plugin_contribtracker_participation (contrib_id,actor_id,role_id) VALUES ($1,$2,$3)',
752 array ($contrib->getID(),
755 if (!$res || db_affected_rows ($res) < 1) {
756 $this->setError (sprintf(_('Could not create object in database: %s'),
762 $id = db_insertid ($res, 'plugin_contribtracker_participation', 'participation_id') ;
764 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
771 return $this->fetchData ($id) ;
774 function update ($contrib, $actor, $role) {
775 if (! $this->getId ()) {
776 $this->setError(_('Object does not exist')) ;
780 $id = $this->getId () ;
783 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET (contrib_id,actor_id,role_id) = ($1,$2,$3) WHERE participation_id = $4',
784 array ($contrib->getID(),
788 if (!$res || db_affected_rows ($res) < 1) {
789 $this->setError (sprintf(_('Could not create object update database: %s'),
796 return $this->fetchData ($id) ;
800 $id = $this->getId () ;
802 $this->setError (_('Cannot delete a non-existing object')) ;
806 $res = db_query_params ('DELETE FROM plugin_contribtracker_participation WHERE participation_id = $1',
809 $this->setError (sprintf(_('Could not delete object in database: %s'),
814 $this->data_array = array () ;
820 if (isset ($this->data_array['participation_id'])) {
821 return $this->data_array['participation_id'] ;
826 function getActor () {
827 return new ContribTrackerActor ($this->data_array['actor_id']) ;
829 function getRole () {
830 return new ContribTrackerRole ($this->data_array['role_id']) ;
832 function getContribution () {
833 return new ContribTrackerContribution ($this->data_array['contrib_id']) ;
839 // c-file-style: "bsd"