Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
use core_external\external_api;
18
use core_external\external_format_value;
19
use core_external\external_function_parameters;
20
use core_external\external_multiple_structure;
21
use core_external\external_single_structure;
22
use core_external\external_value;
23
use core_external\external_warnings;
24
use core_external\util;
25
 
26
/**
27
 * External notes API
28
 *
29
 * @package    core_notes
30
 * @category   external
31
 * @copyright  2011 Jerome Mouneyrac
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
 
35
defined('MOODLE_INTERNAL') || die();
36
 
37
require_once($CFG->dirroot . "/notes/lib.php");
38
 
39
/**
40
 * Notes external functions
41
 *
42
 * @package    core_notes
43
 * @category   external
44
 * @copyright  2011 Jerome Mouneyrac
45
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
 * @since Moodle 2.2
47
 */
48
class core_notes_external extends external_api {
49
 
50
    /**
51
     * Returns description of method parameters
52
     *
53
     * @return external_function_parameters
54
     * @since Moodle 2.2
55
     */
56
    public static function create_notes_parameters() {
57
        return new external_function_parameters(
58
            array(
59
                'notes' => new external_multiple_structure(
60
                    new external_single_structure(
61
                        array(
62
                            'userid' => new external_value(PARAM_INT, 'id of the user the note is about'),
63
                            'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
64
                            'courseid' => new external_value(PARAM_INT, 'course id of the note (in Moodle a note can only be created into a course, even for site and personal notes)'),
65
                            'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
1441 ariadna 66
                            'format' => new external_format_value('text', VALUE_DEFAULT, FORMAT_MOODLE),
1 efrain 67
                            'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the note. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL),
68
                        )
69
                    )
70
                )
71
            )
72
        );
73
    }
74
 
75
    /**
76
     * Create notes about some users
77
     * Note: code should be matching the /notes/edit.php checks
78
     * and the /user/addnote.php checks. (they are similar cheks)
79
     *
80
     * @param array $notes  An array of notes to create.
81
     * @return array (success infos and fail infos)
82
     * @since Moodle 2.2
83
     */
84
    public static function create_notes($notes = array()) {
85
        global $CFG, $DB;
86
 
87
        $params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
88
 
89
        // Check if note system is enabled.
90
        if (!$CFG->enablenotes) {
91
            throw new moodle_exception('notesdisabled', 'notes');
92
        }
93
 
94
        // Retrieve all courses.
95
        $courseids = array();
96
        foreach ($params['notes'] as $note) {
97
            $courseids[] = $note['courseid'];
98
        }
99
        $courses = $DB->get_records_list("course", "id", $courseids);
100
 
101
        // Retrieve all users of the notes.
102
        $userids = array();
103
        foreach ($params['notes'] as $note) {
104
            $userids[] = $note['userid'];
105
        }
106
        list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
107
        $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
108
 
109
        $resultnotes = array();
110
        foreach ($params['notes'] as $note) {
111
 
112
            $success = true;
113
            $resultnote = array(); // The infos about the success of the operation.
114
 
115
            // Check the course exists.
116
            if (empty($courses[$note['courseid']])) {
117
                $success = false;
118
                $errormessage = get_string('invalidcourseid', 'error');
119
            } else {
120
                // Ensure the current user is allowed to run this function.
121
                $context = context_course::instance($note['courseid']);
122
                self::validate_context($context);
123
                require_capability('moodle/notes:manage', $context);
124
            }
125
 
126
            // Check the user exists.
127
            if (empty($users[$note['userid']])) {
128
                $success = false;
129
                $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
130
            }
131
 
132
            // Build the resultnote.
133
            if (isset($note['clientnoteid'])) {
134
                $resultnote['clientnoteid'] = $note['clientnoteid'];
135
            }
136
 
137
            if ($success) {
138
                // Now we can create the note.
139
                $dbnote = new stdClass;
140
                $dbnote->courseid = $note['courseid'];
141
                $dbnote->userid = $note['userid'];
142
                // Need to support 'html' and 'text' format values for backward compatibility.
143
                switch (strtolower($note['format'])) {
144
                    case 'html':
145
                        $textformat = FORMAT_HTML;
146
                        break;
147
                    case 'text':
148
                        $textformat = FORMAT_PLAIN;
1441 ariadna 149
                        break;
1 efrain 150
                    default:
151
                        $textformat = util::validate_format($note['format']);
152
                        break;
153
                }
154
                $dbnote->content = $note['text'];
155
                $dbnote->format = $textformat;
156
 
157
                // Get the state ('personal', 'course', 'site').
158
                switch ($note['publishstate']) {
159
                    case 'personal':
160
                        $dbnote->publishstate = NOTES_STATE_DRAFT;
161
                        break;
162
                    case 'course':
163
                        $dbnote->publishstate = NOTES_STATE_PUBLIC;
164
                        break;
165
                    case 'site':
166
                        $dbnote->publishstate = NOTES_STATE_SITE;
167
                        $dbnote->courseid = SITEID;
168
                        break;
169
                    default:
170
                        break;
171
                }
172
 
173
                // TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
174
                if (note_save($dbnote)) { // Note_save attribut an id in case of success.
175
                    $success = $dbnote->id;
176
                }
177
 
178
                $resultnote['noteid'] = $success;
179
            } else {
180
                // WARNINGS: for backward compatibility we return this errormessage.
181
                //          We should have thrown exceptions as these errors prevent results to be returned.
182
                // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
183
                $resultnote['noteid'] = -1;
184
                $resultnote['errormessage'] = $errormessage;
185
            }
186
 
187
            $resultnotes[] = $resultnote;
188
        }
189
 
190
        return $resultnotes;
191
    }
192
 
193
    /**
194
     * Returns description of method result value
195
     *
196
     * @return \core_external\external_description
197
     * @since Moodle 2.2
198
     */
199
    public static function create_notes_returns() {
200
        return new external_multiple_structure(
201
            new external_single_structure(
202
                array(
203
                    'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
204
                    'noteid' => new external_value(PARAM_INT, 'ID of the created note when successful, -1 when failed'),
205
                    'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
206
                )
207
            )
208
        );
209
    }
210
 
211
    /**
212
     * Returns description of delete_notes parameters
213
     *
214
     * @return external_function_parameters
215
     * @since Moodle 2.5
216
     */
217
    public static function delete_notes_parameters() {
218
        return new external_function_parameters(
219
            array(
220
                "notes"=> new external_multiple_structure(
221
                    new external_value(PARAM_INT, 'ID of the note to be deleted'), 'Array of Note Ids to be deleted.'
222
                )
223
            )
224
        );
225
    }
226
 
227
    /**
228
     * Delete notes about users.
229
     * Note: code should be matching the /notes/delete.php checks.
230
     *
231
     * @param array $notes An array of ids for the notes to delete.
232
     * @return null
233
     * @since Moodle 2.5
234
     */
235
    public static function delete_notes($notes = array()) {
236
        global $CFG;
237
 
238
        $params = self::validate_parameters(self::delete_notes_parameters(), array('notes' => $notes));
239
 
240
        // Check if note system is enabled.
241
        if (!$CFG->enablenotes) {
242
            throw new moodle_exception('notesdisabled', 'notes');
243
        }
244
        $warnings = array();
245
        foreach ($params['notes'] as $noteid) {
246
            $note = note_load($noteid);
247
            if (isset($note->id)) {
248
                // Ensure the current user is allowed to run this function.
249
                $context = context_course::instance($note->courseid);
250
                self::validate_context($context);
251
                require_capability('moodle/notes:manage', $context);
252
                note_delete($note);
253
            } else {
254
                $warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
255
            }
256
        }
257
        return $warnings;
258
    }
259
 
260
    /**
261
     * Returns description of delete_notes result value.
262
     *
263
     * @return \core_external\external_description
264
     * @since Moodle 2.5
265
     */
266
    public static function delete_notes_returns() {
267
        return  new external_warnings('item is always \'note\'',
268
                            'When errorcode is savedfailed the note could not be modified.' .
269
                            'When errorcode is badparam, an incorrect parameter was provided.' .
270
                            'When errorcode is badid, the note does not exist',
271
                            'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
272
 
273
    }
274
 
275
    /**
276
     * Returns description of get_notes parameters.
277
     *
278
     * @return external_function_parameters
279
     * @since Moodle 2.5
280
     */
281
    public static function get_notes_parameters() {
282
        return new external_function_parameters(
283
            array(
284
                "notes"=> new external_multiple_structure(
285
                    new external_value(PARAM_INT, 'ID of the note to be retrieved'), 'Array of Note Ids to be retrieved.'
286
                )
287
            )
288
        );
289
    }
290
 
291
    /**
292
     * Get notes about users.
293
     *
294
     * @param array $notes An array of ids for the notes to retrieve.
295
     * @return null
296
     * @since Moodle 2.5
297
     */
298
    public static function get_notes($notes) {
299
        global $CFG;
300
 
301
        $params = self::validate_parameters(self::get_notes_parameters(), array('notes' => $notes));
302
        // Check if note system is enabled.
303
        if (!$CFG->enablenotes) {
304
            throw new moodle_exception('notesdisabled', 'notes');
305
        }
306
        $resultnotes = array();
307
        foreach ($params['notes'] as $noteid) {
308
            $resultnote = array();
309
 
310
            $note = note_load($noteid);
311
            if (isset($note->id)) {
312
                // Ensure the current user is allowed to run this function.
313
                $context = context_course::instance($note->courseid);
314
                self::validate_context($context);
315
                require_capability('moodle/notes:view', $context);
316
                list($gotnote['text'], $gotnote['format']) = util::format_text($note->content,
317
                                                                                  $note->format,
318
                                                                                  $context->id,
319
                                                                                  'notes',
320
                                                                                  '',
321
                                                                                  '');
322
                $gotnote['noteid'] = $note->id;
323
                $gotnote['userid'] = $note->userid;
324
                $gotnote['publishstate'] = $note->publishstate;
325
                $gotnote['courseid'] = $note->courseid;
326
                $resultnotes["notes"][] = $gotnote;
327
            } else {
328
                $resultnotes["warnings"][] = array('item' => 'note',
329
                                                   'itemid' => $noteid,
330
                                                   'warningcode' => 'badid',
331
                                                   'message' => 'Note does not exist');
332
            }
333
        }
334
        return $resultnotes;
335
    }
336
 
337
    /**
338
     * Returns description of get_notes result value.
339
     *
340
     * @return \core_external\external_description
341
     * @since Moodle 2.5
342
     */
343
    public static function get_notes_returns() {
344
        return new external_single_structure(
345
            array(
346
                'notes' => new external_multiple_structure(
347
                    new external_single_structure(
348
                        array(
349
                            'noteid' => new external_value(PARAM_INT, 'id of the note', VALUE_OPTIONAL),
350
                            'userid' => new external_value(PARAM_INT, 'id of the user the note is about', VALUE_OPTIONAL),
351
                            'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\'', VALUE_OPTIONAL),
352
                            'courseid' => new external_value(PARAM_INT, 'course id of the note', VALUE_OPTIONAL),
353
                            'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML', VALUE_OPTIONAL),
354
                            'format' => new external_format_value('text', VALUE_OPTIONAL),
355
                        ), 'note'
356
                    )
357
                 ),
358
                 'warnings' => new external_warnings('item is always \'note\'',
359
                        'When errorcode is savedfailed the note could not be modified.' .
360
                        'When errorcode is badparam, an incorrect parameter was provided.' .
361
                        'When errorcode is badid, the note does not exist',
362
                        'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)')
363
            )
364
        );
365
    }
