category.php 19.1 KB
Newer Older
1
<?php
moodler's avatar
moodler committed
2
      // Displays the top level category or all courses
urs_hunkler's avatar
urs_hunkler committed
3
      // In editing mode, allows the admin to edit a category,
moodler's avatar
moodler committed
4
      // and rearrange courses
moodler's avatar
moodler committed
5

6
7
    require_once("../config.php");
    require_once("lib.php");
moodler's avatar
moodler committed
8

9
10
11
    $id = required_param('id', PARAM_INT); // Category id
    $page = optional_param('page', 0, PARAM_INT); // which page to show
    $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
12
    $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
13
14
15
16
17
18
    $hide = optional_param('hide', 0, PARAM_INT);
    $show = optional_param('show', 0, PARAM_INT);
    $moveup = optional_param('moveup', 0, PARAM_INT);
    $movedown = optional_param('movedown', 0, PARAM_INT);
    $moveto = optional_param('moveto', 0, PARAM_INT);
    $resort = optional_param('resort', 0, PARAM_BOOL);
moodler's avatar
moodler committed
19

20
    $site = get_site();
21

moodler's avatar
moodler committed
22
    if (empty($id)) {
23
        print_error("unknowcategory");
moodler's avatar
moodler committed
24
    }
25

26
    $PAGE->set_category_by_id($id);
27
28
29
30
31
32
33
    $urlparams = array('id' => $id);
    if ($page) {
        $urlparams['page'] = $page;
    }
    if ($perpage) {
        $urlparams['perpage'] = $perpage;
    }
34
    $PAGE->set_url('/course/category.php', $urlparams);
35
36
    $context = $PAGE->context;
    $category = $PAGE->category;
37

38
39
    $canedit = can_edit_in_category($category->id);
    if ($canedit) {
40
        if ($categoryedit !== -1) {
41
            $USER->editing = $categoryedit;
moodler's avatar
moodler committed
42
        }
43
44
        require_login();
        $editingon = $PAGE->user_is_editing();
moodler's avatar
moodler committed
45
    } else {
46
47
48
        if ($CFG->forcelogin) {
            require_login();
        }
49
        $editingon = false;
moodler's avatar
moodler committed
50
    }
moodler's avatar
moodler committed
51

52
53
54
55
    if (!$category->visible) {
        require_capability('moodle/category:viewhiddencategories', $context);
    }

56
57
    // Process any category actions.
    if (has_capability('moodle/category:manage', $context)) {
58
        /// Resort the category if requested
59
        if ($resort and confirm_sesskey()) {
mjollnir_'s avatar
mjollnir_ committed
60
            if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
61
                $i = 1;
62
                foreach ($courses as $course) {
63
64
                    $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
                    $i++;
65
                }
66
                fix_course_sortorder(); // should not be needed
67
            }
68
        }
69
70
    }

71
    // Process any course actions.
72
    if ($editingon) {
urs_hunkler's avatar
urs_hunkler committed
73
    /// Move a specified course to a new category
74
        if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
toyomoyo's avatar
toyomoyo committed
75
            // user must have category update in both cats to perform this
76
77
            require_capability('moodle/category:manage', $context);
            require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
78

79
80
            if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
                print_error('cannotfindcategory', '', '', $data->moveto);
81
82
            }

83
            $courses = array();
84
            foreach ($data as $key => $value) {
85
                if (preg_match('/^c\d+$/', $key)) {
86
87
88
89
90
91
92
93
94
95
96
                    $courseid = substr($key, 1);
                    array_push($courses, $courseid);

                    // check this course's category
                    if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
                        if ($movingcourse->category != $id ) {
                            print_error('coursedoesnotbelongtocategory');
                        }
                    } else {
                        print_error('cannotfindcourse');
                    }
moodler's avatar
moodler committed
97
                }
98
            }
99
            move_courses($courses, $data->moveto);
moodler's avatar
moodler committed
100
101
        }

urs_hunkler's avatar
urs_hunkler committed
102
    /// Hide or show a course
103
104
        if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
            if (!empty($hide)) {
105
                $course = $DB->get_record('course', array('id' => $hide));
moodler's avatar
moodler committed
106
107
                $visible = 0;
            } else {
108
                $course = $DB->get_record('course', array('id' => $show));
moodler's avatar
moodler committed
109
110
                $visible = 1;
            }
111

