compat.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. Functions used for compatibility where the Moodle APIs change.
  4. We should probably move db.php in here too.
  5. */
  6. function sloodle_require_js($js) {
  7. global $PAGE;
  8. global $CFG;
  9. // Moodle 2.4
  10. // We could probably go earlier than this, but this is where it definitively breaks.
  11. if ($CFG->version >= 2012120303) {
  12. $PAGE->requires->js( new moodle_url($js) );
  13. } else {
  14. require_js($js);
  15. }
  16. }
  17. function sloodle_helpbutton($page, $title, $module, $image = null, $linktext = null, $text = null, $return = null, $imagetext = null) {
  18. global $CFG;
  19. global $OUTPUT;
  20. return;
  21. if ($CFG->version >= 2012120303) {
  22. $helpicon = new help_icon();
  23. $helpicon->page = $page; // required
  24. $helpicon->text = $title; // required
  25. $helpicon->module = $module; // defaults to 'moodle'
  26. $helpicon->linktext = $linktext;
  27. echo $OUTPUT->help_icon($helpicon);
  28. } else {
  29. return helpbutton($page, $title, $module, $image, $linktext, $text, $return, $imagetext);
  30. }
  31. }
  32. function sloodle_heading($text, $align = null, $size = null, $class = null, $id = null) {
  33. global $CFG;
  34. global $OUTPUT;
  35. if ($CFG->version >= 2012120303) {
  36. return $OUTPUT->heading($text, $size, $class, $id);
  37. } else {
  38. return print_heading($text, $align, $size, $class, true, $id);
  39. }
  40. }
  41. function sloodle_print_heading($text, $align = '', $size = 2, $class = 'main', $return = null, $id = null) {
  42. if ($return) {
  43. return sloodle_heading($text, $align , $size , $class , $id );
  44. } else {
  45. echo sloodle_heading($text, $align , $size , $class , $id );
  46. }
  47. }
  48. function sloodle_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags) {
  49. global $CFG;
  50. global $OUTPUT;
  51. global $PAGE;
  52. if ($CFG->version >= 2012120303) {
  53. $PAGE->set_heading($heading); // Required
  54. $PAGE->set_title($title);
  55. $PAGE->set_cacheable($cache);
  56. $PAGE->set_focuscontrol($focus);
  57. $PAGE->set_button($button);
  58. if (is_array($navigation) && count($navigation)) {
  59. foreach($navigation as $naventry) {
  60. if ($naventry) {
  61. $PAGE->navbar->add($naventry);
  62. }
  63. }
  64. }
  65. return $OUTPUT->header();
  66. } else {
  67. return print_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, true);
  68. }
  69. }
  70. function sloodle_print_header($title='', $heading='', $navigation='', $focus='',$meta='', $cache=true, $button='&nbsp;', $menu='',$usexml=false, $bodytags='', $return=false) {
  71. if ($return) {
  72. return sloodle_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags);
  73. } else {
  74. echo sloodle_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags);
  75. }
  76. }
  77. function sloodle_print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='',$cache=true, $button='&nbsp;', $menu='', $usexml=false, $bodytags='', $return=false) {
  78. if ($return) {
  79. return sloodle_header_simple($title, $heading, $navigation, $focus, $meta,$cache, $button, $menu, $usexml, $bodytags, true);
  80. } else {
  81. echo sloodle_header_simple($title, $heading, $navigation, $focus, $meta,$cache, $button, $menu, $usexml, $bodytags, true);
  82. }
  83. }
  84. function sloodle_header_simple($title='', $heading='', $navigation='', $focus='', $meta='',$cache=true, $button='&nbsp;', $menu='', $usexml=false, $bodytags='') {
  85. global $CFG;
  86. global $OUTPUT;
  87. global $PAGE;
  88. if ($CFG->version >= 2012120303) {
  89. $PAGE->set_heading($heading); // Required
  90. $PAGE->set_title($title);
  91. $PAGE->set_cacheable($cache);
  92. $PAGE->set_focuscontrol($focus);
  93. $PAGE->set_button($button);
  94. if (is_array($navigation) && count($navigation)) {
  95. foreach($navigation as $naventry) {
  96. if ($naventry) {
  97. $PAGE->navbar->add($naventry);
  98. }
  99. }
  100. }
  101. return $OUTPUT->header();
  102. } else {
  103. return print_header_simple($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, true);
  104. }
  105. }
  106. function sloodle_print_footer($course = null) {
  107. global $CFG;
  108. global $OUTPUT;
  109. if ($CFG->version >= 2012120303) {
  110. echo $OUTPUT->footer($course);
  111. } else {
  112. print_footer($course);
  113. }
  114. }
  115. function sloodle_print_box($message, $classes = '', $ids = '') {
  116. global $CFG;
  117. global $OUTPUT;
  118. if ($CFG->version >= 2012120303) {
  119. echo $OUTPUT->box($message, $classes, $ids);
  120. } else {
  121. print_box($message, $classes, $ids);
  122. }
  123. }
  124. function sloodle_print_box_start($classes = '', $ids = '') {
  125. global $CFG;
  126. global $OUTPUT;
  127. if ($CFG->version >= 2012120303) {
  128. echo $OUTPUT->box_start($classes, $ids);
  129. } else {
  130. print_box_start($classes, $ids);
  131. }
  132. }
  133. function sloodle_print_box_end() {
  134. global $CFG;
  135. global $OUTPUT;
  136. if ($CFG->version >= 2012120303) {
  137. echo $OUTPUT->box_end();
  138. } else {
  139. print_box_end();
  140. }
  141. }
  142. function sloodle_print_table($table) {
  143. global $CFG;
  144. if ($CFG->version >= 2012120303) {
  145. // We've probably passed in a stdClass, so turn it into an html_table
  146. if (!is_a($table, 'html_table')) {
  147. $table = sloodle_cast(new html_table(), $table);
  148. }
  149. echo html_writer::table($table);
  150. } else {
  151. print_table($table);
  152. }
  153. }
  154. function sloodle_cast($destination, stdClass $source) {
  155. $sourceReflection = new ReflectionObject($source);
  156. $sourceProperties = $sourceReflection->getProperties();
  157. foreach ($sourceProperties as $sourceProperty) {
  158. $name = $sourceProperty->getName();
  159. $destination->{$name} = $source->$name;
  160. }
  161. return $destination;
  162. }