%---------------------------------------------------------------------------
% Time-stamp: <2020-12-18 15:41:19 sutner> 
%
% :File:    ksexam.cls
% :Purpose: exam wrapper class for article
% 
%           prints a score table on first page
%           prints total points 
%
%           adjust the CHANGE items for each class
%
%           todo:
%           - score table should split into two columns
%            for more than 8 or so questions
%           - lacks solution mechanism -- import from ks-assign ???  
%           
%---------------------------------------------------------------------------

%---------------------------------------------------------------------------
% identification

\def\fileversion{0.99}
\def\filedate{2020/03/18} 

\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{ksexam}

%---------------------------------------------------------------------------
% initial code

% \listfiles

% \def\coursename{CDM}          % CHANGE in document
% \def\examtype{Final}          % CHANGE in document

%---------------------------------------------------------------------------
% options declarations

\DeclareOption{draft}{\setlength\overfullrule{5pt}}
\DeclareOption{final}{\setlength\overfullrule{0pt}}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

%---------------------------------------------------------------------------
% options execution

\ExecuteOptions{final}

\InputIfFileExists{ksexam.cfg}{}{}      % not much used ...

\ProcessOptions

%---------------------------------------------------------------------------
% package loading

\LoadClass{article}

\RequirePackage{fancyhdr}
\RequirePackage{amsmath}
\RequirePackage{amsfonts}
\RequirePackage{amssymb}
\RequirePackage{multido}
\RequirePackage{array}
\RequirePackage{url}
\RequirePackage{fancyvrb}
\RequirePackage{geometry}
\geometry{margin=18mm,top=25mm}
\RequirePackage{ifthen}
\RequirePackage{graphicx}

%---------------------------------------------------------------------------
% main code
% ---------------------------------------------------------------------------

% ---------------------------------------------------------------------------
% spacing

\parindent=0pt
\parskip=1ex

\def\arrstr{1.5}              % probably should be local


% ---------------------------------------------------------------------------
% pagestyle

\lhead[{\bfseries \coursename\quad\examtype}]%
{{\bfseries \coursename\quad\examtype}}
\chead[]{}
\rhead[{\bfseries\thepage\ of \pageref{exam-last-page}}]%
{{\bfseries\thepage\ of \pageref{exam-last-page}}}
\lfoot[]{}
\cfoot[]{}
\rfoot[]{}
\headheight=15pt

\pagestyle{fancy}

%---------------------------------------------------------------------------
% graphics path

\graphicspath{%
{/home/sutner/class/CDM/Pictures/}%
{/home/sutner/projects/Discrete/pics/auto/}%
{/home/sutner/projects/Discrete/pics/ca/}%
{/home/sutner/projects/Discrete/pics/disc/}%
{/home/sutner/projects/Discrete/pics/people/}%
{/home/sutner/projects/Discrete/pics/stuff/}%
{/home/sutner/Docs/Images/}%
{/home/sutner/projects/Discrete/standalone/}%
{./}}


%---------------------------------------------------------------------------
%
% for captital alphabetic lists
%

\newenvironment{Enumerate}%
{\renewcommand{\theenumi}{\Alph{enumi}}\begin{enumerate}}%
{\end{enumerate}}


%---------------------------------------------------------------------------
% computing scores


\newcounter{questnum}

\AtBeginDocument{%
\newwrite\outfile
\openout\outfile=\jobname.xxx
\ifeof\outfile\typeout{No file \jobname.xxx}
\else\fi}



\newcommand{\titledquestion}[2]{
\stepcounter{questnum}\bigskip\bigskip
{\large{\bf Problem \thequestnum:}\hspace{1ex}{\bfseries {#1}}}%
{\normalsize\hspace{1ex}({#2} pts.) \vspace{1ex} \relax}
\immediate\write\outfile{#2}}

\newcommand{\Background}[0]{
\medskip
{\bf Background}}

\newcommand{\Task}[0]{
\medskip
{\bf Task}}

\newcommand{\Comment}[0]{
\medskip
{\bf Comment}}

%---------------------------------------------------------------------------

\def\score{}
\newcounter{tscore}
\newcounter{cscore}
\newcounter{probnum}

\newboolean{moreinp}
\setboolean{moreinp}{true}

\newcommand{\ReadNextProblem}[1]{%
\ifthenelse{\boolean{moreinp}}%
{\read#1 to \score\ifeof#1 \setboolean{moreinp}{false}\else\fi}{}}

%
% read from #1, write to #2
%
\newlength{\stwidth}
\setlength{\stwidth}{15mm}
\newcounter{pcnt}
\setcounter{pcnt}{0}
\def\oneline#1{\stepcounter{pcnt}\thepcnt & {#1} & \makebox[\stwidth]{}\\ \hline}

\newcommand{\ReadProblems}[2]{%
\immediate\write#2{\string\begin{center}}
\immediate\write#2{\string\renewcommand{\string\arraystretch}{\string\arrstr}}
\immediate\write#2{\string\begin{tabular}{|r|r|c|}}
\immediate\write#2{\string\hline}
\whiledo{\boolean{moreinp}}{%
  \ReadNextProblem{#1}
  \ifthenelse{\boolean{moreinp}}{%
    \addtocounter{probnum}{1}
    \setcounter{cscore}{\score}
    \addtocounter{tscore}{\score}
\immediate\write#2{\string\oneline{\thecscore}}}{\relax}}
\immediate\write#2{\string\hline}
\immediate\write#2{Total & \thetscore & \string\cr}
\immediate\write#2{\string\hline}
\immediate\write#2{\string\end{tabular}}
\immediate\write#2{\string\end{center}}
}


%
% printing the score table on front page
%
\newcommand\PrintScoreTable{%
\newread\infile 
\immediate\openin\infile=\jobname.yyy
\ifeof\infile
\typeout{No file \jobname.yyy} \fbox{Score Table} \else\input\jobname.yyy\fi
\immediate\closein\infile}


%
% handle last page and write aux files
%

\AtEndDocument{
\mbox{ }
\label{exam-last-page}%
\immediate\closeout\outfile
\newread\infile 
\immediate\openin\infile=\jobname.xxx
\immediate\openout\outfile=\jobname.yyy
\ReadProblems{\infile}{\outfile}
\immediate\closein\infile
\immediate\closeout\outfile
\typeout{}
\typeout{****  Questions: \thequestnum}
\typeout{****  Score: \thetscore}
\typeout{}}



%---------------------------------------------------------------------------
\endinput
%---------------------------------------------------------------------------
% eof
