1
0

teacher_viewlib.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. // This file is part of
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. class Teacherview {
  17. /**
  18. * @property gradeimporterid used to query DB at get_submissions()
  19. * @property context used to get studentslist at get_studentlist()
  20. * @property cellstyle style used by table cells. Black border and centered text
  21. */
  22. private $gradeimporterid;
  23. private $context;
  24. private $cmid;
  25. private $cellstyle = "border:1px solid black; text-align:center";
  26. private $edit = 0;
  27. /**
  28. * @param int cmid to get context
  29. * @param int gradeimporterid to query database
  30. * constructor
  31. */
  32. public function __construct (int $cmid, int $gradeimporterid, int $edit) {
  33. $this->gradeimporterid = $gradeimporterid;
  34. $this->cmid = $cmid;
  35. $this->context = context_module::instance($cmid);
  36. $this->edit = $edit;
  37. }
  38. private function get_gradeimporterid () {
  39. return $this->gradeimporterid;
  40. }
  41. private function get_cmid () {
  42. return $this->cmid;
  43. }
  44. private function get_context () {
  45. return $this->context;
  46. }
  47. private function get_cellstyle ($extrastyle = "") {
  48. return "$this->cellstyle $extrastyle";
  49. }
  50. private function get_edit () {
  51. return $this->edit;
  52. }
  53. /**
  54. * builds teacherview table using php html_table class
  55. * Gets all submissions from get_submissions()
  56. * Header is built with get_table_head() passing submissions as param
  57. * Each row is a different student
  58. * Gets each row with get_studentsubmissions()
  59. * @return table as html string
  60. */
  61. public function make_table () {
  62. global $DB, $CFG;
  63. // Prepare variables
  64. $studentlist = $this->get_studentlist();
  65. $submissions = $this->get_submissions();
  66. // Create table
  67. $table = new html_table();
  68. $table->align = array('center');
  69. $table->attributes = array('class' => 'generaltable mod_index');
  70. $submissions = $this->get_submissions();
  71. if (!$submissions) {
  72. $this->no_submissions();
  73. return;
  74. }
  75. $table->data[] = $this->get_table_head($submissions);
  76. foreach ($studentlist as $student) {
  77. $table->data[] = $this->get_studentsubmissions($student, $submissions);
  78. }
  79. return html_writer::table($table);
  80. }
  81. /**
  82. * Gets students list from moodle function get_enrolled_users
  83. * Builds a list from $enrolledusers with:
  84. * * Keys as student id;
  85. * * Values as student fullname and student id.
  86. * @return array $studentlist - students data (name and id)
  87. */
  88. private function get_studentlist () {
  89. // Get students list with userid as key and fullname as value
  90. $enrolledusers = get_enrolled_users($this->get_context(), 'mod/gradeimporter:view',
  91. 0, 'u.id, u.firstname, u.lastname',
  92. 'u.firstname, u.lastname'
  93. );
  94. $studentlist = array();
  95. foreach ($enrolledusers as $user) {
  96. $studentlist[$user->id] = array('name' => $user->firstname." ".$user->lastname,
  97. 'id' => $user->id);
  98. }
  99. return $studentlist;
  100. }
  101. /**
  102. * Builds a row with first column being "name" for students names
  103. * Other rows are submissions names
  104. * @return html_table_row $header
  105. */
  106. private function get_table_head ($submissions) {
  107. // Creates teacher view table head
  108. $header = new html_table_row();
  109. // Add name header to header row
  110. $title = get_string('nameColTitle', 'gradeimporter');
  111. $text = get_string('nameCol', 'gradeimporter');
  112. $celltext = "<a title = \"$title\"> $text </a>";
  113. $header->cells[] = $this->make_cell($celltext, 1);
  114. foreach ($submissions as $submission) {
  115. // Add a icon to redirect to submission form to edit the sub
  116. $celltext = $this->subcelltext($submission);
  117. $header->cells[] = $this->make_cell($celltext, 2);
  118. }
  119. return $header;
  120. }
  121. /**
  122. * Build a row for a student defined by paramcmide and link to " - "
  123. * @return html_table_row $row with each column being grade and feedbackfile for the submission
  124. */
  125. private function get_studentsubmissions ($student, $submissions) {
  126. global $DB;
  127. // Create new row for the student
  128. $row = new html_table_row();
  129. // Set first cell of the row as the students fullname
  130. $row->cells[] = $this->make_cell($student["name"], 1);
  131. // Foreach submission checks if student has a feedback for it
  132. // If they have, fill it with grade + filename (possibly grade+link to full feedback)
  133. // If they don't have fill it with grade and filename as -
  134. foreach ($submissions as $submission) {
  135. $feedback = $DB->get_record('gradeimporter_feedback',
  136. ['submissionid' => $submission->id,
  137. 'studentid' => $student['id']]
  138. );
  139. if ($feedback) {
  140. $fileurl = $this->feedback_url($feedback);
  141. $grade = $feedback->grade;
  142. } else {
  143. $fileurl = '-';
  144. $grade = '-';
  145. }
  146. $row->cells[] = $this->make_cell($grade, 1);
  147. $row->cells[] = $this->make_cell($fileurl, 1);
  148. }
  149. return $row;
  150. }
  151. /**
  152. * Fetch all submissions for this gradeimporter instance on gradeimporter_submissions table
  153. * Build query using $this->gradeimporterid property
  154. * @return $submissions fetched from gradeimporter_submission table
  155. */
  156. private function get_submissions () {
  157. global $CFG, $DB;
  158. // Get table prefix
  159. $tp = $CFG->prefix;
  160. // Build Query
  161. $sql = "SELECT id, name, type, intro
  162. FROM {$tp}gradeimporter_submission
  163. WHERE gradeimporterid ={$this->get_gradeimporterid()}
  164. ORDER BY type, id";
  165. // Query DB
  166. $submissions = $DB->get_records_sql($sql);
  167. // Return submissions
  168. return $submissions;
  169. }
  170. /**
  171. * Prints html when there are no submissions to show
  172. * @underconstruction
  173. * @return void
  174. */
  175. private function no_submissions () {
  176. echo "No submissions";
  177. }
  178. /**
  179. * Builds fileplugin url to feedback file
  180. * @param array $feedback fetched from gradeimporter_feedback table
  181. * @return html hyperlink to download feedback file
  182. */
  183. private function feedback_url ($feedback) {
  184. $url = moodle_url::make_pluginfile_url($feedback->contextid,
  185. 'mod_gradeimporter',
  186. 'submissionfiles',
  187. $feedback->id,
  188. "/",
  189. $feedback->filename,
  190. true
  191. );
  192. return "<a href='{$url}'>$feedback->filename</a>";
  193. }
  194. /**
  195. * Makes a cell object to be inserted into a row
  196. * Cell style is predifined on class properties
  197. * @param string $text - Text shown on the cell
  198. * @param int $colspan - How many columns this cell ocuppies
  199. * @return html_table_cell $cell - cell built on the function
  200. */
  201. private function make_cell ($text, $colspan) {
  202. $cell = new html_table_cell($text);
  203. $cell->style = $this->get_cellstyle();
  204. $cell->colspan = $colspan;
  205. return $cell;
  206. }
  207. private function subcelltext ($submission) {
  208. // Base celltext, shown when editing or not
  209. $celltext = "<a title='$submission->intro'>
  210. $submission->name
  211. </a>";
  212. // If is editing submission, add a edit icons
  213. if ($this->get_edit()) {
  214. // Build edit link icon + url
  215. $editurl = new moodle_url("/mod/gradeimporter/forms/submission/submission.php", array('id' => $this->get_gradeimporterid(),
  216. 'cmid' => $this->get_cmid(),
  217. 'subid' => $submission->id)
  218. );
  219. $editlinktitle = get_string('editSub', 'gradeimporter', $submission->name);
  220. $celltext .= " <a href=\"$editurl\">
  221. <i class = \"icon fa fa-pencil fa-fw\" title=\"$editlinktitle\" aria-label=\"$editlinktitle\">
  222. </i>
  223. </a>";
  224. // Build delete link icon + url
  225. $deleteurl = new moodle_url("/mod/gradeimporter/forms/submission/delete.php", array('id' => $this->get_gradeimporterid(),
  226. 'cmid' => $this->get_cmid(),
  227. 'subid' => $submission->id)
  228. );
  229. $deletelinktitle = get_string('deletesubmission', 'gradeimporter', $submission->name);
  230. $celltext .= "<a href=\"$deleteurl\">
  231. <i class=\"fa fa-trash\" title=\"$deletelinktitle\" aria-label=\"$deletelinktitle\">
  232. </i>
  233. </a>";
  234. }
  235. // Full celltext, with edit and delete icons if teacher is editing
  236. return $celltext;
  237. }
  238. }