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 '<div id="latest-major-contributions">';
104 echo $HTML->boxTop(_('Latest Major Contributions'), 'Latest_Major_Contributions');
107 <table cellspacing="1" cellpadding="5" width="100%" border="0">
109 <th style="text-align:left">'._('Date').'</th>
110 <th style="text-align:left">'._('Contribution').'</th>
111 <th style="text-align:center" colspan="2">'._('Contributor').'</th>
112 <th style="text-align:left">'._('Role').'</th>
115 $contribs = $this->getContributionsByGroup ($group) ;
117 if (count ($contribs) == 0) {
118 echo '<tr><td colspan="5"><strong>'._('No contributions have been recorded for this project yet.').'</strong></td></tr>';
120 $max_displayed_contribs = 3 ;
122 foreach ($contribs as $c) {
123 $parts = $c->getParticipations () ;
124 if (count ($parts) != 0) {
126 foreach ($parts as $p) {
131 echo '<td rowspan="'.count ($parts).'">' ;
132 echo strftime (_('%Y-%m-%d'), $c->getDate ()) ;
134 echo '<td rowspan="'.count ($parts).'">' ;
135 echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id.'&contrib_id='.$c->getId(),htmlspecialchars($c->getName())) ;
140 if ($p->getActor()->getLogo() != '') {
141 print ' <img type="image/png" src="'.util_make_url ('/plugins/'.$this->name.'/actor_logo.php?actor_id='.$p->getActor()->getId ()).'" />' ;
144 printf (_('%s (%s)'),
145 util_make_link ('/plugins/'.$this->name.'/?actor_id='.$p->getActor()->getId (),
146 htmlspecialchars ($p->getActor()->getName())),
147 htmlspecialchars ($p->getActor()->getLegalStructure()->getName())) ;
149 echo htmlspecialchars ($p->getRole()->getName()) ;
155 if ($i > $max_displayed_contribs) {
161 <div style="text-align:center">
162 <?php echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id,_('[View All Contributions]')); ?>
165 echo $HTML->boxBottom();
169 elseif ($hookname == "site_admin_option_hook") {
171 <li><?php echo util_make_link ('/plugins/'.$this->name.'/global_admin.php',
172 _('Edit actors and roles for the contribution tracker plugin')
179 function getActors () {
180 $res = db_query_params ('SELECT actor_id FROM plugin_contribtracker_actor',
182 $ids = util_result_column_to_array ($res, 0) ;
184 $results = array () ;
185 foreach ($ids as $id) {
186 $results[] = new ContribTrackerActor ($id) ;
192 function getLegalStructures () {
193 $res = db_query_params ('SELECT struct_id FROM plugin_contribtracker_legal_structure',
195 $ids = util_result_column_to_array ($res, 0) ;
197 $results = array () ;
198 foreach ($ids as $id) {
199 $results[] = new ContribTrackerLegalStructure ($id) ;
205 function getRoles () {
206 $res = db_query_params ('SELECT role_id FROM plugin_contribtracker_role',
208 $ids = util_result_column_to_array ($res, 0) ;
210 $results = array () ;
211 foreach ($ids as $id) {
212 $results[] = new ContribTrackerRole ($id) ;
218 function getContributions () {
219 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution',
221 $ids = util_result_column_to_array ($res, 0) ;
223 $results = array () ;
224 foreach ($ids as $id) {
225 $results[] = new ContribTrackerContribution ($id) ;
228 $plugin = plugin_get_object ('contribtracker') ;
229 usort ($results, array ($plugin, "ContribComparator")) ;
233 function getContributionsByGroup ($group) {
234 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution WHERE group_id = $1',
235 array ($group->getId())) ;
236 $ids = util_result_column_to_array ($res, 0) ;
238 $results = array () ;
239 foreach ($ids as $id) {
240 $results[] = new ContribTrackerContribution ($id) ;
243 $plugin = plugin_get_object ('contribtracker') ;
244 usort ($results, array ($plugin, "ContribComparator")) ;
248 function ContribComparator ($a, $b) {
249 if ($a->getDate() != $b->getDate()) {
250 return ($a->getDate() < $b->getDate()) ? -1 : 1 ;
251 } elseif ($a->getName() != $b->getName()) {
252 return ($a->getName() < $b->getName()) ? -1 : 1 ;
258 function ParticipationComparator ($a, $b) {
259 if ($a->getContribution()->getDate() != $b->getContribution()->getDate()) {
260 return ($a->getContribution()->getDate() < $b->getContribution()->getDate()) ? -1 : 1 ;
261 } elseif ($a->getContribution() != $b->getContribution()) {
262 return ($a->getContribution() < $b->getContribution()) ? -1 : 1 ;
263 } elseif ($a->getIndex() != $b->getIndex()) {
264 return ($a->getIndex() < $b->getIndex()) ? -1 : 1 ;
271 class ContribTrackerRole extends Error {
274 function ContribTrackerRole ($id=false) {
279 return $this->fetchData ($id) ;
282 function fetchData ($id) {
283 $res = db_query_params ('SELECT * FROM plugin_contribtracker_role WHERE role_id=$1',
285 if (!$res || db_numrows($res) < 1) {
286 $this->setError(sprintf('ContribTrackerRole(): %s',db_error()));
290 $this->data_array = db_fetch_array ($res) ;
294 function create ($name, $description) {
295 if ($this->getId ()) {
296 $this->setError(_('Object already exists')) ;
301 $res = db_query_params ('INSERT INTO plugin_contribtracker_role (name, description) VALUES ($1,$2)',
304 if (!$res || db_affected_rows ($res) < 1) {
305 $this->setError (sprintf(_('Could not create object in database: %s.'),
311 $id = db_insertid ($res, 'plugin_contribtracker_role', 'role_id') ;
313 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
320 return $this->fetchData ($id) ;
323 function update ($name, $description) {
324 if (! $this->getId ()) {
325 $this->setError(_('Object does not exist')) ;
329 $id = $this->getId () ;
332 $res = db_query_params ('UPDATE plugin_contribtracker_role SET (name, description) = ($1,$2) WHERE role_id = $3',
336 if (!$res || db_affected_rows ($res) < 1) {
337 $this->setError (sprintf(_('Could not update object in database: %s.'),
344 return $this->fetchData ($id) ;
348 $id = $this->getId () ;
350 $this->setError (_('Cannot delete a non-existing object.')) ;
354 $res = db_query_params ('DELETE FROM plugin_contribtracker_role WHERE role_id = $1',
357 $this->setError (sprintf(_('Could not delete object in database: %s.'),
362 $this->data_array = array () ;
368 if (isset ($this->data_array['role_id'])) {
369 return $this->data_array['role_id'] ;
374 function getName () { return $this->data_array['name'] ; }
375 function getDescription () { return $this->data_array['description'] ; }
379 class ContribTrackerLegalStructure extends Error {
382 function ContribTrackerLegalStructure ($id=false) {
387 return $this->fetchData ($id) ;
390 function fetchData ($id) {
391 $res = db_query_params ('SELECT * FROM plugin_contribtracker_legal_structure WHERE struct_id=$1',
393 if (!$res || db_numrows($res) < 1) {
394 $this->setError(sprintf('ContribTrackerLegalStructure(): %s',db_error()));
398 $this->data_array = db_fetch_array ($res) ;
402 function create ($name) {
403 if ($this->getId ()) {
404 $this->setError(_('Object already exists')) ;
409 $res = db_query_params ('INSERT INTO plugin_contribtracker_legal_structure (name) VALUES ($1)',
411 if (!$res || db_affected_rows ($res) < 1) {
412 $this->setError (sprintf(_('Could not create object in database: %s.'),
418 $id = db_insertid ($res, 'plugin_contribtracker_legal_structure', 'struct_id') ;
420 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
427 return $this->fetchData ($id) ;
430 function update ($name) {
431 if (! $this->getId ()) {
432 $this->setError(_('Object does not exist')) ;
436 $id = $this->getId () ;
439 $res = db_query_params ('UPDATE plugin_contribtracker_legal_structure SET (name) = ($1) WHERE struct_id = $2',
442 if (!$res || db_affected_rows ($res) < 1) {
443 $this->setError (sprintf(_('Could not update object in database: %s.'),
450 return $this->fetchData ($id) ;
454 $id = $this->getId () ;
456 $this->setError (_('Cannot delete a non-existing object.')) ;
460 $res = db_query_params ('DELETE FROM plugin_contribtracker_legal_structure WHERE struct_id = $1',
463 $this->setError (sprintf(_('Could not delete object in database: %s.'),
468 $this->data_array = array () ;
474 if (isset ($this->data_array['struct_id'])) {
475 return $this->data_array['struct_id'] ;
480 function getName () { return $this->data_array['name'] ; }
483 class ContribTrackerActor extends Error {
486 function ContribTrackerActor ($id=false) {
491 return $this->fetchData ($id) ;
494 function fetchData ($id) {
495 $res = db_query_params ('SELECT * FROM plugin_contribtracker_actor WHERE actor_id=$1',
497 if (!$res || db_numrows($res) < 1) {
498 $this->setError(sprintf('ContribTrackerActor(): %s',db_error()));
502 $this->data_array = db_fetch_array ($res) ;
506 function create ($name, $address, $email, $description, $logo, $structure) {
507 if ($this->getId ()) {
508 $this->setError(_('Object already exists')) ;
513 $res = db_query_params ('INSERT INTO plugin_contribtracker_actor (name,address,email,description,logo,struct_id) VALUES ($1,$2,$3,$4,$5,$6)',
518 base64_encode ($logo),
519 $structure->getID())) ;
520 if (!$res || db_affected_rows ($res) < 1) {
521 $this->setError (sprintf(_('Could not create object in database: %s.'),
527 $id = db_insertid ($res, 'plugin_contribtracker_actor', 'actor_id') ;
529 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
536 return $this->fetchData ($id) ;
539 function update ($name, $address, $email, $description, $logo, $structure) {
540 if (! $this->getId ()) {
541 $this->setError(_('Object does not exist')) ;
545 $id = $this->getId () ;
548 $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',
553 base64_encode ($logo),
556 if (!$res || db_affected_rows ($res) < 1) {
557 $this->setError (sprintf(_('Could not update object in database: %s.'),
564 return $this->fetchData ($id) ;
568 $id = $this->getId () ;
570 $this->setError (_('Cannot delete a non-existing object.')) ;
574 $res = db_query_params ('DELETE FROM plugin_contribtracker_actor WHERE actor_id = $1',
577 $this->setError (sprintf(_('Could not delete object in database: %s.'),
582 $this->data_array = array () ;
588 if (isset ($this->data_array['actor_id'])) {
589 return $this->data_array['actor_id'] ;
594 function getName () { return $this->data_array['name'] ; }
595 function getAddress () { return $this->data_array['address'] ; }
596 function getEmail () { return $this->data_array['email'] ; }
597 function getDescription () { return $this->data_array['description'] ; }
598 function getLegalStructure () {
599 return new ContribTrackerLegalStructure ($this->data_array['struct_id']) ;
601 function getLogo () { return base64_decode ($this->data_array['logo']) ; }
603 function getParticipations () {
604 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE actor_id = $1',
605 array ($this->getId())) ;
606 $ids = util_result_column_to_array ($res, 0) ;
608 $results = array () ;
609 foreach ($ids as $id) {
610 $results[] = new ContribTrackerParticipation ($id) ;
613 $plugin = plugin_get_object ('contribtracker') ;
614 usort ($results, array ($plugin, "ParticipationComparator")) ;
620 class ContribTrackerContribution extends Error {
623 function ContribTrackerContribution ($id=false) {
628 return $this->fetchData ($id) ;
631 function fetchData ($id) {
632 $res = db_query_params ('SELECT * FROM plugin_contribtracker_contribution WHERE contrib_id=$1',
634 if (!$res || db_numrows($res) < 1) {
635 $this->setError(sprintf('ContribTrackerContribution(): %s',db_error()));
639 $this->data_array = db_fetch_array ($res) ;
643 function create ($name, $date, $description, $group) {
644 if ($this->getId ()) {
645 $this->setError(_('Object already exists')) ;
650 $res = db_query_params ('INSERT INTO plugin_contribtracker_contribution (name,date,description,group_id) VALUES ($1,$2,$3,$4)',
655 if (!$res || db_affected_rows ($res) < 1) {
656 $this->setError (sprintf(_('Could not create object in database: %s.'),
662 $id = db_insertid ($res, 'plugin_contribtracker_contribution', 'contrib_id') ;
664 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
671 return $this->fetchData ($id) ;
674 function update ($name, $date, $description, $group) {
675 if (! $this->getId ()) {
676 $this->setError(_('Object does not exist')) ;
680 $id = $this->getId () ;
683 $res = db_query_params ('UPDATE plugin_contribtracker_contribution SET (name,date,description,group_id) = ($1,$2,$3,$4) WHERE contrib_id = $5',
689 if (!$res || db_affected_rows ($res) < 1) {
690 $this->setError (sprintf(_('Could not update object in database: %s.'),
697 return $this->fetchData ($id) ;
701 $id = $this->getId () ;
703 $this->setError (_('Cannot delete a non-existing object.')) ;
707 $res = db_query_params ('DELETE FROM plugin_contribtracker_contribution WHERE contrib_id = $1',
710 $this->setError (sprintf(_('Could not delete object in database: %s.'),
715 $this->data_array = array () ;
721 if (isset ($this->data_array['contrib_id'])) {
722 return $this->data_array['contrib_id'] ;
727 function getName () { return $this->data_array['name'] ; }
728 function getDate () { return $this->data_array['date'] ; }
729 function getDescription () { return $this->data_array['description'] ; }
730 function getGroup () {
731 return group_get_object ($this->data_array['group_id']) ;
734 function getParticipations () {
735 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE contrib_id = $1',
736 array ($this->getId())) ;
737 $ids = util_result_column_to_array ($res, 0) ;
739 $results = array () ;
740 foreach ($ids as $id) {
741 $results[] = new ContribTrackerParticipation ($id) ;
744 $plugin = plugin_get_object ('contribtracker') ;
745 usort ($results, array ($plugin, "ParticipationComparator")) ;
749 function getLastPartIndex () {
750 $res = db_query_params ('SELECT COUNT(*) AS c FROM plugin_contribtracker_participation WHERE contrib_id = $1',
751 array ($this->getId())) ;
752 $curindex = db_result ($res,0,'c') ;
757 class ContribTrackerParticipation extends Error {
760 function ContribTrackerParticipation ($id=false) {
765 return $this->fetchData ($id) ;
768 function fetchData ($id) {
769 $res = db_query_params ('SELECT * FROM plugin_contribtracker_participation WHERE participation_id=$1',
771 if (!$res || db_numrows($res) < 1) {
772 $this->setError(sprintf('ContribTrackerParticipation(): %s',db_error()));
776 $this->data_array = db_fetch_array ($res) ;
780 function create ($contrib, $actor, $role) {
781 if ($this->getId ()) {
782 $this->setError(_('Object already exists')) ;
787 $index = $contrib->getLastPartIndex () + 1;
789 $res = db_query_params ('INSERT INTO plugin_contribtracker_participation (contrib_id,actor_id,role_id,index) VALUES ($1,$2,$3,$4)',
790 array ($contrib->getID(),
794 if (!$res || db_affected_rows ($res) < 1) {
795 $this->setError (sprintf(_('Could not create object in database: %s.'),
801 $id = db_insertid ($res, 'plugin_contribtracker_participation', 'participation_id') ;
803 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
810 return $this->fetchData ($id) ;
813 function update ($contrib, $actor, $role) {
814 if (! $this->getId ()) {
815 $this->setError(_('Object does not exist')) ;
819 $id = $this->getId () ;
820 if ($contrib->getID() != $this->getContribution()->getID()) {
821 $this->setError (_('Cannot currently move a participation across contributions.')) ;
826 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET (contrib_id,actor_id,role_id) = ($1,$2,$3) WHERE participation_id = $4',
827 array ($contrib->getID(),
831 if (!$res || db_affected_rows ($res) < 1) {
832 $this->setError (sprintf(_('Could not update object in database: %s.'),
839 return $this->fetchData ($id) ;
843 $id = $this->getId () ;
845 $this->setError (_('Cannot delete a non-existing object.')) ;
850 $curindex = $this->getIndex() ;
851 $res = db_query_params ('DELETE FROM plugin_contribtracker_participation WHERE participation_id = $1',
854 $this->setError (sprintf(_('Could not delete object in database: %s.'),
858 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = -index WHERE contrib_id = $1 and index > $2',
859 array ($this->getContribution()->getId(),
862 $this->setError (sprintf(_('Could not update indices in database: %s.'),
866 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = -index -1 WHERE contrib_id = $1 and index < 0',
867 array ($this->getContribution()->getId())) ;
869 $this->setError (sprintf(_('Could not update indices in database: %s.'),
875 $this->data_array = array () ;
881 $id = $this->getId () ;
883 $this->setError (_('Cannot update a non-existing object.')) ;
887 $cur = $this->getIndex() ;
891 $contrib_id = $this->getContribution()->getId() ;
894 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = 0 WHERE participation_id = $1',
896 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = index+1 WHERE contrib_id = $1 AND index = $2',
899 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = $1 WHERE contrib_id = $2 AND index = 0',
905 function moveDown () {
906 $id = $this->getId () ;
908 $this->setError (_('Cannot update a non-existing object.')) ;
912 $lastid = $this->getContribution()->getLastPartIndex() ;
914 $cur = $this->getIndex() ;
915 if ($cur == $lastid) {
918 $contrib_id = $this->getContribution()->getId() ;
921 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = 0 WHERE participation_id = $1',
923 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = index-1 WHERE contrib_id = $1 AND index = $2',
926 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = $1 WHERE contrib_id = $2 AND index = 0',
933 if (isset ($this->data_array['participation_id'])) {
934 return $this->data_array['participation_id'] ;
939 function getIndex () {
940 if (isset ($this->data_array['index'])) {
941 return $this->data_array['index'] ;
946 function getActor () {
947 return new ContribTrackerActor ($this->data_array['actor_id']) ;
949 function getRole () {
950 return new ContribTrackerRole ($this->data_array['role_id']) ;
952 function getContribution () {
953 return new ContribTrackerContribution ($this->data_array['contrib_id']) ;
959 // c-file-style: "bsd"