"MDL-14460, fix get_context_instance, merged from MOODLE_19_STABLE"

This commit is contained in:
dongsheng 2008-05-01 06:07:24 +00:00
parent 2cb5f03a66
commit 12d0687748
8 changed files with 23 additions and 23 deletions

View file

@ -442,7 +442,7 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*")
$sqland = "AND "; $sqland = "AND ";
} }
if (!empty($USER->id)) { // May need to check they are a teacher if (!empty($USER->id)) { // May need to check they are a teacher
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
$visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')"; $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
$teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id"; $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id";
} }
@ -543,7 +543,7 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
$sqland = "AND "; $sqland = "AND ";
} }
if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
$visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')"; $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
$teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id"; $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id";
} }
@ -815,7 +815,7 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NUL
global $CFG,$USER; global $CFG,$USER;
// Guest's do not have any courses // Guest's do not have any courses
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); $sitecontext = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) { if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) {
return(array()); return(array());
} }
@ -2135,7 +2135,7 @@ function count_login_failures($mode, $username, $lastlogin) {
$select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin; $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin;
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Return information about all accounts if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { // Return information about all accounts
if ($count->attempts = count_records_select('log', $select)) { if ($count->attempts = count_records_select('log', $select)) {
$count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)'); $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
return $count; return $count;
@ -2252,7 +2252,7 @@ function user_can_create_courses() {
global $USER; global $USER;
// if user has course creation capability at any site or course cat, then return true; // if user has course creation capability at any site or course cat, then return true;
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
return true; return true;
} else { } else {
return (bool) count(get_creatable_categories()); return (bool) count(get_creatable_categories());

View file

@ -2199,7 +2199,7 @@ function main_upgrade($oldversion=0) {
if (!empty($CFG->rolesactive) and $oldversion < 2006091212) { // Reload the guest roles completely with new defaults if (!empty($CFG->rolesactive) and $oldversion < 2006091212) { // Reload the guest roles completely with new defaults
if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
delete_records('capabilities'); delete_records('capabilities');
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); $sitecontext = get_context_instance(CONTEXT_SYSTEM);
foreach ($guestroles as $guestrole) { foreach ($guestroles as $guestrole) {
delete_records('role_capabilities', 'roleid', $guestrole->id); delete_records('role_capabilities', 'roleid', $guestrole->id);
assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id); assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id);

View file

@ -1798,7 +1798,7 @@ function main_upgrade($oldversion=0) {
if (!empty($CFG->rolesactive) and $oldversion < 2006091212) { // Reload the guest roles completely with new defaults if (!empty($CFG->rolesactive) and $oldversion < 2006091212) { // Reload the guest roles completely with new defaults
if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
delete_records('capabilities'); delete_records('capabilities');
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); $sitecontext = get_context_instance(CONTEXT_SYSTEM);
foreach ($guestroles as $guestrole) { foreach ($guestroles as $guestrole) {
delete_records('role_capabilities', 'roleid', $guestrole->id); delete_records('role_capabilities', 'roleid', $guestrole->id);
assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id); assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id);

View file

@ -881,7 +881,7 @@ function xmldb_main_upgrade($oldversion=0) {
if( $defaultroleid != $userrole->id ) { if( $defaultroleid != $userrole->id ) {
// Add in the new moodle/my:manageblocks capibility to the default user role // Add in the new moodle/my:manageblocks capibility to the default user role
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
assign_capability('moodle/my:manageblocks',CAP_ALLOW,$defaultroleid,$context->id); assign_capability('moodle/my:manageblocks',CAP_ALLOW,$defaultroleid,$context->id);
} }

View file

@ -119,7 +119,7 @@ function isadmin($userid=0) {
return record_exists('user_admins', 'userid', $userid); return record_exists('user_admins', 'userid', $userid);
} }
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/legacy:admin', $context, $userid, false); return has_capability('moodle/legacy:admin', $context, $userid, false);
} }
@ -145,7 +145,7 @@ function isteacher($courseid=0, $userid=0, $obsolete_includeadmin=true) {
if ($courseid) { if ($courseid) {
$context = get_context_instance(CONTEXT_COURSE, $courseid); $context = get_context_instance(CONTEXT_COURSE, $courseid);
} else { } else {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
} }
return (has_capability('moodle/legacy:teacher', $context, $userid, false) return (has_capability('moodle/legacy:teacher', $context, $userid, false)
@ -199,7 +199,7 @@ function isteacherinanycourse($userid=0, $includeadmin=true) {
/// Include admins if required /// Include admins if required
if ($includeadmin) { if ($includeadmin) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/legacy:admin', $context, $userid, false)) { if (has_capability('moodle/legacy:admin', $context, $userid, false)) {
return true; return true;
} }
@ -223,7 +223,7 @@ function isteacheredit($courseid, $userid=0, $obsolete_ignorestudentview=false)
} }
if (empty($courseid)) { if (empty($courseid)) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
} else { } else {
$context = get_context_instance(CONTEXT_COURSE, $courseid); $context = get_context_instance(CONTEXT_COURSE, $courseid);
} }
@ -245,7 +245,7 @@ function iscreator ($userid=0) {
return false; return false;
} }
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
return (has_capability('moodle/legacy:coursecreator', $context, $userid, false) return (has_capability('moodle/legacy:coursecreator', $context, $userid, false)
or has_capability('moodle/legacy:admin', $context, $userid, false)); or has_capability('moodle/legacy:admin', $context, $userid, false));
@ -271,7 +271,7 @@ function isstudent($courseid=0, $userid=0) {
} }
if ($courseid == 0) { if ($courseid == 0) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
} else { } else {
$context = get_context_instance(CONTEXT_COURSE, $courseid); $context = get_context_instance(CONTEXT_COURSE, $courseid);
} }
@ -355,7 +355,7 @@ function unenrol_student($userid, $courseid=0) {
} }
/// remove from all legacy student roles /// remove from all legacy student roles
if ($courseid == SITEID) { if ($courseid == SITEID) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
} else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) { } else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
return false; return false;
} }

