print_single_button('assign.php', array('contextid' => $contextid), get_string('assignrolesin', 'role', print_context_name($context))); echo "</div>"; } else { // Print overview table // sync metacourse enrolments if needed if ($inmeta) { sync_metacourse($course); } // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users, // and so determine whether to show the extra column. $rolehodlercount = array(); $rolehodlernames = array(); $strmorethanten = get_string('morethan', 'role', MAX_USERS_TO_LIST_PER_ROLE); $showroleholders = false; foreach ($assignableroles as $roleid => $rolename) { $countusers = count_role_users($roleid, $context); $rolehodlercount[$roleid] = $countusers; $roleusers = ''; if (0 < $countusers && $countusers <= MAX_USERS_TO_LIST_PER_ROLE) { $roleusers = get_role_users($roleid, $context, false, 'u.id, u.lastname, u.firstname'); if (!empty($roleusers)) { $strroleusers = array(); foreach ($roleusers as $user) { $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>'; } $rolehodlernames[$roleid] = implode('<br />', $strroleusers); $showroleholders = true; } } else { if ($countusers > MAX_USERS_TO_LIST_PER_ROLE) { $rolehodlernames[$roleid] = '<a href="' . $baseurl . '&roleid=' . $roleid . '">' . $strmorethanten . '</a>';
/** * Init this page * * @param bool $selfitemisempty True if we have not selected a user. */ public function init($selfitemisempty = false) { $roleids = explode(',', get_config('moodle', 'gradebookroles')); $this->items = array(); foreach ($roleids as $roleid) { // Keeping the first user appearance. $this->items = $this->items + get_role_users($roleid, $this->context, false, '', 'u.lastname, u.firstname', null, $this->groupid); } $this->totalitemcount = count_role_users($roleids, $this->context); if ($selfitemisempty) { return; } $params = array('id' => $this->itemid, 'courseid' => $this->courseid); $this->item = grade_item::fetch($params); if (!self::filter($this->item)) { $this->items = array(); $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview')); } $this->requiresextra = !$this->item->is_manual_item(); $this->setup_structure(); $this->set_definition($this->original_definition()); $this->set_headers($this->original_headers()); }
public function top_promoted_courses() { global $CFG, $OUTPUT, $DB, $PAGE; $featuredcontent = ''; /* Get Featured courses id from DB */ $featuredids = theme_pioneer_get_setting('toppromotedcourses'); $rcourseids = !empty($featuredids) ? explode(",", $featuredids, 10) : array(); if (empty($rcourseids)) { return false; } $hcourseids = theme_pioneer_hidden_courses_ids(); if (!empty($hcourseids)) { foreach ($rcourseids as $key => $val) { if (in_array($val, $hcourseids)) { unset($rcourseids[$key]); } } } foreach ($rcourseids as $key => $val) { $ccourse = $DB->get_record('course', array('id' => $val)); if (empty($ccourse)) { unset($rcourseids[$key]); continue; } } if (empty($rcourseids)) { return false; } $fcourseids = array_chunk($rcourseids, 10); $totalfcourse = count($fcourseids); $promotedtitle = theme_pioneer_get_setting('toppromotedtitle', 'format_text'); $promotedtitle = theme_pioneer_lang($promotedtitle); $closelisting = theme_pioneer_get_setting('topclosefeatured', 'format_text'); $topshowfeatured = theme_pioneer_get_setting('topshowfeatured', 'format_text'); $featuredheader = '<div class="custom-courses-list" id="topPromoted-Courses"> <div class="container-fluid"> <div class="promoted_courses" data-crow="' . $totalfcourse . '">'; $featuredfooter = ' </div> </div> </div>'; if (!empty($fcourseids)) { echo '<div id="featured-listing" class="collapse out">'; echo '<div data-toggle="collapse" data-target="#featured-listing" class="btn-link"style="text-align:center;">' . $closelisting . '</div>'; foreach ($fcourseids as $courseids) { $rowcontent = '<div><div class="row-fluid topcarousel">'; foreach ($courseids as $courseid) { $course = get_course($courseid); $no = get_config('theme_pioneer', 'toppatternselect'); $nimgp = empty($no) || $no == "default" ? 'no-image' : 'cs0' . $no . '/no-image'; $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 = ''; $summary = theme_pioneer_strip_html_tags($course->summary); $summary = theme_pioneer_course_trim_char($summary, 125); $trimtitle = theme_pioneer_course_trim_char($course->fullname, 90); $context = context_course::instance($course->id); $nostudents = count_role_users(5, $context); 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; } } $listitems = ' <div class="row-fluid"> <div class="span3"> <img src="' . $imgurl . '" width="100%" height="75px" alt="' . $course->fullname . '"> </div> <div class="span5"> <h5><a href="' . $courseurl . '" >' . $trimtitle . '</a></h5> </div> <div class="span4"> <p>' . $summary . '</p> </div> </div>'; $coursehtml = ' <div style="background-image: url(' . $imgurl . ');background-repeat: no-repeat;background-size:cover; background-position:center;" class="promowrap"> <div class="fp-coursebox"> <div class="fp-courseinfo"> <p style="font-size:24px;font-weight:bold;"><a href="' . $courseurl . '" id="button" data-toggle="tooltip" data-placement="bottom"title="' . $summary . '" >' . $trimtitle . '</a></p> <div class="titlebar"> <h5>' . $promotedtitle . '</h5> </div> <div data-toggle="collapse" data-target="#featured-listing" class="btn-link">' . $topshowfeatured . '</div> </div> </div> </div>'; $rowcontent .= $coursehtml; echo $listitems; } $rowcontent .= '</div></div> '; $featuredcontent .= $rowcontent; } echo "</div>"; } $featuredcourses = $featuredheader . $featuredcontent . $featuredfooter; return $featuredcourses; }
/** * Tests count_role_users function. */ public function test_count_role_users() { global $DB; $this->resetAfterTest(true); $generator = self::getDataGenerator(); // Create a course in a category, and some users. $category = $generator->create_category(); $course = $generator->create_course(array('category' => $category->id)); $user1 = $generator->create_user(); $user2 = $generator->create_user(); $user3 = $generator->create_user(); $user4 = $generator->create_user(); $user5 = $generator->create_user(); $roleid1 = $DB->get_field('role', 'id', array('shortname' => 'manager'), MUST_EXIST); $roleid2 = $DB->get_field('role', 'id', array('shortname' => 'coursecreator'), MUST_EXIST); // Enrol two users as managers onto the course, and 1 onto the category. $generator->enrol_user($user1->id, $course->id, $roleid1); $generator->enrol_user($user2->id, $course->id, $roleid1); $generator->role_assign($roleid1, $user3->id, context_coursecat::instance($category->id)); // Enrol 1 user as a coursecreator onto the course, and another onto the category. // This is to ensure we do not count users with roles that are not specified. $generator->enrol_user($user4->id, $course->id, $roleid2); $generator->role_assign($roleid2, $user5->id, context_coursecat::instance($category->id)); // Check that the correct users are found on the course. $this->assertEquals(2, count_role_users($roleid1, context_course::instance($course->id), false)); $this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id), true)); // Check for the category. $this->assertEquals(1, count_role_users($roleid1, context_coursecat::instance($category->id), false)); $this->assertEquals(1, count_role_users($roleid1, context_coursecat::instance($category->id), true)); // Have a user with the same role at both the category and course level. $generator->role_assign($roleid1, $user1->id, context_coursecat::instance($category->id)); // The course level checks should remain the same. $this->assertEquals(2, count_role_users($roleid1, context_course::instance($course->id), false)); $this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id), true)); }
function tao_message_count_recipients_by_target($target, $course, $user = null) { static $cache = array(); $key = $target->key . ' | ' . $course->id; if (array_key_exists($key, $cache)) { return $cache[$key]; } $user = tao_user_parameter($user); $context = get_context_instance(CONTEXT_COURSE, $course->id); if (!empty($target->recipientrole) || !empty($target->recipientroles)) { $roleid = false; $rolesql = ''; if (!empty($target->recipientrole)) { $roleid = get_field('role', 'id', 'shortname', $target->recipientrole); $rolesql = " = {$roleid}"; } else { if (!empty($target->recipientroles)) { $rolesql = " IN ( '" . implode("','", $target->recipientroles) . "' ) "; $roleid = array_keys(get_records_select('role', 'shortname ' . $rolesql, '', 'id, id')); $rolesql = " IN ( '" . implode("','", $roleid) . "' ) "; } } if (!empty($target->recipientcontext)) { $context = get_context_instance_by_id($target->recipientcontext); } $firstcount = count_role_users($roleid, $context); if ($assign = count_records_select('role_assignments', "userid = {$user->id} AND roleid {$rolesql} AND contextid = {$context->id}")) { $firstcount = $firstcount - $assign; } $count = $firstcount; } else { if (is_array($target->recipientfunction)) { // recipientfunction $function = $target->recipientfunction['count']; $count = $function($user, $course); } } $cache[$key] = $count; return $count; }
function execute($data, $row, $user, $courseid, $starttime = 0, $endtime = 0) { $courseid = $row->id; $context = get_context_instance(CONTEXT_COURSE, $courseid); return count_role_users($data->roles, $context); }
public function promoted_courses() { global $CFG, $OUTPUT, $DB; $pcourseenable = theme_eguru_get_setting('pcourseenable'); if (!$pcourseenable) { return false; } $featuredcontent = ''; /* Get Featured courses id from DB */ $featuredids = theme_eguru_get_setting('promotedcourses'); $rcourseids = !empty($featuredids) ? explode(",", $featuredids) : array(); if (empty($rcourseids)) { return false; } $hcourseids = theme_eguru_hidden_courses_ids(); if (!empty($hcourseids)) { foreach ($rcourseids as $key => $val) { if (in_array($val, $hcourseids)) { unset($rcourseids[$key]); } } } foreach ($rcourseids as $key => $val) { $ccourse = $DB->get_record('course', array('id' => $val)); if (empty($ccourse)) { unset($rcourseids[$key]); continue; } } if (empty($rcourseids)) { return false; } $fcourseids = array_chunk($rcourseids, 6); $totalfcourse = count($fcourseids); $promotedtitle = theme_eguru_get_setting('promotedtitle', 'format_text'); $promotedtitle = theme_eguru_lang($promotedtitle); $featuredheader = '<div class="custom-courses-list" id="Promoted-Courses"> <div class="container-fluid"> <div class="titlebar with-felements"> <h2>' . $promotedtitle . '</h2> <div class="slidenav pagenav"> <button class="nav-item nav-prev slick-prev"> <i class="fa fa-chevron-right"></i><i class="fa fa-chevron-left"></i> </button> <button class="nav-item nav-next slick-next"> <i class="fa fa-chevron-right"></i><i class="fa fa-chevron-left"></i> </button> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> <div class="promoted_courses" data-crow="' . $totalfcourse . '">'; $featuredfooter = ' </div> </div> </div>'; if (!empty($fcourseids)) { foreach ($fcourseids as $courseids) { $rowcontent = '<div><div class="row-fluid">'; foreach ($courseids as $courseid) { $course = get_course($courseid); $no = get_config('theme_eguru', 'patternselect'); $nimgp = empty($no) || $no == "default" ? 'cs00/no-image' : 'cs0' . $no . '/no-image'; $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 = ''; $summary = theme_eguru_strip_html_tags($course->summary); $summary = theme_eguru_course_trim_char($summary, 75); $context = context_course::instance($course->id); $nostudents = count_role_users(5, $context); 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 = $noimgurl; } $coursehtml = '<div class="span2"> <div class="course-box"> <div class="thumb"><a href="' . $courseurl . '"> <img src="' . $imgurl . '" width="135" height="135" alt="' . $course->fullname . '"></a></div> <div class="info"> <h5><a href="' . $courseurl . '">' . $course->fullname . '</a></h5> </div> </div> </div>'; $rowcontent .= $coursehtml; } $rowcontent .= '</div></div>'; $featuredcontent .= $rowcontent; } } $featuredcourses = $featuredheader . $featuredcontent . $featuredfooter; return $featuredcourses; }
public static function get_course_info($courseID) { global $DB; console . log("in course_info. CourseID: " + $courseID); //check parameters $params = self::validate_parameters(self::get_course_info_parameters(), array('courseID' => $courseID)); // now security checks $coursecontext = context_course::instance($params['courseID']); self::validate_context($coursecontext); //Is the user allowes to use this web service? //require_capability('moodle/site:viewparticipants', $context); // is the user normaly allowed to see all participants of the course require_capability('tool/supporter:get_course_info', $coursecontext); // is the user coursecreator, manager, teacher, editingteacher $select = "SELECT c.id, c.shortname, c.fullname, c.visible, cat.name AS fb, (SELECT name FROM {course_categories} WHERE id = cat.parent) AS semester FROM {course} c, {course_categories} cat WHERE c.category = cat.id AND c.id = '{$courseID}'"; $courseDetails = $DB->get_record_sql($select); $courseDetails = (array) $courseDetails; $courseDetails['enrolledUsers'] = count_enrolled_users($coursecontext, $withcapability = '', $groupid = '0'); $roles = array(); $roleList = get_all_roles($coursecontext); // array('moodle/legacy:student', 'moodle/legacy:teacher', 'moodle/legacy:editingteacher', 'moodle/legacy:coursecreator'); $count = count_role_users([1, 2, 3, 4, 5, 6, 7], $coursecontext); print_r($roleList); echo "count"; print_r($count); foreach ($roleList as $r) { if ($r->coursealias != NULL) { $roleName = $r->coursealias; } else { $roleName = $r->shortname; } $roleNumber = count_role_users($r->id, $coursecontext); //$roleNumber = count_enrolled_users($coursecontext, $withcapability = $role, $groupid = 0); if ($roleNumber != 0) { $roles[] = ['roleName' => $roleName, 'roleNumber' => $roleNumber]; } } $users_raw = get_enrolled_users($coursecontext, $withcapability = '', $groupid = 0, $userfields = 'u.id,u.username,u.firstname, u.lastname', $orderby = '', $limitfrom = 0, $limitnum = 0); $users = array(); foreach ($users_raw as $u) { $users[] = (array) $u; } $activities = array(); $modules = get_array_of_activities($courseID); foreach ($modules as $mo) { $section = get_section_name($courseID, $mo->section); $activity = ['section' => $section, 'activity' => $mo->mod, 'name' => $mo->name, 'visible' => $mo->visible]; $activities[] = $activity; } $data = ['courseDetails' => $courseDetails, 'roles' => $roles, 'users' => $users, 'activities' => $activities]; return $data; }
function action_default() { global $CURMAN; $roleid = $this->optional_param('role', '', PARAM_INT); $context = $this->get_context(); if ($roleid) { if (!user_can_assign($context, $roleid)) { print_error('nopermissions', 'error'); } return parent::action_default(); } else { $decorators = array('name' => new role_name_decorator(clone $this)); $assignableroles = get_assignable_roles($context, 'name', ROLENAME_BOTH); $roles = array(); foreach ($assignableroles as $roleid => $rolename) { $rec = new stdClass(); $rec->id = $roleid; $rec->name = $rolename; $rec->description = format_string(get_field('role', 'description', 'id', $roleid)); $rec->count = count_role_users($roleid, $context); $roles[$roleid] = $rec; } $table = new nosort_table($roles, array('name' => get_string('name'), 'description' => get_string('description'), 'count' => get_string('users')), $this->get_moodle_url(), $decorators); $table->print_table(); } }
/** * Counts all the users assigned this role in this context or higher * (can be used by child classes to override the counting with other criteria) * * @param mixed $roleid either int or an array of ints * @param object $context * @param bool $parent if true, get list of users assigned in higher context too * @return int Returns the result count */ function count_role_users($roleid, $context, $parent = false) { //default behaviour of counting role assignments return count_role_users($roleid, $context, $parent); }