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>'._('This Project Has Not Released Any Files').'</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())) ;
141 echo '</ul></td></tr>' ;
144 if ($i > $max_displayed_contribs) {
150 <div style="text-align:center">
151 <?php echo util_make_link ('/plugins/'.$this->name.'/?group_id='.$group_id,_('[View All Contributions]')); ?>
154 echo $HTML->boxBottom();
157 elseif ($hookname == "site_admin_option_hook") {
159 <li><?php echo util_make_link ('/plugins/'.$this->name.'/global_admin.php',
160 _('Edit actors and roles for the contribution tracker plugin')
167 function getActors () {
168 $res = db_query_params ('SELECT actor_id FROM plugin_contribtracker_actor',
170 $ids = util_result_column_to_array ($res, 0) ;
172 $results = array () ;
173 foreach ($ids as $id) {
174 $results[] = new ContribTrackerActor ($id) ;
180 function getLegalStructures () {
181 $res = db_query_params ('SELECT struct_id FROM plugin_contribtracker_legal_structure',
183 $ids = util_result_column_to_array ($res, 0) ;
185 $results = array () ;
186 foreach ($ids as $id) {
187 $results[] = new ContribTrackerLegalStructure ($id) ;
193 function getRoles () {
194 $res = db_query_params ('SELECT role_id FROM plugin_contribtracker_role',
196 $ids = util_result_column_to_array ($res, 0) ;
198 $results = array () ;
199 foreach ($ids as $id) {
200 $results[] = new ContribTrackerRole ($id) ;
206 function getContributions () {
207 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution',
209 $ids = util_result_column_to_array ($res, 0) ;
211 $results = array () ;
212 foreach ($ids as $id) {
213 $results[] = new ContribTrackerContribution ($id) ;
219 function getContributionsByGroup ($group) {
220 $res = db_query_params ('SELECT contrib_id FROM plugin_contribtracker_contribution WHERE group_id = $1',
221 array ($group->getId())) ;
222 $ids = util_result_column_to_array ($res, 0) ;
224 $results = array () ;
225 foreach ($ids as $id) {
226 $results[] = new ContribTrackerContribution ($id) ;
232 function ContribComparator ($a, $b) {
233 if ($a->getDate() != $b->getDate()) {
234 return ($a->getDate() < $b->getDate()) ? -1 : 1 ;
235 } elseif ($a->getName() != $b->getName()) {
236 return ($a->getName() < $b->getName()) ? -1 : 1 ;
243 class ContribTrackerRole extends Error {
246 function ContribTrackerRole ($id=false) {
251 return $this->fetchData ($id) ;
254 function fetchData ($id) {
255 $res = db_query_params ('SELECT * FROM plugin_contribtracker_role WHERE role_id=$1',
257 if (!$res || db_numrows($res) < 1) {
258 $this->setError(sprintf('ContribTrackerRole(): %s',db_error()));
262 $this->data_array = db_fetch_array ($res) ;
266 function create ($name, $description) {
267 if ($this->getId ()) {
268 $this->setError(_('Object already exists')) ;
273 $res = db_query_params ('INSERT INTO plugin_contribtracker_role (name, description) VALUES ($1,$2)',
276 if (!$res || db_affected_rows ($res) < 1) {
277 $this->setError (sprintf(_('Could not create object in database: %s'),
283 $id = db_insertid ($res, 'plugin_contribtracker_role', 'role_id') ;
285 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
292 return $this->fetchData ($id) ;
295 function update ($name, $description) {
296 if (! $this->getId ()) {
297 $this->setError(_('Object does not exist')) ;
301 $id = $this->getId () ;
304 $res = db_query_params ('UPDATE plugin_contribtracker_role SET (name, description) = ($1,$2) WHERE role_id = $3',
308 if (!$res || db_affected_rows ($res) < 1) {
309 $this->setError (sprintf(_('Could not update object in database: %s'),
316 return $this->fetchData ($id) ;
320 $id = $this->getId () ;
322 $this->setError (_('Cannot delete a non-existing object')) ;
326 $res = db_query_params ('DELETE FROM plugin_contribtracker_role WHERE role_id = $1',
329 $this->setError (sprintf(_('Could not delete object in database: %s'),
334 $this->data_array = array () ;
340 if (isset ($this->data_array['role_id'])) {
341 return $this->data_array['role_id'] ;
346 function getName () { return $this->data_array['name'] ; }
347 function getDescription () { return $this->data_array['description'] ; }
351 class ContribTrackerLegalStructure extends Error {
354 function ContribTrackerLegalStructure ($id=false) {
359 return $this->fetchData ($id) ;
362 function fetchData ($id) {
363 $res = db_query_params ('SELECT * FROM plugin_contribtracker_legal_structure WHERE struct_id=$1',
365 if (!$res || db_numrows($res) < 1) {
366 $this->setError(sprintf('ContribTrackerLegalStructure(): %s',db_error()));
370 $this->data_array = db_fetch_array ($res) ;
374 function create ($name) {
375 if ($this->getId ()) {
376 $this->setError(_('Object already exists')) ;
381 $res = db_query_params ('INSERT INTO plugin_contribtracker_legal_structure (name) VALUES ($1)',
383 if (!$res || db_affected_rows ($res) < 1) {
384 $this->setError (sprintf(_('Could not create object in database: %s'),
390 $id = db_insertid ($res, 'plugin_contribtracker_legal_structure', 'struct_id') ;
392 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
399 return $this->fetchData ($id) ;
402 function update ($name) {
403 if (! $this->getId ()) {
404 $this->setError(_('Object does not exist')) ;
408 $id = $this->getId () ;
411 $res = db_query_params ('UPDATE plugin_contribtracker_legal_structure SET (name) = ($1) WHERE struct_id = $2',
414 if (!$res || db_affected_rows ($res) < 1) {
415 $this->setError (sprintf(_('Could not update object in database: %s'),
422 return $this->fetchData ($id) ;
426 $id = $this->getId () ;
428 $this->setError (_('Cannot delete a non-existing object')) ;
432 $res = db_query_params ('DELETE FROM plugin_contribtracker_legal_structure WHERE struct_id = $1',
435 $this->setError (sprintf(_('Could not delete object in database: %s'),
440 $this->data_array = array () ;
446 if (isset ($this->data_array['struct_id'])) {
447 return $this->data_array['struct_id'] ;
452 function getName () { return $this->data_array['name'] ; }
455 class ContribTrackerActor extends Error {
458 function ContribTrackerActor ($id=false) {
463 return $this->fetchData ($id) ;
466 function fetchData ($id) {
467 $res = db_query_params ('SELECT * FROM plugin_contribtracker_actor WHERE actor_id=$1',
469 if (!$res || db_numrows($res) < 1) {
470 $this->setError(sprintf('ContribTrackerActor(): %s',db_error()));
474 $this->data_array = db_fetch_array ($res) ;
478 function create ($name, $address, $email, $description, $structure) {
479 if ($this->getId ()) {
480 $this->setError(_('Object already exists')) ;
485 $res = db_query_params ('INSERT INTO plugin_contribtracker_actor (name,address,email,description,struct_id) VALUES ($1,$2,$3,$4,$5)',
490 $structure->getID())) ;
491 if (!$res || db_affected_rows ($res) < 1) {
492 $this->setError (sprintf(_('Could not create object in database: %s'),
498 $id = db_insertid ($res, 'plugin_contribtracker_actor', 'actor_id') ;
500 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
507 return $this->fetchData ($id) ;
510 function update ($name, $address, $email, $description, $structure) {
511 if (! $this->getId ()) {
512 $this->setError(_('Object does not exist')) ;
516 $id = $this->getId () ;
519 $res = db_query_params ('UPDATE plugin_contribtracker_actor SET (name,address,email,description,struct_id) = ($1,$2,$3,$4,$5) WHERE actor_id = $6',
526 if (!$res || db_affected_rows ($res) < 1) {
527 $this->setError (sprintf(_('Could not create object in update: %s'),
534 return $this->fetchData ($id) ;
538 $id = $this->getId () ;
540 $this->setError (_('Cannot delete a non-existing object')) ;
544 $res = db_query_params ('DELETE FROM plugin_contribtracker_actor WHERE actor_id = $1',
547 $this->setError (sprintf(_('Could not delete object in database: %s'),
552 $this->data_array = array () ;
558 if (isset ($this->data_array['actor_id'])) {
559 return $this->data_array['actor_id'] ;
564 function getName () { return $this->data_array['name'] ; }
565 function getAddress () { return $this->data_array['address'] ; }
566 function getEmail () { return $this->data_array['email'] ; }
567 function getDescription () { return $this->data_array['description'] ; }
568 function getLegalStructure () {
569 return new ContribTrackerLegalStructure ($this->data_array['struct_id']) ;
572 function getParticipations () {
573 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE actor_id = $1',
574 array ($this->getId())) ;
575 $ids = util_result_column_to_array ($res, 0) ;
577 $results = array () ;
578 foreach ($ids as $id) {
579 $results[] = new ContribTrackerParticipation ($id) ;
587 class ContribTrackerContribution extends Error {
590 function ContribTrackerContribution ($id=false) {
595 return $this->fetchData ($id) ;
598 function fetchData ($id) {
599 $res = db_query_params ('SELECT * FROM plugin_contribtracker_contribution WHERE contrib_id=$1',
601 if (!$res || db_numrows($res) < 1) {
602 $this->setError(sprintf('ContribTrackerContribution(): %s',db_error()));
606 $this->data_array = db_fetch_array ($res) ;
610 function create ($name, $date, $description, $group) {
611 if ($this->getId ()) {
612 $this->setError(_('Object already exists')) ;
617 $res = db_query_params ('INSERT INTO plugin_contribtracker_contribution (name,date,description,group_id) VALUES ($1,$2,$3,$4)',
622 if (!$res || db_affected_rows ($res) < 1) {
623 $this->setError (sprintf(_('Could not create object in database: %s'),
629 $id = db_insertid ($res, 'plugin_contribtracker_contribution', 'contrib_id') ;
631 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
638 return $this->fetchData ($id) ;
641 function update ($name, $date, $description, $group) {
642 if (! $this->getId ()) {
643 $this->setError(_('Object does not exist')) ;
647 $id = $this->getId () ;
650 $res = db_query_params ('UPDATE plugin_contribtracker_contribution SET (name,date,description,group_id) = ($1,$2,$3,$4) WHERE contrib_id = $5',
656 if (!$res || db_affected_rows ($res) < 1) {
657 $this->setError (sprintf(_('Could not create object update database: %s'),
664 return $this->fetchData ($id) ;
668 $id = $this->getId () ;
670 $this->setError (_('Cannot delete a non-existing object')) ;
674 $res = db_query_params ('DELETE FROM plugin_contribtracker_contribution WHERE contrib_id = $1',
677 $this->setError (sprintf(_('Could not delete object in database: %s'),
682 $this->data_array = array () ;
688 if (isset ($this->data_array['contrib_id'])) {
689 return $this->data_array['contrib_id'] ;
694 function getName () { return $this->data_array['name'] ; }
695 function getDate () { return $this->data_array['date'] ; }
696 function getDescription () { return $this->data_array['description'] ; }
697 function getGroup () {
698 return group_get_object ($this->data_array['group_id']) ;
701 function getParticipations () {
702 $res = db_query_params ('SELECT participation_id FROM plugin_contribtracker_participation WHERE contrib_id = $1',
703 array ($this->getId())) ;
704 $ids = util_result_column_to_array ($res, 0) ;
706 $results = array () ;
707 foreach ($ids as $id) {
708 $results[] = new ContribTrackerParticipation ($id) ;
715 class ContribTrackerParticipation extends Error {
718 function ContribTrackerParticipation ($id=false) {
723 return $this->fetchData ($id) ;
726 function fetchData ($id) {
727 $res = db_query_params ('SELECT * FROM plugin_contribtracker_participation WHERE participation_id=$1',
729 if (!$res || db_numrows($res) < 1) {
730 $this->setError(sprintf('ContribTrackerParticipation(): %s',db_error()));
734 $this->data_array = db_fetch_array ($res) ;
738 function create ($contrib, $actor, $role) {
739 if ($this->getId ()) {
740 $this->setError(_('Object already exists')) ;
745 $res = db_query_params ('INSERT INTO plugin_contribtracker_participation (contrib_id,actor_id,role_id) VALUES ($1,$2,$3)',
746 array ($contrib->getID(),
749 if (!$res || db_affected_rows ($res) < 1) {
750 $this->setError (sprintf(_('Could not create object in database: %s'),
756 $id = db_insertid ($res, 'plugin_contribtracker_participation', 'participation_id') ;
758 $this->setError (sprintf(_('Could not get ID from object in database: %s'),
765 return $this->fetchData ($id) ;
768 function update ($contrib, $actor, $role) {
769 if (! $this->getId ()) {
770 $this->setError(_('Object does not exist')) ;
774 $id = $this->getId () ;
777 $res = db_query_params ('UPDATE plugin_contribtracker_participation SET (contrib_id,actor_id,role_id) = ($1,$2,$3) WHERE participation_id = $4',
778 array ($contrib->getID(),
782 if (!$res || db_affected_rows ($res) < 1) {
783 $this->setError (sprintf(_('Could not create object update database: %s'),
790 return $this->fetchData ($id) ;
794 $id = $this->getId () ;
796 $this->setError (_('Cannot delete a non-existing object')) ;
800 $res = db_query_params ('DELETE FROM plugin_contribtracker_participation WHERE participation_id = $1',
803 $this->setError (sprintf(_('Could not delete object in database: %s'),
808 $this->data_array = array () ;
814 if (isset ($this->data_array['participation_id'])) {
815 return $this->data_array['participation_id'] ;
820 function getActor () {
821 return new ContribTrackerActor ($this->data_array['actor_id']) ;
823 function getRole () {
824 return new ContribTrackerRole ($this->data_array['role_id']) ;
826 function getContribution () {
827 return new ContribTrackerContribution ($this->data_array['contrib_id']) ;
833 // c-file-style: "bsd"