Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/category.php
Go to the documentation of this file.
00001 <?php
00002       // Displays the top level category or all courses
00003       // In editing mode, allows the admin to edit a category,
00004       // and rearrange courses
00005 
00006     require_once("../config.php");
00007     require_once("lib.php");
00008 
00009     $id = required_param('id', PARAM_INT); // Category id
00010     $page = optional_param('page', 0, PARAM_INT); // which page to show
00011     $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
00012     $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
00013     $hide = optional_param('hide', 0, PARAM_INT);
00014     $show = optional_param('show', 0, PARAM_INT);
00015     $moveup = optional_param('moveup', 0, PARAM_INT);
00016     $movedown = optional_param('movedown', 0, PARAM_INT);
00017     $moveto = optional_param('moveto', 0, PARAM_INT);
00018     $resort = optional_param('resort', 0, PARAM_BOOL);
00019 
00020     $site = get_site();
00021 
00022     if (empty($id)) {
00023         print_error("unknowcategory");
00024     }
00025 
00026     $PAGE->set_category_by_id($id);
00027     $urlparams = array('id' => $id);
00028     if ($page) {
00029         $urlparams['page'] = $page;
00030     }
00031     if ($perpage) {
00032         $urlparams['perpage'] = $perpage;
00033     }
00034     $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
00035     navigation_node::override_active_url($PAGE->url);
00036     $context = $PAGE->context;
00037     $category = $PAGE->category;
00038 
00039     $canedit = can_edit_in_category($category->id);
00040     if ($canedit) {
00041         if ($categoryedit !== -1) {
00042             $USER->editing = $categoryedit;
00043         }
00044         require_login();
00045         $editingon = $PAGE->user_is_editing();
00046     } else {
00047         if ($CFG->forcelogin) {
00048             require_login();
00049         }
00050         $editingon = false;
00051     }
00052 
00053     if (!$category->visible) {
00054         require_capability('moodle/category:viewhiddencategories', $context);
00055     }
00056 
00057     // Process any category actions.
00058     if (has_capability('moodle/category:manage', $context)) {
00060         if ($resort and confirm_sesskey()) {
00061             if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
00062                 $i = 1;
00063                 foreach ($courses as $course) {
00064                     $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
00065                     $i++;
00066                 }
00067                 fix_course_sortorder(); // should not be needed
00068             }
00069         }
00070     }
00071 
00072     // Process any course actions.
00073     if ($editingon) {
00075         if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
00076             // user must have category update in both cats to perform this
00077             require_capability('moodle/category:manage', $context);
00078             require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
00079 
00080             if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
00081                 print_error('cannotfindcategory', '', '', $data->moveto);
00082             }
00083 
00084             $courses = array();
00085             foreach ($data as $key => $value) {
00086                 if (preg_match('/^c\d+$/', $key)) {
00087                     $courseid = substr($key, 1);
00088                     array_push($courses, $courseid);
00089 
00090                     // check this course's category
00091                     if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
00092                         if ($movingcourse->category != $id ) {
00093                             print_error('coursedoesnotbelongtocategory');
00094                         }
00095                     } else {
00096                         print_error('cannotfindcourse');
00097                     }
00098                 }
00099             }
00100             move_courses($courses, $data->moveto);
00101         }
00102 
00104         if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
00105             if (!empty($hide)) {
00106                 $course = $DB->get_record('course', array('id' => $hide));
00107                 $visible = 0;
00108             } else {
00109                 $course = $DB->get_record('course', array('id' => $show));
00110                 $visible = 1;
00111             }
00112 
00113             if ($course) {
00114                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00115                 require_capability('moodle/course:visibility', $coursecontext);
00116                 $DB->set_field('course', 'visible', $visible, array('id' => $course->id));
00117                 $DB->set_field('course', 'visibleold', $visible, array('id' => $course->id)); // we set the old flag when user manually changes visibility of course
00118             }
00119         }
00120 
00121 
00123         if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
00124             require_capability('moodle/category:manage', $context);
00125 
00126             // Ensure the course order has continuous ordering
00127             fix_course_sortorder();
00128             $swapcourse = NULL;
00129 
00130             if (!empty($moveup)) {
00131                 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
00132                     $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
00133                 }
00134             } else {
00135                 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
00136                     $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
00137                 }
00138             }
00139             if ($swapcourse and $movecourse) {
00140                 // check course's category
00141                 if ($movecourse->category != $id) {
00142                     print_error('coursedoesnotbelongtocategory');
00143                 }
00144                 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
00145                 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
00146             }
00147         }
00148 
00149     } // End of editing stuff
00150 
00151     // Print headings
00152     $numcategories = $DB->count_records('course_categories');
00153 
00154     $stradministration = get_string('administration');
00155     $strcategories = get_string('categories');
00156     $strcategory = get_string('category');
00157     $strcourses = get_string('courses');
00158 
00159     if ($editingon && can_edit_in_category()) {
00160         // Integrate into the admin tree only if the user can edit categories at the top level,
00161         // otherwise the admin block does not appear to this user, and you get an error.
00162         require_once($CFG->libdir . '/adminlib.php');
00163         admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
00164         $PAGE->set_context($context);   // Ensure that we are actually showing blocks etc for the cat context
00165 
00166         $settingsnode = $PAGE->settingsnav->find_active_node();
00167         if ($settingsnode) {
00168             $settingsnode->make_inactive();
00169             $settingsnode->force_open();
00170             $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
00171         }
00172         echo $OUTPUT->header();
00173     } else {
00174         $PAGE->set_title("$site->shortname: $category->name");
00175         $PAGE->set_heading($site->fullname);
00176         $PAGE->set_button(print_course_search('', true, 'navbar'));
00177         $PAGE->set_pagelayout('coursecategory');
00178         echo $OUTPUT->header();
00179     }
00180 
00182     $displaylist = array();
00183     $notused = array();
00184     make_categories_list($displaylist, $notused);
00185 
00186     echo '<div class="categorypicker">';
00187     $select = new single_select(new moodle_url('category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
00188     $select->set_label($strcategories.':');
00189     echo $OUTPUT->render($select);
00190     echo '</div>';
00191 
00193     if (!$editingon && $category->description) {
00194         echo $OUTPUT->box_start();
00195         $options = new stdClass;
00196         $options->noclean = true;
00197         $options->para = false;
00198         $options->overflowdiv = true;
00199         if (!isset($category->descriptionformat)) {
00200             $category->descriptionformat = FORMAT_MOODLE;
00201         }
00202         $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
00203         echo format_text($text, $category->descriptionformat, $options);
00204         echo $OUTPUT->box_end();
00205     }
00206 
00207     if ($editingon && has_capability('moodle/category:manage', $context)) {
00208         echo $OUTPUT->container_start('buttons');
00209 
00210         // Print button to update this category
00211         $options = array('id' => $category->id);
00212         echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('editcategorythis'), 'get');
00213 
00214         // Print button for creating new categories
00215         $options = array('parent' => $category->id);
00216         echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('addsubcategory'), 'get');
00217 
00218         echo $OUTPUT->container_end();
00219     }
00220 
00222     if ($subcategories = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
00223         $firstentry = true;
00224         foreach ($subcategories as $subcategory) {
00225             if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) {
00226                 $subcategorieswereshown = true;
00227                 if ($firstentry) {
00228                     echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
00229                     echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
00230                     echo '<tr><td style="white-space: nowrap">';
00231                     $firstentry = false;
00232                 }
00233                 $catlinkcss = $subcategory->visible ? '' : ' class="dimmed" ';
00234                 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
00235                      format_string($subcategory->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $subcategory->id))).'</a><br />';
00236             }
00237         }
00238         if (!$firstentry) {
00239             echo '</td></tr></table>';
00240             echo '<br />';
00241         }
00242     }
00243 
00245     $courses = get_courses_page($category->id, 'c.sortorder ASC',
00246             'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
00247             $totalcount, $page*$perpage, $perpage);
00248     $numcourses = count($courses);
00249 
00250     if (!$courses) {
00251         if (empty($subcategorieswereshown)) {
00252             echo $OUTPUT->heading(get_string("nocoursesyet"));
00253         }
00254 
00255     } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
00256         echo $OUTPUT->box_start('courseboxes');
00257         print_courses($category);
00258         echo $OUTPUT->box_end();
00259 
00260     } else {
00261         echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
00262 
00263         $strcourses = get_string('courses');
00264         $strselect = get_string('select');
00265         $stredit = get_string('edit');
00266         $strdelete = get_string('delete');
00267         $strbackup = get_string('backup');
00268         $strrestore = get_string('restore');
00269         $strmoveup = get_string('moveup');
00270         $strmovedown = get_string('movedown');
00271         $strupdate = get_string('update');
00272         $strhide = get_string('hide');
00273         $strshow = get_string('show');
00274         $strsummary = get_string('summary');
00275         $strsettings = get_string('settings');
00276 
00277 
00278         echo '<form id="movecourses" action="category.php" method="post"><div>';
00279         echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
00280         echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
00281         echo '<th class="header" scope="col">'.$strcourses.'</th>';
00282         if ($editingon) {
00283             echo '<th class="header" scope="col">'.$stredit.'</th>';
00284             echo '<th class="header" scope="col">'.$strselect.'</th>';
00285         } else {
00286             echo '<th class="header" scope="col">&nbsp;</th>';
00287         }
00288         echo '</tr>';
00289 
00290 
00291         $count = 0;
00292         $abletomovecourses = false;  // for now
00293 
00294         // Checking if we are at the first or at the last page, to allow courses to
00295         // be moved up and down beyond the paging border
00296         if ($totalcount > $perpage) {
00297             $atfirstpage = ($page == 0);
00298             if ($perpage > 0) {
00299                 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
00300             } else {
00301                 $atlastpage = true;
00302             }
00303         } else {
00304             $atfirstpage = true;
00305             $atlastpage = true;
00306         }
00307 
00308         foreach ($courses as $acourse) {
00309             $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
00310 
00311             $count++;
00312             $up = ($count > 1 || !$atfirstpage);
00313             $down = ($count < $numcourses || !$atlastpage);
00314 
00315             $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
00316             echo '<tr>';
00317             $coursename = get_course_display_name_for_list($acourse);
00318             echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
00319             if ($editingon) {
00320                 echo '<td>';
00321                 if (has_capability('moodle/course:update', $coursecontext)) {
00322                     echo $OUTPUT->action_icon(new moodle_url('/course/edit.php',
00323                             array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category')),
00324                             new pix_icon('t/edit', $strsettings));
00325                 }
00326 
00327                 // role assignment link
00328                 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
00329                     echo $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $acourse->id)),
00330                             new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
00331                 }
00332 
00333                 if (can_delete_course($acourse->id)) {
00334                     echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)),
00335                             new pix_icon('t/delete', $strdelete));
00336                 }
00337 
00338                 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
00339                 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
00340                     if (!empty($acourse->visible)) {
00341                         echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
00342                                 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
00343                                         'hide' => $acourse->id, 'sesskey' => sesskey())),
00344                                 new pix_icon('t/hide', $strhide));
00345                     } else {
00346                         echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
00347                                 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
00348                                         'show' => $acourse->id, 'sesskey' => sesskey())),
00349                                 new pix_icon('t/show', $strshow));
00350                     }
00351                 }
00352 
00353                 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
00354                     echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)),
00355                             new pix_icon('t/backup', $strbackup));
00356                 }
00357 
00358                 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
00359                     echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)),
00360                             new pix_icon('t/restore', $strrestore));
00361                 }
00362 
00363                 if (has_capability('moodle/category:manage', $context)) {
00364                     if ($up) {
00365                         echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
00366                                 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
00367                                         'moveup' => $acourse->id, 'sesskey' => sesskey())),
00368                                 new pix_icon('t/up', $strmoveup));
00369                     }
00370 
00371                     if ($down) {
00372                         echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
00373                                 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
00374                                         'movedown' => $acourse->id, 'sesskey' => sesskey())),
00375                                 new pix_icon('t/down', $strmovedown));
00376                     }
00377                     $abletomovecourses = true;
00378                 }
00379 
00380                 echo '</td>';
00381                 echo '<td align="center">';
00382                 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
00383                 echo '</td>';
00384             } else {
00385                 echo '<td align="right">';
00386                 // print enrol info
00387                 if ($icons = enrol_get_course_info_icons($acourse)) {
00388                     foreach ($icons as $pix_icon) {
00389                         echo $OUTPUT->render($pix_icon);
00390                     }
00391                 }
00392                 if (!empty($acourse->summary)) {
00393                     $link = new moodle_url("/course/info.php?id=$acourse->id");
00394                     echo $OUTPUT->action_link($link, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
00395                         new popup_action('click', $link, 'courseinfo'), array('title'=>$strsummary));
00396                 }
00397                 echo "</td>";
00398             }
00399             echo "</tr>";
00400         }
00401 
00402         if ($abletomovecourses) {
00403             $movetocategories = array();
00404             $notused = array();
00405             make_categories_list($movetocategories, $notused, 'moodle/category:manage');
00406             $movetocategories[$category->id] = get_string('moveselectedcoursesto');
00407             echo '<tr><td colspan="3" align="right">';
00408             echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
00409             $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
00410             echo '<input type="hidden" name="id" value="'.$category->id.'" />';
00411             echo '</td></tr>';
00412         }
00413 
00414         echo '</table>';
00415         echo '</div></form>';
00416         echo '<br />';
00417     }
00418 
00419     echo '<div class="buttons">';
00420     if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
00422         unset($options);
00423         $options['id'] = $category->id;
00424         $options['resort'] = 'name';
00425         $options['sesskey'] = sesskey();
00426         echo $OUTPUT->single_button(new moodle_url('category.php', $options), get_string('resortcoursesbyname'), 'get');
00427     }
00428 
00429     if (has_capability('moodle/course:create', $context)) {
00431         unset($options);
00432         $options['category'] = $category->id;
00433         $options['returnto'] = 'category';
00434         echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
00435     }
00436 
00437     if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
00438         print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
00439     }
00440     echo '</div>';
00441 
00442     print_course_search();
00443 
00444     echo $OUTPUT->footer();
00445 
 All Data Structures Namespaces Files Functions Variables Enumerations