gradebook_list.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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[] = array(
  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. 'minimum',
  82. get_lang('MinimumGradebookToValidate'),
  83. false
  84. );
  85. $form->addButtonSave(get_lang('Add'));
  86. $contentForm = $form->returnForm();
  87. if ($form->validate()) {
  88. $values = $form->getSubmitValues();
  89. $courseId = isset($values['course_id']) ? $values['course_id'] : 0;
  90. $courseInfo = api_get_course_info_by_id($courseId);
  91. $courseCode = $courseInfo['code'];
  92. $criteria = ['courseCode' => $courseCode];
  93. $exists = $repo->findBy($criteria);
  94. if (empty($exists) || empty($courseId)) {
  95. if (empty($courseId)) {
  96. $courseCode = '';
  97. }
  98. $category = new GradebookCategory();
  99. $category
  100. ->setName($values['name'])
  101. ->setWeight($values['weight'])
  102. ->setVisible(1)
  103. ->setLocked(0)
  104. ->setGenerateCertificates(0)
  105. ->setIsRequirement(false)
  106. ->setCourseCode($courseCode)
  107. ->setUserId(api_get_user_id());
  108. $em->persist($category);
  109. $em->flush();
  110. if ($category->getId()) {
  111. $params = [];
  112. if (!empty($values['depends'])) {
  113. $depends = $values['depends'];
  114. $depends = array_map('intval', $depends);
  115. $value = serialize($depends);
  116. $params['depends'] = $value;
  117. }
  118. if (!empty($values['minimum'])) {
  119. $params['minimum_to_validate'] = (int) $values['minimum'];
  120. }
  121. if (!empty($params)) {
  122. Database::update(
  123. $table,
  124. $params,
  125. ['id = ?' => $category->getId()]
  126. );
  127. }
  128. Display::addFlash(Display::return_message(get_lang('Added')));
  129. header('Location: '.$currentUrl);
  130. exit;
  131. }
  132. } else {
  133. Display::addFlash(Display::return_message(get_lang('CategoryExists')));
  134. }
  135. }
  136. break;
  137. case 'edit':
  138. $toolbar = Display::url(
  139. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  140. $currentUrl
  141. );
  142. /** @var GradebookCategory $category */
  143. $category = $repo->find($categoryId);
  144. if (!empty($category)) {
  145. $form = new FormValidator(
  146. 'category_edit',
  147. 'post',
  148. $currentUrl.'&action=edit&id='.$categoryId
  149. );
  150. $form->addText('name', get_lang('Name'));
  151. $form->addText('weight', get_lang('Weight'));
  152. $form->addLabel(get_lang('Course'), $category->getCourseCode());
  153. $sql = "SELECT depends, minimum_to_validate
  154. FROM $table WHERE id = ".$categoryId;
  155. $result = Database::query($sql);
  156. $categoryData = Database::fetch_array($result, 'ASSOC');
  157. $options = [];
  158. if (!empty($categoryData['depends'])) {
  159. $list = unserialize($categoryData['depends']);
  160. foreach ($list as $itemId) {
  161. $courseInfo = api_get_course_info_by_id($itemId);
  162. $options[$itemId] = $courseInfo['name'];
  163. }
  164. }
  165. $form->addSelectAjax(
  166. 'depends',
  167. get_lang('DependsOnGradebook'),
  168. $options,
  169. [
  170. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
  171. 'multiple' => 'multiple'
  172. ]
  173. );
  174. $form->addText(
  175. 'minimum',
  176. get_lang('MinimumGradebookToValidate'),
  177. false
  178. );
  179. $form->addButtonSave(get_lang('Edit'));
  180. $defaults = [
  181. 'name' => $category->getName(),
  182. 'weight' => $category->getWeight(),
  183. 'depends' => array_keys($options),
  184. 'minimum' => $categoryData['minimum_to_validate']
  185. ];
  186. $form->setDefaults($defaults);
  187. $contentForm = $form->returnForm();
  188. if ($form->validate()) {
  189. $values = $form->getSubmitValues();
  190. $category->setName($values['name']);
  191. $category->setWeight($values['weight']);
  192. $em->merge($category);
  193. $em->flush();
  194. if (!empty($values['depends'])) {
  195. $depends = $values['depends'];
  196. $depends = array_map('intval', $depends);
  197. $value = serialize($depends);
  198. $params['depends'] = $value;
  199. }
  200. if (!empty($values['minimum'])) {
  201. $params['minimum_to_validate'] = (int) $values['minimum'];
  202. }
  203. if (!empty($params)) {
  204. Database::update(
  205. $table,
  206. $params,
  207. ['id = ?' => $category->getId()]
  208. );
  209. }
  210. Display::addFlash(Display::return_message(get_lang('Updated')));
  211. header('Location: '.$currentUrl);
  212. exit;
  213. }
  214. }
  215. break;
  216. case 'list':
  217. default:
  218. $paginator = new Paginator();
  219. $pagination = $paginator->paginate(
  220. $gradeBookList,
  221. $page,
  222. $maxItems
  223. );
  224. // pagination.tpl needs current_url with out "page" param
  225. $pagination->setCustomParameters(['current_url' => $currentUrl]);
  226. $pagination->renderer = function ($data) use ($tpl) {
  227. foreach ($data as $key => $value) {
  228. $tpl->assign($key, $value);
  229. }
  230. $layout = $tpl->get_template('admin/pagination.tpl');
  231. $content = $tpl->fetch($layout);
  232. return $content;
  233. };
  234. break;
  235. }
  236. $searchForm = new FormValidator(
  237. 'course_filter',
  238. 'get',
  239. '',
  240. '',
  241. array(),
  242. FormValidator::LAYOUT_INLINE
  243. );
  244. $searchForm->addText('keyword', '', false);
  245. $searchForm->addButtonSearch(get_lang('Search'));
  246. $tpl->assign('current_url', $currentUrl);
  247. $tpl->assign(
  248. 'actions',
  249. Display::toolbarAction(
  250. 'toolbar',
  251. [$toolbar, $searchForm->returnForm()],
  252. [1, 4]
  253. )
  254. );
  255. $tpl->assign('form', $contentForm);
  256. if (!empty($pagination)) {
  257. $tpl->assign('gradebook_list', $pagination);
  258. }
  259. $layout = $tpl->get_template('admin/gradebook_list.tpl');
  260. $tpl->display($layout);