gradebook_dependency.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. $categoryId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 1;
  13. $em = Database::getManager();
  14. $repo = $em->getRepository('ChamiloCoreBundle:GradebookCategory');
  15. /** @var GradebookCategory $category */
  16. $category = $repo->find($categoryId);
  17. if (!$category) {
  18. api_not_allowed(true);
  19. }
  20. $categoryObj = Category::load($categoryId);
  21. /** @var Category $categoryObj */
  22. $categoryObj = $categoryObj[0];
  23. $dependencies = $categoryObj->getCourseListDependency();
  24. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  25. $currentUrl = api_get_self().'?';
  26. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  27. $interbreadcrumb[] = array(
  28. 'url' => api_get_path(WEB_CODE_PATH).'admin/gradebook_list.php',
  29. 'name' => get_lang('Gradebook')
  30. );
  31. $tpl = new Template(get_lang('CourseList'));
  32. $toolbar = Display::url(
  33. Display::return_icon('back.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
  34. api_get_path(WEB_CODE_PATH).'admin/gradebook_list.php'
  35. );
  36. if (empty($dependencies)) {
  37. Display::addFlash(
  38. Display::return_message(get_lang('ThisGradebookDoesntHaveDependencies'))
  39. );
  40. }
  41. $content = '';
  42. $courseList = [];
  43. $mandatoryList = api_get_configuration_value('gradebook_dependency_mandatory_courses');
  44. $mandatoryList = isset($mandatoryList['courses']) ? $mandatoryList['courses'] : [];
  45. $mandatoryListCompleteList = [];
  46. foreach ($mandatoryList as $courseMandatoryId) {
  47. $mandatoryListCompleteList[] = api_get_course_info_by_id($courseMandatoryId);
  48. }
  49. $totalDependencies = count($dependencies);
  50. $min = $categoryObj->getMinimumToValidate();
  51. $userResult = [];
  52. $dependencyList = [];
  53. foreach ($dependencies as $courseId) {
  54. $dependencyList[$courseId] = api_get_course_info_by_id($courseId);
  55. }
  56. $courseUserLoaded = [];
  57. foreach ($dependencyList as $courseId => $courseInfo) {
  58. $courseCode = $courseInfo['code'];
  59. $subCategory = Category::load(null, null, $courseCode);
  60. /** @var Category $subCategory */
  61. $subCategory = $subCategory ? $subCategory[0] : [];
  62. if (empty($subCategory)) {
  63. continue;
  64. }
  65. $userList = CourseManager::get_student_list_from_course_code($courseCode);
  66. foreach ($userList as $user) {
  67. $userId = $user['user_id'];
  68. $userInfo = api_get_user_info($userId);
  69. $courseId = $courseInfo['real_id'];
  70. $userCourseList = CourseManager::get_courses_list_by_user_id(
  71. $userId,
  72. false,
  73. false,
  74. true,
  75. [],
  76. false
  77. );
  78. $userCourseListCode = array_column($userCourseList, 'code');
  79. if (!isset($userResult[$userId]['result_mandatory_20'])) {
  80. $userResult[$userId]['result_mandatory_20'] = 0;
  81. }
  82. if (!isset($userResult[$userId]['result_not_mandatory_80'])) {
  83. $userResult[$userId]['result_not_mandatory_80'] = 0;
  84. }
  85. foreach ($userCourseList as $courseItem) {
  86. $myCourseCode = $courseItem['code'];
  87. $myCourseId = $courseItem['real_id'];
  88. if (in_array($myCourseId, $dependencies)) {
  89. continue;
  90. }
  91. if (isset($courseUserLoaded[$userId][$myCourseId])) {
  92. continue;
  93. } else {
  94. $courseUserLoaded[$userId][$myCourseId] = true;
  95. }
  96. //var_dump($myCourseCode);
  97. $courseCategory = Category::load(
  98. null,
  99. null,
  100. $myCourseCode
  101. );
  102. $courseCategory = isset($courseCategory[0]) ? $courseCategory[0] : [];
  103. $userResult[$userId]['result_out_dependencies'][$myCourseCode] = false;
  104. if (!empty($courseCategory)) {
  105. $result = Category::userFinishedCourse(
  106. $userId,
  107. $courseCategory,
  108. true
  109. );
  110. $userResult[$userId]['result_out_dependencies'][$myCourseCode] = $result;
  111. if (in_array($myCourseId, $mandatoryList)) {
  112. if ($userResult[$userId]['result_mandatory_20'] < 20 && $result) {
  113. $userResult[$userId]['result_mandatory_20'] += 10;
  114. }
  115. } else {
  116. if ($userResult[$userId]['result_not_mandatory_80'] < 80 && $result) {
  117. $userResult[$userId]['result_not_mandatory_80'] += 10;
  118. // var_dump($userResult[$userId]['result_80'] );
  119. }
  120. }
  121. }
  122. }
  123. $result = Category::userFinishedCourse(
  124. $userId,
  125. $subCategory,
  126. true
  127. );
  128. $userResult[$userId]['result_dependencies'][$courseCode] = $result;
  129. $userResult[$userId]['user_info'] = $userInfo;
  130. //var_dump("$courseCode : $result");
  131. if (in_array($courseId, $mandatoryList)) {
  132. if ($userResult[$userId]['result_mandatory_20'] < 20 && $result) {
  133. $userResult[$userId]['result_mandatory_20'] += 10;
  134. }
  135. } else {
  136. if ($userResult[$userId]['result_not_mandatory_80'] < 80 && $result) {
  137. $userResult[$userId]['result_not_mandatory_80'] += 10;
  138. //var_dump($userResult[$userId]['result_80'] );
  139. }
  140. }
  141. }
  142. //$courseInfo['users'] = $users;
  143. //$courseInfo['is_mandatory'] = in_array($courseCode, $mandatoryList);
  144. $courseList[] = $courseInfo;
  145. }
  146. foreach ($userResult as $userId => &$userData) {
  147. $courseListPassedOutDependency = count(array_filter($userData['result_out_dependencies']));
  148. $courseListPassedDependency = count(array_filter($userData['result_dependencies']));
  149. $total = $courseListPassedDependency + $courseListPassedOutDependency;
  150. $userData['course_list_passed_out_dependency'] = $courseListPassedOutDependency;
  151. $userData['course_list_passed_out_dependency_count'] = count($userData['result_out_dependencies']);
  152. // Min req must apply + mandatory should be 20
  153. //$userData['final_result'] = $total >= $min && $userData['result_mandatory_20'] == 20;
  154. $userData['final_result'] = $total >= $min && $courseListPassedDependency == $totalDependencies;
  155. }
  156. $tpl->assign('current_url', $currentUrl);
  157. $tpl->assign(
  158. 'actions',
  159. Display::toolbarAction(
  160. 'toolbar',
  161. [$toolbar],
  162. [1, 4]
  163. )
  164. );
  165. $tpl->assign('mandatory_courses', $mandatoryListCompleteList);
  166. $tpl->assign('min_to_validate', $min);
  167. $tpl->assign('gradebook_category', $category);
  168. $tpl->assign('courses', $courseList);
  169. $tpl->assign('users', $userResult);
  170. $layout = $tpl->get_template('admin/gradebook_dependency.tpl');
  171. $tpl->display($layout);