Skip to content
Snippets Groups Projects
Commit 6feaba56 authored by xamgore's avatar xamgore
Browse files

API origin

parent 4a4fe972
Branches
Tags
No related merge requests found
...@@ -185,6 +185,13 @@ Route::set('handler', 'handler/<controller>/<action>(/<id>)') ...@@ -185,6 +185,13 @@ Route::set('handler', 'handler/<controller>/<action>(/<id>)')
'directory' => 'handler' 'directory' => 'handler'
)); ));
// todo: we want an infinite param sequence
Route::set('api', 'api/<controller>/<action>(/<param1>)')
->defaults(array(
// 'action' => 'index',
'directory' => 'api',
));
Route::set('window', 'window/<id>') Route::set('window', 'window/<id>')
->defaults(array( ->defaults(array(
'controller' => 'Window', 'controller' => 'Window',
......
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Api_Discipline extends Controller_Handler {
public $data;
public function before() {
// todo: var $user should be defined here
// either by GET param (token),
// or on the session variables.
$this->setAccessLevel(self::ACCESS_USER);
parent::before();
}
public function after() {
$this->response->body(json_encode($this->data));
}
public function action_create() {
try {
$discipline = Model_Discipline::make()
->author ($this->user['TeacherID'])
->faculty ($this->post['facultyID'])
->subject ($this->post['subjectID'])
->grade ($this->post['gradeID'])
->lectures($this->post['lectures'])
->practice($this->post['practice'])
->labs ($this->post['labs'])
->type ($this->post['type'])
->create();
if ($this->post['bonus'] == "true")
Model_Map::AddModuleBonus($this->user['TeacherID'], $discipline->ID);
$this->data = ['success' => true, 'ID' => $discipline->ID];
} catch (Exception $e) {
$this->data = ['success' => false, 'message' => $e->getMessage()];
}
}
}
\ No newline at end of file
...@@ -15,47 +15,6 @@ class Controller_Handler_Map extends Controller_Handler { ...@@ -15,47 +15,6 @@ class Controller_Handler_Map extends Controller_Handler {
$result = $this->model->deleteSubmodule($this->user['TeacherID'], $SubmoduleID); $result = $this->model->deleteSubmodule($this->user['TeacherID'], $SubmoduleID);
return $result[0]['Num']; return $result[0]['Num'];
} }
// Добавление преподавателем дисциплины
public function action_AddDiscipline() {
$data['success'] = false;
$this->post -> rule('Grade', 'not_empty')
-> rule('Grade', 'digit')
-> rule('SubjectID', 'not_empty')
-> rule('SubjectID', 'digit')
-> rule('BonusRate', 'not_empty')
-> rule('ExamType', 'not_empty')
-> rule('LectureCount', 'not_empty')
-> rule('LectureCount', 'digit')
-> rule('PracticeCount', 'not_empty')
-> rule('PracticeCount', 'digit')
-> rule('LabCount', 'not_empty')
-> rule('LabCount', 'digit')
-> rule('FacultyID', 'not_empty')
-> rule('FacultyID', 'digit');
if ($this->post->check()) {
$result = Model_Discipline::create(
$this->user['TeacherID'],
$this->post['Grade'],
$this->post['SubjectID'],
$this->post['ExamType'],
$this->post['LectureCount'],
$this->post['PracticeCount'],
$this->post['LabCount'],
$this->post['FacultyID']
);
$data['DisciplineID'] = $result[0]['Num'];
if ($this->post['BonusRate'] == "true")
$result = $this->model->AddModuleBonus(
$this->user['TeacherID'],
$data['DisciplineID']
);
if ($data['DisciplineID'] > 0)
$data['success'] = true;
}
$this->response->body(json_encode($data));
}
// Удаление дисциплины // Удаление дисциплины
public function action_DeleteDiscipline() { public function action_DeleteDiscipline() {
......
<?php defined('SYSPATH') or die('No direct script access.'); <?php defined('SYSPATH') or die('No direct script access.');
/**
* Class Model_Discipline
*
* @property-read $ID int
*/
class Model_Discipline extends Model class Model_Discipline extends Model
{ {
private $data;
/**
* @param $associated bool
* Indicates whether object exists in db.
*/
function __construct(array & $data, $associated = true) {
$this->data = $data;
if (!$associated || !isset($data['ID']))
$this->create();
}
/**
* Creation of new discipline from raw data.
* @return Model_Helper_DisciplineBuilder
*/
static public function make() {
return new Model_Helper_DisciplineBuilder();
}
/**
* Load the discipline from database.
* @param $id int discipline id
* @return $this new instance
*/
static public function load($id) {
// todo: lazy loading
throw new LogicException('Not Implemented Yet');
return new self($data = [], true);
}
/**
* Create new discipline in db, based on $data.
*/
private function create() {
$sql = "SELECT `AddDiscipline`(authorID, gradeID, subjectID, type, lectures, practice, labs, facultyID, NULL) AS `Num`;";
$this->data['ID'] = DB::query(Database::SELECT, $sql)
->parameters($this->data)
->execute()[0]['Num'];
}
function __get($name) {
if ($name == 'ID') {
return $this->data['ID'];
}
throw new InvalidArgumentException;
}
/** /**
* @param $id int discipline id * @param $id int discipline id
* @return array data from <tt>view_disciplines</tt> table * @return array data from <tt>view_disciplines</tt> table
...@@ -17,24 +73,6 @@ class Model_Discipline extends Model ...@@ -17,24 +73,6 @@ class Model_Discipline extends Model
} }
/**
* Add new discipline.
*
* @param $teacher int teacher id
* @param $grade int 1-7
* @param $subject int subject id
* @param $examType string <tt>exam</tt> or <tt>credit</tt>
* @param $lectureCount int
* @param $practiceCount int
* @param $labCount int
* @param $department int department id
* @return Database_Result
*/
public static function create($teacher, $grade, $subject, $examType, $lectureCount, $practiceCount, $labCount, $department) {
$sql = "SELECT `AddDiscipline`('$teacher', '$grade', '$subject', '$examType', '$lectureCount', '$practiceCount', '$labCount', '$department', NULL) AS `Num`;";
return DB::query(Database::SELECT, $sql)->execute();
}
/** Change grade. */ /** Change grade. */
public static function changeGrade($DisciplineID, $teacherID, $Grade) { public static function changeGrade($DisciplineID, $teacherID, $Grade) {
$sql = "SELECT `ChangeDisciplineGrade`('$teacherID', '$DisciplineID', '$Grade') AS `Num`;"; $sql = "SELECT `ChangeDisciplineGrade`('$teacherID', '$DisciplineID', '$Grade') AS `Num`;";
......
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Helper_DisciplineBuilder extends Model
{
private $data;
public function create() {
if (count($this->data) != 8)
throw new ErrorException;
return new Model_Discipline($this->data, false);
}
function & author($id) {
if (!is_numeric($id) && $id <= 0)
throw new InvalidArgumentException();
$this->data['authorID'] = $id;
return $this;
}
function & grade($id) {
if (!is_numeric($id) && $id <= 0)
throw new InvalidArgumentException();
$this->data['gradeID'] = $id;
return $this;
}
function & faculty($id) {
if (!is_numeric($id) && $id <= 0)
throw new InvalidArgumentException();
$this->data['facultyID'] = $id;
return $this;
}
function & subject($id) {
if (!is_numeric($id) && $id <= 0)
throw new InvalidArgumentException();
$this->data['subjectID'] = $id;
return $this;
}
function & lectures($hours) {
if (!is_numeric($hours) && $hours < 0)
throw new InvalidArgumentException();
$this->data['lectures'] = $hours;
return $this;
}
function & practice($hours) {
if (!is_numeric($hours) && $hours < 0)
throw new InvalidArgumentException();
$this->data['practice'] = $hours;
return $this;
}
function & labs($hours) {
if (!is_numeric($hours) && $hours < 0)
throw new InvalidArgumentException();
$this->data['labs'] = $hours;
return $this;
}
function & type($name) {
if (!$name) // todo: enum
throw new InvalidArgumentException();
$this->data['type'] = $name;
return $this;
}
}
\ No newline at end of file
...@@ -34,22 +34,22 @@ $(function() { ...@@ -34,22 +34,22 @@ $(function() {
if (errCount === 0) { if (errCount === 0) {
$.post( $.post(
g_URLdir + "handler/map/AddDiscipline", g_URLdir + "api/discipline/create",
{ {
"Grade": gradeID, "gradeID": gradeID,
"SubjectID": subjectID, "subjectID": subjectID,
"BonusRate": bonusRate, "facultyID": $("select.SelectFaculty").val(),
"ExamType": examType, "lectures": $("input.InputLectureCount").val(),
"LectureCount": $("input.InputLectureCount").val(), "labs": $("input.InputLabCount").val(),
"LabCount": $("input.InputLabCount").val(), "practice": $("input.InputPracticeCount").val(),
"PracticeCount": $("input.InputPracticeCount").val(), "bonus": bonusRate,
"FacultyID": $("select.SelectFaculty").val() "type": examType
}, },
function(data) function(data)
{ {
data = $.parseJSON(data); data = $.parseJSON(data);
if(data.success === true) { if(data.success === true) {
setTimeout("location.replace('"+g_URLdir+"discipline/structure/"+data.DisciplineID+"')", 500); setTimeout("location.replace('"+g_URLdir+"discipline/structure/"+data.ID+"')", 500);
} else { } else {
jThis.removeAttr("disabled"); jThis.removeAttr("disabled");
EventInspector_ShowMsg("Ошибка при добавлении дисциплины", "error"); EventInspector_ShowMsg("Ошибка при добавлении дисциплины", "error");
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment