resulttable.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $model = ExerciseLib::getCourseScoreModel();
  63. if (empty($model)) {
  64. $this->set_header($column++, get_lang('Score'));
  65. }
  66. if ($scoredisplay->is_custom()) {
  67. $this->set_header($column++, get_lang('Display'));
  68. }
  69. if (!$this->forprint) {
  70. $this->set_header($column++, get_lang('Modify'), false);
  71. }
  72. }
  73. /**
  74. * Function used by SortableTable to get total number of items in the table.
  75. */
  76. public function get_total_number_of_items()
  77. {
  78. return $this->datagen->get_total_results_count();
  79. }
  80. /**
  81. * Function used by SortableTable to generate the data to display.
  82. */
  83. public function get_table_data(
  84. $from = 1,
  85. $per_page = null,
  86. $column = null,
  87. $direction = null,
  88. $sort = null
  89. ) {
  90. $isWesternNameOrder = api_is_western_name_order();
  91. $scoredisplay = ScoreDisplay::instance();
  92. // determine sorting type
  93. $col_adjust = $this->iscourse == '1' ? 1 : 0;
  94. switch ($this->column) {
  95. // first name or last name
  96. case 0 + $col_adjust:
  97. if ($isWesternNameOrder) {
  98. $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
  99. } else {
  100. $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
  101. }
  102. break;
  103. // first name or last name
  104. case 1 + $col_adjust:
  105. if ($isWesternNameOrder) {
  106. $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
  107. } else {
  108. $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
  109. }
  110. break;
  111. // Score
  112. case 2 + $col_adjust:
  113. $sorting = ResultsDataGenerator::RDG_SORT_SCORE;
  114. break;
  115. case 3 + $col_adjust:
  116. $sorting = ResultsDataGenerator::RDG_SORT_MASK;
  117. break;
  118. }
  119. if ($this->direction === 'DESC') {
  120. $sorting |= ResultsDataGenerator::RDG_SORT_DESC;
  121. } else {
  122. $sorting |= ResultsDataGenerator::RDG_SORT_ASC;
  123. }
  124. $data_array = $this->datagen->get_data($sorting, $from, $this->per_page);
  125. $model = ExerciseLib::getCourseScoreModel();
  126. // generate the data to display
  127. $sortable_data = [];
  128. foreach ($data_array as $item) {
  129. $row = [];
  130. if ($this->iscourse == '1') {
  131. $row[] = $item['result_id'];
  132. }
  133. if ($isWesternNameOrder) {
  134. $row[] = $item['firstname'];
  135. $row[] = $item['lastname'];
  136. } else {
  137. $row[] = $item['lastname'];
  138. $row[] = $item['firstname'];
  139. }
  140. if (empty($model)) {
  141. $row[] = Display::bar_progress(
  142. $item['percentage_score'],
  143. false,
  144. $item['score']
  145. );
  146. }
  147. if ($scoredisplay->is_custom()) {
  148. $row[] = $item['display'];
  149. }
  150. if (!$this->forprint) {
  151. $row[] = $this->build_edit_column($item);
  152. }
  153. $sortable_data[] = $row;
  154. }
  155. return $sortable_data;
  156. }
  157. /**
  158. * @param Result $result
  159. * @param string $url
  160. *
  161. * @return string
  162. */
  163. public static function getResultAttemptTable($result, $url = '')
  164. {
  165. if (empty($result)) {
  166. return '';
  167. }
  168. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT);
  169. $sql = "SELECT * FROM $table WHERE result_id = ".$result->get_id().' ORDER BY created_at DESC';
  170. $resultQuery = Database::query($sql);
  171. $list = Database::store_result($resultQuery);
  172. $htmlTable = new HTML_Table(['class' => 'data_table']);
  173. $htmlTable->setHeaderContents(0, 0, get_lang('Score'));
  174. $htmlTable->setHeaderContents(0, 1, get_lang('Comment'));
  175. $htmlTable->setHeaderContents(0, 2, get_lang('CreatedAt'));
  176. if (!empty($url)) {
  177. $htmlTable->setHeaderContents(0, 3, get_lang('Actions'));
  178. }
  179. $row = 1;
  180. foreach ($list as $data) {
  181. $htmlTable->setCellContents($row, 0, $data['score']);
  182. $htmlTable->setCellContents($row, 1, $data['comment']);
  183. $htmlTable->setCellContents($row, 2, Display::dateToStringAgoAndLongDate($data['created_at']));
  184. if (!empty($url)) {
  185. $htmlTable->setCellContents(
  186. $row,
  187. 3,
  188. Display::url(
  189. Display::return_icon('delete.png', get_lang('Delete')),
  190. $url.'&action=delete_attempt&result_attempt_id='.$data['id']
  191. )
  192. );
  193. }
  194. $row++;
  195. }
  196. return $htmlTable->toHtml();
  197. }
  198. /**
  199. * @param array $item
  200. *
  201. * @return string
  202. */
  203. private function build_edit_column($item)
  204. {
  205. $locked_status = $this->evaluation->get_locked();
  206. $allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts');
  207. $baseUrl = api_get_self().'?selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq();
  208. $editColumn = '';
  209. if (api_is_allowed_to_edit(null, true) && $locked_status == 0) {
  210. if ($allowMultipleAttempts) {
  211. if (!empty($item['percentage_score'])) {
  212. $editColumn .=
  213. Display::url(
  214. Display::return_icon('add.png', get_lang('AddAttempt'), '', '22'),
  215. $baseUrl.'&action=add_attempt&editres='.$item['result_id']
  216. );
  217. } else {
  218. $editColumn .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  219. Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
  220. }
  221. } else {
  222. $editColumn .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  223. Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
  224. }
  225. $editColumn .= ' <a href="'.api_get_self().'?delete_mark='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
  226. Display::return_icon('delete.png', get_lang('Delete'), '', '22').'</a>';
  227. }
  228. if ($this->evaluation->get_course_code() == null) {
  229. $editColumn .= '&nbsp;<a href="'.api_get_self().'?resultdelete='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'" onclick="return confirmationuser();">';
  230. $editColumn .= Display::return_icon('delete.png', get_lang('Delete'));
  231. $editColumn .= '</a>';
  232. $editColumn .= '&nbsp;<a href="user_stats.php?userid='.$item['id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">';
  233. $editColumn .= Display::return_icon('statistics.gif', get_lang('Statistics'));
  234. $editColumn .= '</a>';
  235. }
  236. // Evaluation's origin is a link
  237. if ($this->evaluation->get_category_id() < 0) {
  238. $link = LinkFactory::get_evaluation_link($this->evaluation->get_id());
  239. $doc_url = $link->get_view_url($item['id']);
  240. if ($doc_url != null) {
  241. $editColumn .= '&nbsp;<a href="'.$doc_url.'" target="_blank">';
  242. $editColumn .= Display::return_icon('link.gif', get_lang('OpenDocument')).'</a>';
  243. }
  244. }
  245. return $editColumn;
  246. }
  247. }