<?
require"ygl.php";
if(!isset($ses_user_id)||!isset($ses_user_psd)){errorview("对不起，你没有登陆！！");exit;}
$user_login_id=$ses_user_id;
$user_login_psd=$ses_user_psd;
read_user_zl($user_login_id);
if($userpass1!=$user_login_psd){errorview("非法登陆！");exit;}
if($classpage==""||$classpage=="list")classlist();
if($classpage=="addpage")addpage();
if($classpage=="add")add();
if($classpage=="classlist")classlist();
if($classpage=="join")joinclass();
$ynhead2="1";
####################
function classlist()
{
global $user_login_id,$page,$perpage;
$ynhead2="1";
$title="班级列表";
include"header.php";
if(file_exists("class/classlist.php"))
{
$classlist_file=file("class/classlist.php");
$size=count($classlist_file);
$ii=1; $j=$size;
while ($j>0) {
 $j=$j-$perpage;
}
print<<<eot
<table width="750" border="1" cellspacing="0" cellpadding="2" bordercolorlight="#B5E1F7" bordercolordark="#FFFFFF" align="center">
 <tr>
  <td colspan="6">
   <div align="right">共有 <font color=red>$size</font> 个班级</div>
  </td>
 </tr>
 <tr>
  <td width="130" bgcolor="#B5E1F7">
   <div align="center">学校所属省份、城市</div>
  </td>
  <td width="205" bgcolor="#B5E1F7">
   <div align="center">学校名称</div>
  </td>
  <td width="140" bgcolor="#B5E1F7">
   <div align="center">班级名称</div>
  </td>
  <td width="100" bgcolor="#B5E1F7">
   <div align="center">创办人</div>
  </td>
  <td width="75" bgcolor="#B5E1F7">
   <div align="center">创办时间</div>
  </td>
  <td width="60" bgcolor="#B5E1F7">
   <div align="center">加入该班</div>
  </td>
 </tr>
eot;
if ($page == "") {$page=$ii;}
$pages=$page-1;
$pages=$page*$perpage-$perpage;
for($i=$pages; $i<$pages+$perpage; $i++) {
 $linea=$classlist_file[$i];
 $linea=ereg_replace("\n", "", $linea);
 if ($linea != "") {
 $lines1a=file("class/classlist.php");
 while (list($tmpkey, $line1a) = each($lines1a))
  {
  $line1=ereg_replace("\n", "", $line1);
  list($classid,$usercount)=split("\t",$linea);
//   list($to,$cname,$schoolfrom,$schoolcity,$schoolname,$classname,$add_day,$admin1,$admin2)=split("\t",$line1);
   }
$lines = file("class/$classid/config.php");
while (list($tmpkey,$line) = each($lines))
   {
   $line=ereg_replace("\n","",$line);
   list($to,$cname,$schoolfrom,$schoolcity,$schoolname,$classname,$add_day,$admin1,$admin2,$admin3)=split("\t",$line);
   }
print"
<tr><td width=130 align=center>$schoolfrom&nbsp;$schoolcity</td><td width=205 align=center>$schoolname</td>
<td width=140 align=center>$classname</td><td width=100 align=center>$cname</td>
<td width=75 align=center>$add_day</td><td width=60 align=center><a href=addclass.php?classpage=join&join_id=$user_login_id&class_id=$classid>加入该班</a>
</td></tr>";
}
}
print"<tr><td colspan=7 align=right>";
 $j=$size;
while ($j>0) {
 if ($page != $ii) {print "<a href=addclass.php?classpage=classlist&page=$ii>";};
 print "[$ii]";
 if ($page != $ii) {print "</a>";};
 $j=$j-$perpage;
 $ii++;
}
print"</td></tr></table>";
}
include"footer.php";

 }
