本文整理汇总了PHP中can_access_course函数的典型用法代码示例。如果您正苦于以下问题:PHP can_access_course函数的具体用法?PHP can_access_course怎么用?PHP can_access_course使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了can_access_course函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: tool_lp_extend_navigation_course
/**
* This function extends the course navigation
*
* @param navigation_node $navigation The navigation node to extend
* @param stdClass $course The course to object for the tool
* @param context $coursecontext The context of the course
*/
function tool_lp_extend_navigation_course($navigation, $course, $coursecontext)
{
if (!get_config('core_competency', 'enabled')) {
return;
}
// Check access to the course and competencies page.
$capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
$context = context_course::instance($course->id);
if (!has_any_capability($capabilities, $context) || !can_access_course($course)) {
return;
}
// Just a link to course competency.
$title = get_string('competencies', 'core_competency');
$path = new moodle_url("/admin/tool/lp/coursecompetencies.php", array('courseid' => $course->id));
$settingsnode = navigation_node::create($title, $path, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/competencies', ''));
if (isset($settingsnode)) {
$navigation->add_node($settingsnode);
}
}
示例2: initialise
/**
* Initialise the navigation given the type and id for the branch to expand.
*
* @return array An array of the expandable nodes
*/
public function initialise()
{
global $DB, $SITE;
if ($this->initialised || during_initial_install()) {
return $this->expandable;
}
$this->initialised = true;
$this->rootnodes = array();
$this->rootnodes['site'] = $this->add_course($SITE);
$this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses');
$this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses');
// The courses branch is always displayed, and is always expandable (although may be empty).
// This mimicks what is done during {@link global_navigation::initialise()}.
$this->rootnodes['courses']->isexpandable = true;
// Branchtype will be one of navigation_node::TYPE_*
switch ($this->branchtype) {
case 0:
if ($this->instanceid === 'mycourses') {
$this->load_courses_enrolled();
} else {
if ($this->instanceid === 'courses') {
$this->load_courses_other();
}
}
break;
case self::TYPE_CATEGORY:
$this->load_category($this->instanceid);
break;
case self::TYPE_MY_CATEGORY:
$this->load_category($this->instanceid, self::TYPE_MY_CATEGORY);
break;
case self::TYPE_COURSE:
$course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST);
if (!can_access_course($course, null, '', true)) {
// Thats OK all courses are expandable by default. We don't need to actually expand it we can just
// add the course node and break. This leads to an empty node.
$this->add_course($course);
break;
}
require_course_login($course, true, null, false, true);
$this->page->set_context(context_course::instance($course->id));
$coursenode = $this->add_course($course);
$this->add_course_essentials($coursenode, $course);
$this->load_course_sections($course, $coursenode);
break;
case self::TYPE_SECTION:
$sql = 'SELECT c.*, cs.section AS sectionnumber
FROM {course} c
LEFT JOIN {course_sections} cs ON cs.course = c.id
WHERE cs.id = ?';
$course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST);
require_course_login($course, true, null, false, true);
$this->page->set_context(context_course::instance($course->id));
$coursenode = $this->add_course($course);
$this->add_course_essentials($coursenode, $course);
$this->load_course_sections($course, $coursenode, $course->sectionnumber);
break;
case self::TYPE_ACTIVITY:
$sql = "SELECT c.*\n FROM {course} c\n JOIN {course_modules} cm ON cm.course = c.id\n WHERE cm.id = :cmid";
$params = array('cmid' => $this->instanceid);
$course = $DB->get_record_sql($sql, $params, MUST_EXIST);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($this->instanceid);
require_course_login($course, true, $cm, false, true);
$this->page->set_context(context_module::instance($cm->id));
$coursenode = $this->load_course($course);
$this->load_course_sections($course, $coursenode, null, $cm);
$activitynode = $coursenode->find($cm->id, self::TYPE_ACTIVITY);
if ($activitynode) {
$modulenode = $this->load_activity($cm, $course, $activitynode);
}
break;
default:
throw new Exception('Unknown type');
return $this->expandable;
}
if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id) {
$this->load_for_user(null, true);
}
$this->find_expandable($this->expandable);
return $this->expandable;
}
示例3: can_access
/**
* Returns true if the current user can access this course.
* @return bool
*/
public function can_access()
{
if ($this->canaccess === null) {
$this->canaccess = can_access_course($this->record);
}
return $this->canaccess;
}
示例4: check_access
/**
* Whether the user can access the document or not.
*
* @param int $id The course instance id.
* @return int
*/
public function check_access($id)
{
global $DB;
$course = $DB->get_record('course', array('id' => $id));
if (!$course) {
return \core_search\manager::ACCESS_DELETED;
}
if (can_access_course($course)) {
return \core_search\manager::ACCESS_GRANTED;
}
return \core_search\manager::ACCESS_DENIED;
}
示例5: glossary_extend_settings_navigation
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $glossarynode The node to add module settings to
*/
function glossary_extend_settings_navigation(settings_navigation $settings, navigation_node $glossarynode) {
global $PAGE, $DB, $CFG, $USER;
$mode = optional_param('mode', '', PARAM_ALPHA);
$hook = optional_param('hook', 'ALL', PARAM_CLEAN);
if (has_capability('mod/glossary:import', $PAGE->cm->context)) {
$glossarynode->add(get_string('importentries', 'glossary'), new moodle_url('/mod/glossary/import.php', array('id'=>$PAGE->cm->id)));
}
if (has_capability('mod/glossary:export', $PAGE->cm->context)) {
$glossarynode->add(get_string('exportentries', 'glossary'), new moodle_url('/mod/glossary/export.php', array('id'=>$PAGE->cm->id, 'mode'=>$mode, 'hook'=>$hook)));
}
if (has_capability('mod/glossary:approve', $PAGE->cm->context) && ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$PAGE->cm->instance, 'approved'=>0)))) {
$glossarynode->add(get_string('waitingapproval', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$PAGE->cm->id, 'mode'=>'approval')));
}
if (has_capability('mod/glossary:write', $PAGE->cm->context)) {
$glossarynode->add(get_string('addentry', 'glossary'), new moodle_url('/mod/glossary/edit.php', array('cmid'=>$PAGE->cm->id)));
}
$glossary = $DB->get_record('glossary', array("id" => $PAGE->cm->instance));
if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds) && $glossary->rsstype && $glossary->rssarticles && can_access_course($PAGE->course, $USER)) {
require_once("$CFG->libdir/rsslib.php");
$string = get_string('rsstype','forum');
$url = new moodle_url(rss_get_url($PAGE->cm->context->id, $USER->id, 'mod_glossary', $glossary->id));
$glossarynode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
}
}
示例6: bigbluebuttonbn_bbb_broker_add_error
}
if (isset($SESSION->bigbluebuttonbn_bbbsession) && !is_null($SESSION->bigbluebuttonbn_bbbsession)) {
$bbbsession = $SESSION->bigbluebuttonbn_bbbsession;
} else {
$error = bigbluebuttonbn_bbb_broker_add_error($error, "No session variable set");
}
}
}
header('Content-Type: application/javascript; charset=utf-8');
if (empty($error)) {
if (!isloggedin() && $PAGE->course->id == SITEID) {
$userid = guest_user()->id;
} else {
$userid = $USER->id;
}
$hascourseaccess = $PAGE->course->id == SITEID || can_access_course($PAGE->course, $userid);
if (!$hascourseaccess) {
header("HTTP/1.0 401 Unauthorized");
return;
} else {
try {
switch (strtolower($params['action'])) {
case 'meeting_info':
$meeting_info = bigbluebuttonbn_bbb_broker_get_meeting_info($params['id'], $bbbsession['modPW']);
$meeting_running = bigbluebuttonbn_bbb_broker_is_meeting_running($meeting_info);
$status_can_end = '';
$status_can_tag = '';
if ($meeting_running) {
$join_button_text = get_string('view_conference_action_join', 'bigbluebuttonbn');
if ($bbbsession['userlimit'] == 0 || $meeting_info->participantCount < $bbbsession['userlimit']) {
$initial_message = get_string('view_message_conference_in_progress', 'bigbluebuttonbn');
示例7: load_for_user
//.........这里部分代码省略.........
if (!empty($CFG->messaging)) {
$messageargs = array('user1' => $USER->id);
if ($USER->id != $user->id) {
$messageargs['user2'] = $user->id;
}
if ($course->id != $SITE->id) {
$messageargs['viewing'] = MESSAGE_VIEW_COURSE. $course->id;
}
$url = new moodle_url('/message/index.php',$messageargs);
$usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages');
}
if ($iscurrentuser && has_capability('moodle/user:manageownfiles', context_user::instance($USER->id))) {
$url = new moodle_url('/user/files.php');
$usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING);
}
if (!empty($CFG->enablebadges) && $iscurrentuser &&
has_capability('moodle/badges:manageownbadges', context_user::instance($USER->id))) {
$url = new moodle_url('/badges/mybadges.php');
$usernode->add(get_string('mybadges', 'badges'), $url, self::TYPE_SETTING);
}
// Add a node to view the users notes if permitted
if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) {
$url = new moodle_url('/notes/index.php',array('user'=>$user->id));
if ($coursecontext->instanceid) {
$url->param('course', $coursecontext->instanceid);
}
$usernode->add(get_string('notes', 'notes'), $url);
}
// If the user is the current user add the repositories for the current user
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
if ($iscurrentuser) {
if (!$this->cache->cached('contexthasrepos'.$usercontext->id)) {
require_once($CFG->dirroot . '/repository/lib.php');
$editabletypes = repository::get_editable_types($usercontext);
$haseditabletypes = !empty($editabletypes);
unset($editabletypes);
$this->cache->set('contexthasrepos'.$usercontext->id, $haseditabletypes);
} else {
$haseditabletypes = $this->cache->{'contexthasrepos'.$usercontext->id};
}
if ($haseditabletypes) {
$usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id)));
}
} else if ($course->id == $SITE->id && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) {
// Add view grade report is permitted
$reports = core_component::get_plugin_list('gradereport');
arsort($reports); // user is last, we want to test it first
$userscourses = enrol_get_users_courses($user->id);
$userscoursesnode = $usernode->add(get_string('courses'));
foreach ($userscourses as $usercourse) {
$usercoursecontext = context_course::instance($usercourse->id);
$usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext));
$usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$usercourse->id)), self::TYPE_CONTAINER);
$gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext);
if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) {
foreach ($reports as $plugin => $plugindir) {
if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) {
//stop when the first visible plugin is found
$gradeavailable = true;
break;
}
}
}
if ($gradeavailable) {
$url = new moodle_url('/grade/report/index.php', array('id'=>$usercourse->id));
$usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', ''));
}
// Add a node to view the users notes if permitted
if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) {
$url = new moodle_url('/notes/index.php',array('user'=>$user->id, 'course'=>$usercourse->id));
$usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING);
}
if (can_access_course($usercourse, $user->id)) {
$usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', array('id'=>$usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', ''));
}
$reporttab = $usercoursenode->add(get_string('activityreports'));
$reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php');
foreach ($reports as $reportfunction) {
$reportfunction($reporttab, $user, $usercourse);
}
$reporttab->trim_if_empty();
}
}
return true;
}
示例8: glossary_rss_get_feed
function glossary_rss_get_feed($context, $args)
{
global $CFG, $DB, $COURSE, $USER;
$status = true;
if (empty($CFG->glossary_enablerssfeeds)) {
debugging("DISABLED (module configuration)");
return null;
}
$glossaryid = clean_param($args[3], PARAM_INT);
$cm = get_coursemodule_from_instance('glossary', $glossaryid, 0, false, MUST_EXIST);
if ($cm) {
$modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($COURSE->id == $cm->course) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
}
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
//context id from db should match the submitted one
//no specific capability required to view glossary entries so just check user is enrolled
if ($context->id != $modcontext->id || !can_access_course($coursecontext, $USER)) {
return null;
}
}
$glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
if (!rss_enabled_for_mod('glossary', $glossary)) {
return null;
}
$sql = glossary_rss_get_sql($glossary);
//get the cache file info
$filename = rss_get_file_name($glossary, $sql);
$cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
//Is the cache out of date?
$cachedfilelastmodified = 0;
if (file_exists($cachedfilepath)) {
$cachedfilelastmodified = filemtime($cachedfilepath);
}
//if the cache is more than 60 seconds old and there's new stuff
$dontrecheckcutoff = time() - 60;
if ($dontrecheckcutoff > $cachedfilelastmodified && glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
if (!($recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles))) {
return null;
}
$items = array();
$formatoptions = new stdClass();
$formatoptions->trusttext = true;
foreach ($recs as $rec) {
$item = new stdClass();
$user = new stdClass();
$item->title = $rec->entryconcept;
if ($glossary->rsstype == 1) {
//With author
$user->firstname = $rec->userfirstname;
$user->lastname = $rec->userlastname;
$item->author = fullname($user);
}
$item->pubdate = $rec->entrytimecreated;
$item->link = $CFG->wwwroot . "/mod/glossary/showentry.php?courseid=" . $glossary->course . "&eid=" . $rec->entryid;
$item->description = format_text($rec->entrydefinition, $rec->entryformat, $formatoptions, $glossary->course);
$items[] = $item;
}
//First all rss feeds common headers
$header = rss_standard_header(format_string($glossary->name, true), $CFG->wwwroot . "/mod/glossary/view.php?g=" . $glossary->id, format_string($glossary->intro, true));
//Now all the rss items
if (!empty($header)) {
$articles = rss_add_items($items);
}
//Now all rss feeds common footers
if (!empty($header) && !empty($articles)) {
$footer = rss_standard_footer();
}
//Now, if everything is ok, concatenate it
if (!empty($header) && !empty($articles) && !empty($footer)) {
$rss = $header . $articles . $footer;
//Save the XML contents to file.
$status = rss_save_file('mod_glossary', $filename, $rss);
}
}
if (!$status) {
$cachedfilepath = null;
}
return $cachedfilepath;
}
示例9: get_activity_allowed_groups
/**
* Gets a list of groups that the user is allowed to access within the specified activity.
*
* @throws moodle_exception
* @param int $cmid course module id
* @param int $userid id of user.
* @return array of group objects (id, name, description, format) and possible warnings.
* @since Moodle 3.0
*/
public static function get_activity_allowed_groups($cmid, $userid = 0)
{
global $USER;
// Warnings array, it can be empty at the end but is mandatory.
$warnings = array();
$params = array('cmid' => $cmid, 'userid' => $userid);
$params = self::validate_parameters(self::get_activity_allowed_groups_parameters(), $params);
$cmid = $params['cmid'];
$userid = $params['userid'];
$cm = get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST);
// Security checks.
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($cm->course);
self::validate_context($context);
if (empty($userid)) {
$userid = $USER->id;
}
$user = core_user::get_user($userid, '*', MUST_EXIST);
core_user::require_active_user($user);
// Check if we have permissions for retrieve the information.
if ($user->id != $USER->id) {
if (!has_capability('moodle/course:managegroups', $context)) {
throw new moodle_exception('accessdenied', 'admin');
}
// Validate if the user is enrolled in the course.
$course = get_course($cm->course);
if (!can_access_course($course, $user, '', true)) {
// We return a warning because the function does not fail for not enrolled users.
$warning = array();
$warning['item'] = 'course';
$warning['itemid'] = $cm->course;
$warning['warningcode'] = '1';
$warning['message'] = "User {$user->id} cannot access course {$cm->course}";
$warnings[] = $warning;
}
}
$usergroups = array();
if (empty($warnings)) {
$groups = groups_get_activity_allowed_groups($cm, $user->id);
foreach ($groups as $group) {
list($group->description, $group->descriptionformat) = external_format_text($group->description, $group->descriptionformat, $coursecontext->id, 'group', 'description', $group->id);
$group->courseid = $cm->course;
$usergroups[] = $group;
}
}
$results = array('groups' => $usergroups, 'warnings' => $warnings);
return $results;
}
示例10: can_access_course
/**
* Checks if current user has access to the course
*
* This method calls global function {@link can_access_course} and caches results
*
* @param int $courseid
* @return bool
*/
public function can_access_course($courseid)
{
$this->init_course_access();
if (!array_key_exists($courseid, $this->courseaccess)) {
$this->courseaccess[$courseid] = can_access_course($this->get_course($courseid)) ? 1 : 0;
$this->cachechangedcourse = true;
}
return $this->courseaccess[$courseid];
}
示例11: user_can_access_course
/**
* Determines a user's access to a course with a given capability, using cached results where possible.
*
* @since 3.2.0
* @param \stdClass $user the user record.
* @param \stdClass $course the course record.
* @param string $capability the capability to check.
* @return bool true if the user can access the course with the specified capability, false otherwise.
*/
protected function user_can_access_course($user, $course, $capability)
{
if (!isset($this->courseaccesscache[$course->id][$user->id][$capability])) {
$this->courseaccesscache[$course->id][$user->id][$capability] = can_access_course($course, $user, $capability, true);
}
return $this->courseaccesscache[$course->id][$user->id][$capability];
}
示例12: print_course_toc
/**
* Print table of contents for a course
*
* @Author: Stuart Lamour
*/
public function print_course_toc()
{
global $COURSE;
// No access to course, return nothing.
if (!can_access_course($COURSE)) {
return '';
}
$format = course_get_format($this->page->course);
$course = $format->get_course();
// We don't want to display the toc if the current course is the site.
if ($COURSE->id == SITEID) {
return;
}
// If course does not have any sections then exit - it can't be a course without sections!!!
if (!isset($course->numsections)) {
return;
}
$singlepage = true;
if ($COURSE->format === 'folderview') {
$singlepage = false;
}
$contents = get_string('contents', 'theme_snap');
$o = '<nav id="course-toc">
<div>
<h2 id="toc-desktop-menu-heading">
<span class=sr-only>Page</span>' . $contents . '</h2>
<form id="toc-search" onSubmit="return false;">
<input id="toc-search-input" type="search" title="' . get_string("search") . '" placeholder="' . get_string("search") . '" aria-autocomplete="list" aria-haspopup="true" aria-activedescendant="toc-search-results" autocomplete="off" />
' . $this->modulesearch() . '
</form>
<a id="toc-mobile-menu-toggle" title="' . $contents . '" href="#course-toc"><i class="icon icon-office-52"></i></a>
</div>';
$listlarge = '';
if ($course->numsections > 9) {
$listlarge = "list-large";
}
$toc = '<ol id="chapters" class="chapters ' . $listlarge . '" start="0">';
course_create_sections_if_missing($course, range(0, $course->numsections));
$canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id));
$modinfo = get_fast_modinfo($course);
foreach ($modinfo->get_section_info_all() as $section => $thissection) {
if ($section > $course->numsections) {
continue;
}
// Students - If course hidden sections completely invisible & section is hidden, and you cannot
// see hidden things, bale out.
if ($course->hiddensections && !$thissection->visible && !$canviewhidden) {
continue;
}
$linkinfo = '';
$outputlink = true;
$conditional = $this->is_section_conditional($thissection);
if ($canviewhidden) {
// Teachers.
if ($conditional) {
$linkinfo .= $this->toc_linkinfo(get_string('conditional', 'theme_snap'));
}
if (!$thissection->visible) {
$linkinfo .= $this->toc_linkinfo(get_string('notpublished', 'theme_snap'));
}
} else {
// Students.
if ($conditional && $thissection->availableinfo) {
// Conditional section, with text explaining conditions.
$linkinfo .= $this->toc_linkinfo(get_string('conditional', 'theme_snap'));
}
if ($conditional && !$thissection->uservisible && !$thissection->availableinfo) {
// Conditional section, totally hidden from user so skip.
continue;
}
if (!$conditional && !$thissection->visible) {
// Hidden section collapsed, so show as text in TOC.
$outputlink = false;
// Top trump - if not clickable, replace linkinfo.
$linkinfo = $this->toc_linkinfo(get_string('notavailable'));
}
}
/*
// fun understanding what all these vars mean //
$linkinfo .= $course->hiddensections;
// visible - shows on conditionals when not completely hidden
if($thissection->visible){
$linkinfo .= " section->visible ";
}
// uservisible - shows while conditions are met?
if($thissection->uservisible){
$linkinfo .= " section->uservisible ";
}
// available - shown on hidden when 'colapsed'
if($thissection->available){
$linkinfo .= " section->available ";
}
// availability info - shown on conditional when not hidden or met
if($thissection->availableinfo){
$linkinfo .= " section->availableinfo ";
//.........这里部分代码省略.........
示例13: view_notes
/**
* Simulates the web interface view of notes/index.php: trigger events
*
* @param int $courseid id of the course
* @param int $userid id of the user
* @return array of warnings and status result
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function view_notes($courseid, $userid = 0)
{
global $CFG;
require_once $CFG->dirroot . "/notes/lib.php";
if (empty($CFG->enablenotes)) {
throw new moodle_exception('notesdisabled', 'notes');
}
$warnings = array();
$arrayparams = array('courseid' => $courseid, 'userid' => $userid);
$params = self::validate_parameters(self::view_notes_parameters(), $arrayparams);
if (empty($params['courseid'])) {
$params['courseid'] = SITEID;
}
$course = get_course($params['courseid']);
if ($course->id == SITEID) {
$context = context_system::instance();
} else {
$context = context_course::instance($course->id);
}
// First of all, validate the context before do further permission checks.
self::validate_context($context);
require_capability('moodle/notes:view', $context);
if (!empty($params['userid'])) {
$user = core_user::get_user($params['userid'], 'id, deleted', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
throw new moodle_exception('invaliduserid');
}
if ($course->id != SITEID and !can_access_course($course, $user, '', true)) {
throw new moodle_exception('notenrolledprofile');
}
}
note_view($context, $params['userid']);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
示例14: forum_get_posts_by_user
/**
* Returns posts made by the selected user in the requested courses.
*
* This method can be used to return all of the posts made by the requested user
* within the given courses.
* For each course the access of the current user and requested user is checked
* and then for each post access to the post and forum is checked as well.
*
* This function is safe to use with usercapabilities.
*
* @global moodle_database $DB
* @param stdClass $user The user whose posts we want to get
* @param array $courses The courses to search
* @param bool $musthaveaccess If set to true errors will be thrown if the user
* cannot access one or more of the courses to search
* @param bool $discussionsonly If set to true only discussion starting posts
* will be returned.
* @param int $limitfrom The offset of records to return
* @param int $limitnum The number of records to return
* @return stdClass An object the following properties
* ->totalcount: the total number of posts made by the requested user
* that the current user can see.
* ->courses: An array of courses the current user can see that the
* requested user has posted in.
* ->forums: An array of forums relating to the posts returned in the
* property below.
* ->posts: An array containing the posts to show for this request.
*/
function forum_get_posts_by_user($user, array $courses, $musthaveaccess = false, $discussionsonly = false, $limitfrom = 0, $limitnum = 50) {
global $DB, $USER, $CFG;
$return = new stdClass;
$return->totalcount = 0; // The total number of posts that the current user is able to view
$return->courses = array(); // The courses the current user can access
$return->forums = array(); // The forums that the current user can access that contain posts
$return->posts = array(); // The posts to display
// First up a small sanity check. If there are no courses to check we can
// return immediately, there is obviously nothing to search.
if (empty($courses)) {
return $return;
}
// A couple of quick setups
$isloggedin = isloggedin();
$isguestuser = $isloggedin && isguestuser();
$iscurrentuser = $isloggedin && $USER->id == $user->id;
// Checkout whether or not the current user has capabilities over the requested
// user and if so they have the capabilities required to view the requested
// users content.
$usercontext = context_user::instance($user->id, MUST_EXIST);
$hascapsonuser = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id));
$hascapsonuser = $hascapsonuser && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
// Before we actually search each course we need to check the user's access to the
// course. If the user doesn't have the appropraite access then we either throw an
// error if a particular course was requested or we just skip over the course.
foreach ($courses as $course) {
$coursecontext = context_course::instance($course->id, MUST_EXIST);
if ($iscurrentuser || $hascapsonuser) {
// If it is the current user, or the current user has capabilities to the
// requested user then all we need to do is check the requested users
// current access to the course.
// Note: There is no need to check group access or anything of the like
// as either the current user is the requested user, or has granted
// capabilities on the requested user. Either way they can see what the
// requested user posted, although its VERY unlikely in the `parent` situation
// that the current user will be able to view the posts in context.
if (!is_viewing($coursecontext, $user) && !is_enrolled($coursecontext, $user)) {
// Need to have full access to a course to see the rest of own info
if ($musthaveaccess) {
print_error('errorenrolmentrequired', 'forum');
}
continue;
}
} else {
// Check whether the current user is enrolled or has access to view the course
// if they don't we immediately have a problem.
if (!can_access_course($course)) {
if ($musthaveaccess) {
print_error('errorenrolmentrequired', 'forum');
}
continue;
}
// Check whether the requested user is enrolled or has access to view the course
// if they don't we immediately have a problem.
if (!can_access_course($course, $user)) {
if ($musthaveaccess) {
print_error('notenrolled', 'forum');
}
continue;
}
// If groups are in use and enforced throughout the course then make sure
// we can meet in at least one course level group.
// Note that we check if either the current user or the requested user have
// the capability to access all groups. This is because with that capability
// a user in group A could post in the group B forum. Grrrr.
//.........这里部分代码省略.........
示例15: view_user_profile
/**
* Simulate the /user/index.php and /user/profile.php web interface page triggering events
*
* @param int $userid id of user
* @param int $courseid id of course
* @return array of warnings and status result
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function view_user_profile($userid, $courseid = 0)
{
global $CFG, $USER;
require_once $CFG->dirroot . "/user/profile/lib.php";
$params = self::validate_parameters(self::view_user_profile_parameters(), array('userid' => $userid, 'courseid' => $courseid));
$warnings = array();
if (empty($params['userid'])) {
$params['userid'] = $USER->id;
}
if (empty($params['courseid'])) {
$params['courseid'] = SITEID;
}
$course = get_course($params['courseid']);
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
// Can not view profile of guest - thre is nothing to see there.
throw new moodle_exception('invaliduserid');
}
if ($course->id == SITEID) {
$coursecontext = context_system::instance();
} else {
$coursecontext = context_course::instance($course->id);
}
self::validate_context($coursecontext);
$currentuser = $USER->id == $user->id;
$usercontext = context_user::instance($user->id);
if (!$currentuser and !has_capability('moodle/user:viewdetails', $coursecontext) and !has_capability('moodle/user:viewdetails', $usercontext)) {
throw new moodle_exception('cannotviewprofile');
}
// Case like user/profile.php.
if ($course->id == SITEID) {
profile_view($user, $usercontext);
} else {
// Case like user/view.php.
if (!$currentuser and !can_access_course($course, $user, '', true)) {
throw new moodle_exception('notenrolledprofile');
}
profile_view($user, $coursecontext, $course);
}
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}