forked from IOC/moodle-local_mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.class.php
More file actions
128 lines (96 loc) · 3.43 KB
/
label.class.php
File metadata and controls
128 lines (96 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
// Local mail plugin for Moodle
// Copyright © 2012,2013 Institut Obert de Catalunya
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Ths program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
defined('MOODLE_INTERNAL') || die();
class local_mail_label {
private $id;
private $userid;
private $name;
private $color;
static function create($userid, $name, $color='') {
global $DB;
assert($userid > 0);
assert(strlen($name) > 0);
assert(!$color or in_array($color, self::valid_colors()));
$record = new stdClass;
$record->userid = $userid;
$record->name = $name;
$record->color = $color;
$record->id = $DB->insert_record('local_mail_labels', $record);
return self::from_record($record);
}
static function fetch($id) {
global $DB;
$record = $DB->get_record('local_mail_labels', array('id' => $id));
if ($record) {
return self::from_record($record);
}
}
static function fetch_user($userid) {
global $DB;
$result = array();
$records = $DB->get_records('local_mail_labels', array('userid' => $userid), 'name');
foreach ($records as $record) {
$result[] = self::from_record($record);
}
return $result;
}
static function from_record($record) {
$label = new self;
$label->id = (int) $record->id;
$label->userid = (int) $record->userid;
$label->name = $record->name;
$label->color = $record->color;
return $label;
}
static function valid_colors() {
return array('red', 'orange', 'yellow', 'green', 'blue', 'purple', 'black');
}
function color() {
return $this->color;
}
function delete() {
global $DB;
$transaction = $DB->start_delegated_transaction();
$DB->delete_records('local_mail_labels', array('id' => $this->id));
$DB->delete_records('local_mail_message_labels', array('labelid' => $this->id));
$conditions = array('userid' => $this->userid, 'type' => 'label', 'item' => $this->id);
$ret = $DB->delete_records('local_mail_index', $conditions);
$transaction->allow_commit();
return $ret;
}
function id() {
return $this->id;
}
function name() {
return $this->name;
}
function save($name, $color) {
global $DB;
assert(!$color or in_array($color, self::valid_colors()));
assert(strlen($name) > 0);
$record = new stdClass;
$record->id = $this->id;
$record->name = $this->name = $name;
$record->color = $this->color = $color;
$DB->update_record('local_mail_labels', $record);
}
function userid() {
return $this->userid;
}
private function __construct() {}
}