gradebook_edit_all.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. * @author Julio Montoya - fixes in order to use gradebook models + some code cleaning
  7. */
  8. $cidReset = true;
  9. $this_section = SECTION_COURSES;
  10. $current_course_tool = TOOL_GRADEBOOK;
  11. api_protect_course_script(true);
  12. api_block_anonymous_users();
  13. if (!api_is_allowed_to_edit()) {
  14. header('Location: /index.php');
  15. exit;
  16. }
  17. $my_selectcat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : '';
  18. if (empty($my_selectcat)) {
  19. api_not_allowed();
  20. }
  21. $em = Database::getManager();
  22. $course_id = GradebookUtils::get_course_id_by_link_id($my_selectcat);
  23. $tbl_forum_thread = Database:: get_course_table(TABLE_FORUM_THREAD);
  24. $tbl_work = Database:: get_course_table(TABLE_STUDENT_PUBLICATION);
  25. $tbl_attendance = Database:: get_course_table(TABLE_ATTENDANCE);
  26. $table_evaluated = GradebookUtils::getTables();
  27. $submitted = isset($_POST['submitted']) ? $_POST['submitted'] : '';
  28. if ($submitted == 1) {
  29. Display :: display_confirmation_message(get_lang('GradebookWeightUpdated')) . '<br /><br />';
  30. if (isset($_POST['evaluation'])) {
  31. $eval_log = new Evaluation();
  32. }
  33. }
  34. $output = '';
  35. $my_cat = Category::load($my_selectcat);
  36. $my_cat = $my_cat[0];
  37. $parent_id = $my_cat->get_parent_id();
  38. $parent_cat = Category::load($parent_id);
  39. $my_category = array();
  40. $cat = new Category();
  41. $my_category = $cat->shows_all_information_an_category($my_selectcat);
  42. $original_total = $my_category->getWeight();
  43. $masked_total = $parent_cat[0]->get_weight();
  44. $links = $em
  45. ->getRepository('ChamiloCoreBundle:GradebookLink')
  46. ->findBy([
  47. 'categoryId' => $my_selectcat
  48. ]);
  49. $linksInfo = [];
  50. foreach ($links as $row) {
  51. $item_weight = $row->getWeight();
  52. $sql = 'SELECT * FROM '.GradebookUtils::get_table_type_course($row->getType()).'
  53. WHERE c_id = '.$course_id.' AND '.$table_evaluated[$row->getType()][2].' = '.$row->getRefId();
  54. $result = Database::query($sql);
  55. $resource_name = Database::fetch_array($result);
  56. if (isset($resource_name['lp_type'])) {
  57. $resource_name = $resource_name[4];
  58. } else {
  59. $resource_name = $resource_name[3];
  60. }
  61. $linksInfo[] = [
  62. 'id' => $row->getId(),
  63. 'resource_name' => $resource_name
  64. ];
  65. // Update only if value changed
  66. if (isset($_POST['link'][$row->getId()])) {
  67. $new_weight = trim($_POST['link'][$row->getId()]);
  68. GradebookUtils::updateLinkWeight(
  69. $row->getId(),
  70. $resource_name,
  71. $new_weight
  72. );
  73. $item_weight = $new_weight;
  74. }
  75. $output .= '<tr><td>'.GradebookUtils::build_type_icon_tag($row->getType()).'</td>
  76. <td> '.$resource_name.' '.Display::label(
  77. $table_evaluated[$row->getType()][3],
  78. 'info'
  79. ).' </td>';
  80. $output .= '<td>
  81. <input type="hidden" name="link_'.$row->getId().'" value="'.$resource_name.'" />
  82. <input size="10" type="text" name="link['.$row->getId().']" value="'.$item_weight.'"/>
  83. </td></tr>';
  84. }
  85. $evaluations = $em
  86. ->getRepository('ChamiloCoreBundle:GradebookEvaluation')
  87. ->findBy([
  88. 'categoryId' => $my_selectcat
  89. ]);
  90. foreach ($evaluations as $evaluationRow) {
  91. $item_weight = $evaluationRow->getWeight();
  92. // update only if value changed
  93. if (isset($_POST['evaluation'][$evaluationRow->getId()])) {
  94. $new_weight = trim($_POST['evaluation'][$evaluationRow->getId()]);
  95. GradebookUtils::updateEvaluationWeight(
  96. $evaluationRow->getId(),
  97. $new_weight
  98. );
  99. $item_weight = $new_weight;
  100. }
  101. $output .= '<tr>
  102. <td>'.GradebookUtils::build_type_icon_tag('evalnotempty').'</td>
  103. <td>'.$evaluationRow->getName().' '.Display::label(
  104. get_lang('Evaluation')
  105. ).'</td>';
  106. $output .= '<td>
  107. <input type="hidden" name="eval_'.$evaluationRow->getId().'" value="'.$evaluationRow->getName().'" />
  108. <input type="text" size="10" name="evaluation['.$evaluationRow->getId().']" value="'.$item_weight.'"/>
  109. </td></tr>';
  110. }
  111. $my_api_cidreq = api_get_cidreq();
  112. if ($my_api_cidreq == '') {
  113. $my_api_cidreq = 'cidReq='.$my_category->getCourse()->getCode();
  114. }
  115. $currentUrl = api_get_self().'?'.api_get_cidreq().'&selectcat='.$my_selectcat;
  116. $form = new FormValidator('auto_weight', 'post', $currentUrl);
  117. $form->addHeader(get_lang('AutoWeight'));
  118. $form->addLabel(null, get_lang('AutoWeightExplanation'));
  119. $form->addButtonUpdate(get_lang('AutoWeight'));
  120. if ($form->validate()) {
  121. $itemCount = count($links) + count($evaluations);
  122. $weight = 0;
  123. if ($itemCount > 0) {
  124. $weight = round($original_total / $itemCount, 2);
  125. }
  126. $total = $weight * $itemCount;
  127. $diff = null;
  128. if ($original_total !== $total) {
  129. if ($total > $original_total) {
  130. $diff = $total - $original_total;
  131. }
  132. }
  133. $total = 0;
  134. $diffApplied = false;
  135. foreach ($linksInfo as $link) {
  136. $weightToApply = $weight;
  137. if ($diffApplied == false) {
  138. if (!empty($diff)) {
  139. $weightToApply = $weight - $diff;
  140. $diffApplied = true;
  141. }
  142. }
  143. GradebookUtils::updateLinkWeight(
  144. $link['id'],
  145. $link['resource_name'],
  146. $weightToApply
  147. );
  148. }
  149. foreach ($evaluations as $evaluation) {
  150. $weightToApply = $weight;
  151. if ($diffApplied == false) {
  152. if (!empty($diff)) {
  153. $weightToApply = $weight - $diff;
  154. $diffApplied = true;
  155. }
  156. }
  157. GradebookUtils::updateEvaluationWeight(
  158. $evaluation['id'],
  159. $weightToApply
  160. );
  161. }
  162. header('Location:'.$currentUrl);
  163. exit;
  164. }
  165. // DISPLAY HEADERS AND MESSAGES
  166. if (!isset($_GET['exportpdf']) and !isset($_GET['export_certificate'])) {
  167. if (isset ($_GET['studentoverview'])) {
  168. $interbreadcrumb[] = array(
  169. 'url' => Security::remove_XSS(
  170. $_SESSION['gradebook_dest']
  171. ).'?selectcat='.$my_selectcat,
  172. 'name' => get_lang('Gradebook'),
  173. );
  174. Display:: display_header(get_lang('FlatView'));
  175. } elseif (isset ($_GET['search'])) {
  176. $interbreadcrumb[] = array(
  177. 'url' => Security::remove_XSS(
  178. $_SESSION['gradebook_dest']
  179. ).'?selectcat='.$my_selectcat,
  180. 'name' => get_lang('Gradebook'),
  181. );
  182. Display:: display_header(get_lang('SearchResults'));
  183. } else {
  184. $interbreadcrumb[] = array(
  185. 'url' => Security::remove_XSS(
  186. $_SESSION['gradebook_dest']
  187. ).'?selectcat=1',
  188. 'name' => get_lang('Gradebook'),
  189. );
  190. $interbreadcrumb[] = array(
  191. 'url' => '#',
  192. 'name' => get_lang('EditAllWeights'),
  193. );
  194. Display:: display_header('');
  195. }
  196. }
  197. ?>
  198. <div class="actions">
  199. <a href="<?php echo Security::remove_XSS(
  200. $_SESSION['gradebook_dest']
  201. ).'?'.$my_api_cidreq ?>&selectcat=<?php echo $my_selectcat ?>">
  202. <?php echo Display::return_icon(
  203. 'back.png',
  204. get_lang('FolderView'),
  205. '',
  206. ICON_SIZE_MEDIUM
  207. ); ?>
  208. </a>
  209. </div>
  210. <?php
  211. $form->display();
  212. $formNormal = new FormValidator('normal_weight', 'post', $currentUrl);
  213. $formNormal->addHeader(get_lang('EditWeight'));
  214. $formNormal->display();
  215. $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $original_total);
  216. Display::display_warning_message($warning_message, false);
  217. ?>
  218. <form method="post"
  219. action="gradebook_edit_all.php?<?php echo $my_api_cidreq ?>&selectcat=<?php echo $my_selectcat ?>">
  220. <table class="data_table">
  221. <tr class="row_odd">
  222. <th style="width: 35px;"><?php echo get_lang('Type'); ?></th>
  223. <th><?php echo get_lang('Resource'); ?></th>
  224. <th><?php echo get_lang('Weight'); ?></th>
  225. </tr>
  226. <?php echo $output; ?>
  227. </table>
  228. <input type="hidden" name="submitted" value="1"/>
  229. <br/>
  230. <button class="btn btn-primary" type="submit" name="name"
  231. value="<?php echo get_lang('Save') ?>">
  232. <?php echo get_lang('SaveScoringRules') ?>
  233. </button>
  234. </form>
  235. <?php
  236. Display:: display_footer();