/** * Adds module specific settings to the settings block. * * @param settings_navigation $settings The settings navigation object * @param stdClass $context The node context */ function local_loginas_extends_settings_navigation(settings_navigation $settings, $context) { global $DB, $CFG, $PAGE, $USER; // Course id and context. $courseid = !empty($PAGE->course->id) ? $PAGE->course->id : SITEID; $coursecontext = context_course::instance($courseid); // Must have the loginas capability. if (!has_capability('moodle/user:loginas', $coursecontext)) { return; } // Set the settings category. $loginas = $settings->add(get_string('loginas')); // Login as list by admin setting. if (is_siteadmin($USER)) { // Admin settings page. $url = new moodle_url('/admin/settings.php', array('section' => 'localsettingloginas')); $loginas->add(get_string('settings'), $url, $settings::TYPE_SETTING); // Users list. $loginasusers = array(); // Since 2.6, use all the required fields. $ufields = 'id, ' . get_all_user_name_fields(true); // Get users by id. if ($configuserids = get_config('local_loginas', 'loginasusers')) { $userids = explode(',', $configuserids); if ($users = $DB->get_records_list('user', 'id', $userids, '', $ufields)) { $loginasusers = $users; } } // Get users by username. if ($configusernames = get_config('local_loginas', 'loginasusernames')) { $usernames = explode(',', $configusernames); if ($users = $DB->get_records_list('user', 'username', $usernames, '', $ufields)) { $loginasusers = $loginasusers + $users; } } // Add action links for specified users. if ($loginasusers) { $params = array('id' => $courseid, 'sesskey' => sesskey()); foreach ($loginasusers as $userid => $lauser) { $url = new moodle_url('/course/loginas.php', $params); $url->param('user', $userid); $loginas->add(fullname($lauser, true), $url, $settings::TYPE_SETTING); } } } // Course users login as. if (!($configcourseusers = get_config('local_loginas', 'courseusers'))) { return; } $loggedinas = \core\session\manager::is_loggedinas(); if (!$loggedinas) { // Ajax link. $node = $loginas->add(get_string('courseusers', 'local_loginas'), 'javascript:void();', $settings::TYPE_SETTING); $node->add_class('local_loginas_setting_link'); local_loginas_require_js($PAGE); } }
/** * This function extends the course navigation with the report items * * @param navigation_node $navigation The navigation node to extend * @param stdClass $user * @param stdClass $course The course to object for the report */ function report_usersessions_extend_navigation_user($navigation, $user, $course) { global $USER; if (isguestuser() or !isloggedin()) { return; } if (\core\session\manager::is_loggedinas() or $USER->id != $user->id) { // No peeking at somebody else's sessions! return; } $context = context_user::instance($USER->id); if (has_capability('report/usersessions:manageownsessions', $context)) { $navigation->add(get_string('navigationlink', 'report_usersessions'), new moodle_url('/report/usersessions/user.php'), $navigation::TYPE_SETTING); } }
/** * Add nodes to myprofile page. * * @param \core_user\output\myprofile\tree $tree Tree object * @param stdClass $user user object * @param bool $iscurrentuser * @param stdClass $course Course object * * @return bool */ function report_usersessions_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { global $USER; if (isguestuser() or !isloggedin()) { return; } if (\core\session\manager::is_loggedinas() or $USER->id != $user->id) { // No peeking at somebody else's sessions! return; } $context = context_user::instance($USER->id); if (has_capability('report/usersessions:manageownsessions', $context)) { $node = new core_user\output\myprofile\node('reports', 'usersessions', get_string('navigationlink', 'report_usersessions'), null, new moodle_url('/report/usersessions/user.php')); $tree->add_node($node); } return true; }
/** * Write event in the store with buffering. Method insert_event_entries() must be * defined. * * @param \core\event\base $event * * @return void */ public function write(\core\event\base $event) { global $PAGE; if ($this->is_event_ignored($event)) { return; } // We need to capture current info at this moment, // at the same time this lowers memory use because // snapshots and custom objects may be garbage collected. $entry = $event->get_data(); $entry['other'] = serialize($entry['other']); $entry['origin'] = $PAGE->requestorigin; $entry['ip'] = $PAGE->requestip; $entry['realuserid'] = \core\session\manager::is_loggedinas() ? $GLOBALS['USER']->realuser : null; $this->buffer[] = $entry; $this->count++; if (!isset($this->buffersize)) { $this->buffersize = $this->get_config('buffersize', 50); } if ($this->count >= $this->buffersize) { $this->flush(); } }
/** * Return the standard string that says whether you are logged in (and switched * roles/logged in as another user). * @param bool $withlinks if false, then don't include any links in the HTML produced. * If not set, the default is the nologinlinks option from the theme config.php file, * and if that is not set, then links are included. * @return string HTML fragment. */ public function login_info($withlinks = null) { global $USER, $CFG, $DB, $SESSION; if (during_initial_install()) { return ''; } if (is_null($withlinks)) { $withlinks = empty($this->page->layout_options['nologinlinks']); } $loginpage = (string) $this->page->url === get_login_url(); $course = $this->page->course; if (\core\session\manager::is_loggedinas()) { $realuser = session_get_realuser(); $fullname = fullname($realuser, true); if ($withlinks) { $realuserinfo = " [<a href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&sesskey=" . sesskey() . "\">{$fullname}</a>] "; } else { $realuserinfo = " [{$fullname}] "; } } else { $realuserinfo = ''; } $loginurl = get_login_url(); if (empty($course->id)) { // $course->id is not defined during installation return ''; } else { if (isloggedin()) { $context = context_course::instance($course->id); $fullname = fullname($USER, true); // Since Moodle 2.0 this link always goes to the public profile page (not the course profile page) if ($withlinks) { $username = "******"{$CFG->wwwroot}/user/profile.php?id={$USER->id}\">{$fullname}</a>"; } else { $username = $fullname; } if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid))) { if ($withlinks) { $username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>"; } else { $username .= " from {$idprovider->name}"; } } if (isguestuser()) { $loggedinas = $realuserinfo . get_string('loggedinasguest'); if (!$loginpage && $withlinks) { $loggedinas .= '<a class="btn btn-small btn-default" href="' . $loginurl . '"><i class="fa fa-sign-in"></i> ' . get_string('login') . '</a>'; } } else { if (is_role_switched($course->id)) { // Has switched roles $rolename = ''; if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) { $rolename = ': ' . format_string($role->name); } $loggedinas = get_string('loggedinas', 'moodle', $username) . $rolename; if ($withlinks) { $loggedinas .= ' (<a href="$CFG->wwwroot/course/view.php?id=$course->id&switchrole=0&sesskey=' . sesskey() . '">' . get_string('switchrolereturn') . '</a>)'; } } else { $loggedinas = $realuserinfo . get_string('loggedinas', 'moodle', $username); if ($withlinks) { $loggedinas .= ' <a class="btn btn-small" href="' . $CFG->wwwroot . '/login/logout.php?sesskey=' . sesskey() . '"><i class="fa fa-sign-out"></i> ' . get_string('logout') . '</a>'; } } } } else { $loggedinas = get_string('loggedinnot', 'moodle'); if (!$loginpage && $withlinks) { $loggedinas .= ' <a class="btn btn-small btn-default" href="' . $loginurl . '"><i class="fa fa-sign-in"></i> ' . get_string('login') . '</a>'; } } } $loggedinas = '<div class="logininfo">' . $loggedinas . '</div>'; if (isset($SESSION->justloggedin)) { unset($SESSION->justloggedin); if (!empty($CFG->displayloginfailures)) { if (!isguestuser()) { // Include this file only when required. require_once $CFG->dirroot . '/user/lib.php'; if ($count = user_count_login_failures($USER)) { $loggedinas .= '<div class="loginfailures">'; $a = new stdClass(); $a->attempts = $count; $loggedinas .= get_string('failedloginattempts', '', $a); if (file_exists("{$CFG->dirroot}/report/log/index.php") and has_capability('report/log:view', context_system::instance())) { $loggedinas .= ' (' . html_writer::link(new moodle_url('/report/log/index.php', array('chooselog' => 1, 'id' => 0, 'modid' => 'site_errors')), get_string('logs')) . ')'; } $loggedinas .= '</div>'; } } } } return $loggedinas; }
/** * Return the standard string that says whether you are logged in (and switched * roles/logged in as another user). * @param bool $withlinks if false, then don't include any links in the HTML produced. * If not set, the default is the nologinlinks option from the theme config.php file, * and if that is not set, then links are included. * @return string HTML fragment. */ public function login_info($withlinks = null) { global $USER, $CFG, $DB, $SESSION; if (during_initial_install()) { return ''; } if (is_null($withlinks)) { $withlinks = empty($this->page->layout_options['nologinlinks']); } $loginpage = (string) $this->page->url === get_login_url(); $course = $this->page->course; if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $fullname = fullname($realuser, true); if ($withlinks) { $loginastitle = get_string('loginas'); $realuserinfo = " [<a href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&sesskey=" . sesskey() . "\""; $realuserinfo .= "title =\"" . $loginastitle . "\">{$fullname}</a>] "; } else { $realuserinfo = " [{$fullname}] "; } } else { $realuserinfo = ''; } $loginurl = get_login_url(); if (empty($course->id)) { // $course->id is not defined during installation return ''; } else { if (isloggedin()) { $context = context_course::instance($course->id); $fullname = fullname($USER, true); // Since Moodle 2.0 this link always goes to the public profile page (not the course profile page) if ($withlinks) { $linktitle = get_string('viewprofile'); $username = "******"{$CFG->wwwroot}/user/profile.php?id={$USER->id}\" title=\"{$linktitle}\">{$fullname}</a>"; } else { $username = $fullname; } if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid))) { if ($withlinks) { $username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>"; } else { $username .= " from {$idprovider->name}"; } } if (isguestuser()) { $loggedinas = $realuserinfo . get_string('loggedinasguest'); if (!$loginpage && $withlinks) { $loggedinas .= " (<a href=\"{$loginurl}\">" . get_string('login') . '</a>)'; } } else { if (is_role_switched($course->id)) { // Has switched roles $rolename = ''; if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) { $rolename = ': ' . role_get_name($role, $context); } $loggedinas = get_string('loggedinas', 'moodle', $username) . $rolename; if ($withlinks) { $url = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false))); $loggedinas .= '(' . html_writer::tag('a', get_string('switchrolereturn'), array('href' => $url)) . ')'; } } else { $loggedinas = $realuserinfo . get_string('loggedinas', 'moodle', $username); if ($withlinks) { echo "<i class='fa fa-user hide979 mywhite'></i> "; //****************$loggedinas .= " (<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\">".get_string('logout').'</a>)'; $loggedinas .= " <span class=\"line-trans\">|</span><a class=\"logtop\" href=\"{$CFG->wwwroot}/login/logout.php?sesskey=" . sesskey() . "\"> " . get_string('logout') . '</a><span class="line-trans"> |</span>'; } } } } else { $loggedinas = get_string('loggedinnot', 'moodle'); if (!$loginpage && $withlinks) { //****************$loggedinas $loggedinas .= " (<a href=\"$loginurl\">".get_string('login').'</a>)'; echo "<i class='fa fa-lock hide979 mywhite'></i> "; $loggedinas .= " | <a href=\"{$loginurl}\">" . get_string('login') . '</a> |'; } } } $loggedinas = '<div class="logininfo">' . $loggedinas . '</div>'; if (isset($SESSION->justloggedin)) { unset($SESSION->justloggedin); if (!empty($CFG->displayloginfailures)) { if (!isguestuser()) { if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) { $loggedinas .= ' <div class="loginfailures">'; if (empty($count->accounts)) { $loggedinas .= get_string('failedloginattempts', '', $count); } else { $loggedinas .= get_string('failedloginattemptsall', '', $count); } if (file_exists("{$CFG->dirroot}/report/log/index.php") and has_capability('report/log:view', context_system::instance())) { $loggedinas .= ' (<a href="' . $CFG->wwwroot . '/report/log/index.php' . '?chooselog=1&id=1&modid=site_errors">' . get_string('logs') . '</a>)'; } $loggedinas .= '</div>'; } } } } return $loggedinas; }
/** * Checks if user has a capability to view the current repository. * * @return bool true when the user can, otherwise throws an exception. * @throws repository_exception when the user does not meet the requirements. */ public final function check_capability() { global $USER; // The context we are on. $currentcontext = $this->context; // Ensure that the user can view the repository in the current context. $can = has_capability('repository/' . $this->get_typename() . ':view', $currentcontext); // Context in which the repository has been created. $repocontext = context::instance_by_id($this->instance->contextid); // Prevent access to private repositories when logged in as. if ($can && \core\session\manager::is_loggedinas()) { if ($this->contains_private_data() || $repocontext->contextlevel == CONTEXT_USER) { $can = false; } } // We are going to ensure that the current context was legit, and reliable to check // the capability against. (No need to do that if we already cannot). if ($can) { if ($repocontext->contextlevel == CONTEXT_USER) { // The repository is a user instance, ensure we're the right user to access it! if ($repocontext->instanceid != $USER->id) { $can = false; } } else { if ($repocontext->contextlevel == CONTEXT_COURSE) { // The repository is a course one. Let's check that we are on the right course. if (in_array($currentcontext->contextlevel, array(CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_BLOCK))) { $coursecontext = $currentcontext->get_course_context(); if ($coursecontext->instanceid != $repocontext->instanceid) { $can = false; } } else { // We are on a parent context, therefore it's legit to check the permissions // in the current context. } } else { // Nothing to check here, system instances can have different permissions on different // levels. We do not want to prevent URL hack here, because it does not make sense to // prevent a user to access a repository in a context if it's accessible in another one. } } } if ($can) { return true; } throw new repository_exception('nopermissiontoaccess', 'repository'); }
/** * Get a list of essential user navigation items. * * @param stdclass $user user object. * @param moodle_page $page page object. * @return stdClass $returnobj navigation information object, where: * * $returnobj->navitems array array of links where each link is a * stdClass with fields url, title, and * pix * $returnobj->metadata array array of useful user metadata to be * used when constructing navigation; * fields include: * * ROLE FIELDS * asotherrole bool whether viewing as another role * rolename string name of the role * * USER FIELDS * These fields are for the currently-logged in user, or for * the user that the real user is currently logged in as. * * userid int the id of the user in question * userfullname string the user's full name * userprofileurl moodle_url the url of the user's profile * useravatar string a HTML fragment - the rendered * user_picture for this user * userloginfail string an error string denoting the number * of login failures since last login * * "REAL USER" FIELDS * These fields are for when asotheruser is true, and * correspond to the underlying "real user". * * asotheruser bool whether viewing as another user * realuserid int the id of the user in question * realuserfullname string the user's full name * realuserprofileurl moodle_url the url of the user's profile * realuseravatar string a HTML fragment - the rendered * user_picture for this user * * MNET PROVIDER FIELDS * asmnetuser bool whether viewing as a user from an * MNet provider * mnetidprovidername string name of the MNet provider * mnetidproviderwwwroot string URL of the MNet provider */ function user_get_user_navigation_info($user, $page) { global $OUTPUT, $DB, $SESSION, $CFG; $returnobject = new stdClass(); $returnobject->navitems = array(); $returnobject->metadata = array(); $course = $page->course; // Query the environment. $context = context_course::instance($course->id); // Get basic user metadata. $returnobject->metadata['userid'] = $user->id; $returnobject->metadata['userfullname'] = fullname($user, true); $returnobject->metadata['userprofileurl'] = new moodle_url('/user/profile.php', array('id' => $user->id)); $returnobject->metadata['useravatar'] = $OUTPUT->user_picture($user, array('link' => false, 'visibletoscreenreaders' => false)); // Build a list of items for a regular user. // Query MNet status. if ($returnobject->metadata['asmnetuser'] = is_mnet_remote_user($user)) { $mnetidprovider = $DB->get_record('mnet_host', array('id' => $user->mnethostid)); $returnobject->metadata['mnetidprovidername'] = $mnetidprovider->name; $returnobject->metadata['mnetidproviderwwwroot'] = $mnetidprovider->wwwroot; } // Did the user just log in? if (isset($SESSION->justloggedin)) { // Don't unset this flag as login_info still needs it. if (!empty($CFG->displayloginfailures)) { // We're already in /user/lib.php, so we don't need to include. if ($count = user_count_login_failures($user)) { // Get login failures string. $a = new stdClass(); $a->attempts = html_writer::tag('span', $count, array('class' => 'value')); $returnobject->metadata['userloginfail'] = get_string('failedloginattempts', '', $a); } } } // Links: Dashboard. $myhome = new stdClass(); $myhome->itemtype = 'link'; $myhome->url = new moodle_url('/my/'); $myhome->title = get_string('mymoodle', 'admin'); $myhome->pix = "i/course"; $returnobject->navitems[] = $myhome; // Links: My Profile. $myprofile = new stdClass(); $myprofile->itemtype = 'link'; $myprofile->url = new moodle_url('/user/profile.php', array('id' => $user->id)); $myprofile->title = get_string('profile'); $myprofile->pix = "i/user"; $returnobject->navitems[] = $myprofile; // Links: Role-return or logout link. $lastobj = null; $buildlogout = true; $returnobject->metadata['asotherrole'] = false; if (is_role_switched($course->id)) { if ($role = $DB->get_record('role', array('id' => $user->access['rsw'][$context->path]))) { // Build role-return link instead of logout link. $rolereturn = new stdClass(); $rolereturn->itemtype = 'link'; $rolereturn->url = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $page->url->out_as_local_url(false))); $rolereturn->pix = "a/logout"; $rolereturn->title = get_string('switchrolereturn'); $lastobj = $rolereturn; $returnobject->metadata['asotherrole'] = true; $returnobject->metadata['rolename'] = role_get_name($role, $context); $buildlogout = false; } } if ($returnobject->metadata['asotheruser'] = \core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); // Save values for the real user, as $user will be full of data for the // user the user is disguised as. $returnobject->metadata['realuserid'] = $realuser->id; $returnobject->metadata['realuserfullname'] = fullname($realuser, true); $returnobject->metadata['realuserprofileurl'] = new moodle_url('/user/profile.php', array('id' => $realuser->id)); $returnobject->metadata['realuseravatar'] = $OUTPUT->user_picture($realuser, array('link' => false, 'visibletoscreenreaders' => false)); // Build a user-revert link. $userrevert = new stdClass(); $userrevert->itemtype = 'link'; $userrevert->url = new moodle_url('/course/loginas.php', array('id' => $course->id, 'sesskey' => sesskey())); $userrevert->pix = "a/logout"; $userrevert->title = get_string('logout'); $lastobj = $userrevert; $buildlogout = false; } if ($buildlogout) { // Build a logout link. $logout = new stdClass(); $logout->itemtype = 'link'; $logout->url = new moodle_url('/login/logout.php', array('sesskey' => sesskey())); $logout->pix = "a/logout"; $logout->title = get_string('logout'); $lastobj = $logout; } // Before we add the last item (usually a logout link), add any // custom-defined items. $customitems = user_convert_text_to_menu_items($CFG->customusermenuitems, $page); foreach ($customitems as $item) { $returnobject->navitems[] = $item; } // Add the last item to the list. if (!is_null($lastobj)) { $returnobject->navitems[] = $lastobj; } return $returnobject; }
/** * Start output by sending the HTTP headers, and printing the HTML <head> * and the start of the <body>. * * To control what is printed, you should set properties on $PAGE. If you * are familiar with the old {@link print_header()} function from Moodle 1.9 * you will find that there are properties on $PAGE that correspond to most * of the old parameters to could be passed to print_header. * * Not that, in due course, the remaining $navigation, $menu parameters here * will be replaced by more properties of $PAGE, but that is still to do. * * @return string HTML that you must output this, preferably immediately. */ public function header() { global $USER, $CFG; if (\core\session\manager::is_loggedinas()) { $this->page->add_body_class('userloggedinas'); } // If the user is logged in, and we're not in initial install, // check to see if the user is role-switched and add the appropriate // CSS class to the body element. if (!during_initial_install() && isloggedin() && is_role_switched($this->page->course->id)) { $this->page->add_body_class('userswitchedrole'); } // Give themes a chance to init/alter the page object. $this->page->theme->init_page($this->page); $this->page->set_state(moodle_page::STATE_PRINTING_HEADER); // Find the appropriate page layout file, based on $this->page->pagelayout. $layoutfile = $this->page->theme->layout_file($this->page->pagelayout); // Render the layout using the layout file. $rendered = $this->render_page_layout($layoutfile); // Slice the rendered output into header and footer. $cutpos = strpos($rendered, $this->unique_main_content_token); if ($cutpos === false) { $cutpos = strpos($rendered, self::MAIN_CONTENT_TOKEN); $token = self::MAIN_CONTENT_TOKEN; } else { $token = $this->unique_main_content_token; } if ($cutpos === false) { throw new coding_exception('page layout file ' . $layoutfile . ' does not contain the main content placeholder, please include "<?php echo $OUTPUT->main_content() ?>" in theme layout file.'); } $header = substr($rendered, 0, $cutpos); $footer = substr($rendered, $cutpos + strlen($token)); if (empty($this->contenttype)) { debugging('The page layout file did not call $OUTPUT->doctype()'); $header = $this->doctype() . $header; } // If this theme version is below 2.4 release and this is a course view page if ((!isset($this->page->theme->settings->version) || $this->page->theme->settings->version < 2012101500) && $this->page->pagelayout === 'course' && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { // check if course content header/footer have not been output during render of theme layout $coursecontentheader = $this->course_content_header(true); $coursecontentfooter = $this->course_content_footer(true); if (!empty($coursecontentheader)) { // display debug message and add header and footer right above and below main content // Please note that course header and footer (to be displayed above and below the whole page) // are not displayed in this case at all. // Besides the content header and footer are not displayed on any other course page debugging('The current theme is not optimised for 2.4, the course-specific header and footer defined in course format will not be output', DEBUG_DEVELOPER); $header .= $coursecontentheader; $footer = $coursecontentfooter. $footer; } } send_headers($this->contenttype, $this->page->cacheable); $this->opencontainers->push('header/footer', $footer); $this->page->set_state(moodle_page::STATE_IN_BODY); return $header . $this->skip_link_target('maincontent'); }
redirect(get_login_url()); } $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); $context = context_course::instance($course->id, MUST_EXIST); // Everybody is enrolled on the frontpage if ($course->id == SITEID) { redirect("{$CFG->wwwroot}/"); } if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { print_error('coursehidden'); } $PAGE->set_course($course); $PAGE->set_pagelayout('course'); $PAGE->set_url('/enrol/index.php', array('id' => $course->id)); // do not allow enrols when in login-as session if (\core\session\manager::is_loggedinas() and $USER->loginascontext->contextlevel == CONTEXT_COURSE) { print_error('loginasnoenrol', '', $CFG->wwwroot . '/course/view.php?id=' . $USER->loginascontext->instanceid); } // get all enrol forms available in this course $enrols = enrol_get_plugins(true); $enrolinstances = enrol_get_instances($course->id, true); $forms = array(); foreach ($enrolinstances as $instance) { if (!isset($enrols[$instance->enrol])) { continue; } $form = $enrols[$instance->enrol]->enrol_page_hook($instance); if ($form) { $forms[$instance->id] = $form; } }
/** * Outputs the user menu. * @return custom_menu object */ public function custom_menu_user() { // Die if executed during install. if (during_initial_install()) { return false; } global $USER, $CFG, $DB; $loginurl = get_login_url(); $usermenu = html_writer::start_tag('ul', array('class' => 'nav')); $usermenu .= html_writer::start_tag('li', array('class' => 'dropdown')); if (!isloggedin()) { if ($this->page->pagelayout != 'login') { $userpic = '<em><i class="fa fa-sign-in"></i>' . get_string('login') . '</em>'; $usermenu .= html_writer::link($loginurl, $userpic, array('class' => 'loginurl')); } } else { if (isguestuser()) { $userurl = new moodle_url('#'); $userpic = parent::user_picture($USER, array('link' => false)); $caret = '<i class="fa fa-caret-right"></i>'; $userclass = array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'); $usermenu .= html_writer::link($userurl, $userpic . get_string('guest') . $caret, $userclass); // Render direct logout link. $usermenu .= html_writer::start_tag('ul', array('class' => 'dropdown-menu pull-right')); $branchlabel = '<em><i class="fa fa-sign-out"></i>' . get_string('logout') . '</em>'; $branchurl = new moodle_url('/login/logout.php'); $branchurl->param('sesskey', sesskey()); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); // Render Help Link. $usermenu .= $this->theme_essential_render_helplink(); $usermenu .= html_writer::end_tag('ul'); } else { $course = $this->page->course; $context = context_course::instance($course->id); // Output Profile link. $userurl = new moodle_url('#'); $userpic = parent::user_picture($USER, array('link' => false)); $caret = '<i class="fa fa-caret-right"></i>'; $userclass = array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'); if (!empty($USER->alternatename)) { $usermenu .= html_writer::link($userurl, $userpic . $USER->alternatename . $caret, $userclass); } else { $usermenu .= html_writer::link($userurl, $userpic . $USER->firstname . $caret, $userclass); } // Start dropdown menu items. $usermenu .= html_writer::start_tag('ul', array('class' => 'dropdown-menu pull-right')); if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $branchlabel = '<em><i class="fa fa-key"></i>' . fullname($realuser, true) . get_string('loggedinas', 'theme_essential') . fullname($USER, true) . '</em>'; } else { $branchlabel = '<em><i class="fa fa-user"></i>' . fullname($USER, true) . '</em>'; } $branchurl = new moodle_url('/user/profile.php', array('id' => $USER->id)); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); if (is_mnet_remote_user($USER) && ($idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid)))) { $branchlabel = '<em><i class="fa fa-users"></i>' . get_string('loggedinfrom', 'theme_essential') . $idprovider->name . '</em>'; $branchurl = new moodle_url($idprovider->wwwroot); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } if (is_role_switched($course->id)) { // Has switched roles. $branchlabel = '<em><i class="fa fa-users"></i>' . get_string('switchrolereturn') . '</em>'; $branchurl = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false))); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } // Add preferences submenu. $usermenu .= $this->theme_essential_render_preferences($context); $usermenu .= html_writer::empty_tag('hr', array('class' => 'sep')); // Output Calendar link if user is allowed to edit own calendar entries. if (has_capability('moodle/calendar:manageownentries', $context)) { $branchlabel = '<em><i class="fa fa-calendar"></i>' . get_string('pluginname', 'block_calendar_month') . '</em>'; $branchurl = new moodle_url('/calendar/view.php'); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } // Check if messaging is enabled. if (!empty($CFG->messaging)) { $branchlabel = '<em><i class="fa fa-envelope"></i>' . get_string('pluginname', 'block_messages') . '</em>'; $branchurl = new moodle_url('/message/index.php'); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } // Check if user is allowed to manage files. if (has_capability('moodle/user:manageownfiles', $context)) { $branchlabel = '<em><i class="fa fa-file"></i>' . get_string('privatefiles', 'block_private_files') . '</em>'; $branchurl = new moodle_url('/user/files.php'); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } // Check if user is allowed to view discussions. if (has_capability('mod/forum:viewdiscussion', $context)) { $branchlabel = '<em><i class="fa fa-list-alt"></i>' . get_string('forumposts', 'mod_forum') . '</em>'; $branchurl = new moodle_url('/mod/forum/user.php', array('id' => $USER->id)); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); $branchlabel = '<em><i class="fa fa-list"></i>' . get_string('discussions', 'mod_forum') . '</em>'; $branchurl = new moodle_url('/mod/forum/user.php', array('id' => $USER->id, 'mode' => 'discussions')); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); $usermenu .= html_writer::empty_tag('hr', array('class' => 'sep')); } // Output user grade links course sensitive, workaround for frontpage, selecting first enrolled course. if ($course->id == SITEID) { $branchlabel = '<em><i class="fa fa-list-alt"></i>' . get_string('mygrades', 'theme_essential') . '</em>'; $branchurl = new moodle_url('/grade/report/overview/index.php', array('id' => $course->id, 'userid' => $USER->id)); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } else { if (has_capability('gradereport/overview:view', $context)) { $branchlabel = '<em><i class="fa fa-list-alt"></i>' . get_string('mygrades', 'theme_essential') . '</em>'; $branchurl = new moodle_url('/grade/report/overview/index.php', array('id' => $course->id, 'userid' => $USER->id)); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } if (has_capability('gradereport/user:view', $context)) { // In Course also output Course grade links. $branchlabel = '<em><i class="fa fa-list-alt"></i>' . get_string('coursegrades', 'theme_essential') . '</em>'; $branchurl = new moodle_url('/grade/report/user/index.php', array('id' => $course->id, 'userid' => $USER->id)); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } } // Check if badges are enabled. if (!empty($CFG->enablebadges) && has_capability('moodle/badges:manageownbadges', $context)) { $branchlabel = '<em><i class="fa fa-certificate"></i>' . get_string('badges') . '</em>'; $branchurl = new moodle_url('/badges/mybadges.php'); $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); } $usermenu .= html_writer::empty_tag('hr', array('class' => 'sep')); // Render direct logout link. $branchlabel = '<em><i class="fa fa-sign-out"></i>' . get_string('logout') . '</em>'; if (\core\session\manager::is_loggedinas()) { $branchurl = new moodle_url('/course/loginas.php', array('id' => $course->id, 'sesskey' => sesskey())); } else { $branchurl = new moodle_url('/login/logout.php', array('sesskey' => sesskey())); } $usermenu .= html_writer::tag('li', html_writer::link($branchurl, $branchlabel)); // Render Help Link. $usermenu .= $this->theme_essential_render_helplink(); $usermenu .= html_writer::end_tag('ul'); } } $usermenu .= html_writer::end_tag('li'); $usermenu .= html_writer::end_tag('ul'); return $usermenu; }
public function test_log_writing() { global $DB, $CFG; $this->resetAfterTest(); $this->preventResetByRollback(); // Logging waits till the transaction gets committed. $dbman = $DB->get_manager(); $this->assertTrue($dbman->table_exists('logstore_standard_log')); $DB->delete_records('logstore_standard_log'); $this->setAdminUser(); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $course1 = $this->getDataGenerator()->create_course(); $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1)); $course2 = $this->getDataGenerator()->create_course(); $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2)); // Test all plugins are disabled by this command. set_config('enabled_stores', '', 'tool_log'); $manager = get_log_manager(true); $stores = $manager->get_readers(); $this->assertCount(0, $stores); // Fake the settings, we will abuse the standard plugin table here... $parts = explode('_', get_class($DB)); set_config('dbdriver', $parts[1] . '/' . $parts[0], 'logstore_database'); set_config('dbhost', $CFG->dbhost, 'logstore_database'); set_config('dbuser', $CFG->dbuser, 'logstore_database'); set_config('dbpass', $CFG->dbpass, 'logstore_database'); set_config('dbname', $CFG->dbname, 'logstore_database'); set_config('dbtable', $CFG->prefix . 'logstore_standard_log', 'logstore_database'); if (!empty($CFG->dboptions['dbpersist'])) { set_config('dbpersist', 1, 'logstore_database'); } else { set_config('dbpersist', 0, 'logstore_database'); } if (!empty($CFG->dboptions['dbsocket'])) { set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database'); } else { set_config('dbsocket', '', 'logstore_database'); } if (!empty($CFG->dboptions['dbport'])) { set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database'); } else { set_config('dbport', '', 'logstore_database'); } if (!empty($CFG->dboptions['dbschema'])) { set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database'); } else { set_config('dbschema', '', 'logstore_database'); } if (!empty($CFG->dboptions['dbcollation'])) { set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database'); } else { set_config('dbcollation', '', 'logstore_database'); } // Enable logging plugin. set_config('enabled_stores', 'logstore_database', 'tool_log'); set_config('buffersize', 0, 'logstore_database'); set_config('logguests', 1, 'logstore_database'); $manager = get_log_manager(true); $stores = $manager->get_readers(); $this->assertCount(1, $stores); $this->assertEquals(array('logstore_database'), array_keys($stores)); $store = $stores['logstore_database']; $this->assertInstanceOf('logstore_database\\log\\store', $store); $this->assertInstanceOf('tool_log\\log\\writer', $store); $this->assertTrue($store->is_logging()); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(0, $logs); $this->setCurrentTimeStart(); $this->setUser(0); $event1 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10))); $event1->trigger(); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(1, $logs); $log1 = reset($logs); unset($log1->id); $log1->other = unserialize($log1->other); $log1 = (array) $log1; $data = $event1->get_data(); $data['origin'] = 'cli'; $data['ip'] = null; $data['realuserid'] = null; $this->assertEquals($data, $log1); $this->setAdminUser(); \core\session\manager::loginas($user1->id, context_system::instance()); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); $event2 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9))); $event2->trigger(); \core\session\manager::init_empty_session(); $this->assertFalse(\core\session\manager::is_loggedinas()); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(3, $logs); array_shift($logs); $log2 = array_shift($logs); $this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname); $log3 = array_shift($logs); unset($log3->id); $log3->other = unserialize($log3->other); $log3 = (array) $log3; $data = $event2->get_data(); $data['origin'] = 'cli'; $data['ip'] = null; $data['realuserid'] = 2; $this->assertEquals($data, $log3); // Test reading. $this->assertSame(3, $store->get_events_select_count('', array())); $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC". $this->assertCount(3, $events); $resev1 = array_shift($events); array_shift($events); $resev2 = array_shift($events); $this->assertEquals($event1->get_data(), $resev1->get_data()); $this->assertEquals($event2->get_data(), $resev2->get_data()); // Test buffering. set_config('buffersize', 3, 'logstore_database'); $manager = get_log_manager(true); $stores = $manager->get_readers(); /** @var \logstore_database\log\store $store */ $store = $stores['logstore_database']; $DB->delete_records('logstore_standard_log'); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $store->flush(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(8, $DB->count_records('logstore_standard_log')); // Test guest logging setting. set_config('logguests', 0, 'logstore_database'); set_config('buffersize', 0, 'logstore_database'); get_log_manager(true); $DB->delete_records('logstore_standard_log'); get_log_manager(true); $this->setUser(null); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $this->setGuestUser(); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $this->setUser($user1); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(1, $DB->count_records('logstore_standard_log')); $this->setUser($user2); \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); set_config('enabled_stores', '', 'tool_log'); get_log_manager(true); }
/** Function to build the user's menu for the top navigation bar @param bool $withlinks - not sure if we want this @return string HTML fragment **/ public function user_menu($user = NULL, $withlinks = NULL) { global $USER, $CFG, $DB, $SESSION, $PAGE; // if we are during install return an empty string if (during_initial_install()) { return ''; } $course = $this->page->course; // at certain times - i.e. installation course id will be empty or null if (empty($course->id)) { return ''; } // this is the login page so return nothing $loginurl = get_login_url(); if ((string) $this->page->url === $loginurl) { return ''; } // user not logged in so return login link if (!isloggedin()) { return "<li><a href=\"{$loginurl}\">" . get_string('login') . '</a></li>'; } // begin building the user's display name $user_string = ""; // check if the user is logged in as someone else #if (session_is_loggedinas()) { # $realuser = session_get_realuser(); if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $user_string = fullname($realuser, true) . " pretending to be "; } $user_string .= fullname($USER, true); $role_switched = false; if (is_role_switched($course->id)) { // Has switched roles $context = context_course::instance($course->id); $role_switched = true; $rolename = ''; if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) { $rolename = ': ' . role_get_name($role, $context); } $user_string .= $rolename; } $user_picture = new user_picture($USER); $userimgsrc = $user_picture->get_url($PAGE); $user_menu = array("<li class='dropdown'>"); $user_menu[] = "<a class='dropdown-toggle' data-toggle='dropdown' href='#' style='min-height: 50px'>"; $user_menu[] = "<img class='nav-avatar' src='" . $userimgsrc . "'>"; $user_menu[] = "<span class='hidden-xs'>" . $user_string . "</span>"; $user_menu[] = "</a>"; $user_menu[] = "<ul class='dropdown-menu dropdown-menu-right'>"; if ($role_switched) { $url = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false))); $user_menu[] = "<li><a href='" . $url . "'>" . get_string('switchrolereturn') . "</a></li>"; } $user_menu[] = "<li><a href='" . $CFG->wwwroot . "/user/profile.php?id=" . $USER->id . "'>My Profile</a></li>"; $user_menu[] = "<li class='menu-item-resources'><a href='/'>My Modules</a></li>"; $user_menu[] = "<li id='student-email-link' style='display: none'><a href='http://studentmail.falmouth.ac.uk/'>My Email</a></li>"; $user_menu[] = "<li id='staff-email-link' style='display: none'><a href='http://mailspace.falmouth.ac.uk/'>My Email</a></li>"; $user_menu[] = "<li><a href='http://mytimetable.falmouth.ac.uk/'>My Timetable</a></li>"; $user_menu[] = "<li class='divider'></li>"; $user_menu[] = "<li><a href='" . $CFG->wwwroot . "/login/logout.php?sesskey=" . sesskey() . "'>" . get_string('logout') . '</a></li>'; $user_menu[] = "</ul></li>"; return implode("", $user_menu); }
function get_content() { global $CFG, $USER, $DB, $OUTPUT; // shortcut - only for logged in users! if (!isloggedin() || isguestuser()) { return false; } if (\core\session\manager::is_loggedinas()) { $this->content = new stdClass(); $this->content->footer = html_writer::tag('span', get_string('notpermittedtojumpas', 'mnet')); return $this->content; } // according to start_jump_session, // remote users can't on-jump // so don't show this block to them if (is_mnet_remote_user($USER)) { if (debugging() and !empty($CFG->debugdisplay)) { $this->content = new stdClass(); $this->content->footer = html_writer::tag('span', get_string('error_localusersonly', 'block_mnet_hosts'), array('class' => 'error')); return $this->content; } else { return ''; } } if (!is_enabled_auth('mnet')) { if (debugging() and !empty($CFG->debugdisplay)) { $this->content = new stdClass(); $this->content->footer = html_writer::tag('span', get_string('error_authmnetneeded', 'block_mnet_hosts'), array('class' => 'error')); return $this->content; } else { return ''; } } if (!has_capability('moodle/site:mnetlogintoremote', context_system::instance(), NULL, false)) { if (debugging() and !empty($CFG->debugdisplay)) { $this->content = new stdClass(); $this->content->footer = html_writer::tag('span', get_string('error_roamcapabilityneeded', 'block_mnet_hosts'), array('class' => 'error')); return $this->content; } else { return ''; } } if ($this->content !== NULL) { return $this->content; } // TODO: Test this query - it's appropriate? It works? // get the hosts and whether we are doing SSO with them $sql = "\n SELECT DISTINCT\n h.id,\n h.name,\n h.wwwroot,\n a.name as application,\n a.display_name\n FROM\n {mnet_host} h,\n {mnet_application} a,\n {mnet_host2service} h2s_IDP,\n {mnet_service} s_IDP,\n {mnet_host2service} h2s_SP,\n {mnet_service} s_SP\n WHERE\n h.id <> ? AND\n h.id <> ? AND\n h.id = h2s_IDP.hostid AND\n h.deleted = 0 AND\n h.applicationid = a.id AND\n h2s_IDP.serviceid = s_IDP.id AND\n s_IDP.name = 'sso_idp' AND\n h2s_IDP.publish = '1' AND\n h.id = h2s_SP.hostid AND\n h2s_SP.serviceid = s_SP.id AND\n s_SP.name = 'sso_idp' AND\n h2s_SP.publish = '1'\n ORDER BY\n a.display_name,\n h.name"; $hosts = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id, $CFG->mnet_all_hosts_id)); $this->content = new stdClass(); $this->content->items = array(); $this->content->icons = array(); $this->content->footer = ''; if ($hosts) { foreach ($hosts as $host) { $icon = '<img src="' . $OUTPUT->pix_url('i/' . $host->application . '_host') . '"' . ' class="icon" alt="' . get_string('server', 'block_mnet_hosts') . '" /> '; if ($host->id == $USER->mnethostid) { $this->content->items[] = "<a title=\"" . s($host->name) . "\" href=\"{$host->wwwroot}\">" . $icon . s($host->name) . "</a>"; } else { $this->content->items[] = "<a title=\"" . s($host->name) . "\" href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$host->id}\">" . $icon . s($host->name) . "</a>"; } } } return $this->content; }
/** * Print fixy (login or menu for signed in users) * */ public function print_fixed_menu() { global $CFG, $USER, $PAGE, $DB; $logout = get_string('logout'); $isguest = isguestuser(); if (!isloggedin() || $isguest) { $login = get_string('login'); $cancel = get_string('cancel'); $username = get_string('username'); $password = get_string('password'); $loginform = get_string('loginform', 'theme_snap'); $helpstr = ''; if (empty($CFG->forcelogin) || $isguest || !isloggedin() || !empty($CFG->registerauth) || is_enabled_auth('none') || !empty($CFG->auth_instructions)) { if ($isguest) { $helpstr = '<p class="text-center">' . get_string('loggedinasguest', 'theme_snap') . '</p>'; $helpstr .= '<p class="text-center">' . '<a class="btn btn-primary" href="' . s($CFG->wwwroot) . '/login/logout.php?sesskey=' . sesskey() . '">' . $logout . '</a></p>'; $helpstr .= '<p class="text-center">' . '<a href="' . s($CFG->wwwroot) . '/login/index.php">' . get_string('helpwithloginandguest', 'theme_snap') . '</a></p>'; } else { if (empty($CFG->forcelogin)) { $help = get_string('helpwithloginandguest', 'theme_snap'); } else { $help = get_string('helpwithlogin', 'theme_snap'); } $helpstr = "<p class='text-center'><a href='" . s($CFG->wwwroot) . "/login/index.php'>{$help}</a></p>"; } } echo $this->print_login_button(); echo "<form class=fixy action='{$CFG->wwwroot}/login/' method='post' id='login'>\n <a id='fixy-close' class='pull-right snap-action-icon' href='#'>\n <i class='icon icon-office-52'></i><small>{$cancel}</small>\n </a>\n <div class=fixy-inner>\n <legend>{$loginform}</legend>\n <label for='username'>{$username}</label>\n <input autocapitalize='off' type='text' name='username' id='username' placeholder='" . s($username) . "'>\n <label for='password'>{$password}</label>\n <input type='password' name='password' id='password' placeholder='" . s($password) . "'>\n <br>\n <input type='submit' id='loginbtn' value='" . s($login) . "'>\n {$helpstr}\n </div>\n </form>"; } else { $courselist = ""; $userpicture = new user_picture($USER); $userpicture->link = false; $userpicture->alttext = false; $userpicture->size = 100; $picture = $this->render($userpicture); $mycourses = enrol_get_my_courses(null, 'visible DESC, fullname ASC, id DESC'); $courselist .= "<section id='fixy-my-courses'><div class='clearfix'><h2>" . get_string('courses') . "</h2>"; foreach ($mycourses as $c) { $pubstatus = ""; if (!$c->visible) { $notpublished = get_string('notpublished', 'theme_snap'); $pubstatus = "<small class='published-status'>" . $notpublished . "</small>"; } $bgcolor = local::get_course_color($c->id); $courseimagecss = "background-color: #{$bgcolor};"; $bgimage = local::course_coverimage_url($c->id); if (!empty($bgimage)) { $courseimagecss .= "background-image: url({$bgimage});"; } $dynamicinfo = '<div data-courseid="' . $c->id . '" class=dynamicinfo></div>'; $teachers = ''; $courseteachers = ''; $clist = new course_in_list($c); $teachers = $clist->get_course_contacts(); if (!empty($teachers)) { $courseteachers = "<div class='sr-only'>" . get_string('coursecontacts', 'theme_snap') . "</div>"; // Get all teacher user records in one go. $teacherids = array(); foreach ($teachers as $teacher) { $teacherids[] = $teacher['user']->id; } $teacherusers = $DB->get_records_list('user', 'id', $teacherids); foreach ($teachers as $teacher) { if (!isset($teacherusers[$teacher['user']->id])) { continue; } $teacheruser = $teacherusers[$teacher['user']->id]; $userpicture = new user_picture($teacheruser); $userpicture->link = false; $userpicture->size = 100; $teacherpicture = $this->render($userpicture); $courseteachers .= $teacherpicture; } } $clink = '<div data-href="' . $CFG->wwwroot . '/course/view.php?id=' . $c->id . '" class="courseinfo" style="' . $courseimagecss . '"> <div class="courseinfo-body"><h3><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $c->id . '">' . format_string($c->fullname) . '</a></h3>' . $dynamicinfo . $courseteachers . $pubstatus . '</div></div>'; $courselist .= $clink; } $courselist .= "</div>"; $courselist .= '<div class="row fixy-browse-search-courses"><br>'; if (has_capability('moodle/site:config', context_system::instance())) { $courserenderer = $PAGE->get_renderer('core', 'course'); $courselist .= '<div class="col-md-6">'; $courselist .= $courserenderer->course_search_form(null, 'fixy'); $courselist .= '</div>'; } $courselist .= '<div class="col-md-6">'; $courselist .= $this->print_view_all_courses(); $courselist .= '</div>'; $courselist .= '</div></section>'; // Close row. $menu = get_string('menu', 'theme_snap'); echo '<a href="#primary-nav" aria-haspopup="true" class="fixy-trigger" id="js-personal-menu-trigger" ' . 'aria-controls="primary-nav" title="' . get_string('sitenavigation', 'theme_snap') . '" data-toggle="tooltip" data-placement="bottom">' . $menu . $picture . $this->render_badge_count() . '</a>'; $close = get_string('close', 'theme_snap'); $viewyourprofile = get_string('viewyourprofile', 'theme_snap'); $realuserinfo = ''; if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $via = get_string('via', 'theme_snap'); $fullname = fullname($realuser, true); $realuserinfo = html_writer::span($via . ' ' . html_writer::span($fullname, 'real-user-name'), 'real-user-info'); } echo '<nav id="primary-nav" class="fixy toggle-details" tabindex="0"> <a id="fixy-close" class="pull-right snap-action-icon" href="#"> <i class="icon icon-office-52"></i><small>' . $close . '</small> </a> <div class=fixy-inner> <h1 id="fixy-profile-link"> <a title="' . s($viewyourprofile) . '" href="' . s($CFG->wwwroot) . '/user/profile.php" >' . $picture . '<span id="fixy-username">' . format_string(fullname($USER)) . '</span> </a> </h1>' . $realuserinfo . $courselist . $this->render_callstoaction() . ' <div class="fixy-logout-footer clearfix text-center"> <a class="btn btn-default logout" href="' . s($CFG->wwwroot) . '/login/logout.php?sesskey=' . sesskey() . '">' . $logout . '</a> </div> </div><!-- end fixy-inner --> </nav><!-- end primary nav -->'; } }
/** * Store user last access times - called when use enters a course or site * * @package core * @category log * @global stdClass $USER * @global stdClass $CFG * @global moodle_database $DB * @uses LASTACCESS_UPDATE_SECS * @uses SITEID * @param int $courseid empty courseid means site * @return void */ function user_accesstime_log($courseid = 0) { global $USER, $CFG, $DB; if (!isloggedin() or \core\session\manager::is_loggedinas()) { // no access tracking return; } if (isguestuser()) { // Do not update guest access times/ips for performance. return; } if (empty($courseid)) { $courseid = SITEID; } $timenow = time(); /// Store site lastaccess time for the current user if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) { /// Update $USER->lastaccess for next checks $USER->lastaccess = $timenow; $last = new stdClass(); $last->id = $USER->id; $last->lastip = getremoteaddr(); $last->lastaccess = $timenow; $DB->update_record_raw('user', $last); } if ($courseid == SITEID) { /// no user_lastaccess for frontpage return; } /// Store course lastaccess times for the current user if (empty($USER->currentcourseaccess[$courseid]) or $timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS) { $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $USER->id, 'courseid' => $courseid)); if ($lastaccess === false) { // Update course lastaccess for next checks $USER->currentcourseaccess[$courseid] = $timenow; $last = new stdClass(); $last->userid = $USER->id; $last->courseid = $courseid; $last->timeaccess = $timenow; try { $DB->insert_record_raw('user_lastaccess', $last, false); } catch (dml_write_exception $e) { // During a race condition we can fail to find the data, then it appears. // If we still can't find it, rethrow the exception. $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $USER->id, 'courseid' => $courseid)); if ($lastaccess === false) { throw $e; } // If we did find it, the race condition was true and another thread has inserted the time for us. // We can just continue without having to do anything. } } else { if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) { // no need to update now, it was updated recently in concurrent login ;-) } else { // Update course lastaccess for next checks $USER->currentcourseaccess[$courseid] = $timenow; $DB->set_field('user_lastaccess', 'timeaccess', $timenow, array('userid' => $USER->id, 'courseid' => $courseid)); } } } }
/** * This function gets called by {@link settings_navigation::load_user_settings()} and actually works out * what can be shown/done * * @param int $courseid The current course' id * @param int $userid The user id to load for * @param string $gstitle The string to pass to get_string for the branch title * @return navigation_node|false */ protected function generate_user_settings($courseid, $userid, $gstitle = 'usercurrentsettings') { global $DB, $CFG, $USER, $SITE; if ($courseid != $SITE->id) { if (!empty($this->page->course->id) && $this->page->course->id == $courseid) { $course = $this->page->course; } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT c.*, {$select}\n FROM {course} c\n JOIN {context} ctx ON c.id = ctx.instanceid\n WHERE c.id = :courseid AND ctx.contextlevel = :contextlevel"; $params = array('courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE); $course = $DB->get_record_sql($sql, $params, MUST_EXIST); context_helper::preload_from_record($course); } } else { $course = $SITE; } $coursecontext = context_course::instance($course->id); // Course context $systemcontext = context_system::instance(); $currentuser = $USER->id == $userid; if ($currentuser) { $user = $USER; $usercontext = context_user::instance($user->id); // User context } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT u.*, {$select}\n FROM {user} u\n JOIN {context} ctx ON u.id = ctx.instanceid\n WHERE u.id = :userid AND ctx.contextlevel = :contextlevel"; $params = array('userid' => $userid, 'contextlevel' => CONTEXT_USER); $user = $DB->get_record_sql($sql, $params, IGNORE_MISSING); if (!$user) { return false; } context_helper::preload_from_record($user); // Check that the user can view the profile $usercontext = context_user::instance($user->id); // User context $canviewuser = has_capability('moodle/user:viewdetails', $usercontext); if ($course->id == $SITE->id) { if ($CFG->forceloginforprofiles && !has_coursecontact_role($user->id) && !$canviewuser) { // Reduce possibility of "browsing" userbase at site level // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366) return false; } } else { $canviewusercourse = has_capability('moodle/user:viewdetails', $coursecontext); $userisenrolled = is_enrolled($coursecontext, $user->id); if (!$canviewusercourse && !$canviewuser || !$userisenrolled) { return false; } $canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); if (!$canaccessallgroups && groups_get_course_groupmode($course) == SEPARATEGROUPS) { // If groups are in use, make sure we can see that group return false; } } } $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->page->context)); $key = $gstitle; if ($gstitle != 'usercurrentsettings') { $key .= $userid; } // Add a user setting branch $usersetting = $this->add(get_string($gstitle, 'moodle', $fullname), null, self::TYPE_CONTAINER, null, $key); $usersetting->id = 'usersettings'; if ($this->page->context->contextlevel == CONTEXT_USER && $this->page->context->instanceid == $user->id) { // Automatically start by making it active $usersetting->make_active(); } // Check if the user has been deleted if ($user->deleted) { if (!has_capability('moodle/user:update', $coursecontext)) { // We can't edit the user so just show the user deleted message $usersetting->add(get_string('userdeleted'), null, self::TYPE_SETTING); } else { // We can edit the user so show the user deleted message and link it to the profile if ($course->id == $SITE->id) { $profileurl = new moodle_url('/user/profile.php', array('id' => $user->id)); } else { $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); } $usersetting->add(get_string('userdeleted'), $profileurl, self::TYPE_SETTING); } return true; } $userauthplugin = false; if (!empty($user->auth)) { $userauthplugin = get_auth_plugin($user->auth); } // Add the profile edit link if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if (($currentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update', $systemcontext)) { $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $course->id)); $usersetting->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } else { if (has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user) || $currentuser && has_capability('moodle/user:editownprofile', $systemcontext)) { if ($userauthplugin && $userauthplugin->can_edit_profile()) { $url = $userauthplugin->edit_profile_url(); if (empty($url)) { $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id)); } $usersetting->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } } } } // Change password link if ($userauthplugin && $currentuser && !\core\session\manager::is_loggedinas() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) { $passwordchangeurl = $userauthplugin->change_password_url(); if (empty($passwordchangeurl)) { $passwordchangeurl = new moodle_url('/login/change_password.php', array('id' => $course->id)); } $usersetting->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING); } // View the roles settings if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:manage'), $usercontext)) { $roles = $usersetting->add(get_string('roles'), null, self::TYPE_SETTING); $url = new moodle_url('/admin/roles/usersroles.php', array('userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('thisusersroles', 'role'), $url, self::TYPE_SETTING); $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH); if (!empty($assignableroles)) { $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('assignrolesrelativetothisuser', 'role'), $url, self::TYPE_SETTING); } if (has_capability('moodle/role:review', $usercontext) || count(get_overridable_roles($usercontext, ROLENAME_BOTH)) > 0) { $url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); } $url = new moodle_url('/admin/roles/check.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); } // Portfolio if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) { require_once $CFG->libdir . '/portfoliolib.php'; if (portfolio_has_visible_instances()) { $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING); $url = new moodle_url('/user/portfolio.php', array('courseid' => $course->id)); $portfolio->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING); $url = new moodle_url('/user/portfoliologs.php', array('courseid' => $course->id)); $portfolio->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING); } } $enablemanagetokens = false; if (!empty($CFG->enablerssfeeds)) { $enablemanagetokens = true; } else { if (!is_siteadmin($USER->id) && !empty($CFG->enablewebservices) && has_capability('moodle/webservice:createtoken', context_system::instance())) { $enablemanagetokens = true; } } // Security keys if ($currentuser && $enablemanagetokens) { $url = new moodle_url('/user/managetoken.php', array('sesskey' => sesskey())); $usersetting->add(get_string('securitykeys', 'webservice'), $url, self::TYPE_SETTING); } // Messaging if ($currentuser && has_capability('moodle/user:editownmessageprofile', $systemcontext) || !isguestuser($user) && has_capability('moodle/user:editmessageprofile', $usercontext) && !is_primary_admin($user->id)) { $url = new moodle_url('/message/edit.php', array('id' => $user->id)); $usersetting->add(get_string('messaging', 'message'), $url, self::TYPE_SETTING); } // Blogs if ($currentuser && !empty($CFG->enableblogs)) { $blog = $usersetting->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER, null, 'blogs'); $blog->add(get_string('preferences', 'blog'), new moodle_url('/blog/preferences.php'), navigation_node::TYPE_SETTING); if (!empty($CFG->useexternalblogs) && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', context_system::instance())) { $blog->add(get_string('externalblogs', 'blog'), new moodle_url('/blog/external_blogs.php'), navigation_node::TYPE_SETTING); $blog->add(get_string('addnewexternalblog', 'blog'), new moodle_url('/blog/external_blog_edit.php'), navigation_node::TYPE_SETTING); } } // Badges. if ($currentuser && !empty($CFG->enablebadges)) { $badges = $usersetting->add(get_string('badges'), null, navigation_node::TYPE_CONTAINER, null, 'badges'); $badges->add(get_string('preferences'), new moodle_url('/badges/preferences.php'), navigation_node::TYPE_SETTING); if (!empty($CFG->badges_allowexternalbackpack)) { $badges->add(get_string('backpackdetails', 'badges'), new moodle_url('/badges/mybackpack.php'), navigation_node::TYPE_SETTING); } } // Add reports node. $reporttab = $usersetting->add(get_string('activityreports')); $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); foreach ($reports as $reportfunction) { $reportfunction($reporttab, $user, $course); } $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); if ($anyreport || $course->showreports && $currentuser) { // Add grade hardcoded grade report if necessary. $gradeaccess = false; if (has_capability('moodle/grade:viewall', $coursecontext)) { // Can view all course grades. $gradeaccess = true; } else { if ($course->showgrades) { if ($currentuser && has_capability('moodle/grade:view', $coursecontext)) { // Can view own grades. $gradeaccess = true; } else { if (has_capability('moodle/grade:viewall', $usercontext)) { // Can view grades of this user - parent most probably. $gradeaccess = true; } else { if ($anyreport) { // Can view grades of this user - parent most probably. $gradeaccess = true; } } } } } if ($gradeaccess) { $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $usercontext->instanceid))); } } // Check the number of nodes in the report node... if there are none remove the node $reporttab->trim_if_empty(); // Login as ... if (!$user->deleted and !$currentuser && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) && !is_siteadmin($user->id)) { $url = new moodle_url('/course/loginas.php', array('id' => $course->id, 'user' => $user->id, 'sesskey' => sesskey())); $usersetting->add(get_string('loginas'), $url, self::TYPE_SETTING); } return $usersetting; }
public function login_info($withlinks = null) { global $USER, $CFG, $DB, $SESSION,$OUTPUT; if (during_initial_install()) { return ''; } if (is_null($withlinks)) { $withlinks = empty($this->page->layout_options['nologinlinks']); } $loginpage = ((string)$this->page->url === get_login_url()); $course = $this->page->course; if (\core\session\manager::is_loggedinas()) { $realuser = session_get_realuser(); $fullname = fullname($realuser, true); if ($withlinks) { $loginastitle = get_string('loginas'); $realuserinfo = " <a href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&sesskey=".sesskey()."\""; $realuserinfo .= "title =\"".$loginastitle."\">$fullname</a>"; } else { $realuserinfo = "$fullname"; } } else { $realuserinfo = ''; } $loginurl = get_login_url(); if (empty($course->id)) { // $course->id is not defined during installation return ''; } else if (isloggedin()) { $context = context_course::instance($course->id); $fullname = fullname($USER, true); // Since Moodle 2.0 this link always goes to the public profile page (not the course profile page) if ($withlinks) { $linktitle = get_string('viewprofile'); $username = "******"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\" title=\"$linktitle\">$fullname</a>"; } else { $username = $fullname; } if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) { if ($withlinks) { $username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>"; } else { $username .= " from {$idprovider->name}"; } } if (isguestuser()) { $loggedinas = get_string('loggedinasguest','theme_colms'); $loggout = "<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\" id='logout'>".get_string('logout').'</a>'; if (!$loginpage && $withlinks) { $loggedinas .= " (<a href=\"$loginurl\" style=\"vertical-align:middle;margin:0;\">".get_string('login').'</a>)'; } } else if (is_role_switched($course->id)) { // Has switched roles $rolename = ''; if ($role = $DB->get_record('role', array('id'=>$USER->access['rsw'][$context->path]))) { // $rolename = ': '.role_get_name($role, $context); $rolename = ''; } $loggedinas = get_string('loggedinas', 'theme_colms', $username).$rolename; if ($withlinks) { $url = new moodle_url('/course/switchrole.php', array('id'=>$course->id,'sesskey'=>sesskey(), 'switchrole'=>0, 'returnurl'=>$this->page->url->out_as_local_url(false))); // $loggedinas .= '('.html_writer::tag('a', get_string('switchrolereturn'), array('href'=>$url)).')'; } } else { $loggedinas = $realuserinfo.get_string('loggedinas', 'theme_colms', $username); if ($withlinks) { $loggout = "<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\" id='logout'>".get_string('logout').'</a>'; } } } else { $loggedinas = get_string('loggedinnot', 'theme_colms'); if (!$loginpage && $withlinks) { $loggedinas = "<a href='javascript:void(0)' id='login_button'>".get_string('login').'</a>'; } } // if(isloggedin() || isguestuser()){ // $loggedinas = '<div class="logininfo"><div id="logoutlink">'. $loggedinas .'<a href="javascript:void(0)" id="pop_logout"><img src=' . $OUTPUT->pix_url("down_arrow","theme") .' /></a></div>', array("class"=>"userimg")) . ''.$loggout.'</div>'; // } if(isloggedin() || isguestuser()){ $loggout = "<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\" class='box_log'>".get_string('logout').'</a>'; $loggedinas = '<div class="logininfo"><div class="logoutlink">'. $loggedinas .'</div><div class="box_log">'.$loggout.'</div></div>'; } if (isset($SESSION->justloggedin)) { unset($SESSION->justloggedin); if (!empty($CFG->displayloginfailures)) { if (!isguestuser()) { if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) { $loggedinas .= ' <div class="loginfailures">'; if (empty($count->accounts)) { $loggedinas .= get_string('failedloginattempts', '', $count); } else { $loggedinas .= get_string('failedloginattemptsall', '', $count); } if (file_exists("$CFG->dirroot/report/log/index.php") and has_capability('report/log:view', context_system::instance())) { $loggedinas .= ' (<a href="'.$CFG->wwwroot.'/report/log/index.php'. '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)'; } $loggedinas .= '</div>'; } } } } return $loggedinas; }
/** * Is current $USER logged-in-as somebody else? * @deprecated since 2.6 * @return bool */ function session_is_loggedinas() { debugging('session_is_loggedinas() is deprecated, use \\core\\session\\manager::is_loggedinas() instead', DEBUG_DEVELOPER); return \core\session\manager::is_loggedinas(); }
public function test_log_writing() { global $DB; $this->resetAfterTest(); $this->preventResetByRollback(); // Logging waits till the transaction gets committed. $this->setAdminUser(); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $course1 = $this->getDataGenerator()->create_course(); $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1)); $course2 = $this->getDataGenerator()->create_course(); $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2)); // Test all plugins are disabled by this command. set_config('enabled_stores', '', 'tool_log'); $manager = get_log_manager(true); $stores = $manager->get_readers(); $this->assertCount(0, $stores); // Enable logging plugin. set_config('enabled_stores', 'logstore_standard', 'tool_log'); set_config('buffersize', 0, 'logstore_standard'); set_config('logguests', 1, 'logstore_standard'); $manager = get_log_manager(true); $stores = $manager->get_readers(); $this->assertCount(1, $stores); $this->assertEquals(array('logstore_standard'), array_keys($stores)); /** @var \logstore_standard\log\store $store */ $store = $stores['logstore_standard']; $this->assertInstanceOf('logstore_standard\\log\\store', $store); $this->assertInstanceOf('tool_log\\log\\writer', $store); $this->assertTrue($store->is_logging()); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(0, $logs); $this->setCurrentTimeStart(); $this->setUser(0); $event1 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10))); $event1->trigger(); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(1, $logs); $log1 = reset($logs); unset($log1->id); $log1->other = unserialize($log1->other); $log1 = (array) $log1; $data = $event1->get_data(); $data['origin'] = 'cli'; $data['ip'] = null; $data['realuserid'] = null; $this->assertEquals($data, $log1); $this->setAdminUser(); \core\session\manager::loginas($user1->id, context_system::instance()); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); logstore_standard_restore::hack_executing(1); $event2 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9))); $event2->trigger(); logstore_standard_restore::hack_executing(0); \core\session\manager::init_empty_session(); $this->assertFalse(\core\session\manager::is_loggedinas()); $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC'); $this->assertCount(3, $logs); array_shift($logs); $log2 = array_shift($logs); $this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname); $this->assertSame('cli', $log2->origin); $log3 = array_shift($logs); unset($log3->id); $log3->other = unserialize($log3->other); $log3 = (array) $log3; $data = $event2->get_data(); $data['origin'] = 'restore'; $data['ip'] = null; $data['realuserid'] = 2; $this->assertEquals($data, $log3); // Test table exists. $tablename = $store->get_internal_log_table_name(); $this->assertTrue($DB->get_manager()->table_exists($tablename)); // Test reading. $this->assertSame(3, $store->get_events_select_count('', array())); $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC". $this->assertCount(3, $events); $resev1 = array_shift($events); array_shift($events); $resev2 = array_shift($events); $this->assertEquals($event1->get_data(), $resev1->get_data()); $this->assertEquals($event2->get_data(), $resev2->get_data()); // Test buffering. set_config('buffersize', 3, 'logstore_standard'); $manager = get_log_manager(true); $stores = $manager->get_readers(); /** @var \logstore_standard\log\store $store */ $store = $stores['logstore_standard']; $DB->delete_records('logstore_standard_log'); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $store->flush(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(5, $DB->count_records('logstore_standard_log')); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(8, $DB->count_records('logstore_standard_log')); // Test guest logging setting. set_config('logguests', 0, 'logstore_standard'); set_config('buffersize', 0, 'logstore_standard'); get_log_manager(true); $DB->delete_records('logstore_standard_log'); get_log_manager(true); $this->setUser(null); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $this->setGuestUser(); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(0, $DB->count_records('logstore_standard_log')); $this->setUser($user1); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(1, $DB->count_records('logstore_standard_log')); $this->setUser($user2); \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); $this->assertEquals(2, $DB->count_records('logstore_standard_log')); set_config('enabled_stores', '', 'tool_log'); get_log_manager(true); }
/** * This function checks that the current user is logged in and has the * required privileges * * This function checks that the current user is logged in, and optionally * whether they are allowed to be in a particular course and view a particular * course module. * If they are not logged in, then it redirects them to the site login unless * $autologinguest is set and {@link $CFG}->autologinguests is set to 1 in which * case they are automatically logged in as guests. * If $courseid is given and the user is not enrolled in that course then the * user is redirected to the course enrolment page. * If $cm is given and the course module is hidden and the user is not a teacher * in the course then the user is redirected to the course home page. * * When $cm parameter specified, this function sets page layout to 'module'. * You need to change it manually later if some other layout needed. * * @package core_access * @category access * * @param mixed $courseorid id of the course or course object * @param bool $autologinguest default true * @param object $cm course module object * @param bool $setwantsurltome Define if we want to set $SESSION->wantsurl, defaults to * true. Used to avoid (=false) some scripts (file.php...) to set that variable, * in order to keep redirects working properly. MDL-14495 * @param bool $preventredirect set to true in scripts that can not redirect (CLI, rss feeds, etc.), throws exceptions * @return mixed Void, exit, and die depending on path * @throws coding_exception * @throws require_login_exception */ function require_login($courseorid = null, $autologinguest = true, $cm = null, $setwantsurltome = true, $preventredirect = false) { global $CFG, $SESSION, $USER, $PAGE, $SITE, $DB, $OUTPUT; // Must not redirect when byteserving already started. if (!empty($_SERVER['HTTP_RANGE'])) { $preventredirect = true; } if (AJAX_SCRIPT) { // We cannot redirect for AJAX scripts either. $preventredirect = true; } // Setup global $COURSE, themes, language and locale. if (!empty($courseorid)) { if (is_object($courseorid)) { $course = $courseorid; } else { if ($courseorid == SITEID) { $course = clone $SITE; } else { $course = $DB->get_record('course', array('id' => $courseorid), '*', MUST_EXIST); } } if ($cm) { if ($cm->course != $course->id) { throw new coding_exception('course and cm parameters in require_login() call do not match!!'); } // Make sure we have a $cm from get_fast_modinfo as this contains activity access details. if (!$cm instanceof cm_info) { // Note: nearly all pages call get_fast_modinfo anyway and it does not make any // db queries so this is not really a performance concern, however it is obviously // better if you use get_fast_modinfo to get the cm before calling this. $modinfo = get_fast_modinfo($course); $cm = $modinfo->get_cm($cm->id); } } } else { // Do not touch global $COURSE via $PAGE->set_course(), // the reasons is we need to be able to call require_login() at any time!! $course = $SITE; if ($cm) { throw new coding_exception('cm parameter in require_login() requires valid course parameter!'); } } // If this is an AJAX request and $setwantsurltome is true then we need to override it and set it to false. // Otherwise the AJAX request URL will be set to $SESSION->wantsurl and events such as self enrolment in the future // risk leading the user back to the AJAX request URL. if ($setwantsurltome && defined('AJAX_SCRIPT') && AJAX_SCRIPT) { $setwantsurltome = false; } // Redirect to the login page if session has expired, only with dbsessions enabled (MDL-35029) to maintain current behaviour. if ((!isloggedin() or isguestuser()) && !empty($SESSION->has_timed_out) && !empty($CFG->dbsessions)) { if ($preventredirect) { throw new require_login_session_timeout_exception(); } else { if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } redirect(get_login_url()); } } // If the user is not even logged in yet then make sure they are. if (!isloggedin()) { if ($autologinguest and !empty($CFG->guestloginbutton) and !empty($CFG->autologinguests)) { if (!($guest = get_complete_user_data('id', $CFG->siteguest))) { // Misconfigured site guest, just redirect to login page. redirect(get_login_url()); exit; // Never reached. } $lang = isset($SESSION->lang) ? $SESSION->lang : $CFG->lang; complete_user_login($guest); $USER->autologinguest = true; $SESSION->lang = $lang; } else { // NOTE: $USER->site check was obsoleted by session test cookie, $USER->confirmed test is in login/index.php. if ($preventredirect) { throw new require_login_exception('You are not logged in'); } if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } $referer = get_local_referer(false); if (!empty($referer)) { $SESSION->fromurl = $referer; } // Give auth plugins an opportunity to authenticate or redirect to an external login page $authsequence = get_enabled_auth_plugins(true); // auths, in sequence foreach ($authsequence as $authname) { $authplugin = get_auth_plugin($authname); $authplugin->pre_loginpage_hook(); if (isloggedin()) { break; } } // If we're still not logged in then go to the login page if (!isloggedin()) { redirect(get_login_url()); exit; // Never reached. } } } // Loginas as redirection if needed. if ($course->id != SITEID and \core\session\manager::is_loggedinas()) { if ($USER->loginascontext->contextlevel == CONTEXT_COURSE) { if ($USER->loginascontext->instanceid != $course->id) { print_error('loginasonecourse', '', $CFG->wwwroot . '/course/view.php?id=' . $USER->loginascontext->instanceid); } } } // Check whether the user should be changing password (but only if it is REALLY them). if (get_user_preferences('auth_forcepasswordchange') && !\core\session\manager::is_loggedinas()) { $userauth = get_auth_plugin($USER->auth); if ($userauth->can_change_password() and !$preventredirect) { if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } if ($changeurl = $userauth->change_password_url()) { // Use plugin custom url. redirect($changeurl); } else { // Use moodle internal method. if (empty($CFG->loginhttps)) { redirect($CFG->wwwroot . '/login/change_password.php'); } else { $wwwroot = str_replace('http:', 'https:', $CFG->wwwroot); redirect($wwwroot . '/login/change_password.php'); } } } else { if ($userauth->can_change_password()) { throw new moodle_exception('forcepasswordchangenotice'); } else { throw new moodle_exception('nopasswordchangeforced', 'auth'); } } } // Check that the user account is properly set up. If we can't redirect to // edit their profile, perform just the lax check. It will allow them to // use filepicker on the profile edit page. if ($preventredirect) { $usernotfullysetup = user_not_fully_set_up($USER, false); } else { $usernotfullysetup = user_not_fully_set_up($USER, true); } if ($usernotfullysetup) { if ($preventredirect) { throw new moodle_exception('usernotfullysetup'); } if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } redirect($CFG->wwwroot . '/user/edit.php?id=' . $USER->id . '&course=' . SITEID); } // Make sure the USER has a sesskey set up. Used for CSRF protection. sesskey(); // Do not bother admins with any formalities. if (is_siteadmin()) { // Set the global $COURSE. if ($cm) { $PAGE->set_cm($cm, $course); $PAGE->set_pagelayout('incourse'); } else { if (!empty($courseorid)) { $PAGE->set_course($course); } } // Set accesstime or the user will appear offline which messes up messaging. user_accesstime_log($course->id); return; } // Check that the user has agreed to a site policy if there is one - do not test in case of admins. if (!$USER->policyagreed and !is_siteadmin()) { if (!empty($CFG->sitepolicy) and !isguestuser()) { if ($preventredirect) { throw new moodle_exception('sitepolicynotagreed', 'error', '', $CFG->sitepolicy); } if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } redirect($CFG->wwwroot . '/user/policy.php'); } else { if (!empty($CFG->sitepolicyguest) and isguestuser()) { if ($preventredirect) { throw new moodle_exception('sitepolicynotagreed', 'error', '', $CFG->sitepolicyguest); } if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } redirect($CFG->wwwroot . '/user/policy.php'); } } } // Fetch the system context, the course context, and prefetch its child contexts. $sysctx = context_system::instance(); $coursecontext = context_course::instance($course->id, MUST_EXIST); if ($cm) { $cmcontext = context_module::instance($cm->id, MUST_EXIST); } else { $cmcontext = null; } // If the site is currently under maintenance, then print a message. if (!empty($CFG->maintenance_enabled) and !has_capability('moodle/site:maintenanceaccess', $sysctx)) { if ($preventredirect) { throw new require_login_exception('Maintenance in progress'); } $PAGE->set_context(null); print_maintenance_message(); } // Make sure the course itself is not hidden. if ($course->id == SITEID) { // Frontpage can not be hidden. } else { if (is_role_switched($course->id)) { // When switching roles ignore the hidden flag - user had to be in course to do the switch. } else { if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { // Originally there was also test of parent category visibility, BUT is was very slow in complex queries // involving "my courses" now it is also possible to simply hide all courses user is not enrolled in :-). if ($preventredirect) { throw new require_login_exception('Course is hidden'); } $PAGE->set_context(null); // We need to override the navigation URL as the course won't have been added to the navigation and thus // the navigation will mess up when trying to find it. navigation_node::override_active_url(new moodle_url('/')); notice(get_string('coursehidden'), $CFG->wwwroot . '/'); } } } // Is the user enrolled? if ($course->id == SITEID) { // Everybody is enrolled on the frontpage. } else { if (\core\session\manager::is_loggedinas()) { // Make sure the REAL person can access this course first. $realuser = \core\session\manager::get_realuser(); if (!is_enrolled($coursecontext, $realuser->id, '', true) and !is_viewing($coursecontext, $realuser->id) and !is_siteadmin($realuser->id)) { if ($preventredirect) { throw new require_login_exception('Invalid course login-as access'); } $PAGE->set_context(null); echo $OUTPUT->header(); notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot . '/'); } } $access = false; if (is_role_switched($course->id)) { // Ok, user had to be inside this course before the switch. $access = true; } else { if (is_viewing($coursecontext, $USER)) { // Ok, no need to mess with enrol. $access = true; } else { if (isset($USER->enrol['enrolled'][$course->id])) { if ($USER->enrol['enrolled'][$course->id] > time()) { $access = true; if (isset($USER->enrol['tempguest'][$course->id])) { unset($USER->enrol['tempguest'][$course->id]); remove_temp_course_roles($coursecontext); } } else { // Expired. unset($USER->enrol['enrolled'][$course->id]); } } if (isset($USER->enrol['tempguest'][$course->id])) { if ($USER->enrol['tempguest'][$course->id] == 0) { $access = true; } else { if ($USER->enrol['tempguest'][$course->id] > time()) { $access = true; } else { // Expired. unset($USER->enrol['tempguest'][$course->id]); remove_temp_course_roles($coursecontext); } } } if (!$access) { // Cache not ok. $until = enrol_get_enrolment_end($coursecontext->instanceid, $USER->id); if ($until !== false) { // Active participants may always access, a timestamp in the future, 0 (always) or false. if ($until == 0) { $until = ENROL_MAX_TIMESTAMP; } $USER->enrol['enrolled'][$course->id] = $until; $access = true; } else { $params = array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED); $instances = $DB->get_records('enrol', $params, 'sortorder, id ASC'); $enrols = enrol_get_plugins(true); // First ask all enabled enrol instances in course if they want to auto enrol user. foreach ($instances as $instance) { if (!isset($enrols[$instance->enrol])) { continue; } // Get a duration for the enrolment, a timestamp in the future, 0 (always) or false. $until = $enrols[$instance->enrol]->try_autoenrol($instance); if ($until !== false) { if ($until == 0) { $until = ENROL_MAX_TIMESTAMP; } $USER->enrol['enrolled'][$course->id] = $until; $access = true; break; } } // If not enrolled yet try to gain temporary guest access. if (!$access) { foreach ($instances as $instance) { if (!isset($enrols[$instance->enrol])) { continue; } // Get a duration for the guest access, a timestamp in the future or false. $until = $enrols[$instance->enrol]->try_guestaccess($instance); if ($until !== false and $until > time()) { $USER->enrol['tempguest'][$course->id] = $until; $access = true; break; } } } } } } } if (!$access) { if ($preventredirect) { throw new require_login_exception('Not enrolled'); } if ($setwantsurltome) { $SESSION->wantsurl = qualified_me(); } redirect($CFG->wwwroot . '/enrol/index.php?id=' . $course->id); } } // Check visibility of activity to current user; includes visible flag, conditional availability, etc. if ($cm && !$cm->uservisible) { if ($preventredirect) { throw new require_login_exception('Activity is hidden'); } if ($course->id != SITEID) { $url = new moodle_url('/course/view.php', array('id' => $course->id)); } else { $url = new moodle_url('/'); } redirect($url, get_string('activityiscurrentlyhidden')); } // Set the global $COURSE. if ($cm) { $PAGE->set_cm($cm, $course); $PAGE->set_pagelayout('incourse'); } else { if (!empty($courseorid)) { $PAGE->set_course($course); } } // Finally access granted, update lastaccess times. user_accesstime_log($course->id); }
public function test_is_loggedinas() { $this->resetAfterTest(); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $this->assertFalse(\core\session\manager::is_loggedinas()); $this->setUser($user1); \core\session\manager::loginas($user2->id, context_system::instance()); $this->assertTrue(\core\session\manager::is_loggedinas()); }
} } } } // Apache log integration. In apache conf file one can use ${MOODULEUSER}n in // LogFormat to get the current logged in username in moodle. if ($USER && function_exists('apache_note') && !empty($CFG->apacheloguser) && isset($USER->username)) { $apachelog_userid = $USER->id; $apachelog_username = clean_filename($USER->username); $apachelog_name = ''; if (isset($USER->firstname)) { // We can assume both will be set // - even if to empty. $apachelog_name = clean_filename($USER->firstname . " " . $USER->lastname); } if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $apachelog_username = clean_filename($realuser->username . " as " . $apachelog_username); $apachelog_name = clean_filename($realuser->firstname . " " . $realuser->lastname . " as " . $apachelog_name); $apachelog_userid = clean_filename($realuser->id . " as " . $apachelog_userid); } switch ($CFG->apacheloguser) { case 3: $logname = $apachelog_username; break; case 2: $logname = $apachelog_name; break; case 1: default: $logname = $apachelog_userid;
/** * Store user last access times - called when use enters a course or site * * @package core * @category log * @global stdClass $USER * @global stdClass $CFG * @global moodle_database $DB * @uses LASTACCESS_UPDATE_SECS * @uses SITEID * @param int $courseid empty courseid means site * @return void */ function user_accesstime_log($courseid = 0) { global $USER, $CFG, $DB; if (!isloggedin() or \core\session\manager::is_loggedinas()) { // no access tracking return; } if (isguestuser()) { // Do not update guest access times/ips for performance. return; } if (empty($courseid)) { $courseid = SITEID; } $timenow = time(); /// Store site lastaccess time for the current user if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) { /// Update $USER->lastaccess for next checks $USER->lastaccess = $timenow; $last = new stdClass(); $last->id = $USER->id; $last->lastip = getremoteaddr(); $last->lastaccess = $timenow; $DB->update_record_raw('user', $last); } if ($courseid == SITEID) { /// no user_lastaccess for frontpage return; } /// Store course lastaccess times for the current user if (empty($USER->currentcourseaccess[$courseid]) or $timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS) { $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $USER->id, 'courseid' => $courseid)); if ($lastaccess === false) { // Update course lastaccess for next checks $USER->currentcourseaccess[$courseid] = $timenow; $last = new stdClass(); $last->userid = $USER->id; $last->courseid = $courseid; $last->timeaccess = $timenow; $DB->insert_record_raw('user_lastaccess', $last, false); } else { if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) { // no need to update now, it was updated recently in concurrent login ;-) } else { // Update course lastaccess for next checks $USER->currentcourseaccess[$courseid] = $timenow; $DB->set_field('user_lastaccess', 'timeaccess', $timenow, array('userid' => $USER->id, 'courseid' => $courseid)); } } } }
/** * Legacy add_to_log() code. * * @param int $courseid The course id * @param string $module The module name e.g. forum, journal, resource, course, user etc * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify. * @param string $url The file and parameters used to see the results of the action * @param string $info Additional description information * @param int $cm The course_module->id if there is one * @param int|\stdClass $user If log regards $user other than $USER */ public function legacy_add_to_log($courseid, $module, $action, $url, $info, $cm, $user) { // Note that this function intentionally does not follow the normal Moodle DB access idioms. // This is for a good reason: it is the most frequently used DB update function, // so it has been optimised for speed. global $DB, $CFG, $USER; if (!$this->is_logging()) { return; } if ($cm === '' || is_null($cm)) { // Postgres won't translate empty string to its default. $cm = 0; } if ($user) { $userid = $user; } else { if (\core\session\manager::is_loggedinas()) { // Don't log. return; } $userid = empty($USER->id) ? '0' : $USER->id; } if (isset($CFG->logguests) and !$CFG->logguests) { if (!$userid or isguestuser($userid)) { return; } } $remoteaddr = getremoteaddr(); $timenow = time(); if (!empty($url)) { // Could break doing html_entity_decode on an empty var. $url = html_entity_decode($url, ENT_QUOTES, 'UTF-8'); } else { $url = ''; } // Restrict length of log lines to the space actually available in the // database so that it doesn't cause a DB error. Log a warning so that // developers can avoid doing things which are likely to cause this on a // routine basis. if (\core_text::strlen($action) > 40) { $action = \core_text::substr($action, 0, 37) . '...'; debugging('Warning: logged very long action', DEBUG_DEVELOPER); } if (!empty($info) && \core_text::strlen($info) > 255) { $info = \core_text::substr($info, 0, 252) . '...'; debugging('Warning: logged very long info', DEBUG_DEVELOPER); } // If the 100 field size is changed, also need to alter print_log in course/lib.php. if (!empty($url) && \core_text::strlen($url) > 100) { $url = \core_text::substr($url, 0, 97) . '...'; debugging('Warning: logged very long URL', DEBUG_DEVELOPER); } if (defined('MDL_PERFDB')) { global $PERF; $PERF->logwrites++; } $log = array('time' => $timenow, 'userid' => $userid, 'course' => $courseid, 'ip' => $remoteaddr, 'module' => $module, 'cmid' => $cm, 'action' => $action, 'url' => $url, 'info' => $info); try { $DB->insert_record_raw('log', $log, false); } catch (\dml_exception $e) { debugging('Error: Could not insert a new entry to the Moodle log. ' . $e->errorcode, DEBUG_ALL); // MDL-11893, alert $CFG->supportemail if insert into log failed. if ($CFG->supportemail and empty($CFG->noemailever)) { // Function email_to_user is not usable because email_to_user tries to write to the logs table, // and this will get caught in an infinite loop, if disk is full. $site = get_site(); $subject = 'Insert into log failed at your moodle site ' . $site->fullname; $message = "Insert into log table failed at " . date('l dS \\of F Y h:i:s A') . ".\n It is possible that your disk is full.\n\n"; $message .= "The failed query parameters are:\n\n" . var_export($log, true); $lasttime = get_config('admin', 'lastloginserterrormail'); if (empty($lasttime) || time() - $lasttime > 60 * 60 * 24) { // Limit to 1 email per day. // Using email directly rather than messaging as they may not be able to log in to access a message. mail($CFG->supportemail, $subject, $message); set_config('lastloginserterrormail', time(), 'admin'); } } } }
/** * Starts an RPC jump session and returns the jump redirect URL. * * @param int $mnethostid id of the mnet host to jump to * @param string $wantsurl url to redirect to after the jump (usually on remote system) * @param boolean $wantsurlbackhere defaults to false, means that the remote system should bounce us back here * rather than somewhere inside *its* wwwroot */ function start_jump_session($mnethostid, $wantsurl, $wantsurlbackhere = false) { global $CFG, $USER, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; if (\core\session\manager::is_loggedinas()) { print_error('notpermittedtojumpas', 'mnet'); } // check remote login permissions if (!has_capability('moodle/site:mnetlogintoremote', context_system::instance()) or is_mnet_remote_user($USER) or isguestuser() or !isloggedin()) { print_error('notpermittedtojump', 'mnet'); } // check for SSO publish permission first if ($this->has_service($mnethostid, 'sso_sp') == false) { print_error('hostnotconfiguredforsso', 'mnet'); } // set RPC timeout to 30 seconds if not configured if (empty($this->config->rpc_negotiation_timeout)) { $this->config->rpc_negotiation_timeout = 30; set_config('rpc_negotiation_timeout', '30', 'auth_mnet'); } // get the host info $mnet_peer = new mnet_peer(); $mnet_peer->set_id($mnethostid); // set up the session $mnet_session = $DB->get_record('mnet_session', array('userid' => $USER->id, 'mnethostid' => $mnethostid, 'useragent' => sha1($_SERVER['HTTP_USER_AGENT']))); if ($mnet_session == false) { $mnet_session = new stdClass(); $mnet_session->mnethostid = $mnethostid; $mnet_session->userid = $USER->id; $mnet_session->username = $USER->username; $mnet_session->useragent = sha1($_SERVER['HTTP_USER_AGENT']); $mnet_session->token = $this->generate_token(); $mnet_session->confirm_timeout = time() + $this->config->rpc_negotiation_timeout; $mnet_session->expires = time() + (int) ini_get('session.gc_maxlifetime'); $mnet_session->session_id = session_id(); $mnet_session->id = $DB->insert_record('mnet_session', $mnet_session); } else { $mnet_session->useragent = sha1($_SERVER['HTTP_USER_AGENT']); $mnet_session->token = $this->generate_token(); $mnet_session->confirm_timeout = time() + $this->config->rpc_negotiation_timeout; $mnet_session->expires = time() + (int) ini_get('session.gc_maxlifetime'); $mnet_session->session_id = session_id(); $DB->update_record('mnet_session', $mnet_session); } // construct the redirection URL //$transport = mnet_get_protocol($mnet_peer->transport); $wantsurl = urlencode($wantsurl); $url = "{$mnet_peer->wwwroot}{$mnet_peer->application->sso_land_url}?token={$mnet_session->token}&idp={$this->mnet->wwwroot}&wantsurl={$wantsurl}"; if ($wantsurlbackhere) { $url .= '&remoteurl=1'; } return $url; }
/** * This function gets called by {@link settings_navigation::load_user_settings()} and actually works out * what can be shown/done * * @param int $courseid The current course' id * @param int $userid The user id to load for * @param string $gstitle The string to pass to get_string for the branch title * @return navigation_node|false */ protected function generate_user_settings($courseid, $userid, $gstitle = 'usercurrentsettings') { global $DB, $CFG, $USER, $SITE; if ($courseid != $SITE->id) { if (!empty($this->page->course->id) && $this->page->course->id == $courseid) { $course = $this->page->course; } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT c.*, {$select}\n FROM {course} c\n JOIN {context} ctx ON c.id = ctx.instanceid\n WHERE c.id = :courseid AND ctx.contextlevel = :contextlevel"; $params = array('courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE); $course = $DB->get_record_sql($sql, $params, MUST_EXIST); context_helper::preload_from_record($course); } } else { $course = $SITE; } $coursecontext = context_course::instance($course->id); // Course context $systemcontext = context_system::instance(); $currentuser = $USER->id == $userid; if ($currentuser) { $user = $USER; $usercontext = context_user::instance($user->id); // User context } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT u.*, {$select}\n FROM {user} u\n JOIN {context} ctx ON u.id = ctx.instanceid\n WHERE u.id = :userid AND ctx.contextlevel = :contextlevel"; $params = array('userid' => $userid, 'contextlevel' => CONTEXT_USER); $user = $DB->get_record_sql($sql, $params, IGNORE_MISSING); if (!$user) { return false; } context_helper::preload_from_record($user); // Check that the user can view the profile $usercontext = context_user::instance($user->id); // User context $canviewuser = has_capability('moodle/user:viewdetails', $usercontext); if ($course->id == $SITE->id) { if ($CFG->forceloginforprofiles && !has_coursecontact_role($user->id) && !$canviewuser) { // Reduce possibility of "browsing" userbase at site level // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366) return false; } } else { $canviewusercourse = has_capability('moodle/user:viewdetails', $coursecontext); $userisenrolled = is_enrolled($coursecontext, $user->id, '', true); if (!$canviewusercourse && !$canviewuser || !$userisenrolled) { return false; } $canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); if (!$canaccessallgroups && groups_get_course_groupmode($course) == SEPARATEGROUPS && !$canviewuser) { // If groups are in use, make sure we can see that group (MDL-45874). That does not apply to parents. if ($courseid == $this->page->course->id) { $mygroups = get_fast_modinfo($this->page->course)->groups; } else { $mygroups = groups_get_user_groups($courseid); } $usergroups = groups_get_user_groups($courseid, $userid); if (!array_intersect_key($mygroups[0], $usergroups[0])) { return false; } } } } $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->page->context)); $key = $gstitle; $prefurl = new moodle_url('/user/preferences.php'); if ($gstitle != 'usercurrentsettings') { $key .= $userid; $prefurl->param('userid', $userid); } // Add a user setting branch. if ($gstitle == 'usercurrentsettings') { $dashboard = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_CONTAINER, null, 'dashboard'); // This should be set to false as we don't want to show this to the user. It's only for generating the correct // breadcrumb. $dashboard->display = false; if (get_home_page() == HOMEPAGE_MY) { $dashboard->mainnavonly = true; } $iscurrentuser = $user->id == $USER->id; $baseargs = array('id' => $user->id); if ($course->id != $SITE->id && !$iscurrentuser) { $baseargs['course'] = $course->id; $issitecourse = false; } else { // Load all categories and get the context for the system. $issitecourse = true; } // Add the user profile to the dashboard. $profilenode = $dashboard->add(get_string('profile'), new moodle_url('/user/profile.php', array('id' => $user->id)), self::TYPE_SETTING, null, 'myprofile'); if (!empty($CFG->navadduserpostslinks)) { // Add nodes for forum posts and discussions if the user can view either or both // There are no capability checks here as the content of the page is based // purely on the forums the current user has access too. $forumtab = $profilenode->add(get_string('forumposts', 'forum')); $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs), null, 'myposts'); $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode' => 'discussions'))), null, 'mydiscussions'); } // Add blog nodes. if (!empty($CFG->enableblogs)) { if (!$this->cache->cached('userblogoptions' . $user->id)) { require_once $CFG->dirroot . '/blog/lib.php'; // Get all options for the user. $options = blog_get_options_for_user($user); $this->cache->set('userblogoptions' . $user->id, $options); } else { $options = $this->cache->{'userblogoptions' . $user->id}; } if (count($options) > 0) { $blogs = $profilenode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); foreach ($options as $type => $option) { if ($type == "rss") { $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); } else { $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, 'blog' . $type); } } } } // Add the messages link. // It is context based so can appear in the user's profile and in course participants information. 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); $dashboard->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); } // Add the "My private files" link. // This link doesn't have a unique display for course context so only display it under the user's profile. if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { $url = new moodle_url('/user/files.php'); $dashboard->add(get_string('privatefiles'), $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 != SITEID) { $url->param('course', $coursecontext->instanceid); } $profilenode->add(get_string('notes', 'notes'), $url); } // Show the grades node. if ($issitecourse && $iscurrentuser || has_capability('moodle/user:viewdetails', $usercontext)) { require_once $CFG->dirroot . '/user/lib.php'; // Set the grades node to link to the "Grades" page. if ($course->id == SITEID) { $url = user_mygrades_url($user->id, $course->id); } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version). $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id)); } $dashboard->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'mygrades'); } // Let plugins hook into user navigation. $pluginsfunction = get_plugins_with_function('extend_navigation_user', 'lib.php'); foreach ($pluginsfunction as $plugintype => $plugins) { if ($plugintype != 'report') { foreach ($plugins as $pluginfunction) { $pluginfunction($profilenode, $user, $usercontext, $course, $coursecontext); } } } $usersetting = navigation_node::create(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); $dashboard->add_node($usersetting); } else { $usersetting = $this->add(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); $usersetting->display = false; } $usersetting->id = 'usersettings'; // Check if the user has been deleted. if ($user->deleted) { if (!has_capability('moodle/user:update', $coursecontext)) { // We can't edit the user so just show the user deleted message. $usersetting->add(get_string('userdeleted'), null, self::TYPE_SETTING); } else { // We can edit the user so show the user deleted message and link it to the profile. if ($course->id == $SITE->id) { $profileurl = new moodle_url('/user/profile.php', array('id' => $user->id)); } else { $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); } $usersetting->add(get_string('userdeleted'), $profileurl, self::TYPE_SETTING); } return true; } $userauthplugin = false; if (!empty($user->auth)) { $userauthplugin = get_auth_plugin($user->auth); } $useraccount = $usersetting->add(get_string('useraccount'), null, self::TYPE_CONTAINER, null, 'useraccount'); // Add the profile edit link. if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if (($currentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update', $systemcontext)) { $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } else { if (has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user) || $currentuser && has_capability('moodle/user:editownprofile', $systemcontext)) { if ($userauthplugin && $userauthplugin->can_edit_profile()) { $url = $userauthplugin->edit_profile_url(); if (empty($url)) { $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id)); } $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } } } } // Change password link. if ($userauthplugin && $currentuser && !\core\session\manager::is_loggedinas() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) { $passwordchangeurl = $userauthplugin->change_password_url(); if (empty($passwordchangeurl)) { $passwordchangeurl = new moodle_url('/login/change_password.php', array('id' => $course->id)); } $useraccount->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING, null, 'changepassword'); } if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/language.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('preferredlanguage'), $url, self::TYPE_SETTING, null, 'preferredlanguage'); } } $pluginmanager = core_plugin_manager::instance(); $enabled = $pluginmanager->get_enabled_plugins('mod'); if (isset($enabled['forum']) && isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/forum.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('forumpreferences'), $url, self::TYPE_SETTING); } } $editors = editors_get_enabled(); if (count($editors) > 1) { if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/editor.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('editorpreferences'), $url, self::TYPE_SETTING); } } } // Add "Course preferences" link. if (isloggedin() && !isguestuser($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/course.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('coursepreferences'), $url, self::TYPE_SETTING, null, 'coursepreferences'); } } // View the roles settings. if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:manage'), $usercontext)) { $roles = $usersetting->add(get_string('roles'), null, self::TYPE_SETTING); $url = new moodle_url('/admin/roles/usersroles.php', array('userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('thisusersroles', 'role'), $url, self::TYPE_SETTING); $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH); if (!empty($assignableroles)) { $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('assignrolesrelativetothisuser', 'role'), $url, self::TYPE_SETTING); } if (has_capability('moodle/role:review', $usercontext) || count(get_overridable_roles($usercontext, ROLENAME_BOTH)) > 0) { $url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); } $url = new moodle_url('/admin/roles/check.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); } // Repositories. 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) { $repositories = $usersetting->add(get_string('repositories', 'repository'), null, self::TYPE_SETTING); $repositories->add(get_string('manageinstances', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); } // Portfolio. if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) { require_once $CFG->libdir . '/portfoliolib.php'; if (portfolio_has_visible_instances()) { $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING); $url = new moodle_url('/user/portfolio.php', array('courseid' => $course->id)); $portfolio->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING); $url = new moodle_url('/user/portfoliologs.php', array('courseid' => $course->id)); $portfolio->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING); } } $enablemanagetokens = false; if (!empty($CFG->enablerssfeeds)) { $enablemanagetokens = true; } else { if (!is_siteadmin($USER->id) && !empty($CFG->enablewebservices) && has_capability('moodle/webservice:createtoken', context_system::instance())) { $enablemanagetokens = true; } } // Security keys. if ($currentuser && $enablemanagetokens) { $url = new moodle_url('/user/managetoken.php', array('sesskey' => sesskey())); $useraccount->add(get_string('securitykeys', 'webservice'), $url, self::TYPE_SETTING); } // Messaging. if ($currentuser && has_capability('moodle/user:editownmessageprofile', $systemcontext) || !isguestuser($user) && has_capability('moodle/user:editmessageprofile', $usercontext) && !is_primary_admin($user->id)) { $url = new moodle_url('/message/edit.php', array('id' => $user->id)); $useraccount->add(get_string('messaging', 'message'), $url, self::TYPE_SETTING); } // Blogs. if ($currentuser && !empty($CFG->enableblogs)) { $blog = $usersetting->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER, null, 'blogs'); if (has_capability('moodle/blog:view', $systemcontext)) { $blog->add(get_string('preferences', 'blog'), new moodle_url('/blog/preferences.php'), navigation_node::TYPE_SETTING); } if (!empty($CFG->useexternalblogs) && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', $systemcontext)) { $blog->add(get_string('externalblogs', 'blog'), new moodle_url('/blog/external_blogs.php'), navigation_node::TYPE_SETTING); $blog->add(get_string('addnewexternalblog', 'blog'), new moodle_url('/blog/external_blog_edit.php'), navigation_node::TYPE_SETTING); } // Remove the blog node if empty. $blog->trim_if_empty(); } // Badges. if ($currentuser && !empty($CFG->enablebadges)) { $badges = $usersetting->add(get_string('badges'), null, navigation_node::TYPE_CONTAINER, null, 'badges'); if (has_capability('moodle/badges:manageownbadges', $usercontext)) { $url = new moodle_url('/badges/mybadges.php'); $badges->add(get_string('managebadges', 'badges'), $url, self::TYPE_SETTING); } $badges->add(get_string('preferences', 'badges'), new moodle_url('/badges/preferences.php'), navigation_node::TYPE_SETTING); if (!empty($CFG->badges_allowexternalbackpack)) { $badges->add(get_string('backpackdetails', 'badges'), new moodle_url('/badges/mybackpack.php'), navigation_node::TYPE_SETTING); } } // Let plugins hook into user settings navigation. $pluginsfunction = get_plugins_with_function('extend_navigation_user_settings', 'lib.php'); foreach ($pluginsfunction as $plugintype => $plugins) { foreach ($plugins as $pluginfunction) { $pluginfunction($usersetting, $user, $usercontext, $course, $coursecontext); } } return $usersetting; }
/** * Defines core nodes for my profile navigation tree. * * @param \core_user\output\myprofile\tree $tree Tree object * @param stdClass $user user object * @param bool $iscurrentuser is the user viewing profile, current user ? * @param stdClass $course course object * * @return bool */ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { global $CFG, $USER, $DB; $usercontext = context_user::instance($user->id, MUST_EXIST); $systemcontext = context_system::instance(); $context = !empty($course) ? context_course::instance($course->id) : $systemcontext; $courseid = !empty($course) ? $course->id : SITEID; $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails')); $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'), 'contact'); $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'), 'coursedetails'); $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous'); $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'reports'); $loginactivitycategory = new core_user\output\myprofile\category('loginactivity', get_string('loginactivity'), 'administration'); // Add categories. $tree->add_category($contactcategory); $tree->add_category($coursedetailscategory); $tree->add_category($miscategory); $tree->add_category($reportcategory); $tree->add_category($admincategory); $tree->add_category($loginactivitycategory); // Add core nodes. // Full profile node. if (!empty($course)) { if (empty($CFG->forceloginforprofiles) || $iscurrentuser || has_capability('moodle/user:viewdetails', context_user::instance($user->id)) || has_coursecontact_role($user->id)) { $url = new moodle_url('/user/profile.php', array('id' => $user->id)); $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url); $tree->add_node($node); } } // Edit profile. if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update', $systemcontext)) { $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid)); $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url); $tree->add_node($node); } else { if (has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user) || $iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext)) { $userauthplugin = false; if (!empty($user->auth)) { $userauthplugin = get_auth_plugin($user->auth); } if ($userauthplugin && $userauthplugin->can_edit_profile()) { $url = $userauthplugin->edit_profile_url(); if (empty($url)) { if (empty($course)) { $url = new moodle_url('/user/edit.php', array('userid' => $user->id)); } else { $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id)); } } $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url); $tree->add_node($node); } } } } // Preference page. Only visible by administrators. if (is_siteadmin()) { $url = new moodle_url('/user/preferences.php', array('userid' => $user->id)); $title = $iscurrentuser ? get_string('mypreferences') : get_string('userspreferences', 'moodle', fullname($user)); $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url); $tree->add_node($node); } // Login as ... if (!$user->deleted && !$iscurrentuser && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) { $url = new moodle_url('/course/loginas.php', array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey())); $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url); $tree->add_node($node); } // Contact details. if (has_capability('moodle/user:viewhiddendetails', $usercontext)) { $hiddenfields = array(); } else { $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); } if (has_capability('moodle/site:viewuseridentity', $context)) { $identityfields = array_flip(explode(',', $CFG->showuseridentity)); } else { $identityfields = array(); } if (is_mnet_remote_user($user)) { $sql = "SELECT h.id, h.name, h.wwwroot,\n a.name as application, a.display_name\n FROM {mnet_host} h, {mnet_application} a\n WHERE h.id = ? AND h.applicationid = a.id"; $remotehost = $DB->get_record_sql($sql, array($user->mnethostid)); $remoteuser = new stdclass(); $remoteuser->remotetype = $remotehost->display_name; $hostinfo = new stdclass(); $hostinfo->remotename = $remotehost->name; $hostinfo->remoteurl = $remotehost->wwwroot; $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null, get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo'); $tree->add_node($node); } if (isset($identityfields['email']) and ($iscurrentuser or $user->maildisplay == 1 or has_capability('moodle/course:useremail', $usercontext) or $user->maildisplay == 2 and enrol_sharing_course($user, $USER))) { $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null, obfuscate_mailto($user->email, '')); $tree->add_node($node); } if (!isset($hiddenfields['country']) && $user->country) { $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null, get_string($user->country, 'countries')); $tree->add_node($node); } if (!isset($hiddenfields['city']) && $user->city) { $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city); $tree->add_node($node); } if (isset($identityfields['address']) && $user->address) { $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address); $tree->add_node($node); } if (isset($identityfields['phone1']) && $user->phone1) { $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone'), null, null, $user->phone1); $tree->add_node($node); } if (isset($identityfields['phone2']) && $user->phone2) { $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2); $tree->add_node($node); } if (isset($identityfields['institution']) && $user->institution) { $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null, $user->institution); $tree->add_node($node); } if (isset($identityfields['department']) && $user->department) { $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null, $user->institution); $tree->add_node($node); } if (isset($identityfields['idnumber']) && $user->idnumber) { $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null, $user->institution); $tree->add_node($node); } if ($user->url && !isset($hiddenfields['webpage'])) { $url = $user->url; if (strpos($user->url, '://') === false) { $url = 'http://' . $url; } $webpageurl = new moodle_url($url); $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, null, html_writer::link($url, $webpageurl)); $tree->add_node($node); } // Printing tagged interests. We want this only for full profile. if (!empty($CFG->usetags) && empty($course)) { if ($interests = tag_get_tags_csv('user', $user->id)) { $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests); $tree->add_node($node); } } if (!isset($hiddenfields['mycourses'])) { $showallcourses = optional_param('showallcourses', 0, PARAM_INT); if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) { $shown = 0; $courselisting = html_writer::start_tag('ul'); foreach ($mycourses as $mycourse) { if ($mycourse->category) { context_helper::preload_from_record($mycourse); $ccontext = context_course::instance($mycourse->id); if (!isset($course) || $mycourse->id != $course->id) { $linkattributes = null; if ($mycourse->visible == 0) { if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) { continue; } $linkattributes['class'] = 'dimmed'; } $params = array('id' => $user->id, 'course' => $mycourse->id); if ($showallcourses) { $params['showallcourses'] = 1; } $url = new moodle_url('/user/view.php', $params); $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false), $linkattributes)); } else { $courselisting .= html_writer::tag('li', $course->fullname); } } $shown++; if (!$showallcourses && $shown == $CFG->navcourselimit) { $url = null; if (isset($course)) { $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1)); } else { $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1)); } $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'), array('title' => get_string('viewmore')))); break; } } $courselisting .= html_writer::end_tag('ul'); if (!empty($mycourses)) { // Add this node only if there are courses to display. $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles', get_string('courseprofiles'), null, null, rtrim($courselisting, ', ')); $tree->add_node($node); } } } if (!empty($course)) { // Show roles in this course. if ($rolestring = get_user_roles_in_course($user->id, $course->id)) { $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring); $tree->add_node($node); } // Show groups this user is in. if (!isset($hiddenfields['groups']) && !empty($course)) { $accessallgroups = has_capability('moodle/site:accessallgroups', $context); if ($usergroups = groups_get_all_groups($course->id, $user->id)) { $groupstr = ''; foreach ($usergroups as $group) { if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) { if (!groups_is_member($group->id, $user->id)) { continue; } } if ($course->groupmode != NOGROUPS) { $groupstr .= ' <a href="' . $CFG->wwwroot . '/user/index.php?id=' . $course->id . '&group=' . $group->id . '">' . format_string($group->name) . '</a>,'; } else { // The user/index.php shows groups only when course in group mode. $groupstr .= ' ' . format_string($group->name); } } if ($groupstr !== '') { $node = new core_user\output\myprofile\node('coursedetails', 'groups', get_string('group'), null, null, rtrim($groupstr, ', ')); $tree->add_node($node); } } } if (!isset($hiddenfields['suspended'])) { if ($user->suspended) { $node = new core_user\output\myprofile\node('coursedetails', 'suspended', null, null, null, get_string('suspended', 'auth')); $tree->add_node($node); } } echo html_writer::end_tag('dl'); } if ($user->icq && !isset($hiddenfields['icqnumber'])) { $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq)); $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5')); $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status'))); $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null, html_writer::link($imurl, s($user->icq) . $statusicon)); $tree->add_node($node); } if ($user->skype && !isset($hiddenfields['skypeid'])) { $imurl = 'skype:' . urlencode($user->skype) . '?call'; $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/' . urlencode($user->skype)); if (is_https()) { // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233. $statusicon = ''; } else { $statusicon = html_writer::empty_tag('img', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status'))); } $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null, html_writer::link($imurl, s($user->skype) . $statusicon)); $tree->add_node($node); } if ($user->yahoo && !isset($hiddenfields['yahooid'])) { $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg')); $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0')); $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status'))); $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null, html_writer::link($imurl, s($user->yahoo) . $statusicon)); $tree->add_node($node); } if ($user->aim && !isset($hiddenfields['aimid'])) { $imurl = 'aim:goim?screenname=' . urlencode($user->aim); $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null, html_writer::link($imurl, s($user->aim))); $tree->add_node($node); } if ($user->msn && !isset($hiddenfields['msnid'])) { $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null, s($user->msn)); $tree->add_node($node); } if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) { foreach ($categories as $category) { if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) { foreach ($fields as $field) { require_once $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php'; $newfield = 'profile_field_' . $field->datatype; $formfield = new $newfield($field->id, $user->id); if ($formfield->is_visible() and !$formfield->is_empty()) { $node = new core_user\output\myprofile\node('contact', $formfield->field->shortname, format_string($formfield->field->name), null, null, $formfield->display_data()); $tree->add_node($node); } } } } } // First access. (Why only for sites ?) if (!isset($hiddenfields['firstaccess']) && empty($course)) { if ($user->firstaccess) { $datestring = userdate($user->firstaccess) . " (" . format_time(time() - $user->firstaccess) . ")"; } else { $datestring = get_string("never"); } $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null, $datestring); $tree->add_node($node); } // Last access. if (!isset($hiddenfields['lastaccess'])) { if (empty($course)) { $string = get_string('lastsiteaccess'); if ($user->lastaccess) { $datestring = userdate($user->lastaccess) . " (" . format_time(time() - $user->lastaccess) . ")"; } else { $datestring = get_string("never"); } } else { $string = get_string('lastcourseaccess'); if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) { $datestring = userdate($lastaccess->timeaccess) . " (" . format_time(time() - $lastaccess->timeaccess) . ")"; } else { $datestring = get_string("never"); } } $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null, $datestring); $tree->add_node($node); } // Last ip. if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) { if ($user->lastip) { $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id)); $ipstring = html_writer::link($iplookupurl, $user->lastip); } else { $ipstring = get_string("none"); } $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null, $ipstring); $tree->add_node($node); } }
} $row->cells[1]->text .= $OUTPUT->container_end(); $row->cells[2] = new html_table_cell(); $row->cells[2]->attributes['class'] = 'links'; $row->cells[2]->text = ''; $links = array(); if ($CFG->enableblogs && ($CFG->bloglevel != BLOG_USER_LEVEL || $USER->id == $user->id)) { $links[] = html_writer::link(new moodle_url('/blog/index.php?userid=' . $user->id), get_string('blogs', 'blog')); } if (!empty($CFG->enablenotes) and has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context)) { $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id . '&user='******'notes', 'notes')); } if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) { $links[] = html_writer::link(new moodle_url('/course/user.php?id=' . $course->id . '&user='******'activity')); } if ($USER->id != $user->id && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) { $links[] = html_writer::link(new moodle_url('/course/loginas.php?id=' . $course->id . '&user='******'&sesskey=' . sesskey()), get_string('loginas')); } $links[] = html_writer::link(new moodle_url('/user/view.php?id=' . $user->id . '&course=' . $course->id), get_string('fullprofile') . '...'); $row->cells[2]->text .= implode('', $links); if ($bulkoperations) { $row->cells[2]->text .= '<br /><input type="checkbox" class="usercheckbox" name="user' . $user->id . '" /> '; } $table->data = array($row); echo html_writer::table($table); } } else { echo $OUTPUT->heading(get_string('nothingtodisplay')); } } } else {
/** * The "fixy" overlay that drops down when the link in the top right corner is clicked. It will say either * "login" or "menu" (for signed in users). * */ public function fixed_menu() { global $CFG, $USER; $logout = get_string('logout'); $isguest = isguestuser(); $courseservice = course::service(); $output = ''; if (!isloggedin() || $isguest) { $login = get_string('login'); $cancel = get_string('cancel'); if (!empty($CFG->loginpasswordautocomplete)) { $autocomplete = 'autocomplete="off"'; } else { $autocomplete = ''; } if (empty($CFG->authloginviaemail)) { $username = get_string('username'); } else { $username = get_string('usernameemail'); } if (empty($CFG->loginhttps)) { $wwwroot = $CFG->wwwroot; } else { $wwwroot = str_replace("http://", "https://", $CFG->wwwroot); } $password = get_string('password'); $loginform = get_string('loginform', 'theme_snap'); $helpstr = ''; if (empty($CFG->forcelogin) || $isguest || !isloggedin() || !empty($CFG->registerauth) || is_enabled_auth('none') || !empty($CFG->auth_instructions)) { if ($isguest) { $helpstr = '<p class="text-center">' . get_string('loggedinasguest', 'theme_snap') . '</p>'; $helpstr .= '<p class="text-center">' . '<a class="btn btn-primary" href="' . s($CFG->wwwroot) . '/login/logout.php?sesskey=' . sesskey() . '">' . $logout . '</a></p>'; $helpstr .= '<p class="text-center">' . '<a href="' . s($wwwroot) . '/login/index.php">' . get_string('helpwithloginandguest', 'theme_snap') . '</a></p>'; } else { if (empty($CFG->forcelogin)) { $help = get_string('helpwithloginandguest', 'theme_snap'); } else { $help = get_string('helpwithlogin', 'theme_snap'); } $helpstr = "<p class='text-center'><a href='" . s($wwwroot) . "/login/index.php'>{$help}</a></p>"; } } if (local::current_url_path() != '/login/index.php') { $output .= $this->login_button(); $altlogins = $this->render_login_alternative_methods(new login_alternative_methods()); $output .= "<div class='fixy' id='snap-login' role='dialog' aria-label='{$loginform}' tabindex='-1'>\n <form action='{$wwwroot}/login/index.php' method='post'>\n <div class=fixy-inner>\n <div class=fixy-header>\n <a id='fixy-close' class='js-personal-menu-trigger pull-right snap-action-icon' href='#'>\n <i class='icon icon-close'></i><small>{$cancel}</small>\n </a>\n <h1>{$login}</h1>\n </div>\n <label for='username'>{$username}</label>\n <input autocapitalize='off' type='text' name='username' id='username'>\n <label for='password'>{$password}</label>\n <input type='password' name='password' id='password' {$autocomplete}>\n <br>\n <input type='submit' value='" . s($login) . "'>\n {$helpstr}\n {$altlogins}\n </div>\n </form></div>"; } } else { $courselist = ""; $userpicture = new user_picture($USER); $userpicture->link = false; $userpicture->alttext = false; $userpicture->size = 100; $picture = $this->render($userpicture); list($favorited, $notfavorited) = $courseservice->my_courses_split_by_favorites(); // Create courses array with favorites first. $mycourses = $favorited + $notfavorited; $courselist .= '<section id="fixy-my-courses"><div class="clearfix"><h2>' . get_string('courses') . '</h2>'; $courselist .= '<div id="fixy-visible-courses">'; // Default text when no courses. if (!$mycourses) { $courselist .= "<p>" . get_string('coursefixydefaulttext', 'theme_snap') . "</p>"; } // Visible / hidden course vars. $visiblecoursecount = 0; // How many courses are in the hidden section (hidden and not favorited). $hiddencoursecount = 0; $hiddencourselist = ''; // How many courses are actually hidden. $actualhiddencount = 0; foreach ($mycourses as $course) { $ccard = new course_card($course->id); $coursecard = $this->render($ccard); // If course is not visible. if (!$course->visible) { $actualhiddencount++; // Only add to list of hidden courses if not favorited. if (!isset($favorited[$course->id])) { $hiddencoursecount++; $hiddencourselist .= $coursecard; } else { // OK, this is hidden but it's favorited, so technically visible. $visiblecoursecount++; $courselist .= $coursecard; } } else { $visiblecoursecount++; $courselist .= $coursecard; } } $courselist .= '</div>'; $courselist .= $this->browse_all_courses_button(); $courselist .= '</div>'; if ($actualhiddencount && $visiblecoursecount) { // Output hidden courses toggle when there are visible courses. $togglevisstate = !empty($hiddencourselist) ? ' state-visible' : ''; $hiddencourses = '<div class="clearfix"><h2 class="header-hidden-courses' . $togglevisstate . '"><a id="js-toggle-hidden-courses" href="#">' . get_string('hiddencoursestoggle', 'theme_snap', $hiddencoursecount) . '</a></h2>'; $hiddencourses .= '<div id="fixy-hidden-courses" class="clearfix" tabindex="-1">' . $hiddencourselist . '</div>'; $hiddencourses .= '</div>'; $courselist .= $hiddencourses; } else { if (!$visiblecoursecount && $hiddencoursecount) { $hiddencourses = '<div id="fixy-hidden-courses" class="clearfix state-visible">' . $hiddencourselist . '</div>'; $courselist .= $hiddencourses; } } $courselist .= '</section>'; $menu = get_string('menu', 'theme_snap'); $badge = $this->render_badge_count(); $linkcontent = $menu . $picture . $badge; $attributes = array('aria-haspopup' => 'true', 'class' => 'js-personal-menu-trigger snap-my-courses-menu', 'id' => 'fixy-trigger', 'aria-controls' => 'primary-nav'); $output .= html_writer::link('#', $linkcontent, $attributes); $close = get_string('close', 'theme_snap'); $viewyourprofile = get_string('viewyourprofile', 'theme_snap'); $realuserinfo = ''; if (\core\session\manager::is_loggedinas()) { $realuser = \core\session\manager::get_realuser(); $via = get_string('via', 'theme_snap'); $fullname = fullname($realuser, true); $realuserinfo = html_writer::span($via . ' ' . html_writer::span($fullname, 'real-user-name'), 'real-user-info'); } $output .= '<nav id="primary-nav" class="fixy toggle-details" tabindex="-1"> <div class="fixy-inner"> <div class="fixy-header"> <a id="fixy-close" class="js-personal-menu-trigger pull-right snap-action-icon" href="#"> <i class="icon icon-close"></i><small>' . $close . '</small> </a> <div id="fixy-user">' . $picture . ' <div id="fixy-user-details"> <a title="' . s($viewyourprofile) . '" href="' . s($CFG->wwwroot) . '/user/profile.php" >' . '<span class="h1" role="heading" aria-level="1">' . format_string(fullname($USER)) . '</span> </a> ' . $realuserinfo . ' <a id="fixy-logout" href="' . s($CFG->wwwroot) . '/login/logout.php?sesskey=' . sesskey() . '">' . $logout . '</a> </div> </div> </div> <div id="fixy-content">' . $courselist . $this->render_callstoaction() . ' </div><!-- end fixy-content --> </div><!-- end fixy-inner --> </nav><!-- end primary nav -->'; } return $output; }