PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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