366
 
367
    /**
368
     * Returns description of update_notes parameters.
369
     *
370
     * @return external_function_parameters
371
     * @since Moodle 2.5
372
     */
373
    public static function update_notes_parameters() {
374
        return new external_function_parameters(
375
            array(
376
                'notes' => new external_multiple_structure(
377
                    new external_single_structure(
378
                        array(
379
                            'id' => new external_value(PARAM_INT, 'id of the note'),
380
                            'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
381
                            'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
382
                            'format' => new external_format_value('text', VALUE_DEFAULT),
383
                        )
384
                    ), "Array of Notes", VALUE_DEFAULT, array()
385
                )
386
            )
387
        );
388
    }
389
 
390
    /**
391
     * Update notes about users.
392
     *
393
     * @param array $notes An array of ids for the notes to update.
394
     * @return array fail infos.
395
     * @since Moodle 2.2
396
     */
397
    public static function update_notes($notes = array()) {
398
        global $CFG, $DB;
399
 
400
        $params = self::validate_parameters(self::update_notes_parameters(), array('notes' => $notes));
401
 
402
        // Check if note system is enabled.
403
        if (!$CFG->enablenotes) {
404
            throw new moodle_exception('notesdisabled', 'notes');
405
        }
406
 
407
        $warnings = array();
408
        foreach ($params['notes'] as $note) {
409
            $notedetails = note_load($note['id']);
410
            if (isset($notedetails->id)) {
411
                // Ensure the current user is allowed to run this function.
412
                $context = context_course::instance($notedetails->courseid);
413
                self::validate_context($context);
414
                require_capability('moodle/notes:manage', $context);
415
 
416
                $dbnote = new stdClass;
417
                $dbnote->id = $note['id'];
418
                $dbnote->content = $note['text'];
419
                $dbnote->format = util::validate_format($note['format']);
420
                // Get the state ('personal', 'course', 'site').
421
                switch ($note['publishstate']) {
422
                    case 'personal':
423
                        $dbnote->publishstate = NOTES_STATE_DRAFT;
424
                        break;
425
                    case 'course':
426
                        $dbnote->publishstate = NOTES_STATE_PUBLIC;
427
                        break;
428
                    case 'site':
429
                        $dbnote->publishstate = NOTES_STATE_SITE;
430
                        $dbnote->courseid = SITEID;
431
                        break;
432
                    default:
433
                        $warnings[] = array('item' => 'note',
434
                                            'itemid' => $note["id"],
435
                                            'warningcode' => 'badparam',
436
                                            'message' => 'Provided publishstate incorrect');
437
                        break;
438
                }
439
                if (!note_save($dbnote)) {
440
                    $warnings[] = array('item' => 'note',
441
                                        'itemid' => $note["id"],
442
                                        'warningcode' => 'savedfailed',
443
                                        'message' => 'Note could not be modified');
444
                }
445
            } else {
446
                $warnings[] = array('item' => 'note',
447
                                    'itemid' => $note["id"],
448
                                    'warningcode' => 'badid',
449
                                    'message' => 'Note does not exist');
450
            }
451
        }
452
        return $warnings;
453
    }
