💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# actionGetTeacherTimetable D:\wamp\www\ketang.test\weike\protected\modules\xingke\controllers\TimetableController.php 1. 查找该老师的课表记录(根据当前教师用户的班级和科目查找) ``` $criteria = new CDbCriteria(); $criteria->addInCondition('t.fdClassID', $classIDs); $criteria->addInCondition('t.fdSubjectID', $subjectIDs); $res2 = TimetableService::factory()->getAttributesByObject(teaTimetable::model()->findAll($criteria)); ``` ![](https://img.kancloud.cn/63/f4/63f46d5723e638df19f13046a8e2a1a1_292x631.png) 2. 组装数组 ``` $arr = []; $week = TimeFormatUtils::get_week($start ? floor($start / 1000) : time(), 'Y-m-d'); for ($i = 1; $i <= 7; $i++) { $arr[$i]['weekID'] = $i; $arr[$i]['date'] = $week[$i - 1]['date']; $weekText = $week[$i - 1]['week']; $arr[$i]['week'] = $weekText; } ``` ![](https://img.kancloud.cn/0b/df/0bdf6a7528c157cad0eb8b842aa61984_304x782.png) 3. 遍历课表记录,组装每节课的信息到组装数组 ``` foreach ($res2 as $item) { $temp = []; $weekID = $item['fdWeekID']; $classIndex = $item['fdClassIndex']; $classID = $item['fdClassID']; $subjectID = $item['fdSubjectID']; $date = $arr[$weekID]['date']; //日期,如3-11 $dateFullName = date('Y-m-d', strtotime($date)); // 完成日期,如2019-03-11 // 检查是否是调课 ['type' => 1 和谁调换 2 被谁调换 3 和空白课调换] $adjust = TimetableService::factory()->getIsAdjustTimetable($termID, $classID, $dateFullName, $classIndex, $subjectID); // 课表信息:节次 $temp['classIndex'] = $classIndex; if ($adjust) { ... } else { // 课表信息:科目信息 $temp['subject']['id'] = $subjectID; $temp['subject']['title'] = Yii::app()->params['SUBJECT_ARR'][$subjectID]; $temp['subject']['isAdjust'] = 0; // 不是调课 // 课表信息:资源信息 $temp['resource'] = TimetableService::factory()->getAllTypesResourcesStatus($termID, $classID, $dateFullName, $classIndex); } // 课表信息:班级信息 $temp['class']['id'] = $classID; $class = wkeClass::model()->findByPk($classID); $temp['class']['title'] = $class->fdName; $temp['class']['gradeID'] = $class->fdGradeID; // 空白课调课情况 if ($adjust['type'] == 3) { ... } // 非空白课程调换 if (in_array($adjust['type'], [1, 2]) && !$isAdjust) { if ($adjust['type'] == 2) { $weekID = TimeFormatUtils::get_week_id($adjust['data']->fdDate); $classIndex = $adjust['data']->fdClassIndex; $temp['classIndex'] = $classIndex; $temp['subject']['id'] = $adjust['data']->fdSourceSubjectID; $temp['subject']['title'] = Yii::app()->params['SUBJECT_ARR'][$adjust['data']->fdSourceSubjectID]; $temp['subject']['isAdjust'] = 1; // 是调课*/ $dateFullName = $adjust['data']->fdDate; } elseif ($adjust['type'] == 1) { $weekID = TimeFormatUtils::get_week_id($adjust['data']->fdSourceDate); $classIndex = $adjust['data']->fdSourceClassIndex; $temp['classIndex'] = $classIndex; $temp['subject']['id'] = $adjust['data']->fdSubjectID; $temp['subject']['title'] = Yii::app()->params['SUBJECT_ARR'][$adjust['data']->fdSubjectID]; $temp['subject']['isAdjust'] = 1; // 是调课*/ $dateFullName = $adjust['data']->fdSourceDate; } // 调课后,资源访问 $temp['resource'] = TimetableService::factory()->getAllTypesResourcesStatus($termID, $classID, $dateFullName, $classIndex); } $arr[$weekID]['class'][$classIndex] = $temp; } ``` ![](https://img.kancloud.cn/2e/cc/2eccf6ddda5ee6579150582db669e5d6_304x427.png) 课表组装到数组后: ![](https://img.kancloud.cn/bb/14/bb1456d6715e9e7d5247c86ecbe7fc5a_695x787.png) 4. 按节次排序,修改数组索引 ``` ksort($arr); // 一维数组按照主键排序 foreach ($arr as $k => $v) { $temp = $v['class']; $temp && ksort($temp); $arr[$k]['class'] = $temp; // 二维数组按照主键排序 } // 去除 index主键 foreach ($arr as $k => $v) { $temp = []; $temp['weekID'] = $v['weekID']; $temp['date'] = $v['date']; $temp['week'] = $v['week']; $temp['class'] = $v['class'] ? array_values($v['class']) : null; krsort($temp); $arr[$k] = $temp; } ``` ![](https://img.kancloud.cn/58/16/58161dc41bc6a8d49f9fd6a0f22aa855_463x716.png)