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 <th style="text-align:left">'._('Date').'</th>
109 <th style="text-align:left">'._('Contribution').'</th>
110 <th style="text-align:center" colspan="2">'._('Contributor').'</th>
111 <th style="text-align:left">'._('Role').'</th>
114 $contribs = $this->getContributionsByGroup ($group) ;
116 if (count ($contribs) == 0) {
117 echo '<tr><td colspan="5"><strong>'._('No contributions have been recorded for this project yet.').'</strong></td></tr>';
119 $max_displayed_contribs = 3 ;
121 foreach ($contribs as $c) {
122 $parts = $c->getParticipations () ;
123 if (count ($parts) != 0) {
125 foreach ($parts as $p) {
130 echo '<td rowspan="'.count ($parts).'">' ;
131 echo strftime (_('%Y-%m-%d'), $c->getDate ()) ;
133 echo '<td rowspan="'.count ($parts).'">' ;
134 echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id.'&contrib_id='.$c->getId(),htmlspecialchars($c->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 ()).'" />' ;
143 printf (_('%s (%s)'),
144 util_make_link ('/plugins/'.$this->name.'/?actor_id='.$p->getActor()->getId (),
145 htmlspecialchars ($p->getActor()->getName())),
146 htmlspecialchars ($p->getActor()->getLegalStructure()->getName())) ;
148 echo htmlspecialchars ($p->getRole()->getName()) ;
154 if ($i > $max_displayed_contribs) {
160 <div style="text-align:center">
161 <?php echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id,_('[View All Contributions]')); ?>
164 echo $HTML->boxBottom();
167 elseif ($hookname == "site_admin_option_hook") {
169 <li><?php echo util_make_link ('/plugins/'.$this->name.'/global_admin.php',
170 _('Edit actors and roles for the contribution tracker plugin')
177 function getActors () {
178 $res = db_query_params ('SELECT actor_id FROM plugin_contribtracker_actor',
180 $ids = util_result_column_to_array ($res, 0) ;
182 $results = array () ;
183 foreach ($ids as $id) {
184 $results[] = new ContribTrackerActor ($id) ;
190 function getLegalStructures () {
191 $res = db_query_params ('SELECT struct_id FROM plugin_contribtracker_legal_structure',
193 $ids = util_result_column_to_array ($res, 0) ;
195 $results = array () ;
196 foreach ($ids as $id) {
197 $results[] = new ContribTrackerLegalStructure ($id) ;
203 function getRoles () {
204 $res = db_query_params ('SELECT role_id FROM plugin_contribtracker_role',
206 $ids = util_result_column_to_array ($res, 0) ;
208 $results = array () ;
209 foreach ($ids as $id) {
210 $results[] = new ContribTrackerRole ($id) ;
216 function getContributions () {
217 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution',
219 $ids = util_result_column_to_array ($res, 0) ;
221 $results = array () ;
222 foreach ($ids as $id) {
223 $results[] = new ContribTrackerContribution ($id) ;
226 $plugin = plugin_get_object ('contribtracker') ;
227 usort ($results, array ($plugin, "ContribComparator")) ;
231 function getContributionsByGroup ($group) {
232 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution WHERE group_id = $1',
233 array ($group->getId())) ;
234 $ids = util_result_column_to_array ($res, 0) ;
236 $results = array () ;
237 foreach ($ids as $id) {
238 $results[] = new ContribTrackerContribution ($id) ;
241 $plugin = plugin_get_object ('contribtracker') ;
242 usort ($results, array ($plugin, "ContribComparator")) ;
246 function ContribComparator ($a, $b) {
247 if ($a->getDate() != $b->getDate()) {
248 return ($a->getDate() < $b->getDate()) ? -1 : 1 ;
249 } elseif ($a->getName() != $b->getName()) {
250 return ($a->getName() < $b->getName()) ? -1 : 1 ;
256 function ParticipationComparator ($a, $b) {
257 if ($a->getContribution()->getDate() != $b->getContribution()->getDate()) {
258 return ($a->getContribution()->getDate() < $b->getContribution()->getDate()) ? -1 : 1 ;
259 } elseif ($a->getContribution() != $b->getContribution()) {
260 return ($a->getContribution() < $b->getContribution()) ? -1 : 1 ;
261 } elseif ($a->getIndex() != $b->getIndex()) {
262 return ($a->getIndex() < $b->getIndex()) ? -1 : 1 ;
269 class ContribTrackerRole extends Error {
272 function ContribTrackerRole ($id=false) {
277 return $this->fetchData ($id) ;
280 function fetchData ($id) {
281 $res = db_query_params ('SELECT * FROM plugin_contribtracker_role WHERE role_id=$1',
283 if (!$res || db_numrows($res) < 1) {
284 $this->setError(sprintf('ContribTrackerRole(): %s',db_error()));
288 $this->data_array = db_fetch_array ($res) ;
292 function create ($name, $description) {
293 if ($this->getId ()) {
294 $this->setError(_('Object already exists')) ;
299 $res = db_query_params ('INSERT INTO plugin_contribtracker_role (name, description) VALUES ($1,$2)',
302 if (!$res || db_affected_rows ($res) < 1) {
303 $this->setError (sprintf(_('Could not create object in database: %s.'),
309 $id = db_insertid ($res, 'plugin_contribtracker_role', 'role_id') ;
311 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
318 return $this->fetchData ($id) ;
321 function update ($name, $description) {
322 if (! $this->getId ()) {
323 $this->setError(_('Object does not exist')) ;
327 $id = $this->getId () ;
330 $res = db_query_params ('UPDATE plugin_contribtracker_role SET (name, description) = ($1,$2) WHERE role_id = $3',
334 if (!$res || db_affected_rows ($res) < 1) {
335 $this->setError (sprintf(_('Could not update object in database: %s.'),
342 return $this->fetchData ($id) ;
346 $id = $this->getId () ;
348 $this->setError (_('Cannot delete a non-existing object.')) ;
352 $res = db_query_params ('DELETE FROM plugin_contribtracker_role WHERE role_id = $1',
355 $this->setError (sprintf(_('Could not delete object in database: %s.'),
360 $this->data_array = array () ;
366 if (isset ($this->data_array['role_id'])) {
367 return $this->data_array['role_id'] ;
372 function getName () { return $this->data_array['name'] ; }
373 function getDescription () { return $this->data_array['description'] ; }
377 class ContribTrackerLegalStructure extends Error {
380 function ContribTrackerLegalStructure ($id=false) {
385 return $this->fetchData ($id) ;
388 function fetchData ($id) {
389 $res = db_query_params ('SELECT * FROM plugin_contribtracker_legal_structure WHERE struct_id=$1',
391 if (!$res || db_numrows($res) < 1) {
392 $this->setError(sprintf('ContribTrackerLegalStructure(): %s',db_error()));
396 $this->data_array = db_fetch_array ($res) ;
400 function create ($name) {
401 if ($this->getId ()) {
402 $this->setError(_('Object already exists')) ;
407 $res = db_query_params ('INSERT INTO plugin_contribtracker_legal_structure (name) VALUES ($1)',
409 if (!$res || db_affected_rows ($res) < 1) {
410 $this->setError (sprintf(_('Could not create object in database: %s.'),
416 $id = db_insertid ($res, 'plugin_contribtracker_legal_structure', 'struct_id') ;
418 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
425 return $this->fetchData ($id) ;
428 function update ($name) {
429 if (! $this->getId ()) {
430 $this->setError(_('Object does not exist')) ;
434 $id = $this->getId () ;
437 $res = db_query_params ('UPDATE plugin_contribtracker_legal_structure SET (name) = ($1) WHERE struct_id = $2',
440 if (!$res || db_affected_rows ($res) < 1) {
441 $this->setError (sprintf(_('Could not update object in database: %s.'),
448 return $this->fetchData ($id) ;
452 $id = $this->getId () ;
454 $this->setError (_('Cannot delete a non-existing object.')) ;
458 $res = db_query_params ('DELETE FROM plugin_contribtracker_legal_structure WHERE struct_id = $1',
461 $this->setError (sprintf(_('Could not delete object in database: %s.'),
466 $this->data_array = array () ;
472 if (isset ($this->data_array['struct_id'])) {
473 return $this->data_array['struct_id'] ;
478 function getName () { return $this->data_array['name'] ; }
481 class ContribTrackerActor extends Error {
484 function ContribTrackerActor ($id=false) {
489 return $this->fetchData ($id) ;
492 function fetchData ($id) {
493 $res = db_query_params ('SELECT * FROM plugin_contribtracker_actor WHERE actor_id=$1',
495 if (!$res || db_numrows($res) < 1) {
496 $this->setError(sprintf('ContribTrackerActor(): %s',db_error()));
500 $this->data_array = db_fetch_array ($res) ;
504 function create ($name, $address, $email, $description, $logo, $structure) {
505 if ($this->getId ()) {
506 $this->setError(_('Object already exists')) ;
511 $res = db_query_params ('INSERT INTO plugin_contribtracker_actor (name,address,email,description,logo,struct_id) VALUES ($1,$2,$3,$4,$5,$6)',
516 base64_encode ($logo),
517 $structure->getID())) ;
518 if (!$res || db_affected_rows ($res) < 1) {
519 $this->setError (sprintf(_('Could not create object in database: %s.'),
525 $id = db_insertid ($res, 'plugin_contribtracker_actor', 'actor_id') ;
527 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
534 return $this->fetchData ($id) ;
537 function update ($name, $address, $email, $description, $logo, $structure) {
538 if (! $this->getId ()) {
539 $this->setError(_('Object does not exist')) ;
543 $id = $this->getId () ;
546 $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',
551 base64_encode ($logo),
554 if (!$res || db_affected_rows ($res) < 1) {
555 $this->setError (sprintf(_('Could not update object in database: %s.'),
562 return $this->fetchData ($id) ;
566 $id = $this->getId () ;
568 $this->setError (_('Cannot delete a non-existing object.')) ;
572 $res = db_query_params ('DELETE FROM plugin_contribtracker_actor WHERE actor_id = $1',
575 $this->setError (sprintf(_('Could not delete object in database: %s.'),
580 $this->data_array = array () ;
586 if (isset ($this->data_array['actor_id'])) {
587 return $this->data_array['actor_id'] ;
592 function getName () { return $this->data_array['name'] ; }
593 function getAddress () { return $this->data_array['address'] ; }
594 function getEmail () { return $this->data_array['email'] ; }
595 function getDescription () { return $this->data_array['description'] ; }
596 function getLegalStructure () {
597 return new ContribTrackerLegalStructure ($this->data_array['struct_id']) ;
599 function getLogo () { return base64_decode ($this->data_array['logo']) ; }
601 function getParticipations () {
602 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE actor_id = $1',
603 array ($this->getId())) ;
604 $ids = util_result_column_to_array ($res, 0) ;
606 $results = array () ;
607 foreach ($ids as $id) {
608 $results[] = new ContribTrackerParticipation ($id) ;
611 $plugin = plugin_get_object ('contribtracker') ;
612 usort ($results, array ($plugin, "ParticipationComparator")) ;
618 class ContribTrackerContribution extends Error {
621 function ContribTrackerContribution ($id=false) {
626 return $this->fetchData ($id) ;
629 function fetchData ($id) {
630 $res = db_query_params ('SELECT * FROM plugin_contribtracker_contribution WHERE contrib_id=$1',
632 if (!$res || db_numrows($res) < 1) {
633 $this->setError(sprintf('ContribTrackerContribution(): %s',db_error()));
637 $this->data_array = db_fetch_array ($res) ;
641 function create ($name, $date, $description, $group) {
642 if ($this->getId ()) {
643 $this->setError(_('Object already exists')) ;
648 $res = db_query_params ('INSERT INTO plugin_contribtracker_contribution (name,date,description,group_id) VALUES ($1,$2,$3,$4)',
653 if (!$res || db_affected_rows ($res) < 1) {
654 $this->setError (sprintf(_('Could not create object in database: %s.'),
660 $id = db_insertid ($res, 'plugin_contribtracker_contribution', 'contrib_id') ;
662 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
669 return $this->fetchData ($id) ;
672 function update ($name, $date, $description, $group) {
673 if (! $this->getId ()) {
674 $this->setError(_('Object does not exist')) ;
678 $id = $this->getId () ;
681 $res = db_query_params ('UPDATE plugin_contribtracker_contribution SET (name,date,description,group_id) = ($1,$2,$3,$4) WHERE contrib_id = $5',
687 if (!$res || db_affected_rows ($res) < 1) {
688 $this->setError (sprintf(_('Could not update object in database: %s.'),
695 return $this->fetchData ($id) ;
699 $id = $this->getId () ;
701 $this->setError (_('Cannot delete a non-existing object.')) ;
705 $res = db_query_params ('DELETE FROM plugin_contribtracker_contribution WHERE contrib_id = $1',
708 $this->setError (sprintf(_('Could not delete object in database: %s.'),
713 $this->data_array = array () ;
719 if (isset ($this->data_array['contrib_id'])) {
720 return $this->data_array['contrib_id'] ;
725 function getName () { return $this->data_array['name'] ; }
726 function getDate () { return $this->data_array['date'] ; }
727 function getDescription () { return $this->data_array['description'] ; }
728 function getGroup () {
729 return group_get_object ($this->data_array['group_id']) ;
732 function getParticipations () {
733 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE contrib_id = $1',
734 array ($this->getId())) ;
735 $ids = util_result_column_to_array ($res, 0) ;
737 $results = array () ;
738 foreach ($ids as $id) {
739 $results[] = new ContribTrackerParticipation ($id) ;
742 $plugin = plugin_get_object ('contribtracker') ;
743 usort ($results, array ($plugin, "ParticipationComparator")) ;
747 function getLastPartIndex () {
748 $res = db_query_params ('SELECT COUNT(*) AS c FROM plugin_contribtracker_participation WHERE contrib_id = $1',
749 array ($this->getId())) ;
750 $curindex = db_result ($res,0,'c') ;
755 class ContribTrackerParticipation extends Error {
758 function ContribTrackerParticipation ($id=false) {
763 return $this->fetchData ($id) ;
766 function fetchData ($id) {
767 $res = db_query_params ('SELECT * FROM plugin_contribtracker_participation WHERE participation_id=$1',
769 if (!$res || db_numrows($res) < 1) {
770 $this->setError(sprintf('ContribTrackerParticipation(): %s',db_error()));
774 $this->data_array = db_fetch_array ($res) ;
778 function create ($contrib, $actor, $role) {
779 if ($this->getId ()) {
780 $this->setError(_('Object already exists')) ;
785 $index = $contrib->getLastPartIndex () + 1;
787 $res = db_query_params ('INSERT INTO plugin_contribtracker_participation (contrib_id,actor_id,role_id,index) VALUES ($1,$2,$3,$4)',
788 array ($contrib->getID(),
792 if (!$res || db_affected_rows ($res) < 1) {
793 $this->setError (sprintf(_('Could not create object in database: %s.'),
799 $id = db_insertid ($res, 'plugin_contribtracker_participation', 'participation_id') ;
801 $this->setError (sprintf(_('Could not get ID from object in database: %s.'),
808 return $this->fetchData ($id) ;
811 function update ($contrib, $actor, $role) {
812 if (! $this->getId ()) {
813 $this->setError(_('Object does not exist')) ;
817 $id = $this->getId () ;
818 if ($contrib->getID() != $this->getContribution()->getID()) {
819 $this->setError (_('Cannot currently move a participation across contributions.')) ;
824 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET (contrib_id,actor_id,role_id) = ($1,$2,$3) WHERE participation_id = $4',
825 array ($contrib->getID(),
829 if (!$res || db_affected_rows ($res) < 1) {
830 $this->setError (sprintf(_('Could not update object in database: %s.'),
837 return $this->fetchData ($id) ;
841 $id = $this->getId () ;
843 $this->setError (_('Cannot delete a non-existing object.')) ;
848 $curindex = $this->getIndex() ;
849 $res = db_query_params ('DELETE FROM plugin_contribtracker_participation WHERE participation_id = $1',
852 $this->setError (sprintf(_('Could not delete object in database: %s.'),
856 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = -index WHERE contrib_id = $1 and index > $2',
857 array ($this->getContribution()->getId(),
860 $this->setError (sprintf(_('Could not update indices in database: %s.'),
864 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = -index -1 WHERE contrib_id = $1 and index < 0',
865 array ($this->getContribution()->getId())) ;
867 $this->setError (sprintf(_('Could not update indices in database: %s.'),
873 $this->data_array = array () ;
879 $id = $this->getId () ;
881 $this->setError (_('Cannot update a non-existing object.')) ;
885 $cur = $this->getIndex() ;
889 $contrib_id = $this->getContribution()->getId() ;
892 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = 0 WHERE participation_id = $1',
894 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = index+1 WHERE contrib_id = $1 AND index = $2',
897 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = $1 WHERE contrib_id = $2 AND index = 0',
903 function moveDown () {
904 $id = $this->getId () ;
906 $this->setError (_('Cannot update a non-existing object.')) ;
910 $lastid = $this->getContribution()->getLastPartIndex() ;
912 $cur = $this->getIndex() ;
913 if ($cur == $lastid) {
916 $contrib_id = $this->getContribution()->getId() ;
919 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = 0 WHERE participation_id = $1',
921 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = index-1 WHERE contrib_id = $1 AND index = $2',
924 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET index = $1 WHERE contrib_id = $2 AND index = 0',
931 if (isset ($this->data_array['participation_id'])) {
932 return $this->data_array['participation_id'] ;
937 function getIndex () {
938 if (isset ($this->data_array['index'])) {
939 return $this->data_array['index'] ;
944 function getActor () {
945 return new ContribTrackerActor ($this->data_array['actor_id']) ;
947 function getRole () {
948 return new ContribTrackerRole ($this->data_array['role_id']) ;
950 function getContribution () {
951 return new ContribTrackerContribution ($this->data_array['contrib_id']) ;
957 // c-file-style: "bsd"