###################
function add()
{
global $schoolfrom,$schoolcity,$schoolname,$classname,$user_login_id,$schoolcity,$usermail;
if ($schoolfrom=="请选择"||empty($schoolcity)||empty($schoolname)||empty($classname))
         {
         errorview("请认真填写好必需要填的资料！！");
         exit;
         }
$class_add_date=date("Y/n/j");
$classid=date("YmjHis");
$oldlist=read_file("class/classlist.php");
                                $oldlist=explode("\n",$oldlist);
                                $listcount=count($oldlist);
                                for ($j=0;$j<$listcount;$j++)
                                        {
                                        $listdetail=explode("\t",$oldlist[$j]);
                                        }
                                $oldlist=implode("\n",$oldlist);
                                $new_class[0]=$classid;
                                $new_class[1]="1";
                                $newlist=implode("\t",$new_class)."\n".$oldlist;
                                write_file("class/classlist.php",$newlist);
mkdir("class/$classid",0777) or die("create class error");
$fp5=fopen("class/$classid/gbook.gb", "w+");
fclose($fp5);
mkdir("class/$classid/msg",0777) or die("create class error");
mkdir("class/$classid/photo",0777) or die("create class error");
$fp8=fopen("class/$classid/photo/list", "a+");
fclose($fp8);
mkdir("class/$classid/read",0777) or die("create class error");
$fp7=fopen("class/$classid/read/list", "a+");
fclose($fp7);
$fp1=fopen("class/$classid/list", "a+");
         $writeuser=$user_login_id."\t".$class_add_date."\t0\n";
         fwrite($fp1,"$writeuser");
         fclose($fp1);
$fp3=fopen("class/$classid/news", "a+");
         fwrite($fp3,"欢迎光临我们这一班！\n");
         fclose($fp3);
$fp4=fopen("class/$classid/maillist", "w+");
if(!empty($usermail))fwrite($fp4,$usermail."\n");else fwrite($fp4,"");
fclose($fp4);
$fp5=fopen("class/$classid/online", "w+");
fclose($fp5);
########
$oldlist=read_file("user/$user_login_id.cla");
$oldlist=explode("\n",$oldlist);
$oldlist=implode("\n",$oldlist);
$new_line[0]=$classid;
$new_line[1]=$class_add_date;
$new_line[2]="0";
$newlist=implode("\t",$new_line)."\n".$oldlist;
write_file("user/$user_login_id.cla",$newlist);
##############
$admin1="未定";
$admin2="未定";
$admin3="未定";
$fp2=fopen("class/$classid/config.php", "a+");
         $writedata="<?exit;\t".$user_login_id."\t".$schoolfrom."\t".$schoolcity."\t".$schoolname."\t".$classname."\t".$class_add_date."\t".$admin1."\t".$admin2."\t".$admin3."\n";
         fwrite($fp2,"$writedata");
         fclose($fp2);
$ynrefresh="1";
$gopage="login.php";
$ynhead2="1";
$title="建立班级成功";
include"header.php";
print<<<eot
<table width="500" border="1" cellspacing="0" cellpadding="4"  bordercolorlight="#B5E1F7" bordercolordark="#FFFFFF" align="center">
  <tr>
    <td height="70">
      <div align="left"><a href="login.php">&gt;&gt;点击返回同学录</a></div>
    </td>
  </tr>
</table>
eot;
include"footer.php";
}

