forked from donhinkelman/moodle-block_sharing_cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.php
163 lines (148 loc) · 4.42 KB
/
record.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Sharing Cart
*
* @package block_sharing_cart
* @copyright 2017 (C) VERSION2, INC.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_sharing_cart;
defined('MOODLE_INTERNAL') || die();
use block_sharing_cart\exception as sharing_cart_exception;
/**
* Sharing Cart record manager
*/
class record {
const TABLE = 'block_sharing_cart';
const WEIGHT_BOTTOM = 9999;
public $id = null;
public $userid = null;
public $modname = null;
public $modicon = '';
public $modtext = null;
public $ctime = null;
public $filename = null;
public $tree = '';
public $weight = 0;
public $course = 0;
public $coursefullname = '';
/**
* Constructor
*
* @param mixed $record = empty
*/
public function __construct($record = array()) {
global $USER;
foreach ((array) $record as $field => $value) {
$this->{$field} = $value;
}
// default values
$this->userid || $this->userid = $USER->id;
$this->ctime || $this->ctime = time();
}
/**
* Create record instance from record ID
*
* @param int $id
* @return record
* @throws exception
*/
public static function from_id($id) {
global $DB;
$record = $DB->get_record(self::TABLE, array('id' => $id));
if (!$record) {
throw new sharing_cart_exception('recordnotfound');
}
return new self($record);
}
/**
* Insert record
*
* @return int
* @throws exception
*/
public function insert() {
global $DB;
if (!$this->weight) {
$this->weight = self::WEIGHT_BOTTOM;
}
$this->id = $DB->insert_record(self::TABLE, $this);
if (!$this->id) {
throw new sharing_cart_exception('unexpectederror');
}
self::renumber($this->userid);
return $this->id;
}
/**
* Update record
*
* @throws exception
*/
public function update() {
global $DB;
if (!$DB->update_record(self::TABLE, $this)) {
throw new sharing_cart_exception('unexpectederror');
}
self::renumber($this->userid);
}
/**
* Delete record
*
* @throws exception
*/
public function delete() {
global $DB;
$DB->delete_records(self::TABLE, array('id' => $this->id));
self::renumber($this->userid);
}
/**
* Renumber all items sequentially
*
* @param int $userid = $USER->id
* @throws exception
* @global \moodle_database $DB
* @global \stdClass $USER
*/
public static function renumber($userid = null) {
global $DB, $USER;
if ($items = $DB->get_records(self::TABLE, array('userid' => $userid ?: $USER->id))) {
$tree = array();
foreach ($items as $it) {
if (!isset($tree[$it->tree])) {
$tree[$it->tree] = array();
}
$tree[$it->tree][] = $it;
}
foreach ($tree as $items) {
usort($items, function($lhs, $rhs) {
// keep their order if already weighted
if ($lhs->weight < $rhs->weight) {
return -1;
}
if ($lhs->weight > $rhs->weight) {
return +1;
}
// order by modtext otherwise
return strnatcasecmp($lhs->modtext, $rhs->modtext);
});
foreach ($items as $i => $it) {
$DB->set_field(self::TABLE, 'weight', 1 + $i, array('id' => $it->id));
}
}
}
}
}