2011-06-07 17:28:51 +08:00
< ? 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/>.
2022-12-16 11:00:58 +08:00
use core_external\external_api ;
use core_external\external_format_value ;
use core_external\external_function_parameters ;
use core_external\external_multiple_structure ;
use core_external\external_single_structure ;
use core_external\external_value ;
use core_external\external_warnings ;
use core_external\util ;
2012-01-18 10:52:25 +08:00
2011-06-07 17:28:51 +08:00
/**
* External notes API
*
2012-01-18 10:52:25 +08:00
* @ package core_notes
* @ category external
* @ copyright 2011 Jerome Mouneyrac
2011-06-07 17:28:51 +08:00
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2012-01-18 10:52:25 +08:00
2017-09-21 13:01:28 +02:00
defined ( 'MOODLE_INTERNAL' ) || die ();
2015-04-02 11:34:11 +05:30
require_once ( $CFG -> dirroot . " /notes/lib.php " );
2011-06-07 17:28:51 +08:00
2011-10-18 12:57:33 +08:00
/**
2012-01-18 10:52:25 +08:00
* Notes external functions
*
* @ package core_notes
* @ category external
* @ copyright 2011 Jerome Mouneyrac
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
* @ since Moodle 2.2
2011-10-18 12:57:33 +08:00
*/
class core_notes_external extends external_api {
2011-06-07 17:28:51 +08:00
/**
* Returns description of method parameters
2012-01-18 10:52:25 +08:00
*
2011-06-07 17:28:51 +08:00
* @ return external_function_parameters
2012-01-18 10:52:25 +08:00
* @ since Moodle 2.2
2011-06-07 17:28:51 +08:00
*/
public static function create_notes_parameters () {
return new external_function_parameters (
array (
'notes' => new external_multiple_structure (
new external_single_structure (
array (
'userid' => new external_value ( PARAM_INT , 'id of the user the note is about' ),
'publishstate' => new external_value ( PARAM_ALPHA , '\'personal\', \'course\' or \'site\'' ),
'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)' ),
'text' => new external_value ( PARAM_RAW , 'the text of the message - text or HTML' ),
2013-01-07 12:01:22 +08:00
'format' => new external_format_value ( 'text' , VALUE_DEFAULT ),
2011-06-07 17:28:51 +08:00
'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 ),
)
)
)
)
);
}
/**
* Create notes about some users
* Note : code should be matching the / notes / edit . php checks
* and the / user / addnote . php checks . ( they are similar cheks )
2012-01-18 10:52:25 +08:00
*
2011-06-08 10:11:51 +08:00
* @ param array $notes An array of notes to create .
2011-06-07 17:28:51 +08:00
* @ return array ( success infos and fail infos )
2012-01-18 10:52:25 +08:00
* @ since Moodle 2.2
2011-06-07 17:28:51 +08:00
*/
public static function create_notes ( $notes = array ()) {
global $CFG , $DB ;
$params = self :: validate_parameters ( self :: create_notes_parameters (), array ( 'notes' => $notes ));
2013-01-07 12:01:22 +08:00
// Check if note system is enabled.
2011-06-07 17:28:51 +08:00
if ( ! $CFG -> enablenotes ) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
2013-01-07 12:01:22 +08:00
// Retrieve all courses.
2011-06-07 17:28:51 +08:00
$courseids = array ();
2014-07-10 16:16:26 +08:00
foreach ( $params [ 'notes' ] as $note ) {
2011-06-07 17:28:51 +08:00
$courseids [] = $note [ 'courseid' ];
}
$courses = $DB -> get_records_list ( " course " , " id " , $courseids );
2013-01-07 12:01:22 +08:00
// Retrieve all users of the notes.
2011-06-07 17:28:51 +08:00
$userids = array ();
2014-07-10 16:16:26 +08:00
foreach ( $params [ 'notes' ] as $note ) {
2011-06-07 17:28:51 +08:00
$userids [] = $note [ 'userid' ];
}
list ( $sqluserids , $sqlparams ) = $DB -> get_in_or_equal ( $userids , SQL_PARAMS_NAMED , 'userid_' );
$users = $DB -> get_records_select ( " user " , " id " . $sqluserids . " AND deleted = 0 " , $sqlparams );
$resultnotes = array ();
foreach ( $params [ 'notes' ] as $note ) {
$success = true ;
2013-01-07 12:01:22 +08:00
$resultnote = array (); // The infos about the success of the operation.
2011-06-07 17:28:51 +08:00
2013-01-07 12:01:22 +08:00
// Check the course exists.
2011-06-07 17:28:51 +08:00
if ( empty ( $courses [ $note [ 'courseid' ]])) {
$success = false ;
2012-06-27 13:29:51 +08:00
$errormessage = get_string ( 'invalidcourseid' , 'error' );
2011-06-07 17:28:51 +08:00
} else {
2013-01-07 12:01:22 +08:00
// Ensure the current user is allowed to run this function.
2012-08-02 11:20:48 +08:00
$context = context_course :: instance ( $note [ 'courseid' ]);
2011-06-07 17:28:51 +08:00
self :: validate_context ( $context );
require_capability ( 'moodle/notes:manage' , $context );
}
2013-01-07 12:01:22 +08:00
// Check the user exists.
2011-06-07 17:28:51 +08:00
if ( empty ( $users [ $note [ 'userid' ]])) {
$success = false ;
$errormessage = get_string ( 'invaliduserid' , 'notes' , $note [ 'userid' ]);
}
2013-01-07 12:01:22 +08:00
// Build the resultnote.
2011-06-07 17:28:51 +08:00
if ( isset ( $note [ 'clientnoteid' ])) {
$resultnote [ 'clientnoteid' ] = $note [ 'clientnoteid' ];
}
if ( $success ) {
2013-01-07 12:01:22 +08:00
// Now we can create the note.
2011-06-08 10:11:51 +08:00
$dbnote = new stdClass ;
2011-06-07 17:28:51 +08:00
$dbnote -> courseid = $note [ 'courseid' ];
2011-06-08 10:11:51 +08:00
$dbnote -> userid = $note [ 'userid' ];
2012-05-31 12:31:27 +08:00
// Need to support 'html' and 'text' format values for backward compatibility.
2011-06-07 17:28:51 +08:00
switch ( strtolower ( $note [ 'format' ])) {
case 'html' :
2012-05-31 12:31:27 +08:00
$textformat = FORMAT_HTML ;
2011-06-07 17:28:51 +08:00
break ;
case 'text' :
2012-05-31 12:31:27 +08:00
$textformat = FORMAT_PLAIN ;
2011-06-07 17:28:51 +08:00
default :
2022-12-16 11:00:58 +08:00
$textformat = util :: validate_format ( $note [ 'format' ]);
2011-06-07 17:28:51 +08:00
break ;
}
2012-05-31 12:31:27 +08:00
$dbnote -> content = $note [ 'text' ];
$dbnote -> format = $textformat ;
2011-06-07 17:28:51 +08:00
2013-01-07 12:01:22 +08:00
// Get the state ('personal', 'course', 'site').
2011-06-07 17:28:51 +08:00
switch ( $note [ 'publishstate' ]) {
case 'personal' :
$dbnote -> publishstate = NOTES_STATE_DRAFT ;
break ;
case 'course' :
$dbnote -> publishstate = NOTES_STATE_PUBLIC ;
break ;
case 'site' :
$dbnote -> publishstate = NOTES_STATE_SITE ;
$dbnote -> courseid = SITEID ;
break ;
default :
break ;
}
2013-01-07 12:01:22 +08:00
// TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
if ( note_save ( $dbnote )) { // Note_save attribut an id in case of success.
2011-06-07 17:28:51 +08:00
$success = $dbnote -> id ;
}
$resultnote [ 'noteid' ] = $success ;
} else {
2012-05-31 12:31:27 +08:00
// WARNINGS: for backward compatibility we return this errormessage.
// We should have thrown exceptions as these errors prevent results to be returned.
// See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
2011-06-07 17:28:51 +08:00
$resultnote [ 'noteid' ] = - 1 ;
$resultnote [ 'errormessage' ] = $errormessage ;
}
$resultnotes [] = $resultnote ;
}
return $resultnotes ;
}
/**
* Returns description of method result value
2012-01-18 10:52:25 +08:00
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2012-01-18 10:52:25 +08:00
* @ since Moodle 2.2
2011-06-07 17:28:51 +08:00
*/
public static function create_notes_returns () {
return new external_multiple_structure (
2011-06-08 10:11:51 +08:00
new external_single_structure (
array (
'clientnoteid' => new external_value ( PARAM_ALPHANUMEXT , 'your own id for the note' , VALUE_OPTIONAL ),
2014-07-10 16:16:26 +08:00
'noteid' => new external_value ( PARAM_INT , 'ID of the created note when successful, -1 when failed' ),
2011-06-08 10:11:51 +08:00
'errormessage' => new external_value ( PARAM_TEXT , 'error message - if failed' , VALUE_OPTIONAL )
2011-06-07 17:28:51 +08:00
)
2011-06-08 10:11:51 +08:00
)
2011-06-07 17:28:51 +08:00
);
}
2013-01-07 12:01:22 +08:00
/**
* Returns description of delete_notes parameters
*
* @ return external_function_parameters
* @ since Moodle 2.5
*/
public static function delete_notes_parameters () {
return new external_function_parameters (
array (
" notes " => new external_multiple_structure (
2013-02-25 13:14:18 +08:00
new external_value ( PARAM_INT , 'ID of the note to be deleted' ), 'Array of Note Ids to be deleted.'
2013-01-07 12:01:22 +08:00
)
)
);
}
/**
* Delete notes about users .
* Note : code should be matching the / notes / delete . php checks .
*
* @ param array $notes An array of ids for the notes to delete .
* @ return null
* @ since Moodle 2.5
*/
public static function delete_notes ( $notes = array ()) {
global $CFG ;
2013-08-05 14:07:03 +08:00
$params = self :: validate_parameters ( self :: delete_notes_parameters (), array ( 'notes' => $notes ));
2013-01-07 12:01:22 +08:00
// Check if note system is enabled.
if ( ! $CFG -> enablenotes ) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
$warnings = array ();
foreach ( $params [ 'notes' ] as $noteid ) {
$note = note_load ( $noteid );
if ( isset ( $note -> id )) {
// Ensure the current user is allowed to run this function.
$context = context_course :: instance ( $note -> courseid );
self :: validate_context ( $context );
require_capability ( 'moodle/notes:manage' , $context );
2015-07-13 15:05:16 +02:00
note_delete ( $note );
2013-01-07 12:01:22 +08:00
} else {
$warnings [] = array ( 'item' => 'note' , 'itemid' => $noteid , 'warningcode' => 'badid' , 'message' => 'Note does not exist' );
}
}
return $warnings ;
}
/**
* Returns description of delete_notes result value .
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2013-01-07 12:01:22 +08:00
* @ since Moodle 2.5
*/
public static function delete_notes_returns () {
return new external_warnings ( 'item is always \'note\'' ,
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist' ,
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)' );
}
/**
* Returns description of get_notes parameters .
*
* @ return external_function_parameters
* @ since Moodle 2.5
*/
public static function get_notes_parameters () {
return new external_function_parameters (
array (
" notes " => new external_multiple_structure (
new external_value ( PARAM_INT , 'ID of the note to be retrieved' ), 'Array of Note Ids to be retrieved.'
)
)
);
}
/**
* Get notes about users .
*
* @ param array $notes An array of ids for the notes to retrieve .
* @ return null
* @ since Moodle 2.5
*/
public static function get_notes ( $notes ) {
global $CFG ;
2013-08-05 14:07:03 +08:00
$params = self :: validate_parameters ( self :: get_notes_parameters (), array ( 'notes' => $notes ));
2013-01-07 12:01:22 +08:00
// Check if note system is enabled.
if ( ! $CFG -> enablenotes ) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
$resultnotes = array ();
foreach ( $params [ 'notes' ] as $noteid ) {
$resultnote = array ();
$note = note_load ( $noteid );
if ( isset ( $note -> id )) {
// Ensure the current user is allowed to run this function.
$context = context_course :: instance ( $note -> courseid );
self :: validate_context ( $context );
require_capability ( 'moodle/notes:view' , $context );
2022-12-16 11:00:58 +08:00
list ( $gotnote [ 'text' ], $gotnote [ 'format' ]) = util :: format_text ( $note -> content ,
2014-07-10 16:16:26 +08:00
$note -> format ,
$context -> id ,
'notes' ,
'' ,
'' );
2013-01-07 12:01:22 +08:00
$gotnote [ 'noteid' ] = $note -> id ;
$gotnote [ 'userid' ] = $note -> userid ;
$gotnote [ 'publishstate' ] = $note -> publishstate ;
$gotnote [ 'courseid' ] = $note -> courseid ;
$resultnotes [ " notes " ][] = $gotnote ;
} else {
2014-07-10 16:16:26 +08:00
$resultnotes [ " warnings " ][] = array ( 'item' => 'note' ,
'itemid' => $noteid ,
'warningcode' => 'badid' ,
'message' => 'Note does not exist' );
2013-01-07 12:01:22 +08:00
}
}
return $resultnotes ;
}
/**
* Returns description of get_notes result value .
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2013-01-07 12:01:22 +08:00
* @ since Moodle 2.5
*/
public static function get_notes_returns () {
return new external_single_structure (
array (
'notes' => new external_multiple_structure (
new external_single_structure (
array (
'noteid' => new external_value ( PARAM_INT , 'id of the note' , VALUE_OPTIONAL ),
'userid' => new external_value ( PARAM_INT , 'id of the user the note is about' , VALUE_OPTIONAL ),
'publishstate' => new external_value ( PARAM_ALPHA , '\'personal\', \'course\' or \'site\'' , VALUE_OPTIONAL ),
'courseid' => new external_value ( PARAM_INT , 'course id of the note' , VALUE_OPTIONAL ),
'text' => new external_value ( PARAM_RAW , 'the text of the message - text or HTML' , VALUE_OPTIONAL ),
'format' => new external_format_value ( 'text' , VALUE_OPTIONAL ),
), 'note'
)
),
'warnings' => new external_warnings ( 'item is always \'note\'' ,
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist' ,
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)' )
)
);
}
/**
* Returns description of update_notes parameters .
*
* @ return external_function_parameters
* @ since Moodle 2.5
*/
public static function update_notes_parameters () {
return new external_function_parameters (
array (
'notes' => new external_multiple_structure (
new external_single_structure (
array (
'id' => new external_value ( PARAM_INT , 'id of the note' ),
'publishstate' => new external_value ( PARAM_ALPHA , '\'personal\', \'course\' or \'site\'' ),
'text' => new external_value ( PARAM_RAW , 'the text of the message - text or HTML' ),
'format' => new external_format_value ( 'text' , VALUE_DEFAULT ),
)
), " Array of Notes " , VALUE_DEFAULT , array ()
)
)
);
}
/**
* Update notes about users .
*
* @ param array $notes An array of ids for the notes to update .
* @ return array fail infos .
* @ since Moodle 2.2
*/
public static function update_notes ( $notes = array ()) {
global $CFG , $DB ;
$params = self :: validate_parameters ( self :: update_notes_parameters (), array ( 'notes' => $notes ));
// Check if note system is enabled.
if ( ! $CFG -> enablenotes ) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
$warnings = array ();
foreach ( $params [ 'notes' ] as $note ) {
$notedetails = note_load ( $note [ 'id' ]);
if ( isset ( $notedetails -> id )) {
// Ensure the current user is allowed to run this function.
$context = context_course :: instance ( $notedetails -> courseid );
self :: validate_context ( $context );
require_capability ( 'moodle/notes:manage' , $context );
$dbnote = new stdClass ;
$dbnote -> id = $note [ 'id' ];
$dbnote -> content = $note [ 'text' ];
2022-12-16 11:00:58 +08:00
$dbnote -> format = util :: validate_format ( $note [ 'format' ]);
2013-01-07 12:01:22 +08:00
// Get the state ('personal', 'course', 'site').
switch ( $note [ 'publishstate' ]) {
case 'personal' :
$dbnote -> publishstate = NOTES_STATE_DRAFT ;
break ;
case 'course' :
$dbnote -> publishstate = NOTES_STATE_PUBLIC ;
break ;
case 'site' :
$dbnote -> publishstate = NOTES_STATE_SITE ;
$dbnote -> courseid = SITEID ;
break ;
default :
2014-07-10 16:16:26 +08:00
$warnings [] = array ( 'item' => 'note' ,
'itemid' => $note [ " id " ],
'warningcode' => 'badparam' ,
'message' => 'Provided publishstate incorrect' );
2013-01-07 12:01:22 +08:00
break ;
}
if ( ! note_save ( $dbnote )) {
2014-07-10 16:16:26 +08:00
$warnings [] = array ( 'item' => 'note' ,
'itemid' => $note [ " id " ],
'warningcode' => 'savedfailed' ,
'message' => 'Note could not be modified' );
2013-01-07 12:01:22 +08:00
}
} else {
2014-07-10 16:16:26 +08:00
$warnings [] = array ( 'item' => 'note' ,
'itemid' => $note [ " id " ],
'warningcode' => 'badid' ,
'message' => 'Note does not exist' );
2013-01-07 12:01:22 +08:00
}
}
return $warnings ;
}
/**
* Returns description of update_notes result value .
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2013-01-07 12:01:22 +08:00
* @ since Moodle 2.5
*/
public static function update_notes_returns () {
return new external_warnings ( 'item is always \'note\'' ,
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist' ,
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)' );
}
2015-03-20 12:56:32 +01:00
/**
* Returns description of method parameters
*
* @ return external_function_parameters
* @ since Moodle 2.9
*/
public static function get_course_notes_parameters () {
return new external_function_parameters (
array (
'courseid' => new external_value ( PARAM_INT , 'course id, 0 for SITE' ),
2015-10-13 19:01:42 +01:00
'userid' => new external_value ( PARAM_INT , 'user id' , VALUE_DEFAULT , 0 ),
2015-03-20 12:56:32 +01:00
)
);
}
/**
* Create a notes list
*
* @ param int $courseid ID of the Course
* @ param stdClass $context context object
* @ param int $userid ID of the User
* @ param int $state
* @ param int $author
* @ return array of notes
* @ since Moodle 2.9
*/
protected static function create_note_list ( $courseid , $context , $userid , $state , $author = 0 ) {
2022-12-16 11:00:58 +08:00
$results = [];
2015-03-20 12:56:32 +01:00
$notes = note_list ( $courseid , $userid , $state , $author );
foreach ( $notes as $key => $note ) {
$note = ( array ) $note ;
2022-12-16 11:00:58 +08:00
[ $note [ 'content' ], $note [ 'format' ]] = util :: format_text (
$note [ 'content' ],
$note [ 'format' ],
$context -> id ,
'' ,
'' ,
0
);
2015-03-20 12:56:32 +01:00
$results [ $key ] = $note ;
}
return $results ;
}
/**
* Get a list of course notes
*
* @ param int $courseid ID of the Course
* @ param int $userid ID of the User
* @ return array of site , course and personal notes and warnings
* @ since Moodle 2.9
* @ throws moodle_exception
*/
public static function get_course_notes ( $courseid , $userid = 0 ) {
global $CFG , $USER ;
if ( empty ( $CFG -> enablenotes )) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
$warnings = array ();
$arrayparams = array (
'courseid' => $courseid ,
'userid' => $userid ,
);
$params = self :: validate_parameters ( self :: get_course_notes_parameters (), $arrayparams );
if ( empty ( $params [ 'courseid' ])) {
$params [ 'courseid' ] = SITEID ;
}
$user = null ;
if ( ! empty ( $params [ 'userid' ])) {
2015-09-23 11:00:32 +02:00
$user = core_user :: get_user ( $params [ 'userid' ], '*' , MUST_EXIST );
core_user :: require_active_user ( $user );
2015-03-20 12:56:32 +01:00
}
$course = get_course ( $params [ 'courseid' ]);
2019-01-30 17:01:48 +01:00
$systemcontext = context_system :: instance ();
$canmanagesystemnotes = has_capability ( 'moodle/notes:manage' , $systemcontext );
2015-03-20 12:56:32 +01:00
if ( $course -> id == SITEID ) {
2019-01-30 17:01:48 +01:00
$context = $systemcontext ;
$canmanagecoursenotes = $canmanagesystemnotes ;
2015-03-20 12:56:32 +01:00
} else {
$context = context_course :: instance ( $course -> id );
2019-01-30 17:01:48 +01:00
$canmanagecoursenotes = has_capability ( 'moodle/notes:manage' , $context );
2015-03-20 12:56:32 +01:00
}
self :: validate_context ( $context );
$sitenotes = array ();
$coursenotes = array ();
$personalnotes = array ();
if ( $course -> id != SITEID ) {
require_capability ( 'moodle/notes:view' , $context );
2019-01-30 17:01:48 +01:00
$sitenotes = self :: create_note_list ( 0 , $systemcontext , $params [ 'userid' ], NOTES_STATE_SITE );
2015-03-20 12:56:32 +01:00
$coursenotes = self :: create_note_list ( $course -> id , $context , $params [ 'userid' ], NOTES_STATE_PUBLIC );
$personalnotes = self :: create_note_list ( $course -> id , $context , $params [ 'userid' ], NOTES_STATE_DRAFT ,
$USER -> id );
} else {
if ( has_capability ( 'moodle/notes:view' , $context )) {
$sitenotes = self :: create_note_list ( 0 , $context , $params [ 'userid' ], NOTES_STATE_SITE );
}
// It returns notes only for a specific user!
if ( ! empty ( $user )) {
$usercourses = enrol_get_users_courses ( $user -> id , true );
foreach ( $usercourses as $c ) {
// All notes at course level, only if we have capability on every course.
if ( has_capability ( 'moodle/notes:view' , context_course :: instance ( $c -> id ))) {
$coursenotes += self :: create_note_list ( $c -> id , $context , $params [ 'userid' ], NOTES_STATE_PUBLIC );
}
}
}
}
$results = array (
'sitenotes' => $sitenotes ,
'coursenotes' => $coursenotes ,
'personalnotes' => $personalnotes ,
2019-01-30 17:01:48 +01:00
'canmanagesystemnotes' => $canmanagesystemnotes ,
'canmanagecoursenotes' => $canmanagecoursenotes ,
2015-03-20 12:56:32 +01:00
'warnings' => $warnings
);
return $results ;
}
/**
* Returns array of note structure
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2015-03-20 12:56:32 +01:00
* @ since Moodle 2.9
*/
protected static function get_note_structure () {
return array (
'id' => new external_value ( PARAM_INT , 'id of this note' ),
'courseid' => new external_value ( PARAM_INT , 'id of the course' ),
'userid' => new external_value ( PARAM_INT , 'user id' ),
'content' => new external_value ( PARAM_RAW , 'the content text formated' ),
'format' => new external_format_value ( 'content' ),
'created' => new external_value ( PARAM_INT , 'time created (timestamp)' ),
'lastmodified' => new external_value ( PARAM_INT , 'time of last modification (timestamp)' ),
'usermodified' => new external_value ( PARAM_INT , 'user id of the creator of this note' ),
'publishstate' => new external_value ( PARAM_ALPHA , " state of the note (i.e. draft, public, site) " )
);
}
/**
* Returns description of method result value
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2015-03-20 12:56:32 +01:00
* @ since Moodle 2.9
*/
public static function get_course_notes_returns () {
return new external_single_structure (
array (
2019-01-30 17:01:48 +01:00
'sitenotes' => new external_multiple_structure (
new external_single_structure ( self :: get_note_structure () , '' ), 'site notes' , VALUE_OPTIONAL
),
'coursenotes' => new external_multiple_structure (
new external_single_structure ( self :: get_note_structure () , '' ), 'couse notes' , VALUE_OPTIONAL
),
'personalnotes' => new external_multiple_structure (
new external_single_structure ( self :: get_note_structure () , '' ), 'personal notes' , VALUE_OPTIONAL
),
'canmanagesystemnotes' => new external_value ( PARAM_BOOL , 'Whether the user can manage notes at system level.' ,
VALUE_OPTIONAL ),
'canmanagecoursenotes' => new external_value ( PARAM_BOOL , 'Whether the user can manage notes at the given course.' ,
VALUE_OPTIONAL ),
'warnings' => new external_warnings ()
2015-03-20 12:56:32 +01:00
), 'notes'
);
}
2015-03-31 13:31:11 +02:00
/**
* Returns description of method parameters
*
* @ return external_function_parameters
* @ since Moodle 2.9
*/
public static function view_notes_parameters () {
return new external_function_parameters (
array (
'courseid' => new external_value ( PARAM_INT , 'course id, 0 for notes at system level' ),
'userid' => new external_value ( PARAM_INT , 'user id, 0 means view all the user notes' , VALUE_DEFAULT , 0 )
)
);
}
/**
* Simulates the web interface view of notes / index . php : trigger events
*
* @ param int $courseid id of the course
* @ param int $userid id of the user
* @ return array of warnings and status result
* @ since Moodle 2.9
* @ throws moodle_exception
*/
public static function view_notes ( $courseid , $userid = 0 ) {
global $CFG ;
require_once ( $CFG -> dirroot . " /notes/lib.php " );
if ( empty ( $CFG -> enablenotes )) {
throw new moodle_exception ( 'notesdisabled' , 'notes' );
}
$warnings = array ();
$arrayparams = array (
'courseid' => $courseid ,
'userid' => $userid
);
$params = self :: validate_parameters ( self :: view_notes_parameters (), $arrayparams );
if ( empty ( $params [ 'courseid' ])) {
$params [ 'courseid' ] = SITEID ;
}
$course = get_course ( $params [ 'courseid' ]);
if ( $course -> id == SITEID ) {
$context = context_system :: instance ();
} else {
$context = context_course :: instance ( $course -> id );
}
// First of all, validate the context before do further permission checks.
self :: validate_context ( $context );
require_capability ( 'moodle/notes:view' , $context );
if ( ! empty ( $params [ 'userid' ])) {
2015-09-23 11:00:32 +02:00
$user = core_user :: get_user ( $params [ 'userid' ], '*' , MUST_EXIST );
core_user :: require_active_user ( $user );
2015-03-31 13:31:11 +02:00
2015-09-22 15:29:49 +02:00
if ( $course -> id != SITEID and ! can_access_course ( $course , $user , '' , true )) {
2015-03-31 13:31:11 +02:00
throw new moodle_exception ( 'notenrolledprofile' );
}
}
note_view ( $context , $params [ 'userid' ]);
$result = array ();
$result [ 'status' ] = true ;
$result [ 'warnings' ] = $warnings ;
return $result ;
}
/**
* Returns description of method result value
*
2022-12-16 11:00:58 +08:00
* @ return \core_external\external_description
2015-03-31 13:31:11 +02:00
* @ since Moodle 2.9
*/
public static function view_notes_returns () {
return new external_single_structure (
array (
'status' => new external_value ( PARAM_BOOL , 'status: true if success' ),
'warnings' => new external_warnings ()
)
);
}
2011-06-07 17:28:51 +08:00
}