Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 cesa_notes block helper functions and callbacks
19
 *
20
 * @package    block_cesa_notes
21
 * @author     Gautam Kumar Das<gautam.arg@gmail.com>
22
 */
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
 
27
class block_cesa_notes_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_cesa_notes');
37
        $this->perpage = $this->config->cesa_notesperpage;
38
    }
39
 
40
    /**
41
     * Returns matched cesa_notes
42
     *
43
     * @param  int $page
44
     * @return array
45
     */
46
    public function get_cesa_notes($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, m.noteurl, m.modulename
69
                  FROM {block_cesa_notes} 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
        $cesa_notes = 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($u->noteurl, $u->coursename . ' - ' . $u->modulename);
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
            $c->noteurl = $u->noteurl;
95
            $c->modulename = $u->modulename;
96
            $cesa_notes[] = $c;
97
        }
98
        $rs->close();
99
        return $cesa_notes;
100
    }
101
 
102
    /**
103
     * Returns count of the cesa_notes in a table where all the given conditions met.
104
     * @return array All the notes created by user
105
     */
106
    public function get_notes_to_print() {
107
        global $USER, $DB;
108
        $notes = $DB->get_records('block_cesa_notes', ['userid' => (int) $USER->id]);
109
 
110
        if ($notes) {
111
            foreach ($notes as $note) {
112
                $course = $DB->get_record('course', ['id' => $note->courseid]);
113
                $note->coursename = $course ? $course->fullname : '';
114
                $note->timecreated = date("d-m-Y", $note->timecreated);
115
            }
116
        }
117
        return $notes;
118
    }
119
 
120
    /**
121
     * Returns count of the cesa_notes in a table where all the given conditions met.
122
     *
123
     * @param object $options
124
     * @return int The count of records
125
     */
126
    public function count_cesa_notes($options) {
127
        global $DB, $USER;
128
        $params = array();
129
        $params['userid'] = $USER->id;
130
        if (isset($options->contextarea) && !empty($options->contextarea)) {
131
            $params['contextarea'] = $options->contextarea;
132
        }
133
        if (isset($options->courseid) && !empty($options->courseid)) {
134
            $params['courseid'] = $options->courseid;
135
        }
136
        return $DB->count_records('block_cesa_notes', $params);
137
    }
138
 
139
    /*
140
     * Returns paging bar for cesa_notes
141
     *
142
     * @param object $options must contain properties(contextid, count, page, perpage)
143
     * @return html
144
     */
145
    public function get_pagination($options) {
146
        global $OUTPUT;
147
        $baseurl = new moodle_url('/blocks/cesa_notes/cesa_notes_ajax.php',
148
                array(
149
                    'contextid' => $options->contextid)
150
                );
151
        return $OUTPUT->paging_bar($options->count, $options->page, $this->perpage, $baseurl);
152
    }
153
 
154
    /*
155
     * Adding new record of mynote.
156
     *
157
     * @return object of single mynote record if insert to DB else false
158
     */
159
    public function addmynote($context, $contextarea, $course, $content, $noteurl, $moduleid, $format = FORMAT_MOODLE) {
160
        global $CFG, $DB, $USER, $OUTPUT;
161
 
162
        $newnote = new stdClass;
163
        $newnote->contextid = $context->id;
164
        $newnote->contextarea = $contextarea;
165
        $newnote->content = $content;
166
        $newnote->courseid = $course->id;
167
        $newnote->format = $format;
168
        $newnote->userid = $USER->id;
169
        $newnote->timecreated = time();
170
        $newnote->noteurl = $noteurl;
171
 
172
        $coursemodules = get_fast_modinfo($course->id);
173
        $module = false;
174
 
175
        if ($coursemodules && $moduleid) {
176
            $module = $coursemodules->get_cm($moduleid);
177
        }
178
 
179
        $newnote->modulename = $module ? $module->name : '';
180
 
181
        if ($cmtid = $DB->insert_record('block_cesa_notes', $newnote)) {
182
            $newnote->id = $cmtid;
183
            $newnote->content = format_text($newnote->content, $newnote->format, array('overflowdiv' => true));
184
            $newnote->timecreated = userdate($newnote->timecreated, get_string('strftimerecentfull', 'langconfig'));
185
            $newnote->coursename = ($newnote->courseid == SITEID && $newnote->modulename == '') ? '' : $course->fullname . ' - ' . $newnote->modulename;
186
 
187
            if (!empty($newnote->coursename)) {
188
                $newnote->coursename = html_writer::link($noteurl, $newnote->coursename);
189
            }
190
            return $newnote;
191
        } else {
192
            return false;
193
        }
194
    }/*
195
     * Editing an existing record of mynote.
196
     *
197
     * @return object of single mynote record if insert to DB else false
198
     */
