gradebook_list.php 10 KB

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