gradebook_list.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\GradebookCategory;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Knp\Component\Pager\Paginator;
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. api_protect_admin_script();
  8. $allow = api_get_configuration_value('gradebook_dependency');
  9. if ($allow == false) {
  10. api_not_allowed(true);
  11. }
  12. $em = Database::getManager();
  13. $repo = $em->getRepository('ChamiloCoreBundle:GradebookCategory');
  14. $maxItems = 20;
  15. $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
  16. $categoryId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 1;
  17. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  18. $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
  19. if (empty($keyword)) {
  20. $gradeBookList = $repo->findAll();
  21. } else {
  22. $criteria = new Criteria();
  23. $criteria->where(
  24. Criteria::expr()->orX(
  25. Criteria::expr()->contains('courseCode', $keyword),
  26. Criteria::expr()->contains('name', $keyword)
  27. )
  28. );
  29. $gradeBookList = $repo->matching($criteria);
  30. }
  31. $currentUrl = api_get_self().'?';
  32. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  33. $contentForm = '';
  34. $toolbar = Display::url(
  35. Display::return_icon('add.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
  36. $currentUrl.'&action=add'
  37. );
  38. $toolName = get_lang('Gradebook');
  39. switch ($action) {
  40. case 'add':
  41. case 'edit':
  42. $interbreadcrumb[] = [
  43. 'url' => $currentUrl,
  44. 'name' => get_lang('Gradebook'),
  45. ];
  46. $toolName = get_lang(ucfirst($action));
  47. break;
  48. }
  49. $tpl = new Template($toolName);
  50. switch ($action) {
  51. case 'add':
  52. $toolbar = Display::url(
  53. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  54. $currentUrl
  55. );
  56. $form = new FormValidator(
  57. 'category_add',
  58. 'post',
  59. $currentUrl.'&action=add'
  60. );
  61. $form->addText('name', get_lang('Name'));
  62. $form->addText('weight', get_lang('Weight'));
  63. $form->addSelectAjax(
  64. 'course_id',
  65. get_lang('Course'),
  66. null,
  67. [
  68. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
  69. ]
  70. );
  71. $form->addSelectAjax(
  72. 'depends',
  73. get_lang('DependsOnGradebook'),
  74. null,
  75. [
  76. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
  77. 'multiple' => 'multiple',
  78. ]
  79. );
  80. $form->addText(
  81. 'gradebooks_to_validate_in_dependence',
  82. get_lang('NumberOfGradebookToValidateInDependence')
  83. );
  84. $form->addText(
  85. 'minimum',
  86. get_lang('MinimumGradebookToValidate'),
  87. false
  88. );
  89. $form->addButtonSave(get_lang('Add'));
  90. $contentForm = $form->returnForm();
  91. if ($form->validate()) {
  92. $values = $form->getSubmitValues();
  93. $courseId = isset($values['course_id']) ? $values['course_id'] : 0;
  94. $courseInfo = api_get_course_info_by_id($courseId);
  95. $courseCode = $courseInfo['code'];
  96. $criteria = ['courseCode' => $courseCode];
  97. $exists = $repo->findBy($criteria);
  98. if (empty($exists) || empty($courseId)) {
  99. if (empty($courseId)) {
  100. $courseCode = '';
  101. }
  102. $category = new GradebookCategory();
  103. $category
  104. ->setName($values['name'])
  105. ->setWeight($values['weight'])
  106. ->setVisible(1)
  107. ->setLocked(0)
  108. ->setGenerateCertificates(0)
  109. ->setIsRequirement(false)
  110. ->setCourseCode($courseCode)
  111. ->setUserId(api_get_user_id());
  112. $em->persist($category);
  113. $em->flush();
  114. if ($category->getId()) {
  115. $params = [];
  116. if (!empty($values['depends'])) {
  117. $depends = $values['depends'];
  118. $depends = array_map('intval', $depends);
  119. $value = serialize($depends);
  120. $params['depends'] = $value;
  121. }
  122. if (!empty($values['minimum'])) {
  123. $params['minimum_to_validate'] = (int) $values['minimum'];
  124. }
  125. if (!empty($values['gradebooks_to_validate_in_dependence'])) {
  126. $params['gradebooks_to_validate_in_dependence'] = (int) $values['gradebooks_to_validate_in_dependence'];
  127. }
  128. if (!empty($params)) {
  129. Database::update(
  130. $table,
  131. $params,
  132. ['id = ?' => $category->getId()]
  133. );
  134. }
  135. Display::addFlash(Display::return_message(get_lang('Added')));
  136. header('Location: '.$currentUrl);
  137. exit;
  138. }
  139. } else {
  140. Display::addFlash(Display::return_message(get_lang('CategoryExists')));
  141. }
  142. }
  143. break;
  144. case 'edit':
  145. $toolbar = Display::url(
  146. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  147. $currentUrl
  148. );
  149. /** @var GradebookCategory $category */
  150. $category = $repo->find($categoryId);
  151. if (!empty($category)) {
  152. $form = new FormValidator(
  153. 'category_edit',
  154. 'post',
  155. $currentUrl.'&action=edit&id='.$categoryId
  156. );
  157. $form->addText('name', get_lang('Name'));
  158. $form->addText('weight', get_lang('Weight'));
  159. $form->addLabel(get_lang('Course'), $category->getCourseCode());
  160. $sql = "SELECT
  161. depends,
  162. minimum_to_validate,
  163. gradebooks_to_validate_in_dependence
  164. FROM $table WHERE id = ".$categoryId;
  165. $result = Database::query($sql);
  166. $categoryData = Database::fetch_array($result, 'ASSOC');
  167. $options = [];
  168. if (!empty($categoryData['depends'])) {
  169. $list = UnserializeApi::unserialize('not_allowed_classes', $categoryData['depends']);
  170. foreach ($list as $itemId) {
  171. $courseInfo = api_get_course_info_by_id($itemId);
  172. $options[$itemId] = $courseInfo['name'];
  173. }
  174. }
  175. $form->addSelectAjax(
  176. 'depends',
  177. get_lang('DependsOnGradebook'),
  178. $options,
  179. [
  180. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
  181. 'multiple' => 'multiple',
  182. ]
  183. );
  184. $form->addText(
  185. 'gradebooks_to_validate_in_dependence',
  186. get_lang('NumberOfGradebookToValidateInDependence')
  187. );
  188. $form->addText(
  189. 'minimum',
  190. get_lang('MinimumGradebookToValidate'),
  191. false
  192. );
  193. $form->addButtonSave(get_lang('Edit'));
  194. $defaults = [
  195. 'name' => $category->getName(),
  196. 'weight' => $category->getWeight(),
  197. 'gradebooks_to_validate_in_dependence' => $categoryData['gradebooks_to_validate_in_dependence'],
  198. 'depends' => array_keys($options),
  199. 'minimum' => $categoryData['minimum_to_validate'],
  200. ];
  201. $form->setDefaults($defaults);
  202. $contentForm = $form->returnForm();
  203. if ($form->validate()) {
  204. $values = $form->getSubmitValues();
  205. $category->setName($values['name']);
  206. $category->setWeight($values['weight']);
  207. $em->merge($category);
  208. $em->flush();
  209. if (!empty($values['depends'])) {
  210. $depends = $values['depends'];
  211. $depends = array_map('intval', $depends);
  212. $value = serialize($depends);
  213. $params['depends'] = $value;
  214. }
  215. if (!empty($values['minimum'])) {
  216. $params['minimum_to_validate'] = (int) $values['minimum'];
  217. }
  218. if (!empty($values['gradebooks_to_validate_in_dependence'])) {
  219. $params['gradebooks_to_validate_in_dependence'] = (int) $values['gradebooks_to_validate_in_dependence'];
  220. }
  221. if (!empty($params)) {
  222. Database::update(
  223. $table,
  224. $params,
  225. ['id = ?' => $category->getId()]
  226. );
  227. }
  228. Display::addFlash(Display::return_message(get_lang('Updated')));
  229. header('Location: '.$currentUrl);
  230. exit;
  231. }
  232. }
  233. break;
  234. case 'list':
  235. default:
  236. $paginator = new Paginator();
  237. $pagination = $paginator->paginate(
  238. $gradeBookList,
  239. $page,
  240. $maxItems
  241. );
  242. // pagination.tpl needs current_url with out "page" param
  243. $pagination->setCustomParameters(['current_url' => $currentUrl]);
  244. $pagination->renderer = function ($data) use ($tpl) {
  245. foreach ($data as $key => $value) {
  246. $tpl->assign($key, $value);
  247. }
  248. $layout = $tpl->get_template('admin/pagination.tpl');
  249. $content = $tpl->fetch($layout);
  250. return $content;
  251. };
  252. break;
  253. }
  254. $searchForm = new FormValidator(
  255. 'course_filter',
  256. 'get',
  257. '',
  258. '',
  259. [],
  260. FormValidator::LAYOUT_INLINE
  261. );
  262. $searchForm->addText('keyword', '', false);
  263. $searchForm->addButtonSearch(get_lang('Search'));
  264. $tpl->assign('current_url', $currentUrl);
  265. $tpl->assign(
  266. 'actions',
  267. Display::toolbarAction(
  268. 'toolbar',
  269. [$toolbar, $searchForm->returnForm()],
  270. [1, 4]
  271. )
  272. );
  273. $tpl->assign('form', $contentForm);
  274. if (!empty($pagination)) {
  275. $tpl->assign('gradebook_list', $pagination);
  276. }
  277. $layout = $tpl->get_template('admin/gradebook_list.tpl');
  278. $tpl->display($layout);