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 |
/**
|
|
|
18 |
* The mynotes block helper functions and callbacks
|
|
|
19 |
*
|
|
|
20 |
* @package block_mynotes
|
|
|
21 |
* @author Gautam Kumar Das<gautam.arg@gmail.com>
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
defined('MOODLE_INTERNAL') || die();
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
class block_mynotes_manager {
|
|
|
28 |
|
|
|
29 |
public $perpage = 5;
|
|
|
30 |
private $config = null;
|
|
|
31 |
|
|
|
32 |
/*
|
|
|
33 |
* Constructor.
|
|
|
34 |
*/
|
|
|
35 |
public function __construct() {
|
|
|
36 |
$this->config = get_config('block_mynotes');
|
|
|
37 |
$this->perpage = $this->config->mynotesperpage;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Returns matched mynotes
|
|
|
42 |
*
|
|
|
43 |
* @param int $page
|
|
|
44 |
* @return array
|
|
|
45 |
*/
|
|
|
46 |
public function get_mynotes($options) {
|
|
|
47 |
global $DB, $CFG, $USER, $OUTPUT;
|
|
|
48 |
|
|
|
49 |
$page = (!isset($options->page) || !$options->page) ? 0 : $options->page;
|
|
|
50 |
$perpage = $this->perpage;
|
|
|
51 |
|
|
|
52 |
$params = array();
|
|
|
53 |
$start = $page * $perpage;
|
|
|
54 |
$ufields = 'u.id';
|
|
|
55 |
|
|
|
56 |
$where = ' m.userid = :userid';
|
|
|
57 |
$params['userid'] = $USER->id;
|
|
|
58 |
if (isset($options->contextarea) && !empty($options->contextarea)) {
|
|
|
59 |
$where .= ' AND m.contextarea = :contextarea';
|
|
|
60 |
$params['contextarea'] = $options->contextarea;
|
|
|
61 |
}
|
|
|
62 |
if (isset($options->courseid) && $options->courseid) {
|
|
|
63 |
$where .= ' AND m.courseid = :courseid';
|
|
|
64 |
$params['courseid'] = $options->courseid;
|
|
|
65 |
}
|
|
|
66 |
$sql = "SELECT $ufields,
|
|
|
67 |
m.id AS mynoteid, m.content AS ccontent, m.contextarea, m.format AS cformat,
|
|
|
68 |
m.timecreated AS timecreated, c.fullname as coursename, m.courseid
|
|
|
69 |
FROM {block_mynotes} m
|
|
|
70 |
JOIN {user} u ON u.id = m.userid
|
|
|
71 |
LEFT JOIN {course} c ON c.id = m.courseid
|
|
|
72 |
WHERE $where
|
|
|
73 |
ORDER BY m.timecreated DESC";
|
|
|
74 |
$strftime = get_string('strftimerecentfull', 'langconfig');
|
|
|
75 |
$mynotes = array();
|
|
|
76 |
$formatoptions = array('overflowdiv' => true);
|
|
|
77 |
$start = (isset($options->limitfrom)) ? $options->limitfrom : $start;
|
|
|
78 |
$rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
|
|
|
79 |
foreach ($rs as $u) {
|
|
|
80 |
$c = new stdClass();
|
|
|
81 |
$c->id = $u->mynoteid;
|
|
|
82 |
$c->userid = $u->id;
|
|
|
83 |
if ($u->courseid != SITEID) {
|
|
|
84 |
$c->coursename = html_writer::link(course_get_url($u->courseid), $u->coursename);
|
|
|
85 |
} else {
|
|
|
86 |
$c->coursename = '';
|
|
|
87 |
}
|
|
|
88 |
$c->content = $u->ccontent;
|
|
|
89 |
$c->contextarea = $u->contextarea;
|
|
|
90 |
$c->format = $u->cformat;
|
|
|
91 |
$c->timecreated = userdate($u->timecreated, $strftime);
|
|
|
92 |
$c->content = format_text($c->content, $c->format, $formatoptions);
|
|
|
93 |
$c->delete = true;
|
|
|
94 |
$mynotes[] = $c;
|
|
|
95 |
}
|
|
|
96 |
$rs->close();
|
|
|
97 |
return $mynotes;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Returns count of the mynotes in a table where all the given conditions met.
|
|
|
102 |
*
|
|
|
103 |
* @param object $options
|
|
|
104 |
* @return int The count of records
|
|
|
105 |
*/
|
|
|
106 |
public function count_mynotes($options) {
|
|
|
107 |
global $DB, $USER;
|
|
|
108 |
$params = array();
|
|
|
109 |
$params['userid'] = $USER->id;
|
|
|
110 |
if (isset($options->contextarea) && !empty($options->contextarea)) {
|
|
|
111 |
$params['contextarea'] = $options->contextarea;
|
|
|
112 |
}
|
|
|
113 |
if (isset($options->courseid) && !empty($options->courseid)) {
|
|
|
114 |
$params['courseid'] = $options->courseid;
|
|
|
115 |
}
|
|
|
116 |
return $DB->count_records('block_mynotes', $params);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/*
|
|
|
120 |
* Returns paging bar for mynotes
|
|
|
121 |
*
|
|
|
122 |
* @param object $options must contain properties(contextid, count, page, perpage)
|
|
|
123 |
* @return html
|
|
|
124 |
*/
|
|
|
125 |
public function get_pagination($options) {
|
|
|
126 |
global $OUTPUT;
|
|
|
127 |
$baseurl = new moodle_url('/blocks/mynotes/mynotes_ajax.php',
|
|
|
128 |
array(
|
|
|
129 |
'contextid' => $options->contextid)
|
|
|
130 |
);
|
|
|
131 |
return $OUTPUT->paging_bar($options->count, $options->page, $this->perpage, $baseurl);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/*
|
|
|
135 |
* Adding new record of mynote.
|
|
|
136 |
*
|
|
|
137 |
* @return object of single mynote record if insert to DB else false
|
|
|
138 |
*/
|
|
|
139 |
public function addmynote($context, $contextarea, $course, $content, $format = FORMAT_MOODLE) {
|
|
|
140 |
global $CFG, $DB, $USER, $OUTPUT;
|
|
|
141 |
|
|
|
142 |
$newnote = new stdClass;
|
|
|
143 |
$newnote->contextid = $context->id;
|
|
|
144 |
$newnote->contextarea = $contextarea;
|
|
|
145 |
$newnote->content = $content;
|
|
|
146 |
$newnote->courseid = $course->id;
|
|
|
147 |
$newnote->format = $format;
|
|
|
148 |
$newnote->userid = $USER->id;
|
|
|
149 |
$newnote->timecreated = time();
|
|
|
150 |
|
|
|
151 |
if ($cmtid = $DB->insert_record('block_mynotes', $newnote)) {
|
|
|
152 |
$newnote->id = $cmtid;
|
|
|
153 |
$newnote->content = format_text($newnote->content, $newnote->format, array('overflowdiv' => true));
|
|
|
154 |
$newnote->timecreated = userdate($newnote->timecreated, get_string('strftimerecentfull', 'langconfig'));
|
|
|
155 |
$newnote->coursename = ($newnote->courseid == SITEID) ? '' : $course->fullname;
|
|
|
156 |
if (!empty($newnote->coursename)) {
|
|
|
157 |
$newnote->coursename = html_writer::link(course_get_url($course), $newnote->coursename);
|
|
|
158 |
}
|
|
|
159 |
return $newnote;
|
|
|
160 |
} else {
|
|
|
161 |
return false;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/*
|
|
|
166 |
* Find all available context areas which is used to store and retrieve mynotes.
|
|
|
167 |
*
|
|
|
168 |
* @return array
|
|
|
169 |
*/
|
|
|
170 |
public function get_available_contextareas() {
|
|
|
171 |
return array(
|
|
|
172 |
'site' => get_string('site', 'block_mynotes'),
|
|
|
173 |
'course' => get_string('course', 'block_mynotes'),
|
|
|
174 |
'mod' => get_string('mod', 'block_mynotes'),
|
|
|
175 |
'user' => get_string('user', 'block_mynotes'),
|
|
|
176 |
);
|
|
|
177 |
}
|
|
|
178 |
/*
|
|
|
179 |
* Find context area using context level.
|
|
|
180 |
*
|
|
|
181 |
* @param object $context
|
|
|
182 |
* @retrun string
|
|
|
183 |
*/
|
|
|
184 |
public function get_current_tab($context, $page) {
|
|
|
185 |
|
|
|
186 |
if ($page->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
|
|
|
187 |
return 'user';
|
|
|
188 |
|
|
|
189 |
} else if ($page->url->compare(new moodle_url('/user/profile.php'), URL_MATCH_BASE)) {
|
|
|
190 |
return 'user';
|
|
|
191 |
|
|
|
192 |
} else if ($context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
193 |
return 'site';
|
|
|
194 |
|
|
|
195 |
} else if ($context->contextlevel == CONTEXT_COURSE) {
|
|
|
196 |
if ($context->instanceid == SITEID) {
|
|
|
197 |
return 'site';
|
|
|
198 |
}
|
|
|
199 |
return 'course';
|
|
|
200 |
|
|
|
201 |
} else if ($context->contextlevel == CONTEXT_MODULE) {
|
|
|
202 |
return 'mod';
|
|
|
203 |
|
|
|
204 |
} else if ($context->contextlevel == CONTEXT_USER) {
|
|
|
205 |
return 'user';
|
|
|
206 |
|
|
|
207 |
} else if ($context->contextlevel == CONTEXT_BLOCK) {
|
|
|
208 |
$parent = $context->get_parent_context();
|
|
|
209 |
|
|
|
210 |
if ($parent->contextlevel == CONTEXT_COURSE) {
|
|
|
211 |
return 'course';
|
|
|
212 |
} else if ($parent->contextlevel == CONTEXT_MODULE) {
|
|
|
213 |
return 'mod';
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Delete a note
|
|
|
220 |
*
|
|
|
221 |
* @param int $mynoteid
|
|
|
222 |
* @return bool
|
|
|
223 |
*/
|
|
|
224 |
public function delete($mynoteid) {
|
|
|
225 |
global $DB, $USER;
|
|
|
226 |
if (!$mynote = $DB->get_record('block_mynotes', array('id' => $mynoteid))) {
|
|
|
227 |
throw new mynotes_exception('deletefailed', 'block_mynotes');
|
|
|
228 |
}
|
|
|
229 |
if ($USER->id != $mynote->userid) {
|
|
|
230 |
throw new mynotes_exception('nopermissiontodelete', 'block_mynotes');
|
|
|
231 |
}
|
|
|
232 |
return $DB->delete_records('block_mynotes', array('id' => $mynoteid));
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Mynotes exception class
|
|
|
238 |
*/
|
|
|
239 |
class mynotes_exception extends moodle_exception {
|
|
|
240 |
}
|