Exemplo n.º 1
0
 public function import($department_id = null, $semester_id = null)
 {
     // 		debug($_FILES);
     $this->layout = 'ajax';
     //* 	extjs的上传 */
     if ($this->request->is('post')) {
         if ($_FILES['import']['error'] > 0) {
             $error = $_FILES['import']['error'];
             $response = array('success' => false, 'msg' => $error);
             echo json_encode($response);
         } else {
             $file_name = $_FILES['import']['name'];
             $file_type = $_FILES['import']['type'];
             $file_size = round($_FILES['import']['size'] / 1024, 2) . '  Kilo Bytes';
             $uploaddir = WWW_ROOT . "uploads/";
             //debug(WWW_ROOT);
             /*取时间戳为文件名*/
             $name = basename($_FILES['import']['name']);
             $name = $this->rm($name);
             //debug($name);
             $uploadfile = $uploaddir . $name;
             // 			/*取原名为文件名*/
             //$uploadfile = $uploaddir . basename($_FILES['import']['name']);
             /* 如果上传文件名有乱码 */
             /* $uploadfile=iconv("utf-8","GBK", $uploadfile); */
             if (move_uploaded_file($_FILES['import']['tmp_name'], $uploadfile)) {
                 // 					echo "File is valid, and was successfully uploaded.\n";}
                 //debug($uploadfile);
                 $data = $this->showXls($uploadfile, $department_id, $semester_id);
                 $dataLength = count($data);
                 // 										debug($data);
                 $elective_data = array();
                 foreach ($data as $value) {
                     $stu_number = $value['stu_number'];
                     $course_id = $value['course'];
                     $elective_data[$stu_number] = $course_id;
                     // 								debug($value);
                 }
                 // 							debug($elective_data);
                 $this->Elective->unbindModel(array('belongsTo' => array('CoursePlan', 'Course', 'Student')));
                 App::import('Controller', 'CoursePlans');
                 $CoursePlan = new CoursePlansController();
                 $coursepalns = $CoursePlan->listCoursePlans($department_id, $semester_id, null, 2, true);
                 foreach ($coursepalns as $courseplan) {
                     $courseplan_id[] = $courseplan['CoursePlan']['id'];
                 }
                 $Department = new Department();
                 $depts = $Department->read('dept_number', $department_id);
                 $options = array('conditions' => array('stu_number like' => $depts['Department']['dept_number'] . '%', 'course_plan_id' => $courseplan_id));
                 // 							debug($options);
                 $stu_electiv_namelist = $this->Elective->find('all', $options);
                 // 							debug($stu_electiv_namelist);
                 $num_of_success = 0;
                 $data = array();
                 foreach ($stu_electiv_namelist as $idx => $record) {
                     // 								debug($student_record);
                     $id = $record['Elective']['id'];
                     $stu_number = $record['Elective']['stu_number'];
                     $data[$idx]['id'] = $id;
                     $data[$idx]['stu_number'] = $stu_number;
                     if (isset($elective_data[$stu_number])) {
                         $data[$idx]['course_id'] = $elective_data[$stu_number];
                         $elective_data[$stu_number] == 0 ?: $num_of_success++;
                     }
                 }
                 // 							debug($data);
                 if ($this->Elective->saveAll($data)) {
                     // 						echo 'ok';
                     $success = true;
                 } else {
                     // 					echo 'no';
                     $success = false;
                 }
                 $response = array('success' => true, 'data' => array('name' => $file_name, 'size' => $file_size), 'msg' => '上传了' . $dataLength . '条记录,成功导入了' . $num_of_success . '条记录。');
                 echo json_encode($response);
             }
         }
     }
     die;
 }
Exemplo n.º 2
0
 public function elective($course_plan_id, $course_id = 0, $course_type, $department_id, $semester_id)
 {
     $Department = new Department();
     $success = false;
     $depts = $Department->read('dept_number', $department_id);
     // 	$depts=$Departments->getChildren($department_id);
     // 	$conditions['Student.stu_number LIKE']=$depts['Department']['dept_number'].'%';
     $options = array('conditions' => array('dept_number like' => $depts['Department']['dept_number'] . '%'));
     //debug($options);
     $Student = new Student();
     $students = $Student->find('all', $options);
     // 		debug($students);
     $data = array();
     foreach ($students as $student) {
         $data[] = array('stu_number' => $student['Student']['stu_number'], 'dept_number' => $student['Student']['dept_number'], 'course_plan_id' => $course_plan_id, 'course_type' => $course_type, 'course_id' => $course_id);
     }
     // 					debug($data);
     if ($data != array()) {
         $Elective = new Elective();
         $success = $Elective->saveAll($data);
     }
     // 	$dept_number=
     // 	$students=
     // 			die();
     return $success;
 }