submission.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // This file is part of LInE contributions to Free Education, Private Data
  3. // LInE (Laboratory of Informatics in Education)
  4. // www.usp.br/line
  5. //
  6. // Moodle is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Moodle is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  18. require_once('../../../../config.php');
  19. require_once('../../locallib.php');
  20. require_once('../../lib.php');
  21. // Test libs
  22. // require_once('../../libs/testlib.php'); // Remove when going to production
  23. require_once('submission_form.php'); // Requires Form class File
  24. require_once('submission_form_functions.php'); // Require functions file for submission form
  25. $cmid = required_param('cmid', PARAM_INT); // Course Module ID.
  26. $id = optional_param('id', 0, PARAM_INT); // Gradeimporter id.
  27. $subid = optional_param('subid', -1, PARAM_INT); // Submission id.
  28. // $update = optional_param('update', 0, PARAM_INT); // If 1 the submission is beign updated.
  29. // Checks if everything is correct
  30. // If any of the queries fail, throw error because of MUST_EXIST clause
  31. $cm = get_coursemodule_from_id('gradeimporter', $cmid, 0, false, MUST_EXIST);
  32. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  33. $gradeimporter = $DB->get_record('gradeimporter', array('id' => $cm->instance), '*', MUST_EXIST);
  34. // Gets context for File API
  35. $context = context_module::instance($cm->id);
  36. // Sets URL
  37. $url = new moodle_url('/mod/gradeimporter/forms/submission/submission.php', array('cmid' => $cm->id));
  38. if (!empty($id)) {
  39. $url->param('id', $id);
  40. }
  41. $PAGE->set_url($url);
  42. // Requires capabilities
  43. require_login($course, false, $cm);
  44. require_capability('mod/gradeimporter:edit', $context);
  45. // Prepare data for submission form
  46. if (!isset($entry)) {
  47. // If its creating new submission
  48. $entry = new stdClass();
  49. $entry->id = null;
  50. }
  51. // Set parameters to sendo to the form
  52. $entry->cmid = $cm->id;
  53. $maxbytes = $course->maxbytes;
  54. // Prepare file manager
  55. $filemanageroptions = array('subdirs' => 0,
  56. 'maxbytes' => $maxbytes,
  57. 'areamaxbytes' => 10485760,
  58. 'maxfiles' => 1,
  59. 'accepted_types' => array('.csv', '.zip'));
  60. $entry = file_prepare_standard_filemanager($entry, 'submissionfiles', $filemanageroptions, $context, 'mod_gradeimporter', 'submissionfiles', $entry->id);
  61. // Create new mform to show to the user
  62. $mform = new mod_gradeimporter_submission_form(null, array('submission' => $entry,
  63. 'filemanageroptions' => $filemanageroptions,
  64. 'gradeimporterid' => $gradeimporter->id,
  65. 'cmid' => $cm->id
  66. ));
  67. if ($mform->is_cancelled()) {
  68. // Handle form cancel operation, if cancel button is present on form
  69. redirect($CFG->wwwroot . "/mod/gradeimporter/view.php?id=" . $cm->id . "&edit=1");
  70. }
  71. else if ($formdata = $mform->get_data()) {
  72. // reset_submissions($context->id); // Remove when going to production
  73. $entry = create_submission($formdata, $gradeimporter->id, $USER->id);
  74. // Gets file manager content
  75. $entry = file_postupdate_standard_filemanager($entry, 'submissionfiles', $filemanageroptions, $context, 'mod_gradeimporter', 'submissionfiles', $entry->id);
  76. store_files($context, $cm, $entry);
  77. // When complete redirect to view.php
  78. // uncomment later
  79. redirect("view.php?id=" . $cm->id . "&edit=1");
  80. // In this case you process validated data. $mform->get_data() returns data posted in form.
  81. }
  82. else { // else if ($formdata = $mform->get_data())
  83. // This branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
  84. // ...or on the first display of the form.
  85. if ($subid != -1) {
  86. // If its updating an existing submission
  87. // Get data from db and insert into form
  88. $subdata = $DB->get_record('gradeimporter_submission', array('id' => $subid));
  89. $subdata->description = array('text' => $subdata->description, 'format' => $subdata->descriptionformat);
  90. $mform->set_data($subdata);
  91. }
  92. $PAGE->set_title($gradeimporter->name);
  93. $PAGE->set_heading($course->fullname);
  94. print $OUTPUT->header();
  95. print $OUTPUT->heading(format_string($gradeimporter->name), 2);
  96. $mform->display();
  97. print $OUTPUT->footer();
  98. }