2013-02-28 13:53:29 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
##############################################################################
from openerp . osv import fields , osv
from templates import TemplateHelper
from datetime import date , datetime , timedelta
import calendar
2013-03-29 16:31:37 +00:00
import logging
_logger = logging . getLogger ( __name__ )
2013-02-28 13:53:29 +00:00
def start_end_date_for_period ( period ) :
""" Return the start and end date for a goal period based on today
2013-03-15 13:39:29 +00:00
: return : ( start_date , end_date ) , datetime . date objects , False if the period is
2013-02-28 13:53:29 +00:00
not defined or unknown """
today = date . today ( )
if period == ' daily ' :
start_date = today
2013-03-14 15:09:55 +00:00
end_date = start_date
2013-02-28 13:53:29 +00:00
elif period == ' weekly ' :
delta = timedelta ( days = today . weekday ( ) )
start_date = today - delta
end_date = start_date + timedelta ( days = 7 )
elif period == ' monthly ' :
month_range = calendar . monthrange ( today . year , today . month )
2013-03-01 08:11:10 +00:00
start_date = today . replace ( day = 1 )
2013-02-28 13:53:29 +00:00
end_date = today . replace ( day = month_range [ 1 ] )
elif period == ' yearly ' :
start_date = today . replace ( month = 1 , day = 1 )
end_date = today . replace ( month = 12 , day = 31 )
2013-03-13 16:43:25 +00:00
else : # period == 'once':
2013-03-11 09:39:30 +00:00
start_date = False # for manual goal, start each time
2013-02-28 13:53:29 +00:00
end_date = False
2013-03-11 09:39:30 +00:00
2013-02-28 13:53:29 +00:00
return ( start_date , end_date )
class gamification_goal_plan ( osv . Model ) :
""" Gamification goal plan
Set of predifined goals to be able to automate goal settings or
quickly apply several goals manually to a group of users
If ' user_ids ' is defined and ' period ' is different than ' one ' , the set will
2013-03-11 09:39:30 +00:00
be assigned to the users for each period ( eg : every 1 st of each month if
' monthly ' is selected )
2013-02-28 13:53:29 +00:00
"""
_name = ' gamification.goal.plan '
_description = ' Gamification goal plan '
_inherit = ' mail.thread '
2013-03-04 11:27:12 +00:00
def _get_next_report_date ( self , cr , uid , ids , field_name , arg , context = None ) :
""" Return the next report date based on the last report date and report
2013-03-15 13:39:29 +00:00
period .
: return : a string in isoformat representing the date """
2013-03-04 11:27:12 +00:00
res = { }
for plan in self . browse ( cr , uid , ids , context ) :
2013-03-13 10:26:14 +00:00
last = datetime . strptime ( plan . last_report_date , ' % Y- % m- %d ' ) . date ( )
2013-03-04 11:27:12 +00:00
if plan . report_message_frequency == ' daily ' :
2013-03-13 10:49:00 +00:00
next = last + timedelta ( days = 1 )
res [ plan . id ] = next . isoformat ( )
2013-03-04 11:27:12 +00:00
elif plan . report_message_frequency == ' weekly ' :
2013-03-13 10:49:00 +00:00
next = last + timedelta ( days = 7 )
res [ plan . id ] = next . isoformat ( )
2013-03-04 11:27:12 +00:00
elif plan . report_message_frequency == ' monthly ' :
month_range = calendar . monthrange ( last . year , last . month )
2013-03-13 10:49:00 +00:00
next = last . replace ( day = month_range [ 1 ] ) + timedelta ( days = 1 )
res [ plan . id ] = next . isoformat ( )
2013-03-04 11:27:12 +00:00
elif plan . report_message_frequency == ' yearly ' :
2013-03-13 10:26:14 +00:00
res [ plan . id ] = last . replace ( year = last . year + 1 ) . isoformat ( )
2013-03-11 09:39:30 +00:00
else : # frequency == 'once':
2013-03-13 10:26:14 +00:00
res [ plan . id ] = False
2013-03-04 11:27:12 +00:00
return res
2013-04-08 13:17:29 +00:00
def _planline_count ( self , cr , uid , ids , field_name , arg , context = None ) :
res = dict . fromkeys ( ids , 0 )
for plan in self . browse ( cr , uid , ids , context ) :
res [ plan . id ] = len ( plan . planline_ids )
return res
2013-02-28 13:53:29 +00:00
_columns = {
2013-03-13 10:26:14 +00:00
' name ' : fields . char ( ' Plan Name ' , required = True , translate = True ) ,
2013-04-08 13:17:29 +00:00
' state ' : fields . selection ( [
( ' draft ' , ' Draft ' ) ,
( ' inprogress ' , ' In Progress ' ) ,
( ' done ' , ' Done ' ) ,
] ,
string = ' State ' ,
required = True ) ,
' manager_id ' : fields . many2one ( ' res.users ' ,
string = ' Responsible ' , help = " The user responsible for the plan. " ) ,
' start_date ' : fields . date ( ' Planned Start Date ' , help = " The day a new plan will be automatically started. The start and end dates for goals are still defined by the periodicity (eg: weekly goals run from Monday to Sunday). " ) ,
2013-03-13 10:26:14 +00:00
' user_ids ' : fields . many2many ( ' res.users ' ,
2013-02-28 13:53:29 +00:00
string = ' Users ' ,
2013-03-06 16:56:59 +00:00
help = " List of users to which the goal will be set " ) ,
2013-04-08 13:17:29 +00:00
' autojoin_group_id ' : fields . many2one ( ' res.groups ' ,
string = ' Auto-subscription Group ' ,
help = ' Group of users whose members will automatically be added to the users ' ) ,
2013-03-13 10:26:14 +00:00
' planline_ids ' : fields . one2many ( ' gamification.goal.planline ' ,
2013-02-28 13:53:29 +00:00
' plan_id ' ,
string = ' Planline ' ,
help = " list of goals that will be set " ,
required = True ) ,
2013-04-08 13:17:29 +00:00
' planline_count ' : fields . function ( _planline_count , type = ' integer ' , string = " Planlines " ) ,
2013-03-13 10:26:14 +00:00
' period ' : fields . selection ( [
2013-04-08 13:17:29 +00:00
( ' once ' , ' Non recurring ' ) ,
2013-02-28 13:53:29 +00:00
( ' daily ' , ' Daily ' ) ,
( ' weekly ' , ' Weekly ' ) ,
( ' monthly ' , ' Monthly ' ) ,
( ' yearly ' , ' Yearly ' )
] ,
string = ' Periodicity ' ,
2013-03-01 15:43:36 +00:00
help = ' Period of automatic goal assigment. If none is selected, should be launched manually. ' ,
2013-02-28 13:53:29 +00:00
required = True ) ,
2013-03-13 10:26:14 +00:00
' visibility_mode ' : fields . selection ( [
2013-04-08 13:17:29 +00:00
( ' progressbar ' , ' Individual Goals ' ) ,
( ' board ' , ' Leader Board (Group Ranking) ' ) ,
2013-02-28 13:53:29 +00:00
] ,
2013-04-10 10:14:54 +00:00
string = " Display Mode " , required = True ) ,
2013-03-13 10:26:14 +00:00
' report_message_frequency ' : fields . selection ( [
2013-02-28 13:53:29 +00:00
( ' never ' , ' Never ' ) ,
( ' onchange ' , ' On change ' ) ,
( ' daily ' , ' Daily ' ) ,
( ' weekly ' , ' Weekly ' ) ,
( ' monthly ' , ' Monthly ' ) ,
( ' yearly ' , ' Yearly ' )
] ,
2013-04-10 10:14:54 +00:00
string = " Report Frequency " , required = True ) ,
2013-03-13 10:26:14 +00:00
' report_message_group_id ' : fields . many2one ( ' mail.group ' ,
2013-02-28 13:53:29 +00:00
string = ' Send a copy to ' ,
help = ' Group that will receive a copy of the report in addition to the user ' ) ,
2013-03-13 10:26:14 +00:00
' report_header ' : fields . text ( ' Report Header ' ) ,
2013-04-08 13:17:29 +00:00
' remind_update_delay ' : fields . integer ( ' Non-updated manual goals will be reminded after ' ,
help = " Never reminded if no value or zero is specified. " ) ,
2013-03-04 11:27:12 +00:00
' last_report_date ' : fields . date ( ' Last Report Date ' ) ,
' next_report_date ' : fields . function ( _get_next_report_date ,
type = ' date ' ,
string = ' Next Report Date ' ) ,
2013-04-10 10:14:54 +00:00
' category ' : fields . selection ( [
2013-04-11 09:21:03 +00:00
( ' hr ' , ' Human Ressources / Appraisals ' ) ,
2013-04-10 10:14:54 +00:00
( ' other ' , ' Other ' ) ,
] ,
2013-04-11 09:21:03 +00:00
string = " Appears in " , help = " Only HR plans can be accessed by managers " , required = True )
2013-02-28 13:53:29 +00:00
}
_defaults = {
' period ' : ' once ' ,
' state ' : ' draft ' ,
' visibility_mode ' : ' progressbar ' ,
' report_message_frequency ' : ' onchange ' ,
2013-04-08 13:17:29 +00:00
' last_report_date ' : fields . date . today ,
' start_date ' : fields . date . today ,
' manager_id ' : lambda s , cr , uid , c : uid ,
2013-04-10 10:14:54 +00:00
' category ' : ' hr ' ,
2013-02-28 13:53:29 +00:00
}
def write ( self , cr , uid , ids , vals , context = None ) :
""" Overwrite the write method to add the user of groups """
2013-03-07 13:44:47 +00:00
context = context or { }
2013-03-05 09:34:42 +00:00
if not ids :
return True
2013-02-28 13:53:29 +00:00
write_res = super ( gamification_goal_plan , self ) . write ( cr , uid , ids , vals , context = context )
2013-03-05 09:34:42 +00:00
2013-02-28 13:53:29 +00:00
# add users when change the group auto-subscription
if ' autojoin_group_id ' in vals :
new_group = self . pool . get ( ' res.groups ' ) . browse ( cr , uid , vals [ ' autojoin_group_id ' ] , context = context )
2013-03-04 15:31:38 +00:00
if new_group :
self . plan_subscribe_users ( cr , uid , ids , [ user . id for user in new_group . users ] , context = context )
2013-03-13 10:12:58 +00:00
2013-02-28 13:53:29 +00:00
return write_res
2013-03-01 15:43:36 +00:00
2013-03-06 14:45:44 +00:00
def _cron_update ( self , cr , uid , context = None , ids = False ) :
""" Daily cron check.
2013-02-28 13:53:29 +00:00
2013-03-06 14:45:44 +00:00
Start planned plans ( in draft and with start_date = today )
2013-04-04 15:23:59 +00:00
Create the goals for planlines not linked to goals ( eg : modified the
2013-02-28 13:53:29 +00:00
plan to add planlines )
2013-04-12 12:19:13 +00:00
Update every plan running
2013-03-29 16:31:37 +00:00
"""
2013-03-06 14:45:44 +00:00
if not context : context = { }
# start planned plans
2013-03-12 14:15:31 +00:00
planned_plan_ids = self . search ( cr , uid , [
( ' state ' , ' = ' , ' draft ' ) ,
2013-04-02 09:59:18 +00:00
( ' start_date ' , ' <= ' , fields . date . today ( ) ) ] )
2013-03-06 14:45:44 +00:00
self . action_start ( cr , uid , planned_plan_ids , context = context )
2013-02-28 13:53:29 +00:00
if not ids :
ids = self . search ( cr , uid , [ ( ' state ' , ' = ' , ' inprogress ' ) ] )
2013-03-06 14:45:44 +00:00
return self . _update_all ( cr , uid , ids , context = context )
def _update_all ( self , cr , uid , ids , context = None ) :
""" Update the plans and related goals
2013-03-12 14:15:31 +00:00
: param list ( int ) ids : the ids of the plans to update , if False will
2013-03-06 14:45:44 +00:00
update only plans in progress . """
if not context : context = { }
2013-03-07 09:12:01 +00:00
goal_obj = self . pool . get ( ' gamification.goal ' )
2013-04-12 12:19:13 +00:00
# we use yesterday to update the goals that just ended
yesterday = date . today ( ) - timedelta ( days = 1 )
goal_ids = goal_obj . search ( cr , uid , [
( ' plan_id ' , ' in ' , ids ) ,
' | ' ,
( ' state ' , ' in ' , ( ' inprogress ' , ' inprogress_update ' ) ) ,
' & ' ,
( ' state ' , ' in ' , ( ' reached ' , ' failed ' ) ) ,
' | ' ,
( ' end_date ' , ' >= ' , yesterday . isoformat ( ) ) ,
( ' end_date ' , ' = ' , False )
] , context = context )
# update every running goal already generated linked to selected plans
goal_obj . update ( cr , uid , goal_ids , context = context )
2013-03-04 11:27:12 +00:00
for plan in self . browse ( cr , uid , ids , context = context ) :
2013-04-04 15:23:59 +00:00
if plan . autojoin_group_id :
2013-04-10 08:49:40 +00:00
self . plan_subscribe_users ( cr , uid , [ plan . id ] , [ user . id for user in plan . autojoin_group_id . users ] , context = context )
2013-04-04 15:23:59 +00:00
self . generate_goals_from_plan ( cr , uid , [ plan . id ] , context = context )
2013-03-04 11:27:12 +00:00
# goals closed but still opened at the last report date
2013-03-12 14:15:31 +00:00
closed_goals_to_report = goal_obj . search ( cr , uid , [
( ' plan_id ' , ' = ' , plan . id ) ,
( ' start_date ' , ' >= ' , plan . last_report_date ) ,
( ' end_date ' , ' <= ' , plan . last_report_date )
] )
2013-03-04 11:27:12 +00:00
if len ( closed_goals_to_report ) > 0 :
# some goals need a final report
self . report_progress ( cr , uid , plan , subset_goal_ids = closed_goals_to_report , context = context )
2013-03-12 14:15:31 +00:00
self . write ( cr , uid , plan . id , { ' last_report_date ' : fields . date . today } , context = context )
2013-03-04 11:27:12 +00:00
if fields . date . today ( ) == plan . next_report_date :
self . report_progress ( cr , uid , plan , context = context )
2013-03-12 14:15:31 +00:00
self . write ( cr , uid , plan . id , { ' last_report_date ' : fields . date . today } , context = context )
2013-04-05 15:19:53 +00:00
return True
2013-03-04 11:27:12 +00:00
2013-03-25 16:15:27 +00:00
def quick_update ( self , cr , uid , plan_id , context = None ) :
""" Update all the goals of a plan, no generation of new goals """
if not context : context = { }
plan = self . browse ( cr , uid , plan_id , context = context )
goal_ids = self . pool . get ( ' gamification.goal ' ) . search ( cr , uid , [ ( ' plan_id ' , ' = ' , plan_id ) ] , context = context )
self . pool . get ( ' gamification.goal ' ) . update ( cr , uid , goal_ids , context = context )
return True
2013-02-28 13:53:29 +00:00
def action_start ( self , cr , uid , ids , context = None ) :
""" Start a draft goal plan
2013-03-15 11:09:38 +00:00
Change the state of the plan to in progress and genereate related goals
"""
2013-03-04 15:31:38 +00:00
# subscribe users if autojoin group
for plan in self . browse ( cr , uid , ids , context = context ) :
if plan . autojoin_group_id :
self . plan_subscribe_users ( cr , uid , ids , [ user . id for user in plan . autojoin_group_id . users ] , context = context )
2013-03-29 16:31:37 +00:00
if len ( plan . user_ids ) > 0 :
self . write ( cr , uid , plan . id , { ' state ' : ' inprogress ' } , context = context )
else :
_logger . warning ( " Can not start planned plan, no subscribed users " )
2013-03-06 14:45:44 +00:00
return self . generate_goals_from_plan ( cr , uid , ids , context = context )
2013-02-28 13:53:29 +00:00
def action_check ( self , cr , uid , ids , context = None ) :
""" Check a goal plan
Create goals that haven ' t been created yet (eg: if added users of planlines)
Recompute the current value for each goal related """
2013-03-06 14:45:44 +00:00
return self . _update_all ( cr , uid , ids = ids , context = context )
2013-02-28 13:53:29 +00:00
def action_close ( self , cr , uid , ids , context = None ) :
""" Close a plan in progress
Change the state of the plan to in done
Does NOT close the related goals , this is handled by the goal itself """
return self . write ( cr , uid , ids , { ' state ' : ' done ' } , context = context )
def action_reset ( self , cr , uid , ids , context = None ) :
""" Reset a closed goal plan
Change the state of the plan to in progress
Closing a plan does not affect the goals so neither does reset """
return self . write ( cr , uid , ids , { ' state ' : ' inprogress ' } , context = context )
def action_cancel ( self , cr , uid , ids , context = None ) :
""" Cancel a plan in progress
Change the state of the plan to draft
Cancel the related goals """
self . write ( cr , uid , ids , { ' state ' : ' draft ' } , context = context )
2013-03-12 14:15:31 +00:00
goal_ids = self . pool . get ( ' gamification.goal ' ) . search ( cr , uid , [ ( ' plan_id ' , ' in ' , ids ) ] , context = context )
self . pool . get ( ' gamification.goal ' ) . write ( cr , uid , goal_ids , { ' state ' : ' canceled ' } , context = context )
2013-02-28 13:53:29 +00:00
return True
2013-03-04 11:27:12 +00:00
def action_report_progress ( self , cr , uid , ids , context = None ) :
""" Manual report of a goal, does not influence automatic report frequency """
for plan in self . browse ( cr , uid , ids , context ) :
self . report_progress ( cr , uid , plan , context = context )
return True
2013-02-28 13:53:29 +00:00
def generate_goals_from_plan ( self , cr , uid , ids , context = None ) :
2013-03-01 11:27:20 +00:00
""" Generate the list of goals linked to a plan.
2013-03-12 14:15:31 +00:00
If goals already exist for this planline , the planline is skipped . This
2013-03-01 11:27:20 +00:00
can be called after each change in the user or planline list .
: param list ( int ) ids : the list of plan concerned """
2013-02-28 13:53:29 +00:00
for plan in self . browse ( cr , uid , ids , context ) :
( start_date , end_date ) = start_end_date_for_period ( plan . period )
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
for planline in plan . planline_ids :
for user in plan . user_ids :
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
goal_obj = self . pool . get ( ' gamification.goal ' )
2013-03-12 14:15:31 +00:00
domain = [ ( ' planline_id ' , ' = ' , planline . id ) , ( ' user_id ' , ' = ' , user . id ) ]
2013-02-28 13:53:29 +00:00
if start_date :
domain . append ( ( ' start_date ' , ' = ' , start_date ) )
2013-03-12 14:15:31 +00:00
# goal already existing for this planline ?
2013-02-28 13:53:29 +00:00
if len ( goal_obj . search ( cr , uid , domain , context = context ) ) > 0 :
# resume canceled goals
domain . append ( ( ' state ' , ' = ' , ' canceled ' ) )
canceled_goal_ids = goal_obj . search ( cr , uid , domain , context = context )
goal_obj . write ( cr , uid , canceled_goal_ids , { ' state ' : ' inprogress ' } , context = context )
goal_obj . update ( cr , uid , canceled_goal_ids , context = context )
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
# skip to next user
continue
values = {
2013-03-12 14:15:31 +00:00
' type_id ' : planline . type_id . id ,
' planline_id ' : planline . id ,
' user_id ' : user . id ,
' target_goal ' : planline . target_goal ,
' state ' : ' inprogress ' ,
2013-02-28 13:53:29 +00:00
}
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
if start_date :
values [ ' start_date ' ] = start_date . isoformat ( )
if end_date :
values [ ' end_date ' ] = end_date . isoformat ( )
if planline . plan_id . remind_update_delay :
values [ ' remind_update_delay ' ] = planline . plan_id . remind_update_delay
new_goal_id = goal_obj . create ( cr , uid , values , context )
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
goal_obj . update ( cr , uid , [ new_goal_id ] , context = context )
return True
def plan_subscribe_users ( self , cr , uid , ids , new_user_ids , context = None ) :
""" Add the following users to plans
: param ids : ids of plans to which the users will be added
2013-03-01 11:27:20 +00:00
: param new_user_ids : ids of the users to add """
2013-02-28 13:53:29 +00:00
2013-03-12 14:15:31 +00:00
for plan in self . browse ( cr , uid , ids , context ) :
2013-02-28 13:53:29 +00:00
subscription = [ user . id for user in plan . user_ids ]
subscription . extend ( new_user_ids )
2013-03-01 11:27:20 +00:00
# remove duplicates
2013-02-28 13:53:29 +00:00
unified_subscription = list ( set ( subscription ) )
2013-03-05 09:34:42 +00:00
self . write ( cr , uid , ids , { ' user_ids ' : [ ( 4 , user ) for user in unified_subscription ] } , context = context )
2013-02-28 13:53:29 +00:00
return True
2013-03-25 15:39:53 +00:00
def get_board_goal_info ( self , cr , uid , plan , subset_goal_ids = False , context = None ) :
""" Get the list of latest goals for a plan, sorted by user ranking for each planline """
goal_obj = self . pool . get ( ' gamification.goal ' )
planlines_boards = [ ]
( start_date , end_date ) = start_end_date_for_period ( plan . period )
for planline in plan . planline_ids :
domain = [
( ' planline_id ' , ' = ' , planline . id ) ,
( ' state ' , ' in ' , ( ' inprogress ' , ' inprogress_update ' ,
' reached ' , ' failed ' ) ) ,
]
if subset_goal_ids :
goal_ids = goal_obj . search ( cr , uid , domain , context = context )
common_goal_ids = [ goal for goal in goal_ids if goal in subset_goal_ids ]
else :
# if no subset goals, use the dates for restriction
if start_date :
domain . append ( ( ' start_date ' , ' = ' , start_date . isoformat ( ) ) )
if end_date :
domain . append ( ( ' end_date ' , ' = ' , end_date . isoformat ( ) ) )
common_goal_ids = goal_obj . search ( cr , uid , domain , context = context )
board_goals = [ goal for goal in goal_obj . browse ( cr , uid , common_goal_ids , context = context ) ]
2013-04-08 14:11:32 +00:00
if len ( board_goals ) == 0 :
# planline has no generated goals
continue
2013-03-25 15:39:53 +00:00
# most complete first, current if same percentage (eg: if several 100%)
sorted_board = enumerate ( sorted ( board_goals , key = lambda k : ( k . completeness , k . current ) , reverse = True ) )
2013-04-12 15:21:50 +00:00
planlines_boards . append ( { ' goal_type ' : planline . type_id , ' board_goals ' : sorted_board , ' target_goal ' : planline . target_goal } )
2013-03-25 15:39:53 +00:00
return planlines_boards
def get_indivual_goal_info ( self , cr , uid , user_id , plan , subset_goal_ids = False , context = None ) :
""" Get the list of latest goals of a user for a plan """
domain = [
( ' plan_id ' , ' = ' , plan . id ) ,
( ' user_id ' , ' = ' , user_id ) ,
( ' state ' , ' in ' , ( ' inprogress ' , ' inprogress_update ' ,
' reached ' , ' failed ' ) ) ,
]
goal_obj = self . pool . get ( ' gamification.goal ' )
( start_date , end_date ) = start_end_date_for_period ( plan . period )
if subset_goal_ids :
# use the domain for safety, don't want irrelevant report if wrong argument
goal_ids = goal_obj . search ( cr , uid , domain , context = context )
related_goal_ids = [ goal for goal in goal_ids if goal in subset_goal_ids ]
else :
# if no subset goals, use the dates for restriction
if start_date :
domain . append ( ( ' start_date ' , ' = ' , start_date . isoformat ( ) ) )
if end_date :
domain . append ( ( ' end_date ' , ' = ' , end_date . isoformat ( ) ) )
related_goal_ids = goal_obj . search ( cr , uid , domain , context = context )
if len ( related_goal_ids ) == 0 :
return False
values = { ' goals ' : [ ] }
all_done = True
for goal in goal_obj . browse ( cr , uid , related_goal_ids , context = context ) :
if goal . end_date :
if goal . end_date < fields . date . today ( ) :
# do not include goals of previous plan run
continue
else :
all_done = False
else :
if goal . state == ' inprogress ' or goal . state == ' inprogress_update ' :
all_done = False
values [ ' goals ' ] . append ( goal )
if all_done :
# skip plans where all goal are done or failed
return False
else :
return values
2013-03-04 11:27:12 +00:00
def report_progress ( self , cr , uid , plan , context = None , users = False , subset_goal_ids = False ) :
2013-02-28 13:53:29 +00:00
""" Post report about the progress of the goals
2013-03-04 11:27:12 +00:00
: param plan : the plan object that need to be reported
2013-03-12 14:15:31 +00:00
: param users : the list ( res . users ) of users that are concerned by
2013-02-28 13:53:29 +00:00
the report . If False , will send the report to every user concerned
2013-03-07 13:44:47 +00:00
( goal users and group that receive a copy ) . Only used for plan with
2013-03-04 11:27:12 +00:00
a visibility mode set to ' personal ' .
2013-03-12 14:15:31 +00:00
: param goal_ids : the list ( int ) of goal ids linked to the plan for
the report . If not specified , use the goals for the current plan
2013-03-04 11:27:12 +00:00
period . This parameter can be used to produce report for previous plan
2013-03-12 14:15:31 +00:00
periods .
: param subset_goal_ids : a list ( int ) of goal ids to restrict the report
"""
2013-02-28 13:53:29 +00:00
context = context or { }
goal_obj = self . pool . get ( ' gamification.goal ' )
template_env = TemplateHelper ( )
2013-03-04 11:27:12 +00:00
if plan . visibility_mode == ' board ' :
2013-03-25 15:39:53 +00:00
planlines_boards = self . get_board_goal_info ( cr , uid , plan , subset_goal_ids , context )
2013-03-12 14:15:31 +00:00
body_html = template_env . get_template ( ' group_progress.mako ' ) . render ( { ' object ' : plan , ' planlines_boards ' : planlines_boards } )
2013-03-04 11:27:12 +00:00
self . message_post ( cr , uid , plan . id ,
body = body_html ,
partner_ids = [ ( 6 , 0 , [ user . partner_id . id for user in plan . user_ids ] ) ] ,
context = context ,
subtype = ' mail.mt_comment ' )
if plan . report_message_group_id :
self . pool . get ( ' mail.group ' ) . message_post ( cr , uid , plan . report_message_group_id . id ,
body = body_html ,
context = context ,
subtype = ' mail.mt_comment ' )
2013-03-12 14:15:31 +00:00
2013-03-04 11:27:12 +00:00
else :
# generate individual reports
for user in users or plan . user_ids :
2013-03-25 15:39:53 +00:00
values = self . get_indivual_goal_info ( cr , uid , user . id , plan , subset_goal_ids , context = context )
if not values :
2013-03-04 11:27:12 +00:00
continue
2013-03-25 15:39:53 +00:00
values [ ' object ' ] = plan
values [ ' user ' ] = user ,
body_html = template_env . get_template ( ' personal_progress.mako ' ) . render ( values )
2013-03-12 14:15:31 +00:00
2013-02-28 13:53:29 +00:00
self . message_post ( cr , uid , plan . id ,
2013-03-12 14:15:31 +00:00
body = body_html ,
partner_ids = [ ( 4 , user . partner_id . id ) ] ,
context = context ,
subtype = ' mail.mt_comment ' )
2013-02-28 16:35:13 +00:00
if plan . report_message_group_id :
self . pool . get ( ' mail.group ' ) . message_post ( cr , uid , plan . report_message_group_id . id ,
2013-03-12 14:15:31 +00:00
body = body_html ,
context = context ,
subtype = ' mail.mt_comment ' )
2013-02-28 13:53:29 +00:00
return True
2013-03-04 15:31:38 +00:00
2013-02-28 13:53:29 +00:00
class gamification_goal_planline ( osv . Model ) :
""" Gamification goal planline
Predifined goal for ' gamification_goal_plan '
These are generic list of goals with only the target goal defined
Should only be created for the gamification_goal_plan object
"""
_name = ' gamification.goal.planline '
_description = ' Gamification generic goal for plan '
2013-04-04 15:23:59 +00:00
_order = " sequence_type, id "
2013-02-28 13:53:29 +00:00
def _get_planline_types ( self , cr , uid , ids , context = None ) :
""" Return the ids of planline items related to the gamification.goal.type
objects in ' ids (used to update the value of ' sequence_type ' ) ' """
result = { }
for goal_type in self . pool . get ( ' gamification.goal.type ' ) . browse ( cr , uid , ids , context = context ) :
domain = [ ( ' type_id ' , ' = ' , goal_type . id ) ]
planline_ids = self . pool . get ( ' gamification.goal.planline ' ) . search ( cr , uid , domain , context = context )
for p_id in planline_ids :
result [ p_id ] = True
return result . keys ( )
_columns = {
2013-04-04 15:23:59 +00:00
' name ' : fields . related ( ' type_id ' , ' name ' , string = " Name " ) ,
2013-03-12 14:15:31 +00:00
' plan_id ' : fields . many2one ( ' gamification.goal.plan ' ,
2013-02-28 13:53:29 +00:00
string = ' Plan ' ,
2013-04-04 15:23:59 +00:00
required = True ,
2013-02-28 13:53:29 +00:00
ondelete = " cascade " ) ,
2013-03-12 14:15:31 +00:00
' type_id ' : fields . many2one ( ' gamification.goal.type ' ,
2013-02-28 13:53:29 +00:00
string = ' Goal Type ' ,
required = True ,
ondelete = " cascade " ) ,
2013-03-12 14:15:31 +00:00
' target_goal ' : fields . float ( ' Target Value to Reach ' ,
2013-02-28 13:53:29 +00:00
required = True ) ,
2013-03-12 14:15:31 +00:00
' sequence_type ' : fields . related ( ' type_id ' , ' sequence ' ,
2013-02-28 13:53:29 +00:00
type = ' integer ' ,
string = ' Sequence ' ,
readonly = True ,
store = {
' gamification.goal.type ' : ( _get_planline_types , [ ' sequence ' ] , 10 ) ,
} ) ,
2013-04-05 15:44:37 +00:00
' type_condition ' : fields . related ( ' type_id ' , ' condition ' , type = " selection " ,
readonly = True , string = " Condition " , selection = [ ( ' lower ' , ' <= ' ) , ( ' higher ' , ' >= ' ) ] ) ,
' type_unit ' : fields . related ( ' type_id ' , ' unit ' , type = " char " , readonly = True , string = " Unit " ) ,
2013-04-08 13:17:29 +00:00
' type_monetary ' : fields . related ( ' type_id ' , ' monetary ' , type = " boolean " , readonly = True , string = " Monetary " ) ,
2013-04-10 08:49:40 +00:00
' type_full_suffix ' : fields . related ( ' type_id ' , ' full_suffix ' , type = " char " , readonly = True , string = " Suffix " ) ,
2013-03-04 11:27:12 +00:00
}