lib.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. //adds gradeimporter to add new activity page
  3. function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
  4. $url = new moodle_url('/admin/tool/devcourse/index.php');
  5. $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
  6. $navigation->add_node($devcoursenode);
  7. }
  8. function gradeimporter_supports($feature) {
  9. switch($feature) {
  10. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  11. case FEATURE_GROUPS: return false;
  12. case FEATURE_GROUPINGS: return false;
  13. case FEATURE_MOD_INTRO: return true;
  14. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  15. case FEATURE_GRADE_HAS_GRADE: return false;
  16. case FEATURE_GRADE_OUTCOMES: return false;
  17. case FEATURE_BACKUP_MOODLE2: return true;
  18. default: return null;
  19. }
  20. }
  21. function gradeimporter_add_instance ($data, $mform) {
  22. /*
  23. *Given an object containing all the necessary data,
  24. *(defined by the form in mod_form.php) this function
  25. *creates a new instance and returns the id of this new
  26. *instance.
  27. *
  28. *@param $data: an object from the form in mod_form.php
  29. *@return int: the id of the newly inserted gradeimport record
  30. */
  31. global $DB;
  32. $data->timemodified = time();
  33. $data->id = $DB->insert_record("gradeimporter", $data);
  34. return $data->id;
  35. }
  36. function gradeimporter_update_instance ($data){
  37. /*
  38. *given an object containing all the necessary data,
  39. *(defined by the form in mod_form.php) this function
  40. *updates an existing instance with the new data.
  41. *
  42. *@param $data: an object from the form mod_form.php
  43. *@return boolean: if the record update was a success of fail
  44. */
  45. global $DB;
  46. $data->timemodified = time();
  47. $data->id = $data->instance;
  48. return $DB->update_record('gradeimporter', $data);
  49. }
  50. function gradeimporter_delete_instance($data){
  51. /*
  52. *Given an id of a gradeimporter instance,
  53. * this function permanently deletes the instance
  54. * and any data that depends on it.
  55. *
  56. *@param int $id: Id of the gradeimporter instance
  57. *@return boolean, if the deletion was a success or
  58. * a failure.
  59. */
  60. global $DB;
  61. if (!$data = $DB->get_record('gradeimporter', array('id'=>$id) ) ){
  62. return false;
  63. }
  64. $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id);
  65. $context = context_module::instance($cm->id);
  66. // Files
  67. $fs = get_file_storage();
  68. $fs->delete_area_files($context->id, 'mod_gradeimporter');
  69. //delete all files and submissions associated with this instance
  70. $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
  71. $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
  72. //delete the instance itself
  73. $DB->delete_records('gradeimporter', array('id'=>$id) );
  74. return true;
  75. }
  76. function gradeimporter_check_for_zips ($context, $cm, $submission){
  77. global $DB;
  78. $fs = get_file_storage();
  79. $files = $fs->get_area_files($context->id, 'mod_gradeimporter', 'submission', $submission->id, "itemid, filepath, filename", false);
  80. foreach ($files as $storedfile) {
  81. if ($storedfile->get_mimetype() == 'application/zip') {
  82. //unpack zip
  83. $packer = get_file_packer('application/zip');
  84. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  85. $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter', 'unpacktemp', 0, "item, filepath, filename", false);
  86. $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0, "itemid, filepath, filename", false);
  87. if (count($tempfiles) > 0) {
  88. $storedfile->delete(); // delete the zip
  89. foreach ($tempfiles as $storedfile){
  90. if ($storedfile->get_mimetype() == 'text/csv'){
  91. $csvData = $storedfile->get_content();
  92. $csvLines = explode(PHP_EOL, $csvData);
  93. $csv = array();
  94. foreach($csvLines as $line){
  95. $csv[] = str_getcsv($line);
  96. }
  97. //array_map('str_getcsv', $storedfile->get_content() );
  98. array_walk($csv, function(&$a) use ($csv) { //adds first row as keys
  99. $a = array_combine($csv[0], $a);
  100. });
  101. array_shift($csv); // remove column header
  102. for ($i = 0; $i< sizeof($csv); $i++){
  103. foreach ($tempfiles as $storedfile) {
  104. if ($storedfile->get_filename() == $csv[$i]['file']){
  105. $fileinfo = array(
  106. 'contextid' => $context->id,
  107. 'component' => 'mod_gradeimporter',
  108. 'filearea' => 'gradeimporter_feedback',
  109. 'itemid' => $submission->id,
  110. 'filepath' => '/'.$csv[$i]['file'].'.',
  111. 'filename' => $csv[$i]['file']
  112. );
  113. //$storedfile = $fs->create_file_from_storedfile($fileinfo, $storedfile);
  114. $dbentry = new stdClass();
  115. $dbentry->id = null;
  116. $dbentry->submissionid = $submission->id;
  117. $dbentry->studentid = $csv[$i]['id'];
  118. $dbentry->grade = $csv[$i]['grade'];
  119. $dbentry->comment = $csv[$i]['comment'];
  120. $dbentry->fileid = 0; //precisa consertar
  121. $dbentry->usermodified = 0;
  122. $dbentry->timecreated = time();
  123. $dbentry->timemodified = time();
  124. $dbentry->contextid = $context->id;
  125. $dbentry->name = $csv[$i]['file'];
  126. $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  135. }
  136. }
  137. }