locallib.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. defined('MOODLE_INTERNAL') || die();
  17. function get_comments($cmid, $id) {
  18. global $DB, $USER, $CFG;
  19. $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
  20. $sql = "
  21. SELECT gf.id gf_id,
  22. gf.grade,
  23. gf.comment gf_comment,
  24. gf.contextid gf_contextid,
  25. gf.fileid gf_fileid,
  26. gf.name gf_name,
  27. gs.name gs_name,
  28. gs.description gs_description,
  29. gst.name gst_name,
  30. gst.description gst_description,
  31. fileid
  32. FROM ".$tp."gradeimporter_feedback gf
  33. JOIN ".$tp."gradeimporter_submission gs
  34. ON gf.submissionid = gs.id
  35. JOIN ".$tp."gradeimporter_submissiontype gst
  36. ON gs.type = gst.id
  37. WHERE gf.studentid = ?";
  38. $records = $DB->get_records_sql($sql, array('studentid' => $USER->id));
  39. $data = array();
  40. if (count($records)) {
  41. foreach ($records as $value) {
  42. if (!array_key_exists($value->gst_name, $data)) {
  43. $data[$value->gst_name] = array();
  44. }
  45. $fileurl = buildurl($cmid, $value->gf_id, $value->gf_name);
  46. $data[$value->gst_name][] = array($value->gs_name, $value->grade, $value->gf_comment, $fileurl);
  47. }
  48. }
  49. return $data;
  50. }
  51. function get_teacher_view($cmid, $gradeimporterid, $enrolledusers) {
  52. global $DB, $CFG;
  53. $tp = $CFG->prefix; // gets moodle tables prefix, not everyone uses mdl_
  54. $table = new html_table();
  55. $table->align = array('center');
  56. $table->attributes = array('class' => 'generaltable mod_index');
  57. // Creates table head
  58. $subrow = new html_table_row();
  59. $cell = new html_table_cell(get_string('nameCol', 'gradeimporter'));
  60. $cell->style = "border:1px solid black";
  61. $cell->colspan = 1;
  62. $subrow->cells[] = $cell;
  63. $submissionssql = "SELECT id, name
  64. FROM ".$tp."gradeimporter_submission
  65. WHERE gradeimporterid = $gradeimporterid
  66. ORDER BY id";
  67. $submissions = $DB->get_records_sql($submissionssql);
  68. foreach ($submissions as $submission) {
  69. $subname = editSub($submission->name, $gradeimporterid, $cmid, $submission->id);
  70. $cell = new html_table_cell($subname);
  71. $cell->colspan = 2;
  72. $cell->style = "border:1px solid black";
  73. $subrow->cells[] = $cell;
  74. // Adds 2 align, one for grade and another to the file
  75. $table->align[] = 'center';
  76. $table->align[] = 'center';
  77. }
  78. $table->data[] = $subrow;
  79. // All submissions names are on the table
  80. $sql = " SELECT gs.id gs_id,
  81. gs.gradeimporterid gs_gradeimporterid,
  82. gs.type gs_type,
  83. gs.name gs_name,
  84. gst.name gst_name,
  85. gf.id gf_id,
  86. gf.studentid gf_studentid,
  87. gf.grade gf_grade,
  88. gf.fileid gf_fileid,
  89. gf.contextid gf_contextid,
  90. gf.name gf_name,
  91. user.firstname user_firstname,
  92. user.lastname user_lastname
  93. FROM ".$tp."gradeimporter_feedback gf
  94. JOIN ".$tp."user user
  95. ON user.id = gf.studentid
  96. JOIN ".$tp."gradeimporter_submission gs
  97. ON gs.id = gf.submissionid
  98. JOIN ".$tp."gradeimporter_submissiontype gst
  99. ON gst.id = gs.type
  100. WHERE gs.gradeimporterid = $gradeimporterid
  101. ORDER BY user.firstname, user.lastname, gs.id, gf.id";
  102. $records = $DB->get_recordset_sql($sql);
  103. $userslist = array();
  104. foreach ($enrolledusers as $user) {
  105. $userslist[$user->id] = array('name' => $user->firstname." ".$user->lastname);
  106. }
  107. foreach ($records as $record) {
  108. $url = buildurl($cmid, $record->gf_id, $record->gf_name);
  109. $userslist[$record->gf_studentid][$record->gs_id] = array('grade' => $record->gf_grade, 'file' => $url);
  110. }
  111. foreach ($userslist as $user) {
  112. $row = new html_table_row();
  113. $cell = new html_table_cell($user['name']);
  114. $cell->style = "border:1px solid black";
  115. $row->cells[] = $cell;
  116. foreach ($submissions as $submission) {
  117. if (array_key_exists($submission->id, $user)) {
  118. $url = $user[$submission->id]['file'];
  119. $grade = $user[$submission->id]['grade'];
  120. } else {
  121. $url = '-';
  122. $grade = '-';
  123. }
  124. $cell = new html_table_cell($grade);
  125. $cell->style = "border:1px solid black; border-right:0";
  126. $row->cells[] = $cell;
  127. $cell = new html_table_cell($url);
  128. $cell->style = "border:1px solid black; border-left:0";
  129. $row->cells[] = $cell;
  130. }
  131. $table->data[] = $row;
  132. }
  133. echo html_writer::table($table);
  134. }
  135. require_once($CFG->libdir . '/filelib.php');
  136. require_once("$CFG->libdir/csvlib.class.php");
  137. function buildurl($cmid, $fileid, $filename) {
  138. $fileurl = new moodle_url("/mod/gradeimporter/view.php", array('id' => $cmid, 'fileid' => $fileid, 'filename' => $filename, 'action' => 1));
  139. return "<a href=$fileurl target=_blank> $filename</a>";
  140. }
  141. function editSub($subname, $gradeimporterid, $cmid, $subid) {
  142. $url = new moodle_url("/mod/gradeimporter/submission.php", array('id' => $gradeimporterid,
  143. 'cmid' => $cmid,
  144. 'subid' => $subid,
  145. 'update' => 1
  146. )
  147. );
  148. return $subname.'<a href='.$url.' target="_blank"><i class="icon fa fa-pencil fa-fw" title="'
  149. .get_string('editSub', 'gradeimporter')
  150. .'" aria-label="'
  151. .get_string('editSub', 'gradeimporter').'"></i>';
  152. }
  153. function exportCSV($context) {
  154. $enrolledusers = get_enrolled_users($context, 'mod/gradeimporter:student');
  155. $header = array('id', 'name', 'email', 'grade', 'comment', 'file');
  156. $csvexport = new csv_export_writer();
  157. $csvexport->set_filename('config');
  158. $csvexport->add_data($header);
  159. foreach ($enrolledusers as $value) {
  160. $name = $value->firstname . ' ' . $value->lastname;
  161. $studententry = array($value->id, $name, $value->email, '', '', '');
  162. $csvexport->add_data($studententry);
  163. }
  164. $csvexport->download_file();
  165. // $dlfile = $csvexporter->download_array('config', $data);
  166. }