作者 何书鹏
1 个管道 的构建 通过 耗费 4 秒

用户添加和编辑科目

... ... @@ -76,6 +76,87 @@ class User extends Backend
/**
* 添加
*/
// public function add()
// {
// if ($this->request->isPost()) {
// $params = $this->request->post("row/a");
// if ($params) {
// $params = $this->preExcludeFields($params);
// $params['status'] = 'normal';
//
// if(empty($params['expirationtime'])){
// $this->error('有效期不能为空');
// }
// $params['expirationtime'] = strtotime($params['expirationtime']);
// if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
// $params[$this->dataLimitField] = $this->auth->id;
// }
// if($this->check_identity($params['card'])) {
// $this->error('该身份证号已存在');
// }
// $study = [];
// if($params['study_id'] && $params['periodtime']) {
// $study = [
// 'study_id' => $params['study_id'],
// 'periodtime' => strtotime($params['periodtime'])
// ];
// unset($params['study_id']);
// unset($params['periodtime']);
// }
//// $result = $this->model->validate('User.add')->save($params);
//// if ($result === false) {
//// $this->error($this->model->getError());
//// }
// if (!empty($params['mobile'])) {
// if(\app\common\model\User::where('mobile', $params['mobile'])->find()){
// $this->error('注册手机号已存在');
// }
// $params['phone'] = $params['mobile'];
// }
// $result = false;
// Db::startTrans();
// try {
// //是否采用模型验证
// if ($this->modelValidate) {
// $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
// $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
// $this->model->validateFailException(true)->validate($validate);
// }
// $result = $this->model->allowField(true)->save($params);
// if($study) {
// $study['third_id'] = $this->model->id;
// $res['third_id'] = $study['third_id'];
// $res['study_id'] = $study['study_id'];
// $res['createtime'] = time();
// $res['playtime'] = 0;
// $classes_ids = Db::name('classes')->where('study_id',$res['study_id'])->where('is_major',1)->column('id');
// foreach ($classes_ids as $v){
// $res['class_id'] = $v;
// Db::name('study_class')->insertGetId($res);
// }
// Db::name('third_study')->insertGetId($study);
// }
// Db::commit();
// } catch (ValidateException $e) {
// Db::rollback();
// $this->error($e->getMessage());
// } catch (PDOException $e) {
// Db::rollback();
// $this->error($e->getMessage());
// } catch (Exception $e) {
// Db::rollback();
// $this->error($e->getMessage());
// }
// if ($result !== false) {
// $this->success();
// } else {
// $this->error(__('No rows were inserted'));
// }
// }
// $this->error(__('Parameter %s can not be empty', ''));
// }
// return $this->view->fetch();
// }
public function add()
{
if ($this->request->isPost()) {
... ... @@ -94,25 +175,20 @@ class User extends Backend
if($this->check_identity($params['card'])) {
$this->error('该身份证号已存在');
}
$study = [];
if($params['study_id'] && $params['periodtime']) {
$study = [
'study_id' => $params['study_id'],
'periodtime' => strtotime($params['periodtime'])
];
unset($params['study_id']);
unset($params['periodtime']);
}
// $result = $this->model->validate('User.add')->save($params);
// if ($result === false) {
// $this->error($this->model->getError());
// }
if (!empty($params['mobile'])) {
if(\app\common\model\User::where('mobile', $params['mobile'])->find()){
$this->error('注册手机号已存在');
}
$params['phone'] = $params['mobile'];
}
// 考试数据
$study_ids = [];
if($params['study_id'] && $params['periodtime']) {
$study_ids = explode(',',$params['study_id']);
$periodtime = strtotime($params['periodtime']);
}
$result = false;
Db::startTrans();
try {
... ... @@ -123,19 +199,27 @@ class User extends Backend
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
if($study) {
$study['third_id'] = $this->model->id;
$res['third_id'] = $study['third_id'];
$res['study_id'] = $study['study_id'];
$res['createtime'] = time();
$res['playtime'] = 0;
$classes_ids = Db::name('classes')->where('study_id',$res['study_id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $v){
$res['class_id'] = $v;
Db::name('study_class')->insertGetId($res);
// 考试数据-添加
foreach ($study_ids as $study_id){
$third_id = $this->model->id;
Db::name('third_study')->insertGetId([
'third_id' => $third_id,
'study_id' => $study_id,
'periodtime' => $periodtime,
]);
$classes_ids = Db::name('classes')->where('study_id',$study_id)->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
Db::name('study_class')->insertGetId([
'third_id' => $third_id,
'study_id' => $study_id,
'class_id' => $class_id,
'playtime' => 0,
'createtime' => time(),
]);
}
Db::name('third_study')->insertGetId($study);
}
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
... ... @@ -171,6 +255,69 @@ class User extends Backend
/**
* 编辑
*/
// public function edit($ids = null)
// {
// $row = $this->model->get($ids);
// if (!$row) {
// $this->error(__('No Results were found'));
// }
// $adminIds = $this->getDataLimitAdminIds();
// if (is_array($adminIds)) {
// if (!in_array($row[$this->dataLimitField], $adminIds)) {
// $this->error(__('You have no permission'));
// }
// }
// if ($this->request->isPost()) {
// $params = $this->request->post("row/a");
// if ($params) {
// if(empty($params['expirationtime'])){
// $this->error('有效期不能为空');
// }
// $params['expirationtime'] = strtotime($params['expirationtime']);
// if($this->check_identity($params['card'],$ids)) {
// $this->error('该身份证号已存在');
// }
//// if(strlen($params['username']) < 6 || strlen($params['username']) > 18) {
//// $this->error('用户名须为6-18位');
//// }
// if (!empty($params['mobile'])) {
// if(\app\common\model\User::where('mobile', $params['mobile'])->where('id', '<>', $row->id)->find()){
// $this->error('注册手机号已存在');
// }
// $params['phone'] = $params['mobile'];
// }
// $result = false;
// Db::startTrans();
// try {
// //是否采用模型验证
// if ($this->modelValidate) {
// $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
// $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
// $row->validateFailException(true)->validate($validate);
// }
// $result = $row->allowField(true)->save($params);
// Db::commit();
// } catch (ValidateException $e) {
// Db::rollback();
// $this->error($e->getMessage());
// } catch (PDOException $e) {
// Db::rollback();
// $this->error($e->getMessage());
// } catch (Exception $e) {
// Db::rollback();
// $this->error($e->getMessage());
// }
// if ($result !== false) {
// $this->success();
// } else {
// $this->error(__('No rows were updated'));
// }
// }
// $this->error(__('Parameter %s can not be empty', ''));
// }
// $this->view->assign("row", $row);
// return $this->view->fetch();
// }
public function edit($ids = null)
{
$row = $this->model->get($ids);
... ... @@ -193,15 +340,20 @@ class User extends Backend
if($this->check_identity($params['card'],$ids)) {
$this->error('该身份证号已存在');
}
// if(strlen($params['username']) < 6 || strlen($params['username']) > 18) {
// $this->error('用户名须为6-18位');
// }
if (!empty($params['mobile'])) {
if(\app\common\model\User::where('mobile', $params['mobile'])->where('id', '<>', $row->id)->find()){
$this->error('注册手机号已存在');
}
$params['phone'] = $params['mobile'];
}
// 考试数据
$study_ids = [];
if($params['study_id'] && $params['periodtime']) {
$study_ids = explode(',',$params['study_id']);
$periodtime = strtotime($params['periodtime']);
}
$result = false;
Db::startTrans();
try {
... ... @@ -212,6 +364,50 @@ class User extends Backend
$row->validateFailException(true)->validate($validate);
}
$result = $row->allowField(true)->save($params);
$third_id = $row['id'];
// 考试数据-编辑
foreach ($study_ids as $study_id){
$my_study = Db::name('third_study')->where('study_id',$study_id)->where('third_id',$third_id)->find();
if(!$my_study){
Db::name('third_study')->insertGetId([
'third_id' => $third_id,
'study_id' => $study_id,
'periodtime' => $periodtime,
]);
}else{
// 修改到期时间
Db::name('third_study')->where('id',$my_study['id'])->update(['periodtime'=>$periodtime]);
}
$classes_ids = Db::name('classes')->where('study_id',$study_id)->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
$my_class = Db::name('study_class')->where('class_id',$class_id)->where('third_id',$third_id)->find();
if(!$my_class){
Db::name('study_class')->insertGetId([
'third_id' => $third_id,
'study_id' => $study_id,
'class_id' => $class_id,
'playtime' => 0,
'createtime' => time(),
]);
}
}
}
// 删除去掉的科目
$my_study_ids = Db::name('third_study')->where('third_id',$third_id)->column('study_id');
if($my_study_ids){
$delete_study_ids = array_diff($my_study_ids,$study_ids);
Db::name('third_study')
->where('third_id',$third_id)
->where('study_id','in',$delete_study_ids)
->delete();
Db::name('study_class')
->where('third_id',$third_id)
->where('study_id','in',$delete_study_ids)
->delete();
}
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
... ... @@ -231,6 +427,13 @@ class User extends Backend
}
$this->error(__('Parameter %s can not be empty', ''));
}
// 我的科目
$my_study_list = Db::name('third_study')->where('third_id',$row['id'])->select();
$my_study_ids = array_column($my_study_list,'study_id');
$this->view->assign("study_id", $my_study_ids ? implode(',',$my_study_ids) : '');
$this->view->assign("periodtime", count($my_study_list) > 0 ? $my_study_list[0]['periodtime'] : time());
$this->view->assign("row", $row);
return $this->view->fetch();
}
... ...
... ... @@ -93,7 +93,7 @@
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Study_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-study_id" data-rule="" data-source="study/index" data-field="examname" class="form-control selectpage" name="row[study_id]" type="text" value="">
<input id="c-study_id" data-rule="" data-source="study/index" data-field="examname" data-multiple="true" class="form-control selectpage" name="row[study_id]" type="text" value="">
</div>
</div>
<div class="form-group">
... ...
... ... @@ -68,6 +68,18 @@
<ul class="row list-inline plupload-preview" id="p-image"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Study_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-study_id" data-rule="" data-source="study/index" data-field="examname" data-multiple="true" class="form-control selectpage" name="row[study_id]" type="text" value="{$study_id}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Periodtime')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-periodtime" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[periodtime]" type="text" value="{:date('Y-m-d H:i:s',$periodtime)}">
</div>
</div>
<!--<div class="form-group">
<label for="c-mobile" class="control-label col-xs-12 col-sm-2">{:__('手机号')}:</label>
<div class="col-xs-12 col-sm-8">
... ...