resulttable.class.php 9.4 KB

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