moodle_page: MDL-12212 reimplement user_is_editing, deprecate isediting
[moodle.git] / blocks / admin / block_admin.php
1 <?php //$Id$
3 class block_admin extends block_list {
4     function init() {
5         $this->title = get_string('administration');
6         $this->version = 2007101509;
7     }
9     function get_content() {
11         global $CFG, $USER, $SITE, $COURSE, $DB, $PAGE;
13         if ($this->content !== NULL) {
14             return $this->content;
15         }
17         $this->content = new stdClass;
18         $this->content->items = array();
19         $this->content->icons = array();
20         $this->content->footer = '';
22         if (empty($this->instance->pageid)) { // sticky
23             if (!empty($COURSE)) {
24                 $this->instance->pageid = $COURSE->id;
25             }
26         }
28         if (empty($this->instance)) {
29             return $this->content = '';
30         } else if ($this->instance->pageid == SITEID) {
31             // return $this->content = '';
32         }
34         if (!empty($this->instance->pageid)) {
35             $context = get_context_instance(CONTEXT_COURSE, $this->instance->pageid);
36             if ($COURSE->id == $this->instance->pageid) {
37                 $course = $COURSE;
38             } else {
39                 $course = $DB->get_record('course', array('id'=>$this->instance->pageid));
40             }
41         } else {
42             $context = get_context_instance(CONTEXT_SYSTEM);
43             $course = $SITE;
44         }
46         if (!has_capability('moodle/course:view', $context)) {  // Just return
47             return $this->content;
48         }
50         if (empty($CFG->loginhttps)) {
51             $securewwwroot = $CFG->wwwroot;
52         } else {
53             $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
54         }
56     /// Course editing on/off
58         if ($course->id !== SITEID and has_capability('moodle/course:update', $context)) {
59             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/edit.gif" class="icon" alt="" />';
60             if ($PAGE->user_is_editing()) {
61                 $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=off&amp;sesskey='.sesskey().'">'.get_string('turneditingoff').'</a>';
62             } else {
63                 $this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=on&amp;sesskey='.sesskey().'">'.get_string('turneditingon').'</a>';
64             }
66             $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/edit.php?id='.$this->instance->pageid.'">'.get_string('settings').'</a>';
67             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/settings.gif" class="icon" alt="" />';
68         }
70     /// Assign roles to the course
72         if ($course->id != SITEID) {
73             if (has_capability('moodle/role:assign', $context)) {
74                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id.'">'.get_string('assignroles', 'role').'</a>';
75                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/roles.gif" class="icon" alt="" />';
76             } else if (get_overridable_roles($context, ROLENAME_ORIGINAL)) {
77                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$context->id.'">'.get_string('overridepermissions', 'role').'</a>';
78                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/roles.gif" class="icon" alt="" />';
79             }
80         }
82     /// View course grades (or just your own grades, same link)
83     /// find all accessible reports
84         if ($course->id !== SITEID) {
85             $reportavailable = false;
86             if (has_capability('moodle/grade:viewall', $context)) {
87                 $reportavailable = true;
88             } else if (!empty($course->showgrades)) {
89                 if ($reports = get_list_of_plugins('grade/report')) {     // Get all installed reports
90                     arsort($reports); // user is last, we want to test it first
91                     foreach ($reports as $plugin) {
92                         if (has_capability('gradereport/'.$plugin.':view', $context)) {
93                             //stop when the first visible plugin is found
94                             $reportavailable = true;
95                             break;
96                         }
97                     }
98                 }
99             }
101             if ($reportavailable) {
102                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/report/index.php?id='.$this->instance->pageid.'">'.get_string('grades').'</a>';
103                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" class="icon" alt="" />';
104             }
105         }
107     /// Course outcomes (to help give it more prominence because it's important)
108         if (!empty($CFG->enableoutcomes)) {
109             if ($course->id!==SITEID and has_capability('moodle/course:update', $context)) {
110                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$this->instance->pageid.'">'.get_string('outcomes', 'grades').'</a>';
111                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon" alt="" />';
112             }
113         }
115     /// Manage metacourses
116         if ($course->metacourse) {
117             if (has_capability('moodle/course:managemetacourse', $context)) {
118                 $strchildcourses = get_string('childcourses');
119                 $this->content->items[]='<a href="importstudents.php?id='.$this->instance->pageid.'">'.$strchildcourses.'</a>';
120                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/course.gif" class="icon" alt="" />';
121             } else if (has_capability('moodle/role:assign', $context)) {
122                 $strchildcourses = get_string('childcourses');
123                 $this->content->items[]='<span class="dimmed_text">'.$strchildcourses.'</span>';
124                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/course.gif" class="icon" alt="" />';
125             }
126         }
129     /// Manage groups in this course
131         if (($course->id!==SITEID) && ($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $context)) {
132             $strgroups = get_string('groups');
133             $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/group/index.php?id='.$this->instance->pageid.'">'.$strgroups.'</a>';
134             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" class="icon" alt="" />';
135         }
137     /// Backup this course
139         if ($course->id!==SITEID and has_capability('moodle/site:backup', $context)) {
140             $this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->instance->pageid.'">'.get_string('backup').'</a>';
141             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" class="icon" alt="" />';
142         }
144     /// Restore to this course
145         if ($course->id !== SITEID and has_capability('moodle/site:restore', $context)) {
146             $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'&amp;wdir=/backupdata">'.get_string('restore').'</a>';
147             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
148         }
150     /// Import data from other courses
151         if ($course->id !== SITEID and has_capability('moodle/site:import', $context)) {
152             $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/import.php?id='.$this->instance->pageid.'">'.get_string('import').'</a>';
153             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
154         }
156     /// Reset this course
157         if ($course->id!==SITEID and has_capability('moodle/course:reset', $context)) {
158             $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/reset.php?id='.$this->instance->pageid.'">'.get_string('reset').'</a>';
159             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/return.gif" class="icon" alt="" />';
160         }
162     /// View course reports
163         if ($course->id !== SITEID and has_capability('moodle/site:viewreports', $context)) { // basic capability for listing of reports
164             $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/report.php?id='.$this->instance->pageid.'">'.get_string('reports').'</a>';
165             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/stats.gif" class="icon" alt="" />';
166         }
168     /// Manage questions
169         if ($course->id !== SITEID){
170             $questionlink = '';
171             $questioncaps = array(
172                                     'moodle/question:add',
173                                     'moodle/question:editmine',
174                                     'moodle/question:editall',
175                                     'moodle/question:viewmine',
176                                     'moodle/question:viewall',
177                                     'moodle/question:movemine',
178                                     'moodle/question:moveall');
179             foreach ($questioncaps as $questioncap){
180                 if (has_capability($questioncap, $context)){
181                     $questionlink = 'edit.php';
182                     break;
183                 }
184             }
185             if (!$questionlink && has_capability('moodle/question:managecategory', $context)) {
186                $questionlink = 'category.php';
187             }
188             if ($questionlink) {
189                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/question/'.$questionlink.
190                         '?courseid='.$this->instance->pageid.'">'.get_string('questions', 'quiz').'</a>';
191                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/questions.gif" class="icon" alt="" />';
192             }
193         }
195     /// Repository Instances
196         require_once($CFG->dirroot.'/repository/lib.php');
197         $editabletypes = repository::get_editable_types($context);
198         if ($course->id !== SITEID && has_capability('moodle/course:update', $context) && !empty($editabletypes)) {
199             $this->content->items[]='<a href="'.$CFG->wwwroot.'/repository/manage_instances.php?contextid='.$context->id.'">'.get_string('repositories').'</a>';
200             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/repository.png" alt=""/>';
201         }
203     /// Manage files
204         if ($course->id !== SITEID and has_capability('moodle/course:managefiles', $context)) {
205             $this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'">'.get_string('files').'</a>';
206             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" class="icon" alt=""/>';
207         }
209     /// Authorize hooks
210         if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize') && ($course->id!==SITEID)) {
211             require_once($CFG->dirroot.'/enrol/authorize/const.php');
212             $paymenturl = '<a href="'.$CFG->wwwroot.'/enrol/authorize/index.php?course='.$course->id.'">'.get_string('payments').'</a> ';
213             if (has_capability('enrol/authorize:managepayments', $context)) {
214                 if ($cnt = $DB->count_records('enrol_authorize', array('status'=>AN_STATUS_AUTH, 'courseid'=>$course->id))) {
215                     $paymenturl .= '<a href="'.$CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH.'&amp;course='.$course->id.'">'.get_string('paymentpending', 'moodle', $cnt).'</a>';
216                 }
217             }
218             $this->content->items[] = $paymenturl;
219             $this->content->icons[] = '<img src="'.$CFG->pixpath.'/i/payment.gif" class="icon" alt="" />';
220         }
222     /// Unenrol link
223         if (empty($course->metacourse) && ($course->id!==SITEID)) {
224             if (has_capability('moodle/legacy:guest', $context, NULL, false)) {   // Are a guest now
225                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/enrol.php?id='.$this->instance->pageid.'">'.get_string('enrolme', '', format_string($course->shortname)).'</a>';
226                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
227             } else if (has_capability('moodle/role:unassignself', $context, NULL, false) and get_user_roles($context, $USER->id, false)) {  // Have some role
228                 $this->content->items[]='<a href="'.$CFG->wwwroot.'/course/unenrol.php?id='.$this->instance->pageid.'">'.get_string('unenrolme', '', format_string($course->shortname)).'</a>';
229                 $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
230             }
231         }
233     /// Link to the user own profile (except guests)
234         if (!isguestuser() and isloggedin()) {
235             $this->content->items[]='<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&amp;course='.$course->id.'">'.get_string('profile').'</a>';
236             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" alt="" />';
237         }
239         return $this->content;
240     }
242     function applicable_formats() {
243         return array('course' => true);   // Not needed on site
244     }
247 ?>