01ded849fd53cceaeca5d6cd307aeb2e1390c526
[moodle.git] / blocks / course_list / block_course_list.php
1 <?PHP //$Id$
3 include_once($CFG->dirroot . '/course/lib.php');
5 class block_course_list extends block_list {
6     function init() {
7         $this->title = get_string('courses');
8         $this->version = 2004111600;
9     }
10     
11     function has_config() {
12         return true;
13     }
15     function get_content() {
16         global $THEME, $CFG, $USER;
18         if($this->content !== NULL) {
19             return $this->content;
20         }
22         $this->content = new stdClass;
23         $this->content->items = array();
24         $this->content->icons = array();
25         $this->content->footer = '';
27         $icon  = "<img src=\"$CFG->pixpath/i/course.gif\"".
28                  " height=\"16\" width=\"16\" alt=\"".get_string("course")."\" />";
29        
30         $adminseesall = true;
31         if (isset($CFG->block_course_list_adminview)) {
32            if ( $CFG->block_course_list_adminview == 'own'){
33                $adminseesall = false;
34            }
35         }
37         if (empty($CFG->disablemycourses) and !empty($USER->id) and !(has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $adminseesall)) {    // Just print My Courses
38             if ($courses = get_my_courses($USER->id)) {
39                 foreach ($courses as $course) {
40                     if ($course->id == SITEID) {
41                         continue;
42                     }
43                     $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
44                     $this->content->items[]="<a $linkcss title=\"$course->shortname\" ".
45                                "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
46                     $this->content->icons[]=$icon;
47                 }
48                 $this->title = get_string('mycourses');
49                 $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
50                 if ($this->content->items) { // make sure we don't return an empty list
51                     return $this->content;
52                 }
53             }
54         }
56         $categories = get_categories("0");  // Parent = 0   ie top-level categories only
57         if ($categories) {   //Check we have categories
58             if (count($categories) > 1 || (count($categories) == 1 && count_records('course') > 200)) {     // Just print top level category links
59                 foreach ($categories as $category) {
60                     $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
61                     $this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
62                     $this->content->icons[]=$icon;
63                 }
64                 $this->content->footer = "<a href=\"$CFG->wwwroot/course/\">".get_string("searchcourses")."</a>...<br />".
65                                          "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
66                 $this->title = get_string('categories');
67             } else {                          // Just print course names of single category
68                 $category = array_shift($categories);
69                 $courses = get_courses($category->id);
71                 if ($courses) {
72                     foreach ($courses as $course) {
73                         $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
74                         $this->content->items[]="<a $linkcss title=\"$course->shortname\" ".
75                                    "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
76                         $this->content->icons[]=$icon;
77                     }
78                     $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
79                 } else {
80                     $this->content->items = array();
81                     $this->content->icons = array();
82                     $this->content->footer = get_string('nocoursesyet').'<br /><br />';
83                     if (iscreator()) {
84                         $this->content->footer .= '<a href="'.$CFG->wwwroot.'/course/edit.php?category='.$category->id.'">'.get_string("addnewcourse").'</a>...';
85                     }
86                 }
87                 $this->title = get_string('courses');
88             }
89         }
90         return $this->content;
91     }
92 }
94 ?>