moodler's avatar
moodler committed
112
            if ($course) {
113
114
                $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
                require_capability('moodle/course:visibility', $coursecontext);
115
116
                $DB->set_field('course', 'visible', $visible, array('id' => $course->id));
                $DB->set_field('course', 'visibleold', $visible, array('id' => $course->id)); // we set the old flag when user manually changes visibility of course
moodler's avatar
moodler committed
117
118
            }
        }
moodler's avatar
moodler committed
119
120


moodler's avatar
moodler committed
121
    /// Move a course up or down
122
        if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
123
            require_capability('moodle/category:manage', $context);
urs_hunkler's avatar
urs_hunkler committed
124

125
            // Ensure the course order has continuous ordering
126
127
            fix_course_sortorder();
            $swapcourse = NULL;
urs_hunkler's avatar
urs_hunkler committed
128

129
            if (!empty($moveup)) {
130
131
                if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
                    $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
132
                }
mjollnir_'s avatar
mjollnir_ committed
133
            } else {
134
135
                if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
                    $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
moodler's avatar
moodler committed
136
                }
moodler's avatar
moodler committed
137
            }
138
            if ($swapcourse and $movecourse) {
139
140
141
142
                // check course's category
                if ($movecourse->category != $id) {
                    print_error('coursedoesnotbelongtocategory');
                }
143
144
                $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
                $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
145
            }
mjollnir_'s avatar
mjollnir_ committed
146
        }
moodler's avatar
moodler committed
147
148

    } // End of editing stuff
moodler's avatar
moodler committed
149

150
151
152
153
154
155
156
157
    // Print headings
    $numcategories = $DB->count_records('course_categories');

    $stradministration = get_string('administration');
    $strcategories = get_string('categories');
    $strcategory = get_string('category');
    $strcourses = get_string('courses');

158
    if ($editingon && can_edit_in_category()) {
159
160
        // Integrate into the admin tree only if the user can edit categories at the top level,
        // otherwise the admin block does not appear to this user, and you get an error.
161
162
        require_once($CFG->libdir . '/adminlib.php');
        admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
163
        $PAGE->set_context($context);   // Ensure that we are actually showing blocks etc for the cat context
164
        echo $OUTPUT->header();
165
    } else {
Rossiani Wijaya's avatar
Rossiani Wijaya committed
166
167
168
        $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
        $PAGE->navbar->add($category->name);
        $PAGE->navbar->add($strcourses);
169
        $PAGE->set_title("$site->shortname: $category->name");
Rossiani Wijaya's avatar
Rossiani Wijaya committed
170
        $PAGE->set_heading($site->fullname);
171
        $PAGE->set_button(print_course_search('', true, 'navbar'));
172
        $PAGE->set_pagelayout('coursecategory');
173
        echo $OUTPUT->header();
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    }

/// Print link to roles
    if (has_capability('moodle/role:assign', $context)) {
        echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
         $context->id.'">'.get_string('assignroles','role').'</a></div>';
    }

/// Print the category selector
    $displaylist = array();
    $notused = array();
    make_categories_list($displaylist, $notused);

    echo '<div class="categorypicker">';
188
    $select = new single_select(new moodle_url('category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
189
    $select->set_label($strcategories.':');
190
    echo $OUTPUT->render($select);
191
192
193
194
    echo '</div>';

/// Print current category description
    if (!$editingon && $category->description) {
195
        echo $OUTPUT->box_start();
196
197
198
        $options = new stdClass;
        $options->noclean = true;
        $options->para = false;
199
        $options->overflowdiv = true;
200
201
202
        if (!isset($category->descriptionformat)) {
            $category->descriptionformat = FORMAT_MOODLE;
        }
203
        $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
204
        echo format_text($text, $category->descriptionformat, $options);
205
        echo $OUTPUT->box_end();
206
207
    }

208
    if ($editingon && has_capability('moodle/category:manage', $context)) {
209
        echo $OUTPUT->container_start('buttons');
moodler's avatar
moodler committed
210

211
212
        // Print button to update this category
        $options = array('id' => $category->id);
213
        echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('editcategorythis'), 'get');
214
215
216

        // Print button for creating new categories
        $options = array('parent' => $category->id);
217
        echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('addsubcategory'), 'get');
moodler's avatar
moodler committed
218

219
        echo $OUTPUT->container_end();
moodler's avatar
moodler committed
220
221
    }

