Subversion Repositories wimsdev

Rev

Rev 12445 | Rev 12508 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* Routines treating user scores. */
  19. #include "wims.h"
  20.  
  21. double oldfactor=0.85;     /* quality factor, should remain stable. */
  22.  
  23. enum {sr_require, sr_weight, sr_score, sr_mean, sr_remain,
  24.   sr_last, sr_try, sr_best, sr_level, sr_new, sr_seedlast, sr_seedscorelast,
  25.   sr_seedscores, sr_seedlastcnt};
  26. char scorebuf[MAX_CLASSEXOS*sizeof(scoreresult)+32];
  27. struct scoreresult *rscore;
  28. int scorecnt;
  29. double scoresum[MAX_SHEETS];
  30. int sheetstart[MAX_SHEETS], shexocnt[MAX_SHEETS];
  31. int examstart, examcnt;
  32. char rscore_class[MAX_CLASSLEN+1];
  33. char rscore_user[MAX_NAMELEN+1];
  34.  
  35. int totsheets=0;
  36. int score_ispublic=0;
  37. int score_status=-1;          /* save of score status */
  38. int score_statussheet=-1;
  39. int score_statusisexam=-1;
  40. double scorerecfactor=0.9;
  41.  
  42. /* gather user score, core routine.
  43.   rscore data is created (table of structures).
  44.   according to the value of wims_sheet or wims_exo
  45. */
  46. int getscoreuser(char *classe, char *user)
  47. {
  48.   int i, j, osh, sh;
  49.   double s;
  50.   char *nowuser, *nowsheet, *nowexo, *nowscore;
  51.   char *pp;
  52.   if(user==NULL || *user==0) {
  53.     user=getvar("wims_user");
  54.     if(user==NULL || *user==0) return -1;
  55.   }
  56.   if(strcmp(classe,rscore_class)==0 && strcmp(user,rscore_user)==0) return 0;
  57.   nowsheet=nowexo=nowscore="";
  58.   nowuser=getvar("wims_user"); if(nowuser!=NULL && strcmp(user,nowuser)==0) {
  59.     nowscore=getvar("module_score");
  60.     if(nowscore!=NULL && *nowscore!=0) {
  61.       nowsheet=getvar("wims_sheet");
  62.       if(nowsheet==NULL) nowsheet="";
  63.       nowexo=getvar("wims_exo");
  64.       if(nowexo==NULL) nowexo="";
  65.     }
  66.     else nowscore="";
  67.   }
  68.   snprintf(scorebuf+sizeof(int),sizeof(scorebuf)-sizeof(int),
  69.        "-c%s -u%s getscore %s %s %s",
  70.        classe,user,nowsheet,nowexo,nowscore);
  71.   i=kerneld(scorebuf,sizeof(scorebuf));
  72.   if(i<0)  internal_error("getscoreuser(): daemon failure.");
  73.   if(memcmp(scorebuf+sizeof(int),"OK",2)!=0) {
  74.     if(memcmp(scorebuf+sizeof(int),"ERROR",5)==0) {
  75.       module_error(find_word_start(scorebuf+sizeof(int)+8));
  76.     }
  77.     else internal_error("getscoreuser(): communication error with wimslogd.");
  78.   }
  79. /* 3 is here the length of "OK " */
  80.   pp=scorebuf+sizeof(int)+3; rscore=(struct scoreresult *) pp;
  81.   scorecnt=(i-sizeof(int)-3)/sizeof(scoreresult);
  82.   if(scorecnt>MAX_CLASSEXOS) module_error("too_many_exercises");
  83.   s=0; for(i=osh=0;i<scorecnt;i++) {
  84.     sh=(rscore[i].num>>8)+1;
  85.     if(sh<1 || sh>MAX_SHEETS) break;
  86.     if(osh>0 && osh<sh) {scoresum[osh-1]=s; s=0;}
  87.     for(;osh<sh && osh<MAX_SHEETS;osh++) sheetstart[osh]=i;
  88.     s+=rscore[i].require*rscore[i].weight;
  89.   }
  90.   if(osh>0) scoresum[osh-1]=s;
  91.   totsheets=osh;
  92.   for(j=0;j<totsheets-1;j++) shexocnt[j]=sheetstart[j+1]-sheetstart[j];
  93.   shexocnt[totsheets-1]=i-sheetstart[totsheets-1];
  94.   examstart=i; examcnt=scorecnt-examstart;
  95.   mystrncpy(rscore_class,classe,sizeof(rscore_class));
  96.   mystrncpy(rscore_user,user,sizeof(rscore_user));
  97.   return 0;
  98. }
  99. /* work : exo */
  100. char *scorepname[]={
  101.   "class","user","sheet","work","exam"
  102. };
  103. #define scorepname_no (sizeof(scorepname)/sizeof(scorepname[0]))
  104. char score_class[MAX_CLASSLEN+1];
  105. int score_sheet,score_exo,score_isexam;
  106. char score_user[256];
  107.  
  108. /* Uniformed treatment of score command parameters
  109.  * format: class=? user=? sheet=? work=?
  110.  * all are optional.
  111.  */
  112. void _scoreparm(char *p)
  113. {
  114.     int i;
  115.     char *pn, *pe, *pd, *pf;
  116.     char sav;
  117.  
  118.     score_sheet=score_exo=score_isexam=score_ispublic=0; *score_class=0;
  119.     score_user[0]=0;
  120.     for(i=0;i<scorepname_no;i++) {
  121.      pf=p;
  122.      ahead:
  123.      pn=strstr(pf,scorepname[i]); pf=pn+1;
  124.      if(pn==NULL) continue;
  125.      if(pn>p && !isspace(*(pn-1))) goto ahead;
  126.      pe=find_word_start(pn+strlen(scorepname[i]));
  127.      if(*pe!='=') goto ahead;
  128.      pd=find_word_start(pe+1);
  129.      pf=find_word_end(pd);
  130.      if(pf<=pd) continue;
  131.      sav=*pf; *pf=0;
  132.      switch(i) {
  133.          case 0: /* class */
  134.            mystrncpy(score_class,pd,sizeof(score_class)); break;
  135.          case 1: /* user */
  136.            mystrncpy(score_user,pd,sizeof(score_user)); break;
  137.          case 2: { /* sheet */
  138.           if(*pd=='P') {pd++; score_ispublic=1;}
  139.           score_sheet=atoi(pd);
  140.           break;
  141.          }
  142.          case 3: /* work=exo */
  143.            score_exo=atoi(pd); break;
  144.          case 4: /* exam */
  145.            score_isexam=1; break;
  146.      }
  147.      *pf=sav; ovlstrcpy(pn, pf);
  148.     }
  149.     *p=0;
  150.   /* the commands are OK from a non trusted module if the user and the
  151.   class are not precised, so it can be only the user in his class */
  152.     if((*score_class!=0 || score_user[0]!=0) && !trusted_module()) {
  153.      module_error("not_trusted"); return;
  154.     }
  155.     if(*score_class==0) {
  156.      char *classe;
  157.      classe=getvar("wims_class");
  158.      if(classe==NULL || *classe==0) return;
  159.      else mystrncpy(score_class,classe,sizeof(score_class));
  160.     }
  161.     if(score_user[0]==0) {
  162.      char *user;
  163.      user=getvar("wims_user");
  164.      if(user!=NULL) mystrncpy(score_user,user,sizeof(score_user));
  165.     }
  166. }
  167.  
  168. /* gather score: relatif to some of the
  169.  *  user class work sheet exam
  170.  *  dtype can be require weight score mean remain best last level try
  171.  */
  172.  
  173. void _getscore(char *p,int dtype)
  174. {
  175.   int i,sh,ex,osh;
  176.   float d;
  177.   char dc[SEEDSIZE];
  178.   char ds[SEEDSCORES];
  179.   char *p1;
  180.  
  181.   _scoreparm(p);
  182.   if(*score_class==0 || *score_user==0) return;
  183.   if(getscoreuser(score_class,score_user)<0) return;
  184. /*  2^8 no_sheet + no_exo_in_the_sheet so limit no_exo to 2^8 */
  185.   for(i=osh=0,p1=p;i<scorecnt && p1-p<MAX_LINELEN-32;i++) {
  186.     sh=(rscore[i].num>>8)+1; if(sh<1 || sh>MAX_SHEETS) break;
  187.     if(score_sheet!=0) {
  188.       if(sh<score_sheet) continue;
  189.       if(sh>score_sheet || sh>MAX_SHEETS) break;
  190.     }
  191.     ex=((rscore[i].num)&255)+1;
  192.     if(score_exo!=0 && ex!=score_exo) continue;
  193.     if(osh!=0 && sh!=osh) *p1++='\n';
  194.     switch(dtype) {
  195.       case sr_require: {d=rscore[i].require; break;}
  196.       case sr_weight: {d=rscore[i].weight; break;}
  197.       case sr_score: {d=rscore[i].score; break;}
  198.       case sr_mean: {d=rscore[i].mean; break;}
  199.       case sr_remain: {d=rscore[i].require-rscore[i].score; break;}
  200.       case sr_last: {d=rscore[i].last; break;}
  201.       case sr_try: {d=rscore[i].try; break;}
  202.       case sr_new: {d=rscore[i].new; break;}
  203.       case sr_best: {d=rscore[i].best; break;}
  204.       case sr_level: {d=rscore[i].level; break;}
  205.       case sr_seedscorelast: {d=rscore[i].seedscorelast; break;}
  206.       case sr_seedlastcnt: {d=rscore[i].seedlastcnt; break;}
  207.       case sr_seedlast: { mystrncpy(dc,rscore[i].seedlast,SEEDSIZE);
  208.         break;
  209.       }
  210.       case sr_seedscores: {
  211.         mystrncpy(ds,rscore[i].seedscores,SEEDSCORES); break;
  212.       }
  213.       default: {d=0; break;}
  214.     }
  215.     switch(dtype) {
  216.       case sr_seedlast:
  217.         { snprintf(p1,SEEDSIZE, "%s", dc); p1+=strlen(p1); break; }
  218.        case sr_seedscores:
  219.         { snprintf(p1,SEEDSCORES, "%s", ds); p1+=strlen(p1); break; }
  220.       default:
  221.         { p1=moneyprint(p1,d); }
  222.     }
  223.     *p1++=' ';
  224.     osh=sh;
  225.   }
  226.   *p1++='\n'; *p1=0;
  227. }
  228.  
  229. /* gather user score. */
  230. void calc_getscore(char *p)
  231. {
  232.   _getscore(p,sr_score);
  233. }
  234.  
  235. /* gather user score average. */
  236. void calc_getscoremean(char *p)
  237. {
  238.   _getscore(p,sr_mean);
  239. }
  240.  
  241. /* gather remaining of score to get for user. */
  242. void calc_getscoreremain(char *p)
  243. {
  244.   _getscore(p,sr_remain);
  245. }
  246.  
  247. /* Require score table. */
  248. void calc_getscorerequire(char *p)
  249. {
  250.   _getscore(p,sr_require);
  251. }
  252.  
  253. /* Score weight table. */
  254. void calc_getscoreweight(char *p)
  255. {
  256.   _getscore(p,sr_weight);
  257. }
  258. /* user last score*/
  259. void calc_getscorelast(char *p)
  260. {
  261.   _getscore(p,sr_last);
  262. }
  263. /* gather user score try numbers with score activated. */
  264. void calc_getscoretry(char *p)
  265. {
  266.   _getscore(p,sr_try);
  267. }
  268. /* gather all user try numbers (terminated or not, but with notation activated)*/
  269. void calc_getscorealltries(char *p)
  270. {
  271.   _getscore(p,sr_new);
  272. }
  273.  
  274. /* if the required points are 10* N, gather N user best scores */
  275. void calc_getscorebest(char *p)
  276. {
  277.   _getscore(p,sr_best);
  278. }
  279.  
  280. /* gather user score average. */
  281. void calc_getscorelevel(char *p)
  282. {
  283.   _getscore(p,sr_level);
  284. }
  285.  
  286. /* percentage of work done for each sheet:
  287.  * score mean best level
  288.  * as follows
  289.  * score=100*cumulative_points/required_points) (< 100)
  290.  * mean=quality (<10)
  291.  * best=10*(required/10 best_scores)/required_points (< 100)
  292.  * level=minimum of the required/10 best_scores (< 10)
  293.  */
  294.  
  295.  void calc_getscorepercent(char *p)
  296. {
  297.   int i,j,jend;
  298.   double tot, mean, totb, totl, totw, d;
  299.   char *p1;
  300.  
  301.   _scoreparm(p);
  302.   if(*score_class==0 || *score_user==0) return;
  303.   if(getscoreuser(score_class,score_user)<0) return;
  304.   for(p1=p,i=0;i<totsheets && p1-p<MAX_LINELEN-32;i++) {
  305.     if(scoresum[i]==0) {
  306.       ovlstrcpy(p1,"0 0 0 0\n"); p1+=strlen(p1); continue;
  307.     }
  308.     if(score_sheet!=0 && i!=score_sheet-1) continue;
  309.     if(scoresum[i]<=0) *p1++='\n';
  310.     tot=mean=totb=totl=totw=0; jend=sheetstart[i]+shexocnt[i];
  311.     for(j=sheetstart[i];j<jend;j++) {
  312. /* if mean<1 then ignore score.
  313.  * if mean<2 then half score.
  314.  */
  315.       if(rscore[j].mean>=1) {
  316.         double dt=rscore[j].score;
  317.         float db=rscore[j].best;
  318.         float dl=rscore[j].level;
  319.         if(rscore[j].mean<2) {dt=dt/2; db=db/2; dl=dl/2;}
  320.         d=dt*rscore[j].weight;
  321. /* quality */
  322.         mean+=rscore[j].mean*d;
  323. /* cumulative score */
  324.         tot+=d;
  325. /* best */
  326.         totb+=db*rscore[j].weight;
  327. /* level */
  328.         totl+=dl*rscore[j].weight;
  329.         totw+=rscore[j].weight;
  330.       }
  331.     }
  332.     if(tot>0) {d=mean/tot;} else d=0;
  333. /* cumulative score */
  334.     p1=moneyprint(p1,rint(100*tot/scoresum[i])); *p1++=' ';
  335. /* quality */
  336.     p1=moneyprint(p1,d); *p1++=' ';
  337. /* best */
  338.     p1=moneyprint(p1,rint(100*totb/scoresum[i])); *p1++=' ';
  339. /* level */
  340.     p1=moneyprint(p1,rint(10*totl/totw));
  341.  
  342.    *p1++='\n';
  343.   }
  344.   *p1=0;
  345. }
  346. /* [seed1,score1;seed2,score2;...] at most MAX_SCORESEED */
  347. void calc_getseedscores(char *p)
  348. {
  349.   _getscore(p,sr_seedscores);
  350. }
  351. /* last seed (0 if there is no seed)*/
  352. void calc_getseedlast(char *p)
  353. {
  354.   _getscore(p,sr_seedlast);
  355. }
  356. /* last score (-1 if does not exist) */
  357. void calc_getseedscorelast(char *p)
  358. {
  359.   _getscore(p,sr_seedscorelast);
  360. }
  361.  
  362. /* number of occurences of the last seed */
  363. void calc_getseedlastcnt(char *p)
  364. {
  365.   _getscore(p,sr_seedlastcnt);
  366. }
  367.  
  368. /* Returns the status of a sheet, or -1 if error */
  369. int getsheetstatus(char *classe, int sheet)
  370. {
  371.   char *p, *st, buf[MAX_LINELEN+1], namebuf[MAX_FNAME+1];
  372.   int i;
  373.  
  374.   if(isexam || score_isexam) st="exam"; else st="sheet";
  375.   mkfname(namebuf,"%s/%s/%ss/.%ss",class_base,classe,st,st);
  376.   direct_datafile=1;datafile_fnd_record(namebuf,sheet,buf);direct_datafile=0;
  377.   p=find_word_start(buf); if(*p==0) return -1;
  378.   i=*p-'0'; if(i>5 || i<0) i=-1;
  379.   if((isexam || score_isexam) && i==0) {
  380.     p=getvar("wims_user"); if(p!=NULL && strcmp(p,"supervisor")==0) i=1;
  381.   }
  382.   return i;
  383. }
  384.  
  385. /* return 1 if a word of bf2 is a substring of host.
  386.  * Content of bf2 is destroyed.
  387.  */
  388. int _subword(char bf2[],char *ftbuf)
  389. {
  390.   char *p1, *p2;
  391.   for(p1=strchr(bf2,'\\'); p1!=NULL; p1=strchr(p1+1,'\\')) {
  392.     char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1], fbuf[MAX_FNAME+1];
  393.     char *classp, *classp2, *userp, *scp;
  394.     classp=getvar("wims_class"); userp=getvar("wims_user");
  395.     if(classp==NULL || userp==NULL || *classp==0 || *userp==0) break;
  396.     scp=getvar("wims_superclass");
  397.     if(scp!=NULL && *scp!=0) classp2=scp; else classp2=classp;
  398.     if(p1>bf2 && !isspace(*(p1-1))) continue;
  399.     if(!isalnum(*(p1+1))) continue;
  400.     p2=find_word_end(p1); if(p2>=p1+MAX_NAMELEN) continue;
  401.     memmove(buf2, p1+1, p2-p1-1); buf2[p2-p1-1]=0;
  402.     /* get value of technical variable */
  403.     snprintf(buf,sizeof(buf),"user_techvar_%s",buf2);
  404.     if(strcmp(userp,"supervisor")==0)
  405.       mkfname(fbuf,"%s/%s/supervisor",class_base,classp);
  406.     else
  407.       mkfname(fbuf,"%s/%s/.users/%s",class_base,classp2,userp);
  408.     getdef(fbuf,buf,buf2);
  409.     if(buf2[0]==0) ovlstrcpy(buf2,"EMPTY"); /* in case of no value defined for the user*/
  410.     /* get time restriction for this value */
  411.     snprintf(buf,sizeof(buf),"techvar_%s",buf2);
  412.     /*mkfname(fbuf,"%s/%s/.E%s",class_base,classp,sheet);*/
  413. /* read time restriction corresponding to the value of
  414.   technical variable in the file of the sheet or exam
  415.  */
  416.     getdef(ftbuf,buf,buf2);
  417.     if(buf2[0]==0) ovlstrcpy(buf2,"none");
  418.     /*string_modify(bf2,p1,p2,buf2);*/
  419.     bf2=buf2;
  420.     p1+=strlen(buf2);
  421.   }
  422.   if((isexam || score_isexam) && bf2[0]=='#') return 1;
  423.   if(wordchr(bf2,"none")!=NULL) return 0;
  424.   if(wordchr(bf2,"all")!=NULL) return 1;
  425.   p1=find_word_start(bf2); if(*p1==0) return 1;
  426.   return checkhostt(p1);
  427. }
  428.  
  429. /* Returns 1 if score registration is open, 0 otherwise. */
  430. int _getscorestatus(char *classe, int sheet)
  431. {
  432.   char nbuf[MAX_LINELEN+1], gbuf[MAX_LINELEN+1];
  433.   char ftbuf[MAX_FNAME+1]="";
  434.   char *es;
  435.  
  436.   if(classe==NULL || *classe==0 || sheet<=0) return 1;
  437.   if(getsheetstatus(classe,sheet)!=1) return 0;
  438.   if(*remote_addr==0) return 0;
  439.   if(isexam || score_isexam) {     /* exam simulation */
  440.     accessfile(nbuf,"r","%s/%s/.E%d",class_base,classe,sheet);
  441.     if(nbuf[0]=='#') return 1;
  442.   }
  443. /* Global restriction data */
  444.   accessfile(nbuf,"r","%s/%s/.security",class_base,classe);
  445.   if(nbuf[0]) {
  446.     _getdef(nbuf,"allow",gbuf);
  447.     if(*find_word_start(gbuf)!=0 && _subword(gbuf,ftbuf)==0)
  448.       return 0;
  449.     _getdef(nbuf,"except",gbuf);
  450.     if(*find_word_start(gbuf)!=0 && _subword(gbuf,ftbuf)==1)
  451.       return 0;
  452.   }
  453.  
  454. /* Sheet restriction data */
  455.   if(isexam || score_isexam) es="E"; else es="";
  456.   accessfile(nbuf,"r","%s/%s/.%s%d",class_base,classe,es,sheet);
  457.   if(*find_word_start(nbuf)==0) return 1;
  458. /* preparing score restriction file name :*/
  459.   mkfname(ftbuf,"%s/%s/.%s%d",class_base,classe,es,sheet);
  460.   return _subword(nbuf,ftbuf);
  461. }
  462.  
  463. /* Returns 1 if score registration is open, 0 otherwise. */
  464. int getscorestatus(char *classe, int sheet)
  465. {
  466.   if(score_status<0 || score_statussheet!=sheet
  467.      || score_statusisexam!=isexam) {
  468.     score_statussheet=sheet; score_statusisexam=isexam;
  469.     score_status=_getscorestatus(classe,sheet); score_isexam=0;
  470.     if(score_status==1 && (cmd_type==cmd_new || cmd_type==cmd_renew
  471.                || isexam)) {
  472.       char *p;
  473.       p=getvar("wims_scorereg");
  474.       if(p==NULL || strcmp(p,"suspend")!=0)
  475.          setvar("wims_scoring","pending");
  476.       else setvar("wims_scoring","");
  477.     }
  478.   }
  479.   if(isexam && score_status==0) {
  480.     char *p;
  481.     p=getvar("wims_user"); if(p==NULL || strcmp(p,"supervisor")!=0)
  482.                  user_error("exam_closed");
  483.   }
  484.   return score_status;
  485. }
  486.  
  487. /* Whether score registering is open */
  488. void calc_getscorestatus(char *p)
  489. {
  490.   _scoreparm(p);
  491.   if(*score_class==0 || score_sheet==0 || *score_user==0) {
  492.     *p=0; return;
  493.   }
  494.   if(getscorestatus(score_class, score_sheet))
  495.     ovlstrcpy(p,"yes");
  496.   else ovlstrcpy(p,"no");
  497. }
  498. /* whether there are too much tries for the exo is too much */
  499.  
  500. void calc_getscoremaxexotry(char *p)
  501. {
  502.   _scoreparm(p);
  503.   if(*score_class==0 || *score_user==0 || score_sheet==0 || score_exo==0) return;
  504.   if(gettrycheck(score_class, score_user, score_sheet, score_exo)==1)
  505.     ovlstrcpy(p,"yes");
  506.   else ovlstrcpy(p,"no");
  507. }
  508.  
  509. double exam_scoredata[MAX_EXOS];
  510.  
  511. /* get current exam score */
  512. void exam_currscore(int esh)
  513. {
  514.   char *p, *bf, pb[MAX_FNAME+1];
  515.   char *s, *p1, *p2, *e1, *e2;
  516.   int i;
  517.   for(i=0;i<MAX_EXOS;i++) exam_scoredata[i]=-1000;
  518. /* session_prefix is not yet defined here */
  519.   s=getvar("wims_session"); if(s==NULL || *s==0) return;
  520.   mystrncpy(pb,s,sizeof(pb));
  521.   p=strchr(pb,'_'); if(p!=NULL) *p=0;
  522.   bf=readfile(mkfname(NULL,"%s/%s/examscore.%d",session_dir,pb,esh),NULL,WORKFILE_LIMIT);
  523.   if(bf==NULL) return;
  524.   for(p1=bf;*p1;p1=p2) {
  525.     p2=strchr(p1,'\n'); if(*p2) *p2++=0;
  526.     else p2=p1+strlen(p1);
  527.     p1=find_word_start(find_word_end(find_word_start(p1)));
  528.     e1=find_word_end(p1); if(*e1) *e1++=0;
  529.     e1=find_word_start(e1); e2=find_word_start(find_word_end(e1));
  530.     *find_word_end(e1)=0;
  531.     i=atoi(p1);
  532.     if(i>=1 && i<=MAX_EXOS &&
  533.        exam_scoredata[i-1]==-1000 && strcmp(e1,"score")==0) {
  534.       *find_word_end(e2)=0;
  535.       exam_scoredata[i-1]=atof(e2);
  536.     }
  537.   }
  538.   free(bf);
  539. }
  540.  
  541. /* Gather exam score. */
  542. void calc_examscore(char *p)
  543. {
  544.   char *p1;
  545.   int i;
  546.   char *withoutip;
  547.  
  548.   _scoreparm(p); *p=0;
  549.   withoutip=getvar("wims_examscore_withoutip");
  550.  
  551.   if(*score_class==0 || *score_user==0) return;
  552.   if(getscoreuser(score_class,score_user)<0) return;
  553.   p1=p;
  554.   if(withoutip!=NULL && strcmp(withoutip,"yes")==0) {
  555.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  556.     p1=moneyprint(p1,rscore[examstart+i].best); *p1++=' ';
  557.     }
  558.   } else {
  559.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  560.       p1=moneyprint(p1,rscore[examstart+i].score); *p1++=' ';
  561.     }
  562.   }
  563.   *p1++='\n';
  564.   for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  565.     p1=moneyprint(p1,rscore[examstart+i].require); *p1++=' ';
  566.     p1=moneyprint(p1,floor(rscore[examstart+i].mean/2)); *p1++=' ';
  567.     p1=moneyprint(p1,(int) rscore[examstart+i].mean%2); *p1++='\n';
  568.   }
  569.   *p1=0;
  570. }
  571.  
  572. /* check score dependency.
  573.  * returns 1 if requirements are met.
  574.  */
  575. int _depcheck(char *ds, struct scoreresult *rs, int ecnt)
  576. {
  577.   char *p1, *p2, *p3, *p4, *pp;
  578.   int perc, t, sum;
  579.   double tgot, ttot, tmean;
  580.  
  581.   for(p1=ds; *p1; p1=p3) {
  582.     p2=strchr(p1,':'); if(p2==NULL) break;
  583.     *p2++=0; p2=find_word_start(p2);
  584.     for(p3=p2; myisdigit(*p3); p3++);
  585.     if(p3<=p2) break;
  586.     *p3++=0; perc=atoi(p2);
  587.     if(perc<=0 || perc>100) break;
  588.     for(pp=p1; *pp; pp++) if(!myisdigit(*pp)) *pp=' ';
  589.     tgot=ttot=tmean=0; sum=0;
  590.     for(pp=find_word_start(p1); *pp; pp=find_word_start(p4)) {
  591.       p4=find_word_end(pp); if(*p4) *p4++=0;
  592.       t=atoi(pp); if(t<=0 || t>ecnt) goto lend;
  593.       t--;
  594.       ttot+=rs[t].require; tgot+=rs[t].score; tmean+=rs[t].mean;
  595.       sum++;
  596.     }
  597.     if(ttot<10) continue;
  598.     if(tgot/ttot*sqrt(tmean/(sum*10))*100<perc) {
  599.       for(pp=p1;pp<p2-1;pp++) if(!*pp) *pp=',';
  600.       *pp=0; setvar("dep_list",p1);
  601.       return 0;
  602.     }
  603.   lend: ;
  604.   }
  605.   return 1;
  606. }
  607.  
  608. int depcheck(char *sh, int exo, char *deps)
  609. {
  610.   char buf[MAX_LINELEN+1];
  611.   char *s, sbuf[64];
  612.   int i, is;
  613.  
  614.   s=getvar("wims_session");
  615.   if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return 0;
  616.   mystrncpy(sbuf,s,sizeof(sbuf));
  617.   s=strchr(sbuf,'_'); if(s) *s=0;
  618.   accessfile(buf,"r","../sessions/%s/exodep.%s",sbuf,sh);
  619.   if(buf[0]==0) {     /* no dep file found */
  620.     is=atoi(sh); if(is<=0 || is>totsheets) return 0;
  621.     s=getvar("wims_class"); if(s==NULL || *s==0) return 0;
  622.     getscoreuser(s,"");
  623.     return _depcheck(deps,rscore+sheetstart[is-1],shexocnt[is-1]);
  624.   }
  625.   for(i=1,s=strchr(buf,':'); s && i<exo; i++, s=strchr(s+1,':'));
  626.   if(s==NULL) return 0;     /* bad file or exo number */
  627.   if(myisdigit(*++s)) return 0; else return 1;
  628. }
  629.  
  630. int exam_depcheck(char *deps, int exam)
  631. {
  632.   static struct scoreresult esc[MAX_EXOS];
  633.   int i;
  634.   exam_currscore(exam);
  635.   for(i=0;i<MAX_EXOS;i++) {
  636.     esc[i].require=esc[i].mean=10;
  637.     if(exam_scoredata[i]==-1000) esc[i].score=0;
  638.     else esc[i].score=exam_scoredata[i];
  639.   }
  640.   return _depcheck(deps,esc,MAX_EXOS);
  641. }
  642.  
  643. /* public sheet gives she=0 */
  644. int gettrycheck(char *classe, char *user, int she, int exo) {
  645.   char *s;
  646.   int sh, ex, i;
  647.   if (she<=0) return 0;
  648.   if(strcmp(user,"supervisor")==0) return 0;
  649.   s=getvar("exotrymax");
  650.   if(s==NULL || *s==0) return 0;
  651.   getscoreuser(classe,user);
  652.   for(i=0;i<scorecnt;i++) {
  653.     sh=(rscore[i].num>>8)+1;
  654.     if (she!=sh) continue;
  655.     ex=((rscore[i].num)&255)+1;
  656.     if(exo!=ex) continue;
  657.     if(cmd_type==cmd_new || cmd_type==cmd_renew)
  658.       if(rscore[i].new >= atoi(s)) return 1;
  659.     if(rscore[i].new > atoi(s)) return 1;
  660.   };
  661.   return 0;
  662. }
  663.  
  664. /* return seed if the seed should be kept and NULL if not */
  665. char *getseedscore(char *classe, char *user, int she, int exo) {
  666.   char *s;
  667.   int sh, ex, i;
  668.   if (she<=0) return NULL;
  669.   if(strcmp(user,"supervisor")==0) return NULL;
  670.   s=getvar("seedrepeat");
  671.   if(s==NULL || *s==0 || atoi(s)<=1) return NULL;
  672.   getscoreuser(classe,user);
  673.   for(i=0;i<scorecnt;i++) {
  674.     sh=(rscore[i].num>>8)+1;
  675.     if (she!=sh) continue;
  676.     ex=((rscore[i].num)&255)+1;
  677.     if(exo!=ex) continue;
  678.     if(atoi(rscore[i].seedlast)==0) {
  679.       char *seed=getvar("wims_seed");
  680.       return seed;
  681.     }
  682.     if(rscore[i].seedlastcnt > MAX_SEEDSCORE) return NULL;
  683.     if(atoi(s)>0 && rscore[i].seedlastcnt >= atoi(s) && cmd_type!=cmd_next) return NULL;
  684.     if(rscore[i].seedscorelast < 10) return rscore[i].seedlast;
  685.   };
  686.   return NULL;
  687. }
  688.