199
    public function editmynote($noteid, $context, $contextarea, $course, $content, $noteurl, $moduleid, $format = FORMAT_MOODLE) {
200
        global $CFG, $DB, $USER, $OUTPUT;
201
 
202
        $newnote = new stdClass;
203
        $newnote->id = $noteid;
204
        $newnote->contextid = $context->id;
205
        $newnote->contextarea = $contextarea;
206
        $newnote->content = $content;
207
        $newnote->courseid = $course->id;
208
        $newnote->format = $format;
209
        $newnote->userid = $USER->id;
210
        $newnote->timecreated = time();
211
        $newnote->noteurl = $noteurl;
212
 
213
        $coursemodules = get_fast_modinfo($course->id);
214
        $module = false;
215
 
216
        if ($coursemodules && $moduleid) {
217
            $module = $coursemodules->get_cm($moduleid);
218
        }
219
 
220
        $newnote->modulename = $module ? $module->name : '';
221
 
222
        if ($DB->update_record('block_cesa_notes', $newnote)) {
223
            $newnote->content = format_text($newnote->content, $newnote->format, array('overflowdiv' => true));
224
            $newnote->timecreated = userdate($newnote->timecreated, get_string('strftimerecentfull', 'langconfig'));
225
            $newnote->coursename = ($newnote->courseid == SITEID && $newnote->modulename == '') ? '' : $course->fullname . ' - ' . $newnote->modulename;
226
 
227
            if (!empty($newnote->coursename)) {
228
                $newnote->coursename = html_writer::link($noteurl, $newnote->coursename);
229
            }
230
            return $newnote;
231
        } else {
232
            return false;
233
        }
234
    }
235
 
236
    /*
237
     * Find all available context areas which is used to store and retrieve cesa_notes.
238
     *
239
     * @return array
240
     */
241
    public function get_available_contextareas() {
242
        return array(
243
            'site' => get_string('site', 'block_cesa_notes'),
244
            'course' => get_string('course', 'block_cesa_notes'),
245
            'mod' => get_string('mod', 'block_cesa_notes'),
246
            'user' => get_string('user', 'block_cesa_notes'),
247
        );
248
    }
249
    /*
250
     * Find context area using context level.
251
     *
252
     * @param object $context
253
     * @retrun string
254
     */
255
    public function get_current_tab($context, $page) {
256
 
257
        if ($page->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
258
            return 'user';
259
 
260
        } else if ($page->url->compare(new moodle_url('/user/profile.php'), URL_MATCH_BASE)) {
261
            return 'user';
262
 
263
        } else if ($context->contextlevel == CONTEXT_SYSTEM) {
264
            return 'site';
265
 
266
        } else if ($context->contextlevel == CONTEXT_COURSE) {
267
            if ($context->instanceid == SITEID) {
268
                return 'site';
269
            }
270
            return 'course';
271
 
272
        } else if ($context->contextlevel == CONTEXT_MODULE) {
273
            return 'mod';
274
 
275
        } else if ($context->contextlevel == CONTEXT_USER) {
276
            return 'user';
277
 
278
        } else if ($context->contextlevel == CONTEXT_BLOCK) {
279
            $parent = $context->get_parent_context();
280
 
281
            if ($parent->contextlevel == CONTEXT_COURSE) {
282
                return 'course';
283
            } else if ($parent->contextlevel == CONTEXT_MODULE) {
284
                return 'mod';
285
            }
286
        }
287
    }
288
 
289
    /**
290
     * Delete a note
291
     *
292
     * @param  int $mynoteid
293
     * @return bool
294
     */
295
    public function delete($mynoteid) {
296
        global $DB, $USER;
297
        if (!$mynote = $DB->get_record('block_cesa_notes', array('id' => $mynoteid))) {
298
            throw new cesa_notes_exception('deletefailed', 'block_cesa_notes');
299
        }
300
        if ($USER->id != $mynote->userid) {
301
            throw new cesa_notes_exception('nopermissiontodelete', 'block_cesa_notes');
302
        }
303
        return $DB->delete_records('block_cesa_notes', array('id' => $mynoteid));
304
    }
305
}
306
 
307
/**
308
 * cesa_notes exception class
309
 */
310
class cesa_notes_exception extends moodle_exception {
311
}