submission.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. require_once('../../config.php');
  17. require_once('locallib.php');
  18. require_once('lib.php');
  19. require_once('forms/submission_form.php'); // Requires Form class File
  20. require_once('forms/submission_form_functions.php'); // Require functions file for submission form
  21. $cmid = required_param('cmid', PARAM_INT); // Course Module ID.
  22. $id = optional_param('id', 0, PARAM_INT); // Gradeimporter id.
  23. $subid = optional_param('subid', -1, PARAM_INT); // Submission id.
  24. // $update = optional_param('update', 0, PARAM_INT); // If 1 the submission is beign updated.
  25. // Checks if everything is correct
  26. // If any of the queries fail, throw error because of MUST_EXIST clause
  27. $cm = get_coursemodule_from_id('gradeimporter', $cmid, 0, false, MUST_EXIST);
  28. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  29. $gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance), '*', MUST_EXIST);
  30. // Gets context for File API
  31. $context = context_module::instance($cm->id);
  32. // Sets URL
  33. $url = new moodle_url('/mod/gradeimporter/submission.php', array('cmid' => $cm->id));
  34. if (!empty($id)) {
  35. $url->param('id', $id);
  36. }
  37. $PAGE->set_url($url);
  38. // Requires capabilities
  39. require_login($course, false, $cm);
  40. require_capability('mod/gradeimporter:edit', $context);
  41. // Prepare data for submission form
  42. if (!isset($entry)) {
  43. // If its creating new submission
  44. $entry = new stdClass();
  45. $entry->id = null;
  46. }
  47. // Set parameters to sendo to the form
  48. $entry->cmid = $cm->id;
  49. $maxbytes = $course->maxbytes;
  50. // Prepare file manager
  51. $filemanageroptions = array('subdirs' => 0,
  52. 'maxbytes' => $maxbytes,
  53. 'areamaxbytes' => 10485760,
  54. 'maxfiles' => 1,
  55. 'accepted_types' => array('.csv', '.zip')
  56. );
  57. $entry = file_prepare_standard_filemanager($entry, 'submissionfiles', $filemanageroptions, $context,
  58. 'mod_gradeimporter', 'submissionfiles', $entry->id);
  59. // Create new mform to show to the user
  60. $mform = new mod_gradeimporter_submission_form(null, array('submission' => $entry,
  61. 'filemanageroptions' => $filemanageroptions
  62. )
  63. );
  64. if ($mform->is_cancelled()) {
  65. // Handle form cancel operation, if cancel button is present on form
  66. echo("form is cancelled");
  67. } else if ($formdata = $mform->get_data()) {
  68. $entry = create_submission($formdata, $gradeimporter->id, $USER->id);
  69. // Gets file manager content
  70. $entry = file_postupdate_standard_filemanager($entry, 'submissionfiles',
  71. $filemanageroptions, $context, 'mod_gradeimporter',
  72. 'submissionfiles', $entry->id);
  73. store_files($context, $cm, $entry);
  74. // When complete redirect to view.php
  75. // uncomment later
  76. // redirect("view.php?id=$cm->id&edit=1");
  77. // In this case you process validated data. $mform->get_data() returns data posted in form.
  78. } else {
  79. // This branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
  80. // ...or on the first display of the form.
  81. if ($subid != -1) {
  82. // If its updating an existing submission
  83. // Get data from db and insert into form
  84. $subdata = $DB->get_record('gradeimporter_submission', array('id' => $subid));
  85. $subdata->description = array('text' => $subdata->description,
  86. 'format' => $subdata->descriptionformat
  87. );
  88. $mform->set_data($subdata);
  89. }
  90. $PAGE->set_title($gradeimporter->name);
  91. $PAGE->set_heading($course->fullname);
  92. echo $OUTPUT->header();
  93. echo $OUTPUT->heading(format_string($gradeimporter->name), 2);
  94. $mform->display();
  95. echo $OUTPUT->footer();
  96. }