Subversion Repositories wimsdev

Rev

Rev 16696 | Rev 18056 | 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, osh, sh;
  49.   char *nowuser, *nowsheet, *nowexo, *nowscore;
  50.   char *pp;
  51.   if(user==NULL || *user==0) {
  52.     user=getvar("wims_user");
  53.     if(user==NULL || *user==0) return -1;
  54.   }
  55.   if(strcmp(classe,rscore_class)==0 && strcmp(user,rscore_user)==0) return 0;
  56.   nowsheet=nowexo=nowscore="";
  57.   nowuser=getvar("wims_user");
  58.   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.   osh=-1;
  84.   for(i=totsheets=0;i<scorecnt;i++) {
  85.     sh=rscore[i].sh;
  86.     if (sh >= MAX_SHEETS) break;
  87.     while(osh<sh)
  88.       {++osh; scoresum[osh]=shexocnt[osh]=0; sheetstart[osh]=i;}
  89.     scoresum[sh]+=rscore[i].require*rscore[i].weight*rscore[i].active;
  90.     shexocnt[sh]++;
  91.   }
  92.   totsheets=osh+1;
  93.  
  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.   for(i=osh=0,p1=p;i<scorecnt && p1-p<MAX_LINELEN-32;i++) {
  185.     sh=rscore[i].sh+1; if(sh<1 || sh>MAX_SHEETS) break;
  186.     if(score_sheet!=0) {
  187.       if(sh<score_sheet) continue;
  188.       if(sh>score_sheet || sh>MAX_SHEETS) break;
  189.     }
  190.     ex=rscore[i].exo+1;
  191.     if(score_exo!=0 && ex!=score_exo) continue;
  192.     if(osh!=0 && sh!=osh) *p1++='\n';
  193.     switch(dtype) {
  194.       case sr_require: {d=rscore[i].require; break;}
  195.       case sr_weight: {d=rscore[i].weight; break;}
  196.       case sr_score: {d=rscore[i].score; break;}
  197.       case sr_mean: {d=rscore[i].mean; break;}
  198.       case sr_remain: {d=rscore[i].require-rscore[i].score; break;}
  199.       case sr_last: {d=rscore[i].last; break;}
  200.       case sr_try: {d=rscore[i].try; break;}
  201.       case sr_new: {d=rscore[i].new; break;}
  202.       case sr_best: {d=rscore[i].best; break;}
  203.       case sr_level: {d=rscore[i].level; break;}
  204.       case sr_seedscorelast: {d=rscore[i].seedscorelast; break;}
  205.       case sr_seedlastcnt: {d=rscore[i].seedlastcnt; break;}
  206.       case sr_seedlast: { mystrncpy(dc,rscore[i].seedlast,SEEDSIZE);
  207.         break;
  208.       }
  209.       case sr_seedscores: {
  210.         mystrncpy(ds,rscore[i].seedscores,SEEDSCORES); break;
  211.       }
  212.       default: {d=0; break;}
  213.     }
  214.     switch(dtype) {
  215.       case sr_seedlast:
  216.         { snprintf(p1,SEEDSIZE, "%s", dc); p1+=strlen(p1); break; }
  217.        case sr_seedscores:
  218.         { snprintf(p1,SEEDSCORES, "%s", ds); p1+=strlen(p1); break; }
  219.       default:
  220.         { p1=moneyprint(p1,d); }
  221.     }
  222.     *p1++=' ';
  223.     osh=sh;
  224.   }
  225.   *p1++='\n'; *p1=0;
  226. }
  227.  
  228. /* gather user score. */
  229. void calc_getscore(char *p)
  230. {
  231.   _getscore(p,sr_score);
  232. }
  233.  
  234. /* gather user score average. */
  235. void calc_getscoremean(char *p)
  236. {
  237.   _getscore(p,sr_mean);
  238. }
  239.  
  240. /* gather remaining of score to get for user. */
  241. void calc_getscoreremain(char *p)
  242. {
  243.   _getscore(p,sr_remain);
  244. }
  245.  
  246. /* Require score table. */
  247. void calc_getscorerequire(char *p)
  248. {
  249.   _getscore(p,sr_require);
  250. }
  251.  
  252. /* Score weight table. */
  253. void calc_getscoreweight(char *p)
  254. {
  255.   _getscore(p,sr_weight);
  256. }
  257. /* user last score */
  258. void calc_getscorelast(char *p)
  259. {
  260.   _getscore(p,sr_last);
  261. }
  262. /* gather user score try numbers with score activated. */
  263. void calc_getscoretry(char *p)
  264. {
  265.   _getscore(p,sr_try);
  266. }
  267. /* gather all user try numbers (terminated or not, but with notation activated)*/
  268. void calc_getscorealltries(char *p)
  269. {
  270.   _getscore(p,sr_new);
  271. }
  272.  
  273. /* if the required points are 10* N, gather N user best scores */
  274. void calc_getscorebest(char *p)
  275. {
  276.   _getscore(p,sr_best);
  277. }
  278.  
  279. /* gather user score average. */
  280. void calc_getscorelevel(char *p)
  281. {
  282.   _getscore(p,sr_level);
  283. }
  284.  
  285. /* percentage of work done for each sheet:
  286.  * score mean best level
  287.  * as follows
  288.  * score=100*cumulative_points/required_points (< 100)
  289.  * mean=quality (<10)
  290.  * best=10*(required/10 best_scores)/required_points (< 100)
  291.  * level=minimum of the required/10 best_scores (< 100)
  292.  */
  293.  
  294.  void calc_getscorepercent(char *p)
  295. {
  296.   int i,j,jend;
  297.   double tot, mean, totb, totl, totw, d;
  298.   char *p1;
  299.  
  300.   _scoreparm(p);
  301.   if(*score_class==0 || *score_user==0) return;
  302.   if(getscoreuser(score_class,score_user)<0) return;
  303.   for(p1=p,i=0;i<totsheets && p1-p<MAX_LINELEN-32;i++) {
  304.     if(scoresum[i]==0) {
  305.       ovlstrcpy(p1,"0 0 0 0\n"); p1+=strlen(p1); continue;
  306.     }
  307.     if(score_sheet!=0 && i!=score_sheet-1) continue;
  308.     if(scoresum[i]<=0) *p1++='\n';
  309.     tot=mean=totb=totl=totw=0; jend=sheetstart[i]+shexocnt[i];
  310.     for(j=sheetstart[i];j<jend;j++) {
  311.       /* if mean<1 then ignore score.
  312.       * if mean<2 then half score.
  313.       */
  314.       if(rscore[j].mean>=1) {
  315.         double dt=rscore[j].score;
  316.         float db=rscore[j].best;
  317.         float dl=rscore[j].level;
  318.         if(rscore[j].mean<2) {dt=dt/2; db=db/2; dl=dl/2;}
  319.         d=dt*rscore[j].weight*rscore[j].active;
  320.         /* quality; barycenter with coefficients d */
  321.         mean+=rscore[j].mean*d;
  322.         /* cumulative score */
  323.         tot+=d;
  324.         /* best */
  325.         totb+=db*rscore[j].weight*rscore[j].active;
  326.         /* level */
  327.         totl+=dl*rscore[j].weight*rscore[j].active;
  328.         totw+=rscore[j].weight*rscore[j].active;
  329.       }
  330.     }
  331.     if(tot>0) {d=mean/tot;} else d=0;
  332.     /* cumulative score */
  333.     p1=moneyprint(p1,rint(100*tot/scoresum[i])); *p1++=' ';
  334.     /* quality */
  335.     p1=moneyprint(p1,d); *p1++=' ';
  336.     /* best */
  337.     p1=moneyprint(p1,rint(100*totb/scoresum[i])); *p1++=' ';
  338.     /* level */
  339.     p1=moneyprint(p1,rint(10*totl/totw));
  340.  
  341.    *p1++='\n';
  342.   }
  343.   *p1=0;
  344. }
  345. /* [seed1,score1;seed2,score2;...] at most MAX_SCORESEED */
  346. void calc_getseedscores(char *p)
  347. {
  348.   _getscore(p,sr_seedscores);
  349. }
  350. /* last seed (0 if there is no seed)*/
  351. void calc_getseedlast(char *p)
  352. {
  353.   _getscore(p,sr_seedlast);
  354. }
  355. /* last score (-1 if does not exist) */
  356. void calc_getseedscorelast(char *p)
  357. {
  358.   _getscore(p,sr_seedscorelast);
  359. }
  360.  
  361. /* number of occurences of the last seed */
  362. void calc_getseedlastcnt(char *p)
  363. {
  364.   _getscore(p,sr_seedlastcnt);
  365. }
  366.  
  367. /* Returns the status of a sheet, or -1 if error */
  368. int getsheetstatus(char *classe, int sheet)
  369. {
  370.   char *p, *st, buf[MAX_LINELEN+1], namebuf[MAX_FNAME+1];
  371.   int i;
  372.  
  373.   if(isexam || score_isexam) st="exam"; else st="sheet";
  374.   mkfname(namebuf,"%s/%s/%ss/.%ss",class_base,classe,st,st);
  375.   direct_datafile=1;datafile_fnd_record(namebuf,sheet,buf);direct_datafile=0;
  376.   p=find_word_start(buf); if(*p==0) return -1;
  377.   i=*p-'0'; if(i>5 || i<0) i=-1;
  378.   if((isexam || score_isexam) && i==0) {
  379.     p=getvar("wims_user"); if(p!=NULL && strcmp(p,"supervisor")==0) i=1;
  380.   }
  381.   return i;
  382. }
  383.  
  384. /* return 1 if a word of bf2 is a substring of host
  385.   and if time restrictions are verified for wims_user
  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.   /* check host and time */
  427.   return checkhostt(p1);
  428. }
  429.  
  430. /* Returns 1 if score registration is open for the user
  431.  * in variable wims_user, 0 otherwise.
  432.  */
  433. int _getscorestatus(char *classe, int sheet)
  434. {
  435.   char nbuf[MAX_LINELEN+1], gbuf[MAX_LINELEN+1];
  436.   char ftbuf[MAX_FNAME+1]="";
  437.   char *es;
  438.  
  439.   if(classe==NULL || *classe==0 || sheet<=0) return 1;
  440.   if(getsheetstatus(classe,sheet)!=1) return 0;
  441.   if(*remote_addr==0) return 0;
  442.   if(isexam || score_isexam) {     /* exam simulation */
  443.     accessfile(nbuf,"r","%s/%s/.E%d",class_base,classe,sheet);
  444.     if(nbuf[0]=='#') return 1;
  445.   }
  446.   /* Global restriction data */
  447.   accessfile(nbuf,"r","%s/%s/.security",class_base,classe);
  448.   if(nbuf[0]) {
  449.     _getdef(nbuf,"allow",gbuf);
  450.     if(*find_word_start(gbuf)!=0 && _subword(gbuf,ftbuf)==0)
  451.       return 0;
  452.     _getdef(nbuf,"except",gbuf);
  453.     if(*find_word_start(gbuf)!=0 && _subword(gbuf,ftbuf)==1)
  454.       return 0;
  455.   }
  456.   /* Sheet restriction data; take in account the technical variables */
  457.   if(isexam || score_isexam) es="E"; else es="";
  458.   accessfile(nbuf,"r","%s/%s/.%s%d",class_base,classe,es,sheet);
  459.   if(*find_word_start(nbuf)==0) return 1;
  460.   /* preparing score restriction file name :*/
  461.   mkfname(ftbuf,"%s/%s/.%s%d",class_base,classe,es,sheet);
  462.   return _subword(nbuf,ftbuf);
  463. }
  464.  
  465. /* Returns 1 if score registration is open for wims_user, 0 otherwise.*/
  466. int getscorestatus(char *classe, int sheet)
  467. {
  468.   if(score_status<0 || score_statussheet!=sheet
  469.      || score_statusisexam!=isexam) {
  470.     score_statussheet=sheet; score_statusisexam=isexam;
  471.     score_status=_getscorestatus(classe,sheet); score_isexam=0;
  472.     if(score_status==1 && (cmd_type==cmd_new || cmd_type==cmd_renew
  473.                || isexam)) {
  474.       char *p;
  475.       p=getvar("wims_scorereg");
  476.       if(p==NULL || strcmp(p,"suspend")!=0)
  477.          setvar("wims_scoring","pending");
  478.       else setvar("wims_scoring","");
  479.     }
  480.   }
  481.   if(isexam && score_status==0) {
  482.     char *p;
  483.     p=getvar("wims_user");
  484.     if(p==NULL || strcmp(p,"supervisor")!=0)
  485.       user_error("exam_closed");
  486.   }
  487.   return score_status;
  488. }
  489.  
  490. /* Whether score registering is open */
  491. void calc_getscorestatus(char *p)
  492. {
  493.   _scoreparm(p);
  494.   if(*score_class==0 || score_sheet==0 || *score_user==0) {
  495.     *p=0; return;
  496.   }
  497.   if(getscorestatus(score_class, score_sheet))
  498.     ovlstrcpy(p,"yes");
  499.   else ovlstrcpy(p,"no");
  500. }
  501.  
  502. /* whether there are too much tries for the exo return yes or no */
  503. void calc_getscoremaxexotry(char *p)
  504. {
  505.   _scoreparm(p);
  506.   if(*score_class==0 || *score_user==0 || score_sheet==0 || score_exo==0) return;
  507.   if(gettrycheck(score_class, score_user, score_sheet, score_exo)==1)
  508.     ovlstrcpy(p,"yes");
  509.   else ovlstrcpy(p,"no");
  510. }
  511.  
  512. double exam_scoredata[MAX_EXAMS];
  513.  
  514. /* get current exam score */
  515. void exam_currscore(int esh)
  516. {
  517.   char *p, *bf, pb[MAX_FNAME+1];
  518.   char *s, *p1, *p2, *e1, *e2;
  519.   int i;
  520.   for(i=0;i<MAX_EXAMS;i++) exam_scoredata[i]=-1000;
  521.   /* session_prefix is not yet defined here */
  522.   s=getvar("wims_session"); if(s==NULL || *s==0) return;
  523.   mystrncpy(pb,s,sizeof(pb));
  524.   p=strchr(pb,'_'); if(p!=NULL) *p=0;
  525.   bf=readfile(mkfname(NULL,"%s/%s/examscore.%d",session_dir,pb,esh),NULL,WORKFILE_LIMIT);
  526.   if(bf==NULL) return;
  527.   for(p1=bf;*p1;p1=p2) {
  528.     p2=strchr(p1,'\n'); if(*p2) *p2++=0;
  529.     else p2=p1+strlen(p1);
  530.     p1=find_word_start(find_word_end(find_word_start(p1)));
  531.     e1=find_word_end(p1); if(*e1) *e1++=0;
  532.     e1=find_word_start(e1); e2=find_word_start(find_word_end(e1));
  533.     *find_word_end(e1)=0;
  534.     i=atoi(p1);
  535.     if(i>=1 && i<=MAX_EXAMS &&
  536.        exam_scoredata[i-1]==-1000 && strcmp(e1,"score")==0) {
  537.       *find_word_end(e2)=0;
  538.       exam_scoredata[i-1]=atof(e2);
  539.     }
  540.   }
  541.   free(bf);
  542. }
  543.  
  544. /* Gather exam score. */
  545. void calc_examscore(char *p)
  546. {
  547.   char *p1;
  548.   int i;
  549.   char *withoutip;
  550.  
  551.   _scoreparm(p); *p=0;
  552.   withoutip=getvar("wims_examscore_withoutip");
  553.  
  554.   if(*score_class==0 || *score_user==0) return;
  555.   if(getscoreuser(score_class,score_user)<0) return;
  556.   p1=p;
  557.   if(withoutip!=NULL && strcmp(withoutip,"yes")==0) {
  558.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  559.     p1=moneyprint(p1,rscore[examstart+i].best); *p1++=' ';
  560.     }
  561.   } else {
  562.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  563.       p1=moneyprint(p1,rscore[examstart+i].score); *p1++=' ';
  564.     }
  565.   }
  566.   *p1++='\n';
  567.   for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  568.     p1=moneyprint(p1,rscore[examstart+i].require); *p1++=' ';
  569.     p1=moneyprint(p1,floor(rscore[examstart+i].mean/2)); *p1++=' ';
  570.     p1=moneyprint(p1,(int) rscore[examstart+i].mean%2); *p1++='\n';
  571.   }
  572.   *p1=0;
  573. }
  574.  
  575. /* check score dependency.
  576.  * returns 1 if requirements are met.
  577.  */
  578. int _depcheck(char *ds, struct scoreresult *rs, int ecnt)
  579. {
  580.   char *p1, *p2, *p3, *p4, *pp;
  581.   int perc, t, sum;
  582.   double tgot, ttot, tmean;
  583.  
  584.   for(p1=ds; *p1; p1=p3) {
  585.     p2=strchr(p1,':'); if(p2==NULL) break;
  586.     *p2++=0; p2=find_word_start(p2);
  587.     for(p3=p2; myisdigit(*p3); p3++);
  588.     if(p3<=p2) break;
  589.     if(*p3) *p3++=0;
  590.     perc=atoi(p2);
  591.     if(perc<=0 || perc>100) break;
  592.     for(pp=p1; *pp; pp++) if(!myisdigit(*pp)) *pp=' ';
  593.     tgot=ttot=tmean=0; sum=0;
  594.     for(pp=find_word_start(p1); *pp; pp=find_word_start(p4)) {
  595.       p4=find_word_end(pp); if(*p4) *p4++=0;
  596.       t=atoi(pp); if(t<=0 || t>ecnt) goto lend;
  597.       t--;
  598.       ttot+=rs[t].require; tgot+=rs[t].score; tmean+=rs[t].mean;
  599.       sum++;
  600.     }
  601.     if(ttot<10) continue;
  602.     if(tgot/ttot*sqrt(tmean/(sum*10))*100<perc) {
  603.       for(pp=p1;pp<p2-1;pp++) if(!*pp) *pp=',';
  604.       *pp=0; setvar("dep_list",p1);
  605.       return 0;
  606.     }
  607.   lend: ;
  608.   }
  609.   return 1;
  610. }
  611.  
  612. int depcheck(char *sh, int exo, char *deps)
  613. {
  614.   char buf[MAX_LINELEN+1];
  615.   char *s, sbuf[64];
  616.   int i, is;
  617.  
  618.   s=getvar("wims_session");
  619.   if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return 0;
  620.   mystrncpy(sbuf,s,sizeof(sbuf));
  621.   s=strchr(sbuf,'_'); if(s) *s=0;
  622.   accessfile(buf,"r","../sessions/%s/exodep.%s",sbuf,sh);
  623.   if(buf[0]==0) {     /* no dep file found */
  624.     is=atoi(sh); if(is<=0 || is>totsheets) return 0;
  625.     s=getvar("wims_class"); if(s==NULL || *s==0) return 0;
  626.     getscoreuser(s,"");
  627.     return _depcheck(deps,rscore+sheetstart[is-1],shexocnt[is-1]);
  628.   }
  629.   for(i=1,s=strchr(buf,':'); s && i<exo; i++, s=strchr(s+1,':'));
  630.   if(s==NULL) return 0;     /* bad file or exo number */
  631.   if(myisdigit(*++s)) return 0; else return 1;
  632. }
  633.  
  634. int exam_depcheck(char *deps, int exam)
  635. {
  636.   static struct scoreresult esc[MAX_EXAMS];
  637.   int i;
  638.   exam_currscore(exam);
  639.   for(i=0;i<MAX_EXAMS;i++) {
  640.     esc[i].require=esc[i].mean=10;
  641.     if(exam_scoredata[i]==-1000) esc[i].score=0;
  642.     else esc[i].score=exam_scoredata[i];
  643.   }
  644.   return _depcheck(deps,esc,MAX_EXAMS);
  645. }
  646.  
  647. /* public sheet gives she=0 */
  648. /* return 1 if score is no more taken in account because of exotrymax */
  649. int gettrycheck(char *classe, char *user, int she, int exo) {
  650.   char *s;
  651.   int sh, ex, i;
  652.   if(classe==NULL || *classe==0 || user==NULL || *user==0) return 0;
  653.   if (she<=0) return 0;
  654.     if(strcmp(user,"supervisor")==0) return 0;
  655.   s=getvar("exotrymax");
  656.   if(s==NULL || *s==0) return 0;
  657.   getscoreuser(classe,user);
  658.   char *p=getvar("wims_scorereg");
  659.   for(i=0;i<scorecnt;i++) {
  660.     sh=rscore[i].sh+1;
  661.     if (she!=sh) continue;
  662.     ex=rscore[i].exo+1;
  663.     if(exo!=ex) continue;
  664.     if(cmd_type==cmd_new || cmd_type==cmd_renew)
  665.       if(rscore[i].new >= atoi(s)) {
  666.         if(strcmp(p,"suspend")!=0) setvar("wims_scorereg","exotrymax");
  667.         return 1;
  668.     }
  669.     if(rscore[i].new > atoi(s)){
  670.       if(strcmp(p,"suspend")!=0) setvar("wims_scorereg","exotrymax");
  671.       return 1;
  672.     }
  673.   }
  674.   return 0;
  675. }
  676.  
  677. /* return seed if the seed should be kept and NULL if not */
  678. char *getseedscore(char *classe, char *user, int she, int exo) {
  679.   char *s;
  680.   int sh, ex, i;
  681.   if (she<=0) return NULL;
  682.   if(strcmp(user,"supervisor")==0) return NULL;
  683.   s=getvar("seedrepeat");
  684.   if(s==NULL || *s==0 || atoi(s)<=0) return NULL;
  685.   getscoreuser(classe,user);
  686.   for(i=0;i<scorecnt;i++) {
  687.     sh=rscore[i].sh+1;
  688.     if (she!=sh) continue;
  689.     ex=rscore[i].exo+1;
  690.     if(exo!=ex) continue;
  691.     if(atoi(rscore[i].seedlast)==0) {
  692.       char *seed=getvar("wims_seed");
  693.       return seed;
  694.     }
  695.     if(rscore[i].seedlastcnt > MAX_SEEDSCORE) return NULL;
  696.     if(atoi(s)>0 && rscore[i].seedlastcnt >= atoi(s) && cmd_type!=cmd_next) return NULL;
  697.     if(rscore[i].seedscorelast < 10) return rscore[i].seedlast;
  698.   };
  699.   return NULL;
  700. }
  701.