Exemplo n.º 1
0
/**
 * This function returns the current language to use, either for a given user
 * or sitewide, or the default.
 *
 * This method is invoked in every call to get_string(), so make it performant!
 *
 * @return string
 */
function current_language()
{
    global $USER, $CFG, $SESSION;
    static $userlang, $lastlang, $instlang;
    $loggedin = $USER instanceof User && $USER->is_logged_in();
    // Retrieve & cache the user lang pref (if the user is logged in)
    if (!isset($userlang) && $loggedin) {
        $userlang = $USER->get_account_preference('lang');
        if ($userlang !== null && $userlang != 'default') {
            if (!language_installed($userlang)) {
                $USER->set_account_preference('lang', 'default');
                $userlang = 'default';
            }
        }
    }
    // Retrieve & cache the institution language (if the user is logged in)
    if (!isset($instlang) && $loggedin) {
        $instlang = get_user_institution_language();
    }
    // Retrieve the $SESSION lang (from the user selecting a language while logged-out)
    // Note that if the user selected a language while logged out, and then logs in,
    // we will have set their user account pref to match that lang, over in
    // LiveUser->authenticate().
    if (!$loggedin && is_a($SESSION, 'Session')) {
        $sesslang = $SESSION->get('lang');
    } else {
        $sesslang = null;
    }
    // Logged-in user's language preference
    if (!empty($userlang) && $userlang != 'default') {
        $lang = $userlang;
    } else {
        if (!empty($sesslang) && $sesslang != 'default') {
            $lang = $sesslang;
        } else {
            if (!empty($instlang) && $instlang != 'default') {
                $lang = $instlang;
            }
        }
    }
    // If there's no language from the user pref or the logged-out lang menu...
    if (empty($lang)) {
        $lang = !empty($CFG->lang) ? $CFG->lang : 'en.utf8';
    }
    if ($lang == $lastlang) {
        return $lang;
    }
    set_locale_for_language($lang);
    return $lastlang = $lang;
}
Exemplo n.º 2
0
 /**
  * @param array $data Parameters:
  *                    - viewid (int)
  *                    - annotationid (int)
  */
 public function __construct($data, $cron = false)
 {
     parent::__construct($data, $cron);
     $annotation = new ArtefactTypeAnnotation($this->annotationid);
     $annotationfeedback = new ArtefactTypeAnnotationfeedback($this->annotationfeedbackid);
     $this->overridemessagecontents = true;
     if ($onartefact = $annotation->get('artefact')) {
         // Feedback on artefact.
         $userid = null;
         require_once get_config('docroot') . 'artefact/lib.php';
         $artefactinstance = artefact_instance_from_id($onartefact);
         if ($artefactinstance->feedback_notify_owner()) {
             $userid = $artefactinstance->get('owner');
             $groupid = $artefactinstance->get('group');
             $institutionid = $artefactinstance->get('institution');
         }
         if (empty($this->url)) {
             $this->url = 'artefact/artefact.php?artefact=' . $onartefact . '&view=' . $this->viewid;
         }
     } else {
         if ($onview = $annotation->get('view')) {
             // Feedback on view.
             if (!($viewrecord = get_record('view', 'id', $onview))) {
                 throw new ViewNotFoundException(get_string('viewnotfound', 'error', $onview));
             }
             $userid = $viewrecord->owner;
             $groupid = $viewrecord->group;
             $institutionid = $viewrecord->institution;
             if (empty($this->url)) {
                 $this->url = 'view/view.php?id=' . $onview;
             }
         } else {
             // Something is wrong.
             throw new ViewNotFoundException(get_string('invalidannotationfeedbacklinkerror', 'artefact.annotation'));
         }
     }
     // Now fetch the users that will need to get notified about this event
     // depending on whether the page has an owner, group, or institution id set.
     if (!empty($userid)) {
         $this->users = activity_get_users($this->get_id(), array($userid));
     } else {
         if (!empty($groupid)) {
             require_once get_config('docroot') . 'lib/group.php';
             $sql = "SELECT u.*\n                    FROM {usr} u, {group_member} m, {group} g\n                    WHERE g.id = m.group\n                    AND m.member = u.id\n                    AND m.group = ?\n                    AND (g.feedbacknotify = " . GROUP_ROLES_ALL . "\n                         OR (g.feedbacknotify = " . GROUP_ROLES_NONMEMBER . " AND (m.role = 'tutor' OR m.role = 'admin'))\n                         OR (g.feedbacknotify = " . GROUP_ROLES_ADMIN . " AND m.role = 'admin')\n                        )";
             $this->users = get_records_sql_array($sql, array($groupid));
         } else {
             if (!empty($institutionid)) {
                 require_once get_config('libroot') . 'institution.php';
                 $institution = new Institution($institutionid);
                 $admins = $institution->institution_and_site_admins();
                 $this->users = get_records_sql_array("SELECT * FROM {usr} WHERE id IN (" . implode(',', $admins) . ")", array());
             }
         }
     }
     if (empty($this->users)) {
         // no one to notify - possibe if group 'feedbacknotify' is set to 0
         return;
     }
     $title = $onartefact ? $artefactinstance->get('title') : $viewrecord->title;
     $this->urltext = $title;
     $body = $annotationfeedback->get('description');
     $posttime = strftime(get_string('strftimedaydatetime'), $annotationfeedback->get('ctime'));
     // Internal
     $this->message = strip_tags(str_shorten_html($body, 200, true));
     // Seen as things like emaildigest base the message on $this->message
     // we need to set the language for the $removedbyline here based on first user.
     $user = $this->users[0];
     $lang = empty($user->lang) || $user->lang == 'default' ? get_config('lang') : $user->lang;
     // Comment deleted notification
     if ($deletedby = $annotationfeedback->get('deletedby')) {
         $this->strings = (object) array('subject' => (object) array('key' => 'annotationfeedbackdeletednotificationsubject', 'section' => 'artefact.annotation', 'args' => array($title)));
         $deletedmessage = ArtefactTypeAnnotationfeedback::deleted_by_types_description();
         $removedbyline = get_string_from_language($lang, $deletedmessage[$deletedby], 'artefact.annotation');
         $this->message = $removedbyline . ":\n" . $this->message;
         foreach ($this->users as $key => $user) {
             if (empty($user->lang) || $user->lang == 'default') {
                 // check to see if we need to show institution language
                 $instlang = get_user_institution_language($user->id);
                 $lang = empty($instlang) || $instlang == 'default' ? get_config('lang') : $instlang;
             } else {
                 $lang = $user->lang;
             }
             // For email we can send the message in the user's preferred language
             $removedbyline = get_string_from_language($lang, $deletedmessage[$deletedby], 'artefact.annotation');
             $this->users[$key]->htmlmessage = get_string_from_language($lang, 'annotationfeedbackdeletedhtml', 'artefact.annotation', hsc($title), $removedbyline, clean_html($body), get_config('wwwroot') . $this->url, hsc($title));
             $this->users[$key]->emailmessage = get_string_from_language($lang, 'annotationfeedbackdeletedtext', 'artefact.annotation', $title, $removedbyline, trim(html2text(htmlspecialchars($body))), $title, get_config('wwwroot') . $this->url);
         }
         return;
     }
     $this->strings = (object) array('subject' => (object) array('key' => 'newannotationfeedbacknotificationsubject', 'section' => 'artefact.annotation', 'args' => array($title)));
     $this->url .= '&showcomment=' . $annotationfeedback->get('id');
     // Email
     $author = $annotationfeedback->get('author');
     foreach ($this->users as $key => $user) {
         $authorname = empty($author) ? $annotationfeedback->get('authorname') : display_name($author, $user);
         if (empty($user->lang) || $user->lang == 'default') {
             // check to see if we need to show institution language
             $instlang = get_user_institution_language($user->id);
             $lang = empty($instlang) || $instlang == 'default' ? get_config('lang') : $instlang;
         } else {
             $lang = $user->lang;
         }
         $this->users[$key]->htmlmessage = get_string_from_language($lang, 'annotationfeedbacknotificationhtml', 'artefact.annotation', hsc($authorname), hsc($title), $posttime, clean_html($body), get_config('wwwroot') . $this->url);
         $this->users[$key]->emailmessage = get_string_from_language($lang, 'annotationfeedbacknotificationtext', 'artefact.annotation', $authorname, $title, $posttime, trim(html2text(htmlspecialchars($body))), get_config('wwwroot') . $this->url);
     }
 }
