-
Notifications
You must be signed in to change notification settings - Fork 988
/
GroupsController.php
75 lines (69 loc) · 2.5 KB
/
GroupsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace ls\controllers;
use \Yii;
class GroupsController extends Controller {
public $layout = 'survey';
public function actionView($id) {
$this->menus['group'] = $group = $this->loadModel($id);
$this->menus['survey'] = $group->survey;
return $this->render('view', ['group' => $group]);
}
/**
* Delete a group.
* @todo Access check
* @todo Method check, delete should always done via HTTP DELETE or DELETE over POST.
* @param $id
*/
public function actionDelete($id)
{
$group = $this->loadModel($id);
if (isset($group) && $group->questionCount == 0 && $group->delete()) {
App()->user->setFlash('success', "Group deleted.");
}
if (isset($group)) {
$this->redirect(["surveys/update", "id" => $group->sid]);
} else {
$this->redirect('surveys/index');
}
}
/**
* @todo Access check
* @param int $surveyId
* @throws \CHttpException
*
*/
public function actionCreate($surveyId) {
/**
* @todo Switch to findByPk after language has been removed from group table.
*/
$this->menus['survey'] = $survey = \Survey::model()->findByPk($surveyId);
if (!isset($survey)) {
throw new \CHttpException(404, "Survey not found.");
} elseif ($survey->isActive) {
throw new \CHttpException(421, "Cannot add groups to active survey.");
}
$group = new \QuestionGroup();
$group->sid = $survey->primaryKey;
if (App()->request->isPostRequest) {
$group->setAttributes(App()->request->getPost('QuestionGroup'));
if ($group->save()) {
$this->redirect(['groups/update', 'id' => $group->primaryKey]);
}
} else {
$lastTitle = ([] != $values = array_values($group->survey->groups)) ? $values[count($group->survey->groups) - 1]->group_name : "q0";
if (isset($lastTitle) && preg_match('/^(.*?)(\d+)$/', $lastTitle, $matches)) {
$group->group_name = $matches[1] . ($matches[2] + 1);
}
}
$this->render('create', ['group' => $group]);
}
public function actionUpdate($id) {
$this->menus['group'] = $group = $this->loadModel($id);
$this->menus['survey'] = $group->survey;
$this->render('update', ['group' => $group]);
}
protected function loadModel($id)
{
return \QuestionGroup::model()->findByPk($id);
}
}