private function get_category_tree($id) { global $DB; $category = $DB->get_record('course_categories', array('id' => $id)); if ($id && !$category) { cli_error("Wrong category '{$id}'"); } elseif (!$id) { $category = NULL; } $parentcategory = \coursecat::get($id); if ($parentcategory->has_children()) { $parentschildren = $parentcategory->get_children(); foreach ($parentschildren as $singlecategory) { if ($singlecategory->has_children()) { $childcategories = $this->get_category_tree($singlecategory->id); $category->categories[] = $childcategories; } else { // coursecat variables are protected, need to get data from db $singlecategory = $DB->get_record('course_categories', array('id' => $singlecategory->id)); $category->categories[] = $singlecategory; } } } return $category; }
/** * Campus theme with the underlying Bootstrap theme. * * @package theme * @subpackage campus * @copyright © 2014-onwards G J Barnard in respect to modifications of the Clean theme. * @copyright © 2014-onwards Work undertaken for David Bogner of Edulabs.org. * @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195} * @author Based on code originally written by Mary Evans, Bas Brands, Stuart Lamour and David Scotson. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function theme_campus_get_top_level_categories() { global $CFG; include_once $CFG->libdir . '/coursecatlib.php'; $categoryids = array(); $categories = coursecat::get(0)->get_children(); // Parent = 0 i.e. top-level categories only. foreach ($categories as $category) { $categoryids[$category->id] = $category->name; } return $categoryids; }
public function execute() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/lib/coursecatlib.php'; foreach ($this->arguments as $argument) { $this->expandOptionsManually(array($argument)); } $this->expandOptions(); $options = $this->expandedOptions; $params = NULL; $sql = "SELECT c.id,c.category,"; if ($options['idnumber']) { $sql .= "c.idnumber,"; } if ($options['empty'] == 'yes' || $options['empty'] == 'no') { $sql .= "COUNT(c.id) AS modules,"; } $sql .= "c.shortname,c.fullname,c.visible FROM {course} c "; if ($options['empty'] == 'yes' || $options['empty'] == 'no') { $sql .= " LEFT JOIN {course_modules} m ON c.id=m.course "; } if ($options['categorysearch'] !== null) { $category = \coursecat::get($options['categorysearch']); $categories = $this->get_categories($category); list($where, $params) = $DB->get_in_or_equal(array_keys($categories)); $sql .= "WHERE c.category {$where}"; } if (isset($this->arguments[0]) && $this->arguments[0]) { $sql .= " AND (" . $this->arguments[0] . ")"; } if ($options['empty'] == 'yes') { $sql .= " GROUP BY c.id HAVING modules < 2"; } if ($options['empty'] == 'no') { $sql .= " GROUP BY c.id HAVING modules > 1"; } //echo "$sql\n"; var_dump($params); $courses = $DB->get_records_sql($sql, $params); // Filter out any that have any section information (summary) if ($options['empty'] == 'yes') { $sql = "SELECT COUNT(*) AS C FROM {course_sections} WHERE course = ? AND summary <> ''"; foreach ($courses as $k => $course) { $sections = $DB->get_record_sql($sql, array($course->id)); if ($sections->c > 0) { unset($courses[$k]); } } } // @TODO: If empty == no, then add those that have no modules but some modification to sections $this->display($courses); }
/** * Check that our hook is called when a course is deleted. */ public function test_pre_course_category_delete_hook() { global $DB; // Should have nothing in the recycle bin. $this->assertEquals(0, $DB->count_records('tool_recyclebin_category')); delete_course($this->course, false); // Check the course is now in the recycle bin. $this->assertEquals(1, $DB->count_records('tool_recyclebin_category')); // Now let's delete the course category. $category = coursecat::get($this->course->category); $category->delete_full(false); // Check that the course was deleted from the category recycle bin. $this->assertEquals(0, $DB->count_records('tool_recyclebin_category')); }
/** * Renderers a search result course list item. * * This function will be called for every course being displayed by course_listing. * * @param course_in_list $course The course to produce HTML for. * @param int $selectedcourse The id of the currently selected course. * @return string */ public function search_listitem(course_in_list $course, $selectedcourse) { $text = $course->get_formatted_name(); $attributes = array('class' => 'listitem listitem-course', 'data-id' => $course->id, 'data-selected' => $selectedcourse == $course->id ? '1' : '0', 'data-visible' => $course->visible ? '1' : '0'); $bulkcourseinput = array('type' => 'checkbox', 'name' => 'bc[]', 'value' => $course->id, 'class' => 'bulk-action-checkbox'); $viewcourseurl = new moodle_url($this->page->url, array('courseid' => $course->id)); $categoryname = coursecat::get($course->category)->get_formatted_name(); $html = html_writer::start_tag('li', $attributes); $html .= html_writer::start_div('clearfix'); $html .= html_writer::start_div('float-left'); $html .= html_writer::empty_tag('input', $bulkcourseinput) . ' '; $html .= html_writer::end_div(); $html .= html_writer::link($viewcourseurl, $text, array('class' => 'float-left coursename')); $html .= html_writer::tag('span', $categoryname, array('class' => 'float-left categoryname')); $html .= html_writer::start_div('float-right'); $html .= $this->search_listitem_actions($course); $html .= html_writer::tag('span', s($course->idnumber), array('class' => 'dimmed idnumber')); $html .= html_writer::end_div(); $html .= html_writer::end_div(); $html .= html_writer::end_tag('li'); return $html; }
public function frontpage_available_courses() { /* available courses */ global $CFG, $OUTPUT, $PAGE; require_once $CFG->libdir . '/coursecatlib.php'; $chelper = new coursecat_helper(); $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->set_courses_display_options(array('recursive' => true, 'limit' => $CFG->frontpagecourselimit, 'viewmoreurl' => new moodle_url('/course/index.php'), 'viewmoretext' => new lang_string('fulllistofcourses'))); $chelper->set_attributes(array('class' => 'frontpage-course-list-all')); $courses = coursecat::get(0)->get_courses($chelper->get_courses_display_options()); $totalcount = coursecat::get(0)->get_courses_count($chelper->get_courses_display_options()); $rcourseids = array_keys($courses); $acourseids = array_chunk($rcourseids, 5); $newcourse = get_string('availablecourses'); $header = '<div id="frontpage-course-list"> <h2>' . $newcourse . '</h2> <div class="courses frontpage-course-list-all">'; $footer = ' </div> </div>'; $content = ''; if (count($rcourseids) > 0) { foreach ($acourseids as $courseids) { $content .= '<div class="row-fluid">'; $rowcontent = ''; foreach ($courseids as $courseid) { $course = get_course($courseid); $no = get_config('theme_pioneer', 'patternselect'); $nimgp = empty($no) || $no == "default" ? 'no-image' : 'cs0' . $no . '/no-image'; $summary = theme_pioneer_strip_html_tags($course->summary); $summary = theme_pioneer_course_trim_char($summary, 20); $trimtitle = theme_pioneer_course_trim_char($course->fullname, 25); $noimgurl = $OUTPUT->pix_url($nimgp, 'theme'); $courseurl = new moodle_url('/course/view.php', array('id' => $courseid)); if ($course instanceof stdClass) { require_once $CFG->libdir . '/coursecatlib.php'; $course = new course_in_list($course); } $imgurl = ''; $context = context_course::instance($course->id); foreach ($course->get_course_overviewfiles() as $file) { $isimage = $file->is_valid_image(); $imgurl = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); if (!$isimage) { $imgurl = $noimgurl; } } if (empty($imgurl)) { $imgurl = $PAGE->theme->setting_file_url('headerbackgroundimage', 'headerbackgroundimage', true); if (!$imgurl) { $imgurl = $noimgurl; } } $rowcontent .= ' <div style="background-image: url(' . $imgurl . ');background-repeat: no-repeat;background-size:cover; background-position:center;" class="fp-avail-courses"> <div class="fp-coursebox"> <div class="fp-courseinfo"> <p><a href="' . $courseurl . '" id="button" data-toggle="tooltip" data-placement="top" title="' . $course->fullname . '" >' . $trimtitle . '</a></p> </div> </div> </div> '; } $content .= $rowcontent; $content .= '</div>'; } } $coursehtml = $header . $content . $footer; echo $coursehtml; if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) { // Print link to create a new course, for the 1st available category. echo $this->add_new_course_button(); } }
protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) { global $CFG; if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { return ''; } if ($course instanceof stdClass) { require_once $CFG->libdir . '/coursecatlib.php'; $course = new course_in_list($course); } $content = ''; // Display course overview files. $contentimages = $contentfiles = ''; foreach ($course->get_course_overviewfiles() as $file) { $isimage = $file->is_valid_image(); $url = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); if ($isimage) { $contentimages .= html_writer::start_tag('div', array('class' => 'imagebox')); $images = html_writer::empty_tag('img', array('src' => $url, 'alt' => 'Course Image ' . $course->fullname, 'class' => 'courseimage')); $contentimages .= html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $images); $contentimages .= html_writer::end_tag('div'); } else { $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle'); $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')) . html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename')); $contentfiles .= html_writer::tag('span', html_writer::link($url, $filename), array('class' => 'coursefile fp-filename-icon')); } } $content .= $contentimages . $contentfiles; // Display course summary. if ($course->has_summary()) { $content .= $chelper->get_course_formatted_summary($course); } // Display course contacts. See course_in_list::get_course_contacts(). if ($course->has_course_contacts()) { $content .= html_writer::start_tag('ul', array('class' => 'teachers')); foreach ($course->get_course_contacts() as $userid => $coursecontact) { $name = $coursecontact['rolename'] . ': ' . html_writer::link(new moodle_url('/user/view.php', array('id' => $userid, 'course' => SITEID)), $coursecontact['username']); $content .= html_writer::tag('li', $name); } $content .= html_writer::end_tag('ul'); // End .teachers div. } // Display course category if necessary (for example in search results). if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) { require_once $CFG->libdir . '/coursecatlib.php'; if ($cat = coursecat::get($course->category, IGNORE_MISSING)) { $content .= html_writer::start_tag('div', array('class' => 'coursecat')); $content .= get_string('category') . ': ' . html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed')); $content .= html_writer::end_tag('div'); // End .coursecat div. } } return $content; }
/** * Return the course information that is public (visible by every one) * * @param course_in_list $course course in list object * @param stdClass $coursecontext course context object * @return array the course information * @since Moodle 3.2 */ protected static function get_course_public_information(course_in_list $course, $coursecontext) { static $categoriescache = array(); // Category information. if (!array_key_exists($course->category, $categoriescache)) { $categoriescache[$course->category] = coursecat::get($course->category, IGNORE_MISSING); } $category = $categoriescache[$course->category]; // Retrieve course overview used files. $files = array(); foreach ($course->get_course_overviewfiles() as $file) { $fileurl = moodle_url::make_webservice_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename())->out(false); $files[] = array('filename' => $file->get_filename(), 'fileurl' => $fileurl, 'filesize' => $file->get_filesize(), 'filepath' => $file->get_filepath(), 'mimetype' => $file->get_mimetype(), 'timemodified' => $file->get_timemodified()); } // Retrieve the course contacts, // we need here the users fullname since if we are not enrolled can be difficult to obtain them via other Web Services. $coursecontacts = array(); foreach ($course->get_course_contacts() as $contact) { $coursecontacts[] = array('id' => $contact['user']->id, 'fullname' => $contact['username']); } // Allowed enrolment methods (maybe we can self-enrol). $enroltypes = array(); $instances = enrol_get_instances($course->id, true); foreach ($instances as $instance) { $enroltypes[] = $instance->enrol; } // Format summary. list($summary, $summaryformat) = external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null); $displayname = get_course_display_name_for_list($course); $coursereturns = array(); $coursereturns['id'] = $course->id; $coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id); $coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id); $coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id); $coursereturns['categoryid'] = $course->category; $coursereturns['categoryname'] = $category == null ? '' : $category->name; $coursereturns['summary'] = $summary; $coursereturns['summaryformat'] = $summaryformat; $coursereturns['summaryfiles'] = external_util::get_area_files($coursecontext->id, 'course', 'summary', false, false); $coursereturns['overviewfiles'] = $files; $coursereturns['contacts'] = $coursecontacts; $coursereturns['enrollmentmethods'] = $enroltypes; return $coursereturns; }
/** * Print courses in category. If category is 0 then all courses are printed. * * @deprecated since 2.5 * * To print a generic list of courses use: * $renderer = $PAGE->get_renderer('core', 'course'); * echo $renderer->courses_list($courses); * * To print list of all courses: * $renderer = $PAGE->get_renderer('core', 'course'); * echo $renderer->frontpage_available_courses(); * * To print list of courses inside category: * $renderer = $PAGE->get_renderer('core', 'course'); * echo $renderer->course_category($category); // this will also print subcategories * * @param int|stdClass $category category object or id. * @return bool true if courses found and printed, else false. */ function print_courses($category) { global $CFG, $OUTPUT, $PAGE; require_once $CFG->libdir . '/coursecatlib.php'; debugging('Function print_courses() is deprecated, please use course renderer', DEBUG_DEVELOPER); if (!is_object($category) && $category == 0) { $courses = coursecat::get(0)->get_courses(array('recursive' => true, 'summary' => true, 'coursecontacts' => true)); } else { $courses = coursecat::get($category->id)->get_courses(array('summary' => true, 'coursecontacts' => true)); } if ($courses) { $renderer = $PAGE->get_renderer('core', 'course'); echo $renderer->courses_list($courses); } else { echo $OUTPUT->heading(get_string("nocoursesyet")); $context = context_system::instance(); if (has_capability('moodle/course:create', $context)) { $options = array(); if (!empty($category->id)) { $options['category'] = $category->id; } else { $options['category'] = $CFG->defaultrequestcategory; } echo html_writer::start_tag('div', array('class' => 'addcoursebutton')); echo $OUTPUT->single_button(new moodle_url('/course/edit.php', $options), get_string("addnewcourse")); echo html_writer::end_tag('div'); return false; } } return true; }
if ($down && empty($searchcriteria)) { $url = new moodle_url($baseurl, array('movedown' => $acourse->id)); $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))); } $abletomovecourses = true; } $table->data[] = new html_table_row(array( new html_table_cell($coursename), new html_table_cell(join('', $icons)), new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id))) )); if (!empty($searchcriteria)) { // add 'Category' column $category = coursecat::get($acourse->category, IGNORE_MISSING, true); $cell = new html_table_cell($category->get_formatted_name()); $cell->attributes['class'] = $category->visible ? '' : 'dimmed_text'; array_splice($table->data[count($table->data) - 1]->cells, 1, 0, array($cell)); } } if ($abletomovecourses) { $movetocategories = coursecat::make_categories_list('moodle/category:manage'); $movetocategories[$id] = get_string('moveselectedcoursesto'); $cell = new html_table_cell(); $cell->colspan = 3; $cell->attributes['class'] = 'mdl-right'; $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide')); $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id' => 'movetoid', 'class' => 'autosubmit'));
private static function traverse_categories($categories, &$cid) { foreach ($categories as $category) { $cid[] = $category->id; $catchildren = \coursecat::get($category->id, IGNORE_MISSING, true)->get_children(); if ($catchildren) { self::traverse_categories($catchildren, $cid); } } }
/** * Construct contents of course_overview block * * @param array $courses list of courses in sorted order * @param array $overviews list of course overviews * @return string html to be displayed in course_overview block */ public function course_overview($courses, $overviews) { global $DB; $html = ''; $config = get_config('block_course_overview'); if ($config->showcategories != BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE) { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; } /*echo '<pre>'; print_r($courses);*/ $ismovingcourse = false; $courseordernumber = 0; $maxcourses = count($courses); $userediting = false; // Intialise string/icon etc if user is editing and courses > 1 if ($this->page->user_is_editing() && count($courses) > 1) { $userediting = true; $this->page->requires->js_init_call('M.block_course_overview.add_handles'); // Check if course is moving $ismovingcourse = optional_param('movecourse', FALSE, PARAM_BOOL); $movingcourseid = optional_param('courseid', 0, PARAM_INT); } /** * Indexador das categorias dos cursos * @author Cássio Queiroz <Cruzz> * @date 2015/12/26 */ $catids = array(); foreach ($courses as $key => $course) { $value = $DB->get_record_sql('SELECT id,name FROM {course_categories} WHERE id = ? ', array($course->category)); $catids[] = $value->id . '@' . $value->name; } $firstCat = explode("@", $catids[0]); $catids = array_unique($catids); // Render first movehere icon. if ($ismovingcourse) { // Remove movecourse param from url. $this->page->ensure_param_not_in_url('movecourse'); // Show moving course notice, so user knows what is being moved. $html .= $this->output->box_start('notice'); $a = new stdClass(); $a->fullname = $courses[$movingcourseid]->fullname; $a->cancellink = html_writer::link($this->page->url, get_string('cancel')); $html .= get_string('movingcourse', 'block_course_overview', $a); $html .= $this->output->box_end(); $moveurl = new moodle_url('/blocks/course_overview/move.php', array('sesskey' => sesskey(), 'moveto' => 0, 'courseid' => $movingcourseid)); // Create move icon, so it can be used. $movetofirsticon = html_writer::empty_tag('img', array('src' => $this->output->pix_url('movehere'), 'alt' => get_string('movetofirst', 'block_course_overview', $courses[$movingcourseid]->fullname), 'title' => get_string('movehere'))); $moveurl = html_writer::link($moveurl, $movetofirsticon); $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere')); } // @autor Cássio Queiroz <Cruzz> $html .= html_writer::start_tag('div', array('class' => 'cat_navigation')); $html .= html_writer::start_span('top-level') . 'Meus Cursos' . html_writer::end_span(); $html .= html_writer::start_tag('div', array('class' => 'cat_navs')); $piper = ''; foreach ($catids as $cor) { $name = explode('@', $cor); $html .= $piper . html_writer::start_span('allcat category-' . $name[0]) . $name[1] . html_writer::end_span(); $piper = ' | '; } $html .= html_writer::end_tag('div'); // END cat_navs $html .= html_writer::end_tag('div'); // END cat_navigation foreach ($courses as $key => $course) { // If moving course, then don't show course which needs to be moved. if ($ismovingcourse && $course->id == $movingcourseid) { continue; } $html .= html_writer::start_tag('div', array('class' => 'all cat_' . $course->category)); // @autor Cássio Queiroz <Cruzz> $html .= $this->output->box_start('coursebox', "course-{$course->id}"); $html .= html_writer::start_tag('div', array('class' => 'course_title')); // If user is editing, then add move icons. if ($userediting && !$ismovingcourse) { $moveicon = html_writer::empty_tag('img', array('src' => $this->pix_url('t/move')->out(false), 'alt' => get_string('movecourse', 'block_course_overview', $course->fullname), 'title' => get_string('move'))); $moveurl = new moodle_url($this->page->url, array('sesskey' => sesskey(), 'movecourse' => 1, 'courseid' => $course->id)); $moveurl = html_writer::link($moveurl, $moveicon); $html .= html_writer::tag('div', $moveurl, array('class' => 'move')); } // No need to pass title through s() here as it will be done automatically by html_writer. $attributes = array('title' => $course->fullname); if ($course->id > 0) { if (empty($course->visible)) { $attributes['class'] = 'dimmed'; } $courseurl = new moodle_url('/course/view.php', array('id' => $course->id)); $coursefullname = format_string(get_course_display_name_for_list($course), true, $course->id); $link = html_writer::link($courseurl, $coursefullname, $attributes); $html .= $this->output->heading($link, 2, 'title'); } else { $html .= $this->output->heading(html_writer::link(new moodle_url('/auth/mnet/jump.php', array('hostid' => $course->hostid, 'wantsurl' => '/course/view.php?id=' . $course->remoteid)), format_string($course->shortname, true), $attributes) . ' (' . format_string($course->hostname) . ')', 2, 'title'); } $html .= $this->output->box('', 'flush'); $html .= html_writer::end_tag('div'); // hide-div $html .= html_writer::end_tag('div'); if (!empty($config->showchildren) && $course->id > 0) { // List children here. if ($children = block_course_overview_get_child_shortnames($course->id)) { $html .= html_writer::tag('span', $children, array('class' => 'coursechildren')); } } // If user is moving courses, then down't show overview. if (isset($overviews[$course->id]) && !$ismovingcourse) { $html .= $this->activity_display($course->id, $overviews[$course->id]); } if ($config->showcategories != BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE) { // List category parent or categories path here. $currentcategory = coursecat::get($course->category, IGNORE_MISSING); if ($currentcategory !== null) { $html .= html_writer::start_tag('div', array('class' => 'categorypath')); if ($config->showcategories == BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH) { foreach ($currentcategory->get_parents() as $categoryid) { $category = coursecat::get($categoryid, IGNORE_MISSING); if ($category !== null) { $html .= $category->get_formatted_name() . ' / '; } } } $html .= $currentcategory->get_formatted_name(); $html .= html_writer::end_tag('div'); } } $html .= $this->output->box('', 'flush'); $html .= $this->output->box_end(); $courseordernumber++; if ($ismovingcourse) { $moveurl = new moodle_url('/blocks/course_overview/move.php', array('sesskey' => sesskey(), 'moveto' => $courseordernumber, 'courseid' => $movingcourseid)); $a = new stdClass(); $a->movingcoursename = $courses[$movingcourseid]->fullname; $a->currentcoursename = $course->fullname; $movehereicon = html_writer::empty_tag('img', array('src' => $this->output->pix_url('movehere'), 'alt' => get_string('moveafterhere', 'block_course_overview', $a), 'title' => get_string('movehere'))); $moveurl = html_writer::link($moveurl, $movehereicon); $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere')); } } /** * Função jquery para mostrar os cursos de acordo com a categoria * @author Cássio Queiroz <Cruzz> * @date 2015/12/26 */ $html .= '<script type="text/javascript"> $(document).ready(function(){ $(".all").hide(); var last = $(".category-' . $firstCat[0] . '"); $(".cat_' . $firstCat[0] . '").fadeIn(); $(".category-' . $firstCat[0] . '").css("color","orange"); $(".allcat").click(function(){ if(typeof last === "undefined"){ last = $(this); } last.css("color","#656565"); last = $(this); $(".all").hide(); $(this).css("color","orange"); var name = $(this).attr("class").split("-"); $(".cat_"+name[1]).fadeIn(); }); }); </script> '; // Wrap course list in a div and return. return html_writer::tag('div', $html, array('class' => 'course_list')); }
/** * Gets the category course title image category id for the given category or 0 if not found. * Walks up the parent tree if the current category does not have an image. * * @return int Category id. */ protected function get_categorycti_catid() { $catid = 0; $currentcatid = $this->get_current_category(); if ($currentcatid) { $image = $this->get_setting('categoryct' . $currentcatid . 'image'); if ($image) { $catid = $currentcatid; } else { $imageurl = $this->get_setting('categoryctimageurl' . $currentcatid); if ($imageurl) { $catid = $currentcatid; } else { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; $parents = array_reverse(\coursecat::get($currentcatid, IGNORE_MISSING, true)->get_parents()); foreach ($parents as $parent) { $image = $this->get_setting('categoryct' . $parent . 'image'); if ($image) { $catid = $parent; break; } $imageurl = $this->get_setting('categoryctimageurl' . $parent); if ($imageurl) { $catid = $parent; break; } } } } } return $catid; }
* When called with an id parameter, edits the category with that id. * Otherwise it creates a new category with default parent from the parent * parameter, which may be 0. * * @package core_course * @copyright 2007 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once '../config.php'; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->libdir . '/coursecatlib.php'; require_login(); $id = optional_param('id', 0, PARAM_INT); $url = new moodle_url('/course/editcategory.php'); if ($id) { $coursecat = coursecat::get($id, MUST_EXIST, true); $category = $coursecat->get_db_record(); $context = context_coursecat::instance($id); $url->param('id', $id); $strtitle = new lang_string('editcategorysettings'); $itemid = 0; // Initialise itemid, as all files in category description has item id 0. $title = $strtitle; $fullname = $coursecat->get_formatted_name(); } else { $parent = required_param('parent', PARAM_INT); $url->param('parent', $parent); if ($parent) { $DB->record_exists('course_categories', array('id' => $parent), '*', MUST_EXIST); $context = context_coursecat::instance($parent); } else {
/** * Obtains all categories that are children to a specific one * * @param unknown $id_category * @return Ambigous <string, unknown> */ function emarking_get_categories_childs($id_category) { $coursecat = coursecat::get($id_category); $ids = array(); $ids[] = $id_category; foreach ($coursecat->get_children() as $id => $childcategory) { $ids[] = $id; } return $ids; }
/** * Apply filtering based on a category * * @return array The structured list of courses as organized by the filter */ private function _filter_by_category() { $mycat = coursecat::get($this->fclconfig->categories, IGNORE_MISSING) ? $this->fclconfig->categories : 0; $mycats = $this->_get_cat_and_descendants($mycat); $results = array(); $other = array(); foreach ($mycats as $cat) { foreach ($this->mycourses as $key => $course) { if ($course->id == SITEID) { continue; } if ($course->category == $cat->id) { $results[$cat->name][] = $course; unset($this->mycourses[$key]); } } } if ($this->fclconfig->hideothercourses == BLOCK_FILTERED_COURSE_LIST_FALSE) { foreach ($this->mycourses as $course) { $other[] = $course; } $results[get_string('othercourses', 'block_filtered_course_list')] = $other; } return $results; }
/** * Searches for categories using a substring match. * * @param string $query The substring to search for. * @return array Matching categories or returns no matches found. */ function report_ncccscensus_category_search($query) { global $DB; $sqlquery = 'SELECT id, name FROM {course_categories} WHERE name LIKE ? '; $categories = $DB->get_records_sql($sqlquery, array("%{$query}%")); $results = array(); foreach ($categories as $category) { $category = coursecat::get($category->id); $parents = $category->get_parents(); $categorynames = array(); foreach ($parents as $categoryid) { $tempcategory = coursecat::get($categoryid); array_push($categorynames, $tempcategory->get_formatted_name()); } array_push($categorynames, $category->get_formatted_name()); $name = array_pop($categorynames); if (count($categorynames) > 0) { $name = array_pop($categorynames) . ' / ' . $name; } array_push($results, array('id' => $category->id, 'name' => $name)); } if (count($results) === 0) { array_push($results, array('name' => get_string('noresults', 'report_ncccscensus') . " {$query}")); } return $results; }
/** * Load categories to display * * @access public */ public function load_categories() { global $DB; // If category 0, make fake object if (!$this->categoryid) { $parent = new stdClass(); $parent->id = 0; } else { // Load category if (!$parent = $DB->get_record('course_categories', array('id' => $this->categoryid))) { print_error('error:categoryidincorrect', 'totara_core'); } } // Load child categories $categories = coursecat::get($parent->id)->get_children(); $category_ids = array(); foreach ($categories as $cat) { $category_ids[] = $cat->id; } // Get item counts for categories $category_item_counts = (count($category_ids) > 0) ? local_get_category_item_count($category_ids, 'program') : array(); // Fix array to be indexed by prefixed id's (so it doesn't conflict with course id's) foreach ($categories as $category) { $item_count = array_key_exists($category->id, $category_item_counts) ? $category_item_counts[$category->id] : 0; //Dont show category if there are no items in it if ($item_count > 0) { $c = new stdClass(); $c->id = 'cat'.$category->id; $c->fullname = $category->name; $this->categories[$c->id] = $c; } } // Also fill parents array $this->parent_items = $this->categories; // Make categories unselectable $this->disabled_items = $this->parent_items; }
/** * Returns HTML to print tree of course categories (with number of courses) for the frontpage * * @return string */ public function frontpage_categories_list() { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; $chelper = new coursecat_helper(); $chelper->set_subcat_depth($CFG->maxcategorydepth)->set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->set_categories_display_options(array('limit' => $CFG->coursesperpage, 'viewmoreurl' => new moodle_url('/course/index.php', array('browse' => 'categories', 'page' => 1))))->set_attributes(array('class' => 'frontpage-category-names')); return $this->coursecat_tree($chelper, coursecat::get(0)); }
/** * Returns an array of all categoryids that have the given category as a parent and their visible value. * @param int $categoryid * @return array */ public static function get_category_children_visibility($categoryid) { global $DB; $category = \coursecat::get($categoryid); $select = $DB->sql_like('path', ':path'); $path = $category->path . '/%'; $sql = "SELECT c.id, c.visible\n FROM {course_categories} c\n WHERE " . $select; $params = array('path' => $path); return $DB->get_records_sql($sql, $params); }
function definition() { global $USER, $CFG, $DB, $PAGE; $mform = $this->_form; $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser', array(array('formid' => $mform->getAttribute('id')))); $course = $this->_customdata['course']; // this contains the data of this form $category = $this->_customdata['category']; $editoroptions = $this->_customdata['editoroptions']; $returnto = $this->_customdata['returnto']; $systemcontext = context_system::instance(); $categorycontext = context_coursecat::instance($category->id); if (!empty($course->id)) { $coursecontext = context_course::instance($course->id); $context = $coursecontext; } else { $coursecontext = null; $context = $categorycontext; } $courseconfig = get_config('moodlecourse'); $this->course = $course; $this->context = $context; /// form definition with new course defaults //-------------------------------------------------------------------------------- $mform->addElement('header', 'general', get_string('general', 'form')); $mform->addElement('hidden', 'returnto', null); $mform->setType('returnto', PARAM_ALPHANUM); $mform->setConstant('returnto', $returnto); $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"'); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_TEXT); if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) { $mform->hardFreeze('fullname'); $mform->setConstant('fullname', $course->fullname); } $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_TEXT); if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) { $mform->hardFreeze('shortname'); $mform->setConstant('shortname', $course->shortname); } // Verify permissions to change course category or keep current. if (empty($course->id)) { if (has_capability('moodle/course:create', $categorycontext)) { $displaylist = coursecat::make_categories_list('moodle/course:create'); $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist); $mform->addHelpButton('category', 'coursecategory'); $mform->setDefault('category', $category->id); } else { $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $category->id); } } else { if (has_capability('moodle/course:changecategory', $coursecontext)) { $displaylist = coursecat::make_categories_list('moodle/course:create'); if (!isset($displaylist[$course->category])) { //always keep current $displaylist[$course->category] = coursecat::get($course->category)->get_formatted_name(); } $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist); $mform->addHelpButton('category', 'coursecategory'); } else { //keep current $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $course->category); } } $choices = array(); $choices['0'] = get_string('hide'); $choices['1'] = get_string('show'); $mform->addElement('select', 'visible', get_string('visible'), $choices); $mform->addHelpButton('visible', 'visible'); $mform->setDefault('visible', $courseconfig->visible); if (!has_capability('moodle/course:visibility', $context)) { $mform->hardFreeze('visible'); if (!empty($course->id)) { $mform->setConstant('visible', $course->visible); } else { $mform->setConstant('visible', $courseconfig->visible); } } $mform->addElement('date_selector', 'startdate', get_string('startdate')); $mform->addHelpButton('startdate', 'startdate'); $mform->setDefault('startdate', time() + 3600 * 24); $mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"'); $mform->addHelpButton('idnumber', 'idnumbercourse'); $mform->setType('idnumber', PARAM_RAW); if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) { $mform->hardFreeze('idnumber'); $mform->setConstants('idnumber', $course->idnumber); } // Description. $mform->addElement('header', 'descriptionhdr', get_string('description')); $mform->setExpanded('descriptionhdr'); $mform->addElement('editor', 'summary_editor', get_string('coursesummary'), null, $editoroptions); $mform->addHelpButton('summary_editor', 'coursesummary'); $mform->setType('summary_editor', PARAM_RAW); $summaryfields = 'summary_editor'; if ($overviewfilesoptions = course_overviewfiles_options($course)) { $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions); $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles'); $summaryfields .= ',overviewfiles_filemanager'; } if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) { // Remove the description header it does not contain anything any more. $mform->removeElement('descriptionhdr'); $mform->hardFreeze($summaryfields); } // Course format. $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin')); $courseformats = get_sorted_course_formats(true); $formcourseformats = array(); foreach ($courseformats as $courseformat) { $formcourseformats[$courseformat] = get_string('pluginname', "format_{$courseformat}"); } if (isset($course->format)) { $course->format = course_get_format($course)->get_format(); // replace with default if not found if (!in_array($course->format, $courseformats)) { // this format is disabled. Still display it in the dropdown $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle', get_string('pluginname', 'format_' . $course->format)); } } $mform->addElement('select', 'format', get_string('format'), $formcourseformats); $mform->addHelpButton('format', 'format'); $mform->setDefault('format', $courseconfig->format); // Button to update format-specific options on format change (will be hidden by JavaScript). $mform->registerNoSubmitButton('updatecourseformat'); $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate')); // Just a placeholder for the course format options. $mform->addElement('hidden', 'addcourseformatoptionshere'); $mform->setType('addcourseformatoptionshere', PARAM_BOOL); // Appearance. $mform->addElement('header', 'appearancehdr', get_string('appearance')); if (!empty($CFG->allowcoursethemes)) { $themeobjects = get_list_of_themes(); $themes = array(); $themes[''] = get_string('forceno'); foreach ($themeobjects as $key => $theme) { if (empty($theme->hidefromselector)) { $themes[$key] = get_string('pluginname', 'theme_' . $theme->name); } } $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); } $languages = array(); $languages[''] = get_string('forceno'); $languages += get_string_manager()->get_list_of_translations(); $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages); $mform->setDefault('lang', $courseconfig->lang); $options = range(0, 10); $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options); $mform->addHelpButton('newsitems', 'newsitemsnumber'); $mform->setDefault('newsitems', $courseconfig->newsitems); $mform->addElement('selectyesno', 'showgrades', get_string('showgrades')); $mform->addHelpButton('showgrades', 'showgrades'); $mform->setDefault('showgrades', $courseconfig->showgrades); $mform->addElement('selectyesno', 'showreports', get_string('showreports')); $mform->addHelpButton('showreports', 'showreports'); $mform->setDefault('showreports', $courseconfig->showreports); // Files and uploads. $mform->addElement('header', 'filehdr', get_string('filesanduploads')); if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) { if (empty($course->legacyfiles)) { //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet $choices = array('0' => get_string('no'), '2' => get_string('yes')); } else { $choices = array('1' => get_string('no'), '2' => get_string('yes')); } $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices); $mform->addHelpButton('legacyfiles', 'courselegacyfiles'); if (!isset($courseconfig->legacyfiles)) { // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses $courseconfig->legacyfiles = 0; } $mform->setDefault('legacyfiles', $courseconfig->legacyfiles); } // Handle non-existing $course->maxbytes on course creation. $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes; // Let's prepare the maxbytes popup. $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes); $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices); $mform->addHelpButton('maxbytes', 'maximumupload'); $mform->setDefault('maxbytes', $courseconfig->maxbytes); // Completion tracking. if (completion_info::is_enabled_for_site()) { $mform->addElement('header', 'completionhdr', get_string('completion', 'completion')); $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion')); $mform->setDefault('enablecompletion', $courseconfig->enablecompletion); $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion'); } else { $mform->addElement('hidden', 'enablecompletion'); $mform->setType('enablecompletion', PARAM_INT); $mform->setDefault('enablecompletion', 0); } //-------------------------------------------------------------------------------- enrol_course_edit_form($mform, $course, $context); //-------------------------------------------------------------------------------- $mform->addElement('header', 'groups', get_string('groups', 'group')); $choices = array(); $choices[NOGROUPS] = get_string('groupsnone', 'group'); $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group'); $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group'); $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices); $mform->addHelpButton('groupmode', 'groupmode', 'group'); $mform->setDefault('groupmode', $courseconfig->groupmode); $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group')); $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group'); $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce); //default groupings selector $options = array(); $options[0] = get_string('none'); $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options); //-------------------------------------------------------------------------------- /// customizable role names in this course //-------------------------------------------------------------------------------- $mform->addElement('header', 'rolerenaming', get_string('rolerenaming')); $mform->addHelpButton('rolerenaming', 'rolerenaming'); if ($roles = get_all_roles()) { $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL); $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE); foreach ($roles as $role) { $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname)); $mform->setType('role_' . $role->id, PARAM_TEXT); } } //-------------------------------------------------------------------------------- $this->add_action_buttons(); //-------------------------------------------------------------------------------- $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); /// finally set the current form data //-------------------------------------------------------------------------------- $this->set_data($course); }
protected function coursecat_coursebox_content(coursecat_helper $chelper, $course, $type = 3) { global $CFG, $OUTPUT, $PAGE; if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { return ''; } if ($course instanceof stdClass) { require_once $CFG->libdir . '/coursecatlib.php'; $course = new course_in_list($course); } if ($type == 3 || $OUTPUT->body_id() != 'page-site-index') { return parent::coursecat_coursebox_content($chelper, $course); } $content = ''; // Display course overview files. $contentimages = ''; $contentfiles = ''; foreach ($course->get_course_overviewfiles() as $file) { $isimage = $file->is_valid_image(); $url = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); if ($isimage) { if ($type == 1) { $contentimages .= html_writer::start_tag('div', array('class' => 'courseimage')); $link = new moodle_url('/course/view.php', array('id' => $course->id)); $contentimages .= html_writer::link($link, html_writer::empty_tag('img', array('src' => $url))); $contentimages .= html_writer::end_tag('div'); } else { $contentimages .= "<div class='cimbox' style='background: #FFF url({$url}) no-repeat center center; background-size: contain;'></div>"; } } else { $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle'); $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')) . html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename')); $contentfiles .= html_writer::tag('span', html_writer::link($url, $filename), array('class' => 'coursefile fp-filename-icon')); } } if (strlen($contentimages) == 0 && $type == 2) { // Default image. $url = $PAGE->theme->setting_file_url('frontpagerendererdefaultimage', 'frontpagerendererdefaultimage'); $contentimages .= "<div class='cimbox' style='background: #FFF url({$url}) no-repeat center center; background-size: contain;'></div>"; } $content .= $contentimages . $contentfiles; if ($type == 2) { $content .= $this->coursecat_coursebox_enrolmenticons($course); } if ($type == 2) { $content .= html_writer::start_tag('div', array('class' => 'coursebox-content')); $coursename = $chelper->get_course_formatted_name($course); $content .= html_writer::tag('h3', html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $coursename, array('class' => $course->visible ? '' : 'dimmed', 'title' => $coursename))); } $content .= html_writer::start_tag('div', array('class' => 'summary')); if (isset($coursename)) { $content .= html_writer::tag('p', html_writer::tag('b', $coursename)); } // Display course summary. if ($course->has_summary()) { $summs = $chelper->get_course_formatted_summary($course, array('overflowdiv' => false, 'noclean' => true, 'para' => false)); $summs = strip_tags($summs); $truncsum = strlen($summs) > 70 ? substr($summs, 0, 70) . "..." : $summs; $content .= html_writer::tag('span', $truncsum, array('title' => $summs)); } $coursecontacts = theme_bcu_get_setting('tilesshowcontacts'); if ($coursecontacts) { $coursecontacttitle = theme_bcu_get_setting('tilescontactstitle'); // Display course contacts. See course_in_list::get_course_contacts(). if ($course->has_course_contacts()) { $content .= html_writer::start_tag('ul', array('class' => 'teachers')); foreach ($course->get_course_contacts() as $userid => $coursecontact) { $name = ($coursecontacttitle ? $coursecontact['rolename'] . ': ' : html_writer::tag('i', ' ', array('class' => 'fa fa-graduation-cap'))) . html_writer::link(new moodle_url('/user/view.php', array('id' => $userid, 'course' => SITEID)), $coursecontact['username']); $content .= html_writer::tag('li', $name); } $content .= html_writer::end_tag('ul'); // Teachers. } } $content .= html_writer::end_tag('div'); // Summary. // Display course category if necessary (for example in search results). if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) { require_once $CFG->libdir . '/coursecatlib.php'; if ($cat = coursecat::get($course->category, IGNORE_MISSING)) { $content .= html_writer::start_tag('div', array('class' => 'coursecat')); $content .= get_string('category') . ': ' . html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed')); $content .= html_writer::end_tag('div'); // Coursecat. } } if ($type == 2) { $content .= html_writer::end_tag('div'); } $content .= html_writer::tag('div', '', array('class' => 'boxfooter')); // Coursecat. return $content; }
/** * Returns the category where this course request should be created * * Note that we don't check here that user has a capability to view * hidden categories if he has capabilities 'moodle/site:approvecourse' and * 'moodle/course:changecategory' * * @return coursecat */ public function get_category() { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; // If the category is not set, if the current user does not have the rights to change the category, or if the // category does not exist, we set the default category to the course to be approved. // The system level is used because the capability moodle/site:approvecourse is based on a system level. if (empty($this->properties->category) || !has_capability('moodle/course:changecategory', context_system::instance()) || !($category = coursecat::get($this->properties->category, IGNORE_MISSING, true))) { $category = coursecat::get($CFG->defaultrequestcategory, IGNORE_MISSING, true); } if (!$category) { $category = coursecat::get_default(); } return $category; }
/** * Move selected categories to top level in the management interface. * * @Given /^I move category "(?P<name_string>(?:[^"]|\\")*)" to top level in the management interface$/ * @param string $name * @return Given[] */ public function i_move_category_to_top_level_in_the_management_interface($name) { $this->i_select_category_in_the_management_interface($name); return array(new Given('I set the field "menumovecategoriesto" to "' . coursecat::get(0)->get_formatted_name() . '"'), new Given('I press "bulkmovecategories"')); }
continue; } list($ignored, $roleid) = explode('_', $fieldname); $rolename = new stdClass(); $rolename->catid = $id; $rolename->roleid = $roleid; $rolename->name = $value; $newrolenames[] = $rolename; } // This is for updating all categories and courses. $cats = array(coursecat::get($id)); $courses = array(); if ($categories = coursecat::get($id)->get_children()) { foreach ($categories as $cat) { array_push($cats, $cat); $cats = array_merge($cats, coursecat::get($cat->id)->get_children()); } } // Update all the category's. foreach ($cats as $coursecat) { $courses = array_merge($courses, get_courses($coursecat->id)); foreach ($newrolenames as $role) { if (!$role->name) { $DB->delete_records('cat_role_names', array('catid' => $coursecat->id, 'roleid' => $role->roleid)); } else { if ($rolename = $DB->get_record('cat_role_names', array('catid' => $coursecat->id, 'roleid' => $role->roleid))) { $rolename->name = $role->name; $DB->update_record('cat_role_names', $rolename); } else { $rolename = new stdClass(); $rolename->catid = $coursecat->id;
/** * Search courses following the specified criteria. * * @param string $criterianame Criteria name (search, modulelist (only admins), blocklist (only admins), tagid) * @param string $criteriavalue Criteria value * @param int $page Page number (for pagination) * @param int $perpage Items per page * @param array $requiredcapabilities Optional list of required capabilities (used to filter the list). * @param int $limittoenrolled Limit to only enrolled courses * @return array of course objects and warnings * @since Moodle 3.0 * @throws moodle_exception */ public static function search_courses($criterianame, $criteriavalue, $page = 0, $perpage = 0, $requiredcapabilities = array(), $limittoenrolled = 0) { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; $warnings = array(); $parameters = array('criterianame' => $criterianame, 'criteriavalue' => $criteriavalue, 'page' => $page, 'perpage' => $perpage, 'requiredcapabilities' => $requiredcapabilities); $params = self::validate_parameters(self::search_courses_parameters(), $parameters); self::validate_context(context_system::instance()); $allowedcriterianames = array('search', 'modulelist', 'blocklist', 'tagid'); if (!in_array($params['criterianame'], $allowedcriterianames)) { throw new invalid_parameter_exception('Invalid value for criterianame parameter (value: ' . $params['criterianame'] . '),' . 'allowed values are: ' . implode(',', $allowedcriterianames)); } if ($params['criterianame'] == 'modulelist' or $params['criterianame'] == 'blocklist') { require_capability('moodle/site:config', context_system::instance()); } $paramtype = array('search' => PARAM_RAW, 'modulelist' => PARAM_PLUGIN, 'blocklist' => PARAM_INT, 'tagid' => PARAM_INT); $params['criteriavalue'] = clean_param($params['criteriavalue'], $paramtype[$params['criterianame']]); // Prepare the search API options. $searchcriteria = array(); $searchcriteria[$params['criterianame']] = $params['criteriavalue']; $options = array(); if ($params['perpage'] != 0) { $offset = $params['page'] * $params['perpage']; $options = array('offset' => $offset, 'limit' => $params['perpage']); } // Search the courses. $courses = coursecat::search_courses($searchcriteria, $options, $params['requiredcapabilities']); $totalcount = coursecat::search_courses_count($searchcriteria, $options, $params['requiredcapabilities']); if (!empty($limittoenrolled)) { // Get the courses where the current user has access. $enrolled = enrol_get_my_courses(array('id', 'cacherev')); } $finalcourses = array(); $categoriescache = array(); foreach ($courses as $course) { if (!empty($limittoenrolled)) { // Filter out not enrolled courses. if (!isset($enrolled[$course->id])) { $totalcount--; continue; } } $coursecontext = context_course::instance($course->id); // Category information. if (!isset($categoriescache[$course->category])) { $categoriescache[$course->category] = coursecat::get($course->category); } $category = $categoriescache[$course->category]; // Retrieve course overfiew used files. $files = array(); foreach ($course->get_course_overviewfiles() as $file) { $fileurl = moodle_url::make_webservice_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename())->out(false); $files[] = array('filename' => $file->get_filename(), 'fileurl' => $fileurl, 'filesize' => $file->get_filesize()); } // Retrieve the course contacts, // we need here the users fullname since if we are not enrolled can be difficult to obtain them via other Web Services. $coursecontacts = array(); foreach ($course->get_course_contacts() as $contact) { $coursecontacts[] = array('id' => $contact['user']->id, 'fullname' => $contact['username']); } // Allowed enrolment methods (maybe we can self-enrol). $enroltypes = array(); $instances = enrol_get_instances($course->id, true); foreach ($instances as $instance) { $enroltypes[] = $instance->enrol; } // Format summary. list($summary, $summaryformat) = external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null); $displayname = get_course_display_name_for_list($course); $coursereturns = array(); $coursereturns['id'] = $course->id; $coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id); $coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id); $coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id); $coursereturns['categoryid'] = $course->category; $coursereturns['categoryname'] = $category->name; $coursereturns['summary'] = $summary; $coursereturns['summaryformat'] = $summaryformat; $coursereturns['overviewfiles'] = $files; $coursereturns['contacts'] = $coursecontacts; $coursereturns['enrollmentmethods'] = $enroltypes; $finalcourses[] = $coursereturns; } return array('total' => $totalcount, 'courses' => $finalcourses, 'warnings' => $warnings); }
function get_content() { global $CFG, $USER, $DB, $OUTPUT; if ($this->content !== NULL) { return $this->content; } $this->content = new stdClass(); $this->content->items = array(); $this->content->icons = array(); $this->content->footer = ''; $icon = '<img src="' . $OUTPUT->pix_url('i/course') . '" class="icon" alt="" />'; $adminseesall = true; if (isset($CFG->block_course_list_adminview)) { if ($CFG->block_course_list_adminview == 'own') { $adminseesall = false; } } if (empty($CFG->disablemycourses) and isloggedin() and !isguestuser() and !(has_capability('moodle/course:update', context_system::instance()) and $adminseesall)) { // Just print My Courses if ($courses = enrol_get_my_courses(NULL, 'visible DESC, fullname ASC')) { foreach ($courses as $course) { $coursecontext = context_course::instance($course->id); $linkcss = $course->visible ? "" : " class=\"dimmed\" "; $this->content->items[] = "<a {$linkcss} title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" " . "href=\"{$CFG->wwwroot}/course/view.php?id={$course->id}\">" . $icon . format_string(get_course_display_name_for_list($course)) . "</a>"; } $this->title = get_string('mycourses'); /// If we can update any course of the view all isn't hidden, show the view all courses link if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) { $this->content->footer = "<a href=\"{$CFG->wwwroot}/course/index.php\">" . get_string("fulllistofcourses") . "</a> ..."; } } $this->get_remote_courses(); if ($this->content->items) { // make sure we don't return an empty list return $this->content; } } $categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only if ($categories) { //Check we have categories if (count($categories) > 1 || count($categories) == 1 && $DB->count_records('course') > 200) { // Just print top level category links foreach ($categories as $category) { $categoryname = $category->get_formatted_name(); $linkcss = $category->visible ? "" : " class=\"dimmed\" "; $this->content->items[] = "<a {$linkcss} href=\"{$CFG->wwwroot}/course/index.php?categoryid={$category->id}\">" . $icon . $categoryname . "</a>"; } /// If we can update any course of the view all isn't hidden, show the view all courses link if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) { $this->content->footer .= "<a href=\"{$CFG->wwwroot}/course/index.php\">" . get_string('fulllistofcourses') . '</a> ...'; } $this->title = get_string('categories'); } else { // Just print course names of single category $category = array_shift($categories); $courses = get_courses($category->id); if ($courses) { foreach ($courses as $course) { $coursecontext = context_course::instance($course->id); $linkcss = $course->visible ? "" : " class=\"dimmed\" "; $this->content->items[] = "<a {$linkcss} title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" " . "href=\"{$CFG->wwwroot}/course/view.php?id={$course->id}\">" . $icon . format_string(get_course_display_name_for_list($course), true, array('context' => context_course::instance($course->id))) . "</a>"; } /// If we can update any course of the view all isn't hidden, show the view all courses link if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) { $this->content->footer .= "<a href=\"{$CFG->wwwroot}/course/index.php\">" . get_string('fulllistofcourses') . '</a> ...'; } $this->get_remote_courses(); } else { $this->content->icons[] = ''; $this->content->items[] = get_string('nocoursesyet'); if (has_capability('moodle/course:create', context_coursecat::instance($category->id))) { $this->content->footer = '<a href="' . $CFG->wwwroot . '/course/edit.php?category=' . $category->id . '">' . get_string("addnewcourse") . '</a> ...'; } $this->get_remote_courses(); } $this->title = get_string('courses'); } } return $this->content; }
public function test_course_contacts() { global $DB, $CFG; $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $managerrole = $DB->get_record('role', array('shortname' => 'manager')); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $oldcoursecontact = $CFG->coursecontact; $CFG->coursecontact = $managerrole->id . ',' . $teacherrole->id; /* * User is listed in course contacts for the course if he has one of the * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course. * If the user has several roles only the highest is displayed. */ // Test case: // // == Cat1 (user2 has teacher role) // == Cat2 // -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2 // -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2 // == Cat4 (user2 has manager role) // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | [Expected] Manager: F5 L5, Teacher: F4 L4 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2 // == Cat3 (user3 has manager role) // -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3 // -- course32 | [Expected] // -- course11 (user1 is enrolled as teacher) | [Expected] Teacher: F1 L1 // -- course12 (user1 has teacher role) | [Expected] // also user4 is enrolled as teacher but enrolment is not active $category = $course = $enrol = $user = array(); $category[1] = coursecat::create(array('name' => 'Cat1'))->id; $category[2] = coursecat::create(array('name' => 'Cat2', 'parent' => $category[1]))->id; $category[3] = coursecat::create(array('name' => 'Cat3', 'parent' => $category[1]))->id; $category[4] = coursecat::create(array('name' => 'Cat4', 'parent' => $category[2]))->id; foreach (array(1, 2, 3, 4) as $catid) { foreach (array(1, 2) as $courseid) { $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array('idnumber' => 'id' . $catid . $courseid, 'category' => $category[$catid]))->id; $enrol[$catid][$courseid] = $DB->get_record('enrol', array('courseid' => $course[$catid][$courseid], 'enrol' => 'manual'), '*', MUST_EXIST); } } foreach (array(1, 2, 3, 4, 5) as $userid) { $user[$userid] = $this->getDataGenerator()->create_user(array('firstname' => 'F' . $userid, 'lastname' => 'L' . $userid))->id; } $manual = enrol_get_plugin('manual'); // Nobody is enrolled now and course contacts are empty. $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1))); foreach ($allcourses as $onecourse) { $this->assertEmpty($onecourse->get_course_contacts()); } // Cat1 (user2 has teacher role) role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1])); // course21 (user2 is enrolled as manager) $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id); // course22 (user2 is enrolled as student) $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id); // Cat4 (user2 has manager role) role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4])); // course41 (user4 is enrolled as teacher, user5 is enrolled as manager) $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id); $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id); // course42 (user2 is enrolled as teacher) $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id); // Cat3 (user3 has manager role) role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3])); // course31 (user3 is enrolled as student) $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id); // course11 (user1 is enrolled as teacher) $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id); // -- course12 (user1 has teacher role) // also user4 is enrolled as teacher but enrolment is not active role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2])); $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED); $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1))); // Simplify the list of contacts for each course (similar as renderer would do). $contacts = array(); foreach (array(1, 2, 3, 4) as $catid) { foreach (array(1, 2) as $courseid) { $tmp = array(); foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) { $tmp[] = $contact['rolename'] . ': ' . $contact['username']; } $contacts[$catid][$courseid] = join(', ', $tmp); } } // Assert: // -- course21 (user2 is enrolled as manager) | Manager: F2 L2 $this->assertSame('Manager: F2 L2', $contacts[2][1]); // -- course22 (user2 is enrolled as student) | Teacher: F2 L2 $this->assertSame('Teacher: F2 L2', $contacts[2][2]); // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | Manager: F5 L5, Teacher: F4 L4 $this->assertSame('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]); // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2 $this->assertSame('Manager: F2 L2', $contacts[4][2]); // -- course31 (user3 is enrolled as student) | Manager: F3 L3 $this->assertSame('Manager: F3 L3', $contacts[3][1]); // -- course32 | $this->assertSame('', $contacts[3][2]); // -- course11 (user1 is enrolled as teacher) | Teacher: F1 L1 $this->assertSame('Teacher: F1 L1', $contacts[1][1]); // -- course12 (user1 has teacher role) | $this->assertSame('', $contacts[1][2]); // Suspend user 4 and make sure he is no longer in contacts of course 1 in category 4. $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED); $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1))); $contacts = $allcourses[$course[4][1]]->get_course_contacts(); $this->assertCount(1, $contacts); $contact = reset($contacts); $this->assertEquals('F5 L5', $contact['username']); $CFG->coursecontact = $oldcoursecontact; }
/** * Delete categories * * @param array $categories A list of category ids * @return array * @since Moodle 2.3 */ public static function delete_categories($categories) { global $CFG, $DB; require_once($CFG->dirroot . "/course/lib.php"); require_once($CFG->libdir . "/coursecatlib.php"); // Validate parameters. $params = self::validate_parameters(self::delete_categories_parameters(), array('categories' => $categories)); $transaction = $DB->start_delegated_transaction(); foreach ($params['categories'] as $category) { $deletecat = coursecat::get($category['id'], MUST_EXIST); $context = context_coursecat::instance($deletecat->id); require_capability('moodle/category:manage', $context); self::validate_context($context); self::validate_context(get_category_or_system_context($deletecat->parent)); if ($category['recursive']) { // If recursive was specified, then we recursively delete the category's contents. if ($deletecat->can_delete_full()) { $deletecat->delete_full(false); } else { throw new moodle_exception('youcannotdeletecategory', '', '', $deletecat->get_formatted_name()); } } else { // In this situation, we don't delete the category's contents, we either move it to newparent or parent. // If the parent is the root, moving is not supported (because a course must always be inside a category). // We must move to an existing category. if (!empty($category['newparent'])) { $newparentcat = coursecat::get($category['newparent']); } else { $newparentcat = coursecat::get($deletecat->parent); } // This operation is not allowed. We must move contents to an existing category. if (!$newparentcat->id) { throw new moodle_exception('movecatcontentstoroot'); } self::validate_context(context_coursecat::instance($newparentcat->id)); if ($deletecat->can_move_content_to($newparentcat->id)) { $deletecat->delete_move($newparentcat->id, false); } else { throw new moodle_exception('youcannotdeletecategory', '', '', $deletecat->get_formatted_name()); } } } $transaction->allow_commit(); }
/** * Returns HTML to display course content (summary, course contacts and optionally category name) * * This method is called from coursecat_coursebox() and may be re-used in AJAX * * @param coursecat_helper $chelper various display options * @param stdClass|course_in_list $course * @return string */ protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) { if (!$this->enablecategoryicon) { return parent::coursecat_coursebox_content($chelper, $course); } global $CFG; if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { return ''; } if ($course instanceof stdClass) { require_once $CFG->libdir . '/coursecatlib.php'; $course = new course_in_list($course); } $content = ''; $coursehascontacts = $course->has_course_contacts(); // Display course summary. if ($course->has_summary()) { $summaryclass = 'summary'; if ($coursehascontacts == false) { $summaryclass .= ' noteachers'; } $content .= html_writer::start_tag('div', array('class' => $summaryclass)); $content .= $chelper->get_course_formatted_summary($course, array('overflowdiv' => true, 'noclean' => true, 'para' => false)); $content .= html_writer::end_tag('div'); // Class .summary. } // Display course overview files. $contentimages = $contentfiles = ''; foreach ($course->get_course_overviewfiles() as $file) { $isimage = $file->is_valid_image(); $url = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); if ($isimage) { $contentimages .= html_writer::tag('div', html_writer::empty_tag('img', array('src' => $url)), array('class' => 'courseimage')); } else { $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle'); $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')) . html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename')); $contentfiles .= html_writer::tag('span', html_writer::link($url, $filename), array('class' => 'coursefile fp-filename-icon')); } } $content .= $contentimages . $contentfiles; // Display course contacts. See course_in_list::get_course_contacts(). if ($coursehascontacts) { $content .= html_writer::start_tag('ul', array('class' => 'teachers')); foreach ($course->get_course_contacts() as $userid => $coursecontact) { $faiconsetting = \theme_essential\toolbox::get_setting('courselistteachericon'); $faiconsettinghtml = empty($faiconsetting) ? '' : '<i class="fa fa-' . $faiconsetting . '"></i> '; $name = $faiconsettinghtml . $coursecontact['rolename'] . ': ' . html_writer::link(new moodle_url('/user/view.php', array('id' => $userid, 'course' => SITEID)), $coursecontact['username']); $content .= html_writer::tag('li', $name); } $content .= html_writer::end_tag('ul'); // Class .teachers. } // Display course category if necessary (for example in search results). if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) { require_once $CFG->libdir . '/coursecatlib.php'; if ($cat = coursecat::get($course->category, IGNORE_MISSING)) { $content .= html_writer::start_tag('div', array('class' => 'coursecat')); $content .= get_string('category') . ': ' . html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed')); $content .= html_writer::end_tag('div'); // Class .coursecat. } } return $content; }