454
 
455
    /**
456
     * Returns description of update_notes result value.
457
     *
458
     * @return \core_external\external_description
459
     * @since Moodle 2.5
460
     */
461
    public static function update_notes_returns() {
462
        return new external_warnings('item is always \'note\'',
463
                            'When errorcode is savedfailed the note could not be modified.' .
464
                            'When errorcode is badparam, an incorrect parameter was provided.' .
465
                            'When errorcode is badid, the note does not exist',
466
                            'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
467
    }
468
 
469
    /**
470
     * Returns description of method parameters
471
     *
472
     * @return external_function_parameters
473
     * @since Moodle 2.9
474
     */
475
    public static function get_course_notes_parameters() {
476
        return new external_function_parameters(
477
            array(
478
                'courseid' => new external_value(PARAM_INT, 'course id, 0 for SITE'),
479
                'userid'   => new external_value(PARAM_INT, 'user id', VALUE_DEFAULT, 0),
480
            )
481
        );
482
    }
483
 
484
    /**
485
     * Create a notes list
486
     *
487
     * @param int $courseid ID of the Course
488
     * @param stdClass $context context object
489
     * @param int $userid ID of the User
490
     * @param int $state
491
     * @param int $author
492
     * @return array of notes
493
     * @since Moodle 2.9
494
     */
495
    protected static function create_note_list($courseid, $context, $userid, $state, $author = 0) {
496
        $results = [];
497
        $notes = note_list($courseid, $userid, $state, $author);
498
        foreach ($notes as $key => $note) {
499
            $note = (array)$note;
500
            [$note['content'], $note['format']] = util::format_text(
501
                $note['content'],
502
                $note['format'],
503
                $context->id,
504
                '',
505
                '',
506
 
507
            );
508
            $results[$key] = $note;
509
        }
510
        return $results;
511
    }
512
 
513
    /**
514
     * Get a list of course notes
515
     *
516
     * @param int $courseid ID of the Course
517
     * @param int $userid ID of the User
518
     * @return array of site, course and personal notes and warnings
519
     * @since Moodle 2.9
520
     * @throws moodle_exception
521
     */
522
    public static function get_course_notes($courseid, $userid = 0) {
523
        global $CFG, $USER;
524
 
525
        if (empty($CFG->enablenotes)) {
526
            throw new moodle_exception('notesdisabled', 'notes');
527
        }
528
 
529
        $warnings = array();
530
        $arrayparams = array(
531
            'courseid' => $courseid,
532
            'userid'   => $userid,
533
        );
534
        $params = self::validate_parameters(self::get_course_notes_parameters(), $arrayparams);
535
 
536
        if (empty($params['courseid'])) {
537
            $params['courseid'] = SITEID;
538
        }
539
        $user = null;
540
        if (!empty($params['userid'])) {
541
            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
542
            core_user::require_active_user($user);
543
        }
544
 
545
        $course = get_course($params['courseid']);
546
 
547
        $systemcontext = context_system::instance();
548
        $canmanagesystemnotes = has_capability('moodle/notes:manage', $systemcontext);
549
 
550
        if ($course->id == SITEID) {
551
            $context = $systemcontext;
552
            $canmanagecoursenotes = $canmanagesystemnotes;
553
        } else {
554
            $context = context_course::instance($course->id);
555
            $canmanagecoursenotes = has_capability('moodle/notes:manage', $context);
556
        }
557
        self::validate_context($context);
558
 
559
        $sitenotes = array();
560
        $coursenotes = array();
561
        $personalnotes = array();
562
 
563
        if ($course->id != SITEID) {
564
 
565
            require_capability('moodle/notes:view', $context);
566
            $sitenotes = self::create_note_list(0, $systemcontext, $params['userid'], NOTES_STATE_SITE);
567
            $coursenotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_PUBLIC);
568
            $personalnotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_DRAFT,
569
                                                        $USER->id);
570
        } else {
571
            if (has_capability('moodle/notes:view', $context)) {
572
                $sitenotes = self::create_note_list(0, $context, $params['userid'], NOTES_STATE_SITE);
573
            }
574
            // It returns notes only for a specific user!
575
            if (!empty($user)) {
576
                $usercourses = enrol_get_users_courses($user->id, true);
577
                foreach ($usercourses as $c) {
578
                    // All notes at course level, only if we have capability on every course.
579
                    if (has_capability('moodle/notes:view', context_course::instance($c->id))) {
580
                        $coursenotes += self::create_note_list($c->id, $context, $params['userid'], NOTES_STATE_PUBLIC);
581
                    }
582
                }
583
            }
584
        }