moodler's avatar
moodler committed
222
/// Print out all the sub-categories
223
    if ($subcategories = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
224
        $firstentry = true;
moodler's avatar
moodler committed
225
        foreach ($subcategories as $subcategory) {
226
            if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) {
227
                $subcategorieswereshown = true;
228
                if ($firstentry) {
andreabix's avatar
andreabix committed
229
                    echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
toyomoyo's avatar
toyomoyo committed
230
                    echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
toyomoyo's avatar
toyomoyo committed
231
                    echo '<tr><td style="white-space: nowrap">';
232
233
                    $firstentry = false;
                }
234
                $catlinkcss = $subcategory->visible ? '' : ' class="dimmed" ';
235
                echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
236
                     format_string($subcategory->name).'</a><br />';
237
238
239
            }
        }
        if (!$firstentry) {
240
241
            echo '</td></tr></table>';
            echo '<br />';
moodler's avatar
moodler committed
242
243
        }
    }
urs_hunkler's avatar
urs_hunkler committed
244

moodler's avatar
moodler committed
245
/// Print out all the courses
urs_hunkler's avatar
urs_hunkler committed
246
    $courses = get_courses_page($category->id, 'c.sortorder ASC',
247
            'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
248
            $totalcount, $page*$perpage, $perpage);
249
    $numcourses = count($courses);
moodler's avatar
moodler committed
250

251
    if (!$courses) {
252
        if (empty($subcategorieswereshown)) {
253
            echo $OUTPUT->heading(get_string("nocoursesyet"));
254
        }
moodler's avatar
moodler committed
255

256
    } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
257
        echo $OUTPUT->box_start('courseboxes');
258
        print_courses($category);
259
        echo $OUTPUT->box_end();
moodler's avatar
moodler committed
260

urs_hunkler's avatar
urs_hunkler committed
261
    } else {
262
        echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
263

264
265
266
267
268
269
270
271
272
273
274
275
276
        $strcourses = get_string('courses');
        $strselect = get_string('select');
        $stredit = get_string('edit');
        $strdelete = get_string('delete');
        $strbackup = get_string('backup');
        $strrestore = get_string('restore');
        $strmoveup = get_string('moveup');
        $strmovedown = get_string('movedown');
        $strupdate = get_string('update');
        $strhide = get_string('hide');
        $strshow = get_string('show');
        $strsummary = get_string('summary');
        $strsettings = get_string('settings');
moodler's avatar
moodler committed
277

moodler's avatar
moodler committed
278

moodler's avatar
moodler committed
279
        echo '<form id="movecourses" action="category.php" method="post"><div>';
moodler's avatar
moodler committed
280
        echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
moodler's avatar
moodler committed
281
282
        echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
        echo '<th class="header" scope="col">'.$strcourses.'</th>';
283
        if ($editingon) {
moodler's avatar
moodler committed
284
            echo '<th class="header" scope="col">'.$stredit.'</th>';
285
            echo '<th class="header" scope="col">'.$strselect.'</th>';
286
        } else {
moodler's avatar
moodler committed
287
            echo '<th class="header" scope="col">&nbsp;</th>';
moodler's avatar
moodler committed
288
        }
moodler's avatar
moodler committed
289
        echo '</tr>';
moodler's avatar
moodler committed
290

moodler's avatar
moodler committed
291
292

        $count = 0;
293
        $abletomovecourses = false;  // for now
moodler's avatar
moodler committed
294

295
296
297
298
        // Checking if we are at the first or at the last page, to allow courses to
        // be moved up and down beyond the paging border
        if ($totalcount > $perpage) {
            $atfirstpage = ($page == 0);
299
300
301
302
303
            if ($perpage > 0) {
                $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
            } else {
                $atlastpage = true;
            }
304
305
306
307
308
        } else {
            $atfirstpage = true;
            $atlastpage = true;
        }

309
        foreach ($courses as $acourse) {
310
            $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
311

moodler's avatar
moodler committed
312
            $count++;
313
314
            $up = ($count > 1 || !$atfirstpage);
            $down = ($count < $numcourses || !$atlastpage);
moodler's avatar
moodler committed
315

316
            $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
moodler's avatar
moodler committed
317
            echo '<tr>';
318
            echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
319
320
            if ($editingon) {
                echo '<td>';
321
                if (has_capability('moodle/course:update', $coursecontext)) {
322
323
324
                    echo $OUTPUT->action_icon(new moodle_url('/course/edit.php',
                            array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category')),
                            new pix_icon('t/edit', $strsettings));
325
                }
326
327

                // role assignment link
328
                if (has_capability('moodle/course:enrolreview', $coursecontext)) {
329
330
                    echo $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $acourse->id)),
                            new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
331
332
                }

vyshane's avatar
vyshane committed
333
                if (can_delete_course($acourse->id)) {
334
335
                    echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)),
                            new pix_icon('t/delete', $strdelete));
