endpoint.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. require_once("../../config.php");
  6. require_login();
  7. global $DB, $USER, $COURSE;
  8. class Performance {
  9. private string $emptyPerformance = '{"type":[{"type":"Leitura e Escrita","total":0,"errors":0},{"type":"Manipulação de Entrada","total":0,"errors":0},{"type":"Lógica","total":0,"errors":0},{"type":"Matemática","total":0,"errors":0},{"type":"Condição","total":0,"errors":0},{"type":"Laço","total":0,"errors":0},{"type":"Vetor","total":0,"errors":0}],"tag":[{"tag":"Caso Geral","total":0,"errors":0},{"tag":"Caso Limite","total":0,"errors":0},{"tag":"Caso Nulo","total":0,"errors":0},{"tag":"Caso Negativo","total":0,"errors":0},{"tag":"Igualdade","total":0,"errors":0},{"tag":"Tipo de Entrada","total":0,"errors":0}],"lastTypeErrors":[],"lastTagErrors":[]}';
  10. public function __construct() {
  11. global $DB;
  12. }
  13. private function add(string $course, string $student): void {
  14. global $DB;
  15. $DB->insert_record('iassign_performance', (object)array('course' => $course, 'student' => $student, 'performance' => $this->emptyPerformance),false);
  16. }
  17. public function find(string $course, string $student): array {
  18. global $DB;
  19. $selectPerformance = $DB->get_record('iassign_performance', array('course' => $course, 'student' => $student));
  20. if (!$selectPerformance) {
  21. $this->add($course,$student);
  22. $performance = (array)$DB->get_record('iassign_performance', array('course' => $course, 'student' => $student));
  23. }
  24. else {
  25. $performance = (array)$selectPerformance;
  26. }
  27. return $performance;
  28. }
  29. public function update(string $course, string $student, array $testCases, array $exerciseType, object $jsPerformance): void {
  30. global $DB;
  31. $performance = json_decode($this->find($course,$student)['performance']);
  32. $incorrect = $this->updateTags($testCases, $performance->tag, $performance);
  33. $this->updateTypes($exerciseType, $performance->type, $performance, $incorrect);
  34. if ($performance!=$jsPerformance) {
  35. echo "Houve uma tentativa irregular de alterar o banco de dados";
  36. }
  37. $newPerformance = json_encode($performance, JSON_UNESCAPED_UNICODE);
  38. $DB->update_record('iassign_performance', (object)array('id' => $this->find($course,$student)['id'], 'course' => $course, 'student' => $student, 'performance' => $newPerformance),false);
  39. }
  40. private function updateTypes(array $exerciseType, array $types, object $performance, bool $incorrect): void {
  41. for($i=0; $i<count($exerciseType); $i++){
  42. for($j=0; $j<count($types); $j++){
  43. if ($exerciseType[$i] == $types[$j]->type) {
  44. $types[$j]->total += 1;
  45. if ($incorrect) {
  46. $types[$j]->errors += 1;
  47. array_push($performance->lastTypeErrors,$exerciseType[$i]);
  48. if (count($performance->lastTypeErrors) > 50) {
  49. array_shift($performance->lastTypeErrors);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. private function updateTags(array $testCases, array $tags, object $performance): bool {
  57. $incorrect = false;
  58. for ($i=0; $i<count($testCases); $i++) {
  59. for ($j=0; $j<count($tags); $j++) {
  60. if (implode("",$testCases[$i]->tags) === $tags[$j]->tag) {
  61. $tags[$j]->total += 1;
  62. if (!$testCases[$i]->results[0]->grade) {
  63. $incorrect = true;
  64. $tags[$j]->errors += 1;
  65. array_push($performance->lastTagErrors,implode("",$testCases[$i]->tags));
  66. if (count($performance->lastTagErrors) > 50) {
  67. array_shift($performance->lastTagErrors);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return $incorrect;
  74. }
  75. }
  76. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  77. $performance = new Performance();
  78. $student_performance = $performance->find(get_coursemodule_from_id('iassign',explode("D",explode("%2",explode("id",explode("get_answer",$_SERVER['HTTP_REFERER'])[1])[1])[0])[1])->id, $USER->id)['performance'];
  79. echo $student_performance;
  80. }
  81. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  82. $performance = new Performance();
  83. $data = $_POST["data"];
  84. $obj = json_decode($data);
  85. $performance->update(get_coursemodule_from_id('iassign',explode("D",explode("%2",explode("id",explode("get_answer",$_SERVER['HTTP_REFERER'])[1])[1])[0])[1])->id, $USER->id, $obj->testCases, $obj->exerciseType, $obj->performance);
  86. }