rolesactive)) { // Teachers are locked out during an upgrade to 1.7 return false; } if ($courseid) { $context = get_context_instance(CONTEXT_COURSE, $courseid); } else { $context = get_context_instance(CONTEXT_SYSTEM); } return (has_capability('moodle/legacy:teacher', $context, $userid, false) or has_capability('moodle/legacy:editingteacher', $context, $userid, false) or has_capability('moodle/legacy:admin', $context, $userid, false)); } /** * Determines if a user is a teacher in any course, or an admin * * @uses $USER * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. * @param bool $includeadmin Include anyone wo is an admin as well * @return bool */ function isteacherinanycourse($userid=0, $includeadmin=true) { global $USER, $CFG, $DB; if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7 return false; } if (!$userid) { if (empty($USER->id)) { return false; } $userid = $USER->id; } if (!$DB->record_exists('role_assignments', array('userid'=>$userid))) { // Has no roles anywhere return false; } /// If this user is assigned as an editing teacher anywhere then return true if ($roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) { foreach ($roles as $role) { if ($DB->record_exists('role_assignments', array('roleid'=>$role->id, 'userid'=>$userid))) { return true; } } } /// If this user is assigned as a non-editing teacher anywhere then return true if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) { foreach ($roles as $role) { if ($DB->record_exists('role_assignments', array('roleid'=>$role->id, 'userid'=>$userid))) { return true; } } } /// Include admins if required if ($includeadmin) { $context = get_context_instance(CONTEXT_SYSTEM); if (has_capability('moodle/legacy:admin', $context, $userid, false)) { return true; } } return false; } /** * Determines if the specified user is logged in as guest. * * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. * @return bool */ function isguest($userid=0) { global $CFG; if (empty($CFG->rolesactive)) { return false; } $context = get_context_instance(CONTEXT_SYSTEM); return has_capability('moodle/legacy:guest', $context, $userid, false); } /** * Get the guest user information from the database * * @return object(user) An associative array with the details of the guest user account. * @todo Is object(user) a correct return type? Or is array the proper return type with a note that the contents include all details for a user. */ function get_guest() { return get_complete_user_data('username', 'guest'); } /** * Returns $user object of the main teacher for a course * * @uses $CFG * @param int $courseid The course in question. * @return user|false A {@link $USER} record of the main teacher for the specified course or false if error. * @todo Finish documenting this function */ function get_teacher($courseid) { global $CFG; $context = get_context_instance(CONTEXT_COURSE, $courseid); // Pass $view=true to filter hidden caps if the user cannot see them if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', '', '', '', '', false, true)) { $users = sort_by_roleassignment_authority($users, $context); return array_shift($users); } return false; } /** * Searches logs to find all enrolments since a certain date * * used to print recent activity * * @uses $CFG * @param int $courseid The course in question. * @return object|false {@link $USER} records or false if error. * @todo Finish documenting this function */ function get_recent_enrolments($courseid, $timestart) { global $DB; $context = get_context_instance(CONTEXT_COURSE, $courseid); $sql = "SELECT DISTINCT u.id, u.firstname, u.lastname, l.time FROM {user} u, {role_assignments} ra, {log} l WHERE l.time > ? AND l.course = ? AND l.module = 'course' AND l.action = 'enrol' AND ".$DB->sql_cast_char2int('l.info')." = u.id AND u.id = ra.userid AND ra.contextid ".get_related_contexts_string($context)." ORDER BY l.time ASC"; $params = array($timestart, $courseid); return $DB->get_records_sql($sql, $params); } ########### FROM weblib.php ########################################################################## /** * Print a message in a standard themed box. * This old function used to implement boxes using tables. Now it uses a DIV, but the old * parameters remain. If possible, $align, $width and $color should not be defined at all. * Preferably just use print_box() in weblib.php * * @param string $align, alignment of the box, not the text (default center, left, right). * @param string $width, width of the box, including units %, for example '100%'. * @param string $color, background colour of the box, for example '#eee'. * @param int $padding, padding in pixels, specified without units. * @param string $class, space-separated class names. * @param string $id, space-separated id names. * @param boolean $return, return as string or just print it */ function print_simple_box($message, $align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) { $output = ''; $output .= print_simple_box_start($align, $width, $color, $padding, $class, $id, true); $output .= stripslashes_safe($message); $output .= print_simple_box_end(true); if ($return) { return $output; } else { echo $output; } } /** * This old function used to implement boxes using tables. Now it uses a DIV, but the old * parameters remain. If possible, $align, $width and $color should not be defined at all. * Even better, please use print_box_start() in weblib.php * * @param string $align, alignment of the box, not the text (default center, left, right). DEPRECATED * @param string $width, width of the box, including % units, for example '100%'. DEPRECATED * @param string $color, background colour of the box, for example '#eee'. DEPRECATED * @param int $padding, padding in pixels, specified without units. OBSOLETE * @param string $class, space-separated class names. * @param string $id, space-separated id names. * @param boolean $return, return as string or just print it */ function print_simple_box_start($align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) { $output = ''; $divclasses = 'box '.$class.' '.$class.'content'; $divstyles = ''; if ($align) { $divclasses .= ' boxalign'.$align; // Implement alignment using a class } if ($width) { // Hopefully we can eliminate these in calls to this function (inline styles are bad) if (substr($width, -1, 1) == '%') { // Width is a % value $width = (int) substr($width, 0, -1); // Extract just the number if ($width < 40) { $divclasses .= ' boxwidthnarrow'; // Approx 30% depending on theme } else if ($width > 60) { $divclasses .= ' boxwidthwide'; // Approx 80% depending on theme } else { $divclasses .= ' boxwidthnormal'; // Approx 50% depending on theme } } else { $divstyles .= ' width:'.$width.';'; // Last resort } } if ($color) { // Hopefully we can eliminate these in calls to this function (inline styles are bad) $divstyles .= ' background:'.$color.';'; } if ($divstyles) { $divstyles = ' style="'.$divstyles.'"'; } if ($id) { $id = ' id="'.$id.'"'; } $output .= '