PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/data/field/date/field.class.php

https://github.com/cmiic/moodle
PHP | 171 lines | 118 code | 27 blank | 26 comment | 14 complexity | 5bec6fb05d3b8498a234f72f1bfdfae3 MD5 | raw file
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.org //
  8. // //
  9. // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. //2/19/07: Advanced search of the date field is currently disabled because it does not track
  25. // pre 1970 dates and does not handle blank entrys. Advanced search functionality for this field
  26. // type can be enabled once these issues are addressed in the core API.
  27. class data_field_date extends data_field_base {
  28. var $type = 'date';
  29. var $day = 0;
  30. var $month = 0;
  31. var $year = 0;
  32. function display_add_field($recordid = 0, $formdata = null) {
  33. global $DB, $OUTPUT;
  34. if ($formdata) {
  35. $fieldname = 'field_' . $this->field->id . '_day';
  36. $day = $formdata->$fieldname;
  37. $fieldname = 'field_' . $this->field->id . '_month';
  38. $month = $formdata->$fieldname;
  39. $fieldname = 'field_' . $this->field->id . '_year';
  40. $year = $formdata->$fieldname;
  41. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  42. $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
  43. $content = make_timestamp(
  44. $gregoriandate['year'],
  45. $gregoriandate['month'],
  46. $gregoriandate['day'],
  47. $gregoriandate['hour'],
  48. $gregoriandate['minute'],
  49. 0,
  50. 0,
  51. false);
  52. } else if ($recordid) {
  53. $content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  54. } else {
  55. $content = time();
  56. }
  57. $str = '<div title="'.s($this->field->description).'" class="mod-data-input form-inline">';
  58. $dayselector = html_writer::select_time('days', 'field_'.$this->field->id.'_day', $content);
  59. $monthselector = html_writer::select_time('months', 'field_'.$this->field->id.'_month', $content);
  60. $yearselector = html_writer::select_time('years', 'field_'.$this->field->id.'_year', $content);
  61. $str .= $dayselector . $monthselector . $yearselector;
  62. $str .= '</div>';
  63. return $str;
  64. }
  65. //Enable the following three functions once core API issues have been addressed.
  66. function display_search_field($value=0) {
  67. $selectors = html_writer::select_time('days', 'f_'.$this->field->id.'_d', $value['timestamp'])
  68. . html_writer::select_time('months', 'f_'.$this->field->id.'_m', $value['timestamp'])
  69. . html_writer::select_time('years', 'f_'.$this->field->id.'_y', $value['timestamp']);
  70. $datecheck = html_writer::checkbox('f_'.$this->field->id.'_z', 1, $value['usedate']);
  71. $str = '<div class="form-inline">' . $selectors . ' ' . $datecheck . ' ' . get_string('usedate', 'data') . '</div>';
  72. return $str;
  73. }
  74. function generate_sql($tablealias, $value) {
  75. global $DB;
  76. static $i=0;
  77. $i++;
  78. $name = "df_date_$i";
  79. $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
  80. return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name => $value['timestamp']));
  81. }
  82. function parse_search_field() {
  83. $day = optional_param('f_'.$this->field->id.'_d', 0, PARAM_INT);
  84. $month = optional_param('f_'.$this->field->id.'_m', 0, PARAM_INT);
  85. $year = optional_param('f_'.$this->field->id.'_y', 0, PARAM_INT);
  86. $usedate = optional_param('f_'.$this->field->id.'_z', 0, PARAM_INT);
  87. $data = array();
  88. if (!empty($day) && !empty($month) && !empty($year) && $usedate == 1) {
  89. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  90. $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
  91. $data['timestamp'] = make_timestamp(
  92. $gregoriandate['year'],
  93. $gregoriandate['month'],
  94. $gregoriandate['day'],
  95. $gregoriandate['hour'],
  96. $gregoriandate['minute'],
  97. 0,
  98. 0,
  99. false);
  100. $data['usedate'] = 1;
  101. return $data;
  102. } else {
  103. return 0;
  104. }
  105. }
  106. function update_content($recordid, $value, $name='') {
  107. global $DB;
  108. $names = explode('_',$name);
  109. $name = $names[2]; // day month or year
  110. $this->$name = $value;
  111. if ($this->day and $this->month and $this->year) { // All of them have been collected now
  112. $content = new stdClass();
  113. $content->fieldid = $this->field->id;
  114. $content->recordid = $recordid;
  115. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  116. $gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day);
  117. $content->content = make_timestamp(
  118. $gregoriandate['year'],
  119. $gregoriandate['month'],
  120. $gregoriandate['day'],
  121. $gregoriandate['hour'],
  122. $gregoriandate['minute'],
  123. 0,
  124. 0,
  125. false);
  126. if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  127. $content->id = $oldcontent->id;
  128. return $DB->update_record('data_content', $content);
  129. } else {
  130. return $DB->insert_record('data_content', $content);
  131. }
  132. }
  133. }
  134. function display_browse_field($recordid, $template) {
  135. global $CFG, $DB;
  136. if ($content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  137. return userdate($content, get_string('strftimedate'), 0);
  138. }
  139. }
  140. function get_sort_sql($fieldname) {
  141. global $DB;
  142. return $DB->sql_cast_char2int($fieldname, true);
  143. }
  144. }