/mod/assign/extensionform.php

https://bitbucket.org/moodle/moodle · PHP · 137 lines · 78 code · 19 blank · 40 comment · 11 complexity · 659c6b619dc8584bd5f62a40b3646f94 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  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. /**
  17. * This file contains the forms to create and edit an instance of this module
  18. *
  19. * @package mod_assign
  20. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  24. require_once($CFG->libdir.'/formslib.php');
  25. require_once($CFG->dirroot . '/mod/assign/locallib.php');
  26. /**
  27. * Assignment extension dates form
  28. *
  29. * @package mod_assign
  30. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class mod_assign_extension_form extends moodleform {
  34. /** @var array $instance - The data passed to this form */
  35. private $instance;
  36. /**
  37. * Define the form - called by parent constructor
  38. */
  39. public function definition() {
  40. global $DB;
  41. $mform = $this->_form;
  42. $params = $this->_customdata;
  43. // Instance variable is used by the form validation function.
  44. $instance = $params['instance'];
  45. $this->instance = $instance;
  46. // Get the assignment class.
  47. $assign = $params['assign'];
  48. $userlist = $params['userlist'];
  49. $usercount = 0;
  50. $usershtml = '';
  51. // TODO Does not support custom user profile fields (MDL-70456).
  52. $extrauserfields = \core_user\fields::get_identity_fields($assign->get_context(), false);
  53. foreach ($userlist as $userid) {
  54. if ($usercount >= 5) {
  55. $usershtml .= get_string('moreusers', 'assign', count($userlist) - 5);
  56. break;
  57. }
  58. $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
  59. $usershtml .= $assign->get_renderer()->render(new assign_user_summary($user,
  60. $assign->get_course()->id,
  61. has_capability('moodle/site:viewfullnames',
  62. $assign->get_course_context()),
  63. $assign->is_blind_marking(),
  64. $assign->get_uniqueid_for_user($user->id),
  65. $extrauserfields,
  66. !$assign->is_active_user($userid)));
  67. $usercount += 1;
  68. }
  69. $userscount = count($userlist);
  70. $listusersmessage = get_string('grantextensionforusers', 'assign', $userscount);
  71. $mform->addElement('header', 'general', $listusersmessage);
  72. $mform->addElement('static', 'userslist', get_string('selectedusers', 'assign'), $usershtml);
  73. if ($instance->allowsubmissionsfromdate) {
  74. $mform->addElement('static', 'allowsubmissionsfromdate', get_string('allowsubmissionsfromdate', 'assign'),
  75. userdate($instance->allowsubmissionsfromdate));
  76. }
  77. $finaldate = 0;
  78. if ($instance->duedate) {
  79. $mform->addElement('static', 'duedate', get_string('duedate', 'assign'), userdate($instance->duedate));
  80. $finaldate = $instance->duedate;
  81. }
  82. if ($instance->cutoffdate) {
  83. $mform->addElement('static', 'cutoffdate', get_string('cutoffdate', 'assign'), userdate($instance->cutoffdate));
  84. $finaldate = $instance->cutoffdate;
  85. }
  86. $mform->addElement('date_time_selector', 'extensionduedate',
  87. get_string('extensionduedate', 'assign'), array('optional'=>true));
  88. $mform->setDefault('extensionduedate', $finaldate);
  89. $mform->addElement('hidden', 'id');
  90. $mform->setType('id', PARAM_INT);
  91. $mform->addElement('hidden', 'userid');
  92. $mform->setType('userid', PARAM_INT);
  93. $mform->addElement('hidden', 'selectedusers');
  94. $mform->setType('selectedusers', PARAM_SEQUENCE);
  95. $mform->addElement('hidden', 'action', 'saveextension');
  96. $mform->setType('action', PARAM_ALPHA);
  97. $this->add_action_buttons(true, get_string('savechanges', 'assign'));
  98. }
  99. /**
  100. * Perform validation on the extension form
  101. * @param array $data
  102. * @param array $files
  103. */
  104. public function validation($data, $files) {
  105. $errors = parent::validation($data, $files);
  106. if ($this->instance->duedate && $data['extensionduedate']) {
  107. if ($this->instance->duedate > $data['extensionduedate']) {
  108. $errors['extensionduedate'] = get_string('extensionnotafterduedate', 'assign');
  109. }
  110. }
  111. if ($this->instance->allowsubmissionsfromdate && $data['extensionduedate']) {
  112. if ($this->instance->allowsubmissionsfromdate > $data['extensionduedate']) {
  113. $errors['extensionduedate'] = get_string('extensionnotafterfromdate', 'assign');
  114. }
  115. }
  116. return $errors;
  117. }
  118. }