Newer
Older
<?php defined('SYSPATH') or die('No direct script access.');
* @property-read $ID int
* @property $AuthorID int
* @property $GradeID int
* @property $GradeNum int
* @property $FacultyID int
* @property $FacultyName string
* @property $Degree string bachelor / master
* @property $SubjectID int
* @property $SubjectName string
* @property $SubjectAbbr string
* @property $SemesterID int
* @property $Lectures int
* @property $Practice int
* @property $Labs int
* @property $Type string
* @property $Subtype string
* @property $IsLocked bool
* @property $IsBonus bool
* @property $CompoundDiscID int
* @property $CompoundDiscName string
class Model_Discipline extends Model_Container
const EXAM = 'exam';
const CREDIT = 'credit';
const GRADING_CREDIT = 'grading_credit';
public function __get($name) {
if ($name === 'Faculty' && !isset($this->data[$name]))
$this->data[$name] = Model_Faculty::with(parent::__get('FacultyID'));
return parent::__get($name);
}
* @param $id int discipline id
* @return array data from <tt>view_disciplines</tt> table
* @throws HTTP_Exception if discipline does not exist
$info = DB::query(Database::SELECT, $sql)
->param(':id', $id)->execute();
if ($info->count() == 0)
throw new InvalidArgumentException(Error::DISCIPLINE_NOT_FOUND);
return $info->offsetGet(0);
}
/**
* @param $id int submodule id
* @return array data from <tt>view_disciplines</tt> table
* @throws HTTP_Exception if discipline does not exist
*/
protected function getRawDataBySubmoduleID($id) {
$sql = 'CALL `Discipline_GetInfoBySubmodule`(:id)';
$info = DB::query(Database::SELECT, $sql)
->param(':id', $id)->execute();
if ($info->count() == 0)
throw new InvalidArgumentException(Error::DISCIPLINE_NOT_FOUND);
return $info->offsetGet(0);
}
/**
* Creation of new discipline from raw data.
* @return Model_Helper_DisciplineBuilder
*/
static public function make() {
return new Model_Helper_DisciplineBuilder();
}
/**
* Create new discipline in db, based on $data.
*/
$sql = 'SELECT `Discipline_Create`(AuthorID, GradeID, SubjectID, Type, Lectures, Practice,
Labs, FacultyID, SemesterID, Subtype) AS `ID`';
$this->data[self::$ID_FIELD] = DB::query(Database::SELECT, $sql)
DB::query(Database::SELECT, $sql)->param(':id', $this->ID)->execute();
}
// todo: should return Model_Group[]
public function getGroups() {
/** Get groups with separately attached students. */
public function getAllGroups() {
$sql = 'CALL `GetGroupsForDisciplineAll`(:id)';
return DB::query(Database::SELECT, $sql)->param(':id', $this->ID)->execute();
}
/** @return Model_Student[] */
public function getStudents() {
return Model_Students::ofDiscipline($this);
}
// todo: should return Model_Teacher[]
public function getTeachers() {
$sql = 'CALL `GetTeachersForDiscipline`(:id)';
return DB::query(Database::SELECT, $sql)
->param(':id', $this->ID)->execute();
public function hasTeacher($teacherID) {
$sql = 'SELECT `InternalIsTeacherBound`(:teacher, :discipline) AS `res`';
return DB::query(Database::SELECT, $sql)
->param(':discipline', $this->ID)
->param(':teacher', $teacherID)
->execute()->get('res');
}
public function bind(Model_Teacher $teacher) {
if ($this->ID == $teacher->ID)
return;
$sql = 'SELECT `Discipline_BindTeacher`(:id, :teacher)';
DB::query(Database::SELECT, $sql)
->param(':teacher', $teacher->ID)
->execute();
}
public function unbind(Model_Teacher $teacher) {
if ($this->ID == $teacher->ID)
return;
$sql = 'SELECT `Discipline_UnbindTeacher`(:id, :teacher)';
DB::query(Database::SELECT, $sql)
->param(':teacher', $teacher->ID)
->execute();
}
/**
* Bind teacher and delegate him the discipline.
* @param Model_Teacher $teacher
*/
public function delegateTo(Model_Teacher $teacher) {
if ($this->ID == $teacher->ID)
return;
$sql = 'SELECT `Discipline_Delegate`(:id, :teacher)';
DB::query(Database::SELECT, $sql)
->param(':teacher', $teacher->ID)
public function changeGrade($teacherID, $grade) {
$sql = 'SELECT `ChangeDisciplineGrade`(:teacher, :discipline, :grade) AS `Num`';
return DB::query(Database::SELECT, $sql)
->parameters([
':teacher' => $teacherID,
* Time machine for discipline.
* @param $stage int number from 0 to 3
Andrew Rudenets
committed
if ($stage < 0 || $stage > 3)
throw new LogicException('Milestone argument is incorrect!');
$sql = 'SELECT `RestrictAfterMilestone`(:discipline, :milestone)';
':discipline' => $this->ID,
':milestone' => $stage,
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
public static function find($facultyID, $semesterID, $subjectID, $type) {
$sql = 'CALL `Discipline_Find`(:facultyID, :semesterID, :subjectID, :type)';
$data = DB::query(Database::SELECT, $sql)
->parameters([
':facultyID' => $facultyID,
':semesterID' => $semesterID,
':subjectID' => $subjectID,
':type' => $type,
])->execute();
$cnt = count($data);
if ($cnt > 1) {
throw new Database_Exception('There are '.$cnt.' disciplines were found, but only one was expected!');
}
elseif ($cnt == 1) {
return Model_Discipline::load($data[0]['ID']);
}
return null;
}
public function update() {
$sql = 'SELECT `Discipline_Update` (ID, AuthorID, GradeID, SubjectID, Type, Lectures, Practice,
Labs, FacultyID, SemesterID, Subtype) AS `Success`';
$res = DB::query(Database::SELECT, $sql)
->parameters($this->getRawData($this->ID))
->execute();
return $res['Success'];
}