locallib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace gradeimporter;
  3. class feedback {
  4. public static function get_comments() {
  5. global $DB, $USER;
  6. $sql = "
  7. SELECT gf.id,
  8. gf.grade,
  9. gf.comment,
  10. gs.name gs_name,
  11. gs.info gs_info,
  12. gst.name gst_name,
  13. gst.info gst_info,
  14. fileid
  15. FROM mdl_gradeimporter_feedback gf
  16. JOIN mdl_gradeimporter_submission gs
  17. ON gf.submissionid = gs.id
  18. JOIN mdl_gradeimporter_submissiontype gst
  19. ON gs.type = gst.id
  20. WHERE gf.studentid = ?";
  21. $comments = array();
  22. $records = $DB->get_records_sql($sql, array('studentid'=>$USER->id));
  23. $data = array();
  24. if (count($records)) {
  25. foreach ($records as $key => $value) {
  26. if (!array_key_exists($value->gst_name, $data)) $data[$value->gst_name] = array();
  27. $data[$value->gst_name][] = array($value->gs_name, $value->grade, $value->fileid);
  28. }
  29. }
  30. return $data;
  31. }
  32. }
  33. function gradeimporter_check_for_zips ($context, $cm, $submission){
  34. $fs = get_file_storage();
  35. $files = $fs->get_area_files($context->id, 'mod_gradeimporter', 'submission', $submission->id, "itemid, filepath, filename", false);
  36. foreach ($files as $storedfile) {
  37. if ($storedfile->get_mimetype() == 'application/zip') {
  38. //unpack zip
  39. $packer = get_file_packer('application/zip');
  40. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  41. $storedfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0, "itemid, filepath, filename", false);
  42. if (count($tempfiles > 0)) {
  43. $storedfile->delete(); // delete the zip
  44. foreach (glob('*.csv') as $filename){
  45. $csv = array_map('str_getcsv', file($filename));
  46. array_walk($csv, function(&$a) use ($csv) { //adds first row as keys
  47. $a = array_combine($csv[0], $a);
  48. });
  49. array_shift($csv); # remove column header
  50. for ($i = 1; i< sizeof($csv); $i++){
  51. $fileinfo = array(
  52. 'contextid' => $context->id,
  53. 'component' => 'mod_gradeimporter',
  54. 'filearea' => 'submission',
  55. 'itemid' => $submission->id,
  56. 'filepath' => '/',
  57. 'filename' => $csv[i]['filename'],
  58. 'studentid' => $csv[i]['id']
  59. );
  60. foreach ($tempfiles as $storedfile) {
  61. if ($storedfile->get_filename() == $csv[i]['filename']){
  62. $storedfile = $fs->create_file_from_storedfile($fileinfo, $storedfile);
  63. $bdentry = array(
  64. 'submissionid' => $submission->id,
  65. 'studentid' => $csv[i]['id'],
  66. 'grade' => $csv[i]['grade'],
  67. 'comment' => $csv[i]['comment'],
  68. 'fileid' => $csv[i][$storedfile],
  69. 'usermodified' => $USER->id,
  70. 'timecreated' => time(),
  71. 'timemodified' => time()
  72. );
  73. $bdentry->id = $DB->insert_record("feedback", $dbentry);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  81. }
  82. }
  83. }
  84. //
  85. // INSERT INTO `mdl_gradeimporter_feedback` (`id`, `submissionid`, `studentid`, `grade`, `comment`, `fileid`, `usermodified`, `timecreated`, `timemodified`)
  86. // VALUES
  87. // (1, 10, 1, 10.00, 'ok muito bem', 100, 0, 0, 0);
  88. //