gradebook_edit_all.php 8.9 KB

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