Merge branch 'MDL-63528-master' of git://github.com/junpataleta/moodle
[moodle.git] / auth / classes / output / login.php
CommitLineData
1fe8e35f
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Login renderable.
19 *
13075628 20 * @package core_auth
1fe8e35f
FM
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
13075628 25namespace core_auth\output;
1fe8e35f
FM
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->libdir . '/externallib.php');
29
30use context_system;
31use help_icon;
32use moodle_url;
33use renderable;
34use renderer_base;
35use stdClass;
36use templatable;
37
38/**
39 * Login renderable class.
40 *
13075628 41 * @package core_auth
1fe8e35f
FM
42 * @copyright 2016 Frédéric Massart - FMCorz.net
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class login implements renderable, templatable {
46
47 /** @var bool Whether to auto focus the form fields. */
48 public $autofocusform;
49 /** @var bool Whether we can login as guest. */
50 public $canloginasguest;
51 /** @var bool Whether we can login by e-mail. */
52 public $canloginbyemail;
53 /** @var bool Whether we can sign-up. */
54 public $cansignup;
55 /** @var help_icon The cookies help icon. */
56 public $cookieshelpicon;
57 /** @var string The error message, if any. */
58 public $error;
59 /** @var moodle_url Forgot password URL. */
60 public $forgotpasswordurl;
61 /** @var array Additional identify providers, contains the keys 'url', 'name' and 'icon'. */
62 public $identityproviders;
63 /** @var string Login instructions, if any. */
64 public $instructions;
65 /** @var moodle_url The form action login URL. */
66 public $loginurl;
1fe8e35f
FM
67 /** @var bool Whether the username should be remembered. */
68 public $rememberusername;
69 /** @var moodle_url The sign-up URL. */
70 public $signupurl;
71 /** @var string The user name to pre-fill the form with. */
72 public $username;
73
74 /**
75 * Constructor.
76 *
77 * @param array $authsequence The enabled sequence of authentication plugins.
78 * @param string $username The username to display.
79 */
80 public function __construct(array $authsequence, $username = '') {
36d7b587 81 global $CFG;
1fe8e35f
FM
82
83 $this->username = $username;
84
85 $this->canloginasguest = $CFG->guestloginbutton and !isguestuser();
86 $this->canloginbyemail = !empty($CFG->authloginviaemail);
87 $this->cansignup = $CFG->registerauth == 'email' || !empty($CFG->registerauth);
3e24741d
AB
88 if ($CFG->rememberusername == 0) {
89 $this->cookieshelpicon = new help_icon('cookiesenabledonlysession', 'core');
36d7b587 90 $this->rememberusername = false;
3e24741d
AB
91 } else {
92 $this->cookieshelpicon = new help_icon('cookiesenabled', 'core');
36d7b587 93 $this->rememberusername = true;
3e24741d 94 }
1fe8e35f
FM
95
96 $this->autofocusform = !empty($CFG->loginpageautofocus);
1fe8e35f 97
7eb50b32
JO
98 $this->forgotpasswordurl = new moodle_url('/login/forgot_password.php');
99 $this->loginurl = new moodle_url('/login/index.php');
1fe8e35f
FM
100 $this->signupurl = new moodle_url('/login/signup.php');
101
102 // Authentication instructions.
103 $this->instructions = $CFG->auth_instructions;
104 if (is_enabled_auth('none')) {
105 $this->instructions = get_string('loginstepsnone');
106 } else if ($CFG->registerauth == 'email' && empty($this->instructions)) {
107 $this->instructions = get_string('loginsteps', 'core', 'signup.php');
108 }
109
110 // Identity providers.
627ea5b1 111 $this->identityproviders = \auth_plugin_base::get_identity_providers($authsequence);
1fe8e35f
FM
112 }
113
114 /**
115 * Set the error message.
116 *
117 * @param string $error The error message.
118 */
119 public function set_error($error) {
120 $this->error = $error;
121 }
122
123 public function export_for_template(renderer_base $output) {
627ea5b1
JP
124
125 $identityproviders = \auth_plugin_base::prepare_identity_providers_for_output($this->identityproviders, $output);
1fe8e35f
FM
126
127 $data = new stdClass();
128 $data->autofocusform = $this->autofocusform;
129 $data->canloginasguest = $this->canloginasguest;
130 $data->canloginbyemail = $this->canloginbyemail;
131 $data->cansignup = $this->cansignup;
132 $data->cookieshelpicon = $this->cookieshelpicon->export_for_template($output);
133 $data->error = $this->error;
134 $data->forgotpasswordurl = $this->forgotpasswordurl->out(false);
386f269f 135 $data->hasidentityproviders = !empty($this->identityproviders);
406b92c6 136 $data->hasinstructions = !empty($this->instructions) || $this->cansignup;
1fe8e35f
FM
137 $data->identityproviders = $identityproviders;
138 list($data->instructions, $data->instructionsformat) = external_format_text($this->instructions, FORMAT_MOODLE,
139 context_system::instance()->id);
140 $data->loginurl = $this->loginurl->out(false);
141 $data->rememberusername = $this->rememberusername;
386f269f 142 $data->signupurl = $this->signupurl->out(false);
8f8ae115 143 $data->username = $this->username;
1fe8e35f
FM
144
145 return $data;
146 }
147}