% Task management for Unified Process Methodology
%
% Copyright (c) 2006-2007 Stephane GALLAND <galland@arakhne.org>
% 
% This program is free library; you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or any later version.
%
% This library 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
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; see the file COPYING.  If not,
% write to the Free Software Foundation, Inc., 59 Temple Place - Suite
% 330, Boston, MA 02111-1307, USA.
%
% Creation date: 2006-04-20
% Modifications:
%   2007-03-19   Add localization.
%

\global\edef\upm@package@task@ver{2007/03/19}

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{upmethodology-task}[\upm@package@task@ver]

%----- LOCALIZATION
\def\upm@task@lang@english{
  \gdef\upm@lang@@{\message{**** upmethodology-task is using English language ****}}%
  \gdef\upm@task@lang@task{Task}
  \gdef\upm@task@lang@description{Description}
  \gdef\upm@task@lang@startat{Start at}
  \gdef\upm@task@lang@endat{End at}
  \gdef\upm@task@lang@archieved{Archieved}
  \gdef\upm@task@lang@managers{Managers}
  \gdef\upm@task@lang@members{Members}
  \gdef\upm@task@lang@milestones{Milestones}
  \gdef\upm@task@lang@subtask{Sub-task of}
}
\def\upm@task@lang@french{
  \gdef\upm@lang@@{\message{**** upmethodology-task is using French language ****}}%
  \gdef\upm@task@lang@task{T\^ache}
  \gdef\upm@task@lang@description{Description}
  \gdef\upm@task@lang@startat{D\'ebut le}
  \gdef\upm@task@lang@endat{Fin le}
  \gdef\upm@task@lang@archieved{Termin\'ee}
  \gdef\upm@task@lang@managers{R\'ef\'erent}
  \gdef\upm@task@lang@members{Membres}
  \gdef\upm@task@lang@milestones{\'Etapes}
  \gdef\upm@task@lang@subtask{Sous-t\^ache de}
}

%----- OPTIONS
\DeclareOption{french}{%
  \upm@task@lang@french
}
\DeclareOption{francais}{%
  \upm@task@lang@french
}
\DeclareOption{english}{%
  \upm@task@lang@english
}
\ExecuteOptions{english}
\ProcessOptions
\upm@lang@@

\RequirePackage{upmethodology-version}

%----- 
\newif\if@upm@task@displaydescription
\newif\if@upm@task@rebuild
\@upm@task@rebuildfalse

\def\upm@task@define#1#2#3{%
	\protected@write\@auxout{}{\string\global\string\@namedef{upm@task@#1@#2}{#3}}%
	\ifthenelse{\equal{\csname upm@task@#1@#2\endcsname}{#3}}{}{\@upm@task@rebuildtrue}%
	\global\@namedef{upm@task@#1@#2}{#3}%
}

\def\upm@task@define@list#1#2#3{%
	\ifthenelse{\equal{\csname upm@task@#1@#2\endcsname}{\@empty}}{%
		\expandafter\xdef\csname upm@task@#1@#2\endcsname{#3}%
	}{%
		\expandafter\xdef\csname upm@task@#1@#2\endcsname{%
			\csname upm@task@#1@#2\endcsname, #3%
		}%
	}%
	\AtEndDocument{\protected@write\@auxout{}{\string\global\string\@namedef{upm@task@#1@#2}{\csname upm@task@#1@#2\endcsname}}}
}

\newenvironment{upm@taskdescription}[1]{%
	\global\@namedef{upm@task@#1@managers}{}%
	\global\@namedef{upm@task@#1@members}{}%
	\global\@namedef{upm@task@#1@milestones}{}%

	\newcommand{\taskname}[1]{\upm@task@define{#1}{name}{##1}}
	\newcommand{\tasksuper}[1]{\upm@task@define{#1}{super}{##1}}
	\newcommand{\taskcomment}[1]{\upm@task@define{#1}{comment}{##1}}%
	\newcommand{\taskprogress}[1]{\upm@task@define{#1}{progress}{##1}}%
	\newcommand{\taskstart}[1]{\upm@task@define{#1}{startat}{##1}}%
	\newcommand{\taskend}[1]{\upm@task@define{#1}{endat}{##1}}%
	\newcommand{\taskmanager}[1]{\upm@task@define@list{#1}{managers}{##1}}
	\newcommand{\taskmember}[1]{%
		\upm@task@define@list{#1}{members}{##1}%
	}
	\newcommand{\taskmilestone}[2]{%
		\upm@task@define@list{#1}{milestones}{##1}%
		\upm@task@define{#1}{ml@##1}{##2}%
		%\global\@namedef{upm@task@#1@description@milestones}{\upm@task@lang@milestones: & \multicolumn{2}{X|}{\parbox[t]{1.9\linewidth}{\thetaskmembers{#1}}} \\}
	}


	\def\upm@task@currenttask{#1}
}{%
	\if@upm@task@displaydescription\thetaskdescription{\upm@task@currenttask}\fi%
	\let\upm@task@currenttask\relax%
}

%-----
%\begin{taskdescription}{id}...\end{taskdescription}
\newenvironment{taskdescription}[1]{%
	\@upm@task@displaydescriptiontrue%
	\upm@taskdescription{#1}%
}{%
	\endupm@taskdescription%
}

%-----
%\begin{taskdescription*}{id}...\end{taskdescription*}
\newenvironment{taskdescription*}[1]{%
	\@upm@task@displaydescriptionfalse%
	\upm@taskdescription{#1}%
}{%
	\endupm@taskdescription%
}

%-----
%\thetasksuper{id}
\newcommand{\thetasksuper}[1]{%
	\expandafter\providecommand\csname upm@task@#1@super\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@super\endcsname%
}

%-----
%\thetaskname{id}
\newcommand{\thetaskname}[1]{%
	\expandafter\providecommand\csname upm@task@#1@name\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@name\endcsname%
}

%-----
%\thetaskcomment{id}
\newcommand{\thetaskcomment}[1]{%
	\expandafter\providecommand\csname upm@task@#1@comment\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@comment\endcsname%
}

%-----
%\thetaskprogress{id}
\newcommand{\thetaskprogress}[1]{%
	\expandafter\providecommand\csname upm@task@#1@progress\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@progress\endcsname%
}

%-----
%\thetaskstart{id}
\newcommand{\thetaskstart}[1]{%
	\expandafter\providecommand\csname upm@task@#1@startat\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@startat\endcsname%
}

%-----
%\thetaskend{id}
\newcommand{\thetaskend}[1]{%
	\expandafter\providecommand\csname upm@task@#1@endat\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@endat\endcsname%
}

%-----
%\thetaskmanagers{id}
\newcommand{\thetaskmanagers}[1]{%
	\expandafter\providecommand\csname upm@task@#1@managers\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@managers\endcsname%
}

%-----
%\thetaskmembers{id}
\newcommand{\thetaskmembers}[1]{%
	\expandafter\providecommand\csname upm@task@#1@members\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@members\endcsname%
}

%-----
%\thetaskmilestones{id}
\newcommand{\thetaskmilestones}[1]{%
	\expandafter\providecommand\csname upm@task@#1@milestones\endcsname{??\@latex@warning{The task identified by '#1' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@milestones\endcsname%
}

%-----
%\thetaskmilestonecomment{id}{date}
\newcommand{\thetaskmilestonecomment}[2]{%
	\expandafter\providecommand\csname upm@task@#1@ml@#2\endcsname{??\@latex@warning{The task milestone identified by '#1' and '#2' was not found.}\@upm@task@rebuildtrue}%
	\expandafter\csname upm@task@#1@ml@#2\endcsname%
}

\def\@upm@trim#1{#1}

%-----
%\thetaskdescription{id}
\newcommand{\thetaskdescription}[2][\linewidth]{%
	\ifthenelse{\equal{\csname upm@task@#2@members\endcsname}{\@empty}}{
		\gdef\upm@task@tmp@a{}%
	}{%
		\gdef\upm@task@tmp@a{\upm@task@lang@members: & \multicolumn{2}{X|}{\parbox[t]{1.9\linewidth}{\thetaskmembers{#2}}} \\}%
	}%
	\ifthenelse{\equal{\csname upm@task@#2@milestones\endcsname}{\@empty}}{
		\gdef\upm@task@tmp@b{}%
	}{%
		\gdef\upm@task@tmp@b{\hline \upm@task@lang@milestones: &%
		\multicolumn{2}{X|}{\parbox[t]{1.95\linewidth}{%
		\edef\upm@task@tmp@c{\csname upm@task@#2@milestones\endcsname}%
		\def\upm@task@tmp@e{}%
		\@for\reserved@a:=\upm@task@tmp@c\do{%
			\edef\upm@task@tmp@d{\expandafter\@upm@trim\reserved@a}%
			\protected@edef\upm@task@tmp@e{\upm@task@tmp@e \protect\item[\upm@task@tmp@d~:] \protect\thetaskmilestonecomment{#2}{\upm@task@tmp@d}}%
		}%
		\begin{description}\upm@task@tmp@e\end{description}%
		}} \\}%
	}%
	\vspace{.25cm}\noindent\begin{mtabular}[#1]{3}{|X|X|X|}
		\tabulartitle{{\upm@task@lang@task}~\textit{#2}: \thetaskname{#2}}
		\hline
		%description
		\upm@task@lang@description: & \multicolumn{2}{X|}{\parbox[t]{1.9\linewidth}{\thetaskcomment{#2}}} \\
		%sub-task
		\expandafter\ifx\csname upm@task@#2@super\endcsname\relax\else%
		\upm@task@lang@subtask: & \multicolumn{2}{X|}{\parbox[t]{1.9\linewidth}{{\upm@task@lang@task}~\textit{\csname upm@task@#2@super\endcsname}}} \\
		\fi
		\hline
		%dates
		\upm@task@lang@startat: & \upm@task@lang@endat: & \upm@task@lang@archieved: \\
		\thetaskstart{#2} & \thetaskend{#2} & \thetaskprogress{#2}\% \\
		\hline
		%manager
		\upm@task@lang@managers: & \multicolumn{2}{X|}{\parbox[t]{1.9\linewidth}{\thetaskmanagers{#2}}} \\
		%members
		\upm@task@tmp@a
		%milestones
		\upm@task@tmp@b
		%\@nameuse{upm@task@#1@description@milestones}
		\hline
	\end{mtabular}\par\vspace{.5cm}%
}

\AtEndDocument{%
	\if@upm@task@rebuild%
	\@latex@warning@no@line{Project Task(s) may have changed.\MessageBreak%
	Rerun to get cross-references right}\fi%

}
\endinput
