1 |
efrain |
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 |
namespace factor_cohort;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
require_once(__DIR__ . '/../../../../../../cohort/lib.php');
|
|
|
21 |
|
|
|
22 |
use stdClass;
|
|
|
23 |
use tool_mfa\local\factor\object_factor_base;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* cohort factor class.
|
|
|
27 |
*
|
|
|
28 |
* @package factor_cohort
|
|
|
29 |
* @author Chris Pratt <tonyyeb@gmail.com>
|
|
|
30 |
* @copyright Chris Pratt
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class factor extends object_factor_base {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* cohort implementation.
|
|
|
37 |
* This factor is a singleton, return single instance.
|
|
|
38 |
*
|
|
|
39 |
* @param stdClass $user the user to check against.
|
|
|
40 |
* @return array
|
|
|
41 |
*/
|
|
|
42 |
public function get_all_user_factors(stdClass $user): array {
|
|
|
43 |
global $DB;
|
|
|
44 |
$records = $DB->get_records('tool_mfa', ['userid' => $user->id, 'factor' => $this->name]);
|
|
|
45 |
if (!empty($records)) {
|
|
|
46 |
return $records;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// Null records returned, build new record.
|
|
|
50 |
$record = [
|
|
|
51 |
'userid' => $user->id,
|
|
|
52 |
'factor' => $this->name,
|
|
|
53 |
'timecreated' => time(),
|
|
|
54 |
'createdfromip' => $user->lastip,
|
|
|
55 |
'timemodified' => time(),
|
|
|
56 |
'revoked' => 0,
|
|
|
57 |
];
|
|
|
58 |
$record['id'] = $DB->insert_record('tool_mfa', $record, true);
|
|
|
59 |
return [(object) $record];
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* cohort implementation.
|
|
|
64 |
* Factor has no input
|
|
|
65 |
*
|
|
|
66 |
* {@inheritDoc}
|
|
|
67 |
*/
|
|
|
68 |
public function has_input(): bool {
|
|
|
69 |
return false;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* cohort implementation.
|
|
|
74 |
* Checks whether the user has selected cohorts in any context.
|
|
|
75 |
*
|
|
|
76 |
* {@inheritDoc}
|
|
|
77 |
*/
|
|
|
78 |
public function get_state(): string {
|
|
|
79 |
global $USER;
|
|
|
80 |
$cohortstring = get_config('factor_cohort', 'cohorts');
|
|
|
81 |
// Nothing selected, everyone passes.
|
|
|
82 |
if (empty($cohortstring)) {
|
|
|
83 |
return \tool_mfa\plugininfo\factor::STATE_PASS;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$selected = explode(',', $cohortstring);
|
|
|
87 |
foreach ($selected as $id) {
|
|
|
88 |
if (cohort_is_member($id, $USER->id)) {
|
|
|
89 |
return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// If we got here, no cohorts matched, allow access.
|
|
|
94 |
return \tool_mfa\plugininfo\factor::STATE_PASS;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* cohort implementation.
|
|
|
99 |
* Cannot set state, return true.
|
|
|
100 |
*
|
|
|
101 |
* @param string $state the state constant to set
|
|
|
102 |
* @return bool
|
|
|
103 |
*/
|
|
|
104 |
public function set_state(string $state): bool {
|
|
|
105 |
return true;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* cohort implementation.
|
|
|
110 |
* User can not influence. Result is whatever current state is.
|
|
|
111 |
*
|
|
|
112 |
* @param stdClass $user
|
|
|
113 |
*/
|
|
|
114 |
public function possible_states(stdClass $user): array {
|
|
|
115 |
return [$this->get_state()];
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* cohort implementation
|
|
|
120 |
* Formats the cohort list nicely.
|
|
|
121 |
*
|
|
|
122 |
* {@inheritDoc}
|
|
|
123 |
*/
|
|
|
124 |
public function get_summary_condition(): string {
|
|
|
125 |
$selectedcohorts = get_config('factor_cohort', 'cohorts');
|
|
|
126 |
if (empty($selectedcohorts)) {
|
|
|
127 |
return get_string('summarycondition', 'factor_cohort', get_string('none'));
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$selectedcohorts = $this->get_cohorts(explode(',', $selectedcohorts));
|
|
|
131 |
if (empty($selectedcohorts)) {
|
|
|
132 |
return get_string('summarycondition', 'factor_cohort', get_string('none'));
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
return get_string('summarycondition', 'factor_cohort', implode(', ', $selectedcohorts));
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Get cohorts information by given ids.
|
|
|
140 |
*
|
|
|
141 |
* @param array $selectedcohorts List of cohort ids.
|
|
|
142 |
* @return array
|
|
|
143 |
*/
|
|
|
144 |
public function get_cohorts(array $selectedcohorts): array {
|
|
|
145 |
global $DB;
|
|
|
146 |
|
|
|
147 |
[$insql, $inparams] = $DB->get_in_or_equal($selectedcohorts);
|
|
|
148 |
$sql = "SELECT id, name FROM {cohort} WHERE id $insql";
|
|
|
149 |
$cohorts = $DB->get_records_sql_menu($sql, $inparams);
|
|
|
150 |
|
|
|
151 |
return $cohorts;
|
|
|
152 |
}
|
|
|
153 |
}
|