gradebook_edit_all.php 8.7 KB

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