teacher_viewlib.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. private $cmid;
  18. private $gradeimporterid;
  19. private $context;
  20. private $cellstyle = "border:1px solid black; text-align:center";
  21. public function __construct (int $cmid, int $gradeimporterid) {
  22. $this->cmid = $cmid;
  23. $this->gradeimporterid = $gradeimporterid;
  24. $this->context = context_module::instance($cmid);
  25. }
  26. private function get_cmid () {
  27. return $this->cmid;
  28. }
  29. private function get_gradeimporterid () {
  30. return $this->gradeimporterid;
  31. }
  32. private function get_context () {
  33. return $this->context;
  34. }
  35. private function get_cellstyle () {
  36. return $this->cellstyle;
  37. }
  38. public function make_table () {
  39. global $DB, $CFG;
  40. // Prepare variables
  41. $studentlist = $this->get_studentlist();
  42. $submissions = $this->get_submissions();
  43. // Create table
  44. $table = new html_table();
  45. $table->align = array('center');
  46. $table->attributes = array('class' => 'generaltable mod_index');
  47. $submissions = $this->get_submissions();
  48. if (!$submissions) {
  49. $this->no_submissions();
  50. return;
  51. }
  52. $table->data[] = $this->get_table_head($submissions);
  53. foreach ($studentlist as $student) {
  54. $table->data[] = $this->get_studentsubmissions($student, $submissions);
  55. }
  56. return html_writer::table($table);
  57. }
  58. private function get_studentlist () {
  59. // Get students list with userid as key and fullname as value
  60. $enrolledusers = get_enrolled_users($this->get_context(), 'mod/gradeimporter:view',
  61. 0, 'u.id, u.firstname, u.lastname',
  62. 'u.firstname, u.lastname'
  63. );
  64. $studentlist = array();
  65. foreach ($enrolledusers as $user) {
  66. $studentlist[$user->id] = array('name' => $user->firstname." ".$user->lastname,
  67. 'id' => $user->id);
  68. }
  69. return $studentlist;
  70. }
  71. private function get_table_head ($submissions) {
  72. // Creates teacher view table head
  73. $header = new html_table_row();
  74. // Add name header to header row
  75. $header->cells[] = $this->make_cell(get_string('nameCol', 'gradeimporter'), 1);
  76. foreach ($submissions as $submission) {
  77. // $subname = editSub($submission->name, $gradeimporterid, $cmid, $submission->id);
  78. $header->cells[] = $this->make_cell($submission->name, 2);
  79. }
  80. return $header;
  81. }
  82. private function get_studentsubmissions ($student, $submissions) {
  83. global $DB;
  84. // Create new row for the student
  85. $row = new html_table_row();
  86. // Set first cell of the row as the students fullname
  87. $row->cells[] = $this->make_cell($student["name"], 1);
  88. // Foreach submission checks if student has a feedback for it
  89. // If they have, fill it with grade + filename (possibly grade+link to full feedback)
  90. // If they don't have fill it with grade and filename as -
  91. foreach ($submissions as $submission) {
  92. $feedback = $DB->get_record('gradeimporter_feedback',
  93. ['submissionid' => $submission->id,
  94. 'studentid' => $student['id']]
  95. );
  96. if ($feedback) {
  97. $fileurl = $this->feedback_url($feedback);
  98. $grade = $feedback->grade;
  99. } else {
  100. $fileurl = '-';
  101. $grade = '-';
  102. }
  103. $row->cells[] = $this->make_cell($grade, 1);
  104. $row->cells[] = $this->make_cell($fileurl, 1);
  105. }
  106. return $row;
  107. }
  108. private function get_submissions () {
  109. global $CFG, $DB;
  110. // Get table prefix
  111. $tp = $CFG->prefix;
  112. // Build Query
  113. $sql = "SELECT id, name, type
  114. FROM {$tp}gradeimporter_submission
  115. WHERE gradeimporterid ={$this->get_gradeimporterid()}
  116. ORDER BY type, id";
  117. // Query DB
  118. $submissions = $DB->get_records_sql($sql);
  119. // Return submissions
  120. return $submissions;
  121. }
  122. private function no_submissions () {
  123. echo "No submissions";
  124. }
  125. private function feedback_url ($feedback) {
  126. $url = moodle_url::make_pluginfile_url($feedback->contextid,
  127. 'mod_gradeimporter',
  128. 'submissionfiles',
  129. 0,
  130. "/",
  131. $feedback->name,
  132. true
  133. );
  134. return "<a href='{$url}'>$feedback->name</a>";
  135. }
  136. private function make_cell ($text, $colspan) {
  137. $cell = new html_table_cell($text);
  138. $cell->style = $this->get_cellstyle();
  139. $cell->colspan = $colspan;
  140. return $cell;
  141. }
  142. }