lib.php
8.63 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?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/>.
/**
* The mynotes block helper functions and callbacks
*
* @package block_mynotes
* @author Gautam Kumar Das<gautam.arg@gmail.com>
*/
defined('MOODLE_INTERNAL') || die();
class block_mynotes_manager {
public $perpage = 5;
private $config = null;
/*
* Constructor.
*/
public function __construct() {
$this->config = get_config('block_mynotes');
$this->perpage = $this->config->mynotesperpage;
}
/**
* Returns matched mynotes
*
* @param int $page
* @return array
*/
public function get_mynotes($options) {
global $DB, $CFG, $USER, $OUTPUT;
$page = (!isset($options->page) || !$options->page) ? 0 : $options->page;
$perpage = $this->perpage;
$params = array();
$start = $page * $perpage;
$ufields = 'u.id';
$where = ' m.userid = :userid';
$params['userid'] = $USER->id;
/* if (isset($options->contextarea) && !empty($options->contextarea)) {
$where .= ' AND m.contextarea = :contextarea';
$params['contextarea'] = $options->contextarea;
}
if (isset($options->courseid) && $options->courseid) {
$where .= ' AND m.courseid = :courseid';
$params['courseid'] = $options->courseid;
} */
if(isset($options->contextid) && $options->contextid){
$where .= ' AND m.contextid = :contextid';
$params['contextid'] = $options->contextid;
}
$sql = "SELECT $ufields,
m.id AS mynoteid, m.content AS ccontent, m.contextarea, m.contextid, m.format AS cformat,
m.timecreated AS timecreated, c.fullname as coursename, m.courseid
FROM {block_mynotes} m
JOIN {user} u ON u.id = m.userid
LEFT JOIN {course} c ON c.id = m.courseid
WHERE $where
ORDER BY m.timecreated DESC";
$strftime = get_string('strftimerecentfull', 'langconfig');
$mynotes = array();
$formatoptions = array('overflowdiv' => true);
$start = (isset($options->limitfrom)) ? $options->limitfrom : $start;
$rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
foreach ($rs as $u) {
$c = new stdClass();
$c->id = $u->mynoteid;
$c->userid = $u->id;
if ($u->courseid != SITEID) {
$c->coursename = html_writer::link(course_get_url($u->courseid), $u->coursename);
} else {
$c->coursename = '';
}
$c->content = $u->ccontent;
$c->contextarea = $u->contextarea;
$c->format = $u->cformat;
$c->timecreated = userdate($u->timecreated, $strftime);
$c->content = format_text($c->content, $c->format, $formatoptions);
$c->delete = true;
$mynotes[] = $c;
}
$rs->close();
return $mynotes;
}
/**
* Returns count of the mynotes in a table where all the given conditions met.
*
* @param object $options
* @return int The count of records
*/
public function count_mynotes($options) {
global $DB, $USER;
$params = array();
// $params ['contextid']= $options->contextid;
// $params= $options->contextid;
$sql='select COUNT(contextarea) from {block_mynotes} where contextid= :contextid';
$params ['contextid']= $options->contextid;
return $DB->count_records_sql($sql,$params);
/* $params['userid'] = $USER->id;
if (isset($options->contextarea) && !empty($options->contextarea)) {
$params['contextarea'] = $options->contextarea;
}
if (isset($options->contextid) && !empty($options->contextid)) {
$params['contextid'] = $options->contextid;
}
return $DB->count_records('block_mynotes',$params); */
}
/*
* Returns paging bar for mynotes
*
* @param object $options must contain properties(contextid, count, page, perpage)
* @return html
*/
public function get_pagination($options) {
global $OUTPUT;
$baseurl = new moodle_url('/blocks/mynotes/mynotes_ajax.php',
array(
'contextid' => $options->contextid)
);
return $OUTPUT->paging_bar($options->count, $options->page, $this->perpage, $baseurl);
}
/*
* Adding new record of mynote.
*
* @return object of single mynote record if insert to DB else false
*/
public function addmynote($context, $contextarea, $course, $content,$timer, $format = FORMAT_MOODLE) {
global $CFG, $DB, $USER, $OUTPUT;
$content=$content.$timer;
$newnote = new stdClass;
$newnote->contextid = $context->id;
$newnote->contextarea = $contextarea;
$newnote->content = $content;
$newnote->courseid = $course->id;
$newnote->format = $format;
$newnote->userid = $USER->id;
$newnote->timecreated = time();
if ($cmtid = $DB->insert_record('block_mynotes', $newnote)) {
$newnote->id = $cmtid;
$newnote->content = format_text($newnote->content, $newnote->format, array('overflowdiv' => true));
$newnote->timecreated = userdate($newnote->timecreated, get_string('strftimerecentfull', 'langconfig'));
$newnote->coursename = ($newnote->courseid == SITEID) ? '' : $course->fullname;
if (!empty($newnote->coursename)) {
$newnote->coursename = html_writer::link(course_get_url($course), $newnote->coursename);
}
return $newnote;
} else {
return false;
}
}
/*
* Find all available context areas which is used to store and retrieve mynotes.
*
* @return array
*/
public function get_available_contextareas() {
return array(
// 'site' => get_string('site', 'block_mynotes'),
// 'course' => get_string('course', 'block_mynotes'),
// 'mod' => get_string('mod', 'block_mynotes'),
'user' => get_string('user', 'block_mynotes'),
);
}
/*
* Find context area using context level.
*
* @param object $context
* @retrun string
*/
public function get_current_tab($context, $page) {
/* if ($page->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
return 'user';
} else if ($page->url->compare(new moodle_url('/user/profile.php'), URL_MATCH_BASE)) {
return 'user';
} else if ($context->contextlevel == CONTEXT_SYSTEM) {
return 'site';
} else if ($context->contextlevel == CONTEXT_COURSE) {
if ($context->instanceid == SITEID) {
return 'site';
}
return 'course';
} else if ($context->contextlevel == CONTEXT_MODULE) {
return 'mod';
} else if ($context->contextlevel == CONTEXT_USER) {
return 'user';
} else if ($context->contextlevel == CONTEXT_BLOCK) {
$parent = $context->get_parent_context();
if ($parent->contextlevel == CONTEXT_COURSE) {
return 'course';
} else if ($parent->contextlevel == CONTEXT_MODULE) {
return 'mod';
}
} */
return 'user';
}
/**
* Delete a note
*
* @param int $mynoteid
* @return bool
*/
public function delete($mynoteid) {
global $DB, $USER;
if (!$mynote = $DB->get_record('block_mynotes', array('id' => $mynoteid))) {
throw new mynotes_exception('deletefailed', 'block_mynotes');
}
if ($USER->id != $mynote->userid) {
throw new mynotes_exception('nopermissiontodelete', 'block_mynotes');
}
return $DB->delete_records('block_mynotes', array('id' => $mynoteid));
}
}
/**
* Mynotes exception class
*/
class mynotes_exception extends moodle_exception {
}