/
questions.php
194 lines (168 loc) · 7.02 KB
/
questions.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\CourseBundle\Entity\CQuizQuestion;
use ChamiloSession as Session;
use Doctrine\Common\Collections\Criteria;
use Knp\Component\Pager\Paginator;
/**
* @package chamilo.admin
*/
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
$this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script();
Session::erase('objExercise');
Session::erase('objQuestion');
Session::erase('objAnswer');
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
$form = new FormValidator('admin_questions', 'get');
$form->addHeader(get_lang('Questions'));
$form->addText('id', get_lang('Id'), false);
$form->addText('title', get_lang('Title'), false);
$form->addText('description', get_lang('Description'), false);
$form->addHidden('form_sent', 1);
$form->addButtonSearch(get_lang('Search'));
$questions = [];
$pagination = '';
$formSent = isset($_REQUEST['form_sent']) ? (int) $_REQUEST['form_sent'] : 0;
$length = 20;
$questionCount = 0;
if ($formSent) {
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : '';
$description = isset($_REQUEST['description']) ? Security::remove_XSS($_REQUEST['description']) : '';
$title = isset($_REQUEST['title']) ? Security::remove_XSS($_REQUEST['title']) : '';
$page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
$em = Database::getManager();
$repo = $em->getRepository('ChamiloCourseBundle:CQuizQuestion');
$criteria = new Criteria();
if (!empty($id)) {
$criteria->where($criteria->expr()->eq('iid', $id));
}
if (!empty($description)) {
$criteria->orWhere($criteria->expr()->contains('description', "%$description%"));
}
if (!empty($title)) {
$criteria->orWhere($criteria->expr()->contains('question', "%$title%"));
}
$questions = $repo->matching($criteria);
if (empty($id)) {
$id = '';
}
$params = [
'id' => $id,
'title' => $title,
'description' => $description,
'form_sent' => 1,
];
$url = api_get_self().'?'.http_build_query($params);
$form->setDefaults($params);
$questionCount = count($questions);
$paginator = new Paginator();
$pagination = $paginator->paginate($questions, $page, $length);
$pagination->setItemNumberPerPage($length);
$pagination->setCurrentPageNumber($page);
$pagination->renderer = function ($data) use ($url) {
$render = '<ul class="pagination">';
for ($i = 1; $i <= $data['pageCount']; $i++) {
$page = (int) $i;
$pageContent = '<li><a href="'.$url.'&page='.$page.'">'.$page.'</a></li>';
if ($data['current'] == $page) {
$pageContent = '<li class="active"><a href="#" >'.$page.'</a></li>';
}
$render .= $pageContent;
}
$render .= '</ul>';
return $render;
};
if ($pagination) {
$url = api_get_path(WEB_CODE_PATH).'exercise/admin.php?';
$exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?';
/** @var CQuizQuestion $question */
foreach ($pagination as $question) {
$courseId = $question->getCId();
$courseInfo = api_get_course_info_by_id($courseId);
$courseCode = $courseInfo['code'];
$question->courseCode = $courseCode;
// Creating empty exercise
$exercise = new Exercise($courseId);
$questionObject = Question::read($question->getId(), $courseInfo);
ob_start();
ExerciseLib::showQuestion(
$exercise,
$question->getId(),
false,
null,
null,
false,
true,
false,
true,
true
);
$question->questionData = ob_get_contents();
$exerciseData = '';
$exerciseId = 0;
if (!empty($questionObject->exerciseList)) {
// Question exists in a valid exercise
$exerciseData .= get_lang('Exercises').'<br />';
foreach ($questionObject->exerciseList as $exerciseId) {
$exercise = new Exercise();
$exercise->course_id = $question->getCId();
$exercise->read($exerciseId);
$exerciseData .= $exercise->title.' ';
$exerciseData .= Display::url(
Display::return_icon('edit.png', get_lang('Edit')),
$url.http_build_query([
'cidReq' => $courseCode,
'id_session' => $exercise->sessionId,
'exerciseId' => $exerciseId,
'type' => $question->getType(),
'editQuestion' => $question->getId(),
])
);
}
$question->questionData .= '<br />'.$exerciseData;
} else {
// Question exists but it's orphan or it belongs to a deleted exercise
$question->questionData = Display::url(
Display::return_icon('edit.png', get_lang('Edit')),
$url.http_build_query([
'cidReq' => $courseCode,
'id_session' => $exercise->sessionId,
'exerciseId' => $exerciseId,
'type' => $question->getType(),
'editQuestion' => $question->getId(),
])
);
// This means the question is added in a deleted exercise
if ($questionObject->getCountExercise() > 0) {
$exerciseList = $questionObject->getExerciseListWhereQuestionExists();
if (!empty($exerciseList)) {
$question->questionData .= '<br />'.get_lang('Exercises').'<br />';
/** @var CQuiz $exercise */
foreach ($exerciseList as $exercise) {
$question->questionData .= $exercise->getTitle();
if ($exercise->getActive() == -1) {
$question->questionData .= '- ('.get_lang('ExerciseDeleted').') ';
}
$question->questionData .= '<br />';
}
}
} else {
// This question is orphan :(
$question->questionData .= ' '.get_lang('OrphanQuestion').'<br />';
}
}
ob_end_clean();
}
}
}
$formContent = $form->returnForm();
$tpl = new Template(get_lang('Questions'));
$tpl->assign('form', $formContent);
$tpl->assign('pagination', $pagination);
$tpl->assign('pagination_length', $length);
$tpl->assign('question_count', $questionCount);
$layout = $tpl->get_template('admin/questions.tpl');
$tpl->display($layout);