/** * Recursive function to print all the categories ready for editing. * * @param html_table $table The table to add data to. * @param coursecat $category The category to render * @param int $depth The depth of the category. * @param bool $up True if this category can be moved up. * @param bool $down True if this category can be moved down. */ function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) { global $OUTPUT; static $str = null; if (is_null($str)) { $str = new stdClass; $str->edit = new lang_string('edit'); $str->delete = new lang_string('delete'); $str->moveup = new lang_string('moveup'); $str->movedown = new lang_string('movedown'); $str->edit = new lang_string('editthiscategory'); $str->hide = new lang_string('hide'); $str->show = new lang_string('show'); $str->cohorts = new lang_string('cohorts', 'cohort'); $str->spacer = $OUTPUT->spacer().' '; } if ($category->id) { $categorycontext = context_coursecat::instance($category->id); $attributes = array(); $attributes['class'] = $category->visible ? '' : 'dimmed'; $attributes['title'] = $str->edit; $categoryurl = new moodle_url('/course/manage.php', array('categoryid' => $category->id, 'sesskey' => sesskey())); $categoryname = $category->get_formatted_name(); $categorypadding = str_repeat(' ', $depth); $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes); $icons = array(); if (has_capability('moodle/category:manage', $categorycontext)) { // Edit category. $icons[] = $OUTPUT->action_icon( new moodle_url('/course/editcategory.php', array('id' => $category->id)), new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->edit) ); // Delete category. $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->delete) ); // Change visibility. if (!empty($category->visible)) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->hide) ); } else { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->show) ); } // Cohorts. if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) { $icons[] = $OUTPUT->action_icon( new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)), new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->cohorts) ); } // Move up/down. if ($up) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->moveup) ); } else { $icons[] = $str->spacer; } if ($down) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->movedown) ); } else { $icons[] = $str->spacer; } } $actions = ''; if (has_capability('moodle/category:manage', $categorycontext)) { $popupurl = new moodle_url('/course/manage.php', array('movecat' => $category->id, 'sesskey' => sesskey())); $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id); $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id"); $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide')); $actions = $OUTPUT->render($select); } $table->data[] = new html_table_row(array( // Category name. new html_table_cell($categoryname), // Course count. new html_table_cell($category->coursecount), // Icons. new html_table_cell(join(' ', $icons)), // Actions. new html_table_cell($actions) )); } if ($categories = $category->get_children()) { // Print all the children recursively. $countcats = count($categories); $count = 0; $first = true; $last = false; foreach ($categories as $cat) { $count++; if ($count == $countcats) { $last = true; } $up = $first ? false : true; $down = $last ? false : true; $first = false; print_category_edit($table, $cat, $depth+1, $up, $down); } } }
/** * Renders a course listing. * * @param coursecat $category The currently selected category. This is what the listing is focused on. * @param course_in_list $course The currently selected course. * @param int $page The page being displayed. * @param int $perpage The number of courses to display per page. * @return string */ public function course_listing(coursecat $category = null, course_in_list $course = null, $page = 0, $perpage = 20) { if ($category === null) { $html = html_writer::start_div('select-a-category'); $html .= html_writer::tag('h3', get_string('courses')); $html .= $this->output->notification(get_string('selectacategory'), 'notifymessage'); $html .= html_writer::end_div(); return $html; } $page = max($page, 0); $perpage = max($perpage, 2); $totalcourses = $category->coursecount; $totalpages = ceil($totalcourses / $perpage); if ($page > $totalpages - 1) { $page = $totalpages - 1; } $options = array('offset' => $page * $perpage, 'limit' => $perpage); $courseid = isset($course) ? $course->id : null; $class = ''; if ($page === 0) { $class .= ' firstpage'; } if ($page + 1 === (int) $totalpages) { $class .= ' lastpage'; } $html = html_writer::start_div('course-listing' . $class, array('data-category' => $category->id, 'data-page' => $page, 'data-totalpages' => $totalpages, 'data-totalcourses' => $totalcourses, 'data-canmoveoutof' => $category->can_move_courses_out_of() && $category->can_move_courses_into())); $html .= html_writer::tag('h3', $category->get_formatted_name()); $html .= $this->course_listing_actions($category, $course, $perpage); $html .= $this->listing_pagination($category, $page, $perpage); $html .= html_writer::start_tag('ul', array('class' => 'ml')); foreach ($category->get_courses($options) as $listitem) { $html .= $this->course_listitem($category, $listitem, $courseid); } $html .= html_writer::end_tag('ul'); $html .= $this->listing_pagination($category, $page, $perpage, true); $html .= $this->course_bulk_actions($category); $html .= html_writer::end_div(); return $html; }
/** * Defines the form. */ public function definition() { $mform = $this->_form; $this->coursecat = $this->_customdata; $categorycontext = context_coursecat::instance($this->coursecat->id); $categoryname = $this->coursecat->get_formatted_name(); // Check permissions, to see if it OK to give the option to delete // the contents, rather than move elsewhere. $candeletecontent = $this->coursecat->can_delete_full(); // Get the list of categories we might be able to move to. $displaylist = $this->coursecat->move_content_targets_list(); // Now build the options. $options = array(); if ($displaylist) { $options[0] = get_string('movecontentstoanothercategory'); } if ($candeletecontent) { $options[1] = get_string('deleteallcannotundo'); } if (empty($options)) { print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname); } // Now build the form. $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname)); // Describe the contents of this category. $contents = ''; if ($this->coursecat->has_children()) { $contents .= '<li>' . get_string('subcategories') . '</li>'; } if ($this->coursecat->has_courses()) { $contents .= '<li>' . get_string('courses') . '</li>'; } if (question_context_has_any_questions($categorycontext)) { $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>'; } if (!empty($contents)) { $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents)); } else { $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty')); } // Give the options for what to do. $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options); if (count($options) == 1) { $optionkeys = array_keys($options); $option = reset($optionkeys); $mform->hardFreeze('fulldelete'); $mform->setConstant('fulldelete', $option); } if ($displaylist) { $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist); if (in_array($this->coursecat->parent, $displaylist)) { $mform->setDefault('newparent', $this->coursecat->parent); } $mform->disabledIf('newparent', 'fulldelete', 'eq', '1'); } $mform->addElement('hidden', 'categoryid', $this->coursecat->id); $mform->setType('categoryid', PARAM_ALPHANUM); $mform->addElement('hidden', 'action', 'deletecategory'); $mform->setType('action', PARAM_ALPHANUM); $mform->addElement('hidden', 'sure'); // This gets set by default to ensure that if the user changes it manually we can detect it. $mform->setDefault('sure', md5(serialize($this->coursecat))); $mform->setType('sure', PARAM_ALPHANUM); $this->add_action_buttons(true, get_string('delete')); }