Subversion Repositories wimsdev

Rev

Rev 7149 | Rev 7407 | 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. void _getscore(char *p,int dtype)
  159. {
  160.     int i,sh,ex,osh;
  161.     float d;
  162.     char *p1;
  163.  
  164.     _scoreparm(p);
  165.     if(*score_class==0 || *score_user==0) return;
  166.     if(getscoreuser(score_class,score_user)<0) return;
  167. /* code exo as 2^8 number_sheet + number_exo, so limit the number_exo to 2^8
  168. */
  169.     for(i=osh=0,p1=p;i<scorecnt && p1-p<MAX_LINELEN-32;i++) {
  170.      sh=(rscore[i].num>>8)+1; if(sh<1 || sh>MAX_SHEETS) break;
  171.      if(score_sheet!=0) {
  172.          if(sh<score_sheet) continue;
  173.          if(sh>score_sheet || sh>MAX_SHEETS) break;
  174.      }
  175.      ex=((rscore[i].num)&255)+1;
  176.      if(score_exo!=0 && ex!=score_exo) continue;
  177.      if(osh!=0 && sh!=osh) *p1++='\n';
  178.      switch(dtype) {
  179.          case sr_require: {d=rscore[i].require; break;}
  180.          case sr_weight: {d=rscore[i].weight; break;}
  181.          case sr_score: {d=rscore[i].score; break;}
  182.          case sr_mean: {d=rscore[i].mean; break;}
  183.          case sr_remain: {d=rscore[i].require-rscore[i].score; break;}
  184.          default: {d=0; break;}
  185.      }
  186.      p1=moneyprint(p1,d); *p1++=' ';
  187.      osh=sh;
  188.     }
  189.     *p1++='\n'; *p1=0;
  190. }
  191.  
  192.      /* gather user score. */
  193. void calc_getscore(char *p)
  194. {
  195.     _getscore(p,sr_score);
  196. }
  197.  
  198.      /* gather user score average. */
  199. void calc_getscoremean(char *p)
  200. {
  201.     _getscore(p,sr_mean);
  202. }
  203.  
  204.      /* gather remaining of score to get for user. */
  205. void calc_getscoreremain(char *p)
  206. {
  207.     _getscore(p,sr_remain);
  208. }
  209.  
  210.      /* Require score table. */
  211. void calc_getscorerequire(char *p)
  212. {
  213.     _getscore(p,sr_require);
  214. }
  215.  
  216.      /* Score weight table. */
  217. void calc_getscoreweight(char *p)
  218. {
  219.     _getscore(p,sr_weight);
  220. }
  221.  
  222.      /* percentage of work done for each sheet. */
  223. void calc_getscorepercent(char *p)
  224. {
  225.     int i,j,jend;
  226.     double tot, mean, d;
  227.     char *p1;
  228.  
  229.     _scoreparm(p);
  230.     if(*score_class==0 || *score_user==0) return;
  231.     if(getscoreuser(score_class,score_user)<0) return;
  232.     for(p1=p,i=0;i<totsheets && p1-p<MAX_LINELEN-32;i++) {
  233.      if(scoresum[i]==0) {
  234.          ovlstrcpy(p1,"0 0\n"); p1+=strlen(p1); continue;
  235.      }
  236.      if(score_sheet!=0 && i!=score_sheet-1) continue;
  237.      if(scoresum[i]<=0) *p1++='\n';
  238.      tot=mean=0; jend=sheetstart[i]+shexocnt[i];
  239.      for(j=sheetstart[i];j<jend;j++) {
  240.      /* if mean<1 then ignore score.
  241.       * if mean<2 then half score. */
  242.          if(rscore[j].mean>=1) {
  243.           double dt=rscore[j].score;
  244.           if(rscore[j].mean<2) dt=dt/2;
  245.           d=dt*rscore[j].weight;
  246.           mean+=rscore[j].mean*d; tot+=d;
  247.          }
  248.      }
  249.      if(tot>0) d=mean/tot; else d=0;
  250.      p1=moneyprint(p1,rint(100*tot/scoresum[i])); *p1++=' ';
  251.      p1=moneyprint(p1,d); *p1++='\n';
  252.     }
  253.     *p1=0;
  254. }
  255.  
  256.      /* Returns the status of a sheet, or -1 if error */
  257. int getsheetstatus(char *classe, int sheet)
  258. {
  259.     char *p, *st, buf[MAX_LINELEN+1], namebuf[MAX_FNAME+1];
  260.     int i;
  261.  
  262.     if(isexam || score_isexam) st="exam"; else st="sheet";
  263.     mkfname(namebuf,"%s/%s/%ss/.%ss",class_base,classe,st,st);
  264.     direct_datafile=1;datafile_fnd_record(namebuf,sheet,buf);direct_datafile=0;
  265.     p=find_word_start(buf); if(*p==0) return -1;
  266.     i=*p-'0'; if(i>5 || i<0) i=-1;
  267.     if((isexam || score_isexam) && i==0) {
  268.      p=getvar("wims_user"); if(p!=NULL && strcmp(p,"supervisor")==0) i=1;
  269.     }
  270.     return i;
  271. }
  272.  
  273.      /* return 1 if a word of bf2 is a substring of host.
  274.       * Content of bf2 is destroyed. */
  275. int _subword(char bf2[])
  276. {
  277.     char *p1, *p2;
  278.     for(p1=strchr(bf2,'\\'); p1!=NULL; p1=strchr(p1+1,'\\')) {
  279.      char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1], fbuf[MAX_FNAME+1];
  280.      char *classp, *classp2, *userp, *scp;
  281.      classp=getvar("wims_class"); userp=getvar("wims_user");
  282.      if(classp==NULL || userp==NULL || *classp==0 || *userp==0) break;
  283.      scp=getvar("wims_superclass");
  284.      if(scp!=NULL && *scp!=0) classp2=scp; else classp2=classp;
  285.      if(p1>bf2 && !isspace(*(p1-1))) continue;
  286.      if(!isalnum(*(p1+1))) continue;
  287.      p2=find_word_end(p1); if(p2>=p1+MAX_NAMELEN) continue;
  288.      memmove(buf2, p1+1, p2-p1-1); buf2[p2-p1-1]=0;
  289.      snprintf(buf,sizeof(buf),"user__%s",buf2);
  290.      if(strcmp(userp,"supervisor")==0)
  291.        mkfname(fbuf,"%s/%s/supervisor",class_base,classp);
  292.      else
  293.        mkfname(fbuf,"%s/%s/.users/%s",class_base,classp2,userp);
  294.      getdef(fbuf,buf,buf2); if(buf2[0]==0) ovlstrcpy(buf2,"none");
  295.      string_modify(bf2,p1,p2,buf2);
  296.      p1+=strlen(buf2);
  297.     }
  298.     if((isexam || score_isexam) && bf2[0]=='#') return 1;
  299.     if(wordchr(bf2,"none")!=NULL) return 0;
  300.     if(wordchr(bf2,"all")!=NULL) return 1;
  301.     p1=find_word_start(bf2); if(*p1==0) return 1;
  302.     return checkhostt(p1);
  303. }
  304.  
  305.      /* Returns 1 if score registration is open, 0 otherwise. */
  306. int _getscorestatus(char *classe, int sheet)
  307. {
  308.     char nbuf[MAX_LINELEN+1], gbuf[MAX_LINELEN+1];
  309.     char *es;
  310.  
  311.     if(classe==NULL || *classe==0 || sheet<=0) return 1;
  312.     if(getsheetstatus(classe,sheet)!=1) return 0;
  313.     if(*remote_addr==0) return 0;
  314.     if(isexam || score_isexam) {     /* exam simulation */
  315.      accessfile(nbuf,"r","%s/%s/.E%d",class_base,classe,sheet);
  316.      if(nbuf[0]=='#') return 1;
  317.     }
  318.      /* Global restriction data */
  319.     accessfile(nbuf,"r","%s/%s/.security",class_base,classe);
  320.     if(nbuf[0]) {
  321.      _getdef(nbuf,"allow",gbuf);
  322.      if(*find_word_start(gbuf)!=0 && _subword(gbuf)==0)
  323.        return 0;
  324.      _getdef(nbuf,"except",gbuf);
  325.      if(*find_word_start(gbuf)!=0 && _subword(gbuf)==1)
  326.        return 0;
  327.     }
  328.  
  329.      /* Sheet restriction data */
  330.     if(isexam || score_isexam) es="E"; else es="";
  331.     accessfile(nbuf,"r","%s/%s/.%s%d",class_base,classe,es,sheet);
  332.     if(*find_word_start(nbuf)==0) return 1;
  333.     return _subword(nbuf);
  334. }
  335.  
  336.      /* Returns 1 if score registration is open, 0 otherwise. */
  337. int getscorestatus(char *classe, int sheet)
  338. {
  339.     if(score_status<0 || score_statussheet!=sheet
  340.        || score_statusisexam!=isexam) {
  341.      score_statussheet=sheet; score_statusisexam=isexam;
  342.      score_status=_getscorestatus(classe,sheet); score_isexam=0;
  343.      if(score_status==1 && (cmd_type==cmd_new || cmd_type==cmd_renew
  344.                       || isexam)) {
  345.          char *p;
  346.          p=getvar("wims_scorereg");
  347.          if(p==NULL || strcmp(p,"suspend")!=0)
  348.            setvar("wims_scoring","pending");
  349.          else setvar("wims_scoring","");
  350.      }
  351.     }
  352.     if(isexam && score_status==0) {
  353.      char *p;
  354.      p=getvar("wims_user"); if(p==NULL || strcmp(p,"supervisor")!=0)
  355.        user_error("exam_closed");
  356.     }
  357.     return score_status;
  358. }
  359.  
  360.      /* Whether score registering is open */
  361. void calc_getscorestatus(char *p)
  362. {
  363.     _scoreparm(p);
  364.     if(*score_class==0 || score_sheet==0 || *score_user==0) {
  365.         *p=0; return;
  366.     }
  367.     if(getscorestatus(score_class, score_sheet))
  368.       ovlstrcpy(p,"yes");
  369.     else ovlstrcpy(p,"no");
  370. }
  371.  
  372. double exam_scoredata[MAX_EXOS];
  373.  
  374.      /* get current exam score */
  375. void exam_currscore(int esh)
  376. {
  377.     char *p, *bf, pb[MAX_FNAME+1];
  378.     char *s, *p1, *p2, *e1, *e2;
  379.     int i;
  380.    
  381.     for(i=0;i<MAX_EXOS;i++) exam_scoredata[i]=-1000;
  382.      /* session_prefix is not yet defined here */
  383.     s=getvar("wims_session"); if(s==NULL || *s==0) return;
  384.     mystrncpy(pb,s,sizeof(pb));
  385.     p=strchr(pb,'_'); if(p!=NULL) *p=0;
  386.     bf=readfile(mkfname(NULL,"%s/%s/examscore.%d",session_dir,pb,esh),NULL,WORKFILE_LIMIT);
  387.     if(bf==NULL) return;
  388.     for(p1=bf;*p1;p1=p2) {
  389.      p2=strchr(p1,'\n'); if(*p2) *p2++=0;
  390.      else p2=p1+strlen(p1);
  391.      p1=find_word_start(find_word_end(find_word_start(p1)));
  392.      e1=find_word_end(p1); if(*e1) *e1++=0;
  393.      e1=find_word_start(e1); e2=find_word_start(find_word_end(e1));
  394.      *find_word_end(e1)=0;
  395.      i=atoi(p1);
  396.      if(i>=1 && i<=MAX_EXOS &&
  397.         exam_scoredata[i-1]==-1000 && strcmp(e1,"score")==0) {
  398.          *find_word_end(e2)=0;
  399.          exam_scoredata[i-1]=atof(e2);
  400.      }
  401.     }
  402.     free(bf);
  403. }
  404.  
  405.      /* Gather exam score. */
  406. void calc_examscore(char *p)
  407. {
  408.     char *p1;
  409.     int i;
  410.    
  411.     _scoreparm(p); *p=0;
  412.     if(*score_class==0 || *score_user==0) return;
  413.     if(getscoreuser(score_class,score_user)<0) return;
  414.     p1=p;
  415.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  416.      p1=moneyprint(p1,rscore[examstart+i].score); *p1++=' ';
  417.     }
  418.     *p1++='\n';
  419.     for(i=0; i<examcnt && p1-p<MAX_LINELEN-32; i++) {
  420.      p1=moneyprint(p1,rscore[examstart+i].require); *p1++=' ';
  421.      p1=moneyprint(p1,floor(rscore[examstart+i].mean/2)); *p1++=' ';
  422.      p1=moneyprint(p1,(int) rscore[examstart+i].mean%2); *p1++='\n';
  423.     }
  424.     *p1=0;
  425. }
  426.  
  427.      /* check score dependency.
  428.       * returns 1 if requirements are met. */
  429. int _depcheck(char *ds, struct scoreresult *rs, int ecnt)
  430. {
  431.     char *p1, *p2, *p3, *p4, *pp;
  432.     int perc, t, sum;
  433.     double tgot, ttot, tmean;
  434.    
  435.     for(p1=ds; *p1; p1=p3) {
  436.      p2=strchr(p1,':'); if(p2==NULL) break;
  437.      *p2++=0; p2=find_word_start(p2);
  438.      for(p3=p2; myisdigit(*p3); p3++);
  439.      if(p3<=p2) break;
  440.      *p3++=0; perc=atoi(p2);
  441.      if(perc<=0 || perc>100) break;
  442.      for(pp=p1; *pp; pp++) if(!myisdigit(*pp)) *pp=' ';
  443.      tgot=ttot=tmean=0; sum=0;
  444.      for(pp=find_word_start(p1); *pp; pp=find_word_start(p4)) {
  445.          p4=find_word_end(pp); if(*p4) *p4++=0;
  446.          t=atoi(pp); if(t<=0 || t>ecnt) goto lend;
  447.          t--;
  448.          ttot+=rs[t].require; tgot+=rs[t].score; tmean+=rs[t].mean;
  449.          sum++;
  450.      }
  451.      if(ttot<10) continue;
  452.      if(tgot/ttot*sqrt(tmean/(sum*10))*100<perc) {
  453.          for(pp=p1;pp<p2-1;pp++) if(!*pp) *pp=',';
  454.          *pp=0; setvar("dep_list",p1);
  455.          return 0;
  456.      }
  457.      lend: ;
  458.     }
  459.     return 1;
  460. }
  461.  
  462. int depcheck(char *sh, int exo, char *deps)
  463. {
  464.     char buf[MAX_LINELEN+1];
  465.     char *s, sbuf[64];
  466.     int i, is;
  467.    
  468.     s=getvar("wims_session");
  469.     if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return 0;
  470.     mystrncpy(sbuf,s,sizeof(sbuf));
  471.     s=strchr(sbuf,'_'); if(s) *s=0;
  472.     accessfile(buf,"r","../sessions/%s/exodep.%s",sbuf,sh);
  473.     if(buf[0]==0) {     /* no dep file found */
  474.      is=atoi(sh); if(is<=0 || is>totsheets) return 0;
  475.      s=getvar("wims_class"); if(s==NULL || *s==0) return 0;
  476.      getscoreuser(s,"");
  477.      return _depcheck(deps,rscore+sheetstart[is-1],shexocnt[is-1]);
  478.     }
  479.     for(i=1,s=strchr(buf,':'); s && i<exo; i++, s=strchr(s+1,':'));
  480.     if(s==NULL) return 0;     /* bad file or exo number */
  481.     if(myisdigit(*++s)) return 0; else return 1;
  482. }
  483.  
  484. int exam_depcheck(char *deps, int exam)
  485. {
  486.     struct scoreresult esc[MAX_EXOS];
  487.     int i;
  488.     exam_currscore(exam);
  489.     for(i=0;i<MAX_EXOS;i++) {
  490.      esc[i].require=esc[i].mean=10;
  491.      if(exam_scoredata[i]==-1000) esc[i].score=0;
  492.      else esc[i].score=exam_scoredata[i];
  493.     }
  494.     return _depcheck(deps,esc,MAX_EXOS);
  495. }
  496.