585
 
586
        $results = array(
587
            'sitenotes'     => $sitenotes,
588
            'coursenotes'   => $coursenotes,
589
            'personalnotes' => $personalnotes,
590
            'canmanagesystemnotes' => $canmanagesystemnotes,
591
            'canmanagecoursenotes' => $canmanagecoursenotes,
592
            'warnings'      => $warnings
593
        );
594
        return $results;
595
 
596
    }
597
 
598
    /**
599
     * Returns array of note structure
600
     *
601
     * @return \core_external\external_description
602
     * @since Moodle 2.9
603
     */
604
    protected static function get_note_structure() {
605
        return array(
606
                     'id'           => new external_value(PARAM_INT, 'id of this note'),
607
                     'courseid'     => new external_value(PARAM_INT, 'id of the course'),
608
                     'userid'       => new external_value(PARAM_INT, 'user id'),
609
                     'content'      => new external_value(PARAM_RAW, 'the content text formated'),
610
                     'format'       => new external_format_value('content'),
611
                     'created'      => new external_value(PARAM_INT, 'time created (timestamp)'),
612
                     'lastmodified' => new external_value(PARAM_INT, 'time of last modification (timestamp)'),
613
                     'usermodified' => new external_value(PARAM_INT, 'user id of the creator of this note'),
614
                     'publishstate' => new external_value(PARAM_ALPHA, "state of the note (i.e. draft, public, site) ")
615
        );
616
    }
