resulttable.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ResultTable
  5. * Table to display results for an evaluation.
  6. *
  7. * @author Stijn Konings
  8. * @author Bert Steppé
  9. *
  10. * @package chamilo.gradebook
  11. */
  12. class ResultTable extends SortableTable
  13. {
  14. private $datagen;
  15. private $evaluation;
  16. private $allresults;
  17. private $iscourse;
  18. /**
  19. * ResultTable constructor.
  20. *
  21. * @param string $evaluation
  22. * @param array $results
  23. * @param string|null $iscourse
  24. * @param array $addparams
  25. * @param bool $forprint
  26. */
  27. public function __construct(
  28. $evaluation,
  29. $results = [],
  30. $iscourse,
  31. $addparams = [],
  32. $forprint = false
  33. ) {
  34. parent:: __construct(
  35. 'resultlist',
  36. null,
  37. null,
  38. api_is_western_name_order() ? 1 : 2
  39. );
  40. $this->datagen = new ResultsDataGenerator($evaluation, $results, true);
  41. $this->evaluation = $evaluation;
  42. $this->iscourse = $iscourse;
  43. $this->forprint = $forprint;
  44. if (isset($addparams)) {
  45. $this->set_additional_parameters($addparams);
  46. }
  47. $scoredisplay = ScoreDisplay::instance();
  48. $column = 0;
  49. if ($this->iscourse == '1') {
  50. $this->set_header($column++, '', false);
  51. $this->set_form_actions([
  52. 'delete' => get_lang('Delete'),
  53. ]);
  54. }
  55. if (api_is_western_name_order()) {
  56. $this->set_header($column++, get_lang('FirstName'));
  57. $this->set_header($column++, get_lang('LastName'));
  58. } else {
  59. $this->set_header($column++, get_lang('LastName'));
  60. $this->set_header($column++, get_lang('FirstName'));
  61. }
  62. $this->set_header($column++, get_lang('Score'));
  63. if ($scoredisplay->is_custom()) {
  64. $this->set_header($column++, get_lang('Display'));
  65. }
  66. if (!$this->forprint) {
  67. $this->set_header($column++, get_lang('Modify'), false);
  68. }
  69. }
  70. /**
  71. * Function used by SortableTable to get total number of items in the table.
  72. */
  73. public function get_total_number_of_items()
  74. {
  75. return $this->datagen->get_total_results_count();
  76. }
  77. /**
  78. * Function used by SortableTable to generate the data to display.
  79. */
  80. public function get_table_data(
  81. $from = 1,
  82. $per_page = null,
  83. $column = null,
  84. $direction = null,
  85. $sort = null
  86. ) {
  87. $isWesternNameOrder = api_is_western_name_order();
  88. $scoredisplay = ScoreDisplay::instance();
  89. // determine sorting type
  90. $col_adjust = $this->iscourse == '1' ? 1 : 0;
  91. switch ($this->column) {
  92. // first name or last name
  93. case 0 + $col_adjust:
  94. if ($isWesternNameOrder) {
  95. $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
  96. } else {
  97. $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
  98. }
  99. break;
  100. // first name or last name
  101. case 1 + $col_adjust:
  102. if ($isWesternNameOrder) {
  103. $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
  104. } else {
  105. $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
  106. }
  107. break;
  108. // Score
  109. case 2 + $col_adjust:
  110. $sorting = ResultsDataGenerator::RDG_SORT_SCORE;
  111. break;
  112. case 3 + $col_adjust:
  113. $sorting = ResultsDataGenerator::RDG_SORT_MASK;
  114. break;
  115. }
  116. if ($this->direction == 'DESC') {
  117. $sorting |= ResultsDataGenerator::RDG_SORT_DESC;
  118. } else {
  119. $sorting |= ResultsDataGenerator::RDG_SORT_ASC;
  120. }
  121. $data_array = $this->datagen->get_data($sorting, $from, $this->per_page);
  122. // generate the data to display
  123. $sortable_data = [];
  124. foreach ($data_array as $item) {
  125. $row = [];
  126. if ($this->iscourse == '1') {
  127. $row[] = $item['result_id'];
  128. }
  129. if ($isWesternNameOrder) {
  130. $row[] = $item['firstname'];
  131. $row[] = $item['lastname'];
  132. } else {
  133. $row[] = $item['lastname'];
  134. $row[] = $item['firstname'];
  135. }
  136. $row[] = Display::bar_progress(
  137. $item['percentage_score'],
  138. false,
  139. $item['score']
  140. );
  141. if ($scoredisplay->is_custom()) {
  142. $row[] = $item['display'];
  143. }
  144. if (!$this->forprint) {
  145. $row[] = $this->build_edit_column($item);
  146. }
  147. $sortable_data[] = $row;
  148. }
  149. return $sortable_data;
  150. }
  151. /**
  152. * @param Result $result
  153. * @param string $url
  154. *
  155. * @return string
  156. */
  157. public static function getResultAttemptTable($result, $url = '')
  158. {
  159. if (empty($result)) {
  160. return '';
  161. }
  162. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT);
  163. $sql = "SELECT * FROM $table WHERE result_id = ".$result->get_id().' ORDER BY created_at DESC';
  164. $resultQuery = Database::query($sql);
  165. $list = Database::store_result($resultQuery);
  166. $htmlTable = new HTML_Table(['class' => 'data_table']);
  167. $htmlTable->setHeaderContents(0, 0, get_lang('Score'));
  168. $htmlTable->setHeaderContents(0, 1, get_lang('Comment'));
  169. $htmlTable->setHeaderContents(0, 2, get_lang('CreatedAt'));
  170. if (!empty($url)) {
  171. $htmlTable->setHeaderContents(0, 3, get_lang('Actions'));
  172. }
  173. $row = 1;
  174. foreach ($list as $data) {
  175. $htmlTable->setCellContents($row, 0, $data['score']);
  176. $htmlTable->setCellContents($row, 1, $data['comment']);
  177. $htmlTable->setCellContents($row, 2, Display::dateToStringAgoAndLongDate($data['created_at']));
  178. if (!empty($url)) {
  179. $htmlTable->setCellContents(
  180. $row,
  181. 3,
  182. Display::url(
  183. Display::return_icon('delete.png', get_lang('Delete')),
  184. $url.'&action=delete_attempt&result_attempt_id='.$data['id']
  185. )
  186. );
  187. }
  188. $row++;
  189. }
  190. return $htmlTable->toHtml();
  191. }
  192. /**
  193. * @param array $item
  194. *
  195. * @return string
  196. */
  197. private function build_edit_column($item)
  198. {
  199. $locked_status = $this->evaluation->get_locked();
  200. $allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts');
  201. $baseUrl = api_get_self().'?selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq();
  202. if (api_is_allowed_to_edit(null, true) && $locked_status == 0) {
  203. $edit_column = '';
  204. if ($allowMultipleAttempts) {
  205. if (!empty($item['percentage_score'])) {
  206. $edit_column .=
  207. Display::url(
  208. Display::return_icon('add.png', get_lang('AddAttempt'), '', '22'),
  209. $baseUrl.'&action=add_attempt&editres='.$item['result_id']
  210. );
  211. } else {
  212. $edit_column .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  213. Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
  214. }
  215. } else {
  216. $edit_column .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  217. Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
  218. }
  219. $edit_column .= ' <a href="'.api_get_self().'?delete_mark='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  220. Display::return_icon('delete.png', get_lang('Delete'), '', '22').'</a>';
  221. }
  222. if ($this->evaluation->get_course_code() == null) {
  223. $edit_column .= '&nbsp;<a href="'.api_get_self().'?resultdelete='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'" onclick="return confirmationuser();">';
  224. $edit_column .= Display::return_icon('delete.png', get_lang('Delete'));
  225. $edit_column .= '</a>';
  226. $edit_column .= '&nbsp;<a href="user_stats.php?userid='.$item['id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">';
  227. $edit_column .= Display::return_icon('statistics.gif', get_lang('Statistics'));
  228. $edit_column .= '</a>';
  229. }
  230. // Evaluation's origin is a link
  231. if ($this->evaluation->get_category_id() < 0) {
  232. $link = LinkFactory::get_evaluation_link($this->evaluation->get_id());
  233. $doc_url = $link->get_view_url($item['id']);
  234. if ($doc_url != null) {
  235. $edit_column .= '&nbsp;<a href="'.$doc_url.'" target="_blank">';
  236. $edit_column .= Display::return_icon('link.gif', get_lang('OpenDocument')).'</a>';
  237. }
  238. }
  239. return $edit_column;
  240. }
  241. }