home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 276.img / FORUM21S.ZIP / VOTING.PAS < prev    next >
Pascal/Delphi Source File  |  1988-02-13  |  11KB  |  454 lines

  1. {$R-,S-,I-,D-,T-,F-,V-,B-,N-,L+ }
  2. {$M 65500,0,0 }
  3.  
  4. unit voting;
  5.  
  6. interface
  7.  
  8. uses gentypes,gensubs,subs1,subs2,userret,overret1;
  9.  
  10. procedure votingbooth (getmandatory:boolean);
  11.  
  12. implementation
  13.  
  14. procedure votingbooth (getmandatory:boolean);
  15. var curtopic:topicrec;
  16.     curtopicnum:integer;
  17.  
  18.   function votefn (n:integer):sstr;
  19.   begin
  20.     votefn:='Votefile.'+strr(n)
  21.   end;
  22.  
  23.   procedure opentopicdir;
  24.   var n:integer;
  25.   begin
  26.     assign (tofile,'VOTEDIR');
  27.     reset (tofile);
  28.     if ioresult<>0 then begin
  29.       close (tofile);
  30.       n:=ioresult;
  31.       rewrite (tofile)
  32.     end
  33.   end;
  34.  
  35.   function numtopics:integer;
  36.   begin
  37.     numtopics:=filesize (tofile)
  38.   end;
  39.  
  40.   procedure opentopic (n:integer);
  41.   var q:integer;
  42.   begin
  43.     curtopicnum:=n;
  44.     close (chfile);
  45.     assign (chfile,votefn(n));
  46.     reset (chfile);
  47.     if ioresult<>0 then begin
  48.       close (chfile);
  49.       q:=ioresult;
  50.       rewrite (chfile)
  51.     end;
  52.     seek (tofile,n-1);
  53.     read (tofile,curtopic)
  54.   end;
  55.  
  56.   function numchoices:integer;
  57.   begin
  58.     numchoices:=filesize (chfile)
  59.   end;
  60.  
  61.   procedure writecurtopic;
  62.   begin
  63.     seek (tofile,curtopicnum-1);
  64.     write (tofile,curtopic)
  65.   end;
  66.  
  67.   procedure listchoices;
  68.   var ch:choicerec;
  69.       cnt:integer;
  70.   begin
  71.     writehdr ('Your Choices');
  72.     seek (chfile,0);
  73.     for cnt:=1 to numchoices do
  74.       begin
  75.         read (chfile,ch);
  76.         writeln (cnt:2,'.  ',ch.choice);
  77.         if break then exit
  78.       end
  79.   end;
  80.  
  81.   function addchoice:integer;
  82.   var ch:choicerec;
  83.   begin
  84.     addchoice:=0;
  85.     buflen:=70;
  86.     writestr (^M'Enter new choice: &');
  87.     if length(input)<2 then exit;
  88.     addchoice:=numchoices+1;
  89.     ch.numvoted:=0;
  90.     ch.choice:=input;
  91.     seek (chfile,numchoices);
  92.     write (chfile,ch);
  93.     writelog (20,2,ch.choice)
  94.   end;
  95.  
  96.   procedure getvote (mandatory:boolean);
  97.   var cnt,chn:integer;
  98.       k:char;
  99.       ch:choicerec;
  100.       tmp:lstr;
  101.       a:boolean;
  102.   begin
  103.     if urec.voted[curtopicnum]<>0 then begin
  104.       writeln ('Sorry, can''t vote twice!!');
  105.       exit
  106.     end;
  107.     a:=ulvl>=curtopic.addlevel;
  108.     tmp:='Your choice [?=List';
  109.     if a then tmp:=tmp+', A to add';
  110.     tmp:=tmp+']:';
  111.     repeat
  112.       writestr (tmp);
  113.       if (length(input)=0) or hungupon then exit;
  114.       chn:=valu(input);
  115.       if chn=0 then begin
  116.         k:=upcase(input[1]);
  117.         if k='?'
  118.           then listchoices
  119.           else if k='A'
  120.             then if a
  121.               then chn:=addchoice
  122.               else writestr ('You may not add choices to this topic!')
  123.       end
  124.     until chn<>0;
  125.     if (chn>numchoices) or (chn<0) then begin
  126.       writeln ('Choice number out of range!');
  127.       exit
  128.     end;
  129.     curtopic.numvoted:=curtopic.numvoted+1;
  130.     writecurtopic;
  131.     seek (chfile,chn-1);
  132.     read (chfile,ch);
  133.     ch.numvoted:=ch.numvoted+1;
  134.     seek (chfile,chn-1);
  135.     write (chfile,ch);
  136.     urec.voted[curtopicnum]:=chn;
  137.     writeurec;
  138.     writeln ('Thanks for voting!')
  139.   end;
  140.  
  141.   procedure showresults;
  142.   var cnt,tpos,n:integer;
  143.       ch:choicerec;
  144.       percent:real;
  145.   begin
  146.     if urec.voted[curtopicnum]=0 then begin
  147.       writeln ('Sorry, you must vote first!');
  148.       exit
  149.     end;
  150.     seek (chfile,0);
  151.     tpos:=1;
  152.     for cnt:=1 to filesize (chfile) do begin
  153.       read (chfile,ch);
  154.       n:=length(ch.choice)+2;
  155.       if n>tpos then tpos:=n
  156.     end;
  157.     writehdr ('The results so far');
  158.     seek (chfile,0);
  159.     for cnt:=1 to numchoices do if not break then begin
  160.       read (chfile,ch);
  161.       tab (ch.choice,tpos);
  162.       writeln (ch.numvoted)
  163.     end;
  164.     if numusers>0
  165.       then percent:=100.0*curtopic.numvoted/numusers
  166.       else percent:=0;
  167.     writeln (^M,percent:0:0,'% of ',numusers,' have voted.')
  168.   end;
  169.  
  170.   procedure listtopics;
  171.   var t:topicrec;
  172.       cnt:integer;
  173.   begin
  174.     writehdr ('Voting Topics');
  175.     seek (tofile,0);
  176.     for cnt:=1 to numtopics do
  177.       if not break then begin
  178.         read (tofile,t);
  179.         writeln (cnt:2,'.  ',t.topicname)
  180.       end
  181.   end;
  182.  
  183.   procedure addtopic;
  184.   var t:topicrec;
  185.       ch:choicerec;
  186.       u:userrec;
  187.       cnt,tpn:integer;
  188.   begin
  189.     if numtopics>=maxtopics then
  190.       begin
  191.         writeln ('No more room to add a topic!');
  192.         exit
  193.       end;
  194.     tpn:=numtopics+1;
  195.     writestr ('Topic name:');
  196.     if length(input)=0 then exit;
  197.     t.topicname:=input;
  198.     t.numvoted:=0;
  199.     writeurec;
  200.     for cnt:=1 to numusers do begin
  201.       seek (ufile,cnt);
  202.       read (ufile,u);
  203.       if u.voted[tpn]<>0
  204.         then
  205.           begin
  206.             u.voted[tpn]:=0;
  207.             seek (ufile,cnt);
  208.             write (ufile,u)
  209.           end
  210.     end;
  211.     readurec;
  212.     writestr (^M'Make all users vote on this topic? *');
  213.     t.mandatory:=yes;
  214.     writestr ('Allow users to add their own choices? *');
  215.     if yes then begin
  216.       writestr ('Level required to add choices? *');
  217.       t.addlevel:=valu(input)
  218.     end else t.addlevel:=maxint;
  219.     seek (tofile,tpn-1);
  220.     write (tofile,t);
  221.     opentopic (tpn);
  222.     writeln (^M^B'Enter choices, blank line to end.');
  223.     cnt:=1;
  224.     repeat
  225.       buflen:=70;
  226.       writestr ('Choice number '+strr(cnt)+': &');
  227.       if length(input)>0 then begin
  228.         cnt:=cnt+1;
  229.         ch.numvoted:=0;
  230.         ch.choice:=input;
  231.         write (chfile,ch)
  232.       end
  233.     until (length(input)=0) or hungupon;
  234.     writeln ('Topic created!');
  235.     writelog (20,3,strr(tpn)+' ('+t.topicname+')')
  236.   end;
  237.  
  238.   procedure maybeaddtopic;
  239.   begin
  240.     writestr ('Create new topic? *');
  241.     if yes then addtopic
  242.   end;
  243.  
  244.   procedure selecttopic;
  245.   var ch:integer;
  246.   begin
  247.     input:=copy(input,2,255);
  248.     if input='' then input:=' ';
  249.     repeat
  250.       if length(input)=0 then exit;
  251.       ch:=valu(input);
  252.       if ch>numtopics then begin
  253.         ch:=numtopics+1;
  254.         if issysop then maybeaddtopic;
  255.         if numtopics<>ch then exit
  256.       end;
  257.       if (ch<1) or (ch>numtopics) then begin
  258.         if input='?' then listtopics;
  259.         writestr ('Topic number [?=list]:');
  260.         ch:=0
  261.       end
  262.     until (ch>0) or hungupon;
  263.     opentopic (ch)
  264.   end;
  265.  
  266.   procedure deltopic;
  267.   var un,cnt:integer;
  268.       u:userrec;
  269.       f:file;
  270.       t:topicrec;
  271.       tn:lstr;
  272.   begin
  273.     tn:=' topic '+strr(curtopicnum)+' ('+curtopic.topicname+')';
  274.     writestr ('Delete topic '+tn+'? *');
  275.     if not yes then exit;
  276.     writelog (20,1,tn);
  277.     close (chfile);
  278.     erase (chfile);
  279.     cnt:=ioresult;
  280.     for cnt:=curtopicnum to numtopics-1 do begin
  281.       assign (f,votefn(cnt+1));
  282.       rename (f,votefn(cnt));
  283.       un:=ioresult;
  284.       seek (tofile,cnt);
  285.       read (tofile,t);
  286.       seek (tofile,cnt-1);
  287.       write (tofile,t)
  288.     end;
  289.     seek (tofile,numtopics-1);
  290.     truncate (tofile);
  291.     if curtopicnum<numtopics then begin
  292.       writeln ('Adjusting user voting record...');
  293.       writeurec;
  294.       for un:=1 to numusers do begin
  295.         seek (ufile,un);
  296.         read (ufile,u);
  297.         for cnt:=curtopicnum to numtopics do
  298.           u.voted[cnt]:=u.voted[cnt+1];
  299.         seek (ufile,un);
  300.         write (ufile,u)
  301.       end;
  302.       readurec
  303.     end;
  304.     if numtopics>0 then opentopic (1)
  305.   end;
  306.  
  307.   procedure removechoice;
  308.   var n:integer;
  309.       delled,c:choicerec;
  310.       cnt:integer;
  311.       u:userrec;
  312.   begin
  313.     n:=valu(copy(input,2,255));
  314.     if (n<1) or (n>numchoices) then n:=0;
  315.     while n=0 do begin
  316.       writestr (^M'Choice to delete [?=list]:');
  317.       n:=valu(input);
  318.       if n=0
  319.         then if input='?'
  320.           then listchoices
  321.           else exit
  322.     end;
  323.     if (n<1) or (n>numchoices) then exit;
  324.     seek (chfile,n-1);
  325.     read (chfile,delled);
  326.     for cnt:=n to numchoices-1 do begin
  327.       seek (chfile,cnt);
  328.       read (chfile,c);
  329.       seek (chfile,cnt-1);
  330.       write (chfile,c)
  331.     end;
  332.     seek (chfile,numchoices-1);
  333.     truncate (chfile);
  334.     curtopic.numvoted:=curtopic.numvoted-delled.numvoted;
  335.     writecurtopic;
  336.     write (^B^M'Choice deleted; updating user voting records...');
  337.     writeurec;
  338.     for cnt:=1 to numusers do begin
  339.       seek (ufile,cnt);
  340.       read (ufile,u);
  341.       u.voted[curtopicnum]:=0;
  342.       seek (ufile,cnt);
  343.       write (ufile,u)
  344.     end;
  345.     readurec;
  346.     writeln (^B'Done.')
  347.   end;
  348.  
  349.   procedure nexttopic;
  350.   begin
  351.     if curtopicnum=numtopics
  352.       then writeln ('No more topics!')
  353.       else opentopic (curtopicnum+1)
  354.   end;
  355.  
  356.   procedure voteonmandatory;
  357.   var n:integer;
  358.       t:topicrec;
  359.   begin
  360.     for n:=1 to numtopics do
  361.       if urec.voted[n]=0 then begin
  362.         seek (tofile,n-1);
  363.         read (tofile,t);
  364.         if t.mandatory then begin
  365.           opentopic (n);
  366.           clearbreak;
  367.           nobreak:=true;
  368.           writeln (^M'Mandatory voting topic: ',t.topicname,^M);
  369.           listchoices;
  370.           getvote (true);
  371.           if urec.voted[curtopicnum]<>0 then begin
  372.             writestr (^M'See results? *');
  373.             if yes then showresults
  374.           end
  375.         end
  376.       end
  377.   end;
  378.  
  379.   procedure sysopvoting;
  380.   var q,dum:integer;
  381.   begin
  382.     writelog (19,1,curtopic.topicname);
  383.     repeat
  384.       q:=menu ('Voting sysop','VSYSOP','QACDR');
  385.       if hungupon then exit;
  386.       case q of
  387.         2:addtopic;
  388.         3:dum:=addchoice;
  389.         4:deltopic;
  390.         5:removechoice;
  391.       end
  392.     until (q=1) or hungupon or (numtopics=0)
  393.   end;
  394.  
  395. var q:integer;
  396. label exit;
  397. begin
  398.   cursection:=votingsysop;
  399.   opentopicdir;
  400.   repeat
  401.     if numtopics=0 then begin
  402.       if getmandatory then goto exit;
  403.       writeln ('No voting topics right now!');
  404.       if not issysop
  405.         then goto exit
  406.         else
  407.           begin
  408.             writestr ('Make topic #1? *');
  409.             if yes
  410.               then addtopic
  411.               else goto exit
  412.           end
  413.     end
  414.   until (numtopics>0) or hungupon;
  415.   if hungupon then goto exit;
  416.   if getmandatory then begin
  417.     voteonmandatory;
  418.     goto exit
  419.   end;
  420.   opentopic (1);
  421.   writehdr ('The Voting Booths');
  422.   writeln ('Number of topics: ',numtopics);
  423.   repeat
  424.     writeln (^M'Active topic (',curtopicnum,'): ',curtopic.topicname);
  425.     q:=menu ('Voting','VOTING','QS_VLR#*H%@');
  426.     if hungupon then goto exit;
  427.     if q<0
  428.       then
  429.         begin
  430.           q:=-q;
  431.           if q<=numtopics then opentopic (q);
  432.           q:=0
  433.         end
  434.       else
  435.         case q of
  436.           2,8:selecttopic;
  437.           3:nexttopic;
  438.           4:getvote (false);
  439.           5:listchoices;
  440.           6:showresults;
  441.           9:help ('Voting.hlp');
  442.           10:sysopvoting
  443.         end
  444.   until (q=1) or hungupon or (numtopics=0);
  445.   if numtopics=0 then writeln (^B'No voting topics right now!');
  446.   exit:
  447.   close (tofile);
  448.   close (chfile)
  449. end;
  450.  
  451. begin
  452. end.
  453.  
  454.