617
 
618
    /**
619
     * Returns description of method result value
620
     *
621
     * @return \core_external\external_description
622
     * @since Moodle 2.9
623
     */
624
    public static function get_course_notes_returns() {
625
        return new external_single_structure(
626
            array(
627
                'sitenotes' => new external_multiple_structure(
628
                    new external_single_structure(self::get_note_structure() , ''), 'site notes', VALUE_OPTIONAL
629
                ),
630
                'coursenotes' => new external_multiple_structure(
631
                    new external_single_structure(self::get_note_structure() , ''), 'couse notes', VALUE_OPTIONAL
632
                ),
633
                'personalnotes' => new external_multiple_structure(
634
                    new external_single_structure(self::get_note_structure() , ''), 'personal notes', VALUE_OPTIONAL
635
                ),
636
                'canmanagesystemnotes' => new external_value(PARAM_BOOL, 'Whether the user can manage notes at system level.',
637
                    VALUE_OPTIONAL),
638
                'canmanagecoursenotes' => new external_value(PARAM_BOOL, 'Whether the user can manage notes at the given course.',
639
                    VALUE_OPTIONAL),
640
                'warnings' => new external_warnings()
641
            ), 'notes'
642
        );
643
    }
644
 
645
    /**
646
     * Returns description of method parameters
647
     *
648
     * @return external_function_parameters
649
     * @since Moodle 2.9
650
     */
