PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/course_overview/locallib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 193 lines | 112 code | 19 blank | 62 comment | 20 complexity | c07c2055fb239528faf43d0e04dbf008 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. * Helper functions for course_overview block
  18. *
  19. * @package block_course_overview
  20. * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * Display overview for courses
  25. *
  26. * @param array $courses courses for which overview needs to be shown
  27. * @return array html overview
  28. */
  29. function block_course_overview_get_overviews($courses) {
  30. $htmlarray = array();
  31. if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
  32. foreach ($modules as $fname) {
  33. $fname($courses,$htmlarray);
  34. }
  35. }
  36. return $htmlarray;
  37. }
  38. /**
  39. * Sets user preference for maximum courses to be displayed in course_overview block
  40. *
  41. * @param int $number maximum courses which should be visible
  42. */
  43. function block_course_overview_update_mynumber($number) {
  44. set_user_preference('course_overview_number_of_courses', $number);
  45. }
  46. /**
  47. * Sets user course sorting preference in course_overview block
  48. *
  49. * @param array $sortorder sort order of course
  50. */
  51. function block_course_overview_update_myorder($sortorder) {
  52. set_user_preference('course_overview_course_order', serialize($sortorder));
  53. }
  54. /**
  55. * Returns shortname of activities in course
  56. *
  57. * @param int $courseid id of course for which activity shortname is needed
  58. * @return string|bool list of child shortname
  59. */
  60. function block_course_overview_get_child_shortnames($courseid) {
  61. global $DB;
  62. $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
  63. $sql = "SELECT c.id, c.shortname, $ctxselect
  64. FROM {enrol} e
  65. JOIN {course} c ON (c.id = e.customint1)
  66. JOIN {context} ctx ON (ctx.instanceid = e.customint1)
  67. WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
  68. $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
  69. if ($results = $DB->get_records_sql($sql, $params)) {
  70. $shortnames = array();
  71. // Preload the context we will need it to format the category name shortly.
  72. foreach ($results as $res) {
  73. context_helper::preload_from_record($res);
  74. $context = context_course::instance($res->id);
  75. $shortnames[] = format_string($res->shortname, true, $context);
  76. }
  77. $total = count($shortnames);
  78. $suffix = '';
  79. if ($total > 10) {
  80. $shortnames = array_slice($shortnames, 0, 10);
  81. $diff = $total - count($shortnames);
  82. if ($diff > 1) {
  83. $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
  84. } else {
  85. $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
  86. }
  87. }
  88. $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
  89. $shortnames .= $suffix;
  90. }
  91. return isset($shortnames) ? $shortnames : false;
  92. }
  93. /**
  94. * Returns maximum number of courses which will be displayed in course_overview block
  95. *
  96. * @return int maximum number of courses
  97. */
  98. function block_course_overview_get_max_user_courses() {
  99. // Get block configuration
  100. $config = get_config('block_course_overview');
  101. $limit = $config->defaultmaxcourses;
  102. // If max course is not set then try get user preference
  103. if (empty($config->forcedefaultmaxcourses)) {
  104. $limit = get_user_preferences('course_overview_number_of_courses', $limit);
  105. }
  106. return $limit;
  107. }
  108. /**
  109. * Return sorted list of user courses
  110. *
  111. * @return array list of sorted courses and count of courses.
  112. */
  113. function block_course_overview_get_sorted_courses() {
  114. global $USER;
  115. $limit = block_course_overview_get_max_user_courses();
  116. $courses = enrol_get_my_courses('id, shortname, fullname, modinfo, sectioncache');
  117. $site = get_site();
  118. if (array_key_exists($site->id,$courses)) {
  119. unset($courses[$site->id]);
  120. }
  121. foreach ($courses as $c) {
  122. if (isset($USER->lastcourseaccess[$c->id])) {
  123. $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
  124. } else {
  125. $courses[$c->id]->lastaccess = 0;
  126. }
  127. }
  128. // Get remote courses.
  129. $remotecourses = array();
  130. if (is_enabled_auth('mnet')) {
  131. $remotecourses = get_my_remotecourses();
  132. }
  133. // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
  134. foreach ($remotecourses as $id => $val) {
  135. $remoteid = $val->remoteid * -1;
  136. $val->id = $remoteid;
  137. $courses[$remoteid] = $val;
  138. }
  139. $order = array();
  140. if (!is_null($usersortorder = get_user_preferences('course_overview_course_order'))) {
  141. $order = unserialize($usersortorder);
  142. }
  143. $sortedcourses = array();
  144. $counter = 0;
  145. // Get courses in sort order into list.
  146. foreach ($order as $key => $cid) {
  147. if (($counter >= $limit) && ($limit != 0)) {
  148. break;
  149. }
  150. // Make sure user is still enroled.
  151. if (isset($courses[$cid])) {
  152. $sortedcourses[$cid] = $courses[$cid];
  153. $counter++;
  154. }
  155. }
  156. // Append unsorted courses if limit allows
  157. foreach ($courses as $c) {
  158. if (($limit != 0) && ($counter >= $limit)) {
  159. break;
  160. }
  161. if (!in_array($c->id, $order)) {
  162. $sortedcourses[$c->id] = $c;
  163. $counter++;
  164. }
  165. }
  166. // From list extract site courses for overview
  167. $sitecourses = array();
  168. foreach ($sortedcourses as $key => $course) {
  169. if ($course->id > 0) {
  170. $sitecourses[$key] = $course;
  171. }
  172. }
  173. return array($sortedcourses, $sitecourses, count($courses));
  174. }