336
                }
337

338
339
                // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
                if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
340
                    if (!empty($acourse->visible)) {
341
342
343
344
                        echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
                                array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
                                        'hide' => $acourse->id, 'sesskey' => sesskey())),
                                new pix_icon('t/hide', $strhide));
345
                    } else {
346
347
348
349
                        echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
                                array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
                                        'show' => $acourse->id, 'sesskey' => sesskey())),
                                new pix_icon('t/show', $strshow));
350
                    }
351
                }
352

353
                if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
354
355
                    echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)),
                            new pix_icon('t/backup', $strbackup));
356
                }
357

358
                if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
359
360
                    echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)),
                            new pix_icon('t/restore', $strrestore));
361
                }
urs_hunkler's avatar
urs_hunkler committed
362

363
                if (has_capability('moodle/category:manage', $context)) {
moodler's avatar
moodler committed
364
                    if ($up) {
365
366
367
368
                        echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
                                array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
                                        'moveup' => $acourse->id, 'sesskey' => sesskey())),
                                new pix_icon('t/up', $strmoveup));
moodler's avatar
moodler committed
369
                    }
urs_hunkler's avatar
urs_hunkler committed
370

moodler's avatar
moodler committed
371
                    if ($down) {
372
373
374
375
                        echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
                                array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
                                        'movedown' => $acourse->id, 'sesskey' => sesskey())),
                                new pix_icon('t/down', $strmovedown));
moodler's avatar
moodler committed
376
                    }
377
                    $abletomovecourses = true;
378
                }
379

moodler's avatar
moodler committed
380
                echo '</td>';
381
382
                echo '<td align="center">';
                echo '<input type="checkbox" name="c'.$acourse->id.'" />';
383
                echo '</td>';
384
            } else {
moodler's avatar
moodler committed
385
                echo '<td align="right">';
386
387
388
389
390
391
                // print enrol info
                if ($icons = enrol_get_course_info_icons($acourse)) {
                    foreach ($icons as $pix_icon) {
                        echo $OUTPUT->render($pix_icon);
                    }
                }
392
                if (!empty($acourse->summary)) {
393
394
395
                    $link = new moodle_url("/course/info.php?id=$acourse->id");
                    echo $OUTPUT->action_link($link, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
                        new popup_action('click', $link, 'courseinfo'), array('title'=>$strsummary));
396
397
                }
                echo "</td>";
moodler's avatar
moodler committed
398
399
400
            }
            echo "</tr>";
        }
401
402

        if ($abletomovecourses) {
403
404
405
406
            $movetocategories = array();
            $notused = array();
            make_categories_list($movetocategories, $notused, 'moodle/category:manage');
            $movetocategories[$category->id] = get_string('moveselectedcoursesto');
moodler's avatar
moodler committed
407
            echo '<tr><td colspan="3" align="right">';
408
409
            echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
            $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
410
            echo '<input type="hidden" name="id" value="'.$category->id.'" />';
moodler's avatar
moodler committed
411
            echo '</td></tr>';
412
        }
urs_hunkler's avatar
urs_hunkler committed
413

moodler's avatar
moodler committed
414
        echo '</table>';
moodler's avatar
moodler committed
415
        echo '</div></form>';
moodler's avatar
moodler committed
416
        echo '<br />';
moodler's avatar
moodler committed
417
    }
418

419
420
421
    echo '<div class="buttons">';
    if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
    /// Print button to re-sort courses by name
422
        unset($options);
moodler's avatar
moodler committed
423
424
        $options['id'] = $category->id;
        $options['resort'] = 'name';
Jonathan Newman's avatar
Jonathan Newman committed
425
        $options['sesskey'] = sesskey();
426
        echo $OUTPUT->single_button(new moodle_url('category.php', $options), get_string('resortcoursesbyname'), 'get');
427
    }
428

429
430
    if (has_capability('moodle/course:create', $context)) {
    /// Print button to create a new course
431
        unset($options);
moodler's avatar
moodler committed
432
        $options['category'] = $category->id;
aparup's avatar
aparup committed
433
        $options['returnto'] = 'category';
434
        echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
435
    }
436
437
438
439

    if (!empty($CFG->enablecourserequests) && $category->id == $CFG->enablecourserequests) {
        print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
    }
440
    echo '</div>';
441

skodak's avatar
skodak committed
442
    print_course_search();
443

444
    echo $OUTPUT->footer();
moodler's avatar
moodler committed
445