teacher_viewlib.php 8.1 KB

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