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
37 function CallHook ($hookname, $params) {
38 if ($hookname == "groupmenu") {
39 $group_id=$params['group'];
40 $project = &group_get_object($group_id);
41 if (!$project || !is_object($project)) {
44 if ($project->isError()) {
47 if (!$project->isProject()) {
50 if ( $project->usesPlugin ( $this->name ) ) {
51 $params['TITLES'][] = _('Contribution tracker') ;
52 $params['DIRS'][]='/plugins/'.$this->name.'/?group_id=' . $group_id ;
54 (($params['toptab'] == $this->name) ? $params['selected']=(count($params['TITLES'])-1) : '' );
55 } elseif ($hookname == "groupisactivecheckbox") {
56 //Check if the group is active
57 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
58 $group_id=$params['group'];
59 $group = &group_get_object($group_id);
62 echo ' <input type="CHECKBOX" name="use_contribtrackerplugin" value="1" ';
63 // CHECKED OR UNCHECKED?
64 if ( $group->usesPlugin ( $this->name ) ) {
71 echo _('Use the Contribution Tracker plugin') ;
75 } elseif ($hookname == "groupisactivecheckboxpost") {
76 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
77 $group_id=$params['group'];
78 $group = &group_get_object($group_id);
79 $use_contribtrackerplugin = getStringFromRequest('use_contribtrackerplugin');
80 if ( $use_contribtrackerplugin == 1 ) {
81 $group->setPluginUse ( $this->name );
83 $group->setPluginUse ( $this->name, false );
85 } elseif ($hookname == "project_admin_plugins") {
86 // this displays the link in the project admin options page to its ContribTracker administration
87 $group_id = $params['group_id'];
88 $group = &group_get_object($group_id);
89 if ( $group->usesPlugin ( $this->name ) ) {
90 echo util_make_link ("/plugins/".$this->name."/project_admin.php?group_id=".$group->getID(),
91 _('Contribution Tracker admin')) ;
96 elseif ($hookname == "project_before_frs") {
97 $group_id = $params['group_id'];
98 $group = &group_get_object ($group_id);
100 if ($group->usesPlugin($this->name)) {
102 echo $HTML->boxTop(_('Latest Major Contributions'));
105 <table cellspacing="1" cellpadding="5" width="100%" border="0">
107 <td style="text-align:left">
108 '._('Contribution').'
109 <td style="text-align:center">
110 '._('Contributing organisation').'
114 $contribs = $this->getContributionsByGroup ($group) ;
115 usort ($contribs, array ($this, "ContribComparator")) ;
117 if (count ($contribs) == 0) {
118 echo '<tr><td colspan="5"><strong>'._('This Project Has Not Released Any Files').'</strong></td></tr>';
120 $max_displayed_contribs = 3 ;
122 foreach ($contribs as $c) {
125 echo strftime (_('%Y-%m-%d'), $c->getDate ()) ;
127 echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id.'&contrib_id='.$c->getId(),htmlspecialchars($c->getName())) ;
128 echo '</td><td><ul>' ;
130 $parts = $c->getParticipations () ;
131 foreach ($parts as $p) {
133 printf (_('%s: %s (%s)'),
134 htmlspecialchars ($p->getRole()->getName()),
135 util_make_link ('/plugins/'.$this->name.'/?actor_id='.$p->getActor()->getId (),
136 htmlspecialchars ($p->getActor()->getName())),
137 htmlspecialchars ($p->getActor()->getLegalStructure()->getName())) ;
140 echo '</ul></td></tr>' ;
143 if ($i > $max_displayed_contribs) {
149 <div style="text-align:center">
150 <?php echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id,_('[View All Contributions]')); ?>
153 echo $HTML->boxBottom();
158 function getActors () {
159 $res = db_query_params ('SELECT actor_id FROM plugin_contribtracker_actor',
161 $ids = util_result_column_to_array ($res, 0) ;
163 $results = array () ;
164 foreach ($ids as $id) {
165 $results[] = new ContribTrackerActor ($id) ;
171 function getLegalStructures () {
172 $res = db_query_params ('SELECT struct_id FROM plugin_contribtracker_legal_structure',
174 $ids = util_result_column_to_array ($res, 0) ;
176 $results = array () ;
177 foreach ($ids as $id) {
178 $results[] = new ContribTrackerLegalStructure ($id) ;
184 function getRoles () {
185 $res = db_query_params ('SELECT role_id FROM plugin_contribtracker_role',
187 $ids = util_result_column_to_array ($res, 0) ;
189 $results = array () ;
190 foreach ($ids as $id) {
191 $results[] = new ContribTrackerRole ($id) ;
197 function getContributions () {
198 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution',
200 $ids = util_result_column_to_array ($res, 0) ;
202 $results = array () ;
203 foreach ($ids as $id) {
204 $results[] = new ContribTrackerContribution ($id) ;
210 function getContributionsByGroup ($group) {
211 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution WHERE group_id = $1',
212 array ($group->getId())) ;
213 $ids = util_result_column_to_array ($res, 0) ;
215 $results = array () ;
216 foreach ($ids as $id) {
217 $results[] = new ContribTrackerContribution ($id) ;
223 function ContribComparator ($a, $b) {
224 if ($a->getDate() != $b->getDate()) {
225 return ($a->getDate() < $b->getDate()) ? -1 : 1 ;
226 } elseif ($a->getName() != $b->getName()) {
227 return ($a->getName() < $b->getName()) ? -1 : 1 ;
234 class ContribTrackerRole extends Error {
237 function ContribTrackerRole ($id=false) {
242 return $this->fetchData ($id) ;
245 function fetchData ($id) {
246 $res = db_query_params ('SELECT * FROM plugin_contribtracker_role WHERE role_id=$1',
248 if (!$res || db_numrows($res) < 1) {
249 $this->setError(sprintf('ContribTrackerRole(): %s',db_error()));
253 $this->data_array = db_fetch_array ($res) ;
257 function create ($name, $description) {
258 if ($this->getId ()) {
259 $this->setError(_('Object already exists')) ;
264 $res = db_query_params ('INSERT INTO plugin_contribtracker_role (name, description) VALUES ($1,$2)',
267 if (!$res || db_affected_rows ($res) < 1) {
268 $this->setError (sprintf(_('Could not create object in database: %s'),
274 $id = db_insertid ($res, 'plugin_contribtracker_role', 'role_id') ;
276 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
283 return $this->fetchData ($id) ;
286 function update ($name, $description) {
287 if (! $this->getId ()) {
288 $this->setError(_('Object does not exist')) ;
292 $id = $this->getId () ;
295 $res = db_query_params ('UPDATE plugin_contribtracker_role SET (name, description) = ($1,$2) WHERE role_id = $3',
299 if (!$res || db_affected_rows ($res) < 1) {
300 $this->setError (sprintf(_('Could not update object in database: %s'),
307 return $this->fetchData ($id) ;
311 $id = $this->getId () ;
313 $this->setError (_('Cannot delete a non-existing object')) ;
317 $res = db_query_params ('DELETE FROM plugin_contribtracker_role WHERE role_id = $1',
320 $this->setError (sprintf(_('Could not delete object in database: %s'),
325 $this->data_array = array () ;
331 if (isset ($this->data_array['role_id'])) {
332 return $this->data_array['role_id'] ;
337 function getName () { return $this->data_array['name'] ; }
338 function getDescription () { return $this->data_array['description'] ; }
342 class ContribTrackerLegalStructure extends Error {
345 function ContribTrackerLegalStructure ($id=false) {
350 return $this->fetchData ($id) ;
353 function fetchData ($id) {
354 $res = db_query_params ('SELECT * FROM plugin_contribtracker_legal_structure WHERE struct_id=$1',
356 if (!$res || db_numrows($res) < 1) {
357 $this->setError(sprintf('ContribTrackerLegalStructure(): %s',db_error()));
361 $this->data_array = db_fetch_array ($res) ;
365 function create ($name) {
366 if ($this->getId ()) {
367 $this->setError(_('Object already exists')) ;
372 $res = db_query_params ('INSERT INTO plugin_contribtracker_legal_structure (name) VALUES ($1)',
374 if (!$res || db_affected_rows ($res) < 1) {
375 $this->setError (sprintf(_('Could not create object in database: %s'),
381 $id = db_insertid ($res, 'plugin_contribtracker_legal_structure', 'struct_id') ;
383 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
390 return $this->fetchData ($id) ;
393 function update ($name) {
394 if (! $this->getId ()) {
395 $this->setError(_('Object does not exist')) ;
399 $id = $this->getId () ;
402 $res = db_query_params ('UPDATE plugin_contribtracker_legal_structure SET (name) = ($1) WHERE struct_id = $2',
405 if (!$res || db_affected_rows ($res) < 1) {
406 $this->setError (sprintf(_('Could not update object in database: %s'),
413 return $this->fetchData ($id) ;
417 $id = $this->getId () ;
419 $this->setError (_('Cannot delete a non-existing object')) ;
423 $res = db_query_params ('DELETE FROM plugin_contribtracker_legal_structure WHERE struct_id = $1',
426 $this->setError (sprintf(_('Could not delete object in database: %s'),
431 $this->data_array = array () ;
437 if (isset ($this->data_array['struct_id'])) {
438 return $this->data_array['struct_id'] ;
443 function getName () { return $this->data_array['name'] ; }
446 class ContribTrackerActor extends Error {
449 function ContribTrackerActor ($id=false) {
454 return $this->fetchData ($id) ;
457 function fetchData ($id) {
458 $res = db_query_params ('SELECT * FROM plugin_contribtracker_actor WHERE actor_id=$1',
460 if (!$res || db_numrows($res) < 1) {
461 $this->setError(sprintf('ContribTrackerActor(): %s',db_error()));
465 $this->data_array = db_fetch_array ($res) ;
469 function create ($name, $address, $email, $description, $structure) {
470 if ($this->getId ()) {
471 $this->setError(_('Object already exists')) ;
476 $res = db_query_params ('INSERT INTO plugin_contribtracker_actor (name,address,email,description,struct_id) VALUES ($1,$2,$3,$4,$5)',
481 $structure->getID())) ;
482 if (!$res || db_affected_rows ($res) < 1) {
483 $this->setError (sprintf(_('Could not create object in database: %s'),
489 $id = db_insertid ($res, 'plugin_contribtracker_actor', 'actor_id') ;
491 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
498 return $this->fetchData ($id) ;
501 function update ($name, $address, $email, $description, $structure) {
502 if (! $this->getId ()) {
503 $this->setError(_('Object does not exist')) ;
507 $id = $this->getId () ;
510 $res = db_query_params ('UPDATE plugin_contribtracker_actor SET (name,address,email,description,struct_id) = ($1,$2,$3,$4,$5) WHERE actor_id = $6',
517 if (!$res || db_affected_rows ($res) < 1) {
518 $this->setError (sprintf(_('Could not create object in update: %s'),
525 return $this->fetchData ($id) ;
529 $id = $this->getId () ;
531 $this->setError (_('Cannot delete a non-existing object')) ;
535 $res = db_query_params ('DELETE FROM plugin_contribtracker_actor WHERE actor_id = $1',
538 $this->setError (sprintf(_('Could not delete object in database: %s'),
543 $this->data_array = array () ;
549 if (isset ($this->data_array['actor_id'])) {
550 return $this->data_array['actor_id'] ;
555 function getName () { return $this->data_array['name'] ; }
556 function getAddress () { return $this->data_array['address'] ; }
557 function getEmail () { return $this->data_array['email'] ; }
558 function getDescription () { return $this->data_array['description'] ; }
559 function getLegalStructure () {
560 return new ContribTrackerLegalStructure ($this->data_array['struct_id']) ;
563 function getParticipations () {
564 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE actor_id = $1',
565 array ($this->getId())) ;
566 $ids = util_result_column_to_array ($res, 0) ;
568 $results = array () ;
569 foreach ($ids as $id) {
570 $results[] = new ContribTrackerParticipation ($id) ;
578 class ContribTrackerContribution extends Error {
581 function ContribTrackerContribution ($id=false) {
586 return $this->fetchData ($id) ;
589 function fetchData ($id) {
590 $res = db_query_params ('SELECT * FROM plugin_contribtracker_contribution WHERE contrib_id=$1',
592 if (!$res || db_numrows($res) < 1) {
593 $this->setError(sprintf('ContribTrackerContribution(): %s',db_error()));
597 $this->data_array = db_fetch_array ($res) ;
601 function create ($name, $date, $description, $group) {
602 if ($this->getId ()) {
603 $this->setError(_('Object already exists')) ;
608 $res = db_query_params ('INSERT INTO plugin_contribtracker_contribution (name,date,description,group_id) VALUES ($1,$2,$3,$4)',
613 if (!$res || db_affected_rows ($res) < 1) {
614 $this->setError (sprintf(_('Could not create object in database: %s'),
620 $id = db_insertid ($res, 'plugin_contribtracker_contribution', 'contrib_id') ;
622 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
629 return $this->fetchData ($id) ;
632 function update ($name, $date, $description, $group) {
633 if (! $this->getId ()) {
634 $this->setError(_('Object does not exist')) ;
638 $id = $this->getId () ;
641 $res = db_query_params ('UPDATE plugin_contribtracker_contribution SET (name,date,description,group_id) = ($1,$2,$3,$4) WHERE contrib_id = $5',
647 if (!$res || db_affected_rows ($res) < 1) {
648 $this->setError (sprintf(_('Could not create object update database: %s'),
655 return $this->fetchData ($id) ;
659 $id = $this->getId () ;
661 $this->setError (_('Cannot delete a non-existing object')) ;
665 $res = db_query_params ('DELETE FROM plugin_contribtracker_contribution WHERE contrib_id = $1',
668 $this->setError (sprintf(_('Could not delete object in database: %s'),
673 $this->data_array = array () ;
679 if (isset ($this->data_array['contrib_id'])) {
680 return $this->data_array['contrib_id'] ;
685 function getName () { return $this->data_array['name'] ; }
686 function getDate () { return $this->data_array['date'] ; }
687 function getDescription () { return $this->data_array['description'] ; }
688 function getGroup () {
689 return group_get_object ($this->data_array['group_id']) ;
692 function getParticipations () {
693 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE contrib_id = $1',
694 array ($this->getId())) ;
695 $ids = util_result_column_to_array ($res, 0) ;
697 $results = array () ;
698 foreach ($ids as $id) {
699 $results[] = new ContribTrackerParticipation ($id) ;
706 class ContribTrackerParticipation extends Error {
709 function ContribTrackerParticipation ($id=false) {
714 return $this->fetchData ($id) ;
717 function fetchData ($id) {
718 $res = db_query_params ('SELECT * FROM plugin_contribtracker_participation WHERE participation_id=$1',
720 if (!$res || db_numrows($res) < 1) {
721 $this->setError(sprintf('ContribTrackerParticipation(): %s',db_error()));
725 $this->data_array = db_fetch_array ($res) ;
729 function create ($contrib, $actor, $role) {
730 if ($this->getId ()) {
731 $this->setError(_('Object already exists')) ;
736 $res = db_query_params ('INSERT INTO plugin_contribtracker_participation (contrib_id,actor_id,role_id) VALUES ($1,$2,$3)',
737 array ($contrib->getID(),
740 if (!$res || db_affected_rows ($res) < 1) {
741 $this->setError (sprintf(_('Could not create object in database: %s'),
747 $id = db_insertid ($res, 'plugin_contribtracker_participation', 'participation_id') ;
749 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
756 return $this->fetchData ($id) ;
759 function update ($contrib, $actor, $role) {
760 if (! $this->getId ()) {
761 $this->setError(_('Object does not exist')) ;
765 $id = $this->getId () ;
768 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET (contrib_id,actor_id,role_id) = ($1,$2,$3) WHERE participation_id = $4',
769 array ($contrib->getID(),
773 if (!$res || db_affected_rows ($res) < 1) {
774 $this->setError (sprintf(_('Could not create object update database: %s'),
781 return $this->fetchData ($id) ;
785 $id = $this->getId () ;
787 $this->setError (_('Cannot delete a non-existing object')) ;
791 $res = db_query_params ('DELETE FROM plugin_contribtracker_participation WHERE participation_id = $1',
794 $this->setError (sprintf(_('Could not delete object in database: %s'),
799 $this->data_array = array () ;
805 if (isset ($this->data_array['participation_id'])) {
806 return $this->data_array['participation_id'] ;
811 function getActor () {
812 return new ContribTrackerActor ($this->data_array['actor_id']) ;
814 function getRole () {
815 return new ContribTrackerRole ($this->data_array['role_id']) ;
817 function getContribution () {
818 return new ContribTrackerContribution ($this->data_array['contrib_id']) ;
824 // c-file-style: "bsd"