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
|
|
|
* Date condition. |
19
|
|
|
* |
20
|
|
|
* @package availability_maxviews |
21
|
|
|
* @copyright 2015 Daniel Neis Araujo |
22
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace availability_maxviews; |
26
|
|
|
|
27
|
|
|
defined('MOODLE_INTERNAL') || die(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* maxviews condition. |
31
|
|
|
* |
32
|
|
|
* @package availability_maxviews |
33
|
|
|
* @copyright 2014 The Open University |
34
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
35
|
|
|
*/ |
36
|
|
|
class condition extends \core_availability\condition { |
37
|
|
|
|
38
|
|
|
protected $viewslimit; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param \stdClass $structure Data structure from JSON decode |
44
|
|
|
* @throws \coding_exception If invalid data structure. |
45
|
|
|
*/ |
46
|
|
|
public function __construct($structure) { |
47
|
|
|
$this->viewslimit = $structure->viewslimit; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create object to be saved representing this condition. |
52
|
|
|
*/ |
53
|
|
|
public function save() { |
54
|
|
|
return (object)array('type' => 'maxviews', 'viewslimit' => $this->viewslimit); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a JSON object which corresponds to a condition of this type. |
59
|
|
|
* |
60
|
|
|
* Intended for unit testing, as normally the JSON values are constructed |
61
|
|
|
* by JavaScript code. |
62
|
|
|
* |
63
|
|
|
* @param int $viewslimit The limit of views for users |
64
|
|
|
* @return stdClass Object representing condition |
65
|
|
|
*/ |
66
|
|
|
public static function get_json($viewslimit = 5) { |
67
|
|
|
return (object)array('type' => 'maxviews', 'viewslimit' => (int)$viewslimit); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determines whether a particular item is currently available |
72
|
|
|
* according to this availability condition. |
73
|
|
|
* |
74
|
|
|
* @param bool $not Set true if we are inverting the condition |
75
|
|
|
* @param info $info Item we're checking |
76
|
|
|
* @param bool $grabthelot Performance hint: if true, caches information |
77
|
|
|
* required for all course-modules, to make the front page and similar |
78
|
|
|
* pages work more quickly (works only for current user) |
79
|
|
|
* @param int $userid User ID to check availability for |
80
|
|
|
* @return bool True if available |
81
|
|
|
*/ |
82
|
|
|
public function is_available($not, \core_availability\info $info, $grabthelot, $userid) { |
83
|
|
|
$logmanager = get_log_manager(); |
84
|
|
|
if (!$readers = $logmanager->get_readers('core\log\sql_reader')) { |
85
|
|
|
// Should be using 2.8, use old class. |
86
|
|
|
$readers = $logmanager->get_readers('core\log\sql_select_reader'); |
87
|
|
|
} |
88
|
|
|
$reader = array_pop($readers); |
89
|
|
|
$context = $info->get_context(); |
90
|
|
|
$viewscount = $reader->get_events_select_count('contextid = :context AND userid = :userid AND crud = :crud', |
91
|
|
|
array('context' => $context->id, 'userid' => $userid, 'crud' => 'r')); |
92
|
|
|
$allow = ($viewscount < $this->viewslimit); |
93
|
|
|
if ($not) { |
94
|
|
|
$allow = !$allow; |
95
|
|
|
} |
96
|
|
|
return $allow; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Obtains a string describing this restriction (whether or not |
101
|
|
|
* it actually applies). |
102
|
|
|
* |
103
|
|
|
* @param bool $full Set true if this is the 'full information' view |
104
|
|
|
* @param bool $not Set true if we are inverting the condition |
105
|
|
|
* @param info $info Item we're checking |
106
|
|
|
* @return string Information string (for admin) about all restrictions on |
107
|
|
|
* this item |
108
|
|
|
*/ |
109
|
|
|
public function get_description($full, $not, \core_availability\info $info) { |
110
|
|
|
global $USER; |
|
|
|
|
111
|
|
|
|
112
|
|
|
$logmanager = get_log_manager(); |
113
|
|
|
if (!$readers = $logmanager->get_readers('core\log\sql_reader')) { |
114
|
|
|
// Should be using 2.8, use old class. |
115
|
|
|
$readers = $logmanager->get_readers('core\log\sql_select_reader'); |
116
|
|
|
} |
117
|
|
|
$reader = array_pop($readers); |
118
|
|
|
$context = $info->get_context(); |
119
|
|
|
$viewscount = $reader->get_events_select_count('contextid = :context AND userid = :userid AND crud = :crud', |
120
|
|
|
array('context' => $context->id, 'userid' => $USER->id, 'crud' => 'r')); |
121
|
|
|
|
122
|
|
|
$a = new \stdclass(); |
123
|
|
|
$a->viewslimit = $this->viewslimit; |
124
|
|
|
$a->viewscount = $viewscount; |
125
|
|
|
|
126
|
|
|
if ($not) { |
127
|
|
|
return get_string('eithernotdescription', 'availability_maxviews', $a); |
128
|
|
|
} else { |
129
|
|
|
return get_string('eitherdescription', 'availability_maxviews', $a); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Obtains a representation of the options of this condition as a string, |
135
|
|
|
* for debugging. |
136
|
|
|
* |
137
|
|
|
* @return string Text representation of parameters |
138
|
|
|
*/ |
139
|
|
|
protected function get_debug_string() { |
140
|
|
|
return gmdate('Y-m-d H:i:s'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.