View file

@ -1304,13 +1304,13 @@ function stats_get_report_options($courseid,$mode) {
case STATS_MODE_DETAILED: case STATS_MODE_DETAILED:
$reportoptions[STATS_REPORT_USER_ACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ACTIVITY); $reportoptions[STATS_REPORT_USER_ACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ACTIVITY);
$reportoptions[STATS_REPORT_USER_ALLACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ALLACTIVITY); $reportoptions[STATS_REPORT_USER_ALLACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ALLACTIVITY);
if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM))) {
$site = get_site(); $site = get_site();
$reportoptions[STATS_REPORT_USER_LOGINS] = get_string('statsreport'.STATS_REPORT_USER_LOGINS); $reportoptions[STATS_REPORT_USER_LOGINS] = get_string('statsreport'.STATS_REPORT_USER_LOGINS);
} }
break; break;
case STATS_MODE_RANKED: case STATS_MODE_RANKED:
if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM))) {
$reportoptions[STATS_REPORT_ACTIVE_COURSES] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES); $reportoptions[STATS_REPORT_ACTIVE_COURSES] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES);
$reportoptions[STATS_REPORT_ACTIVE_COURSES_WEIGHTED] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES_WEIGHTED); $reportoptions[STATS_REPORT_ACTIVE_COURSES_WEIGHTED] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES_WEIGHTED);
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES); $reportoptions[STATS_REPORT_PARTICIPATORY_COURSES] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES);

View file

@ -2499,7 +2499,7 @@ function print_header ($title='', $heading='', $navigation='', $focus='',
} else { } else {
$menu .= get_string('failedloginattemptsall', '', $count); $menu .= get_string('failedloginattemptsall', '', $count);
} }
if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM))) {
$menu .= ' (<a href="'.$CFG->wwwroot.'/course/report/log/index.php'. $menu .= ' (<a href="'.$CFG->wwwroot.'/course/report/log/index.php'.
'?chooselog=1&amp;id=1&amp;modid=site_errors">'.get_string('logs').'</a>)'; '?chooselog=1&amp;id=1&amp;modid=site_errors">'.get_string('logs').'</a>)';
} }
@ -4802,7 +4802,7 @@ function print_recent_activity_note($time, $user, $text, $link, $return=false, $
$output = ''; $output = '';
if (is_null($viewfullnames)) { if (is_null($viewfullnames)) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $context = get_context_instance(CONTEXT_SYSTEM);
$viewfullnames = has_capability('moodle/site:viewfullnames', $context); $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
} }
@ -5195,7 +5195,7 @@ function update_category_button($categoryid) {
function update_categories_button() { function update_categories_button() {
global $CFG, $USER; global $CFG, $USER;
if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM))) {
if (!empty($USER->categoryediting)) { if (!empty($USER->categoryediting)) {
$string = get_string('turneditingoff'); $string = get_string('turneditingoff');
$categoryedit = 'off'; $categoryedit = 'off';
@ -5221,7 +5221,7 @@ function update_categories_search_button($search,$page,$perpage) {
global $CFG, $USER; global $CFG, $USER;
// not sure if this capability is the best here // not sure if this capability is the best here
if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM))) {
if (!empty($USER->categoryediting)) { if (!empty($USER->categoryediting)) {
$string = get_string("turneditingoff"); $string = get_string("turneditingoff");
$edit = "off"; $edit = "off";

View file

@ -12,7 +12,7 @@ class page_my_moodle extends page_base {
page_id_and_class($id,$class); page_id_and_class($id,$class);
if ($id == PAGE_MY_MOODLE) { if ($id == PAGE_MY_MOODLE) {
return true; return true;
} else if (has_capability('moodle/my:manageblocks', get_context_instance(CONTEXT_SYSTEM, SITEID)) && defined('ADMIN_STICKYBLOCKS')) { } else if (has_capability('moodle/my:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
return true; return true;
} }
return false; return false;
@ -20,7 +20,7 @@ class page_my_moodle extends page_base {
function user_is_editing() { function user_is_editing() {
global $USER; global $USER;
if (has_capability('moodle/my:manageblocks', get_context_instance(CONTEXT_SYSTEM, SITEID)) && defined('ADMIN_STICKYBLOCKS')) { if (has_capability('moodle/my:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
return true; return true;
} }
return (!empty($USER->editing)); return (!empty($USER->editing));