Update project field with issues time entries

One of the most used Redmine features is the ability to allocate hours to tasks. Users can enter their spent time to each task so that project managers can have a more detailed view of how time is being spent in their projects an organisation.

However when you want to show the spent time in a single number in your project, you can't have it in standard Redmine, since the application doesn't include such feature.

Here I will explain you how to get the total spent time from all issues in a project and show this sum in a project field.

Calculated custom field + Custom workflow

We are going to use two popular and very useful plugins: Calculated custom fields plugin and Custom workflow plugins.

Calculated custom field plugin

First we will create a project custom field of type “float” and marked as “Calculated”. This field will calculate the sum of all spent time input in all its issues.

The code to insert in the formula field is following:

TimeEntry.where(:project => self.project.id ).sum(:hours).to_f

This is how the field should look like:

Custom workflows plugin

OK, now the field is calculating the total spent time in the project but we've got a problem: this field will not be actually calculated until someone resaves it manually. Therefore we have to devise a way to force this action each time a user inserts spent time. We can do this by means of this issue custom workflow:

This is the code snippet for the workflow:

self.project.save unless project.nil?

Caveat: additional workflow for spent time entered through the log time form

Maybe you have noticed that the above method will only be effective if the user enters her spent time through the issue edit form. This method does not apply when the user enters her spent time on the log time form.

To solve this, you just have to create an additional time entry workflow (not an issues workflow). The code is exactly the same. This would be the additional workflow to create:

And this is how the project field will look like in the project's overview:

Here you can read other posts that involve the custom workflows plugin.

I hope this was a useful tip for you!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *