Subversion Repositories wimsdev

Rev

Rev 7363 | Rev 7638 | 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.  
  20. double oldfactor=0.85;     /* quality factor, should remain stable. */
  21.  
  22. enum {sr_require, sr_weight, sr_score, sr_mean, sr_remain};
  23. char scorebuf[MAX_CLASSEXOS*sizeof(scoreresult)+32];
  24. struct scoreresult *rscore;
  25. int scorecnt;
  26. double scoresum[MAX_SHEETS];
  27. int sheetstart[MAX_SHEETS], shexocnt[MAX_SHEETS];
  28. int examstart, examcnt;
  29. char rscore_class[MAX_CLASSLEN+1];
  30. char rscore_user[MAX_NAMELEN+1];
  31.  
  32. int totsheets=0;
  33. int score_ispublic=0;
  34. int score_status=-1;          /* save of score status */
  35. int score_statussheet=-1;
  36. int score_statusisexam=-1;
  37. double scorerecfactor=0.9;
  38.  
  39. /* gather user score, core routine. */
  40. int getscoreuser(char *classe, char *user)
  41. {
  42.   int i, j, osh, sh;
  43.   double s;
  44.   char *nowuser, *nowsheet, *nowexo, *nowscore;
  45.   char *pp;
  46.   if(*user==0) {
  47.     user=getvar("wims_user");
  48.     if(user==NULL || *user==0) return -1;
  49.   }
  50.   if(strcmp(classe,rscore_class)==0 && strcmp(user,rscore_user)==0) return 0;
  51.   nowsheet=nowexo=nowscore="";
  52.   nowuser=getvar("wims_user"); if(nowuser!=NULL && strcmp(user,nowuser)==0) {
  53.     nowscore=getvar("module_score"); if(nowscore!=NULL && *nowscore!=0) {
  54.       nowsheet=getvar("wims_sheet"); if(nowsheet==NULL) nowsheet="";
  55.       nowexo=getvar("wims_exo"); if(nowexo==NULL) nowexo="";
  56.     }
  57.     else nowscore="";
  58.   }
  59.   snprintf(scorebuf+sizeof(int),sizeof(scorebuf)-sizeof(int),
  60.            "-c%s -u%s getscore %s %s %s",
  61.            classe,user,nowsheet,nowexo,nowscore);
  62.   i=kerneld(scorebuf,sizeof(scorebuf)); if(i<0)
  63.                                           internal_error("getscoreuser(): daemon failure.");
  64.   if(memcmp(scorebuf+sizeof(int),"OK",2)!=0) {
  65.     if(memcmp(scorebuf+sizeof(int),"ERROR",5)==0) {
  66.       module_error(find_word_start(scorebuf+sizeof(int)+8));
  67.     }
  68.     else internal_error("getscoreuser(): communication error with wimslogd.");
  69.   }
  70.   pp=scorebuf+sizeof(int)+3; rscore=(struct scoreresult *) pp;
  71.   scorecnt=(i-sizeof(int)-3)/sizeof(scoreresult);
  72.   if(scorecnt>MAX_CLASSEXOS) module_error("too_many_exercises");
  73.   s=0; for(i=osh=0;i<scorecnt;i++) {
  74.     sh=(rscore[i].num>>8)+1;
  75.     if(sh<1 || sh>MAX_SHEETS) break;
  76.     if(osh>0 && osh<sh) {scoresum[osh-1]=s; s=0;}
  77.     for(;osh<sh && osh<MAX_SHEETS;osh++) sheetstart[osh]=i;
  78.     s+=rscore[i].require*rscore[i].weight;
  79.   }
  80.   if(osh>0) scoresum[osh-1]=s; totsheets=osh;
  81.   for(j=0;j<totsheets-1;j++) shexocnt[j]=sheetstart[j+1]-sheetstart[j];
  82.   shexocnt[totsheets-1]=i-sheetstart[totsheets-1];
  83.   examstart=i; examcnt=scorecnt-examstart;
  84.   mystrncpy(rscore_class,classe,sizeof(rscore_class));
  85.   mystrncpy(rscore_user,user,sizeof(rscore_user));
  86.   return 0;
  87. }
  88. // work=exo ??
  89. char *scorepname[]={
  90.      "class","user","sheet","work","exam"
  91. };
  92. #define scorepname_no (sizeof(scorepname)/sizeof(scorepname[0]))
  93. char score_class[MAX_CLASSLEN+1];
  94. int score_sheet,score_exo,score_isexam;
  95. char score_user[256];
  96.  
  97. /* Uniformed treatment of score command parameters
  98.  * format: class=? user=? sheet=? work=?
  99.  * all are optional. */
  100. void _scoreparm(char *p)
  101. {
  102.     int i;
  103.     char *pn, *pe, *pd, *pf;
  104.     char sav;
  105.  
  106.     score_sheet=score_exo=score_isexam=score_ispublic=0; *score_class=0;
  107.     score_user[0]=0;
  108.     for(i=0;i<scorepname_no;i++) {
  109.      pf=p;
  110.      ahead:
  111.      pn=strstr(pf,scorepname[i]); pf=pn+1;
  112.      if(pn==NULL) continue;
  113.      if(pn>p && !isspace(*(pn-1))) goto ahead;
  114.      pe=find_word_start(pn+strlen(scorepname[i]));
  115.      if(*pe!='=') goto ahead;
  116.      pd=find_word_start(pe+1);
  117.      pf=find_word_end(pd);
  118.      if(pf<=pd) continue;
  119.      sav=*pf; *pf=0;
  120.      switch(i) {
  121.          case 0: /* class */
  122.            mystrncpy(score_class,pd,sizeof(score_class)); break;
  123.          case 1: /* user */
  124.            mystrncpy(score_user,pd,sizeof(score_user)); break;
  125.          case 2: { /* sheet */
  126.           if(*pd=='P') {pd++; score_ispublic=1;}
  127.           score_sheet=atoi(pd);
  128.           break;
  129.          }
  130.          case 3: /* work=exo */
  131.            score_exo=atoi(pd); break;
  132.          case 4: /* exam */
  133.            score_isexam=1; break;
  134.      }
  135.      *pf=sav; ovlstrcpy(pn, pf);
  136.     }
  137.     *p=0;
  138.     if((*score_class!=0 || score_user[0]!=0) && !trusted_module()) {
  139.      module_error("not_trusted"); return;
  140.     }
  141.     if(*score_class==0) {
  142.      char *classe;
  143.      classe=getvar("wims_class");
  144.      if(classe==NULL || *classe==0) return;
  145.      else mystrncpy(score_class,classe,sizeof(score_class));
  146.     }
  147.     if(score_user[0]==0) {
  148.      char *user;
  149.      user=getvar("wims_user");
  150.      if(user!=NULL) mystrncpy(score_user,user,sizeof(score_user));
  151.     }
  152. }
  153.  
  154. /* gather score: relatif to some of the
  155.  *  user class work sheet exam
  156.  *    dtype can be require weight score mean remain
  157.  */
  158.  
  159. void _getscore(char *p,int dtype)
  160. {
  161.     int i,sh,ex,osh;
  162.     float d;
  163.     char *p1;
  164.  
  165.     _scoreparm(p);
  166.     if(*score_class==0 || *score_user==0) return;
  167.     if(getscoreuser(score_class,score_user)<0) return;
  168. /* code exo as 2^8 number_sheet + number_exo, so limit the number_exo to 2^8
  169. */
  170.     for(i=osh=0,p1=p;i<scorecnt && p1-p<MAX_LINELEN-32;i++) {
  171.      sh=(rscore[i].num>>8)+1; if(sh<1 || sh>MAX_SHEETS) break;
  172.      if(score_sheet!=0) {
  173.          if(sh<score_sheet) continue;
  174.          if(sh>score_sheet || sh>MAX_SHEETS) break;
  175.      }
  176.      ex=((rscore[i].num)&255)+1;
  177.      if(score_exo!=0 && ex!=score_exo) continue;
  178.      if(osh!=0 && sh!=osh) *p1++='\n';
  179.      switch(dtype) {
  180.          case sr_require: {d=rscore[i].require; break;}
  181.          case sr_weight: {d=rscore[i].weight; break;}
  182.          case sr_score: {d=rscore[i].score; break;}
  183.          case sr_mean: {d=rscore[i].mean; break;}
  184.          case sr_remain: {d=rscore[i].require-rscore[i].score; break;}
  185.          default: {d=0; break;}
  186.      }
  187.      p1=moneyprint(p1,d); *p1++=' ';
  188.      osh=sh;
  189.     }
  190.     *p1++='\n'; *p1=0;
  191. }
  192.  
  193. /* gather user score. */
  194. void calc_getscore(char *p)
  195. {
  196.   _getscore(p,sr_score);
  197. }
  198.  
  199. /* gather user score average. */
  200. void calc_getscoremean(char *p)
  201. {
  202.   _getscore(p,sr_mean);
  203. }
  204.  
  205. /* gather remaining of score to get for user. */
  206. void calc_getscoreremain(char *p)
  207. {
  208.   _getscore(p,sr_remain);
  209. }
  210.  
  211. /* Require score table. */
  212. void calc_getscorerequire(char *p)
  213. {
  214.   _getscore(p,sr_require);
  215. }
  216.  
  217. /* Score weight table. */
  218. void calc_getscoreweight(char *p)
  219. {
  220.   _getscore(p,sr_weight);
  221. }
  222.  
  223. /* percentage of work done for each sheet. */
  224. void calc_getscorepercent(char *p)
  225. {
  226.   int i,j,jend;
  227.   double tot, mean, d;
  228.   char *p1;
  229.  
  230.   _scoreparm(p);
  231.   if(*score_class==0 || *score_user==0) return;
  232.   if(getscoreuser(score_class,score_user)<0) return;
  233.   for(p1=p,i=0;i<totsheets && p1-p<MAX_LINELEN-32;i++) {
  234.     if(scoresum[i]==0) {
  235.       ovlstrcpy(p1,"0 0\n"); p1+=strlen(p1); continue;
  236.     }
  237.     if(score_sheet!=0 && i!=score_sheet-1) continue;
  238.     if(scoresum[i]<=0) *p1++='\n';
  239.     tot=mean=0; jend=sheetstart[i]+shexocnt[i];
  240.     for(j=sheetstart[i];j<jend;j++) {
  241.       /* if mean<1 then ignore score.
  242.        * if mean<2 then half score. */
  243.       if(rscore[j].mean>=1) {
  244.         double dt=rscore[j].score;
  245.         if(rscore[j].mean<2) dt=dt/2;
  246.         d=dt*rscore[j].weight;
  247.         mean+=rscore[j].mean*d; tot+=d;
  248.       }
  249.     }
  250.     if(tot>0) d=mean/tot; else d=0;
  251.     p1=moneyprint(p1,rint(100*tot/scoresum[i])); *p1++=' ';
  252.     p1=moneyprint(p1,d); *p1++='\n';
  253.   }
  254.   *p1=0;
  255. }
  256.  
  257. /* Returns the status of a sheet, or -1 if error */
  258. int getsheetstatus(char *classe, int sheet)
  259. {
  260.   char *p, *st, buf[MAX_LINELEN+1], namebuf[MAX_FNAME+1];
  261.   int i;
  262.  
  263.   if(isexam || score_isexam) st="exam"; else st="sheet";
  264.   mkfname(namebuf,"%s/%s/%ss/.%ss",class_base,classe,st,st);
  265.   direct_datafile=1;datafile_fnd_record(namebuf,sheet,buf);direct_datafile=0;
  266.   p=find_word_start(buf); if(*p==0) return -1;
  267.   i=*p-'0'; if(i>5 || i<0) i=-1;
  268.   if((isexam || score_isexam) && i==0) {
  269.     p=getvar("wims_user"); if(p!=NULL && strcmp(p,"supervisor")==0) i=1;
  270.   }
  271.   return i;
  272. }
  273.  
  274. /* return 1 if a word of bf2 is a substring of host.
  275.  * Content of bf2 is destroyed. */
  276. int _subword(char bf2[])
  277. {
  278.   char *p1, *p2;
  279.   for(p1=strchr(bf2,'\\'); p1!=NULL; p1=strchr(p1+1,'\\')) {
  280.     char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1], fbuf[MAX_FNAME+1];
  281.     char *classp, *classp2, *userp, *scp;
  282.     classp=getvar("wims_class"); userp=getvar("wims_user");
  283.     if(classp==NULL || userp==NULL || *classp==0 || *userp==0) break;
  284.     scp=getvar("wims_superclass");
  285.     if(scp!=NULL && *scp!=0) classp2=scp; else classp2=classp;
  286.     if(p1>bf2 && !isspace(*(p1-1))) continue;
  287.     if(!isalnum(*(p1+1))) continue;
  288.     p2=find_word_end(p1); if(p2>=p1+MAX_NAMELEN) continue;
  289.     memmove(buf2, p1+1, p2-p1-1); buf2[p2-p1-1]=0;
  290.     snprintf(buf,sizeof(buf),"user__%s",buf2);
  291.     if(strcmp(userp,"supervisor")==0)
  292.       mkfname(fbuf,"%s/%s/supervisor",class_base,classp);
  293.     else
  294.       mkfname(fbuf,"%s/%s/.users/%s",class_base,classp2,userp);
  295.     getdef(fbuf,buf,buf2); if(buf2[0]==0) ovlstrcpy(buf2,"none");
  296.     string_modify(bf2,p1,p2,buf2);
  297.     p1+=strlen(buf2);
  298.   }
  299.   if((isexam || score_isexam) && bf2[0]=='#') return 1;
  300.   if(wordchr(bf2,"none")!=NULL) return 0;
  301.   if(wordchr(bf2,"all")!=NULL) return 1;
  302.   p1=find_word_start(bf2); if(*p1==0) return 1;
  303.   return checkhostt(p1);
  304. }
  305.  
  306. /* Returns 1 if score registration is open, 0 otherwise. */
  307. int _getscorestatus(char *classe, int sheet)
  308. {
  309.   char nbuf[MAX_LINELEN+1], gbuf[MAX_LINELEN+1];
  310.   char *es;
  311.  
  312.   if(classe==NULL || *classe==0 || sheet<=0) return 1;
  313.   if(getsheetstatus(classe,sheet)!=1) return 0;
  314.   if(*remote_addr==0) return 0;
  315.   if(isexam || score_isexam) {     /* exam simulation */
  316.     accessfile(nbuf,"r","%s/%s/.E%d",class_base,classe,sheet);
  317.     if(nbuf[0]=='#') return 1;
  318.   }
  319.   /* Global restriction data */
  320.   accessfile(nbuf,"r","%s/%s/.security",class_base,classe);
  321.   if(nbuf[0]) {
  322.     _getdef(nbuf,"allow",gbuf);
  323.     if(*find_word_start(gbuf)!=0 && _subword(gbuf)==0)
  324.       return 0;
  325.     _getdef(nbuf,"except",gbuf);
  326.     if(*find_word_start(gbuf)!=0 && _subword(gbuf)==1)
  327.       return 0;
  328.   }
  329.  
  330.   /* Sheet restriction data */
  331.   if(isexam || score_isexam) es="E"; else es="";
  332.   accessfile(nbuf,"r","%s/%s/.%s%d",class_base,classe,es,sheet);
  333.   if(*find_word_start(nbuf)==0) return 1;
  334.   return _subword(nbuf);
  335. }
  336.  
  337. /* Returns 1 if score registration is open, 0 otherwise. */
  338. int getscorestatus(char *classe, int sheet)
  339. {
  340.   if(score_status<0 || score_statussheet!=sheet
  341.      || score_statusisexam!=isexam) {
  342.     score_statussheet=sheet; score_statusisexam=isexam;
  343.     score_status=_getscorestatus(classe,sheet); score_isexam=0;
  344.     if(score_status==1 && (cmd_type==cmd_new || cmd_type==cmd_renew
  345.                            || isexam)) {
  346.       char *p;
  347.       p=getvar("wims_scorereg");
  348.       if(p==NULL || strcmp(p,"suspend")!=0)
  349.         setvar("wims_scoring","pending");
  350.       else setvar("wims_scoring","");
  351.     }
  352.   }
  353.   if(isexam && score_status==0) {
  354.     char *p;
  355.     p=getvar("wims_user"); if(p==NULL || strcmp(p,"supervisor")!=0)
  356.                              user_error("exam_closed");
  357.   }
  358.   return score_status;
  359. }
  360.  
  361. /* Whether score registering is open */
  362. void calc_getscorestatus(char *p)
  363. {
  364.   _scoreparm(p);
  365.   if(*score_class==0 || score_sheet==0 || *score_user==0) {
  366.     *p=0; return;
  367.   }
  368.   if(getscorestatus(score_class, score_sheet))
  369.     ovlstrcpy(p,"yes");
  370.   else ovlstrcpy(p,"no");
  371. }
  372.  
  373. double exam_scoredata[MAX_EXOS];
  374.  
  375. /* get current exam score */
  376. void exam_currscore(int esh)
  377. {
  378.   char *p, *bf, pb[MAX_FNAME+1];
  379.   char *s, *p1, *p2, *e1, *e2;
  380.   int i;
  381.  
  382.   for(i=0;i<MAX_EXOS;i++) exam_scoredata[i]=-1000;
  383.   /* session_prefix is not yet defined here */
  384.   s=getvar("wims_session"); if(s==NULL || *s==0) return;
  385.   mystrncpy(pb,s,sizeof(pb));
  386.   p=strchr(pb,'_'); if(p!=NULL) *p=0;
  387.   bf=readfile(mkfname(NULL,"%s/%s/examscore.%d",session_dir,pb,esh),NULL,WORKFILE_LIMIT);
  388.   if(bf==NULL) return;
  389.   for(p1=bf;*p1;p1=p2) {
  390.     p2=strchr(p1,'\n'); if(*p2) *p2++=0;
  391.     else p2=p1+strlen(p1);
  392.     p1=find_word_start(find_word_end(find_word_start(p1)));
  393.     e1=find_word_end(p1); if(*e1) *e1++=0;
  394.     e1=find_word_start(e1); e2=find_word_start(find_word_end(e1));
  395.     *find_word_end(e1)=0;
  396.     i=atoi(p1);
  397.     if(i>=1 && i<=MAX_EXOS &&
  398.        exam_scoredata[i-1]==-1000 && strcmp(e1,"score")==0) {
  399.       *find_word_end(e2)=0;
  400.       exam_scoredata[i-1]=atof(e2);
  401.     }
  402.   }
  403.   free(bf);
  404. }
  405.  
  406. /* Gather exam score. */
  407. void calc_examscore(char *p)
  408. {
  409.   char *p1;
  410.   int i;
  411.  
  412.   _scoreparm(p); *p=0;
  413.   if(*score_class==0 || *score_user==0) return;
  414.   if(getscoreuser(score_class,score_user)<0) return;
  415.   p1=p;
  416.   for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  417.     p1=moneyprint(p1,rscore[examstart+i].score); *p1++=' ';
  418.   }
  419.   *p1++='\n';
  420.   for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  421.     p1=moneyprint(p1,rscore[examstart+i].require); *p1++=' ';
  422.     p1=moneyprint(p1,floor(rscore[examstart+i].mean/2)); *p1++=' ';
  423.     p1=moneyprint(p1,(int) rscore[examstart+i].mean%2); *p1++='\n';
  424.   }
  425.   *p1=0;
  426. }
  427.  
  428. /* check score dependency.
  429.  * returns 1 if requirements are met. */
  430. int _depcheck(char *ds, struct scoreresult *rs, int ecnt)
  431. {
  432.   char *p1, *p2, *p3, *p4, *pp;
  433.   int perc, t, sum;
  434.   double tgot, ttot, tmean;
  435.  
  436.   for(p1=ds; *p1; p1=p3) {
  437.     p2=strchr(p1,':'); if(p2==NULL) break;
  438.     *p2++=0; p2=find_word_start(p2);
  439.     for(p3=p2; myisdigit(*p3); p3++);
  440.     if(p3<=p2) break;
  441.     *p3++=0; perc=atoi(p2);
  442.     if(perc<=0 || perc>100) break;
  443.     for(pp=p1; *pp; pp++) if(!myisdigit(*pp)) *pp=' ';
  444.     tgot=ttot=tmean=0; sum=0;
  445.     for(pp=find_word_start(p1); *pp; pp=find_word_start(p4)) {
  446.       p4=find_word_end(pp); if(*p4) *p4++=0;
  447.       t=atoi(pp); if(t<=0 || t>ecnt) goto lend;
  448.       t--;
  449.       ttot+=rs[t].require; tgot+=rs[t].score; tmean+=rs[t].mean;
  450.       sum++;
  451.     }
  452.     if(ttot<10) continue;
  453.     if(tgot/ttot*sqrt(tmean/(sum*10))*100<perc) {
  454.       for(pp=p1;pp<p2-1;pp++) if(!*pp) *pp=',';
  455.       *pp=0; setvar("dep_list",p1);
  456.       return 0;
  457.     }
  458.   lend: ;
  459.   }
  460.   return 1;
  461. }
  462.  
  463. int depcheck(char *sh, int exo, char *deps)
  464. {
  465.   char buf[MAX_LINELEN+1];
  466.   char *s, sbuf[64];
  467.   int i, is;
  468.  
  469.   s=getvar("wims_session");
  470.   if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return 0;
  471.   mystrncpy(sbuf,s,sizeof(sbuf));
  472.   s=strchr(sbuf,'_'); if(s) *s=0;
  473.   accessfile(buf,"r","../sessions/%s/exodep.%s",sbuf,sh);
  474.   if(buf[0]==0) {     /* no dep file found */
  475.     is=atoi(sh); if(is<=0 || is>totsheets) return 0;
  476.     s=getvar("wims_class"); if(s==NULL || *s==0) return 0;
  477.     getscoreuser(s,"");
  478.     return _depcheck(deps,rscore+sheetstart[is-1],shexocnt[is-1]);
  479.   }
  480.   for(i=1,s=strchr(buf,':'); s && i<exo; i++, s=strchr(s+1,':'));
  481.   if(s==NULL) return 0;     /* bad file or exo number */
  482.   if(myisdigit(*++s)) return 0; else return 1;
  483. }
  484.  
  485. int exam_depcheck(char *deps, int exam)
  486. {
  487.   struct scoreresult esc[MAX_EXOS];
  488.   int i;
  489.   exam_currscore(exam);
  490.   for(i=0;i<MAX_EXOS;i++) {
  491.     esc[i].require=esc[i].mean=10;
  492.     if(exam_scoredata[i]==-1000) esc[i].score=0;
  493.     else esc[i].score=exam_scoredata[i];
  494.   }
  495.   return _depcheck(deps,esc,MAX_EXOS);
  496. }
  497.