%%
%% This is file `homework.cls',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% homework.dtx  (with options: `class')
%% 
%% Copyright (C) 2017 by Kim Wittenburg
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3 of this license or (at your option) any later
%% version.  The latest version of this license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.

\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\ProvidesClass{homework}[2017/04/15 v1.1 The Homework Class]



\newif\ifhomework@shorttitle
\DeclareOption{shorttitle}{
    \homework@shorttitletrue
}

\DeclareOption{longtitle}{
    \homework@shorttitlefalse
}

\newif\ifhomework@shortheader
\DeclareOption{shortheader}{
    \homework@shortheadertrue
}

\DeclareOption{longheader}{
    \homework@shortheaderfalse
}

\ExecuteOptions{longtitle,longheader}

\ProcessOptions\relax
\LoadClassWithOptions{scrartcl}

%%%%%%%% Dependencies %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%\fi

\RequirePackage[utf8]{inputenc} % UTF-8 Encoding
\RequirePackage{cmap} % Special Character
\RequirePackage[T1]{fontenc} % Font Encoding
\RequirePackage[ngerman]{babel} % German Language
\RequirePackage{etoolbox} % Programming
\RequirePackage{calc} % Calculations

\RequirePackage[a4paper,
                left=25mm,
                right=25mm,
                top=20mm,
                bottom=50mm]{geometry} % Page Margins

\RequirePackage[headsepline]{scrlayer-scrpage} % Header and Footer
\RequirePackage{lastpage} % Page Numbers

\RequirePackage{xcolor} % Colors
\RequirePackage{framed} % Colored Boxes
\RequirePackage[colorlinks,linkcolor=black]{hyperref} % Links and References
\RequirePackage{lmodern} % Better Font

%%%%%%% Custom Commands %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%\fi