###################
function addpage()
{
$ynhead2="1";
$title="建立班级";
include"header.php";
print<<<EOT
<form name="form1" method="post" action="addclass.php">
 <input type="hidden" name="classpage" value="add">
  <table width="500" border="1"  bordercolorlight="#B5E1F7" bordercolordark="#FFFFFF" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="3">
        <div align="center"><font size="3" color="#000099"><b>请输入学校资料</b></font></div>
      </td>
    </tr>
    <tr>
      <td width="114">学校所在省份</td>
      <td width="226">
        <select name=schoolfrom size=1>
          <option>请选择</option>
          <option VALUE="北京">北京</option>
          <option VALUE="安徽">安徽</option>
          <option VALUE="重庆">重庆</option>
          <option VALUE="福建">福建</option>
          <option VALUE="甘肃">甘肃</option>
          <option VALUE="广东">广东</option>
          <option VALUE="广西">广西</option>
          <option VALUE="贵州">贵州</option>
          <option VALUE="海南">海南</option>
          <option VALUE="河北">河北</option>
          <option VALUE="黑龙江">黑龙江</option>
          <option VALUE="河南">河南</option>
          <option VALUE="湖北">湖北</option>
          <option VALUE="湖南">湖南</option>
          <option VALUE="内蒙古">内蒙古</option>
          <option VALUE="江苏">江苏</option>
          <option VALUE="江西">江西</option>
          <option VALUE="吉林">吉林</option>
          <option VALUE="辽宁">辽宁</option>
          <option VALUE="宁夏">宁夏</option>
          <option VALUE="青海">青海</option>
          <option VALUE="山西">山西</option>
          <option VALUE="陕西">陕西</option>
          <option VALUE="山东">山东</option>
          <option VALUE="上海">上海</option>
          <option VALUE="四川">四川</option>
          <option VALUE="天津">天津</option>
          <option VALUE="西藏">西藏</option>
          <option VALUE="新疆">新疆</option>
          <option VALUE="云南">云南</option>
          <option VALUE="浙江">浙江</option>
          <option VALUE="其它">其他</option>
        </select>
      </td>
      <td rowspan="4" width="140"><font color="#FF0000">注意：该资料一定要真实，否则其它同学找不到你创建的班级</font></td>
    </tr>
    <tr>
      <td width="114">学校所在城市</td>
      <td width="226">
        <input type="text" name="schoolcity" class=text>
      </td>
    </tr>
    <tr>
      <td width="114">学校名称</td>
      <td width="226">
        <input type="text" name="schoolname" class=text>
      </td>
    </tr>
    <tr>
      <td width="114">要建立的班级名称</td>
      <td width="226">
        <input type="text" name="classname" class=text>
      </td>
    </tr>
    <tr>
      <td colspan="3"><font color="#FF0000">注意：若乱建立班级会被系统管理员删掉！</font></td>
    </tr>
    <tr>
      <td colspan="3">
        <div align="center">
          <input type="submit" name="Submit" value="建  立" class=text>
          <input type="submit" name="Submit2" value="重  置" class=text>
        </div>
      </td>
    </tr>
  </table>
</form>
EOT;
include"footer.php";
}
###########
function joinclass()
{
global $user_login_id,$class_id,$todate,$usermail,$stemp;
 if (empty($user_login_id)||empty($class_id))
     {
     errorview("请从新登陆！");
     exit;
     }
     $login_count="0";
     $oldlist=read_file("user/$user_login_id.cla");
     $oldlist=explode("\n",$oldlist);
     $listcount=count($oldlist);
     for ($j=0;$j<$listcount;$j++)
         {
         $listdetail=explode("\t",$oldlist[$j]);
         if($listdetail[0]==$class_id){errorview("你已加入了该班级，请不要重复加入！");exit;}
         }
     $oldlist=implode("\n",$oldlist);
     $new_line[0]=$class_id;
     $new_line[1]=$todate;
     $new_line[2]=$login_count;
     $newlist=implode("\t",$new_line)."\n".$oldlist;
     //$newlist=push_heldtopics($newlist);
     write_file("user/$user_login_id.cla",$newlist); //ok, write to the list
############
$fp6=fopen("class/$class_id/msg/$user_login_id.rec", "a+");
fwrite($fp6,$cuser_id."|欢迎光临我们这一班！|".$stemp."|在使用上有什么不明白就查看同学录的帮助文件！|0|\n");
fclose($fp6);
if(!empty($usermail))
    {
    $fp5=fopen("class/$class_id/maillist","a+");
    fwrite($fp5,$usermail."\n");
    fclose($fp5);
    }
############
     $oldlist=read_file("class/$class_id/list");
     $oldlist=explode("\n",$oldlist);
     $oldlist=implode("\n",$oldlist);
     $new_line[0]=$user_login_id;
     $new_line[1]=$todate;
     $new_line[2]=$login_count;
     $newlist=implode("\t",$new_line)."\n".$oldlist;
     write_file("class/$class_id/list",$newlist);
if (file_exists("class/classlist.php"))
   {
   $classlistdata=file("class/classlist.php");
   $listcount=count($classlistdata);
   }
for ($i=0; $i<$listcount; $i++)
     {
     $detail=explode("\t",$classlistdata[$i]);
     if ($detail[0]==$class_id)
         {
         $detail[1]=$detail[1]+1;
         $detail[1]=$detail[1]."\n";
         $new[]=implode("\t",$detail);
         } else $new[]=$classlistdata[$i];
     }
$new=implode("",$new);
writetofile("class/classlist.php",$new);
$ynrefresh="1";
$gopage="login.php";
$ynhead2="1";
$title="加入班级成功";
include"header.php";
print<<<eot
<table width="500" border="1" cellspacing="0" cellpadding="4"  bordercolorlight="#B5E1F7" bordercolordark="#FFFFFF" align="center">
  <tr>
    <td height="70">
      <div align="left"><a href="login.php">&gt;&gt;加入班级成功，点击返回同学录</a></div>
    </td>
  </tr>
</table>
eot;
include"footer.php";
}


?>