Exemplo n.º 3
0
function general_account_prefs_form_elements($prefs)
{
    global $USER;
    require_once 'license.php';
    $elements = array();
    $elements['friendscontrol'] = array('type' => 'radio', 'defaultvalue' => $prefs->friendscontrol, 'title' => get_string('friendsdescr', 'account'), 'class' => 'mrs mls', 'options' => array('nobody' => get_string('friendsnobody', 'account'), 'auth' => get_string('friendsauth', 'account'), 'auto' => get_string('friendsauto', 'account')), 'help' => true);
    $elements['wysiwyg'] = array('type' => 'switchbox', 'defaultvalue' => get_config('wysiwyg') ? get_config('wysiwyg') == 'enable' : $prefs->wysiwyg, 'title' => get_string('wysiwygdescr', 'account'), 'help' => true, 'disabled' => get_config('wysiwyg'));
    if (get_config('licensemetadata')) {
        $elements['licensedefault'] = license_form_el_basic(null);
        $elements['licensedefault']['title'] = get_string('licensedefault', 'account');
        if ($USER->get('institutions')) {
            $elements['licensedefault']['options'][LICENSE_INSTITUTION_DEFAULT] = get_string('licensedefaultinherit', 'account');
        }
        $elements['licensedefault']['description'] = get_string('licensedefaultdescription', 'account');
        if (isset($prefs->licensedefault)) {
            $elements['licensedefault']['defaultvalue'] = $prefs->licensedefault;
        }
    }
    $elements['maildisabled'] = array('type' => 'switchbox', 'defaultvalue' => $prefs->maildisabled, 'title' => get_string('disableemail', 'account'), 'help' => true);
    $elements['messages'] = array('type' => 'radio', 'defaultvalue' => $prefs->messages, 'title' => get_string('messagesdescr', 'account'), 'options' => array('nobody' => get_string('messagesnobody', 'account'), 'friends' => get_string('messagesfriends', 'account'), 'allow' => get_string('messagesallow', 'account')), 'help' => true);
    $languages = get_languages();
    // Determine default language.
    $instlang = get_user_institution_language($USER->id, $instlanginstname);
    if (!empty($instlang) && $instlang != 'default') {
        $sitedefaultlabel = get_string('defaultlangforinstitution', 'admin', get_config_institution($instlanginstname, 'displayname')) . ' (' . $languages[$instlang] . ')';
    } else {
        $sitedefaultlabel = get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')';
    }
    $elements['lang'] = array('type' => 'select', 'defaultvalue' => $prefs->lang, 'title' => get_string('language', 'account'), 'options' => array_merge(array('default' => $sitedefaultlabel), $languages), 'help' => true, 'ignore' => count($languages) < 2);
    $sitethemes = array();
    // Get all available standard site themes
    if (get_config('sitethemeprefs') && !in_admin_section()) {
        // get_user_accessible_themes() returns 'sitedefault' to mean fall back to the site or
        // institution theme.  This won't work for account prefs, where 'sitedefault' is just
        // a theme that doesn't exist.  So change the 'sitedefault' key to '', and the empty
        // preference will be interpreted as "No theme selected".
        $sitethemes = array_reverse(get_user_accessible_themes());
        unset($sitethemes['sitedefault']);
        $sitethemes = array_reverse($sitethemes);
    }
    // Get all user's institution themes
    $institutionthemes = array();
    if ($institutions = $USER->get('institutions')) {
        $allthemes = get_all_theme_objects();
        foreach ($institutions as $i) {
            if (empty($i->theme)) {
                $institutionthemes['sitedefault' . '/' . $i->institution] = $i->displayname . ' - ' . get_string('sitedefault', 'admin');
            } else {
                $institutionthemes[$i->theme . '/' . $i->institution] = $i->displayname . ' - ' . $allthemes[$i->theme]->displayname;
            }
        }
    }
    $themes = array_merge($sitethemes, $institutionthemes);
    natcasesort($themes);
    $currenttheme = $USER->get_themedata();
    if (!isset($currenttheme->basename)) {
        $defaulttheme = 'sitedefault';
    } else {
        $defaulttheme = $currenttheme->basename;
    }
    if (isset($currenttheme->institutionname)) {
        $defaulttheme = $defaulttheme . '/' . $currenttheme->institutionname;
    }
    if (!array_key_exists($defaulttheme, $themes)) {
        reset($themes);
        $defaulttheme = key($themes);
    }
    $elements['theme'] = array('type' => 'select', 'defaultvalue' => $defaulttheme, 'title' => get_string('theme'), 'options' => $themes, 'ignore' => count($themes) < 2, 'help' => true);
    $elements['addremovecolumns'] = array('type' => 'switchbox', 'defaultvalue' => $prefs->addremovecolumns, 'title' => get_string('showviewcolumns', 'account'), 'help' => 'true');
    // TODO: add a way for plugins (like blog!) to have account preferences
    $elements['multipleblogs'] = array('type' => 'switchbox', 'title' => get_string('enablemultipleblogs1', 'account'), 'description' => get_string('enablemultipleblogsdescription1', 'account'), 'defaultvalue' => $prefs->multipleblogs);
    if (get_config('showtagssideblock')) {
        $elements['tagssideblockmaxtags'] = array('type' => 'text', 'size' => 4, 'title' => get_string('tagssideblockmaxtags', 'account'), 'description' => get_string('tagssideblockmaxtagsdescription', 'account'), 'defaultvalue' => isset($prefs->tagssideblockmaxtags) ? $prefs->tagssideblockmaxtags : get_config('tagssideblockmaxtags'), 'rules' => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000));
    }
    $elements['groupsideblockmaxgroups'] = array('type' => 'text', 'size' => 4, 'title' => get_string('limitto1', 'blocktype.mygroups'), 'description' => get_string('limittodescsideblock1', 'blocktype.mygroups'), 'defaultvalue' => isset($prefs->groupsideblockmaxgroups) ? $prefs->groupsideblockmaxgroups : '', 'rules' => array('regex' => '/^[0-9]*$/', 'minvalue' => 0, 'maxvalue' => 1000));
    $elements['groupsideblocksortby'] = array('type' => 'select', 'defaultvalue' => isset($prefs->groupsideblocksortby) ? $prefs->groupsideblocksortby : 'alphabetical', 'title' => get_string('sortgroups', 'blocktype.mygroups'), 'options' => array('latest' => get_string('latest', 'blocktype.mygroups'), 'earliest' => get_string('earliest', 'blocktype.mygroups'), 'alphabetical' => get_string('alphabetical', 'blocktype.mygroups')));
    if (get_config('userscanhiderealnames')) {
        $elements['hiderealname'] = array('type' => 'switchbox', 'title' => get_string('hiderealname', 'account'), 'description' => get_string('hiderealnamedescription', 'account'), 'defaultvalue' => $prefs->hiderealname);
    }
    if (get_config('homepageinfo')) {
        $elements['showhomeinfo'] = array('type' => 'switchbox', 'defaultvalue' => $prefs->showhomeinfo, 'title' => get_string('showhomeinfo2', 'account'), 'description' => get_string('showhomeinfodescription1', 'account', hsc(get_config('sitename'))), 'help' => 'true');
    }
    if (get_config('showprogressbar')) {
        $elements['showprogressbar'] = array('type' => 'switchbox', 'defaultvalue' => $prefs->showprogressbar, 'title' => get_string('showprogressbar', 'account'), 'description' => get_string('showprogressbardescription', 'account', hsc(get_config('sitename'))));
    }
    if (get_config('allowmobileuploads')) {
        $defaultvalue = array();
        $mobileuploadtoken = isset($prefs->mobileuploadtoken) ? $prefs->mobileuploadtoken : get_config('mobileuploadtoken');
        $defaultvalue = explode('|', trim($mobileuploadtoken, '|'));
        $elements['mobileuploadtoken'] = array('type' => 'multitext', 'title' => get_string('mobileuploadtoken', 'account'), 'defaultvalue' => $defaultvalue, 'help' => 'true');
    }
    if (get_config_plugin('artefact', 'file', 'resizeonuploadenable')) {
        $elements['resizeonuploaduserdefault'] = array('type' => 'switchbox', 'title' => get_string('resizeonuploaduserdefault1', 'account'), 'description' => get_string('resizeonuploaduserdefaultdescription2', 'account'), 'defaultvalue' => $prefs->resizeonuploaduserdefault);
    }
    if (get_config('userscandisabledevicedetection')) {
        $elements['devicedetection'] = array('type' => 'switchbox', 'title' => get_string('devicedetection', 'account'), 'description' => get_string('devicedetectiondescription', 'account'), 'defaultvalue' => $prefs->devicedetection);
    }
    return $elements;
}