\newcommand{\university}[2][]{
    \ifstrempty{#1}{
        \gdef\@shortuniversity{#2}
    }{
        \gdef\@shortuniversity{#1}
    }
    \gdef\@university{#2}
}

\newcommand{\course}[2][]{
    \ifstrempty{#1}{
        \gdef\@shortcourse{#2}
    }{
        \gdef\@shortcourse{#1}
    }
    \gdef\@course{#2}
}

\newcommand{\teacher}[1]{
    \gdef\@teacher{#1}
}

\newcommand{\group}[1]{
    \gdef\@group{#1}
}

\newcommand{\sheetno}[1]{
    \gdef\@sheetno{#1}
}

\newcommand{\id}[1]{\\\ttfamily\footnotesize(#1)}

\university[Uni Hamburg]{Universität Hamburg}
\course{}
\group{}
\teacher{}
\sheetno{}
\title{}


\newcommand{\todomark}{%
    \colorbox{purple}{%
        \textnormal\ttfamily\bfseries\color{white}%
        TODO%
    }%
}

\newcommand{\todo}[1][]{%
    \ifstrempty{#1}{%
        \def\todotext{Todo}%
    }{%
        \def\todotext{Todo: #1}%
    }%
    \todomark%
    {%
        \marginpar{%
            \raggedright\normalfont\sffamily\scriptsize\todotext%
        }%
    }%
}



\setcounter{secnumdepth}{0}

\newif\if@tasknumbers
\@tasknumberstrue

\newcounter{task}
\newcommand{\task}[2][]{%
    \ifstrempty{#1}{%
        \stepcounter{task}%
    }{%
        \setcounter{task}{#1}%
    }%
    \if@tasknumbers%
        \ifstrempty{#2}{%
            \section{Aufgabe \arabic{task}}%
        }{%
            \section{Aufgabe \arabic{task}: #2}%
        }%
    \else%
        \section{#2}%
    \fi%
}

\newcounter{subtask}[task]
\newcommand{\subtask}[2][]{%
    \ifstrempty{#1}{%
        \stepcounter{subtask}%
    }{%
        \setcounter{subtask}{#1}%
    }%
    \if@tasknumbers%
        \ifstrempty{#2}{%
            \subsection{Teilaufgabe \arabic{task}.\arabic{subtask}}%
        }{%
            \subsection{\arabic{task}.\arabic{subtask}. #2}%
        }%
    \else%
        \subsection{#2}%
    \fi%
}

\newcounter{subsubtask}[subtask]
\newcommand{\subsubtask}[2][]{%
    \ifstrempty{#1}{%
        \stepcounter{subsubtask}%
    }{%
        \setcounter{subsubtask}{#1}%
    }%
    \if@tasknumbers%
        \ifstrempty{#2}{%
            \subsection{\arabic{task}.\arabic{subtask}.\arabic{subsubtask}}
        }{%
            \subsubsection{\arabic{task}.\arabic{subtask}.\arabic{subsubtask}. #2}%
        }%
    \else%
        \subsubsection{#2}%
    \fi%
}

%%%%%%%%% Page Setup %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%\fi


\setlength{\headheight}{50pt}
\pagestyle{scrheadings}
\clearpairofpagestyles

\ifhomework@shortheader
    \ihead{\textbf{\@shortcourse}\\Aufgabenblatt \@sheetno}
    \ohead{\textbf{\@shortuniversity}\\\@date}
\else
    \ihead{\textbf{\@course}\\Aufgabenblatt \@sheetno}
    \ohead{\textbf{\@university}\\\@date}
\fi

\setkomafont{pagefoot}{\textnormal}
\cfoot[Seite \pagemark{} von \pageref{LastPage}]{Seite \pagemark{} von \pageref{LastPage}}

\newpagestyle{firstpage}[]{%
    \ihead{h}
    \cfoot{\pagemark{}}
}

%%%%%%%%%%% Layout %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%\fi


\setlength{\parindent}{0em}
\setlength{\parskip}{5pt}

\setkomafont{section}{\normalfont\bfseries\Large}
\setkomafont{subsection}{\normalfont\bfseries\large}
\RedeclareSectionCommands[afterskip=.25\baselineskip]{section}
\RedeclareSectionCommands[
    beforeskip=0.5\baselineskip,
    afterskip=.125\baselineskip]{subsection}

\renewcommand*{\maketitle}{%
    \thispagestyle{plain}%

    \newif\ifextended
    \extendedtrue

    \def\homework@university{\@university}
    \def\homework@course{\@course}
    \ifhomework@shorttitle
        \def\homework@university{\@shortuniversity}
        \def\homework@course{\@shortcourse}
    \else
        \def\homework@university{\@university}
        \def\homework@course{\@course}
    \fi

    %    \end{macrocode}
    % First we define the different layout parts. The title has six \emph{slots}
    % each of which can hold a piece of text:
    % \begin{description}
    %   \item[Top Left] On the top left the course name ist printed in a bold
    %                   font.
    %   \item[Top Right] On the top right the name of the university is printed
    %                    in a bold font.
    %   \item[Meta Left] Below the course name a piece of information is
    %                    printed. The contents of this slot depend on the
    %                    specified information and can be either the group
    %                    number or the teacher.
    %   \item[Meta Right] Similar to meta left but below the university name.
    %                     This slot typically holds the teacher or the date.
    %   \item[Title Left] Above the actual title there is a row of secondary
    %                     titles. On the left of this row the sheet number is
    %                     printed.
    %   \item[Title Right] Similarly to title left this slot may contain the
    %                      date.
    % \end{description}
    % Below the header the actual document title and the author(s) are printed.
    % If no |\title| is specified the sheet number becomes the title (|\title|
    % however remains empfy).
    %    \begin{macrocode}
    \ifdefempty{\@title}{%
        \ifdefempty{\@group}{%
            \def\metaleft{\@teacher}%
            \def\metaright{\@date}%
            \def\titleright{}%
            \extendedfalse
        }{%
            \def\metaleft{Übungsgruppe \@group}%
            \def\metaright{\@teacher}%
            \def\titleright{\@date}%
        }%
        \def\titleleft{}%
        \def\title{Aufgabenblatt \@sheetno}%
    }{%
        \ifdefempty{\@group}{%
            \def\metaleft{}%
            \def\metaright{\@teacher}%
        }{%
            \def\metaleft{Übungsgruppe \@group}%
            \def\metaright{\@teacher}%
        }%
        \def\titleleft{Aufgabenblatt \@sheetno}%
        \def\titleright{\@date}%
        \def\title{\@title}%
    }%

    %    \end{macrocode}
    % The following code prints the title with the information specified above.
    % Depending on the value of |\ifextended| the title will be longer or
    % shorter.
    %
    % Each field (heading, secondary title, title, author) is put in a separate
    % group to avoid interference.
    %    \begin{macrocode}

    \begingroup% Heading Group
        \usekomafont{pageheadfoot}%
        \usekomafont{pagehead}%
        \setlength{\parskip}{0pt}%

        \textbf{\homework@course} \hfill \textbf{\homework@university}\par%

        \begin{minipage}[t]{\widthof{\metaleft}}%
            \metaleft%
        \end{minipage}%
        \hfill%
        \begin{minipage}[t]{\widthof{\metaright}}%
            \raggedleft%
            \metaright%
        \end{minipage}%
    \endgroup%

    \ifextended%
        \vspace{\baselineskip}%
    \fi%

    \begingroup% Secondary Title Group
        \Large\bfseries\titleleft\hfill\titleright%
    \endgroup%

    \begingroup% Title Group
        \begin{center}%
            \Huge\textsc{\title}%
        \end{center}%
    \endgroup%

    \begingroup% Author Group
        \setlength{\baselineskip}{2.75\baselineskip}%
        \vspace{-\baselineskip}%
        \begin{center}%
            \begin{tabular}[t]{c}%
                \@author%
            \end{tabular}%
        \end{center}%
    \endgroup%
}

\endinput
%%
%% End of file `homework.cls'.
