submission.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. require_once('../../config.php');
  3. require_once('locallib.php');
  4. require_once('lib.php');
  5. require_once('submission_form.php');
  6. $cmid = required_param('cmid', PARAM_INT); // Course Module ID
  7. $id = optional_param('id' , 0, PARAM_INT); //Gradeimporter id
  8. $subid = optional_param('subid', 1, PARAM_INT); //submission id
  9. //$delete = optional_param('delete', 0, PARAM_INT);
  10. //$confirm = optional_param('confirm', 0, PARAM_INT);
  11. if (!$cm = get_coursemodule_from_id('gradeimporter', $cmid)){
  12. print_error('invalidcoursemodule');
  13. }
  14. if (!$course = $DB->get_record('course', array('id'=>$cm->course))){
  15. print_error('coursemisconf');
  16. }
  17. $context = context_module::instance($cm->id);
  18. require_capability('mod/gradeimporter:edit', $context);
  19. if (!$gradeimporter = $DB->get_record('gradeimporter', array('id'=>$cm->instance))){
  20. print_error('invalidid', 'gradeimporter');
  21. }
  22. $url = new moodle_url('/mod/gradeimporter/submission.php', array('cmid'=>$cm->id));
  23. if(!empty($id)){
  24. $url->param('id', $id);
  25. }
  26. $PAGE->set_url($url);
  27. require_login($course, false, $cm);
  28. //attempt to get the correct submission
  29. $submission = $DB->get_record('gradeimporter_submission', array('gradeimporterid'=>$gradeimporter->id));
  30. if($submission){
  31. if (isguestuser()){
  32. print_error('guestnoedit', 'gradeimporter', "$CFG->wwwroot/mod/gradeimporter/view.php?id=$cmid");
  33. }
  34. } else{
  35. $submission = new stdClass();
  36. $submission->id = null;
  37. }
  38. $maxfiles = 50;
  39. $maxbytes = $course->maxbytes;
  40. $descriptionoptions = array('trusttext'=>true, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context, 'subdirs'=>file_area_contains_subdirs($context, 'mod_gradeimporter', 'submission', $submission->id));
  41. $submissionoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
  42. $submission = file_prepare_standard_editor($submission, 'description', $descriptionoptions, $context, 'mod_gradeimporter', 'description', $submission->id);
  43. $submission = file_prepare_standard_filemanager($submission, 'submission', $submissionoptions, $context, 'mod_gradeimporter', 'submission', $submission->id);
  44. $submission->cmid = $cm->id;
  45. $mform = new mod_gradeimporter_submission_form(null, array('submission'=>$submission, 'cm'=>$cm, 'descriptionoptions'=>$descriptionoptions, 'submissionoptions'=>$submissionoptions, 'gradeimporterid'=>$gradeimporter->id));
  46. if ($mform->is_cancelled()){
  47. if ($id){
  48. redirect("view.php?id=$cm->id&mode=entry&hook=$id");
  49. } else {
  50. redirect("view.php?id=$cm->id");
  51. }
  52. } else if ($submission = $mform->get_data()){
  53. $timenow = time();
  54. if (empty($submission->id)) {
  55. $submission->gradeimporterid = $gradeimporter->id;
  56. $submission->userid = $USER->id;
  57. $submission->timecreated = $timenow;
  58. $isnewentry = true;
  59. } else {
  60. $isnewentry = false;
  61. }
  62. $submission->description = ''; //updated later
  63. $submission->descriptionformat = FORMAT_HTML; //updated later
  64. $submission->definitiontrust = 0;
  65. $submission->timemodified = $timenow;
  66. $submission->position = -1;
  67. if ($isnewentry) {
  68. //add new entry
  69. $submission->id = $DB->insert_record('gradeimporter_submission', $submission);
  70. } else {
  71. //update existing entry
  72. $DB->update_record('gradeimporter', $submission);
  73. }
  74. $submission = file_postupdate_standard_editor($submission, 'description', $descriptionoptions, $context, 'mod_gradeimporter', 'description', $submission->id);
  75. $submission = file_postupdate_standard_filemanager($submission, 'submission', $submissionoptions, $context, 'mod_gradeimporter', 'submission', $submission->id);
  76. gradeimporter_check_for_zips($context, $cm, $submission);
  77. //store updated values
  78. $DB->update_record('gradeimporter_submission', $submission);
  79. //refetch complete entry
  80. $submission = $DB->get_record('gradeimporter_submission', array('id'=>$submission->id));
  81. //trigger event and update completion (if entry was created)
  82. $eventparams = array(
  83. 'context'=>$context,
  84. 'objectid'=>$submission->id,
  85. );
  86. if ($isnewentry){
  87. //update completion state
  88. $completion = new completion_info($course);
  89. if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $gradeimporter->completionentries) {
  90. $completion->update_state($cm, COMPLETION_COMPLETE);
  91. }
  92. }
  93. redirect("view.php?id=$cm->id&edit=1");
  94. }
  95. if(!empty($id)) {
  96. $PAGE->navbar->add(get_string('edit'));
  97. }
  98. $PAGE->set_title($gradeimporter->name);
  99. $PAGE->set_heading($course->fullname);
  100. echo $OUTPUT->header();
  101. echo $OUTPUT->heading(format_string($gradeimporter->name), 2);
  102. if ($gradeimporter->intro){
  103. echo $OUTPUT->box(format_module_intro('gradeimporter', $gradeimporter, $cm->id), 'generalbox', 'intro');
  104. }
  105. $mform->display();
  106. echo $OUTPUT->footer();