PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/user/profile/field/datetime/define.class.php

http://github.com/moodle/moodle
PHP | 179 lines | 83 code | 27 blank | 69 comment | 9 complexity | b8f2953303d2af322944c85c91971f8b MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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 datetime profile field definition class.
  18. *
  19. * @package profilefield_datetime
  20. * @copyright 2010 Mark Nelson <markn@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22. */
  23. /**
  24. * Define datetime fields.
  25. *
  26. * @copyright 2010 Mark Nelson <markn@moodle.com>
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  28. */
  29. class profile_define_datetime extends profile_define_base {
  30. /**
  31. * Define the setting for a datetime custom field.
  32. *
  33. * @param moodleform $form the user form
  34. */
  35. public function define_form_specific($form) {
  36. // Get the current calendar in use - see MDL-18375.
  37. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  38. // Create variables to store start and end.
  39. list($year, $month, $day) = explode('_', date('Y_m_d'));
  40. $currentdate = $calendartype->convert_from_gregorian($year, $month, $day);
  41. $currentyear = $currentdate['year'];
  42. $arryears = $calendartype->get_years();
  43. // Add elements.
  44. $form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);
  45. $form->setType('param1', PARAM_INT);
  46. $form->setDefault('param1', $currentyear);
  47. $form->addElement('select', 'param2', get_string('endyear', 'profilefield_datetime'), $arryears);
  48. $form->setType('param2', PARAM_INT);
  49. $form->setDefault('param2', $currentyear);
  50. $form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
  51. $form->setType('param3', PARAM_INT);
  52. $form->addElement('hidden', 'startday', '1');
  53. $form->setType('startday', PARAM_INT);
  54. $form->addElement('hidden', 'startmonth', '1');
  55. $form->setType('startmonth', PARAM_INT);
  56. $form->addElement('hidden', 'startyear', '1');
  57. $form->setType('startyear', PARAM_INT);
  58. $form->addElement('hidden', 'endday', '1');
  59. $form->setType('endday', PARAM_INT);
  60. $form->addElement('hidden', 'endmonth', '1');
  61. $form->setType('endmonth', PARAM_INT);
  62. $form->addElement('hidden', 'endyear', '1');
  63. $form->setType('endyear', PARAM_INT);
  64. $form->addElement('hidden', 'defaultdata', '0');
  65. $form->setType('defaultdata', PARAM_INT);
  66. }
  67. /**
  68. * Validate the data from the profile field form.
  69. *
  70. * @param stdClass $data from the add/edit profile field form
  71. * @param array $files
  72. * @return array associative array of error messages
  73. */
  74. public function define_validate_specific($data, $files) {
  75. $errors = array();
  76. // Make sure the start year is not greater than the end year.
  77. if ($data->param1 > $data->param2) {
  78. $errors['param1'] = get_string('startyearafterend', 'profilefield_datetime');
  79. }
  80. return $errors;
  81. }
  82. /**
  83. * Alter form based on submitted or existing data.
  84. *
  85. * @param moodleform $mform
  86. */
  87. public function define_after_data(&$mform) {
  88. global $DB;
  89. // If we are adding a new profile field then the dates have already been set
  90. // by setDefault to the correct dates in the used calendar system. We only want
  91. // to execute the rest of the code when we have the years in the DB saved in
  92. // Gregorian that need converting to the date for this user.
  93. $id = required_param('id', PARAM_INT);
  94. if ($id === 0) {
  95. return;
  96. }
  97. // Get the field data from the DB.
  98. $field = $DB->get_record('user_info_field', array('id' => $id), 'param1, param2', MUST_EXIST);
  99. // Get the current calendar in use - see MDL-18375.
  100. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  101. // An array to store form values.
  102. $values = array();
  103. // The start and end year will be set as a Gregorian year in the DB. We want
  104. // convert these to the equivalent year in the current calendar type being used.
  105. $startdate = $calendartype->convert_from_gregorian($field->param1, 1, 1);
  106. $values['startday'] = $startdate['day'];
  107. $values['startmonth'] = $startdate['month'];
  108. $values['startyear'] = $startdate['year'];
  109. $values['param1'] = $startdate['year'];
  110. $stopdate = $calendartype->convert_from_gregorian($field->param2, 1, 1);
  111. $values['endday'] = $stopdate['day'];
  112. $values['endmonth'] = $stopdate['month'];
  113. $values['endyear'] = $stopdate['year'];
  114. $values['param2'] = $stopdate['year'];
  115. // Set the values.
  116. foreach ($values as $key => $value) {
  117. $param = $mform->getElement($key);
  118. $param->setValue($value);
  119. }
  120. }
  121. /**
  122. * Preprocess data from the profile field form before
  123. * it is saved.
  124. *
  125. * @param stdClass $data from the add/edit profile field form
  126. * @return stdClass processed data object
  127. */
  128. public function define_save_preprocess($data) {
  129. // Get the current calendar in use - see MDL-18375.
  130. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  131. // Check if the start year was changed, if it was then convert from the start of that year.
  132. if ($data->param1 != $data->startyear) {
  133. $startdate = $calendartype->convert_to_gregorian($data->param1, 1, 1);
  134. } else {
  135. $startdate = $calendartype->convert_to_gregorian($data->param1, $data->startmonth, $data->startday);
  136. }
  137. // Check if the end year was changed, if it was then convert from the start of that year.
  138. if ($data->param2 != $data->endyear) {
  139. $stopdate = $calendartype->convert_to_gregorian($data->param2, 1, 1);
  140. } else {
  141. $stopdate = $calendartype->convert_to_gregorian($data->param2, $data->endmonth, $data->endday);
  142. }
  143. $data->param1 = $startdate['year'];
  144. $data->param2 = $stopdate['year'];
  145. if (empty($data->param3)) {
  146. $data->param3 = null;
  147. }
  148. // No valid value in the default data column needed.
  149. $data->defaultdata = '0';
  150. return $data;
  151. }
  152. }