1
0

teacher_viewlib.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 $cellstyle = "border:1px solid black; text-align:center";
  25. /**
  26. * @param int cmid to get context
  27. * @param int gradeimporterid to query database
  28. * constructor
  29. */
  30. public function __construct (int $cmid, int $gradeimporterid) {
  31. $this->gradeimporterid = $gradeimporterid;
  32. $this->context = context_module::instance($cmid);
  33. }
  34. private function get_gradeimporterid () {
  35. return $this->gradeimporterid;
  36. }
  37. private function get_context () {
  38. return $this->context;
  39. }
  40. private function get_cellstyle () {
  41. return $this->cellstyle;
  42. }
  43. /**
  44. * builds teacherview table using php html_table class
  45. * Gets all submissions from get_submissions()
  46. * Header is built with get_table_head() passing submissions as param
  47. * Each row is a different student
  48. * Gets each row with get_studentsubmissions()
  49. * @return table as html string
  50. */
  51. public function make_table () {
  52. global $DB, $CFG;
  53. // Prepare variables
  54. $studentlist = $this->get_studentlist();
  55. $submissions = $this->get_submissions();
  56. // Create table
  57. $table = new html_table();
  58. $table->align = array('center');
  59. $table->attributes = array('class' => 'generaltable mod_index');
  60. $submissions = $this->get_submissions();
  61. if (!$submissions) {
  62. $this->no_submissions();
  63. return;
  64. }
  65. $table->data[] = $this->get_table_head($submissions);
  66. foreach ($studentlist as $student) {
  67. $table->data[] = $this->get_studentsubmissions($student, $submissions);
  68. }
  69. return html_writer::table($table);
  70. }
  71. /**
  72. * Gets students list from moodle function get_enrolled_users
  73. * Builds a list from $enrolledusers with:
  74. * * Keys as student id;
  75. * * Values as student fullname and student id.
  76. * @return array $studentlist - students data (name and id)
  77. */
  78. private function get_studentlist () {
  79. // Get students list with userid as key and fullname as value
  80. $enrolledusers = get_enrolled_users($this->get_context(), 'mod/gradeimporter:view',
  81. 0, 'u.id, u.firstname, u.lastname',
  82. 'u.firstname, u.lastname'
  83. );
  84. $studentlist = array();
  85. foreach ($enrolledusers as $user) {
  86. $studentlist[$user->id] = array('name' => $user->firstname." ".$user->lastname,
  87. 'id' => $user->id);
  88. }
  89. return $studentlist;
  90. }
  91. /**
  92. * Builds a row with first column being "name" for students names
  93. * Other rows are submissions names
  94. * @return html_table_row $header
  95. */
  96. private function get_table_head ($submissions) {
  97. // Creates teacher view table head
  98. $header = new html_table_row();
  99. // Add name header to header row
  100. $header->cells[] = $this->make_cell(get_string('nameCol', 'gradeimporter'), 1);
  101. foreach ($submissions as $submission) {
  102. // Add a icon to redirect to submission form to edit the sub
  103. $header->cells[] = $this->make_cell($submission->name, 2);
  104. }
  105. return $header;
  106. }
  107. /**
  108. * Build a row for a student defined by param $student
  109. * @param array $student, array with keys "name" and "id"
  110. * @param $submissions, submissions list to find each submission for this student
  111. * For each submission at $submissions tries do fetch feedback from DB:
  112. * * If submission is found adds grade and link to feedback file to row
  113. * * If doesn't have the submission sets grade and link to " - "
  114. * @return html_table_row $row with each column being grade and feedbackfile for the submission
  115. */
  116. private function get_studentsubmissions ($student, $submissions) {
  117. global $DB;
  118. // Create new row for the student
  119. $row = new html_table_row();
  120. // Set first cell of the row as the students fullname
  121. $row->cells[] = $this->make_cell($student["name"], 1);
  122. // Foreach submission checks if student has a feedback for it
  123. // If they have, fill it with grade + filename (possibly grade+link to full feedback)
  124. // If they don't have fill it with grade and filename as -
  125. foreach ($submissions as $submission) {
  126. $feedback = $DB->get_record('gradeimporter_feedback',
  127. ['submissionid' => $submission->id,
  128. 'studentid' => $student['id']]
  129. );
  130. if ($feedback) {
  131. $fileurl = $this->feedback_url($feedback);
  132. $grade = $feedback->grade;
  133. } else {
  134. $fileurl = '-';
  135. $grade = '-';
  136. }
  137. $row->cells[] = $this->make_cell($grade, 1);
  138. $row->cells[] = $this->make_cell($fileurl, 1);
  139. }
  140. return $row;
  141. }
  142. /**
  143. * Fetch all submissions for this gradeimporter instance on gradeimporter_submissions table
  144. * Build query using $this->gradeimporterid property
  145. * @return $submissions fetched from gradeimporter_submission table
  146. */
  147. private function get_submissions () {
  148. global $CFG, $DB;
  149. // Get table prefix
  150. $tp = $CFG->prefix;
  151. // Build Query
  152. $sql = "SELECT id, name, type
  153. FROM {$tp}gradeimporter_submission
  154. WHERE gradeimporterid ={$this->get_gradeimporterid()}
  155. ORDER BY type, id";
  156. // Query DB
  157. $submissions = $DB->get_records_sql($sql);
  158. // Return submissions
  159. return $submissions;
  160. }
  161. /**
  162. * Prints html when there are no submissions to show
  163. * @underconstruction
  164. * @return void
  165. */
  166. private function no_submissions () {
  167. echo "No submissions";
  168. }
  169. /**
  170. * Builds fileplugin url to feedback file
  171. * @param array $feedback fetched from gradeimporter_feedback table
  172. * @return html hyperlink to download feedback file
  173. */
  174. private function feedback_url ($feedback) {
  175. $url = moodle_url::make_pluginfile_url($feedback->contextid,
  176. 'mod_gradeimporter',
  177. 'submissionfiles',
  178. $feedback->id,
  179. "/",
  180. $feedback->name,
  181. true
  182. );
  183. return "<a href='{$url}'>$feedback->name</a>";
  184. }
  185. /**
  186. * Makes a cell object to be inserted into a row
  187. * Cell style is predifined on class properties
  188. * @param string $text - Text shown on the cell
  189. * @param int $colspan - How many columns this cell ocuppies
  190. * @return html_table_cell $cell - cell built on the function
  191. */
  192. private function make_cell ($text, $colspan) {
  193. $cell = new html_table_cell($text);
  194. $cell->style = $this->get_cellstyle();
  195. $cell->colspan = $colspan;
  196. return $cell;
  197. }
  198. }