651
    public static function view_notes_parameters() {
652
        return new external_function_parameters(
653
            array(
654
                'courseid' => new external_value(PARAM_INT, 'course id, 0 for notes at system level'),
655
                'userid' => new external_value(PARAM_INT, 'user id, 0 means view all the user notes', VALUE_DEFAULT, 0)
656
            )
657
        );
658
    }
659
 
660
    /**
661
     * Simulates the web interface view of notes/index.php: trigger events
662
     *
663
     * @param int $courseid id of the course
664
     * @param int $userid id of the user
665
     * @return array of warnings and status result
666
     * @since Moodle 2.9
667
     * @throws moodle_exception
668
     */
669
    public static function view_notes($courseid, $userid = 0) {
670
        global $CFG;
671
        require_once($CFG->dirroot . "/notes/lib.php");
672
 
673
        if (empty($CFG->enablenotes)) {
674
            throw new moodle_exception('notesdisabled', 'notes');
675
        }
676
 
677
        $warnings = array();
678
        $arrayparams = array(
679
            'courseid' => $courseid,
680
            'userid' => $userid
681
        );
682
        $params = self::validate_parameters(self::view_notes_parameters(), $arrayparams);
683
 
684
        if (empty($params['courseid'])) {
685
            $params['courseid'] = SITEID;
686
        }
687
 
688
        $course = get_course($params['courseid']);
689
 
690
        if ($course->id == SITEID) {
691
            $context = context_system::instance();
692
        } else {
693
            $context = context_course::instance($course->id);
694
        }
695
 
696
        // First of all, validate the context before do further permission checks.
697
        self::validate_context($context);
698
        require_capability('moodle/notes:view', $context);
699
 
700
        if (!empty($params['userid'])) {
701
            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
702
            core_user::require_active_user($user);
703
 
704
            if ($course->id != SITEID and !can_access_course($course, $user, '', true)) {
705
                throw new moodle_exception('notenrolledprofile');
706
            }
707
        }
708
 
709
        note_view($context, $params['userid']);
710
 
711
        $result = array();
712
        $result['status'] = true;
713
        $result['warnings'] = $warnings;
714
        return $result;
715
 
716
    }
717
 
718
    /**
719
     * Returns description of method result value
720
     *
721
     * @return \core_external\external_description
722
     * @since Moodle 2.9
723
     */
724
    public static function view_notes_returns() {
725
        return new external_single_structure(
726
            array(
727
                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
728
                'warnings' => new external_warnings()
729
            )
730
        );
731
    }
732
 
733
}