Student.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\educational;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use app\admin\model\educational\Teacher as TeacherModel;
  15. use app\admin\model\educational\Classes as ClassesModel;
  16. use app\admin\model\educational\Student as StudentModel;
  17. use app\admin\model\educational\ContactPhone as ContactPhoneModel;
  18. use app\admin\model\questions\TestPaper as TestPaperModel;
  19. use app\admin\model\download\DataDownloadBuy;
  20. use app\admin\model\questions\TestPaperObtain;
  21. use app\admin\model\user\User;
  22. use app\admin\model\special\Special;
  23. use app\admin\model\special\SpecialSource;
  24. use app\admin\model\special\SpecialBuy;
  25. use app\admin\model\merchant\Merchant;
  26. /**
  27. * 学员
  28. * Class Student
  29. */
  30. class Student extends AuthController
  31. {
  32. /**
  33. * 学员列表
  34. */
  35. public function index($cid = 0)
  36. {
  37. $this->assign(['cid' => $cid, 'classes' => ClassesModel::classesList()]);
  38. return $this->fetch();
  39. }
  40. /**
  41. * 获取学员列表
  42. */
  43. public function getStudentList($c_id = 0)
  44. {
  45. $where = parent::getMore([
  46. ['page', 1],
  47. ['limit', 20],
  48. ['cid', 0],
  49. ['title', ''],
  50. ]);
  51. if ($c_id > 0 && $where['cid'] == 0) $where['cid'] = $c_id;
  52. return Json::successlayui(StudentModel::getStudentLists($where));
  53. }
  54. /**
  55. * 班级列表
  56. */
  57. public function classesList()
  58. {
  59. $classes = ClassesModel::classesList();
  60. return Json::successful($classes);
  61. }
  62. /**
  63. * 快速编辑
  64. * @param string $field 字段名
  65. * @param int $id 修改的主键
  66. * @param string value 修改后的值
  67. * @return json
  68. */
  69. public function set_value($field = '', $id = '', $value = '')
  70. {
  71. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  72. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  73. $res = parent::getDataModification('student', $id, $field, $value);
  74. if ($res)
  75. return Json::successful('保存成功');
  76. else
  77. return Json::fail('保存失败');
  78. }
  79. /**添加/编辑
  80. * @param int $id
  81. * @return mixed
  82. */
  83. public function create($id = 0, $uid = 0)
  84. {
  85. $student = $id > 0 ? StudentModel::get($id) : [];
  86. if ($student && $id) $phone = ContactPhoneModel::contactPhoneList($id);
  87. else $phone = [];
  88. $this->assign(['id' => $id, 'uid' => $uid, 'student' => json_encode($student), 'phone' => json_encode($phone)]);
  89. return $this->fetch();
  90. }
  91. /**添加/编辑学员
  92. * @param int $id
  93. */
  94. public function save_add($id = 0)
  95. {
  96. $data = parent::postMore([
  97. ['name', ''],
  98. ['uid', 0],
  99. ['classes_id', 0],
  100. ['sex', 0],
  101. ['image', ''],
  102. ['province', ''],
  103. ['city', ''],
  104. ['district', ''],
  105. ['detail', ''],
  106. ['contact', ''],
  107. ['sort', 0]
  108. ]);
  109. if ($data['classes_id'] <= 0) return Json::fail('请选择所在班级');
  110. if (!$data['name']) return Json::fail('请输入学员名称');
  111. if (!$data['image']) return Json::fail('请选择学员头像');
  112. if (!$data['province'] || !$data['city'] || !$data['district'] || !$data['detail']) return Json::fail('请选择地址');
  113. $contact = json_decode($data['contact'], true);
  114. StudentModel::beginTrans();
  115. if ($id) {
  116. $res = StudentModel::edit($data, $id);
  117. $res1 = ContactPhoneModel::contactPhoneAdd($id, $contact);
  118. } else {
  119. $user = User::where('uid', $data['uid'])->field('identitys,nickname')->find();
  120. if ($user['identitys'] == 2) return Json::fail('该用户已是老师');
  121. $data['nickname'] = $user['nickname'];
  122. $data['add_time'] = time();
  123. $res = true;
  124. if (!StudentModel::be(['uid' => $data['uid'], 'classes_id' => $data['classes_id'], 'is_del' => 0])) {
  125. $upper_limit = ClassesModel::where('id', $data['classes_id'])->value('upper_limit');
  126. $count = StudentModel::where('classes_id', $data['classes_id'])->where('is_del', 0)->count();
  127. if (bcsub($count, $upper_limit, 0) >= 0) return Json::fail('班级学员数量已达上限,不能添加!');
  128. $id = StudentModel::insertGetId($data);
  129. $res1 = ContactPhoneModel::contactPhoneAdd($id, $contact);
  130. if ($id && $res1) User::edit(['identitys' => 1], $data['uid'], 'uid');
  131. } else {
  132. StudentModel::rollbackTrans();
  133. return Json::fail('该用户已在班级中');
  134. }
  135. }
  136. if ($res && $id && $res1) {
  137. StudentModel::commitTrans();
  138. return Json::successful('添加/编辑成功');
  139. } else {
  140. StudentModel::rollbackTrans();
  141. return Json::fail('添加/编辑失败');
  142. }
  143. }
  144. /**删除学员
  145. * @param int $id
  146. */
  147. public function delete($id = 0)
  148. {
  149. if (!$id) return Json::fail('参数错误');
  150. $student = StudentModel::get($id);
  151. if (!$student) return Json::fail('要删除的学员不存在');
  152. $res = parent::getDataModification('student', $id, 'is_del', 1);
  153. if ($res) {
  154. User::edit(['identitys' => 0], $student['uid'], 'uid');
  155. ContactPhoneModel::where('sid', $id)->delete();
  156. return Json::successful('删除成功');
  157. } else {
  158. return Json::fail('删除失败');
  159. }
  160. }
  161. /**给学员发送试卷
  162. * @param string $uid
  163. * @return mixed
  164. */
  165. public function send()
  166. {
  167. $mer_list = Merchant::getMerchantList();
  168. $this->assign(['mer_list' => $mer_list]);
  169. return $this->fetch();
  170. }
  171. /**
  172. *试卷列表
  173. */
  174. public function getTestPaperList()
  175. {
  176. $where = parent::getMore([
  177. ['page', 1],
  178. ['limit', 20],
  179. ['pid', ''],
  180. ['type', ''],
  181. ['is_show', 1],
  182. ['status', 1],
  183. ['mer_id', 0],
  184. ['title', '']
  185. ]);
  186. return Json::successlayui(TestPaperModel::sendTestPaperExercisesList($where));
  187. }
  188. /**发送试卷
  189. * @param $sid
  190. * @param $tid
  191. */
  192. public function sendTestPaper($sid, $tid)
  193. {
  194. if (!$sid || !$tid) return Json::fail('缺少参数无法赠送');
  195. $sid = explode(',', $sid);
  196. $tid = explode(',', $tid);
  197. foreach ($sid as $k => $item) {
  198. $res = TestPaperObtain::addsend($item, $tid);
  199. }
  200. if ($res) {
  201. return Json::successful('发送成功');
  202. } else {
  203. return Json::fail('发送失败');
  204. }
  205. }
  206. /**
  207. *移除已发送的试卷
  208. */
  209. public function removeTestPaper($uid, $test_id)
  210. {
  211. if (!$uid || !$test_id) return Json::fail('参数错误');
  212. $res = TestPaperObtain::where(['uid' => $uid, 'test_id' => $test_id])->update(['is_del' => 1]);
  213. if ($res) {
  214. return Json::successful('移除成功');
  215. } else {
  216. return Json::fail('移除失败');
  217. }
  218. }
  219. /**给学员发送课程
  220. * @param string $uid
  221. * @return mixed
  222. */
  223. public function special()
  224. {
  225. $mer_list = Merchant::getMerchantList();
  226. $this->assign([
  227. 'mer_list' => $mer_list
  228. ]);
  229. return $this->fetch();
  230. }
  231. /**发送专题
  232. * @param $sid
  233. * @param $tid
  234. */
  235. public function sendSpecial($uid, $tid)
  236. {
  237. if (!$uid || !$tid) return Json::fail('缺少参数无法赠送');
  238. $uid = explode(',', $uid);
  239. $tid = explode(',', $tid);
  240. $res = false;
  241. foreach ($uid as $k => $item) {
  242. foreach ($tid as $h => $value) {
  243. $res = $this->save_give($item, $value);
  244. }
  245. }
  246. if ($res) {
  247. return Json::successful('赠送成功');
  248. } else {
  249. return Json::fail('赠送失败');
  250. }
  251. }
  252. /**赠送专题
  253. * @param $uid
  254. * @param $special_id
  255. * @return bool|object
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. * @throws \think\exception\DbException
  259. */
  260. public function save_give($uid, $special_id)
  261. {
  262. if (!$uid || !$special_id) return false;
  263. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  264. if (SpecialBuy::be(['uid' => $uid, 'special_id' => $special_id, 'is_del' => 0, 'type' => 3])) return true;
  265. if ($special['type'] == SPECIAL_COLUMN) {
  266. $special_source = SpecialSource::getSpecialSource($special['id']);
  267. if ($special_source) {
  268. foreach ($special_source as $k => $v) {
  269. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  270. if ($task_special['is_show'] == 1) {
  271. SpecialBuy::setBuySpecial('', $uid, $v['source_id'], 3, $task_special['validity'], $special_id);
  272. }
  273. }
  274. }
  275. }
  276. $res = SpecialBuy::setBuySpecial('', $uid, $special_id, 3, $special['validity']);
  277. if ($res) {
  278. TestPaperObtain::setTestPaper('', $uid, $special_id, 3);
  279. DataDownloadBuy::setDataDownload('', $uid, $special_id, 2);
  280. }
  281. return $res;
  282. }
  283. /**已获得的课程
  284. * @param int $uid
  285. * @return mixed
  286. */
  287. public function special_list($uid = 0)
  288. {
  289. $this->assign('uid', $uid);
  290. return $this->fetch();
  291. }
  292. /**获取已获得课程
  293. * @param $uid
  294. */
  295. public function getUserSpecialList($uid)
  296. {
  297. $where = parent::getMore([
  298. ['page', 1],
  299. ['limit', 20]
  300. ]);
  301. $where['uid'] = $uid;
  302. $special_task = Special::getUserSpecialList($where);
  303. if (isset($special_task['data']) && $special_task['data']) {
  304. foreach ($special_task['data'] as $k => $v) {
  305. if (isset($where['is_light']) && $v['is_light'] && $v['type'] == 6) {
  306. $special_task['data'][$k]['types'] = lightTypeNmae($v['light_type']);
  307. } else {
  308. $special_task['data'][$k]['types'] = parent::specialTaskType($v['type']);
  309. }
  310. }
  311. }
  312. return Json::successlayui($special_task);
  313. }
  314. /**移除专题
  315. * @param int $id
  316. * @throws \think\exception\DbException
  317. */
  318. public function del_special_buy($uid = 0, $special_id = 0)
  319. {
  320. if (!$uid || !$special_id) return Json::fail('缺少参数');
  321. $type = Special::where(['id' => $special_id])->value('type');
  322. $res = SpecialBuy::where(['uid' => $uid, 'type' => 3, 'special_id' => $special_id])->update(['is_del' => 1]);
  323. if ($type == SPECIAL_COLUMN) {
  324. $res2 = SpecialBuy::where(['uid' => $uid, 'type' => 3, 'column_id' => $special_id])->update(['is_del' => 1]);
  325. $res = $res && $res2;
  326. }
  327. if ($res) {
  328. TestPaperObtain::delTestPaper('', $uid, $special_id, 3);
  329. DataDownloadBuy::delDataDownload('', $uid, $special_id, 2);
  330. return Json::successful('移除成功');
  331. } else
  332. return Json::fail('移除失败');
  333. }
  334. /**已获得试卷
  335. * @param int $uid
  336. * @return mixed
  337. */
  338. public function test_paper($uid = 0)
  339. {
  340. $this->assign('uid', $uid);
  341. return $this->fetch();
  342. }
  343. /**获取已获得试卷
  344. * @param $uid
  345. */
  346. public function getUserTestPaperList($uid)
  347. {
  348. $where = parent::getMore([
  349. ['page', 1],
  350. ['limit', 20]
  351. ]);
  352. $where['uid'] = $uid;
  353. return Json::successlayui(TestPaperModel::getUserTestPaperList($where));
  354. }
  355. /**获取用户
  356. * @return mixed
  357. */
  358. public function user()
  359. {
  360. return $this->fetch();
  361. }
  362. /**
  363. * 获取用户列表
  364. */
  365. public function user_list()
  366. {
  367. $where = parent::getMore([
  368. ['page', 1],
  369. ['limit', 20],
  370. ['nickname', ''],
  371. ['identitys', 1],
  372. ['order', '']
  373. ]);
  374. return Json::successlayui(User::add_teacher_user_list($where));
  375. }
  376. }