# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.

!if $status notwordof waiting finish
 !read var.prep
!endif

insplot_set=zeroaxis; parametric;\
	tmargin 1;rmargin 1;lmargin 3; bmargin 2
fx=t
fy=$f
!read definitions
!for i=1 to $present
 j=!item $i of $shuf
 !if $j=$ask
  askshuf=$i
 !endif
 v$i =$j
 x$i =!item $j of $xlist
 y$i =!item $j of $ylist
 n$i =!item $j of $flist
 x$i=!mathsubst f=$f in $(x$i)
 y$i=!mathsubst f=$f in $(y$i)
 n$i=!htmlmath $(n$i)
 n$i=!mathsubst f=$m_f in $(n$i)
!next i

askname=!item $ask of $flist
askname=!htmlmath $askname
askname=!mathsubst f=$m_f in $askname

!ifval $repeat=0
 !for i=1 to $present
  !if $(t$i)>0
   nt$i=!item $(t$i) of $flist
   nt$i=!htmlmath $(nt$i)
   nt$i=!mathsubst f=$m_f in $(nt$i)
  !endif
 !next i
!endif

!if $cmd=reply and $status=waiting
 replycnt=$[$replycnt+1]
 status=done
 !ifval $repeat>1 and (($click>0 and $click=$askshuf) or ($click=0 and $askshuf=$empty))
  cumul=$[$cumul+1]
 !endif
 !if $replycnt>=$repeat
  status=finish
  !ifval $repeat=0
   sc=0
   !for i=1 to $present
    !if $(t$i)=$(v$i)
     sc=$[$sc+2]
    !else
     !if $(t$i)>0
      sc=$[$sc-1]
     !endif
    !endif
   !next i
   !if $sc<0
    module_score=0
   !else
    module_score=$[rint(50*$sc/$present)/10]
   !endif
  !endif
  !if $repeat=1
   !ifval ($click>0 and $click=$askshuf) or ($click=0 and $askshuf=$empty)
    module_score=10
   !else
    module_score=0
   !endif
  !endif
  !if $repeat>1
   module_score=$[rint(100*($cumul*$list-$repeat)/($list-1)/$repeat)/10]
   !if $module_score<0
    module_score=0
   !endif
  !endif
  wims_module_log=score $module_score/10
 !endif
!endif

!if $status=wait
 status=waiting
!endif

