Newer
Older
class Controller_Api_V0_StudyPlan extends Controller_Handler_Api
{
private function normalizeDisciplinesData($disciplineData, $facultyID)
{
foreach ($disciplineData->teachers as $teacherData) {
if (empty($teacherData->hashSnils)) {
Log::instance()->add(Log::WARNING, '{0} {1}: {2}', array(
'{0}' => 'SYNC_DISCIPLINES',
'{1}' => 'Discipline with empty teacher\'s hash INILA',
$ID = Model_Teacher::withINILA($teacherData->hashSnils);
throw new InvalidArgumentException('Discipline without teachers');
$disciplineData->type = Model_Discipline::EXAM;
case 'Кандидатский экзамен':
$disciplineData->type = Model_Discipline::EXAM;
break;
$disciplineData->type = Model_Discipline::CREDIT;
case 'Дифференцированный зачет':
$disciplineData->type = Model_Discipline::GRADING_CREDIT;
break;
throw new InvalidArgumentException('Discipline has bad type');
if (!isset($disciplineData->subjectID) && isset($disciplineData->externalID)) {
$disciplineData->subjectID = Model_Subject::withExternalID($disciplineData->externalID, $disciplineData->name, NULL, $facultyID);
}
private function processDiscipline($disciplineData, $studyPlanID, $semesterID, $facultyID)
{
$discipline = Model_Discipline::find($studyPlanID, $semesterID, $disciplineData->subjectID, $disciplineData->type);
if (isset($discipline)) {
if (isset($disciplineData->gradeID)) {
$discipline->changeGradeUnsafe($disciplineData->gradeID);
}
throw new InvalidArgumentException('Cannot create discipline without teachers');
}
$discipline = Model_Discipline::make()
->author($disciplineData->teacherIDs[0])
->subject($disciplineData->subjectID)
->faculty($facultyID)
->create();
}
if (!empty($disciplineData->teacherIDs)) {
foreach ($disciplineData->teacherIDs as $teacherID) {
$teacher = Model_Teacher::with($teacherID);
$discipline->bind($teacher);
}
$doSyncGlobal = Kohana::$config->load('sync.syncs')['global'];
if ($doSyncGlobal) {
if (isset($discipline)) {
// global = 0 или 1
if ($disciplineData->global) {
$discipline->setInactive();
$discipline->setGlobal();
} else {
if ($discipline->isGlobal()) {
$discipline->setNotGlobal();
$discipline->setActive();
}
private function processStudyPlan($studyPlanData, $year, $semesterID, $facultyID)
{
if (!isset($studyPlanData->id) && isset($studyPlanData->externalID)) {
$studyPlan = Model_Plan::withExternalID($studyPlanData->externalID);
$studyPlan = Model_Plan::make()
->externalID($studyPlanData->externalID)
->year($year)
->faculty($facultyID)
->create();
$studyPlan->changeInfo($year, $facultyID);
} elseif (isset($studyPlanData->id)) {
$studyPlan = Model_Plan::load($studyPlanData->id);
} else {
throw new InvalidArgumentException('Study plan has no id or external id!');
$disciplineIDs = [];
foreach ($studyPlanData->disciplines as &$disciplineData) {
try {
$disciplineData = $this->normalizeDisciplinesData($disciplineData, $facultyID);
$disciplineID = $this->processDiscipline($disciplineData, $studyPlan->ID, $semesterID, $facultyID);
$studyPlan->bindDiscipline($disciplineID);
$disciplineIDs[] = $disciplineID;
} catch (Exception $e) {
Log::instance()->add(Log::WARNING, '{0} {1}: {2}', array(
'{0}' => 'SYNC_DISCIPLINES',
'{1}' => $e->getMessage(),
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @api {put} api/v0/studyPlan Add new study plan(s)
* @apiName Add new study plan
* @apiGroup Study plans
* @apiVersion 0.1.2
* @apiParam {String} token Api key
* @apiDescription This method accepts json body
* with list of study plans to add or update.
* each study plan includes list of disciplines inside
* @apiExample {curl} Example usage:
* curl --header "Content-Type: application/json" \
* --request POST \
* --data '{
* "faculty": "000001387",
* "year": "2018",
* "semester": "1",
* "plans": [
* {
* "externalID": "000133620",
* "disciplines": [
* {
* "externalID": "000145371",
* "name": "Иностранный язык 2 (английский)",
* "type": "Зачет",
* "teachers": [
* {
* "hashSnils": "E043D16D4A36F7C4306B0EDC1C82E0E1D6E5CFF6"
* },
* {
* "hashSnils": "435F6F584A5636D881CF085487157DAEA76FBC8E"
* },
* {
* "hashSnils": "653A81C0F2FD15983C5671A4020CCA545A403426"
* }
* ]
* }
* ]
* }
* ]
* }' \
* http://grade/~dev_rating/api/v0/studyPlan?token=osw839hfgh9a23hgfh92hasff232f2oasf
*/
public function action_put_index()
{
$year = (int)$data->year;
$semesterID = (int)Model_Semesters::find($year, (int)$data->semester)->ID;
$facultyID = Model_Faculties::getIdByExternalID($data->faculty);
foreach ($data->plans as $studyPlanData) {
$res[] = $this->processStudyPlan($studyPlanData, $year, $semesterID, $facultyID);
}
} catch (Exception $e) {
$this->badRequestError($e->getMessage());
}
return $res;
}
}