-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_category.php
161 lines (143 loc) · 6.35 KB
/
create_category.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
require_once('../../config.php');
require_once('components/modal.php');
require_login();
$context = context_system::instance();
$categorytype = required_param('categorytype', PARAM_TEXT);
$pagetype = "create_category";
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/moodle_survey/create_category.php'));
$PAGE->set_title(get_string('createsurveycategory', 'local_moodle_survey'));
$filters = get_filters($categorytype);
echo $OUTPUT->header();
$dbhelper = new \local_moodle_survey\model\survey();
$categories = $dbhelper->get_categories_by_filters($filters, $categorytype);
if (strlen($filters['createcategory']) || $filters['categoryid']) {
redirect(new moodle_url('/local/moodle_survey/create_category.php', ['categorytype' => $categorytype]));
}
echo generate_page_header($categorytype, $filters);
echo generate_filter_form($filters);
echo generate_category_table($filters, $categorytype, $categories);
echo add_dynamic_form_script();
require_once('includes/footer.php');
/**
* Generates the page header including the create survey button and heading.
*
* @return string HTML content for the page header.
*/
function generate_page_header($categorytype, $filters) {
global $PAGE;
$plusicon = new moodle_url('/local/moodle_survey/pix/plus-icon.svg');
$createbutton = html_writer::div(
html_writer::link(
'#',
html_writer::tag('img', '', ['src' => $plusicon, 'alt' => 'Icon', 'class' => 'plus-icon']) . ' ' . get_string('createcategory', 'local_moodle_survey'),
['class' => 'create-button', 'id' => 'open-modal']
),
'create-button-container'
);
if($categorytype == get_string('survey', 'local_moodle_survey')) {
$addcategorytitle = get_string('addsurveycategory', 'local_moodle_survey');
$categoryheading = 'Surveys / '. get_string('surveycategorypagetitle', 'local_moodle_survey');
} else {
$addcategorytitle = get_string('addquestioncategory', 'local_moodle_survey');
$categoryheading = 'Surveys / '. get_string('questioncategorypagetitle', 'local_moodle_survey');
}
$heading = html_writer::tag('span', $categoryheading, ['class' => 'page-title']);
$content = $heading . ' ' . $createbutton;
$modallabel = $addcategorytitle;
$modalurl = new moodle_url($PAGE->url, ['categorytype' => $categorytype]);
$modaldescription = html_writer::start_tag('form', ['method' => 'get', 'action' => $modalurl, 'id' => 'filter-form']) . html_writer::div(
html_writer::empty_tag('input', [
'type' => 'text',
'name' => 'createcategory',
'placeholder' => $addcategorytitle,
'class' => 'add-category-field'
]) .
html_writer::empty_tag('input', [
'type' => 'submit',
'value' => 'Add',
'class' => 'custom-action-btn add-category-btn'
])
, 'add-category-form'
);
$modal = generate_modal($modallabel, $modaldescription);
return html_writer::tag('div', $content . $modal, ['class' => 'survey-header']);
}
/**
* Generates the filter form for searching and filtering survey categories.
*
* @return string HTML content for the filter form.
*/
function generate_filter_form($filters) {
global $PAGE;
return html_writer::start_tag('form', ['method' => 'get', 'action' => $PAGE->url, 'id' => 'filter-form']) .
html_writer::start_div('filter-form d-flex justify-content-around') .
html_writer::empty_tag('input', ['type' => 'date', 'name' => 'createdon', 'placeholder' => get_string('createdat', 'local_moodle_survey'), 'class' => 'date-input category-filter']) .
html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'categorytype', 'value' => $filters['categorytype']]) .
html_writer::empty_tag('input', ['type' => 'text', 'name' => 'search', 'value' => $filters['search'], 'placeholder' => get_string('search', 'local_moodle_survey'), 'class' => 'search-input category-filter']) .
html_writer::end_div() .
html_writer::end_tag('form');
}
/**
* Generates the survey table with categories and actions.
*
* @return string HTML content for the survey table.
*/
function generate_category_table($filters, $categorytype, $categories) {
global $PAGE;
$table = new html_table();
$deleteicon = new moodle_url('/local/moodle_survey/pix/delete-icon.svg');
if(sizeof($categories) > 0) {
$table->head = [
get_string('category', 'local_moodle_survey'),
get_string('createdon', 'local_moodle_survey'),
get_string('action', 'local_moodle_survey'),
];
foreach ($categories as $category) {
$deleteurl = new moodle_url($PAGE->url, ['categoryid' => $category->id, 'categorytype' => $categorytype]);
$table->data[] = [
html_writer::link('#', $category->label),
date('Y-m-d', strtotime($category->created_at)),
html_writer::link($deleteurl,
html_writer::tag('img', '', ['src' => $deleteicon, 'alt' => 'Icon', 'class' => 'plus-icon'])
),
];
}
} else {
echo html_writer::tag('div', get_string('nocategoryfound', 'local_moodle_survey'), ['class' => 'alert alert-info']);
}
return html_writer::table($table);
}
/**
* Retrieves the filter parameters from the request.
*
* @return array
*/
function get_filters($categorytype) {
$search = optional_param('search', '', PARAM_RAW_TRIMMED);
$createdon = optional_param('createdon', '', PARAM_RAW_TRIMMED);
$createcategory = optional_param('createcategory', '', PARAM_RAW_TRIMMED);
$categoryid = optional_param('categoryid', '', PARAM_INT);
return [
'search' => $search,
'createdon' => $createdon,
'categorytype' => $categorytype,
'createcategory' => $createcategory,
'categoryid' => $categoryid
];
}
function add_dynamic_form_script() {
return html_writer::script("
document.addEventListener('DOMContentLoaded', function() {
var dateInput = document.querySelector('.date-input');
var form = document.getElementById('filter-form');
function submitForm() {
form.submit();
}
if (dateInput) {
dateInput.addEventListener('change', submitForm);
}
});
");
}