block_course_list.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Course list block.
  18. *
  19. * @package block_course_list
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. include_once($CFG->dirroot . '/course/lib.php');
  24. include_once($CFG->libdir . '/coursecatlib.php');
  25. class block_course_list extends block_list {
  26. function init() {
  27. $this->title = get_string('pluginname', 'block_course_list');
  28. }
  29. function has_config() {
  30. return true;
  31. }
  32. function get_content() {
  33. global $CFG, $USER, $DB, $OUTPUT;
  34. if($this->content !== NULL) {
  35. return $this->content;
  36. }
  37. $this->content = new stdClass;
  38. $this->content->items = array();
  39. $this->content->icons = array();
  40. $this->content->footer = '';
  41. $icon = '<img src="' . $OUTPUT->pix_url('i/course') . '" class="icon" alt="" />';
  42. $adminseesall = true;
  43. if (isset($CFG->block_course_list_adminview)) {
  44. if ( $CFG->block_course_list_adminview == 'own'){
  45. $adminseesall = false;
  46. }
  47. }
  48. if (empty($CFG->disablemycourses) and isloggedin() and !isguestuser() and
  49. !(has_capability('moodle/course:update', context_system::instance()) and $adminseesall)) { // Just print My Courses
  50. // As this is producing navigation sort order should default to $CFG->navsortmycoursessort instead
  51. // of using the default.
  52. if (!empty($CFG->navsortmycoursessort)) {
  53. $sortorder = 'visible DESC, ' . $CFG->navsortmycoursessort . ' ASC';
  54. } else {
  55. $sortorder = 'visible DESC, sortorder ASC';
  56. }
  57. if ($courses = enrol_get_my_courses(NULL, $sortorder)) {
  58. foreach ($courses as $course) {
  59. $coursecontext = context_course::instance($course->id);
  60. $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
  61. $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" ".
  62. "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".$icon.format_string(get_course_display_name_for_list($course)). "</a>";
  63. }
  64. $this->title = get_string('mycourses');
  65. /// If we can update any course of the view all isn't hidden, show the view all courses link
  66. if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
  67. $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
  68. }
  69. }
  70. $this->get_remote_courses();
  71. if ($this->content->items) { // make sure we don't return an empty list
  72. return $this->content;
  73. }
  74. }
  75. $categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only
  76. if ($categories) { //Check we have categories
  77. if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) { // Just print top level category links
  78. foreach ($categories as $category) {
  79. $categoryname = $category->get_formatted_name();
  80. $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
  81. $this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/index.php?categoryid=$category->id\">".$icon . $categoryname . "</a>";
  82. }
  83. /// If we can update any course of the view all isn't hidden, show the view all courses link
  84. if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
  85. $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
  86. }
  87. $this->title = get_string('categories');
  88. } else { // Just print course names of single category
  89. $category = array_shift($categories);
  90. $courses = get_courses($category->id);
  91. if ($courses) {
  92. foreach ($courses as $course) {
  93. $coursecontext = context_course::instance($course->id);
  94. $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
  95. $this->content->items[]="<a $linkcss title=\""
  96. . format_string($course->shortname, true, array('context' => $coursecontext))."\" ".
  97. "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">"
  98. .$icon. format_string(get_course_display_name_for_list($course), true, array('context' => context_course::instance($course->id))) . "</a>";
  99. }
  100. /// If we can update any course of the view all isn't hidden, show the view all courses link
  101. if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
  102. $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
  103. }
  104. $this->get_remote_courses();
  105. } else {
  106. $this->content->icons[] = '';
  107. $this->content->items[] = get_string('nocoursesyet');
  108. if (has_capability('moodle/course:create', context_coursecat::instance($category->id))) {
  109. $this->content->footer = '<a href="'.$CFG->wwwroot.'/course/edit.php?category='.$category->id.'">'.get_string("addnewcourse").'</a> ...';
  110. }
  111. $this->get_remote_courses();
  112. }
  113. $this->title = get_string('courses');
  114. }
  115. }
  116. return $this->content;
  117. }
  118. function get_remote_courses() {
  119. global $CFG, $USER, $OUTPUT;
  120. if (!is_enabled_auth('mnet')) {
  121. // no need to query anything remote related
  122. return;
  123. }
  124. $icon = '<img src="'.$OUTPUT->pix_url('i/mnethost') . '" class="icon" alt="" />';
  125. // shortcut - the rest is only for logged in users!
  126. if (!isloggedin() || isguestuser()) {
  127. return false;
  128. }
  129. if ($courses = get_my_remotecourses()) {
  130. $this->content->items[] = get_string('remotecourses','mnet');
  131. $this->content->icons[] = '';
  132. foreach ($courses as $course) {
  133. $this->content->items[]="<a title=\"" . format_string($course->shortname, true) . "\" ".
  134. "href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}\">"
  135. .$icon. format_string(get_course_display_name_for_list($course)) . "</a>";
  136. }
  137. // if we listed courses, we are done
  138. return true;
  139. }
  140. if ($hosts = get_my_remotehosts()) {
  141. $this->content->items[] = get_string('remotehosts', 'mnet');
  142. $this->content->icons[] = '';
  143. foreach($USER->mnet_foreign_host_array as $somehost) {
  144. $this->content->items[] = $somehost['count'].get_string('courseson','mnet').'<a title="'.$somehost['name'].'" href="'.$somehost['url'].'">'.$icon.$somehost['name'].'</a>';
  145. }
  146. // if we listed hosts, done
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Returns the role that best describes the course list block.
  153. *
  154. * @return string
  155. */
  156. public function get_aria_role() {
  157. return 'navigation';
  158. }
  159. }